blob: 5c4547d08da05f380dc84cddae46a6e217ff35c3 [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 Moolenaarbc4c5052020-08-13 22:47:35 +0200305 semsg(_(e_name_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.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200377 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 */
379 static int
380may_generate_2STRING(int offset, cctx_T *cctx)
381{
382 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 garray_T *stack = &cctx->ctx_type_stack;
385 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
386
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200387 switch ((*type)->tt_type)
388 {
389 // nothing to be done
390 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200392 // conversion possible
393 case VAR_SPECIAL:
394 case VAR_BOOL:
395 case VAR_NUMBER:
396 case VAR_FLOAT:
397 break;
398
399 // conversion possible (with runtime check)
400 case VAR_ANY:
401 case VAR_UNKNOWN:
402 isntype = ISN_2STRING_ANY;
403 break;
404
405 // conversion not possible
406 case VAR_VOID:
407 case VAR_BLOB:
408 case VAR_FUNC:
409 case VAR_PARTIAL:
410 case VAR_LIST:
411 case VAR_DICT:
412 case VAR_JOB:
413 case VAR_CHANNEL:
414 to_string_error((*type)->tt_type);
415 return FAIL;
416 }
417
418 *type = &t_string;
419 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 return FAIL;
421 isn->isn_arg.number = offset;
422
423 return OK;
424}
425
426 static int
427check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
428{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200429 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200431 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 {
433 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200434 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 else
436 semsg(_("E1036: %c requires number or float arguments"), *op);
437 return FAIL;
438 }
439 return OK;
440}
441
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200442 static int
443generate_add_instr(
444 cctx_T *cctx,
445 vartype_T vartype,
446 type_T *type1,
447 type_T *type2)
448{
449 isn_T *isn = generate_instr_drop(cctx,
450 vartype == VAR_NUMBER ? ISN_OPNR
451 : vartype == VAR_LIST ? ISN_ADDLIST
452 : vartype == VAR_BLOB ? ISN_ADDBLOB
453#ifdef FEAT_FLOAT
454 : vartype == VAR_FLOAT ? ISN_OPFLOAT
455#endif
456 : ISN_OPANY, 1);
457
458 if (vartype != VAR_LIST && vartype != VAR_BLOB
459 && type1->tt_type != VAR_ANY
460 && type2->tt_type != VAR_ANY
461 && check_number_or_float(
462 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
463 return FAIL;
464
465 if (isn != NULL)
466 isn->isn_arg.op.op_type = EXPR_ADD;
467 return isn == NULL ? FAIL : OK;
468}
469
470/*
471 * Get the type to use for an instruction for an operation on "type1" and
472 * "type2". If they are matching use a type-specific instruction. Otherwise
473 * fall back to runtime type checking.
474 */
475 static vartype_T
476operator_type(type_T *type1, type_T *type2)
477{
478 if (type1->tt_type == type2->tt_type
479 && (type1->tt_type == VAR_NUMBER
480 || type1->tt_type == VAR_LIST
481#ifdef FEAT_FLOAT
482 || type1->tt_type == VAR_FLOAT
483#endif
484 || type1->tt_type == VAR_BLOB))
485 return type1->tt_type;
486 return VAR_ANY;
487}
488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489/*
490 * Generate an instruction with two arguments. The instruction depends on the
491 * type of the arguments.
492 */
493 static int
494generate_two_op(cctx_T *cctx, char_u *op)
495{
496 garray_T *stack = &cctx->ctx_type_stack;
497 type_T *type1;
498 type_T *type2;
499 vartype_T vartype;
500 isn_T *isn;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_OK_IF_SKIP(cctx);
503
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200504 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
506 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200507 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508
509 switch (*op)
510 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200511 case '+':
512 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 break;
515
516 case '-':
517 case '*':
518 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
519 op) == FAIL)
520 return FAIL;
521 if (vartype == VAR_NUMBER)
522 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
523#ifdef FEAT_FLOAT
524 else if (vartype == VAR_FLOAT)
525 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
526#endif
527 else
528 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
529 if (isn != NULL)
530 isn->isn_arg.op.op_type = *op == '*'
531 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
532 break;
533
Bram Moolenaar4c683752020-04-05 21:38:23 +0200534 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200536 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 && type2->tt_type != VAR_NUMBER))
538 {
539 emsg(_("E1035: % requires number arguments"));
540 return FAIL;
541 }
542 isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
544 if (isn != NULL)
545 isn->isn_arg.op.op_type = EXPR_REM;
546 break;
547 }
548
549 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 {
552 type_T *type = &t_any;
553
554#ifdef FEAT_FLOAT
555 // float+number and number+float results in float
556 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
557 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
558 type = &t_float;
559#endif
560 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
561 }
562
563 return OK;
564}
565
566/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200567 * Get the instruction to use for comparing "type1" with "type2"
568 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200570 static isntype_T
571get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572{
573 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574
Bram Moolenaar4c683752020-04-05 21:38:23 +0200575 if (type1 == VAR_UNKNOWN)
576 type1 = VAR_ANY;
577 if (type2 == VAR_UNKNOWN)
578 type2 = VAR_ANY;
579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (type1 == type2)
581 {
582 switch (type1)
583 {
584 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
585 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
586 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
587 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
588 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
589 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
590 case VAR_LIST: isntype = ISN_COMPARELIST; break;
591 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
592 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 default: isntype = ISN_COMPAREANY; break;
594 }
595 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200596 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
598 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
599 isntype = ISN_COMPAREANY;
600
601 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
602 && (isntype == ISN_COMPAREBOOL
603 || isntype == ISN_COMPARESPECIAL
604 || isntype == ISN_COMPARENR
605 || isntype == ISN_COMPAREFLOAT))
606 {
607 semsg(_("E1037: Cannot use \"%s\" with %s"),
608 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200609 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 }
611 if (isntype == ISN_DROP
612 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
613 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
614 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
615 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
616 && exptype != EXPR_IS && exptype != EXPR_ISNOT
617 && (type1 == VAR_BLOB || type2 == VAR_BLOB
618 || type1 == VAR_LIST || type2 == VAR_LIST))))
619 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100620 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200624 return isntype;
625}
626
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200627 int
628check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
629{
630 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
631 return FAIL;
632 return OK;
633}
634
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635/*
636 * Generate an ISN_COMPARE* instruction with a boolean result.
637 */
638 static int
639generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
640{
641 isntype_T isntype;
642 isn_T *isn;
643 garray_T *stack = &cctx->ctx_type_stack;
644 vartype_T type1;
645 vartype_T type2;
646
647 RETURN_OK_IF_SKIP(cctx);
648
649 // Get the known type of the two items on the stack. If they are matching
650 // use a type-specific instruction. Otherwise fall back to runtime type
651 // checking.
652 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
653 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
654 isntype = get_compare_isn(exptype, type1, type2);
655 if (isntype == ISN_DROP)
656 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 if ((isn = generate_instr(cctx, isntype)) == NULL)
659 return FAIL;
660 isn->isn_arg.op.op_type = exptype;
661 isn->isn_arg.op.op_ic = ic;
662
663 // takes two arguments, puts one bool back
664 if (stack->ga_len >= 2)
665 {
666 --stack->ga_len;
667 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
668 }
669
670 return OK;
671}
672
673/*
674 * Generate an ISN_2BOOL instruction.
675 */
676 static int
677generate_2BOOL(cctx_T *cctx, int invert)
678{
679 isn_T *isn;
680 garray_T *stack = &cctx->ctx_type_stack;
681
Bram Moolenaar080457c2020-03-03 21:53:32 +0100682 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
684 return FAIL;
685 isn->isn_arg.number = invert;
686
687 // type becomes bool
688 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
689
690 return OK;
691}
692
693 static int
694generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
695{
696 isn_T *isn;
697 garray_T *stack = &cctx->ctx_type_stack;
698
Bram Moolenaar080457c2020-03-03 21:53:32 +0100699 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
701 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200702 // TODO: whole type, e.g. for a function also arg and return types
703 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 isn->isn_arg.type.ct_off = offset;
705
706 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200707 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708
709 return OK;
710}
711
712/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200713 * Check that
714 * - "actual" is "expected" type or
715 * - "actual" is a type that can be "expected" type: add a runtime check; or
716 * - return FAIL.
717 */
718 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200719need_type(
720 type_T *actual,
721 type_T *expected,
722 int offset,
723 cctx_T *cctx,
724 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200725{
726 if (check_type(expected, actual, FALSE) == OK)
727 return OK;
728 if (actual->tt_type != VAR_ANY
729 && actual->tt_type != VAR_UNKNOWN
730 && !(actual->tt_type == VAR_FUNC
731 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
732 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200733 if (!silent)
734 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200735 return FAIL;
736 }
737 generate_TYPECHECK(cctx, expected, offset);
738 return OK;
739}
740
741/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 * Generate an ISN_PUSHNR instruction.
743 */
744 static int
745generate_PUSHNR(cctx_T *cctx, varnumber_T number)
746{
747 isn_T *isn;
748
Bram Moolenaar080457c2020-03-03 21:53:32 +0100749 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
751 return FAIL;
752 isn->isn_arg.number = number;
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_PUSHBOOL instruction.
759 */
760 static int
761generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
762{
763 isn_T *isn;
764
Bram Moolenaar080457c2020-03-03 21:53:32 +0100765 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
767 return FAIL;
768 isn->isn_arg.number = number;
769
770 return OK;
771}
772
773/*
774 * Generate an ISN_PUSHSPEC instruction.
775 */
776 static int
777generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
778{
779 isn_T *isn;
780
Bram Moolenaar080457c2020-03-03 21:53:32 +0100781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
783 return FAIL;
784 isn->isn_arg.number = number;
785
786 return OK;
787}
788
789#ifdef FEAT_FLOAT
790/*
791 * Generate an ISN_PUSHF instruction.
792 */
793 static int
794generate_PUSHF(cctx_T *cctx, float_T fnumber)
795{
796 isn_T *isn;
797
Bram Moolenaar080457c2020-03-03 21:53:32 +0100798 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
800 return FAIL;
801 isn->isn_arg.fnumber = fnumber;
802
803 return OK;
804}
805#endif
806
807/*
808 * Generate an ISN_PUSHS instruction.
809 * Consumes "str".
810 */
811 static int
812generate_PUSHS(cctx_T *cctx, char_u *str)
813{
814 isn_T *isn;
815
Bram Moolenaar080457c2020-03-03 21:53:32 +0100816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
818 return FAIL;
819 isn->isn_arg.string = str;
820
821 return OK;
822}
823
824/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100825 * Generate an ISN_PUSHCHANNEL instruction.
826 * Consumes "channel".
827 */
828 static int
829generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
830{
831 isn_T *isn;
832
Bram Moolenaar080457c2020-03-03 21:53:32 +0100833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100834 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
835 return FAIL;
836 isn->isn_arg.channel = channel;
837
838 return OK;
839}
840
841/*
842 * Generate an ISN_PUSHJOB instruction.
843 * Consumes "job".
844 */
845 static int
846generate_PUSHJOB(cctx_T *cctx, job_T *job)
847{
848 isn_T *isn;
849
Bram Moolenaar080457c2020-03-03 21:53:32 +0100850 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100851 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100852 return FAIL;
853 isn->isn_arg.job = job;
854
855 return OK;
856}
857
858/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 * Generate an ISN_PUSHBLOB instruction.
860 * Consumes "blob".
861 */
862 static int
863generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
864{
865 isn_T *isn;
866
Bram Moolenaar080457c2020-03-03 21:53:32 +0100867 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
869 return FAIL;
870 isn->isn_arg.blob = blob;
871
872 return OK;
873}
874
875/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100876 * Generate an ISN_PUSHFUNC instruction with name "name".
877 * Consumes "name".
878 */
879 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200880generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100881{
882 isn_T *isn;
883
Bram Moolenaar080457c2020-03-03 21:53:32 +0100884 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200885 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100886 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200887 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100888
889 return OK;
890}
891
892/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200893 * Generate an ISN_GETITEM instruction with "index".
894 */
895 static int
896generate_GETITEM(cctx_T *cctx, int index)
897{
898 isn_T *isn;
899 garray_T *stack = &cctx->ctx_type_stack;
900 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
901 type_T *item_type = &t_any;
902
903 RETURN_OK_IF_SKIP(cctx);
904
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200905 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200906 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200907 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200908 emsg(_(e_listreq));
909 return FAIL;
910 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200911 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
913 return FAIL;
914 isn->isn_arg.number = index;
915
916 // add the item type to the type stack
917 if (ga_grow(stack, 1) == FAIL)
918 return FAIL;
919 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
920 ++stack->ga_len;
921 return OK;
922}
923
924/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200925 * Generate an ISN_SLICE instruction with "count".
926 */
927 static int
928generate_SLICE(cctx_T *cctx, int count)
929{
930 isn_T *isn;
931
932 RETURN_OK_IF_SKIP(cctx);
933 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
934 return FAIL;
935 isn->isn_arg.number = count;
936 return OK;
937}
938
939/*
940 * Generate an ISN_CHECKLEN instruction with "min_len".
941 */
942 static int
943generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
944{
945 isn_T *isn;
946
947 RETURN_OK_IF_SKIP(cctx);
948
949 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
950 return FAIL;
951 isn->isn_arg.checklen.cl_min_len = min_len;
952 isn->isn_arg.checklen.cl_more_OK = more_OK;
953
954 return OK;
955}
956
957/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 * Generate an ISN_STORE instruction.
959 */
960 static int
961generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
962{
963 isn_T *isn;
964
Bram Moolenaar080457c2020-03-03 21:53:32 +0100965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
967 return FAIL;
968 if (name != NULL)
969 isn->isn_arg.string = vim_strsave(name);
970 else
971 isn->isn_arg.number = idx;
972
973 return OK;
974}
975
976/*
977 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
978 */
979 static int
980generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
986 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100987 isn->isn_arg.storenr.stnr_idx = idx;
988 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989
990 return OK;
991}
992
993/*
994 * Generate an ISN_STOREOPT instruction
995 */
996 static int
997generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001002 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 return FAIL;
1004 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1005 isn->isn_arg.storeopt.so_flags = opt_flags;
1006
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_LOAD or similar instruction.
1012 */
1013 static int
1014generate_LOAD(
1015 cctx_T *cctx,
1016 isntype_T isn_type,
1017 int idx,
1018 char_u *name,
1019 type_T *type)
1020{
1021 isn_T *isn;
1022
Bram Moolenaar080457c2020-03-03 21:53:32 +01001023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1025 return FAIL;
1026 if (name != NULL)
1027 isn->isn_arg.string = vim_strsave(name);
1028 else
1029 isn->isn_arg.number = idx;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001035 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001036 */
1037 static int
1038generate_LOADV(
1039 cctx_T *cctx,
1040 char_u *name,
1041 int error)
1042{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001043 int di_flags;
1044 int vidx = find_vim_var(name, &di_flags);
1045 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001048 if (vidx < 0)
1049 {
1050 if (error)
1051 semsg(_(e_var_notfound), name);
1052 return FAIL;
1053 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001054 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001056 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001057}
1058
1059/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001060 * Generate an ISN_UNLET instruction.
1061 */
1062 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001063generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001064{
1065 isn_T *isn;
1066
1067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001068 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001069 return FAIL;
1070 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1071 isn->isn_arg.unlet.ul_forceit = forceit;
1072
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 * Generate an ISN_LOADS instruction.
1078 */
1079 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001080generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001084 int sid,
1085 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086{
1087 isn_T *isn;
1088
Bram Moolenaar080457c2020-03-03 21:53:32 +01001089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 if (isn_type == ISN_LOADS)
1091 isn = generate_instr_type(cctx, isn_type, type);
1092 else
1093 isn = generate_instr_drop(cctx, isn_type, 1);
1094 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001096 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1097 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
1099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1104 */
1105 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 cctx_T *cctx,
1108 isntype_T isn_type,
1109 int sid,
1110 int idx,
1111 type_T *type)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if (isn_type == ISN_LOADSCRIPT)
1117 isn = generate_instr_type(cctx, isn_type, type);
1118 else
1119 isn = generate_instr_drop(cctx, isn_type, 1);
1120 if (isn == NULL)
1121 return FAIL;
1122 isn->isn_arg.script.script_sid = sid;
1123 isn->isn_arg.script.script_idx = idx;
1124 return OK;
1125}
1126
1127/*
1128 * Generate an ISN_NEWLIST instruction.
1129 */
1130 static int
1131generate_NEWLIST(cctx_T *cctx, int count)
1132{
1133 isn_T *isn;
1134 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 type_T *type;
1136 type_T *member;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.number = count;
1142
Bram Moolenaar127542b2020-08-09 17:22:04 +02001143 // get the member type from all the items on the stack.
1144 member = get_member_type_from_stack(
1145 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1146 cctx->ctx_type_list);
1147 type = get_list_type(member, cctx->ctx_type_list);
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 // drop the value types
1150 stack->ga_len -= count;
1151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 // add the list type to the type stack
1153 if (ga_grow(stack, 1) == FAIL)
1154 return FAIL;
1155 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1156 ++stack->ga_len;
1157
1158 return OK;
1159}
1160
1161/*
1162 * Generate an ISN_NEWDICT instruction.
1163 */
1164 static int
1165generate_NEWDICT(cctx_T *cctx, int count)
1166{
1167 isn_T *isn;
1168 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 type_T *type;
1170 type_T *member;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = count;
1176
Bram Moolenaar127542b2020-08-09 17:22:04 +02001177 member = get_member_type_from_stack(
1178 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1179 cctx->ctx_type_list);
1180 type = get_dict_type(member, cctx->ctx_type_list);
1181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 // drop the key and value types
1183 stack->ga_len -= 2 * count;
1184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 // add the dict type to the type stack
1186 if (ga_grow(stack, 1) == FAIL)
1187 return FAIL;
1188 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1189 ++stack->ga_len;
1190
1191 return OK;
1192}
1193
1194/*
1195 * Generate an ISN_FUNCREF instruction.
1196 */
1197 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001198generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199{
1200 isn_T *isn;
1201 garray_T *stack = &cctx->ctx_type_stack;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1205 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001206 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001207 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208
1209 if (ga_grow(stack, 1) == FAIL)
1210 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001211 ((type_T **)stack->ga_data)[stack->ga_len] =
1212 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 ++stack->ga_len;
1214
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001219 * Generate an ISN_NEWFUNC instruction.
1220 */
1221 static int
1222generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1223{
1224 isn_T *isn;
1225 char_u *name;
1226
1227 RETURN_OK_IF_SKIP(cctx);
1228 name = vim_strsave(lambda_name);
1229 if (name == NULL)
1230 return FAIL;
1231 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1232 return FAIL;
1233 isn->isn_arg.newfunc.nf_lambda = name;
1234 isn->isn_arg.newfunc.nf_global = func_name;
1235
1236 return OK;
1237}
1238
1239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 * Generate an ISN_JUMP instruction.
1241 */
1242 static int
1243generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1244{
1245 isn_T *isn;
1246 garray_T *stack = &cctx->ctx_type_stack;
1247
Bram Moolenaar080457c2020-03-03 21:53:32 +01001248 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.jump.jump_when = when;
1252 isn->isn_arg.jump.jump_where = where;
1253
1254 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1255 --stack->ga_len;
1256
1257 return OK;
1258}
1259
1260 static int
1261generate_FOR(cctx_T *cctx, int loop_idx)
1262{
1263 isn_T *isn;
1264 garray_T *stack = &cctx->ctx_type_stack;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.forloop.for_idx = loop_idx;
1270
1271 if (ga_grow(stack, 1) == FAIL)
1272 return FAIL;
1273 // type doesn't matter, will be stored next
1274 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1275 ++stack->ga_len;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001282 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 * Return FAIL if the number of arguments is wrong.
1284 */
1285 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001286generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287{
1288 isn_T *isn;
1289 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001290 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001291 type_T *argtypes[MAX_FUNC_ARGS];
1292 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001295 argoff = check_internal_func(func_idx, argcount);
1296 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 return FAIL;
1298
Bram Moolenaar389df252020-07-09 21:20:47 +02001299 if (method_call && argoff > 1)
1300 {
1301 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1302 return FAIL;
1303 isn->isn_arg.shuffle.shfl_item = argcount;
1304 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1305 }
1306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.bfunc.cbf_idx = func_idx;
1310 isn->isn_arg.bfunc.cbf_argcount = argcount;
1311
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001312 for (i = 0; i < argcount; ++i)
1313 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 stack->ga_len -= argcount; // drop the arguments
1316 if (ga_grow(stack, 1) == FAIL)
1317 return FAIL;
1318 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001319 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 ++stack->ga_len; // add return value
1321
1322 return OK;
1323}
1324
1325/*
1326 * Generate an ISN_DCALL or ISN_UCALL instruction.
1327 * Return FAIL if the number of arguments is wrong.
1328 */
1329 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001330generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001335 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
Bram Moolenaar080457c2020-03-03 21:53:32 +01001337 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 if (argcount > regular_args && !has_varargs(ufunc))
1339 {
1340 semsg(_(e_toomanyarg), ufunc->uf_name);
1341 return FAIL;
1342 }
1343 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1344 {
1345 semsg(_(e_toofewarg), ufunc->uf_name);
1346 return FAIL;
1347 }
1348
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001349 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001350 {
1351 int i;
1352
1353 for (i = 0; i < argcount; ++i)
1354 {
1355 type_T *expected;
1356 type_T *actual;
1357
1358 if (i < regular_args)
1359 {
1360 if (ufunc->uf_arg_types == NULL)
1361 continue;
1362 expected = ufunc->uf_arg_types[i];
1363 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001364 else if (ufunc->uf_va_type == NULL)
1365 // possibly a lambda
1366 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001367 else
1368 expected = ufunc->uf_va_type->tt_member;
1369 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001370 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001371 {
1372 arg_type_mismatch(expected, actual, i + 1);
1373 return FAIL;
1374 }
1375 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001376 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001377 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1378 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001379 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001380 }
1381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001383 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001384 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001386 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 {
1388 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1389 isn->isn_arg.dfunc.cdf_argcount = argcount;
1390 }
1391 else
1392 {
1393 // A user function may be deleted and redefined later, can't use the
1394 // ufunc pointer, need to look it up again at runtime.
1395 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1396 isn->isn_arg.ufunc.cuf_argcount = argcount;
1397 }
1398
1399 stack->ga_len -= argcount; // drop the arguments
1400 if (ga_grow(stack, 1) == FAIL)
1401 return FAIL;
1402 // add return value
1403 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1404 ++stack->ga_len;
1405
1406 return OK;
1407}
1408
1409/*
1410 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1411 */
1412 static int
1413generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1414{
1415 isn_T *isn;
1416 garray_T *stack = &cctx->ctx_type_stack;
1417
Bram Moolenaar080457c2020-03-03 21:53:32 +01001418 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1420 return FAIL;
1421 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1422 isn->isn_arg.ufunc.cuf_argcount = argcount;
1423
1424 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001425 if (ga_grow(stack, 1) == FAIL)
1426 return FAIL;
1427 // add return value
1428 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1429 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430
1431 return OK;
1432}
1433
1434/*
1435 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001436 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 */
1438 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001439generate_PCALL(
1440 cctx_T *cctx,
1441 int argcount,
1442 char_u *name,
1443 type_T *type,
1444 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445{
1446 isn_T *isn;
1447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001448 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449
Bram Moolenaar080457c2020-03-03 21:53:32 +01001450 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001451
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001452 if (type->tt_type == VAR_ANY)
1453 ret_type = &t_any;
1454 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001455 {
1456 if (type->tt_argcount != -1)
1457 {
1458 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1459
1460 if (argcount < type->tt_min_argcount - varargs)
1461 {
1462 semsg(_(e_toofewarg), "[reference]");
1463 return FAIL;
1464 }
1465 if (!varargs && argcount > type->tt_argcount)
1466 {
1467 semsg(_(e_toomanyarg), "[reference]");
1468 return FAIL;
1469 }
1470 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001471 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001472 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001473 else
1474 {
1475 semsg(_("E1085: Not a callable type: %s"), name);
1476 return FAIL;
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.pfunc.cpf_top = at_top;
1482 isn->isn_arg.pfunc.cpf_argcount = argcount;
1483
1484 stack->ga_len -= argcount; // drop the arguments
1485
1486 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001487 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001489 // If partial is above the arguments it must be cleared and replaced with
1490 // the return value.
1491 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1492 return FAIL;
1493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 return OK;
1495}
1496
1497/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001498 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 */
1500 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001501generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502{
1503 isn_T *isn;
1504 garray_T *stack = &cctx->ctx_type_stack;
1505 type_T *type;
1506
Bram Moolenaar080457c2020-03-03 21:53:32 +01001507 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001508 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001510 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001512 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001514 if (type->tt_type != VAR_DICT && type != &t_any)
1515 {
1516 emsg(_(e_dictreq));
1517 return FAIL;
1518 }
1519 // change dict type to dict member type
1520 if (type->tt_type == VAR_DICT)
1521 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 return OK;
1524}
1525
1526/*
1527 * Generate an ISN_ECHO instruction.
1528 */
1529 static int
1530generate_ECHO(cctx_T *cctx, int with_white, int count)
1531{
1532 isn_T *isn;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1536 return FAIL;
1537 isn->isn_arg.echo.echo_with_white = with_white;
1538 isn->isn_arg.echo.echo_count = count;
1539
1540 return OK;
1541}
1542
Bram Moolenaarad39c092020-02-26 18:23:43 +01001543/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545 */
1546 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001547generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001548{
1549 isn_T *isn;
1550
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001551 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001552 return FAIL;
1553 isn->isn_arg.number = count;
1554
1555 return OK;
1556}
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 static int
1559generate_EXEC(cctx_T *cctx, char_u *line)
1560{
1561 isn_T *isn;
1562
Bram Moolenaar080457c2020-03-03 21:53:32 +01001563 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1565 return FAIL;
1566 isn->isn_arg.string = vim_strsave(line);
1567 return OK;
1568}
1569
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001570 static int
1571generate_EXECCONCAT(cctx_T *cctx, int count)
1572{
1573 isn_T *isn;
1574
1575 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1576 return FAIL;
1577 isn->isn_arg.number = count;
1578 return OK;
1579}
1580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581/*
1582 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001583 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001585 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1587{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 lvar_T *lvar;
1589
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001590 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001592 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001593 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 }
1595
1596 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 return NULL;
1598 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001600 // Every local variable uses the next entry on the stack. We could re-use
1601 // the last ones when leaving a scope, but then variables used in a closure
1602 // might get overwritten. To keep things simple do not re-use stack
1603 // entries. This is less efficient, but memory is cheap these days.
1604 lvar->lv_idx = cctx->ctx_locals_count++;
1605
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001606 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 lvar->lv_const = isConst;
1608 lvar->lv_type = type;
1609
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001610 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611}
1612
1613/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001614 * Remove local variables above "new_top".
1615 */
1616 static void
1617unwind_locals(cctx_T *cctx, int new_top)
1618{
1619 if (cctx->ctx_locals.ga_len > new_top)
1620 {
1621 int idx;
1622 lvar_T *lvar;
1623
1624 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1625 {
1626 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1627 vim_free(lvar->lv_name);
1628 }
1629 }
1630 cctx->ctx_locals.ga_len = new_top;
1631}
1632
1633/*
1634 * Free all local variables.
1635 */
1636 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001637free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001638{
1639 unwind_locals(cctx, 0);
1640 ga_clear(&cctx->ctx_locals);
1641}
1642
1643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 * Find "name" in script-local items of script "sid".
1645 * Returns the index in "sn_var_vals" if found.
1646 * If found but not in "sn_var_vals" returns -1.
1647 * If not found returns -2.
1648 */
1649 int
1650get_script_item_idx(int sid, char_u *name, int check_writable)
1651{
1652 hashtab_T *ht;
1653 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001654 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 int idx;
1656
1657 // First look the name up in the hashtable.
1658 if (sid <= 0 || sid > script_items.ga_len)
1659 return -1;
1660 ht = &SCRIPT_VARS(sid);
1661 di = find_var_in_ht(ht, 0, name, TRUE);
1662 if (di == NULL)
1663 return -2;
1664
1665 // Now find the svar_T index in sn_var_vals.
1666 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1667 {
1668 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1669
1670 if (sv->sv_tv == &di->di_tv)
1671 {
1672 if (check_writable && sv->sv_const)
1673 semsg(_(e_readonlyvar), name);
1674 return idx;
1675 }
1676 }
1677 return -1;
1678}
1679
1680/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001681 * Find "name" in imported items of the current script or in "cctx" if not
1682 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 */
1684 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001685find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 int idx;
1688
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001689 if (current_sctx.sc_sid <= 0)
1690 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 if (cctx != NULL)
1692 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1693 {
1694 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1695 + idx;
1696
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001697 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1698 : STRLEN(import->imp_name) == len
1699 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 return import;
1701 }
1702
Bram Moolenaarefa94442020-08-08 22:16:00 +02001703 return find_imported_in_script(name, len, current_sctx.sc_sid);
1704}
1705
1706 imported_T *
1707find_imported_in_script(char_u *name, size_t len, int sid)
1708{
1709 scriptitem_T *si = SCRIPT_ITEM(sid);
1710 int idx;
1711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1713 {
1714 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1715
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001716 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1717 : STRLEN(import->imp_name) == len
1718 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 return import;
1720 }
1721 return NULL;
1722}
1723
1724/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001725 * Free all imported variables.
1726 */
1727 static void
1728free_imported(cctx_T *cctx)
1729{
1730 int idx;
1731
1732 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1733 {
1734 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1735
1736 vim_free(import->imp_name);
1737 }
1738 ga_clear(&cctx->ctx_imports);
1739}
1740
1741/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001742 * Return TRUE if "p" points at a "#" but not at "#{".
1743 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001744 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001745vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001746{
1747 return p[0] == '#' && p[1] != '{';
1748}
1749
1750/*
1751 * Return a pointer to the next line that isn't empty or only contains a
1752 * comment. Skips over white space.
1753 * Returns NULL if there is none.
1754 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001755 char_u *
1756peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001757{
1758 int lnum = cctx->ctx_lnum;
1759
1760 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1761 {
1762 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001763 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001764
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001765 // ignore NULLs inserted for continuation lines
1766 if (line != NULL)
1767 {
1768 p = skipwhite(line);
1769 if (*p != NUL && !vim9_comment_start(p))
1770 return p;
1771 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001772 }
1773 return NULL;
1774}
1775
1776/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001777 * Called when checking for a following operator at "arg". When the rest of
1778 * the line is empty or only a comment, peek the next line. If there is a next
1779 * line return a pointer to it and set "nextp".
1780 * Otherwise skip over white space.
1781 */
1782 static char_u *
1783may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1784{
1785 char_u *p = skipwhite(arg);
1786
1787 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001788 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001789 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001790 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001791 if (*nextp != NULL)
1792 return *nextp;
1793 }
1794 return p;
1795}
1796
1797/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001798 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001799 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001800 * Returns NULL when at the end.
1801 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001802 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001803next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001804{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001805 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001806
1807 do
1808 {
1809 ++cctx->ctx_lnum;
1810 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001811 {
1812 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001814 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001815 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001816 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001817 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001818 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001819 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001820 return line;
1821}
1822
1823/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001824 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001825 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001826 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1827 */
1828 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001829may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001830{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001831 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001832 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001833 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001834
1835 if (next == NULL)
1836 return FAIL;
1837 *arg = skipwhite(next);
1838 }
1839 return OK;
1840}
1841
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001842/*
1843 * Idem, and give an error when failed.
1844 */
1845 static int
1846may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1847{
1848 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1849 {
1850 emsg(_("E1097: line incomplete"));
1851 return FAIL;
1852 }
1853 return OK;
1854}
1855
1856
Bram Moolenaara5565e42020-05-09 15:44:01 +02001857// Structure passed between the compile_expr* functions to keep track of
1858// constants that have been parsed but for which no code was produced yet. If
1859// possible expressions on these constants are applied at compile time. If
1860// that is not possible, the code to push the constants needs to be generated
1861// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001862// Using 50 should be more than enough of 5 levels of ().
1863#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001864typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001865 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001866 int pp_used; // active entries in pp_tv[]
1867} ppconst_T;
1868
Bram Moolenaar1c747212020-05-09 18:28:34 +02001869static int compile_expr0(char_u **arg, cctx_T *cctx);
1870static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1871
Bram Moolenaara5565e42020-05-09 15:44:01 +02001872/*
1873 * Generate a PUSH instruction for "tv".
1874 * "tv" will be consumed or cleared.
1875 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1876 */
1877 static int
1878generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1879{
1880 if (tv != NULL)
1881 {
1882 switch (tv->v_type)
1883 {
1884 case VAR_UNKNOWN:
1885 break;
1886 case VAR_BOOL:
1887 generate_PUSHBOOL(cctx, tv->vval.v_number);
1888 break;
1889 case VAR_SPECIAL:
1890 generate_PUSHSPEC(cctx, tv->vval.v_number);
1891 break;
1892 case VAR_NUMBER:
1893 generate_PUSHNR(cctx, tv->vval.v_number);
1894 break;
1895#ifdef FEAT_FLOAT
1896 case VAR_FLOAT:
1897 generate_PUSHF(cctx, tv->vval.v_float);
1898 break;
1899#endif
1900 case VAR_BLOB:
1901 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1902 tv->vval.v_blob = NULL;
1903 break;
1904 case VAR_STRING:
1905 generate_PUSHS(cctx, tv->vval.v_string);
1906 tv->vval.v_string = NULL;
1907 break;
1908 default:
1909 iemsg("constant type not supported");
1910 clear_tv(tv);
1911 return FAIL;
1912 }
1913 tv->v_type = VAR_UNKNOWN;
1914 }
1915 return OK;
1916}
1917
1918/*
1919 * Generate code for any ppconst entries.
1920 */
1921 static int
1922generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1923{
1924 int i;
1925 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001926 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001927
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001928 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001929 for (i = 0; i < ppconst->pp_used; ++i)
1930 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1931 ret = FAIL;
1932 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001933 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001934 return ret;
1935}
1936
1937/*
1938 * Clear ppconst constants. Used when failing.
1939 */
1940 static void
1941clear_ppconst(ppconst_T *ppconst)
1942{
1943 int i;
1944
1945 for (i = 0; i < ppconst->pp_used; ++i)
1946 clear_tv(&ppconst->pp_tv[i]);
1947 ppconst->pp_used = 0;
1948}
1949
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001950/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001951 * Generate an instruction to load script-local variable "name", without the
1952 * leading "s:".
1953 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 */
1955 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001956compile_load_scriptvar(
1957 cctx_T *cctx,
1958 char_u *name, // variable NUL terminated
1959 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001960 char_u **end, // end of variable
1961 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001963 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1965 imported_T *import;
1966
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001967 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001969 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001970 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1971 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 }
1973 if (idx >= 0)
1974 {
1975 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1976
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001977 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 current_sctx.sc_sid, idx, sv->sv_type);
1979 return OK;
1980 }
1981
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001982 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 if (import != NULL)
1984 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001985 if (import->imp_all)
1986 {
1987 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001988 char_u *exp_name;
1989 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001990 ufunc_T *ufunc;
1991 type_T *type;
1992
1993 // Used "import * as Name", need to lookup the member.
1994 if (*p != '.')
1995 {
1996 semsg(_("E1060: expected dot after name: %s"), start);
1997 return FAIL;
1998 }
1999 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002000 if (VIM_ISWHITE(*p))
2001 {
2002 emsg(_("E1074: no white space allowed after dot"));
2003 return FAIL;
2004 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002005
Bram Moolenaar1c991142020-07-04 13:15:31 +02002006 // isolate one name
2007 exp_name = p;
2008 while (eval_isnamec(*p))
2009 ++p;
2010 cc = *p;
2011 *p = NUL;
2012
2013 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2014 *p = cc;
2015 p = skipwhite(p);
2016
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002017 // TODO: what if it is a function?
2018 if (idx < 0)
2019 return FAIL;
2020 *end = p;
2021
2022 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2023 import->imp_sid,
2024 idx,
2025 type);
2026 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002027 else if (import->imp_funcname != NULL)
2028 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002029 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2031 import->imp_sid,
2032 import->imp_var_vals_idx,
2033 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 return OK;
2035 }
2036
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002037 if (error)
2038 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 return FAIL;
2040}
2041
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002042 static int
2043generate_funcref(cctx_T *cctx, char_u *name)
2044{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002045 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002046
2047 if (ufunc == NULL)
2048 return FAIL;
2049
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002050 // Need to compile any default values to get the argument types.
2051 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2052 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2053 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002054 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002055}
2056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057/*
2058 * Compile a variable name into a load instruction.
2059 * "end" points to just after the name.
2060 * When "error" is FALSE do not give an error when not found.
2061 */
2062 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002063compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064{
2065 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002066 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002067 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002069 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070
2071 if (*(*arg + 1) == ':')
2072 {
2073 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002074 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002075 {
2076 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002078 switch (**arg)
2079 {
2080 case 'g': isn_type = ISN_LOADGDICT; break;
2081 case 'w': isn_type = ISN_LOADWDICT; break;
2082 case 't': isn_type = ISN_LOADTDICT; break;
2083 case 'b': isn_type = ISN_LOADBDICT; break;
2084 default:
2085 semsg(_(e_namespace), *arg);
2086 goto theend;
2087 }
2088 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2089 goto theend;
2090 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 else
2093 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002094 isntype_T isn_type = ISN_DROP;
2095
2096 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2097 if (name == NULL)
2098 return FAIL;
2099
2100 switch (**arg)
2101 {
2102 case 'v': res = generate_LOADV(cctx, name, error);
2103 break;
2104 case 's': res = compile_load_scriptvar(cctx, name,
2105 NULL, NULL, error);
2106 break;
2107 case 'g': isn_type = ISN_LOADG; break;
2108 case 'w': isn_type = ISN_LOADW; break;
2109 case 't': isn_type = ISN_LOADT; break;
2110 case 'b': isn_type = ISN_LOADB; break;
2111 default: semsg(_(e_namespace), *arg);
2112 goto theend;
2113 }
2114 if (isn_type != ISN_DROP)
2115 {
2116 // Global, Buffer-local, Window-local and Tabpage-local
2117 // variables can be defined later, thus we don't check if it
2118 // exists, give error at runtime.
2119 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122 }
2123 else
2124 {
2125 size_t len = end - *arg;
2126 int idx;
2127 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002128 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129
2130 name = vim_strnsave(*arg, end - *arg);
2131 if (name == NULL)
2132 return FAIL;
2133
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002134 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002136 if (!gen_load_outer)
2137 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 }
2139 else
2140 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002141 lvar_T *lvar = lookup_local(*arg, len, cctx);
2142
2143 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002145 type = lvar->lv_type;
2146 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002147 if (lvar->lv_from_outer)
2148 gen_load_outer = TRUE;
2149 else
2150 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 }
2152 else
2153 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002154 // "var" can be script-local even without using "s:" if it
2155 // already exists.
2156 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2157 == SCRIPT_VERSION_VIM9
2158 || lookup_script(*arg, len) == OK)
2159 res = compile_load_scriptvar(cctx, name, *arg, &end,
2160 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161
Bram Moolenaara5565e42020-05-09 15:44:01 +02002162 // When the name starts with an uppercase letter or "x:" it
2163 // can be a user defined function.
2164 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2165 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 }
2167 }
2168 if (gen_load)
2169 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002170 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002171 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002172 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002173 cctx->ctx_outer_used = TRUE;
2174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 }
2176
2177 *arg = end;
2178
2179theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002180 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 semsg(_(e_var_notfound), name);
2182 vim_free(name);
2183 return res;
2184}
2185
2186/*
2187 * Compile the argument expressions.
2188 * "arg" points to just after the "(" and is advanced to after the ")"
2189 */
2190 static int
2191compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2192{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002193 char_u *p = *arg;
2194 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195
Bram Moolenaare6085c52020-04-12 20:19:16 +02002196 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002198 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2199 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002200 if (*p == ')')
2201 {
2202 *arg = p + 1;
2203 return OK;
2204 }
2205
Bram Moolenaara5565e42020-05-09 15:44:01 +02002206 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 return FAIL;
2208 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002209
2210 if (*p != ',' && *skipwhite(p) == ',')
2211 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002212 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 p = skipwhite(p);
2214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002216 {
2217 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002218 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002219 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002220 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002221 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002222 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002224failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002225 emsg(_(e_missing_close));
2226 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227}
2228
2229/*
2230 * Compile a function call: name(arg1, arg2)
2231 * "arg" points to "name", "arg + varlen" to the "(".
2232 * "argcount_init" is 1 for "value->method()"
2233 * Instructions:
2234 * EVAL arg1
2235 * EVAL arg2
2236 * BCALL / DCALL / UCALL
2237 */
2238 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002239compile_call(
2240 char_u **arg,
2241 size_t varlen,
2242 cctx_T *cctx,
2243 ppconst_T *ppconst,
2244 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
2246 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002247 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 int argcount = argcount_init;
2249 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002250 char_u fname_buf[FLEN_FIXED + 1];
2251 char_u *tofree = NULL;
2252 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002254 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002255 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
Bram Moolenaara5565e42020-05-09 15:44:01 +02002257 // we can evaluate "has('name')" at compile time
2258 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2259 {
2260 char_u *s = skipwhite(*arg + varlen + 1);
2261 typval_T argvars[2];
2262
2263 argvars[0].v_type = VAR_UNKNOWN;
2264 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002265 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002266 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002267 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002268 s = skipwhite(s);
2269 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2270 {
2271 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2272
2273 *arg = s + 1;
2274 argvars[1].v_type = VAR_UNKNOWN;
2275 tv->v_type = VAR_NUMBER;
2276 tv->vval.v_number = 0;
2277 f_has(argvars, tv);
2278 clear_tv(&argvars[0]);
2279 ++ppconst->pp_used;
2280 return OK;
2281 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002282 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002283 }
2284
2285 if (generate_ppconst(cctx, ppconst) == FAIL)
2286 return FAIL;
2287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 if (varlen >= sizeof(namebuf))
2289 {
2290 semsg(_("E1011: name too long: %s"), name);
2291 return FAIL;
2292 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002293 vim_strncpy(namebuf, *arg, varlen);
2294 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295
2296 *arg = skipwhite(*arg + varlen + 1);
2297 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
Bram Moolenaara1773442020-08-12 15:21:22 +02002300 is_autoload = vim_strchr(name, '#') != NULL;
2301 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 {
2303 int idx;
2304
2305 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002306 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002308 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002309 else
2310 semsg(_(e_unknownfunc), namebuf);
2311 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 }
2313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002315 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002317 {
2318 res = generate_CALL(cctx, ufunc, argcount);
2319 goto theend;
2320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321
2322 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002323 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002324 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002326 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002327 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002328 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002329 garray_T *stack = &cctx->ctx_type_stack;
2330 type_T *type;
2331
2332 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2333 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002334 goto theend;
2335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002337 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002338 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002339 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002340 res = generate_UCALL(cctx, name, argcount);
2341 else
2342 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002343
2344theend:
2345 vim_free(tofree);
2346 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347}
2348
2349// like NAMESPACE_CHAR but with 'a' and 'l'.
2350#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2351
2352/*
2353 * Find the end of a variable or function name. Unlike find_name_end() this
2354 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002355 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 * Return a pointer to just after the name. Equal to "arg" if there is no
2357 * valid name.
2358 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002359 static char_u *
2360to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361{
2362 char_u *p;
2363
2364 // Quick check for valid starting character.
2365 if (!eval_isnamec1(*arg))
2366 return arg;
2367
2368 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2369 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2370 // and can be used in slice "[n:]".
2371 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002372 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2374 break;
2375 return p;
2376}
2377
2378/*
2379 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002380 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
2382 char_u *
2383to_name_const_end(char_u *arg)
2384{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002385 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 typval_T rettv;
2387
2388 if (p == arg && *arg == '[')
2389 {
2390
2391 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002392 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 p = arg;
2394 }
2395 else if (p == arg && *arg == '#' && arg[1] == '{')
2396 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002397 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002399 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 p = arg;
2401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402
2403 return p;
2404}
2405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406/*
2407 * parse a list: [expr, expr]
2408 * "*arg" points to the '['.
2409 */
2410 static int
2411compile_list(char_u **arg, cctx_T *cctx)
2412{
2413 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002414 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 int count = 0;
2416
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002417 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002419 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002420 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002421 semsg(_(e_list_end), *arg);
2422 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002423 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002424 if (*p == ',')
2425 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002426 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002427 return FAIL;
2428 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002429 if (*p == ']')
2430 {
2431 ++p;
2432 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002433 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002434 p += STRLEN(p);
2435 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002436 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002437 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 break;
2439 ++count;
2440 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002441 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002443 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2444 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002445 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002446 return FAIL;
2447 }
2448 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002449 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 p = skipwhite(p);
2451 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002452 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453
2454 generate_NEWLIST(cctx, count);
2455 return OK;
2456}
2457
2458/*
2459 * parse a lambda: {arg, arg -> expr}
2460 * "*arg" points to the '{'.
2461 */
2462 static int
2463compile_lambda(char_u **arg, cctx_T *cctx)
2464{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 typval_T rettv;
2466 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002467 evalarg_T evalarg;
2468
2469 CLEAR_FIELD(evalarg);
2470 evalarg.eval_flags = EVAL_EVALUATE;
2471 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
2473 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002474 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002478 ++ufunc->uf_refcount;
2479 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002480 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481
2482 // The function will have one line: "return {expr}".
2483 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002484 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002486 clear_evalarg(&evalarg, NULL);
2487
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002488 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002489 {
2490 // The return type will now be known.
2491 set_function_type(ufunc);
2492
2493 return generate_FUNCREF(cctx, ufunc);
2494 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002495
2496 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 return FAIL;
2498}
2499
2500/*
2501 * Compile a lamda call: expr->{lambda}(args)
2502 * "arg" points to the "{".
2503 */
2504 static int
2505compile_lambda_call(char_u **arg, cctx_T *cctx)
2506{
2507 ufunc_T *ufunc;
2508 typval_T rettv;
2509 int argcount = 1;
2510 int ret = FAIL;
2511
2512 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002513 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 return FAIL;
2515
2516 if (**arg != '(')
2517 {
2518 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002519 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 else
2521 semsg(_(e_missing_paren), "lambda");
2522 clear_tv(&rettv);
2523 return FAIL;
2524 }
2525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 ufunc = rettv.vval.v_partial->pt_func;
2527 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002528 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002529 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002530
2531 // The function will have one line: "return {expr}".
2532 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002533 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534
2535 // compile the arguments
2536 *arg = skipwhite(*arg + 1);
2537 if (compile_arguments(arg, cctx, &argcount) == OK)
2538 // call the compiled function
2539 ret = generate_CALL(cctx, ufunc, argcount);
2540
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002541 if (ret == FAIL)
2542 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 return ret;
2544}
2545
2546/*
2547 * parse a dict: {'key': val} or #{key: val}
2548 * "*arg" points to the '{'.
2549 */
2550 static int
2551compile_dict(char_u **arg, cctx_T *cctx, int literal)
2552{
2553 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 int count = 0;
2556 dict_T *d = dict_alloc();
2557 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002558 char_u *whitep = *arg;
2559 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560
2561 if (d == NULL)
2562 return FAIL;
2563 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002564 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 {
2566 char_u *key = NULL;
2567
Bram Moolenaar23c55272020-06-21 16:58:13 +02002568 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002569 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 *arg = NULL;
2571 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002572 }
2573
2574 if (**arg == '}')
2575 break;
2576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (literal)
2578 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002579 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 {
2583 semsg(_("E1014: Invalid key: %s"), *arg);
2584 return FAIL;
2585 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002586 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (generate_PUSHS(cctx, key) == FAIL)
2588 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002589 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 }
2591 else
2592 {
2593 isn_T *isn;
2594
Bram Moolenaara5565e42020-05-09 15:44:01 +02002595 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2598 if (isn->isn_type == ISN_PUSHS)
2599 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002600 else
2601 {
2602 type_T *keytype = ((type_T **)stack->ga_data)
2603 [stack->ga_len - 1];
2604 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2605 return FAIL;
2606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 }
2608
2609 // Check for duplicate keys, if using string keys.
2610 if (key != NULL)
2611 {
2612 item = dict_find(d, key, -1);
2613 if (item != NULL)
2614 {
2615 semsg(_(e_duplicate_key), key);
2616 goto failret;
2617 }
2618 item = dictitem_alloc(key);
2619 if (item != NULL)
2620 {
2621 item->di_tv.v_type = VAR_UNKNOWN;
2622 item->di_tv.v_lock = 0;
2623 if (dict_add(d, item) == FAIL)
2624 dictitem_free(item);
2625 }
2626 }
2627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (**arg != ':')
2629 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002630 if (*skipwhite(*arg) == ':')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002631 semsg(_(e_no_white_space_allowed_before), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002632 else
2633 semsg(_(e_missing_dict_colon), *arg);
2634 return FAIL;
2635 }
2636 whitep = *arg + 1;
2637 if (!IS_WHITE_OR_NUL(*whitep))
2638 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002639 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 return FAIL;
2641 }
2642
2643 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002644 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002645 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002646 *arg = NULL;
2647 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002648 }
2649
Bram Moolenaara5565e42020-05-09 15:44:01 +02002650 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 return FAIL;
2652 ++count;
2653
Bram Moolenaar2c330432020-04-13 14:41:35 +02002654 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655 *arg = skipwhite(*arg);
2656 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658 *arg = NULL;
2659 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 if (**arg == '}')
2662 break;
2663 if (**arg != ',')
2664 {
2665 semsg(_(e_missing_dict_comma), *arg);
2666 goto failret;
2667 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002668 if (IS_WHITE_OR_NUL(*whitep))
2669 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002670 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002671 return FAIL;
2672 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002673 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 *arg = skipwhite(*arg + 1);
2675 }
2676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 *arg = *arg + 1;
2678
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002679 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002680 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002681 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002682 *arg += STRLEN(*arg);
2683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 dict_unref(d);
2685 return generate_NEWDICT(cctx, count);
2686
2687failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002688 if (*arg == NULL)
2689 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 dict_unref(d);
2691 return FAIL;
2692}
2693
2694/*
2695 * Compile "&option".
2696 */
2697 static int
2698compile_get_option(char_u **arg, cctx_T *cctx)
2699{
2700 typval_T rettv;
2701 char_u *start = *arg;
2702 int ret;
2703
2704 // parse the option and get the current value to get the type.
2705 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002706 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 if (ret == OK)
2708 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002709 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 char_u *name = vim_strnsave(start, *arg - start);
2711 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2712
2713 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2714 vim_free(name);
2715 }
2716 clear_tv(&rettv);
2717
2718 return ret;
2719}
2720
2721/*
2722 * Compile "$VAR".
2723 */
2724 static int
2725compile_get_env(char_u **arg, cctx_T *cctx)
2726{
2727 char_u *start = *arg;
2728 int len;
2729 int ret;
2730 char_u *name;
2731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 ++*arg;
2733 len = get_env_len(arg);
2734 if (len == 0)
2735 {
2736 semsg(_(e_syntax_at), start - 1);
2737 return FAIL;
2738 }
2739
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002740 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 name = vim_strnsave(start, len + 1);
2742 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2743 vim_free(name);
2744 return ret;
2745}
2746
2747/*
2748 * Compile "@r".
2749 */
2750 static int
2751compile_get_register(char_u **arg, cctx_T *cctx)
2752{
2753 int ret;
2754
2755 ++*arg;
2756 if (**arg == NUL)
2757 {
2758 semsg(_(e_syntax_at), *arg - 1);
2759 return FAIL;
2760 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002761 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
2763 emsg_invreg(**arg);
2764 return FAIL;
2765 }
2766 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2767 ++*arg;
2768 return ret;
2769}
2770
2771/*
2772 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002773 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 */
2775 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002776apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002778 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779
2780 // this works from end to start
2781 while (p > start)
2782 {
2783 --p;
2784 if (*p == '-' || *p == '+')
2785 {
2786 // only '-' has an effect, for '+' we only check the type
2787#ifdef FEAT_FLOAT
2788 if (rettv->v_type == VAR_FLOAT)
2789 {
2790 if (*p == '-')
2791 rettv->vval.v_float = -rettv->vval.v_float;
2792 }
2793 else
2794#endif
2795 {
2796 varnumber_T val;
2797 int error = FALSE;
2798
2799 // tv_get_number_chk() accepts a string, but we don't want that
2800 // here
2801 if (check_not_string(rettv) == FAIL)
2802 return FAIL;
2803 val = tv_get_number_chk(rettv, &error);
2804 clear_tv(rettv);
2805 if (error)
2806 return FAIL;
2807 if (*p == '-')
2808 val = -val;
2809 rettv->v_type = VAR_NUMBER;
2810 rettv->vval.v_number = val;
2811 }
2812 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002813 else if (numeric_only)
2814 {
2815 ++p;
2816 break;
2817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 else
2819 {
2820 int v = tv2bool(rettv);
2821
2822 // '!' is permissive in the type.
2823 clear_tv(rettv);
2824 rettv->v_type = VAR_BOOL;
2825 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2826 }
2827 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002828 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 return OK;
2830}
2831
2832/*
2833 * Recognize v: variables that are constants and set "rettv".
2834 */
2835 static void
2836get_vim_constant(char_u **arg, typval_T *rettv)
2837{
2838 if (STRNCMP(*arg, "v:true", 6) == 0)
2839 {
2840 rettv->v_type = VAR_BOOL;
2841 rettv->vval.v_number = VVAL_TRUE;
2842 *arg += 6;
2843 }
2844 else if (STRNCMP(*arg, "v:false", 7) == 0)
2845 {
2846 rettv->v_type = VAR_BOOL;
2847 rettv->vval.v_number = VVAL_FALSE;
2848 *arg += 7;
2849 }
2850 else if (STRNCMP(*arg, "v:null", 6) == 0)
2851 {
2852 rettv->v_type = VAR_SPECIAL;
2853 rettv->vval.v_number = VVAL_NULL;
2854 *arg += 6;
2855 }
2856 else if (STRNCMP(*arg, "v:none", 6) == 0)
2857 {
2858 rettv->v_type = VAR_SPECIAL;
2859 rettv->vval.v_number = VVAL_NONE;
2860 *arg += 6;
2861 }
2862}
2863
Bram Moolenaar696ba232020-07-29 21:20:41 +02002864 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002865get_compare_type(char_u *p, int *len, int *type_is)
2866{
2867 exptype_T type = EXPR_UNKNOWN;
2868 int i;
2869
2870 switch (p[0])
2871 {
2872 case '=': if (p[1] == '=')
2873 type = EXPR_EQUAL;
2874 else if (p[1] == '~')
2875 type = EXPR_MATCH;
2876 break;
2877 case '!': if (p[1] == '=')
2878 type = EXPR_NEQUAL;
2879 else if (p[1] == '~')
2880 type = EXPR_NOMATCH;
2881 break;
2882 case '>': if (p[1] != '=')
2883 {
2884 type = EXPR_GREATER;
2885 *len = 1;
2886 }
2887 else
2888 type = EXPR_GEQUAL;
2889 break;
2890 case '<': if (p[1] != '=')
2891 {
2892 type = EXPR_SMALLER;
2893 *len = 1;
2894 }
2895 else
2896 type = EXPR_SEQUAL;
2897 break;
2898 case 'i': if (p[1] == 's')
2899 {
2900 // "is" and "isnot"; but not a prefix of a name
2901 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2902 *len = 5;
2903 i = p[*len];
2904 if (!isalnum(i) && i != '_')
2905 {
2906 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2907 *type_is = TRUE;
2908 }
2909 }
2910 break;
2911 }
2912 return type;
2913}
2914
2915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002917 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 */
2919 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002920compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002922 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
2924 // this works from end to start
2925 while (p > start)
2926 {
2927 --p;
2928 if (*p == '-' || *p == '+')
2929 {
2930 int negate = *p == '-';
2931 isn_T *isn;
2932
2933 // TODO: check type
2934 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2935 {
2936 --p;
2937 if (*p == '-')
2938 negate = !negate;
2939 }
2940 // only '-' has an effect, for '+' we only check the type
2941 if (negate)
2942 isn = generate_instr(cctx, ISN_NEGATENR);
2943 else
2944 isn = generate_instr(cctx, ISN_CHECKNR);
2945 if (isn == NULL)
2946 return FAIL;
2947 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002948 else if (numeric_only)
2949 {
2950 ++p;
2951 break;
2952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 else
2954 {
2955 int invert = TRUE;
2956
2957 while (p > start && p[-1] == '!')
2958 {
2959 --p;
2960 invert = !invert;
2961 }
2962 if (generate_2BOOL(cctx, invert) == FAIL)
2963 return FAIL;
2964 }
2965 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002966 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 return OK;
2968}
2969
2970/*
2971 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002972 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 */
2974 static int
2975compile_subscript(
2976 char_u **arg,
2977 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002978 char_u *start_leader,
2979 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002980 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002982 char_u *name_start = *end_leader;
2983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 for (;;)
2985 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002986 char_u *p = skipwhite(*arg);
2987
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002988 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002989 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002990 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002991
2992 // If a following line starts with "->{" or "->X" advance to that
2993 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002994 // Also if a following line starts with ".x".
2995 if (next != NULL &&
2996 ((next[0] == '-' && next[1] == '>'
2997 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002998 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002999 {
3000 next = next_line_from_context(cctx, TRUE);
3001 if (next == NULL)
3002 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003003 *arg = next;
3004 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003005 }
3006 }
3007
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003008 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3009 // not a function call.
3010 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003012 garray_T *stack = &cctx->ctx_type_stack;
3013 type_T *type;
3014 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003016 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003017 return FAIL;
3018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003020 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3021
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003022 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3024 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003025 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 return FAIL;
3027 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003028 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003030 char_u *pstart = p;
3031
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003032 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003033 return FAIL;
3034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 // something->method()
3036 // Apply the '!', '-' and '+' first:
3037 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003038 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003041 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003042 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003043 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 if (**arg == '{')
3045 {
3046 // lambda call: list->{lambda}
3047 if (compile_lambda_call(arg, cctx) == FAIL)
3048 return FAIL;
3049 }
3050 else
3051 {
3052 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003053 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003054 if (!eval_isnamec1(*p))
3055 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003056 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003057 return FAIL;
3058 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003059 if (ASCII_ISALPHA(*p) && p[1] == ':')
3060 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003061 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 ;
3063 if (*p != '(')
3064 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003065 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003069 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 return FAIL;
3071 }
3072 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003073 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003075 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003076 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003077 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003078
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003079 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003080 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003081 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003082 // TODO: blob index
3083 // TODO: more arguments
3084 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003085 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003086 return FAIL;
3087
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003088 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003089 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003090 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003091 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003092 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 return FAIL;
3094
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003095 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 if (**arg != ']')
3098 {
3099 emsg(_(e_missbrac));
3100 return FAIL;
3101 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003102 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003104 // We can index a list and a dict. If we don't know the type
3105 // we can use the index value type.
3106 // TODO: If we don't know use an instruction to figure it out at
3107 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003108 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003109 vtype = (*typep)->tt_type;
3110 if (*typep == &t_any)
3111 {
3112 type_T *valtype = ((type_T **)stack->ga_data)
3113 [stack->ga_len - 1];
3114 if (valtype == &t_string)
3115 vtype = VAR_DICT;
3116 }
3117 if (vtype == VAR_DICT)
3118 {
3119 if ((*typep)->tt_type == VAR_DICT)
3120 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003121 else
3122 {
3123 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3124 return FAIL;
3125 *typep = &t_any;
3126 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003127 if (may_generate_2STRING(-1, cctx) == FAIL)
3128 return FAIL;
3129 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3130 return FAIL;
3131 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003132 else if (vtype == VAR_STRING)
3133 {
3134 *typep = &t_number;
3135 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3136 return FAIL;
3137 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003138 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003139 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 if ((*typep)->tt_type == VAR_LIST)
3141 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003142 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003143 return FAIL;
3144 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003145 else
3146 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003147 emsg(_(e_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003148 return FAIL;
3149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003151 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003153 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003154 return FAIL;
3155
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003156 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003157 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3158 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003160 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003161 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 while (eval_isnamec(*p))
3163 MB_PTR_ADV(p);
3164 if (p == *arg)
3165 {
3166 semsg(_(e_syntax_at), *arg);
3167 return FAIL;
3168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003169 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 return FAIL;
3171 *arg = p;
3172 }
3173 else
3174 break;
3175 }
3176
3177 // TODO - see handle_subscript():
3178 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3179 // Don't do this when "Func" is already a partial that was bound
3180 // explicitly (pt_auto is FALSE).
3181
3182 return OK;
3183}
3184
3185/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003186 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3187 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003189 * If the value is a constant "ppconst->pp_ret" will be set.
3190 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003191 *
3192 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 */
3194
3195/*
3196 * number number constant
3197 * 0zFFFFFFFF Blob constant
3198 * "string" string constant
3199 * 'string' literal string constant
3200 * &option-name option value
3201 * @r register contents
3202 * identifier variable value
3203 * function() function call
3204 * $VAR environment variable
3205 * (expression) nested expression
3206 * [expr, expr] List
3207 * {key: val, key: val} Dictionary
3208 * #{key: val, key: val} Dictionary with literal keys
3209 *
3210 * Also handle:
3211 * ! in front logical NOT
3212 * - in front unary minus
3213 * + in front unary plus (ignored)
3214 * trailing (arg) funcref/partial call
3215 * trailing [] subscript in String or List
3216 * trailing .name entry in Dictionary
3217 * trailing ->name() method call
3218 */
3219 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003220compile_expr7(
3221 char_u **arg,
3222 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003223 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 char_u *start_leader, *end_leader;
3226 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003227 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003228 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229
3230 /*
3231 * Skip '!', '-' and '+' characters. They are handled later.
3232 */
3233 start_leader = *arg;
3234 while (**arg == '!' || **arg == '-' || **arg == '+')
3235 *arg = skipwhite(*arg + 1);
3236 end_leader = *arg;
3237
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003238 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 switch (**arg)
3240 {
3241 /*
3242 * Number constant.
3243 */
3244 case '0': // also for blob starting with 0z
3245 case '1':
3246 case '2':
3247 case '3':
3248 case '4':
3249 case '5':
3250 case '6':
3251 case '7':
3252 case '8':
3253 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003254 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003256 // Apply "-" and "+" just before the number now, right to
3257 // left. Matters especially when "->" follows. Stops at
3258 // '!'.
3259 if (apply_leader(rettv, TRUE,
3260 start_leader, &end_leader) == FAIL)
3261 {
3262 clear_tv(rettv);
3263 return FAIL;
3264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 break;
3266
3267 /*
3268 * String constant: "string".
3269 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003270 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 return FAIL;
3272 break;
3273
3274 /*
3275 * Literal string constant: 'str''ing'.
3276 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003277 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
3279 break;
3280
3281 /*
3282 * Constant Vim variable.
3283 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003284 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 ret = NOTDONE;
3286 break;
3287
3288 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003289 * "true" constant
3290 */
3291 case 't': if (STRNCMP(*arg, "true", 4) == 0
3292 && !eval_isnamec((*arg)[4]))
3293 {
3294 *arg += 4;
3295 rettv->v_type = VAR_BOOL;
3296 rettv->vval.v_number = VVAL_TRUE;
3297 }
3298 else
3299 ret = NOTDONE;
3300 break;
3301
3302 /*
3303 * "false" constant
3304 */
3305 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3306 && !eval_isnamec((*arg)[5]))
3307 {
3308 *arg += 5;
3309 rettv->v_type = VAR_BOOL;
3310 rettv->vval.v_number = VVAL_FALSE;
3311 }
3312 else
3313 ret = NOTDONE;
3314 break;
3315
3316 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 * List: [expr, expr]
3318 */
3319 case '[': ret = compile_list(arg, cctx);
3320 break;
3321
3322 /*
3323 * Dictionary: #{key: val, key: val}
3324 */
3325 case '#': if ((*arg)[1] == '{')
3326 {
3327 ++*arg;
3328 ret = compile_dict(arg, cctx, TRUE);
3329 }
3330 else
3331 ret = NOTDONE;
3332 break;
3333
3334 /*
3335 * Lambda: {arg, arg -> expr}
3336 * Dictionary: {'key': val, 'key': val}
3337 */
3338 case '{': {
3339 char_u *start = skipwhite(*arg + 1);
3340
3341 // Find out what comes after the arguments.
3342 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003343 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 if (ret != FAIL && *start == '>')
3345 ret = compile_lambda(arg, cctx);
3346 else
3347 ret = compile_dict(arg, cctx, FALSE);
3348 }
3349 break;
3350
3351 /*
3352 * Option value: &name
3353 */
3354 case '&': ret = compile_get_option(arg, cctx);
3355 break;
3356
3357 /*
3358 * Environment variable: $VAR.
3359 */
3360 case '$': ret = compile_get_env(arg, cctx);
3361 break;
3362
3363 /*
3364 * Register contents: @r.
3365 */
3366 case '@': ret = compile_get_register(arg, cctx);
3367 break;
3368 /*
3369 * nested expression: (expression).
3370 */
3371 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003372
3373 // recursive!
3374 if (ppconst->pp_used <= PPSIZE - 10)
3375 {
3376 ret = compile_expr1(arg, cctx, ppconst);
3377 }
3378 else
3379 {
3380 // Not enough space in ppconst, flush constants.
3381 if (generate_ppconst(cctx, ppconst) == FAIL)
3382 return FAIL;
3383 ret = compile_expr0(arg, cctx);
3384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 *arg = skipwhite(*arg);
3386 if (**arg == ')')
3387 ++*arg;
3388 else if (ret == OK)
3389 {
3390 emsg(_(e_missing_close));
3391 ret = FAIL;
3392 }
3393 break;
3394
3395 default: ret = NOTDONE;
3396 break;
3397 }
3398 if (ret == FAIL)
3399 return FAIL;
3400
Bram Moolenaar1c747212020-05-09 18:28:34 +02003401 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003403 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003404 clear_tv(rettv);
3405 else
3406 // A constant expression can possibly be handled compile time,
3407 // return the value instead of generating code.
3408 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 }
3410 else if (ret == NOTDONE)
3411 {
3412 char_u *p;
3413 int r;
3414
3415 if (!eval_isnamec1(**arg))
3416 {
3417 semsg(_("E1015: Name expected: %s"), *arg);
3418 return FAIL;
3419 }
3420
3421 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003422 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003424 {
3425 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003428 {
3429 if (generate_ppconst(cctx, ppconst) == FAIL)
3430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (r == FAIL)
3434 return FAIL;
3435 }
3436
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003437 // Handle following "[]", ".member", etc.
3438 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003439 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003440 ppconst) == FAIL)
3441 return FAIL;
3442 if (ppconst->pp_used > 0)
3443 {
3444 // apply the '!', '-' and '+' before the constant
3445 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003446 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003447 return FAIL;
3448 return OK;
3449 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003450 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003452 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453}
3454
3455/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003456 * Give the "white on both sides" error, taking the operator from "p[len]".
3457 */
3458 void
3459error_white_both(char_u *op, int len)
3460{
3461 char_u buf[10];
3462
3463 vim_strncpy(buf, op, len);
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003464 semsg(_(e_white_space_required_before_and_after), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003465}
3466
3467/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003468 * <type>expr7: runtime type check / conversion
3469 */
3470 static int
3471compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3472{
3473 type_T *want_type = NULL;
3474
3475 // Recognize <type>
3476 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3477 {
3478 int called_emsg_before = called_emsg;
3479
3480 ++*arg;
3481 want_type = parse_type(arg, cctx->ctx_type_list);
3482 if (called_emsg != called_emsg_before)
3483 return FAIL;
3484
3485 if (**arg != '>')
3486 {
3487 if (*skipwhite(*arg) == '>')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003488 semsg(_(e_no_white_space_allowed_before), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003489 else
3490 emsg(_("E1104: Missing >"));
3491 return FAIL;
3492 }
3493 ++*arg;
3494 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3495 return FAIL;
3496 }
3497
3498 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3499 return FAIL;
3500
3501 if (want_type != NULL)
3502 {
3503 garray_T *stack = &cctx->ctx_type_stack;
3504 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3505
3506 if (check_type(want_type, actual, FALSE) == FAIL)
3507 {
3508 generate_ppconst(cctx, ppconst);
3509 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3510 return FAIL;
3511 }
3512 }
3513
3514 return OK;
3515}
3516
3517/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 * * number multiplication
3519 * / number division
3520 * % number modulo
3521 */
3522 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003523compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524{
3525 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003526 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003527 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003529 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003530 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 return FAIL;
3532
3533 /*
3534 * Repeat computing, until no "*", "/" or "%" is following.
3535 */
3536 for (;;)
3537 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003538 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 if (*op != '*' && *op != '/' && *op != '%')
3540 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003541 if (next != NULL)
3542 {
3543 *arg = next_line_from_context(cctx, TRUE);
3544 op = skipwhite(*arg);
3545 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003546
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003547 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003549 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003550 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003553 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003556 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003557 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003559
3560 if (ppconst->pp_used == ppconst_used + 2
3561 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3562 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003563 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003564 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3565 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003566 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003568 // both are numbers: compute the result
3569 switch (*op)
3570 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003571 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003572 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003573 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003574 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003575 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003576 break;
3577 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003578 tv1->vval.v_number = res;
3579 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003580 }
3581 else
3582 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003583 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003584 generate_two_op(cctx, op);
3585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
3587
3588 return OK;
3589}
3590
3591/*
3592 * + number addition
3593 * - number subtraction
3594 * .. string concatenation
3595 */
3596 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003597compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598{
3599 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003600 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003602 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603
3604 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 return FAIL;
3607
3608 /*
3609 * Repeat computing, until no "+", "-" or ".." is following.
3610 */
3611 for (;;)
3612 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003613 op = may_peek_next_line(cctx, *arg, &next);
3614 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 break;
3616 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003617 if (next != NULL)
3618 {
3619 *arg = next_line_from_context(cctx, TRUE);
3620 op = skipwhite(*arg);
3621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003623 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003625 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 }
3628
3629 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003630 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003631 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003634 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 return FAIL;
3636
Bram Moolenaara5565e42020-05-09 15:44:01 +02003637 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003638 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003639 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3640 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3641 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3642 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003644 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3645 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003646
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 // concat/subtract/add constant numbers
3648 if (*op == '+')
3649 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3650 else if (*op == '-')
3651 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3652 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003653 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003654 // concatenate constant strings
3655 char_u *s1 = tv1->vval.v_string;
3656 char_u *s2 = tv2->vval.v_string;
3657 size_t len1 = STRLEN(s1);
3658
3659 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3660 if (tv1->vval.v_string == NULL)
3661 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003662 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003663 return FAIL;
3664 }
3665 mch_memmove(tv1->vval.v_string, s1, len1);
3666 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667 vim_free(s1);
3668 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003669 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003670 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
3672 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003673 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003674 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003675 if (*op == '.')
3676 {
3677 if (may_generate_2STRING(-2, cctx) == FAIL
3678 || may_generate_2STRING(-1, cctx) == FAIL)
3679 return FAIL;
3680 generate_instr_drop(cctx, ISN_CONCAT, 1);
3681 }
3682 else
3683 generate_two_op(cctx, op);
3684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 }
3686
3687 return OK;
3688}
3689
3690/*
3691 * expr5a == expr5b
3692 * expr5a =~ expr5b
3693 * expr5a != expr5b
3694 * expr5a !~ expr5b
3695 * expr5a > expr5b
3696 * expr5a >= expr5b
3697 * expr5a < expr5b
3698 * expr5a <= expr5b
3699 * expr5a is expr5b
3700 * expr5a isnot expr5b
3701 *
3702 * Produces instructions:
3703 * EVAL expr5a Push result of "expr5a"
3704 * EVAL expr5b Push result of "expr5b"
3705 * COMPARE one of the compare instructions
3706 */
3707 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003708compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709{
3710 exptype_T type = EXPR_UNKNOWN;
3711 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003712 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003715 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716
3717 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003718 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
3720
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003721 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003722 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
3724 /*
3725 * If there is a comparative operator, use it.
3726 */
3727 if (type != EXPR_UNKNOWN)
3728 {
3729 int ic = FALSE; // Default: do not ignore case
3730
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003731 if (next != NULL)
3732 {
3733 *arg = next_line_from_context(cctx, TRUE);
3734 p = skipwhite(*arg);
3735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 if (type_is && (p[len] == '?' || p[len] == '#'))
3737 {
3738 semsg(_(e_invexpr2), *arg);
3739 return FAIL;
3740 }
3741 // extra question mark appended: ignore case
3742 if (p[len] == '?')
3743 {
3744 ic = TRUE;
3745 ++len;
3746 }
3747 // extra '#' appended: match case (ignored)
3748 else if (p[len] == '#')
3749 ++len;
3750 // nothing appended: match case
3751
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003752 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003754 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003755 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 }
3757
3758 // get the second variable
3759 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003760 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003761 return FAIL;
3762
Bram Moolenaara5565e42020-05-09 15:44:01 +02003763 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 return FAIL;
3765
Bram Moolenaara5565e42020-05-09 15:44:01 +02003766 if (ppconst->pp_used == ppconst_used + 2)
3767 {
3768 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3769 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3770 int ret;
3771
3772 // Both sides are a constant, compute the result now.
3773 // First check for a valid combination of types, this is more
3774 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003775 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003776 ret = FAIL;
3777 else
3778 {
3779 ret = typval_compare(tv1, tv2, type, ic);
3780 tv1->v_type = VAR_BOOL;
3781 tv1->vval.v_number = tv1->vval.v_number
3782 ? VVAL_TRUE : VVAL_FALSE;
3783 clear_tv(tv2);
3784 --ppconst->pp_used;
3785 }
3786 return ret;
3787 }
3788
3789 generate_ppconst(cctx, ppconst);
3790 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 }
3792
3793 return OK;
3794}
3795
Bram Moolenaar7f141552020-05-09 17:35:53 +02003796static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798/*
3799 * Compile || or &&.
3800 */
3801 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003802compile_and_or(
3803 char_u **arg,
3804 cctx_T *cctx,
3805 char *op,
3806 ppconst_T *ppconst,
3807 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003809 char_u *next;
3810 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 int opchar = *op;
3812
3813 if (p[0] == opchar && p[1] == opchar)
3814 {
3815 garray_T *instr = &cctx->ctx_instr;
3816 garray_T end_ga;
3817
3818 /*
3819 * Repeat until there is no following "||" or "&&"
3820 */
3821 ga_init2(&end_ga, sizeof(int), 10);
3822 while (p[0] == opchar && p[1] == opchar)
3823 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003824 if (next != NULL)
3825 {
3826 *arg = next_line_from_context(cctx, TRUE);
3827 p = skipwhite(*arg);
3828 }
3829
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003830 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3831 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003832 semsg(_(e_white_space_required_before_and_after), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003833 return FAIL;
3834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 // TODO: use ppconst if the value is a constant
3837 generate_ppconst(cctx, ppconst);
3838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 if (ga_grow(&end_ga, 1) == FAIL)
3840 {
3841 ga_clear(&end_ga);
3842 return FAIL;
3843 }
3844 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3845 ++end_ga.ga_len;
3846 generate_JUMP(cctx, opchar == '|'
3847 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3848
3849 // eval the next expression
3850 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003851 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003852 return FAIL;
3853
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3855 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 {
3857 ga_clear(&end_ga);
3858 return FAIL;
3859 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003860
3861 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003863 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864
3865 // Fill in the end label in all jumps.
3866 while (end_ga.ga_len > 0)
3867 {
3868 isn_T *isn;
3869
3870 --end_ga.ga_len;
3871 isn = ((isn_T *)instr->ga_data)
3872 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3873 isn->isn_arg.jump.jump_where = instr->ga_len;
3874 }
3875 ga_clear(&end_ga);
3876 }
3877
3878 return OK;
3879}
3880
3881/*
3882 * expr4a && expr4a && expr4a logical AND
3883 *
3884 * Produces instructions:
3885 * EVAL expr4a Push result of "expr4a"
3886 * JUMP_AND_KEEP_IF_FALSE end
3887 * EVAL expr4b Push result of "expr4b"
3888 * JUMP_AND_KEEP_IF_FALSE end
3889 * EVAL expr4c Push result of "expr4c"
3890 * end:
3891 */
3892 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003893compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895 int ppconst_used = ppconst->pp_used;
3896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003898 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 return FAIL;
3900
3901 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003902 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903}
3904
3905/*
3906 * expr3a || expr3b || expr3c logical OR
3907 *
3908 * Produces instructions:
3909 * EVAL expr3a Push result of "expr3a"
3910 * JUMP_AND_KEEP_IF_TRUE end
3911 * EVAL expr3b Push result of "expr3b"
3912 * JUMP_AND_KEEP_IF_TRUE end
3913 * EVAL expr3c Push result of "expr3c"
3914 * end:
3915 */
3916 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003917compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 int ppconst_used = ppconst->pp_used;
3920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 return FAIL;
3924
3925 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003926 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927}
3928
3929/*
3930 * Toplevel expression: expr2 ? expr1a : expr1b
3931 *
3932 * Produces instructions:
3933 * EVAL expr2 Push result of "expr"
3934 * JUMP_IF_FALSE alt jump if false
3935 * EVAL expr1a
3936 * JUMP_ALWAYS end
3937 * alt: EVAL expr1b
3938 * end:
3939 */
3940 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942{
3943 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003944 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003945 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946
Bram Moolenaar61a89812020-05-07 16:58:17 +02003947 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003951 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (*p == '?')
3953 {
3954 garray_T *instr = &cctx->ctx_instr;
3955 garray_T *stack = &cctx->ctx_type_stack;
3956 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003957 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003959 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 int has_const_expr = FALSE;
3962 int const_value = FALSE;
3963 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003965 if (next != NULL)
3966 {
3967 *arg = next_line_from_context(cctx, TRUE);
3968 p = skipwhite(*arg);
3969 }
3970
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003971 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3972 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003973 semsg(_(e_white_space_required_before_and_after), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003974 return FAIL;
3975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
Bram Moolenaara5565e42020-05-09 15:44:01 +02003977 if (ppconst->pp_used == ppconst_used + 1)
3978 {
3979 // the condition is a constant, we know whether the ? or the :
3980 // expression is to be evaluated.
3981 has_const_expr = TRUE;
3982 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3983 clear_tv(&ppconst->pp_tv[ppconst_used]);
3984 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003985 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3986 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 }
3988 else
3989 {
3990 generate_ppconst(cctx, ppconst);
3991 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
3994 // evaluate the second expression; any type is accepted
3995 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003996 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003997 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003998 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003999 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000
Bram Moolenaara5565e42020-05-09 15:44:01 +02004001 if (!has_const_expr)
4002 {
4003 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005 // remember the type and drop it
4006 --stack->ga_len;
4007 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 end_idx = instr->ga_len;
4010 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4011
4012 // jump here from JUMP_IF_FALSE
4013 isn = ((isn_T *)instr->ga_data) + alt_idx;
4014 isn->isn_arg.jump.jump_where = instr->ga_len;
4015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
4017 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004018 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 if (*p != ':')
4020 {
4021 emsg(_(e_missing_colon));
4022 return FAIL;
4023 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004024 if (next != NULL)
4025 {
4026 *arg = next_line_from_context(cctx, TRUE);
4027 p = skipwhite(*arg);
4028 }
4029
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004030 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4031 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004032 semsg(_(e_white_space_required_before_and_after), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004033 return FAIL;
4034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035
4036 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004037 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004038 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4039 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004041 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004043 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004044 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045
Bram Moolenaara5565e42020-05-09 15:44:01 +02004046 if (!has_const_expr)
4047 {
4048 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 // If the types differ, the result has a more generic type.
4051 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4052 common_type(type1, type2, &type2, cctx->ctx_type_list);
4053
4054 // jump here from JUMP_ALWAYS
4055 isn = ((isn_T *)instr->ga_data) + end_idx;
4056 isn->isn_arg.jump.jump_where = instr->ga_len;
4057 }
4058
4059 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 }
4061 return OK;
4062}
4063
4064/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004065 * Toplevel expression.
4066 */
4067 static int
4068compile_expr0(char_u **arg, cctx_T *cctx)
4069{
4070 ppconst_T ppconst;
4071
4072 CLEAR_FIELD(ppconst);
4073 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4074 {
4075 clear_ppconst(&ppconst);
4076 return FAIL;
4077 }
4078 if (generate_ppconst(cctx, &ppconst) == FAIL)
4079 return FAIL;
4080 return OK;
4081}
4082
4083/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 * compile "return [expr]"
4085 */
4086 static char_u *
4087compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4088{
4089 char_u *p = arg;
4090 garray_T *stack = &cctx->ctx_type_stack;
4091 type_T *stack_type;
4092
4093 if (*p != NUL && *p != '|' && *p != '\n')
4094 {
4095 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return NULL;
4098
4099 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4100 if (set_return_type)
4101 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004102 else
4103 {
4104 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4105 && stack_type->tt_type != VAR_VOID
4106 && stack_type->tt_type != VAR_UNKNOWN)
4107 {
4108 emsg(_("E1096: Returning a value in a function without a return type"));
4109 return NULL;
4110 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004111 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4112 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
4116 else
4117 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004118 // "set_return_type" cannot be TRUE, only used for a lambda which
4119 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004120 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4121 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
4123 emsg(_("E1003: Missing return value"));
4124 return NULL;
4125 }
4126
4127 // No argument, return zero.
4128 generate_PUSHNR(cctx, 0);
4129 }
4130
4131 if (generate_instr(cctx, ISN_RETURN) == NULL)
4132 return NULL;
4133
4134 // "return val | endif" is possible
4135 return skipwhite(p);
4136}
4137
4138/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004139 * Get a line from the compilation context, compatible with exarg_T getline().
4140 * Return a pointer to the line in allocated memory.
4141 * Return NULL for end-of-file or some error.
4142 */
4143 static char_u *
4144exarg_getline(
4145 int c UNUSED,
4146 void *cookie,
4147 int indent UNUSED,
4148 int do_concat UNUSED)
4149{
4150 cctx_T *cctx = (cctx_T *)cookie;
4151
4152 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4153 {
4154 iemsg("Heredoc got to end");
4155 return NULL;
4156 }
4157 ++cctx->ctx_lnum;
4158 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4159 [cctx->ctx_lnum]);
4160}
4161
4162/*
4163 * Compile a nested :def command.
4164 */
4165 static char_u *
4166compile_nested_function(exarg_T *eap, cctx_T *cctx)
4167{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004168 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004169 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004170 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004171 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004172 lvar_T *lvar;
4173 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004174 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004175
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004176 // Only g:Func() can use a namespace.
4177 if (name_start[1] == ':' && !is_global)
4178 {
4179 semsg(_(e_namespace), name_start);
4180 return NULL;
4181 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004182 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4183 return NULL;
4184
Bram Moolenaar04b12692020-05-04 23:24:44 +02004185 eap->arg = name_end;
4186 eap->getline = exarg_getline;
4187 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004188 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004189 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004190 lambda_name = get_lambda_name();
4191 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004192
Bram Moolenaar822ba242020-05-24 23:00:18 +02004193 if (ufunc == NULL)
4194 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004195 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004196 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004197 return NULL;
4198
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004199 if (is_global)
4200 {
4201 char_u *func_name = vim_strnsave(name_start + 2,
4202 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004203
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004204 if (func_name == NULL)
4205 r = FAIL;
4206 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004207 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004208 }
4209 else
4210 {
4211 // Define a local variable for the function reference.
4212 lvar = reserve_local(cctx, name_start, name_end - name_start,
4213 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004214 if (lvar == NULL)
4215 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004216 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004217 return NULL;
4218 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4219 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004220
Bram Moolenaar61a89812020-05-07 16:58:17 +02004221 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004222 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004223}
4224
4225/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 * Return the length of an assignment operator, or zero if there isn't one.
4227 */
4228 int
4229assignment_len(char_u *p, int *heredoc)
4230{
4231 if (*p == '=')
4232 {
4233 if (p[1] == '<' && p[2] == '<')
4234 {
4235 *heredoc = TRUE;
4236 return 3;
4237 }
4238 return 1;
4239 }
4240 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4241 return 2;
4242 if (STRNCMP(p, "..=", 3) == 0)
4243 return 3;
4244 return 0;
4245}
4246
4247// words that cannot be used as a variable
4248static char *reserved[] = {
4249 "true",
4250 "false",
4251 NULL
4252};
4253
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004254typedef enum {
4255 dest_local,
4256 dest_option,
4257 dest_env,
4258 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004259 dest_buffer,
4260 dest_window,
4261 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004262 dest_vimvar,
4263 dest_script,
4264 dest_reg,
4265} assign_dest_T;
4266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004268 * Generate the load instruction for "name".
4269 */
4270 static void
4271generate_loadvar(
4272 cctx_T *cctx,
4273 assign_dest_T dest,
4274 char_u *name,
4275 lvar_T *lvar,
4276 type_T *type)
4277{
4278 switch (dest)
4279 {
4280 case dest_option:
4281 // TODO: check the option exists
4282 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4283 break;
4284 case dest_global:
4285 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4286 break;
4287 case dest_buffer:
4288 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4289 break;
4290 case dest_window:
4291 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4292 break;
4293 case dest_tab:
4294 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4295 break;
4296 case dest_script:
4297 compile_load_scriptvar(cctx,
4298 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4299 break;
4300 case dest_env:
4301 // Include $ in the name here
4302 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4303 break;
4304 case dest_reg:
4305 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4306 break;
4307 case dest_vimvar:
4308 generate_LOADV(cctx, name + 2, TRUE);
4309 break;
4310 case dest_local:
4311 if (lvar->lv_from_outer)
4312 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4313 NULL, type);
4314 else
4315 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4316 break;
4317 }
4318}
4319
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004320 void
4321vim9_declare_error(char_u *name)
4322{
4323 char *scope = "";
4324
4325 switch (*name)
4326 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004327 case 'g': scope = _("global"); break;
4328 case 'b': scope = _("buffer"); break;
4329 case 'w': scope = _("window"); break;
4330 case 't': scope = _("tab"); break;
4331 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004332 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004333 return;
4334 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4335 return;
4336 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4337 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004338 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004339 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004340 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004341}
4342
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004343/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004344 * Compile declaration and assignment:
4345 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004347 * Return NULL for an error.
4348 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 */
4350 static char_u *
4351compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4352{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004353 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004355 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 char_u *ret = NULL;
4357 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004358 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004359 int scriptvar_sid = 0;
4360 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004363 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 int oplen = 0;
4366 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004367 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004368 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004369 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004373 // Skip over the "var" or "[var, var]" to get to any "=".
4374 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4375 if (p == NULL)
4376 return *arg == '[' ? arg : NULL;
4377
4378 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004380 // TODO: should we allow this, and figure out type inference from list
4381 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004382 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 return NULL;
4384 }
4385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 sp = p;
4387 p = skipwhite(p);
4388 op = p;
4389 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004390
4391 if (var_count > 0 && oplen == 0)
4392 // can be something like "[1, 2]->func()"
4393 return arg;
4394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4396 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004397 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004398 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004399 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 if (heredoc)
4402 {
4403 list_T *l;
4404 listitem_T *li;
4405
4406 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004407 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004409 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
4411 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004412 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 {
4414 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4415 li->li_tv.vval.v_string = NULL;
4416 }
4417 generate_NEWLIST(cctx, l->lv_len);
4418 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004419 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 list_free(l);
4421 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004422 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004424 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004426 // for "[var, var] = expr" evaluate the expression here, loop over the
4427 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004428
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004429 p = skipwhite(op + oplen);
4430 if (compile_expr0(&p, cctx) == FAIL)
4431 return NULL;
4432 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004434 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436 type_T *stacktype;
4437
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004438 stacktype = stack->ga_len == 0 ? &t_void
4439 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004440 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004442 emsg(_(e_cannot_use_void));
4443 goto theend;
4444 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004445 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004446 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004447 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004448 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4449 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004450 }
4451 }
4452
4453 /*
4454 * Loop over variables in "[var, var] = expr".
4455 * For "var = expr" and "let var: type" this is done only once.
4456 */
4457 if (var_count > 0)
4458 var_start = skipwhite(arg + 1); // skip over the "["
4459 else
4460 var_start = arg;
4461 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4462 {
4463 char_u *var_end = skip_var_one(var_start, FALSE);
4464 size_t varlen;
4465 int new_local = FALSE;
4466 int opt_type;
4467 int opt_flags = 0;
4468 assign_dest_T dest = dest_local;
4469 int vimvaridx = -1;
4470 lvar_T *lvar = NULL;
4471 lvar_T arg_lvar;
4472 int has_type = FALSE;
4473 int has_index = FALSE;
4474 int instr_count = -1;
4475
Bram Moolenaar65821722020-08-02 18:58:54 +02004476 if (*var_start == '@')
4477 p = var_start + 2;
4478 else
4479 {
4480 p = (*var_start == '&' || *var_start == '$')
4481 ? var_start + 1 : var_start;
4482 p = to_name_end(p, TRUE);
4483 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004484
4485 // "a: type" is declaring variable "a" with a type, not "a:".
4486 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4487 --var_end;
4488 if (is_decl && p == var_start + 2 && p[-1] == ':')
4489 --p;
4490
4491 varlen = p - var_start;
4492 vim_free(name);
4493 name = vim_strnsave(var_start, varlen);
4494 if (name == NULL)
4495 return NULL;
4496 if (!heredoc)
4497 type = &t_any;
4498
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004499 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004500 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004501 int declare_error = FALSE;
4502
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004503 if (*var_start == '&')
4504 {
4505 int cc;
4506 long numval;
4507
4508 dest = dest_option;
4509 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 emsg(_(e_const_option));
4512 goto theend;
4513 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004514 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004515 p = var_start;
4516 p = find_option_end(&p, &opt_flags);
4517 if (p == NULL)
4518 {
4519 // cannot happen?
4520 emsg(_(e_letunexp));
4521 goto theend;
4522 }
4523 cc = *p;
4524 *p = NUL;
4525 opt_type = get_option_value(var_start + 1, &numval,
4526 NULL, opt_flags);
4527 *p = cc;
4528 if (opt_type == -3)
4529 {
4530 semsg(_(e_unknown_option), var_start);
4531 goto theend;
4532 }
4533 if (opt_type == -2 || opt_type == 0)
4534 type = &t_string;
4535 else
4536 type = &t_number; // both number and boolean option
4537 }
4538 else if (*var_start == '$')
4539 {
4540 dest = dest_env;
4541 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004542 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004543 }
4544 else if (*var_start == '@')
4545 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004546 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004547 {
4548 emsg_invreg(var_start[1]);
4549 goto theend;
4550 }
4551 dest = dest_reg;
4552 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004553 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004554 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004555 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004556 {
4557 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004558 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004559 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004560 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004561 {
4562 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004563 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004565 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004566 {
4567 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004568 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004569 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004570 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004571 {
4572 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004573 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004574 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004575 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004576 {
4577 typval_T *vtv;
4578 int di_flags;
4579
4580 vimvaridx = find_vim_var(name + 2, &di_flags);
4581 if (vimvaridx < 0)
4582 {
4583 semsg(_(e_var_notfound), var_start);
4584 goto theend;
4585 }
4586 // We use the current value of "sandbox" here, is that OK?
4587 if (var_check_ro(di_flags, name, FALSE))
4588 goto theend;
4589 dest = dest_vimvar;
4590 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004591 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004592 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004593 }
4594 else
4595 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004596 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004597
4598 for (idx = 0; reserved[idx] != NULL; ++idx)
4599 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004600 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004602 goto theend;
4603 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004604
4605 lvar = lookup_local(var_start, varlen, cctx);
4606 if (lvar == NULL)
4607 {
4608 CLEAR_FIELD(arg_lvar);
4609 if (lookup_arg(var_start, varlen,
4610 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4611 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004612 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004613 if (is_decl)
4614 {
4615 semsg(_(e_used_as_arg), name);
4616 goto theend;
4617 }
4618 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004619 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004620 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 if (lvar != NULL)
4622 {
4623 if (is_decl)
4624 {
4625 semsg(_("E1017: Variable already declared: %s"), name);
4626 goto theend;
4627 }
4628 else if (lvar->lv_const)
4629 {
4630 semsg(_("E1018: Cannot assign to a constant: %s"),
4631 name);
4632 goto theend;
4633 }
4634 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004635 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004637 int script_namespace = varlen > 1
4638 && STRNCMP(var_start, "s:", 2) == 0;
4639 int script_var = (script_namespace
4640 ? lookup_script(var_start + 2, varlen - 2)
4641 : lookup_script(var_start, varlen)) == OK;
4642 imported_T *import =
4643 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004644
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004645 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004647 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4648
4649 if (is_decl)
4650 {
4651 if (script_namespace)
4652 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004653 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004654 else
4655 semsg(_("E1054: Variable already declared in the script: %s"),
4656 name);
4657 goto theend;
4658 }
4659 else if (cctx->ctx_ufunc->uf_script_ctx_version
4660 == SCRIPT_VERSION_VIM9
4661 && script_namespace
4662 && !script_var && import == NULL)
4663 {
4664 semsg(_(e_unknown_var), name);
4665 goto theend;
4666 }
4667
4668 dest = dest_script;
4669
4670 // existing script-local variables should have a type
4671 scriptvar_sid = current_sctx.sc_sid;
4672 if (import != NULL)
4673 scriptvar_sid = import->imp_sid;
4674 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4675 rawname, TRUE);
4676 if (scriptvar_idx >= 0)
4677 {
4678 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4679 svar_T *sv =
4680 ((svar_T *)si->sn_var_vals.ga_data)
4681 + scriptvar_idx;
4682 type = sv->sv_type;
4683 }
4684 }
4685 else if (name[1] == ':' && name[2] != NUL)
4686 {
4687 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004688 name);
4689 goto theend;
4690 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004691 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004692 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004693 semsg(_(e_unknown_var), name);
4694 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004695 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004696 else if (check_defined(var_start, varlen, cctx) == FAIL)
4697 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004698 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004699 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004700
4701 if (declare_error)
4702 {
4703 vim9_declare_error(name);
4704 goto theend;
4705 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 }
4707
4708 // handle "a:name" as a name, not index "name" on "a"
4709 if (varlen > 1 || var_start[varlen] != ':')
4710 p = var_end;
4711
4712 if (dest != dest_option)
4713 {
4714 if (is_decl && *p == ':')
4715 {
4716 // parse optional type: "let var: type = expr"
4717 if (!VIM_ISWHITE(p[1]))
4718 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004719 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 goto theend;
4721 }
4722 p = skipwhite(p + 1);
4723 type = parse_type(&p, cctx->ctx_type_list);
4724 has_type = TRUE;
4725 }
4726 else if (lvar != NULL)
4727 type = lvar->lv_type;
4728 }
4729
4730 if (oplen == 3 && !heredoc && dest != dest_global
4731 && type->tt_type != VAR_STRING
4732 && type->tt_type != VAR_ANY)
4733 {
4734 emsg(_("E1019: Can only concatenate to string"));
4735 goto theend;
4736 }
4737
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004738 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 {
4740 if (oplen > 1 && !heredoc)
4741 {
4742 // +=, /=, etc. require an existing variable
4743 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4744 name);
4745 goto theend;
4746 }
4747
4748 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004749 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4750 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004751 goto theend;
4752 lvar = reserve_local(cctx, var_start, varlen,
4753 cmdidx == CMD_const, type);
4754 if (lvar == NULL)
4755 goto theend;
4756 new_local = TRUE;
4757 }
4758
4759 member_type = type;
4760 if (var_end > var_start + varlen)
4761 {
4762 // Something follows after the variable: "var[idx]".
4763 if (is_decl)
4764 {
4765 emsg(_("E1087: cannot use an index when declaring a variable"));
4766 goto theend;
4767 }
4768
4769 if (var_start[varlen] == '[')
4770 {
4771 has_index = TRUE;
4772 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004773 member_type = &t_any;
4774 else
4775 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 }
4777 else
4778 {
4779 semsg("Not supported yet: %s", var_start);
4780 goto theend;
4781 }
4782 }
4783 else if (lvar == &arg_lvar)
4784 {
4785 semsg(_("E1090: Cannot assign to argument %s"), name);
4786 goto theend;
4787 }
4788
4789 if (!heredoc)
4790 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004791 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004793 if (oplen > 0 && var_count == 0)
4794 {
4795 // skip over the "=" and the expression
4796 p = skipwhite(op + oplen);
4797 compile_expr0(&p, cctx);
4798 }
4799 }
4800 else if (oplen > 0)
4801 {
4802 type_T *stacktype;
4803
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 // For "var = expr" evaluate the expression.
4805 if (var_count == 0)
4806 {
4807 int r;
4808
4809 // for "+=", "*=", "..=" etc. first load the current value
4810 if (*op != '=')
4811 {
4812 generate_loadvar(cctx, dest, name, lvar, type);
4813
4814 if (has_index)
4815 {
4816 // TODO: get member from list or dict
4817 emsg("Index with operation not supported yet");
4818 goto theend;
4819 }
4820 }
4821
4822 // Compile the expression. Temporarily hide the new local
4823 // variable here, it is not available to this expression.
4824 if (new_local)
4825 --cctx->ctx_locals.ga_len;
4826 instr_count = instr->ga_len;
4827 p = skipwhite(op + oplen);
4828 r = compile_expr0(&p, cctx);
4829 if (new_local)
4830 ++cctx->ctx_locals.ga_len;
4831 if (r == FAIL)
4832 goto theend;
4833 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004834 else if (semicolon && var_idx == var_count - 1)
4835 {
4836 // For "[var; var] = expr" get the rest of the list
4837 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4838 goto theend;
4839 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004840 else
4841 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004842 // For "[var, var] = expr" get the "var_idx" item from the
4843 // list.
4844 if (generate_GETITEM(cctx, var_idx) == FAIL)
4845 return FAIL;
4846 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004847
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004848 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004849 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004850 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004852 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004853 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004854 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004856 emsg(_(e_cannot_use_void));
4857 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004859 else if ((stacktype->tt_type == VAR_FUNC
4860 || stacktype->tt_type == VAR_PARTIAL)
4861 && var_wrong_func_name(name, TRUE))
4862 {
4863 goto theend;
4864 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 else
4866 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004867 // An empty list or dict has a &t_void member,
4868 // for a variable that implies &t_any.
4869 if (stacktype == &t_list_empty)
4870 lvar->lv_type = &t_list_any;
4871 else if (stacktype == &t_dict_empty)
4872 lvar->lv_type = &t_dict_any;
4873 else
4874 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004875 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004876 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004877 else
4878 {
4879 type_T *use_type = lvar->lv_type;
4880
4881 if (has_index)
4882 {
4883 use_type = use_type->tt_member;
4884 if (use_type == NULL)
4885 use_type = &t_void;
4886 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004887 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004888 == FAIL)
4889 goto theend;
4890 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004891 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004892 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004893 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004894 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004898 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004899 goto theend;
4900 }
4901 else if (!has_type || dest == dest_option)
4902 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004903 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 goto theend;
4905 }
4906 else
4907 {
4908 // variables are always initialized
4909 if (ga_grow(instr, 1) == FAIL)
4910 goto theend;
4911 switch (member_type->tt_type)
4912 {
4913 case VAR_BOOL:
4914 generate_PUSHBOOL(cctx, VVAL_FALSE);
4915 break;
4916 case VAR_FLOAT:
4917#ifdef FEAT_FLOAT
4918 generate_PUSHF(cctx, 0.0);
4919#endif
4920 break;
4921 case VAR_STRING:
4922 generate_PUSHS(cctx, NULL);
4923 break;
4924 case VAR_BLOB:
4925 generate_PUSHBLOB(cctx, NULL);
4926 break;
4927 case VAR_FUNC:
4928 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4929 break;
4930 case VAR_LIST:
4931 generate_NEWLIST(cctx, 0);
4932 break;
4933 case VAR_DICT:
4934 generate_NEWDICT(cctx, 0);
4935 break;
4936 case VAR_JOB:
4937 generate_PUSHJOB(cctx, NULL);
4938 break;
4939 case VAR_CHANNEL:
4940 generate_PUSHCHANNEL(cctx, NULL);
4941 break;
4942 case VAR_NUMBER:
4943 case VAR_UNKNOWN:
4944 case VAR_ANY:
4945 case VAR_PARTIAL:
4946 case VAR_VOID:
4947 case VAR_SPECIAL: // cannot happen
4948 generate_PUSHNR(cctx, 0);
4949 break;
4950 }
4951 }
4952 if (var_count == 0)
4953 end = p;
4954 }
4955
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004956 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004957 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004958 break;
4959
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 if (oplen > 0 && *op != '=')
4961 {
4962 type_T *expected = &t_number;
4963 type_T *stacktype;
4964
4965 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004966 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967
4968 if (*op == '.')
4969 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004970 else if (*op == '+')
4971 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004973 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 goto theend;
4975
4976 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004977 {
4978 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4979 goto theend;
4980 }
4981 else if (*op == '+')
4982 {
4983 if (generate_add_instr(cctx,
4984 operator_type(member_type, stacktype),
4985 member_type, stacktype) == FAIL)
4986 goto theend;
4987 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 else
4989 {
4990 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4991
4992 if (isn == NULL)
4993 goto theend;
4994 switch (*op)
4995 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4997 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4998 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4999 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 }
5002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005005 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005006 int r;
5007
5008 // Compile the "idx" in "var[idx]".
5009 if (new_local)
5010 --cctx->ctx_locals.ga_len;
5011 p = skipwhite(var_start + varlen + 1);
5012 r = compile_expr0(&p, cctx);
5013 if (new_local)
5014 ++cctx->ctx_locals.ga_len;
5015 if (r == FAIL)
5016 goto theend;
5017 if (*skipwhite(p) != ']')
5018 {
5019 emsg(_(e_missbrac));
5020 goto theend;
5021 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005022 if (type == &t_any)
5023 {
5024 type_T *idx_type = ((type_T **)stack->ga_data)[
5025 stack->ga_len - 1];
5026 // Index on variable of unknown type: guess the type from the
5027 // index type: number is dict, otherwise dict.
5028 // TODO: should do the assignment at runtime
5029 if (idx_type->tt_type == VAR_NUMBER)
5030 type = &t_list_any;
5031 else
5032 type = &t_dict_any;
5033 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 if (type->tt_type == VAR_DICT
5035 && may_generate_2STRING(-1, cctx) == FAIL)
5036 goto theend;
5037 if (type->tt_type == VAR_LIST
5038 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005039 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 {
5041 emsg(_(e_number_exp));
5042 goto theend;
5043 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005045 // Load the dict or list. On the stack we then have:
5046 // - value
5047 // - index
5048 // - variable
5049 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005050
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 if (type->tt_type == VAR_LIST)
5052 {
5053 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5054 return FAIL;
5055 }
5056 else if (type->tt_type == VAR_DICT)
5057 {
5058 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5059 return FAIL;
5060 }
5061 else
5062 {
5063 emsg(_(e_listreq));
5064 goto theend;
5065 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005066 }
5067 else
5068 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005069 switch (dest)
5070 {
5071 case dest_option:
5072 generate_STOREOPT(cctx, name + 1, opt_flags);
5073 break;
5074 case dest_global:
5075 // include g: with the name, easier to execute that way
5076 generate_STORE(cctx, ISN_STOREG, 0, name);
5077 break;
5078 case dest_buffer:
5079 // include b: with the name, easier to execute that way
5080 generate_STORE(cctx, ISN_STOREB, 0, name);
5081 break;
5082 case dest_window:
5083 // include w: with the name, easier to execute that way
5084 generate_STORE(cctx, ISN_STOREW, 0, name);
5085 break;
5086 case dest_tab:
5087 // include t: with the name, easier to execute that way
5088 generate_STORE(cctx, ISN_STORET, 0, name);
5089 break;
5090 case dest_env:
5091 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5092 break;
5093 case dest_reg:
5094 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5095 break;
5096 case dest_vimvar:
5097 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5098 break;
5099 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005100 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005101 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 {
5103 char_u *name_s = name;
5104
5105 // Include s: in the name for store_var()
5106 if (name[1] != ':')
5107 {
5108 int len = (int)STRLEN(name) + 3;
5109
5110 name_s = alloc(len);
5111 if (name_s == NULL)
5112 name_s = name;
5113 else
5114 vim_snprintf((char *)name_s, len,
5115 "s:%s", name);
5116 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005117 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5118 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 if (name_s != name)
5120 vim_free(name_s);
5121 }
5122 else
5123 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005124 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005125 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 break;
5127 case dest_local:
5128 if (lvar != NULL)
5129 {
5130 isn_T *isn = ((isn_T *)instr->ga_data)
5131 + instr->ga_len - 1;
5132
5133 // optimization: turn "var = 123" from ISN_PUSHNR +
5134 // ISN_STORE into ISN_STORENR
5135 if (!lvar->lv_from_outer
5136 && instr->ga_len == instr_count + 1
5137 && isn->isn_type == ISN_PUSHNR)
5138 {
5139 varnumber_T val = isn->isn_arg.number;
5140
5141 isn->isn_type = ISN_STORENR;
5142 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5143 isn->isn_arg.storenr.stnr_val = val;
5144 if (stack->ga_len > 0)
5145 --stack->ga_len;
5146 }
5147 else if (lvar->lv_from_outer)
5148 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005149 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 else
5151 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5152 }
5153 break;
5154 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005155 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156
5157 if (var_idx + 1 < var_count)
5158 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005160
5161 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005162 if (var_count > 0 && !semicolon)
5163 {
5164 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5165 goto theend;
5166 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167
Bram Moolenaarb2097502020-07-19 17:17:02 +02005168 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169
5170theend:
5171 vim_free(name);
5172 return ret;
5173}
5174
5175/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005176 * Check if "name" can be "unlet".
5177 */
5178 int
5179check_vim9_unlet(char_u *name)
5180{
5181 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5182 {
5183 semsg(_("E1081: Cannot unlet %s"), name);
5184 return FAIL;
5185 }
5186 return OK;
5187}
5188
5189/*
5190 * Callback passed to ex_unletlock().
5191 */
5192 static int
5193compile_unlet(
5194 lval_T *lvp,
5195 char_u *name_end,
5196 exarg_T *eap,
5197 int deep UNUSED,
5198 void *coookie)
5199{
5200 cctx_T *cctx = coookie;
5201
5202 if (lvp->ll_tv == NULL)
5203 {
5204 char_u *p = lvp->ll_name;
5205 int cc = *name_end;
5206 int ret = OK;
5207
5208 // Normal name. Only supports g:, w:, t: and b: namespaces.
5209 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005210 if (*p == '$')
5211 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5212 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005213 ret = FAIL;
5214 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005215 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005216
5217 *name_end = cc;
5218 return ret;
5219 }
5220
5221 // TODO: unlet {list}[idx]
5222 // TODO: unlet {dict}[key]
5223 emsg("Sorry, :unlet not fully implemented yet");
5224 return FAIL;
5225}
5226
5227/*
5228 * compile "unlet var", "lock var" and "unlock var"
5229 * "arg" points to "var".
5230 */
5231 static char_u *
5232compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5233{
5234 char_u *p = arg;
5235
5236 if (eap->cmdidx != CMD_unlet)
5237 {
5238 emsg("Sorry, :lock and unlock not implemented yet");
5239 return NULL;
5240 }
5241
5242 if (*p == '!')
5243 {
5244 p = skipwhite(p + 1);
5245 eap->forceit = TRUE;
5246 }
5247
5248 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5249 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5250}
5251
5252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 * Compile an :import command.
5254 */
5255 static char_u *
5256compile_import(char_u *arg, cctx_T *cctx)
5257{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005258 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259}
5260
5261/*
5262 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5263 */
5264 static int
5265compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5266{
5267 garray_T *instr = &cctx->ctx_instr;
5268 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5269
5270 if (endlabel == NULL)
5271 return FAIL;
5272 endlabel->el_next = *el;
5273 *el = endlabel;
5274 endlabel->el_end_label = instr->ga_len;
5275
5276 generate_JUMP(cctx, when, 0);
5277 return OK;
5278}
5279
5280 static void
5281compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5282{
5283 garray_T *instr = &cctx->ctx_instr;
5284
5285 while (*el != NULL)
5286 {
5287 endlabel_T *cur = (*el);
5288 isn_T *isn;
5289
5290 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5291 isn->isn_arg.jump.jump_where = instr->ga_len;
5292 *el = cur->el_next;
5293 vim_free(cur);
5294 }
5295}
5296
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005297 static void
5298compile_free_jump_to_end(endlabel_T **el)
5299{
5300 while (*el != NULL)
5301 {
5302 endlabel_T *cur = (*el);
5303
5304 *el = cur->el_next;
5305 vim_free(cur);
5306 }
5307}
5308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005309/*
5310 * Create a new scope and set up the generic items.
5311 */
5312 static scope_T *
5313new_scope(cctx_T *cctx, scopetype_T type)
5314{
5315 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5316
5317 if (scope == NULL)
5318 return NULL;
5319 scope->se_outer = cctx->ctx_scope;
5320 cctx->ctx_scope = scope;
5321 scope->se_type = type;
5322 scope->se_local_count = cctx->ctx_locals.ga_len;
5323 return scope;
5324}
5325
5326/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005327 * Free the current scope and go back to the outer scope.
5328 */
5329 static void
5330drop_scope(cctx_T *cctx)
5331{
5332 scope_T *scope = cctx->ctx_scope;
5333
5334 if (scope == NULL)
5335 {
5336 iemsg("calling drop_scope() without a scope");
5337 return;
5338 }
5339 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005340 switch (scope->se_type)
5341 {
5342 case IF_SCOPE:
5343 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5344 case FOR_SCOPE:
5345 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5346 case WHILE_SCOPE:
5347 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5348 case TRY_SCOPE:
5349 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5350 case NO_SCOPE:
5351 case BLOCK_SCOPE:
5352 break;
5353 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005354 vim_free(scope);
5355}
5356
5357/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 * compile "if expr"
5359 *
5360 * "if expr" Produces instructions:
5361 * EVAL expr Push result of "expr"
5362 * JUMP_IF_FALSE end
5363 * ... body ...
5364 * end:
5365 *
5366 * "if expr | else" Produces instructions:
5367 * EVAL expr Push result of "expr"
5368 * JUMP_IF_FALSE else
5369 * ... body ...
5370 * JUMP_ALWAYS end
5371 * else:
5372 * ... body ...
5373 * end:
5374 *
5375 * "if expr1 | elseif expr2 | else" Produces instructions:
5376 * EVAL expr Push result of "expr"
5377 * JUMP_IF_FALSE elseif
5378 * ... body ...
5379 * JUMP_ALWAYS end
5380 * elseif:
5381 * EVAL expr Push result of "expr"
5382 * JUMP_IF_FALSE else
5383 * ... body ...
5384 * JUMP_ALWAYS end
5385 * else:
5386 * ... body ...
5387 * end:
5388 */
5389 static char_u *
5390compile_if(char_u *arg, cctx_T *cctx)
5391{
5392 char_u *p = arg;
5393 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005394 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005396 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 CLEAR_FIELD(ppconst);
5400 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005401 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 clear_ppconst(&ppconst);
5403 return NULL;
5404 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005405 if (cctx->ctx_skip == SKIP_YES)
5406 clear_ppconst(&ppconst);
5407 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005408 {
5409 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005410 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005411 clear_ppconst(&ppconst);
5412 }
5413 else
5414 {
5415 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005416 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005417 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005418 return NULL;
5419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420
5421 scope = new_scope(cctx, IF_SCOPE);
5422 if (scope == NULL)
5423 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005424 scope->se_skip_save = skip_save;
5425 // "is_had_return" will be reset if any block does not end in :return
5426 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005428 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005429 {
5430 // "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 }
5434 else
5435 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
5437 return p;
5438}
5439
5440 static char_u *
5441compile_elseif(char_u *arg, cctx_T *cctx)
5442{
5443 char_u *p = arg;
5444 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005445 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 isn_T *isn;
5447 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005448 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
5450 if (scope == NULL || scope->se_type != IF_SCOPE)
5451 {
5452 emsg(_(e_elseif_without_if));
5453 return NULL;
5454 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005455 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005456 if (!cctx->ctx_had_return)
5457 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005459 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005460 {
5461 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005463 return NULL;
5464 // previous "if" or "elseif" jumps here
5465 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5466 isn->isn_arg.jump.jump_where = instr->ga_len;
5467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005469 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005470 CLEAR_FIELD(ppconst);
5471 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005472 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005473 clear_ppconst(&ppconst);
5474 return NULL;
5475 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005476 if (scope->se_skip_save == SKIP_YES)
5477 clear_ppconst(&ppconst);
5478 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005479 {
5480 // The expression results in a constant.
5481 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005482 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005483 clear_ppconst(&ppconst);
5484 scope->se_u.se_if.is_if_label = -1;
5485 }
5486 else
5487 {
5488 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005489 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005490 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005491 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005493 // "where" is set when ":elseif", "else" or ":endif" is found
5494 scope->se_u.se_if.is_if_label = instr->ga_len;
5495 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497
5498 return p;
5499}
5500
5501 static char_u *
5502compile_else(char_u *arg, cctx_T *cctx)
5503{
5504 char_u *p = arg;
5505 garray_T *instr = &cctx->ctx_instr;
5506 isn_T *isn;
5507 scope_T *scope = cctx->ctx_scope;
5508
5509 if (scope == NULL || scope->se_type != IF_SCOPE)
5510 {
5511 emsg(_(e_else_without_if));
5512 return NULL;
5513 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005514 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005515 if (!cctx->ctx_had_return)
5516 scope->se_u.se_if.is_had_return = FALSE;
5517 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518
Bram Moolenaarefd88552020-06-18 20:50:10 +02005519 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005520 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005521 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005522 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005523 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005524 if (!cctx->ctx_had_return
5525 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5526 JUMP_ALWAYS, cctx) == FAIL)
5527 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005528 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005529
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005530 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005531 {
5532 if (scope->se_u.se_if.is_if_label >= 0)
5533 {
5534 // previous "if" or "elseif" jumps here
5535 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5536 isn->isn_arg.jump.jump_where = instr->ga_len;
5537 scope->se_u.se_if.is_if_label = -1;
5538 }
5539 }
5540
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005541 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005542 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544
5545 return p;
5546}
5547
5548 static char_u *
5549compile_endif(char_u *arg, cctx_T *cctx)
5550{
5551 scope_T *scope = cctx->ctx_scope;
5552 ifscope_T *ifscope;
5553 garray_T *instr = &cctx->ctx_instr;
5554 isn_T *isn;
5555
5556 if (scope == NULL || scope->se_type != IF_SCOPE)
5557 {
5558 emsg(_(e_endif_without_if));
5559 return NULL;
5560 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005561 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005562 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005563 if (!cctx->ctx_had_return)
5564 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005566 if (scope->se_u.se_if.is_if_label >= 0)
5567 {
5568 // previous "if" or "elseif" jumps here
5569 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5570 isn->isn_arg.jump.jump_where = instr->ga_len;
5571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572 // Fill in the "end" label in jumps at the end of the blocks.
5573 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005574 cctx->ctx_skip = scope->se_skip_save;
5575
5576 // If all the blocks end in :return and there is an :else then the
5577 // had_return flag is set.
5578 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005580 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 return arg;
5582}
5583
5584/*
5585 * compile "for var in expr"
5586 *
5587 * Produces instructions:
5588 * PUSHNR -1
5589 * STORE loop-idx Set index to -1
5590 * EVAL expr Push result of "expr"
5591 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5592 * - if beyond end, jump to "end"
5593 * - otherwise get item from list and push it
5594 * STORE var Store item in "var"
5595 * ... body ...
5596 * JUMP top Jump back to repeat
5597 * end: DROP Drop the result of "expr"
5598 *
5599 */
5600 static char_u *
5601compile_for(char_u *arg, cctx_T *cctx)
5602{
5603 char_u *p;
5604 size_t varlen;
5605 garray_T *instr = &cctx->ctx_instr;
5606 garray_T *stack = &cctx->ctx_type_stack;
5607 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005608 lvar_T *loop_lvar; // loop iteration variable
5609 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 type_T *vartype;
5611
5612 // TODO: list of variables: "for [key, value] in dict"
5613 // parse "var"
5614 for (p = arg; eval_isnamec1(*p); ++p)
5615 ;
5616 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005617 var_lvar = lookup_local(arg, varlen, cctx);
5618 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619 {
5620 semsg(_("E1023: variable already defined: %s"), arg);
5621 return NULL;
5622 }
5623
5624 // consume "in"
5625 p = skipwhite(p);
5626 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5627 {
5628 emsg(_(e_missing_in));
5629 return NULL;
5630 }
5631 p = skipwhite(p + 2);
5632
5633
5634 scope = new_scope(cctx, FOR_SCOPE);
5635 if (scope == NULL)
5636 return NULL;
5637
5638 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005639 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5640 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005641 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005642 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005643 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646
5647 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005648 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5649 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005650 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005651 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005652 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005656 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657
5658 // compile "expr", it remains on the stack until "endfor"
5659 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005660 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005661 {
5662 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005666 // Now that we know the type of "var", check that it is a list, now or at
5667 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005669 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005671 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 return NULL;
5673 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005674 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005675 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676
5677 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005678 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005679
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005680 generate_FOR(cctx, loop_lvar->lv_idx);
5681 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682
5683 return arg;
5684}
5685
5686/*
5687 * compile "endfor"
5688 */
5689 static char_u *
5690compile_endfor(char_u *arg, cctx_T *cctx)
5691{
5692 garray_T *instr = &cctx->ctx_instr;
5693 scope_T *scope = cctx->ctx_scope;
5694 forscope_T *forscope;
5695 isn_T *isn;
5696
5697 if (scope == NULL || scope->se_type != FOR_SCOPE)
5698 {
5699 emsg(_(e_for));
5700 return NULL;
5701 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005702 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005704 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705
5706 // At end of ":for" scope jump back to the FOR instruction.
5707 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5708
5709 // Fill in the "end" label in the FOR statement so it can jump here
5710 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5711 isn->isn_arg.forloop.for_end = instr->ga_len;
5712
5713 // Fill in the "end" label any BREAK statements
5714 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5715
5716 // Below the ":for" scope drop the "expr" list from the stack.
5717 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5718 return NULL;
5719
5720 vim_free(scope);
5721
5722 return arg;
5723}
5724
5725/*
5726 * compile "while expr"
5727 *
5728 * Produces instructions:
5729 * top: EVAL expr Push result of "expr"
5730 * JUMP_IF_FALSE end jump if false
5731 * ... body ...
5732 * JUMP top Jump back to repeat
5733 * end:
5734 *
5735 */
5736 static char_u *
5737compile_while(char_u *arg, cctx_T *cctx)
5738{
5739 char_u *p = arg;
5740 garray_T *instr = &cctx->ctx_instr;
5741 scope_T *scope;
5742
5743 scope = new_scope(cctx, WHILE_SCOPE);
5744 if (scope == NULL)
5745 return NULL;
5746
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005747 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748
5749 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005750 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005751 return NULL;
5752
5753 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005754 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 JUMP_IF_FALSE, cctx) == FAIL)
5756 return FAIL;
5757
5758 return p;
5759}
5760
5761/*
5762 * compile "endwhile"
5763 */
5764 static char_u *
5765compile_endwhile(char_u *arg, cctx_T *cctx)
5766{
5767 scope_T *scope = cctx->ctx_scope;
5768
5769 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5770 {
5771 emsg(_(e_while));
5772 return NULL;
5773 }
5774 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005775 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776
5777 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005778 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
5780 // Fill in the "end" label in the WHILE statement so it can jump here.
5781 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005782 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783
5784 vim_free(scope);
5785
5786 return arg;
5787}
5788
5789/*
5790 * compile "continue"
5791 */
5792 static char_u *
5793compile_continue(char_u *arg, cctx_T *cctx)
5794{
5795 scope_T *scope = cctx->ctx_scope;
5796
5797 for (;;)
5798 {
5799 if (scope == NULL)
5800 {
5801 emsg(_(e_continue));
5802 return NULL;
5803 }
5804 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5805 break;
5806 scope = scope->se_outer;
5807 }
5808
5809 // Jump back to the FOR or WHILE instruction.
5810 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005811 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5812 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813 return arg;
5814}
5815
5816/*
5817 * compile "break"
5818 */
5819 static char_u *
5820compile_break(char_u *arg, cctx_T *cctx)
5821{
5822 scope_T *scope = cctx->ctx_scope;
5823 endlabel_T **el;
5824
5825 for (;;)
5826 {
5827 if (scope == NULL)
5828 {
5829 emsg(_(e_break));
5830 return NULL;
5831 }
5832 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5833 break;
5834 scope = scope->se_outer;
5835 }
5836
5837 // Jump to the end of the FOR or WHILE loop.
5838 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005839 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005840 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005841 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5843 return FAIL;
5844
5845 return arg;
5846}
5847
5848/*
5849 * compile "{" start of block
5850 */
5851 static char_u *
5852compile_block(char_u *arg, cctx_T *cctx)
5853{
5854 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5855 return NULL;
5856 return skipwhite(arg + 1);
5857}
5858
5859/*
5860 * compile end of block: drop one scope
5861 */
5862 static void
5863compile_endblock(cctx_T *cctx)
5864{
5865 scope_T *scope = cctx->ctx_scope;
5866
5867 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005868 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 vim_free(scope);
5870}
5871
5872/*
5873 * compile "try"
5874 * Creates a new scope for the try-endtry, pointing to the first catch and
5875 * finally.
5876 * Creates another scope for the "try" block itself.
5877 * TRY instruction sets up exception handling at runtime.
5878 *
5879 * "try"
5880 * TRY -> catch1, -> finally push trystack entry
5881 * ... try block
5882 * "throw {exception}"
5883 * EVAL {exception}
5884 * THROW create exception
5885 * ... try block
5886 * " catch {expr}"
5887 * JUMP -> finally
5888 * catch1: PUSH exeception
5889 * EVAL {expr}
5890 * MATCH
5891 * JUMP nomatch -> catch2
5892 * CATCH remove exception
5893 * ... catch block
5894 * " catch"
5895 * JUMP -> finally
5896 * catch2: CATCH remove exception
5897 * ... catch block
5898 * " finally"
5899 * finally:
5900 * ... finally block
5901 * " endtry"
5902 * ENDTRY pop trystack entry, may rethrow
5903 */
5904 static char_u *
5905compile_try(char_u *arg, cctx_T *cctx)
5906{
5907 garray_T *instr = &cctx->ctx_instr;
5908 scope_T *try_scope;
5909 scope_T *scope;
5910
5911 // scope that holds the jumps that go to catch/finally/endtry
5912 try_scope = new_scope(cctx, TRY_SCOPE);
5913 if (try_scope == NULL)
5914 return NULL;
5915
5916 // "catch" is set when the first ":catch" is found.
5917 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005918 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919 if (generate_instr(cctx, ISN_TRY) == NULL)
5920 return NULL;
5921
5922 // scope for the try block itself
5923 scope = new_scope(cctx, BLOCK_SCOPE);
5924 if (scope == NULL)
5925 return NULL;
5926
5927 return arg;
5928}
5929
5930/*
5931 * compile "catch {expr}"
5932 */
5933 static char_u *
5934compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5935{
5936 scope_T *scope = cctx->ctx_scope;
5937 garray_T *instr = &cctx->ctx_instr;
5938 char_u *p;
5939 isn_T *isn;
5940
5941 // end block scope from :try or :catch
5942 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5943 compile_endblock(cctx);
5944 scope = cctx->ctx_scope;
5945
5946 // Error if not in a :try scope
5947 if (scope == NULL || scope->se_type != TRY_SCOPE)
5948 {
5949 emsg(_(e_catch));
5950 return NULL;
5951 }
5952
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005953 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 {
5955 emsg(_("E1033: catch unreachable after catch-all"));
5956 return NULL;
5957 }
5958
5959 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005960 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 JUMP_ALWAYS, cctx) == FAIL)
5962 return NULL;
5963
5964 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005965 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 if (isn->isn_arg.try.try_catch == 0)
5967 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005968 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 {
5970 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005971 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 isn->isn_arg.jump.jump_where = instr->ga_len;
5973 }
5974
5975 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005976 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005978 scope->se_u.se_try.ts_caught_all = TRUE;
5979 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 }
5981 else
5982 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005983 char_u *end;
5984 char_u *pat;
5985 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005986 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005987 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 // Push v:exception, push {expr} and MATCH
5990 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5991
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005992 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005993 if (*end != *p)
5994 {
5995 semsg(_("E1067: Separator mismatch: %s"), p);
5996 vim_free(tofree);
5997 return FAIL;
5998 }
5999 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006000 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006001 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006002 len = (int)(end - tofree);
6003 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006004 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006005 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006006 if (pat == NULL)
6007 return FAIL;
6008 if (generate_PUSHS(cctx, pat) == FAIL)
6009 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6012 return NULL;
6013
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006014 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6016 return NULL;
6017 }
6018
6019 if (generate_instr(cctx, ISN_CATCH) == NULL)
6020 return NULL;
6021
6022 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6023 return NULL;
6024 return p;
6025}
6026
6027 static char_u *
6028compile_finally(char_u *arg, cctx_T *cctx)
6029{
6030 scope_T *scope = cctx->ctx_scope;
6031 garray_T *instr = &cctx->ctx_instr;
6032 isn_T *isn;
6033
6034 // end block scope from :try or :catch
6035 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6036 compile_endblock(cctx);
6037 scope = cctx->ctx_scope;
6038
6039 // Error if not in a :try scope
6040 if (scope == NULL || scope->se_type != TRY_SCOPE)
6041 {
6042 emsg(_(e_finally));
6043 return NULL;
6044 }
6045
6046 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006047 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 if (isn->isn_arg.try.try_finally != 0)
6049 {
6050 emsg(_(e_finally_dup));
6051 return NULL;
6052 }
6053
6054 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006055 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056
Bram Moolenaar585fea72020-04-02 22:33:21 +02006057 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006058 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 {
6060 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006061 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006063 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 }
6065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 // TODO: set index in ts_finally_label jumps
6067
6068 return arg;
6069}
6070
6071 static char_u *
6072compile_endtry(char_u *arg, cctx_T *cctx)
6073{
6074 scope_T *scope = cctx->ctx_scope;
6075 garray_T *instr = &cctx->ctx_instr;
6076 isn_T *isn;
6077
6078 // end block scope from :catch or :finally
6079 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6080 compile_endblock(cctx);
6081 scope = cctx->ctx_scope;
6082
6083 // Error if not in a :try scope
6084 if (scope == NULL || scope->se_type != TRY_SCOPE)
6085 {
6086 if (scope == NULL)
6087 emsg(_(e_no_endtry));
6088 else if (scope->se_type == WHILE_SCOPE)
6089 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006090 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 emsg(_(e_endfor));
6092 else
6093 emsg(_(e_endif));
6094 return NULL;
6095 }
6096
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006097 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6099 {
6100 emsg(_("E1032: missing :catch or :finally"));
6101 return NULL;
6102 }
6103
6104 // Fill in the "end" label in jumps at the end of the blocks, if not done
6105 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006106 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
6108 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006109 if (isn->isn_arg.try.try_catch == 0)
6110 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 if (isn->isn_arg.try.try_finally == 0)
6112 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006113
6114 if (scope->se_u.se_try.ts_catch_label != 0)
6115 {
6116 // Last catch without match jumps here
6117 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6118 isn->isn_arg.jump.jump_where = instr->ga_len;
6119 }
6120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121 compile_endblock(cctx);
6122
6123 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6124 return NULL;
6125 return arg;
6126}
6127
6128/*
6129 * compile "throw {expr}"
6130 */
6131 static char_u *
6132compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6133{
6134 char_u *p = skipwhite(arg);
6135
Bram Moolenaara5565e42020-05-09 15:44:01 +02006136 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 return NULL;
6138 if (may_generate_2STRING(-1, cctx) == FAIL)
6139 return NULL;
6140 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6141 return NULL;
6142
6143 return p;
6144}
6145
6146/*
6147 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006148 * compile "echomsg expr"
6149 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006150 * compile "execute expr"
6151 */
6152 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006153compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006154{
6155 char_u *p = arg;
6156 int count = 0;
6157
6158 for (;;)
6159 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006160 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006161 return NULL;
6162 ++count;
6163 p = skipwhite(p);
6164 if (ends_excmd(*p))
6165 break;
6166 }
6167
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006168 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6169 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6170 else if (cmdidx == CMD_execute)
6171 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6172 else if (cmdidx == CMD_echomsg)
6173 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6174 else
6175 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 return p;
6177}
6178
6179/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006180 * A command that is not compiled, execute with legacy code.
6181 */
6182 static char_u *
6183compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6184{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006185 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006186 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006187 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006188
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006189 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006190 goto theend;
6191
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006192 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006193 {
6194 long argt = excmd_get_argt(eap->cmdidx);
6195 int usefilter = FALSE;
6196
6197 has_expr = argt & (EX_XFILE | EX_EXPAND);
6198
6199 // If the command can be followed by a bar, find the bar and truncate
6200 // it, so that the following command can be compiled.
6201 // The '|' is overwritten with a NUL, it is put back below.
6202 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6203 && *eap->arg == '!')
6204 // :w !filter or :r !filter or :r! filter
6205 usefilter = TRUE;
6206 if ((argt & EX_TRLBAR) && !usefilter)
6207 {
6208 separate_nextcmd(eap);
6209 if (eap->nextcmd != NULL)
6210 nextcmd = eap->nextcmd;
6211 }
6212 }
6213
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006214 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6215 {
6216 // expand filename in "syntax include [@group] filename"
6217 has_expr = TRUE;
6218 eap->arg = skipwhite(eap->arg + 7);
6219 if (*eap->arg == '@')
6220 eap->arg = skiptowhite(eap->arg);
6221 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006222
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006223 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006224 {
6225 int count = 0;
6226 char_u *start = skipwhite(line);
6227
6228 // :cmd xxx`=expr1`yyy`=expr2`zzz
6229 // PUSHS ":cmd xxx"
6230 // eval expr1
6231 // PUSHS "yyy"
6232 // eval expr2
6233 // PUSHS "zzz"
6234 // EXECCONCAT 5
6235 for (;;)
6236 {
6237 if (p > start)
6238 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006239 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006240 ++count;
6241 }
6242 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006243 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006244 return NULL;
6245 may_generate_2STRING(-1, cctx);
6246 ++count;
6247 p = skipwhite(p);
6248 if (*p != '`')
6249 {
6250 emsg(_("E1083: missing backtick"));
6251 return NULL;
6252 }
6253 start = p + 1;
6254
6255 p = (char_u *)strstr((char *)start, "`=");
6256 if (p == NULL)
6257 {
6258 if (*skipwhite(start) != NUL)
6259 {
6260 generate_PUSHS(cctx, vim_strsave(start));
6261 ++count;
6262 }
6263 break;
6264 }
6265 }
6266 generate_EXECCONCAT(cctx, count);
6267 }
6268 else
6269 generate_EXEC(cctx, line);
6270
6271theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006272 if (*nextcmd != NUL)
6273 {
6274 // the parser expects a pointer to the bar, put it back
6275 --nextcmd;
6276 *nextcmd = '|';
6277 }
6278
6279 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006280}
6281
6282/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006283 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006284 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006285 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006286 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006287add_def_function(ufunc_T *ufunc)
6288{
6289 dfunc_T *dfunc;
6290
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006291 if (def_functions.ga_len == 0)
6292 {
6293 // The first position is not used, so that a zero uf_dfunc_idx means it
6294 // wasn't set.
6295 if (ga_grow(&def_functions, 1) == FAIL)
6296 return FAIL;
6297 ++def_functions.ga_len;
6298 }
6299
Bram Moolenaar09689a02020-05-09 22:50:08 +02006300 // Add the function to "def_functions".
6301 if (ga_grow(&def_functions, 1) == FAIL)
6302 return FAIL;
6303 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6304 CLEAR_POINTER(dfunc);
6305 dfunc->df_idx = def_functions.ga_len;
6306 ufunc->uf_dfunc_idx = dfunc->df_idx;
6307 dfunc->df_ufunc = ufunc;
6308 ++def_functions.ga_len;
6309 return OK;
6310}
6311
6312/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 * After ex_function() has collected all the function lines: parse and compile
6314 * the lines into instructions.
6315 * Adds the function to "def_functions".
6316 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6317 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006318 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006319 * This can be used recursively through compile_lambda(), which may reallocate
6320 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006321 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006323 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006324compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 char_u *line = NULL;
6327 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 cctx_T cctx;
6330 garray_T *instr;
6331 int called_emsg_before = called_emsg;
6332 int ret = FAIL;
6333 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006334 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006335 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006336 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006338 // When using a function that was compiled before: Free old instructions.
6339 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006340 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6343 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006344 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006346 else
6347 {
6348 if (add_def_function(ufunc) == FAIL)
6349 return FAIL;
6350 new_def_function = TRUE;
6351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
Bram Moolenaar985116a2020-07-12 17:31:09 +02006353 ufunc->uf_def_status = UF_COMPILING;
6354
Bram Moolenaara80faa82020-04-12 19:37:17 +02006355 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 cctx.ctx_ufunc = ufunc;
6357 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006358 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6360 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6361 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6362 cctx.ctx_type_list = &ufunc->uf_type_list;
6363 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6364 instr = &cctx.ctx_instr;
6365
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006366 // Set the context to the function, it may be compiled when called from
6367 // another script. Set the script version to the most modern one.
6368 // The line number will be set in next_line_from_context().
6369 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6371
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006372 // Make sure error messages are OK.
6373 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6374 if (do_estack_push)
6375 estack_push_ufunc(ufunc, 1);
6376
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006377 if (ufunc->uf_def_args.ga_len > 0)
6378 {
6379 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006380 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006381 int i;
6382 char_u *arg;
6383 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006384 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006385
6386 // Produce instructions for the default values of optional arguments.
6387 // Store the instruction index in uf_def_arg_idx[] so that we know
6388 // where to start when the function is called, depending on the number
6389 // of arguments.
6390 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6391 if (ufunc->uf_def_arg_idx == NULL)
6392 goto erret;
6393 for (i = 0; i < count; ++i)
6394 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006395 garray_T *stack = &cctx.ctx_type_stack;
6396 type_T *val_type;
6397 int arg_idx = first_def_arg + i;
6398
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006399 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6400 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006401 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006402 goto erret;
6403
6404 // If no type specified use the type of the default value.
6405 // Otherwise check that the default value type matches the
6406 // specified type.
6407 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6408 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006409 {
6410 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006411 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006412 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006413 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006414 == FAIL)
6415 {
6416 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6417 arg_idx + 1);
6418 goto erret;
6419 }
6420
6421 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006422 goto erret;
6423 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006424 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006425
6426 if (did_set_arg_type)
6427 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006428 }
6429
6430 /*
6431 * Loop over all the lines of the function and generate instructions.
6432 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 for (;;)
6434 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006435 exarg_T ea;
6436 cmdmod_T save_cmdmod;
6437 int starts_with_colon = FALSE;
6438 char_u *cmd;
6439 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006440
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006441 // Bail out on the first error to avoid a flood of errors and report
6442 // the right line number when inside try/catch.
6443 if (emsg_before != called_emsg)
6444 goto erret;
6445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 if (line != NULL && *line == '|')
6447 // the line continues after a '|'
6448 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006449 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006450 && !(*line == '#' && (line == cctx.ctx_line_start
6451 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006453 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 goto erret;
6455 }
6456 else
6457 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006458 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006459 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006460 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006463 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464
Bram Moolenaara80faa82020-04-12 19:37:17 +02006465 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 ea.cmdlinep = &line;
6467 ea.cmd = skipwhite(line);
6468
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006469 // Some things can be recognized by the first character.
6470 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006471 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006472 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006473 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006474 if (ea.cmd[1] != '{')
6475 {
6476 line = (char_u *)"";
6477 continue;
6478 }
6479 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006481 case '}':
6482 {
6483 // "}" ends a block scope
6484 scopetype_T stype = cctx.ctx_scope == NULL
6485 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006487 if (stype == BLOCK_SCOPE)
6488 {
6489 compile_endblock(&cctx);
6490 line = ea.cmd;
6491 }
6492 else
6493 {
6494 emsg(_("E1025: using } outside of a block scope"));
6495 goto erret;
6496 }
6497 if (line != NULL)
6498 line = skipwhite(ea.cmd + 1);
6499 continue;
6500 }
6501
6502 case '{':
6503 // "{" starts a block scope
6504 // "{'a': 1}->func() is something else
6505 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6506 {
6507 line = compile_block(ea.cmd, &cctx);
6508 continue;
6509 }
6510 break;
6511
6512 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006513 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006514 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 }
6516
6517 /*
6518 * COMMAND MODIFIERS
6519 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006520 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6522 {
6523 if (errormsg != NULL)
6524 goto erret;
6525 // empty line or comment
6526 line = (char_u *)"";
6527 continue;
6528 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006529 // TODO: use modifiers in the command
6530 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006531 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532
6533 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006534 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006536 {
6537 if (*ea.cmd == '(')
6538 // not for "call()"
6539 ea.cmd = p;
6540 else
6541 ea.cmd = skipwhite(ea.cmd);
6542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006544 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006546 char_u *pskip;
6547
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006548 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006549 // find what follows.
6550 // Skip over "var.member", "var[idx]" and the like.
6551 // Also "&opt = val", "$ENV = val" and "@r = val".
6552 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006553 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006554 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006555 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006557 char_u *var_end;
6558 int oplen;
6559 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560
Bram Moolenaar65821722020-08-02 18:58:54 +02006561 if (ea.cmd[0] == '@')
6562 var_end = ea.cmd + 2;
6563 else
6564 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006565 FNE_CHECK_START | FNE_INCL_BR);
6566 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006567 if (oplen > 0)
6568 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006569 size_t len = p - ea.cmd;
6570
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006571 // Recognize an assignment if we recognize the variable
6572 // name:
6573 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006574 // "local = expr" where "local" is a local var.
6575 // "script = expr" where "script" is a script-local var.
6576 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006577 // "&opt = expr"
6578 // "$ENV = expr"
6579 // "@r = expr"
6580 if (*ea.cmd == '&'
6581 || *ea.cmd == '$'
6582 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006583 || ((len) > 2 && ea.cmd[1] == ':')
6584 || lookup_local(ea.cmd, len, &cctx) != NULL
6585 || lookup_arg(ea.cmd, len, NULL, NULL,
6586 NULL, &cctx) == OK
6587 || lookup_script(ea.cmd, len) == OK
6588 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006589 {
6590 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006591 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006592 goto erret;
6593 continue;
6594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 }
6596 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006597
6598 if (*ea.cmd == '[')
6599 {
6600 // [var, var] = expr
6601 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6602 if (line == NULL)
6603 goto erret;
6604 if (line != ea.cmd)
6605 continue;
6606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 }
6608
6609 /*
6610 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006611 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006613 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006614 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006615 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006616 ea.cmd = skip_range(ea.cmd, NULL);
6617 if (ea.cmd > cmd && !starts_with_colon)
6618 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006619 emsg(_(e_colon_required_before_a_range));
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006620 goto erret;
6621 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006622 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006623 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006624 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006625 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626
6627 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6628 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006629 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006630 {
6631 line += STRLEN(line);
6632 continue;
6633 }
6634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006636 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006638 // CMD_let cannot happen, compile_assignment() above is used
6639 iemsg("Command from find_ex_command() not handled");
6640 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 }
6643
6644 p = skipwhite(p);
6645
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006646 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006647 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006648 && ea.cmdidx != CMD_elseif
6649 && ea.cmdidx != CMD_else
6650 && ea.cmdidx != CMD_endif)
6651 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006652 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006653 continue;
6654 }
6655
Bram Moolenaarefd88552020-06-18 20:50:10 +02006656 if (ea.cmdidx != CMD_elseif
6657 && ea.cmdidx != CMD_else
6658 && ea.cmdidx != CMD_endif
6659 && ea.cmdidx != CMD_endfor
6660 && ea.cmdidx != CMD_endwhile
6661 && ea.cmdidx != CMD_catch
6662 && ea.cmdidx != CMD_finally
6663 && ea.cmdidx != CMD_endtry)
6664 {
6665 if (cctx.ctx_had_return)
6666 {
6667 emsg(_("E1095: Unreachable code after :return"));
6668 goto erret;
6669 }
6670 }
6671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 switch (ea.cmdidx)
6673 {
6674 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006675 ea.arg = p;
6676 line = compile_nested_function(&ea, &cctx);
6677 break;
6678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006680 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 goto erret;
6682
6683 case CMD_return:
6684 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006685 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 break;
6687
6688 case CMD_let:
6689 case CMD_const:
6690 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006691 if (line == p)
6692 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 break;
6694
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006695 case CMD_unlet:
6696 case CMD_unlockvar:
6697 case CMD_lockvar:
6698 line = compile_unletlock(p, &ea, &cctx);
6699 break;
6700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 case CMD_import:
6702 line = compile_import(p, &cctx);
6703 break;
6704
6705 case CMD_if:
6706 line = compile_if(p, &cctx);
6707 break;
6708 case CMD_elseif:
6709 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006710 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 break;
6712 case CMD_else:
6713 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006714 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 break;
6716 case CMD_endif:
6717 line = compile_endif(p, &cctx);
6718 break;
6719
6720 case CMD_while:
6721 line = compile_while(p, &cctx);
6722 break;
6723 case CMD_endwhile:
6724 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006725 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 break;
6727
6728 case CMD_for:
6729 line = compile_for(p, &cctx);
6730 break;
6731 case CMD_endfor:
6732 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006733 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 break;
6735 case CMD_continue:
6736 line = compile_continue(p, &cctx);
6737 break;
6738 case CMD_break:
6739 line = compile_break(p, &cctx);
6740 break;
6741
6742 case CMD_try:
6743 line = compile_try(p, &cctx);
6744 break;
6745 case CMD_catch:
6746 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006747 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 break;
6749 case CMD_finally:
6750 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006751 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 break;
6753 case CMD_endtry:
6754 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006755 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 break;
6757 case CMD_throw:
6758 line = compile_throw(p, &cctx);
6759 break;
6760
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006761 case CMD_eval:
6762 if (compile_expr0(&p, &cctx) == FAIL)
6763 goto erret;
6764
6765 // drop the return value
6766 generate_instr_drop(&cctx, ISN_DROP, 1);
6767
6768 line = skipwhite(p);
6769 break;
6770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006773 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006774 case CMD_echomsg:
6775 case CMD_echoerr:
6776 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006777 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006779 // TODO: other commands with an expression argument
6780
Bram Moolenaarae616492020-07-28 20:07:27 +02006781 case CMD_append:
6782 case CMD_change:
6783 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006784 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006785 case CMD_xit:
6786 not_in_vim9(&ea);
6787 goto erret;
6788
Bram Moolenaar002262f2020-07-08 17:47:57 +02006789 case CMD_SIZE:
6790 semsg(_("E476: Invalid command: %s"), ea.cmd);
6791 goto erret;
6792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006794 // Not recognized, execute with do_cmdline_cmd().
6795 ea.arg = p;
6796 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 break;
6798 }
6799 if (line == NULL)
6800 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006801 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802
6803 if (cctx.ctx_type_stack.ga_len < 0)
6804 {
6805 iemsg("Type stack underflow");
6806 goto erret;
6807 }
6808 }
6809
6810 if (cctx.ctx_scope != NULL)
6811 {
6812 if (cctx.ctx_scope->se_type == IF_SCOPE)
6813 emsg(_(e_endif));
6814 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6815 emsg(_(e_endwhile));
6816 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6817 emsg(_(e_endfor));
6818 else
6819 emsg(_("E1026: Missing }"));
6820 goto erret;
6821 }
6822
Bram Moolenaarefd88552020-06-18 20:50:10 +02006823 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 {
6825 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6826 {
6827 emsg(_("E1027: Missing return statement"));
6828 goto erret;
6829 }
6830
6831 // Return zero if there is no return at the end.
6832 generate_PUSHNR(&cctx, 0);
6833 generate_instr(&cctx, ISN_RETURN);
6834 }
6835
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006836 {
6837 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6838 + ufunc->uf_dfunc_idx;
6839 dfunc->df_deleted = FALSE;
6840 dfunc->df_instr = instr->ga_data;
6841 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006842 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006843 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006844 if (cctx.ctx_outer_used)
6845 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006846 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848
6849 ret = OK;
6850
6851erret:
6852 if (ret == FAIL)
6853 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006854 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006855 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6856 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006857
6858 for (idx = 0; idx < instr->ga_len; ++idx)
6859 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006861
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006862 // If using the last entry in the table and it was added above, we
6863 // might as well remove it.
6864 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006865 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006866 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006867 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006868 ufunc->uf_dfunc_idx = 0;
6869 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006870 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006871
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006872 while (cctx.ctx_scope != NULL)
6873 drop_scope(&cctx);
6874
Bram Moolenaar20431c92020-03-20 18:39:46 +01006875 // Don't execute this function body.
6876 ga_clear_strings(&ufunc->uf_lines);
6877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 if (errormsg != NULL)
6879 emsg(errormsg);
6880 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006881 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 }
6883
6884 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006885 if (do_estack_push)
6886 estack_pop();
6887
Bram Moolenaar20431c92020-03-20 18:39:46 +01006888 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006889 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006891 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892}
6893
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006894 void
6895set_function_type(ufunc_T *ufunc)
6896{
6897 int varargs = ufunc->uf_va_name != NULL;
6898 int argcount = ufunc->uf_args.ga_len;
6899
6900 // Create a type for the function, with the return type and any
6901 // argument types.
6902 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6903 // The type is included in "tt_args".
6904 if (argcount > 0 || varargs)
6905 {
6906 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6907 argcount, &ufunc->uf_type_list);
6908 // Add argument types to the function type.
6909 if (func_type_add_arg_types(ufunc->uf_func_type,
6910 argcount + varargs,
6911 &ufunc->uf_type_list) == FAIL)
6912 return;
6913 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6914 ufunc->uf_func_type->tt_min_argcount =
6915 argcount - ufunc->uf_def_args.ga_len;
6916 if (ufunc->uf_arg_types == NULL)
6917 {
6918 int i;
6919
6920 // lambda does not have argument types.
6921 for (i = 0; i < argcount; ++i)
6922 ufunc->uf_func_type->tt_args[i] = &t_any;
6923 }
6924 else
6925 mch_memmove(ufunc->uf_func_type->tt_args,
6926 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6927 if (varargs)
6928 {
6929 ufunc->uf_func_type->tt_args[argcount] =
6930 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6931 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6932 }
6933 }
6934 else
6935 // No arguments, can use a predefined type.
6936 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6937 argcount, &ufunc->uf_type_list);
6938}
6939
6940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941/*
6942 * Delete an instruction, free what it contains.
6943 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006944 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945delete_instr(isn_T *isn)
6946{
6947 switch (isn->isn_type)
6948 {
6949 case ISN_EXEC:
6950 case ISN_LOADENV:
6951 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006952 case ISN_LOADB:
6953 case ISN_LOADW:
6954 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006956 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 case ISN_PUSHEXC:
6958 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006959 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006961 case ISN_STOREB:
6962 case ISN_STOREW:
6963 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006964 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 vim_free(isn->isn_arg.string);
6966 break;
6967
6968 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006969 case ISN_STORES:
6970 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 break;
6972
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006973 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006974 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006975 vim_free(isn->isn_arg.unlet.ul_name);
6976 break;
6977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 case ISN_STOREOPT:
6979 vim_free(isn->isn_arg.storeopt.so_name);
6980 break;
6981
6982 case ISN_PUSHBLOB: // push blob isn_arg.blob
6983 blob_unref(isn->isn_arg.blob);
6984 break;
6985
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006986 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006987#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006988 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006989#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006990 break;
6991
6992 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006993#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006994 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006995#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006996 break;
6997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 case ISN_UCALL:
6999 vim_free(isn->isn_arg.ufunc.cuf_name);
7000 break;
7001
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007002 case ISN_FUNCREF:
7003 {
7004 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7005 + isn->isn_arg.funcref.fr_func;
7006 func_ptr_unref(dfunc->df_ufunc);
7007 }
7008 break;
7009
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007010 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007011 {
7012 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7013 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7014
7015 if (ufunc != NULL)
7016 {
7017 // Clear uf_dfunc_idx so that the function is deleted.
7018 clear_def_function(ufunc);
7019 ufunc->uf_dfunc_idx = 0;
7020 func_ptr_unref(ufunc);
7021 }
7022
7023 vim_free(lambda);
7024 vim_free(isn->isn_arg.newfunc.nf_global);
7025 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007026 break;
7027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 case ISN_2BOOL:
7029 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007030 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 case ISN_ADDBLOB:
7032 case ISN_ADDLIST:
7033 case ISN_BCALL:
7034 case ISN_CATCH:
7035 case ISN_CHECKNR:
7036 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007037 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 case ISN_COMPAREANY:
7039 case ISN_COMPAREBLOB:
7040 case ISN_COMPAREBOOL:
7041 case ISN_COMPAREDICT:
7042 case ISN_COMPAREFLOAT:
7043 case ISN_COMPAREFUNC:
7044 case ISN_COMPARELIST:
7045 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 case ISN_COMPARESPECIAL:
7047 case ISN_COMPARESTRING:
7048 case ISN_CONCAT:
7049 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007050 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 case ISN_DROP:
7052 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007053 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007054 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007056 case ISN_EXECCONCAT:
7057 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007059 case ISN_LISTINDEX:
7060 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007061 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007062 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007063 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case ISN_JUMP:
7065 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007066 case ISN_LOADBDICT:
7067 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007068 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007070 case ISN_LOADSCRIPT:
7071 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007073 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 case ISN_NEGATENR:
7075 case ISN_NEWDICT:
7076 case ISN_NEWLIST:
7077 case ISN_OPNR:
7078 case ISN_OPFLOAT:
7079 case ISN_OPANY:
7080 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007081 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 case ISN_PUSHF:
7083 case ISN_PUSHNR:
7084 case ISN_PUSHBOOL:
7085 case ISN_PUSHSPEC:
7086 case ISN_RETURN:
7087 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007088 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007089 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007091 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007093 case ISN_STOREDICT:
7094 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 case ISN_THROW:
7096 case ISN_TRY:
7097 // nothing allocated
7098 break;
7099 }
7100}
7101
7102/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007103 * Free all instructions for "dfunc".
7104 */
7105 static void
7106delete_def_function_contents(dfunc_T *dfunc)
7107{
7108 int idx;
7109
7110 ga_clear(&dfunc->df_def_args_isn);
7111
7112 if (dfunc->df_instr != NULL)
7113 {
7114 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7115 delete_instr(dfunc->df_instr + idx);
7116 VIM_CLEAR(dfunc->df_instr);
7117 }
7118
7119 dfunc->df_deleted = TRUE;
7120}
7121
7122/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007123 * When a user function is deleted, clear the contents of any associated def
7124 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 */
7126 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007127clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007129 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 {
7131 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7132 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133
Bram Moolenaar20431c92020-03-20 18:39:46 +01007134 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007135 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 }
7137}
7138
7139#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007140/*
7141 * Free all functions defined with ":def".
7142 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 void
7144free_def_functions(void)
7145{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007146 int idx;
7147
7148 for (idx = 0; idx < def_functions.ga_len; ++idx)
7149 {
7150 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7151
7152 delete_def_function_contents(dfunc);
7153 }
7154
7155 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156}
7157#endif
7158
7159
7160#endif // FEAT_EVAL