blob: bdb0f064e63c72318b9223f6f30fcd9490e9b2c3 [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 }
1364 else
1365 expected = ufunc->uf_va_type->tt_member;
1366 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001367 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001368 {
1369 arg_type_mismatch(expected, actual, i + 1);
1370 return FAIL;
1371 }
1372 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001373 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001374 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1375 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001376 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001377 }
1378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001380 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001381 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001383 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 {
1385 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1386 isn->isn_arg.dfunc.cdf_argcount = argcount;
1387 }
1388 else
1389 {
1390 // A user function may be deleted and redefined later, can't use the
1391 // ufunc pointer, need to look it up again at runtime.
1392 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1393 isn->isn_arg.ufunc.cuf_argcount = argcount;
1394 }
1395
1396 stack->ga_len -= argcount; // drop the arguments
1397 if (ga_grow(stack, 1) == FAIL)
1398 return FAIL;
1399 // add return value
1400 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1401 ++stack->ga_len;
1402
1403 return OK;
1404}
1405
1406/*
1407 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1408 */
1409 static int
1410generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1411{
1412 isn_T *isn;
1413 garray_T *stack = &cctx->ctx_type_stack;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1417 return FAIL;
1418 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1419 isn->isn_arg.ufunc.cuf_argcount = argcount;
1420
1421 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001422 if (ga_grow(stack, 1) == FAIL)
1423 return FAIL;
1424 // add return value
1425 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1426 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
1428 return OK;
1429}
1430
1431/*
1432 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001433 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 */
1435 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001436generate_PCALL(
1437 cctx_T *cctx,
1438 int argcount,
1439 char_u *name,
1440 type_T *type,
1441 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446
Bram Moolenaar080457c2020-03-03 21:53:32 +01001447 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001448
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001449 if (type->tt_type == VAR_ANY)
1450 ret_type = &t_any;
1451 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001452 {
1453 if (type->tt_argcount != -1)
1454 {
1455 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1456
1457 if (argcount < type->tt_min_argcount - varargs)
1458 {
1459 semsg(_(e_toofewarg), "[reference]");
1460 return FAIL;
1461 }
1462 if (!varargs && argcount > type->tt_argcount)
1463 {
1464 semsg(_(e_toomanyarg), "[reference]");
1465 return FAIL;
1466 }
1467 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001468 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001469 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001470 else
1471 {
1472 semsg(_("E1085: Not a callable type: %s"), name);
1473 return FAIL;
1474 }
1475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1477 return FAIL;
1478 isn->isn_arg.pfunc.cpf_top = at_top;
1479 isn->isn_arg.pfunc.cpf_argcount = argcount;
1480
1481 stack->ga_len -= argcount; // drop the arguments
1482
1483 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001484 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001486 // If partial is above the arguments it must be cleared and replaced with
1487 // the return value.
1488 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1489 return FAIL;
1490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 return OK;
1492}
1493
1494/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001495 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 */
1497 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001498generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499{
1500 isn_T *isn;
1501 garray_T *stack = &cctx->ctx_type_stack;
1502 type_T *type;
1503
Bram Moolenaar080457c2020-03-03 21:53:32 +01001504 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001505 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001507 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001509 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001511 if (type->tt_type != VAR_DICT && type != &t_any)
1512 {
1513 emsg(_(e_dictreq));
1514 return FAIL;
1515 }
1516 // change dict type to dict member type
1517 if (type->tt_type == VAR_DICT)
1518 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519
1520 return OK;
1521}
1522
1523/*
1524 * Generate an ISN_ECHO instruction.
1525 */
1526 static int
1527generate_ECHO(cctx_T *cctx, int with_white, int count)
1528{
1529 isn_T *isn;
1530
Bram Moolenaar080457c2020-03-03 21:53:32 +01001531 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1533 return FAIL;
1534 isn->isn_arg.echo.echo_with_white = with_white;
1535 isn->isn_arg.echo.echo_count = count;
1536
1537 return OK;
1538}
1539
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001541 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001542 */
1543 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545{
1546 isn_T *isn;
1547
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549 return FAIL;
1550 isn->isn_arg.number = count;
1551
1552 return OK;
1553}
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 static int
1556generate_EXEC(cctx_T *cctx, char_u *line)
1557{
1558 isn_T *isn;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.string = vim_strsave(line);
1564 return OK;
1565}
1566
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001567 static int
1568generate_EXECCONCAT(cctx_T *cctx, int count)
1569{
1570 isn_T *isn;
1571
1572 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.number = count;
1575 return OK;
1576}
1577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578/*
1579 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001580 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001582 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1584{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 lvar_T *lvar;
1586
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001587 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001589 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001590 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 }
1592
1593 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001594 return NULL;
1595 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 // Every local variable uses the next entry on the stack. We could re-use
1598 // the last ones when leaving a scope, but then variables used in a closure
1599 // might get overwritten. To keep things simple do not re-use stack
1600 // entries. This is less efficient, but memory is cheap these days.
1601 lvar->lv_idx = cctx->ctx_locals_count++;
1602
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001603 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 lvar->lv_const = isConst;
1605 lvar->lv_type = type;
1606
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001607 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608}
1609
1610/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001611 * Remove local variables above "new_top".
1612 */
1613 static void
1614unwind_locals(cctx_T *cctx, int new_top)
1615{
1616 if (cctx->ctx_locals.ga_len > new_top)
1617 {
1618 int idx;
1619 lvar_T *lvar;
1620
1621 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1622 {
1623 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1624 vim_free(lvar->lv_name);
1625 }
1626 }
1627 cctx->ctx_locals.ga_len = new_top;
1628}
1629
1630/*
1631 * Free all local variables.
1632 */
1633 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001635{
1636 unwind_locals(cctx, 0);
1637 ga_clear(&cctx->ctx_locals);
1638}
1639
1640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 * Find "name" in script-local items of script "sid".
1642 * Returns the index in "sn_var_vals" if found.
1643 * If found but not in "sn_var_vals" returns -1.
1644 * If not found returns -2.
1645 */
1646 int
1647get_script_item_idx(int sid, char_u *name, int check_writable)
1648{
1649 hashtab_T *ht;
1650 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001651 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 int idx;
1653
1654 // First look the name up in the hashtable.
1655 if (sid <= 0 || sid > script_items.ga_len)
1656 return -1;
1657 ht = &SCRIPT_VARS(sid);
1658 di = find_var_in_ht(ht, 0, name, TRUE);
1659 if (di == NULL)
1660 return -2;
1661
1662 // Now find the svar_T index in sn_var_vals.
1663 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1664 {
1665 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1666
1667 if (sv->sv_tv == &di->di_tv)
1668 {
1669 if (check_writable && sv->sv_const)
1670 semsg(_(e_readonlyvar), name);
1671 return idx;
1672 }
1673 }
1674 return -1;
1675}
1676
1677/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001678 * Find "name" in imported items of the current script or in "cctx" if not
1679 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 */
1681 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001682find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 int idx;
1685
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001686 if (current_sctx.sc_sid <= 0)
1687 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 if (cctx != NULL)
1689 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1690 {
1691 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1692 + idx;
1693
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001694 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1695 : STRLEN(import->imp_name) == len
1696 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 return import;
1698 }
1699
Bram Moolenaarefa94442020-08-08 22:16:00 +02001700 return find_imported_in_script(name, len, current_sctx.sc_sid);
1701}
1702
1703 imported_T *
1704find_imported_in_script(char_u *name, size_t len, int sid)
1705{
1706 scriptitem_T *si = SCRIPT_ITEM(sid);
1707 int idx;
1708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1710 {
1711 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1712
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001713 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1714 : STRLEN(import->imp_name) == len
1715 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 return import;
1717 }
1718 return NULL;
1719}
1720
1721/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001722 * Free all imported variables.
1723 */
1724 static void
1725free_imported(cctx_T *cctx)
1726{
1727 int idx;
1728
1729 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1730 {
1731 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1732
1733 vim_free(import->imp_name);
1734 }
1735 ga_clear(&cctx->ctx_imports);
1736}
1737
1738/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001739 * Return TRUE if "p" points at a "#" but not at "#{".
1740 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001741 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001742vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001743{
1744 return p[0] == '#' && p[1] != '{';
1745}
1746
1747/*
1748 * Return a pointer to the next line that isn't empty or only contains a
1749 * comment. Skips over white space.
1750 * Returns NULL if there is none.
1751 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001752 char_u *
1753peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001754{
1755 int lnum = cctx->ctx_lnum;
1756
1757 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1758 {
1759 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001760 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001761
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001762 // ignore NULLs inserted for continuation lines
1763 if (line != NULL)
1764 {
1765 p = skipwhite(line);
1766 if (*p != NUL && !vim9_comment_start(p))
1767 return p;
1768 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001769 }
1770 return NULL;
1771}
1772
1773/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001774 * Called when checking for a following operator at "arg". When the rest of
1775 * the line is empty or only a comment, peek the next line. If there is a next
1776 * line return a pointer to it and set "nextp".
1777 * Otherwise skip over white space.
1778 */
1779 static char_u *
1780may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1781{
1782 char_u *p = skipwhite(arg);
1783
1784 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001785 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001786 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001787 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001788 if (*nextp != NULL)
1789 return *nextp;
1790 }
1791 return p;
1792}
1793
1794/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001795 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001796 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001797 * Returns NULL when at the end.
1798 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001799 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001800next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001801{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001802 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001803
1804 do
1805 {
1806 ++cctx->ctx_lnum;
1807 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001808 {
1809 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001810 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001811 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001812 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001813 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001814 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001815 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001816 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001817 return line;
1818}
1819
1820/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001821 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001822 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001823 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1824 */
1825 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001826may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001827{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001828 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001829 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001830 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001831
1832 if (next == NULL)
1833 return FAIL;
1834 *arg = skipwhite(next);
1835 }
1836 return OK;
1837}
1838
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001839/*
1840 * Idem, and give an error when failed.
1841 */
1842 static int
1843may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1844{
1845 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1846 {
1847 emsg(_("E1097: line incomplete"));
1848 return FAIL;
1849 }
1850 return OK;
1851}
1852
1853
Bram Moolenaara5565e42020-05-09 15:44:01 +02001854// Structure passed between the compile_expr* functions to keep track of
1855// constants that have been parsed but for which no code was produced yet. If
1856// possible expressions on these constants are applied at compile time. If
1857// that is not possible, the code to push the constants needs to be generated
1858// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001859// Using 50 should be more than enough of 5 levels of ().
1860#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001861typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001862 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001863 int pp_used; // active entries in pp_tv[]
1864} ppconst_T;
1865
Bram Moolenaar1c747212020-05-09 18:28:34 +02001866static int compile_expr0(char_u **arg, cctx_T *cctx);
1867static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1868
Bram Moolenaara5565e42020-05-09 15:44:01 +02001869/*
1870 * Generate a PUSH instruction for "tv".
1871 * "tv" will be consumed or cleared.
1872 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1873 */
1874 static int
1875generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1876{
1877 if (tv != NULL)
1878 {
1879 switch (tv->v_type)
1880 {
1881 case VAR_UNKNOWN:
1882 break;
1883 case VAR_BOOL:
1884 generate_PUSHBOOL(cctx, tv->vval.v_number);
1885 break;
1886 case VAR_SPECIAL:
1887 generate_PUSHSPEC(cctx, tv->vval.v_number);
1888 break;
1889 case VAR_NUMBER:
1890 generate_PUSHNR(cctx, tv->vval.v_number);
1891 break;
1892#ifdef FEAT_FLOAT
1893 case VAR_FLOAT:
1894 generate_PUSHF(cctx, tv->vval.v_float);
1895 break;
1896#endif
1897 case VAR_BLOB:
1898 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1899 tv->vval.v_blob = NULL;
1900 break;
1901 case VAR_STRING:
1902 generate_PUSHS(cctx, tv->vval.v_string);
1903 tv->vval.v_string = NULL;
1904 break;
1905 default:
1906 iemsg("constant type not supported");
1907 clear_tv(tv);
1908 return FAIL;
1909 }
1910 tv->v_type = VAR_UNKNOWN;
1911 }
1912 return OK;
1913}
1914
1915/*
1916 * Generate code for any ppconst entries.
1917 */
1918 static int
1919generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1920{
1921 int i;
1922 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001923 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001924
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001925 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001926 for (i = 0; i < ppconst->pp_used; ++i)
1927 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1928 ret = FAIL;
1929 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001930 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001931 return ret;
1932}
1933
1934/*
1935 * Clear ppconst constants. Used when failing.
1936 */
1937 static void
1938clear_ppconst(ppconst_T *ppconst)
1939{
1940 int i;
1941
1942 for (i = 0; i < ppconst->pp_used; ++i)
1943 clear_tv(&ppconst->pp_tv[i]);
1944 ppconst->pp_used = 0;
1945}
1946
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001947/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001948 * Generate an instruction to load script-local variable "name", without the
1949 * leading "s:".
1950 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 */
1952 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001953compile_load_scriptvar(
1954 cctx_T *cctx,
1955 char_u *name, // variable NUL terminated
1956 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001957 char_u **end, // end of variable
1958 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001960 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1962 imported_T *import;
1963
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001964 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001966 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001967 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1968 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 }
1970 if (idx >= 0)
1971 {
1972 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1973
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001974 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 current_sctx.sc_sid, idx, sv->sv_type);
1976 return OK;
1977 }
1978
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001979 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if (import != NULL)
1981 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001982 if (import->imp_all)
1983 {
1984 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001985 char_u *exp_name;
1986 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001987 ufunc_T *ufunc;
1988 type_T *type;
1989
1990 // Used "import * as Name", need to lookup the member.
1991 if (*p != '.')
1992 {
1993 semsg(_("E1060: expected dot after name: %s"), start);
1994 return FAIL;
1995 }
1996 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001997 if (VIM_ISWHITE(*p))
1998 {
1999 emsg(_("E1074: no white space allowed after dot"));
2000 return FAIL;
2001 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002002
Bram Moolenaar1c991142020-07-04 13:15:31 +02002003 // isolate one name
2004 exp_name = p;
2005 while (eval_isnamec(*p))
2006 ++p;
2007 cc = *p;
2008 *p = NUL;
2009
2010 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2011 *p = cc;
2012 p = skipwhite(p);
2013
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002014 // TODO: what if it is a function?
2015 if (idx < 0)
2016 return FAIL;
2017 *end = p;
2018
2019 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2020 import->imp_sid,
2021 idx,
2022 type);
2023 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002024 else if (import->imp_funcname != NULL)
2025 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002026 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002027 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2028 import->imp_sid,
2029 import->imp_var_vals_idx,
2030 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 return OK;
2032 }
2033
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002034 if (error)
2035 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 return FAIL;
2037}
2038
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 static int
2040generate_funcref(cctx_T *cctx, char_u *name)
2041{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002042 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002043
2044 if (ufunc == NULL)
2045 return FAIL;
2046
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002047 // Need to compile any default values to get the argument types.
2048 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2049 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2050 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002051 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002052}
2053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054/*
2055 * Compile a variable name into a load instruction.
2056 * "end" points to just after the name.
2057 * When "error" is FALSE do not give an error when not found.
2058 */
2059 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002060compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061{
2062 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002063 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002064 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002066 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067
2068 if (*(*arg + 1) == ':')
2069 {
2070 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002071 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002072 {
2073 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002075 switch (**arg)
2076 {
2077 case 'g': isn_type = ISN_LOADGDICT; break;
2078 case 'w': isn_type = ISN_LOADWDICT; break;
2079 case 't': isn_type = ISN_LOADTDICT; break;
2080 case 'b': isn_type = ISN_LOADBDICT; break;
2081 default:
2082 semsg(_(e_namespace), *arg);
2083 goto theend;
2084 }
2085 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2086 goto theend;
2087 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 else
2090 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002091 isntype_T isn_type = ISN_DROP;
2092
2093 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2094 if (name == NULL)
2095 return FAIL;
2096
2097 switch (**arg)
2098 {
2099 case 'v': res = generate_LOADV(cctx, name, error);
2100 break;
2101 case 's': res = compile_load_scriptvar(cctx, name,
2102 NULL, NULL, error);
2103 break;
2104 case 'g': isn_type = ISN_LOADG; break;
2105 case 'w': isn_type = ISN_LOADW; break;
2106 case 't': isn_type = ISN_LOADT; break;
2107 case 'b': isn_type = ISN_LOADB; break;
2108 default: semsg(_(e_namespace), *arg);
2109 goto theend;
2110 }
2111 if (isn_type != ISN_DROP)
2112 {
2113 // Global, Buffer-local, Window-local and Tabpage-local
2114 // variables can be defined later, thus we don't check if it
2115 // exists, give error at runtime.
2116 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 }
2120 else
2121 {
2122 size_t len = end - *arg;
2123 int idx;
2124 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002125 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126
2127 name = vim_strnsave(*arg, end - *arg);
2128 if (name == NULL)
2129 return FAIL;
2130
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002131 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002133 if (!gen_load_outer)
2134 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 }
2136 else
2137 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002138 lvar_T *lvar = lookup_local(*arg, len, cctx);
2139
2140 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002142 type = lvar->lv_type;
2143 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002144 if (lvar->lv_from_outer)
2145 gen_load_outer = TRUE;
2146 else
2147 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 }
2149 else
2150 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002151 // "var" can be script-local even without using "s:" if it
2152 // already exists.
2153 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2154 == SCRIPT_VERSION_VIM9
2155 || lookup_script(*arg, len) == OK)
2156 res = compile_load_scriptvar(cctx, name, *arg, &end,
2157 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002158
Bram Moolenaara5565e42020-05-09 15:44:01 +02002159 // When the name starts with an uppercase letter or "x:" it
2160 // can be a user defined function.
2161 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2162 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 }
2164 }
2165 if (gen_load)
2166 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002167 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002168 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002169 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002170 cctx->ctx_outer_used = TRUE;
2171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 }
2173
2174 *arg = end;
2175
2176theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002177 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 semsg(_(e_var_notfound), name);
2179 vim_free(name);
2180 return res;
2181}
2182
2183/*
2184 * Compile the argument expressions.
2185 * "arg" points to just after the "(" and is advanced to after the ")"
2186 */
2187 static int
2188compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2189{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002190 char_u *p = *arg;
2191 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192
Bram Moolenaare6085c52020-04-12 20:19:16 +02002193 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002195 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2196 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002197 if (*p == ')')
2198 {
2199 *arg = p + 1;
2200 return OK;
2201 }
2202
Bram Moolenaara5565e42020-05-09 15:44:01 +02002203 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 return FAIL;
2205 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002206
2207 if (*p != ',' && *skipwhite(p) == ',')
2208 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002209 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002210 p = skipwhite(p);
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 {
2214 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002215 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002216 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002217 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002218 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002219 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002221failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002222 emsg(_(e_missing_close));
2223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224}
2225
2226/*
2227 * Compile a function call: name(arg1, arg2)
2228 * "arg" points to "name", "arg + varlen" to the "(".
2229 * "argcount_init" is 1 for "value->method()"
2230 * Instructions:
2231 * EVAL arg1
2232 * EVAL arg2
2233 * BCALL / DCALL / UCALL
2234 */
2235 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002236compile_call(
2237 char_u **arg,
2238 size_t varlen,
2239 cctx_T *cctx,
2240 ppconst_T *ppconst,
2241 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242{
2243 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002244 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 int argcount = argcount_init;
2246 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002247 char_u fname_buf[FLEN_FIXED + 1];
2248 char_u *tofree = NULL;
2249 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002251 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002252 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253
Bram Moolenaara5565e42020-05-09 15:44:01 +02002254 // we can evaluate "has('name')" at compile time
2255 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2256 {
2257 char_u *s = skipwhite(*arg + varlen + 1);
2258 typval_T argvars[2];
2259
2260 argvars[0].v_type = VAR_UNKNOWN;
2261 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002262 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002264 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002265 s = skipwhite(s);
2266 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2267 {
2268 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2269
2270 *arg = s + 1;
2271 argvars[1].v_type = VAR_UNKNOWN;
2272 tv->v_type = VAR_NUMBER;
2273 tv->vval.v_number = 0;
2274 f_has(argvars, tv);
2275 clear_tv(&argvars[0]);
2276 ++ppconst->pp_used;
2277 return OK;
2278 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002279 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002280 }
2281
2282 if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 if (varlen >= sizeof(namebuf))
2286 {
2287 semsg(_("E1011: name too long: %s"), name);
2288 return FAIL;
2289 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 vim_strncpy(namebuf, *arg, varlen);
2291 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292
2293 *arg = skipwhite(*arg + varlen + 1);
2294 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
Bram Moolenaara1773442020-08-12 15:21:22 +02002297 is_autoload = vim_strchr(name, '#') != NULL;
2298 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 {
2300 int idx;
2301
2302 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002303 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002305 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306 else
2307 semsg(_(e_unknownfunc), namebuf);
2308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 }
2310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002312 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002314 {
2315 res = generate_CALL(cctx, ufunc, argcount);
2316 goto theend;
2317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318
2319 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002320 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002321 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002323 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002324 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002325 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002326 garray_T *stack = &cctx->ctx_type_stack;
2327 type_T *type;
2328
2329 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2330 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002331 goto theend;
2332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002334 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002335 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002336 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002337 res = generate_UCALL(cctx, name, argcount);
2338 else
2339 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340
2341theend:
2342 vim_free(tofree);
2343 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344}
2345
2346// like NAMESPACE_CHAR but with 'a' and 'l'.
2347#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2348
2349/*
2350 * Find the end of a variable or function name. Unlike find_name_end() this
2351 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 * Return a pointer to just after the name. Equal to "arg" if there is no
2354 * valid name.
2355 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002356 static char_u *
2357to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358{
2359 char_u *p;
2360
2361 // Quick check for valid starting character.
2362 if (!eval_isnamec1(*arg))
2363 return arg;
2364
2365 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2366 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2367 // and can be used in slice "[n:]".
2368 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002369 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2371 break;
2372 return p;
2373}
2374
2375/*
2376 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002377 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 */
2379 char_u *
2380to_name_const_end(char_u *arg)
2381{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002382 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 typval_T rettv;
2384
2385 if (p == arg && *arg == '[')
2386 {
2387
2388 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002389 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 p = arg;
2391 }
2392 else if (p == arg && *arg == '#' && arg[1] == '{')
2393 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002394 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002396 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 p = arg;
2398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
2400 return p;
2401}
2402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403/*
2404 * parse a list: [expr, expr]
2405 * "*arg" points to the '['.
2406 */
2407 static int
2408compile_list(char_u **arg, cctx_T *cctx)
2409{
2410 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002411 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 int count = 0;
2413
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002414 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002416 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002417 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002418 semsg(_(e_list_end), *arg);
2419 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002420 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002421 if (*p == ',')
2422 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002423 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002424 return FAIL;
2425 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002426 if (*p == ']')
2427 {
2428 ++p;
2429 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002430 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002431 p += STRLEN(p);
2432 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002433 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002434 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 break;
2436 ++count;
2437 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002438 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002440 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2441 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002442 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002443 return FAIL;
2444 }
2445 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002446 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 p = skipwhite(p);
2448 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002449 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
2451 generate_NEWLIST(cctx, count);
2452 return OK;
2453}
2454
2455/*
2456 * parse a lambda: {arg, arg -> expr}
2457 * "*arg" points to the '{'.
2458 */
2459 static int
2460compile_lambda(char_u **arg, cctx_T *cctx)
2461{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 typval_T rettv;
2463 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002464 evalarg_T evalarg;
2465
2466 CLEAR_FIELD(evalarg);
2467 evalarg.eval_flags = EVAL_EVALUATE;
2468 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469
2470 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002471 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002475 ++ufunc->uf_refcount;
2476 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002477 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478
2479 // The function will have one line: "return {expr}".
2480 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002481 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002483 clear_evalarg(&evalarg, NULL);
2484
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002485 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002486 {
2487 // The return type will now be known.
2488 set_function_type(ufunc);
2489
2490 return generate_FUNCREF(cctx, ufunc);
2491 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002492
2493 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 return FAIL;
2495}
2496
2497/*
2498 * Compile a lamda call: expr->{lambda}(args)
2499 * "arg" points to the "{".
2500 */
2501 static int
2502compile_lambda_call(char_u **arg, cctx_T *cctx)
2503{
2504 ufunc_T *ufunc;
2505 typval_T rettv;
2506 int argcount = 1;
2507 int ret = FAIL;
2508
2509 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002510 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 return FAIL;
2512
2513 if (**arg != '(')
2514 {
2515 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002516 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 else
2518 semsg(_(e_missing_paren), "lambda");
2519 clear_tv(&rettv);
2520 return FAIL;
2521 }
2522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 ufunc = rettv.vval.v_partial->pt_func;
2524 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002525 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002526 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002527
2528 // The function will have one line: "return {expr}".
2529 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002530 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531
2532 // compile the arguments
2533 *arg = skipwhite(*arg + 1);
2534 if (compile_arguments(arg, cctx, &argcount) == OK)
2535 // call the compiled function
2536 ret = generate_CALL(cctx, ufunc, argcount);
2537
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002538 if (ret == FAIL)
2539 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 return ret;
2541}
2542
2543/*
2544 * parse a dict: {'key': val} or #{key: val}
2545 * "*arg" points to the '{'.
2546 */
2547 static int
2548compile_dict(char_u **arg, cctx_T *cctx, int literal)
2549{
2550 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002551 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 int count = 0;
2553 dict_T *d = dict_alloc();
2554 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002555 char_u *whitep = *arg;
2556 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557
2558 if (d == NULL)
2559 return FAIL;
2560 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002561 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 {
2563 char_u *key = NULL;
2564
Bram Moolenaar23c55272020-06-21 16:58:13 +02002565 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002566 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002567 *arg = NULL;
2568 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002569 }
2570
2571 if (**arg == '}')
2572 break;
2573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 if (literal)
2575 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002576 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577
Bram Moolenaar2c330432020-04-13 14:41:35 +02002578 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 {
2580 semsg(_("E1014: Invalid key: %s"), *arg);
2581 return FAIL;
2582 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002583 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 if (generate_PUSHS(cctx, key) == FAIL)
2585 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002586 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 }
2588 else
2589 {
2590 isn_T *isn;
2591
Bram Moolenaara5565e42020-05-09 15:44:01 +02002592 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2595 if (isn->isn_type == ISN_PUSHS)
2596 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002597 else
2598 {
2599 type_T *keytype = ((type_T **)stack->ga_data)
2600 [stack->ga_len - 1];
2601 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2602 return FAIL;
2603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 }
2605
2606 // Check for duplicate keys, if using string keys.
2607 if (key != NULL)
2608 {
2609 item = dict_find(d, key, -1);
2610 if (item != NULL)
2611 {
2612 semsg(_(e_duplicate_key), key);
2613 goto failret;
2614 }
2615 item = dictitem_alloc(key);
2616 if (item != NULL)
2617 {
2618 item->di_tv.v_type = VAR_UNKNOWN;
2619 item->di_tv.v_lock = 0;
2620 if (dict_add(d, item) == FAIL)
2621 dictitem_free(item);
2622 }
2623 }
2624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 if (**arg != ':')
2626 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002627 if (*skipwhite(*arg) == ':')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002628 semsg(_(e_no_white_space_allowed_before), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002629 else
2630 semsg(_(e_missing_dict_colon), *arg);
2631 return FAIL;
2632 }
2633 whitep = *arg + 1;
2634 if (!IS_WHITE_OR_NUL(*whitep))
2635 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002636 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 return FAIL;
2638 }
2639
2640 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002641 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002642 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 *arg = NULL;
2644 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002645 }
2646
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 return FAIL;
2649 ++count;
2650
Bram Moolenaar2c330432020-04-13 14:41:35 +02002651 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002652 *arg = skipwhite(*arg);
2653 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002654 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655 *arg = NULL;
2656 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 if (**arg == '}')
2659 break;
2660 if (**arg != ',')
2661 {
2662 semsg(_(e_missing_dict_comma), *arg);
2663 goto failret;
2664 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002665 if (IS_WHITE_OR_NUL(*whitep))
2666 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002667 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002668 return FAIL;
2669 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002670 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 *arg = skipwhite(*arg + 1);
2672 }
2673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 *arg = *arg + 1;
2675
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002676 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002677 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002678 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002679 *arg += STRLEN(*arg);
2680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 dict_unref(d);
2682 return generate_NEWDICT(cctx, count);
2683
2684failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002685 if (*arg == NULL)
2686 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 dict_unref(d);
2688 return FAIL;
2689}
2690
2691/*
2692 * Compile "&option".
2693 */
2694 static int
2695compile_get_option(char_u **arg, cctx_T *cctx)
2696{
2697 typval_T rettv;
2698 char_u *start = *arg;
2699 int ret;
2700
2701 // parse the option and get the current value to get the type.
2702 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002703 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (ret == OK)
2705 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002706 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 char_u *name = vim_strnsave(start, *arg - start);
2708 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2709
2710 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2711 vim_free(name);
2712 }
2713 clear_tv(&rettv);
2714
2715 return ret;
2716}
2717
2718/*
2719 * Compile "$VAR".
2720 */
2721 static int
2722compile_get_env(char_u **arg, cctx_T *cctx)
2723{
2724 char_u *start = *arg;
2725 int len;
2726 int ret;
2727 char_u *name;
2728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 ++*arg;
2730 len = get_env_len(arg);
2731 if (len == 0)
2732 {
2733 semsg(_(e_syntax_at), start - 1);
2734 return FAIL;
2735 }
2736
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002737 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 name = vim_strnsave(start, len + 1);
2739 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2740 vim_free(name);
2741 return ret;
2742}
2743
2744/*
2745 * Compile "@r".
2746 */
2747 static int
2748compile_get_register(char_u **arg, cctx_T *cctx)
2749{
2750 int ret;
2751
2752 ++*arg;
2753 if (**arg == NUL)
2754 {
2755 semsg(_(e_syntax_at), *arg - 1);
2756 return FAIL;
2757 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002758 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 {
2760 emsg_invreg(**arg);
2761 return FAIL;
2762 }
2763 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2764 ++*arg;
2765 return ret;
2766}
2767
2768/*
2769 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002770 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 */
2772 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002773apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002775 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776
2777 // this works from end to start
2778 while (p > start)
2779 {
2780 --p;
2781 if (*p == '-' || *p == '+')
2782 {
2783 // only '-' has an effect, for '+' we only check the type
2784#ifdef FEAT_FLOAT
2785 if (rettv->v_type == VAR_FLOAT)
2786 {
2787 if (*p == '-')
2788 rettv->vval.v_float = -rettv->vval.v_float;
2789 }
2790 else
2791#endif
2792 {
2793 varnumber_T val;
2794 int error = FALSE;
2795
2796 // tv_get_number_chk() accepts a string, but we don't want that
2797 // here
2798 if (check_not_string(rettv) == FAIL)
2799 return FAIL;
2800 val = tv_get_number_chk(rettv, &error);
2801 clear_tv(rettv);
2802 if (error)
2803 return FAIL;
2804 if (*p == '-')
2805 val = -val;
2806 rettv->v_type = VAR_NUMBER;
2807 rettv->vval.v_number = val;
2808 }
2809 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002810 else if (numeric_only)
2811 {
2812 ++p;
2813 break;
2814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 else
2816 {
2817 int v = tv2bool(rettv);
2818
2819 // '!' is permissive in the type.
2820 clear_tv(rettv);
2821 rettv->v_type = VAR_BOOL;
2822 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2823 }
2824 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002825 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 return OK;
2827}
2828
2829/*
2830 * Recognize v: variables that are constants and set "rettv".
2831 */
2832 static void
2833get_vim_constant(char_u **arg, typval_T *rettv)
2834{
2835 if (STRNCMP(*arg, "v:true", 6) == 0)
2836 {
2837 rettv->v_type = VAR_BOOL;
2838 rettv->vval.v_number = VVAL_TRUE;
2839 *arg += 6;
2840 }
2841 else if (STRNCMP(*arg, "v:false", 7) == 0)
2842 {
2843 rettv->v_type = VAR_BOOL;
2844 rettv->vval.v_number = VVAL_FALSE;
2845 *arg += 7;
2846 }
2847 else if (STRNCMP(*arg, "v:null", 6) == 0)
2848 {
2849 rettv->v_type = VAR_SPECIAL;
2850 rettv->vval.v_number = VVAL_NULL;
2851 *arg += 6;
2852 }
2853 else if (STRNCMP(*arg, "v:none", 6) == 0)
2854 {
2855 rettv->v_type = VAR_SPECIAL;
2856 rettv->vval.v_number = VVAL_NONE;
2857 *arg += 6;
2858 }
2859}
2860
Bram Moolenaar696ba232020-07-29 21:20:41 +02002861 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002862get_compare_type(char_u *p, int *len, int *type_is)
2863{
2864 exptype_T type = EXPR_UNKNOWN;
2865 int i;
2866
2867 switch (p[0])
2868 {
2869 case '=': if (p[1] == '=')
2870 type = EXPR_EQUAL;
2871 else if (p[1] == '~')
2872 type = EXPR_MATCH;
2873 break;
2874 case '!': if (p[1] == '=')
2875 type = EXPR_NEQUAL;
2876 else if (p[1] == '~')
2877 type = EXPR_NOMATCH;
2878 break;
2879 case '>': if (p[1] != '=')
2880 {
2881 type = EXPR_GREATER;
2882 *len = 1;
2883 }
2884 else
2885 type = EXPR_GEQUAL;
2886 break;
2887 case '<': if (p[1] != '=')
2888 {
2889 type = EXPR_SMALLER;
2890 *len = 1;
2891 }
2892 else
2893 type = EXPR_SEQUAL;
2894 break;
2895 case 'i': if (p[1] == 's')
2896 {
2897 // "is" and "isnot"; but not a prefix of a name
2898 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2899 *len = 5;
2900 i = p[*len];
2901 if (!isalnum(i) && i != '_')
2902 {
2903 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2904 *type_is = TRUE;
2905 }
2906 }
2907 break;
2908 }
2909 return type;
2910}
2911
2912/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002914 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 */
2916 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002917compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002919 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920
2921 // this works from end to start
2922 while (p > start)
2923 {
2924 --p;
2925 if (*p == '-' || *p == '+')
2926 {
2927 int negate = *p == '-';
2928 isn_T *isn;
2929
2930 // TODO: check type
2931 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2932 {
2933 --p;
2934 if (*p == '-')
2935 negate = !negate;
2936 }
2937 // only '-' has an effect, for '+' we only check the type
2938 if (negate)
2939 isn = generate_instr(cctx, ISN_NEGATENR);
2940 else
2941 isn = generate_instr(cctx, ISN_CHECKNR);
2942 if (isn == NULL)
2943 return FAIL;
2944 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002945 else if (numeric_only)
2946 {
2947 ++p;
2948 break;
2949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 else
2951 {
2952 int invert = TRUE;
2953
2954 while (p > start && p[-1] == '!')
2955 {
2956 --p;
2957 invert = !invert;
2958 }
2959 if (generate_2BOOL(cctx, invert) == FAIL)
2960 return FAIL;
2961 }
2962 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002963 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 return OK;
2965}
2966
2967/*
2968 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002969 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 */
2971 static int
2972compile_subscript(
2973 char_u **arg,
2974 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002975 char_u *start_leader,
2976 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002977 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002979 char_u *name_start = *end_leader;
2980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 for (;;)
2982 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 char_u *p = skipwhite(*arg);
2984
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002985 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002986 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002987 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002988
2989 // If a following line starts with "->{" or "->X" advance to that
2990 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002991 // Also if a following line starts with ".x".
2992 if (next != NULL &&
2993 ((next[0] == '-' && next[1] == '>'
2994 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002995 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002996 {
2997 next = next_line_from_context(cctx, TRUE);
2998 if (next == NULL)
2999 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003000 *arg = next;
3001 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003002 }
3003 }
3004
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003005 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3006 // not a function call.
3007 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003009 garray_T *stack = &cctx->ctx_type_stack;
3010 type_T *type;
3011 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003013 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003014 return FAIL;
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003017 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3018
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003019 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3021 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003022 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 return FAIL;
3024 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003025 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003027 char_u *pstart = p;
3028
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003029 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003030 return FAIL;
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 // something->method()
3033 // Apply the '!', '-' and '+' first:
3034 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003035 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003038 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003039 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003040 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 if (**arg == '{')
3042 {
3043 // lambda call: list->{lambda}
3044 if (compile_lambda_call(arg, cctx) == FAIL)
3045 return FAIL;
3046 }
3047 else
3048 {
3049 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003050 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003051 if (!eval_isnamec1(*p))
3052 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003053 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003054 return FAIL;
3055 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003056 if (ASCII_ISALPHA(*p) && p[1] == ':')
3057 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003058 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 ;
3060 if (*p != '(')
3061 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003062 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 return FAIL;
3064 }
3065 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003066 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 return FAIL;
3068 }
3069 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003070 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003072 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003073 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003074 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003075
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003076 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003077 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003078 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003079 // TODO: blob index
3080 // TODO: more arguments
3081 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003082 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003083 return FAIL;
3084
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003085 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003086 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003087 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003088 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003089 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 return FAIL;
3091
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003092 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 if (**arg != ']')
3095 {
3096 emsg(_(e_missbrac));
3097 return FAIL;
3098 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003099 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003101 // We can index a list and a dict. If we don't know the type
3102 // we can use the index value type.
3103 // TODO: If we don't know use an instruction to figure it out at
3104 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003105 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003106 vtype = (*typep)->tt_type;
3107 if (*typep == &t_any)
3108 {
3109 type_T *valtype = ((type_T **)stack->ga_data)
3110 [stack->ga_len - 1];
3111 if (valtype == &t_string)
3112 vtype = VAR_DICT;
3113 }
3114 if (vtype == VAR_DICT)
3115 {
3116 if ((*typep)->tt_type == VAR_DICT)
3117 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003118 else
3119 {
3120 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3121 return FAIL;
3122 *typep = &t_any;
3123 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003124 if (may_generate_2STRING(-1, cctx) == FAIL)
3125 return FAIL;
3126 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3127 return FAIL;
3128 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003129 else if (vtype == VAR_STRING)
3130 {
3131 *typep = &t_number;
3132 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3133 return FAIL;
3134 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003135 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003136 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003137 if ((*typep)->tt_type == VAR_LIST)
3138 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003139 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 return FAIL;
3141 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003142 else
3143 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003144 emsg(_(e_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003145 return FAIL;
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003148 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003150 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003151 return FAIL;
3152
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003153 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003154 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3155 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003157 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003158 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 while (eval_isnamec(*p))
3160 MB_PTR_ADV(p);
3161 if (p == *arg)
3162 {
3163 semsg(_(e_syntax_at), *arg);
3164 return FAIL;
3165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003166 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 return FAIL;
3168 *arg = p;
3169 }
3170 else
3171 break;
3172 }
3173
3174 // TODO - see handle_subscript():
3175 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3176 // Don't do this when "Func" is already a partial that was bound
3177 // explicitly (pt_auto is FALSE).
3178
3179 return OK;
3180}
3181
3182/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003183 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3184 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003186 * If the value is a constant "ppconst->pp_ret" will be set.
3187 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003188 *
3189 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 */
3191
3192/*
3193 * number number constant
3194 * 0zFFFFFFFF Blob constant
3195 * "string" string constant
3196 * 'string' literal string constant
3197 * &option-name option value
3198 * @r register contents
3199 * identifier variable value
3200 * function() function call
3201 * $VAR environment variable
3202 * (expression) nested expression
3203 * [expr, expr] List
3204 * {key: val, key: val} Dictionary
3205 * #{key: val, key: val} Dictionary with literal keys
3206 *
3207 * Also handle:
3208 * ! in front logical NOT
3209 * - in front unary minus
3210 * + in front unary plus (ignored)
3211 * trailing (arg) funcref/partial call
3212 * trailing [] subscript in String or List
3213 * trailing .name entry in Dictionary
3214 * trailing ->name() method call
3215 */
3216 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003217compile_expr7(
3218 char_u **arg,
3219 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003220 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 char_u *start_leader, *end_leader;
3223 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003224 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003225 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
3227 /*
3228 * Skip '!', '-' and '+' characters. They are handled later.
3229 */
3230 start_leader = *arg;
3231 while (**arg == '!' || **arg == '-' || **arg == '+')
3232 *arg = skipwhite(*arg + 1);
3233 end_leader = *arg;
3234
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003235 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 switch (**arg)
3237 {
3238 /*
3239 * Number constant.
3240 */
3241 case '0': // also for blob starting with 0z
3242 case '1':
3243 case '2':
3244 case '3':
3245 case '4':
3246 case '5':
3247 case '6':
3248 case '7':
3249 case '8':
3250 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003251 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003253 // Apply "-" and "+" just before the number now, right to
3254 // left. Matters especially when "->" follows. Stops at
3255 // '!'.
3256 if (apply_leader(rettv, TRUE,
3257 start_leader, &end_leader) == FAIL)
3258 {
3259 clear_tv(rettv);
3260 return FAIL;
3261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 break;
3263
3264 /*
3265 * String constant: "string".
3266 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003267 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 return FAIL;
3269 break;
3270
3271 /*
3272 * Literal string constant: 'str''ing'.
3273 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 return FAIL;
3276 break;
3277
3278 /*
3279 * Constant Vim variable.
3280 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003281 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 ret = NOTDONE;
3283 break;
3284
3285 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003286 * "true" constant
3287 */
3288 case 't': if (STRNCMP(*arg, "true", 4) == 0
3289 && !eval_isnamec((*arg)[4]))
3290 {
3291 *arg += 4;
3292 rettv->v_type = VAR_BOOL;
3293 rettv->vval.v_number = VVAL_TRUE;
3294 }
3295 else
3296 ret = NOTDONE;
3297 break;
3298
3299 /*
3300 * "false" constant
3301 */
3302 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3303 && !eval_isnamec((*arg)[5]))
3304 {
3305 *arg += 5;
3306 rettv->v_type = VAR_BOOL;
3307 rettv->vval.v_number = VVAL_FALSE;
3308 }
3309 else
3310 ret = NOTDONE;
3311 break;
3312
3313 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 * List: [expr, expr]
3315 */
3316 case '[': ret = compile_list(arg, cctx);
3317 break;
3318
3319 /*
3320 * Dictionary: #{key: val, key: val}
3321 */
3322 case '#': if ((*arg)[1] == '{')
3323 {
3324 ++*arg;
3325 ret = compile_dict(arg, cctx, TRUE);
3326 }
3327 else
3328 ret = NOTDONE;
3329 break;
3330
3331 /*
3332 * Lambda: {arg, arg -> expr}
3333 * Dictionary: {'key': val, 'key': val}
3334 */
3335 case '{': {
3336 char_u *start = skipwhite(*arg + 1);
3337
3338 // Find out what comes after the arguments.
3339 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003340 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 if (ret != FAIL && *start == '>')
3342 ret = compile_lambda(arg, cctx);
3343 else
3344 ret = compile_dict(arg, cctx, FALSE);
3345 }
3346 break;
3347
3348 /*
3349 * Option value: &name
3350 */
3351 case '&': ret = compile_get_option(arg, cctx);
3352 break;
3353
3354 /*
3355 * Environment variable: $VAR.
3356 */
3357 case '$': ret = compile_get_env(arg, cctx);
3358 break;
3359
3360 /*
3361 * Register contents: @r.
3362 */
3363 case '@': ret = compile_get_register(arg, cctx);
3364 break;
3365 /*
3366 * nested expression: (expression).
3367 */
3368 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003369
3370 // recursive!
3371 if (ppconst->pp_used <= PPSIZE - 10)
3372 {
3373 ret = compile_expr1(arg, cctx, ppconst);
3374 }
3375 else
3376 {
3377 // Not enough space in ppconst, flush constants.
3378 if (generate_ppconst(cctx, ppconst) == FAIL)
3379 return FAIL;
3380 ret = compile_expr0(arg, cctx);
3381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 *arg = skipwhite(*arg);
3383 if (**arg == ')')
3384 ++*arg;
3385 else if (ret == OK)
3386 {
3387 emsg(_(e_missing_close));
3388 ret = FAIL;
3389 }
3390 break;
3391
3392 default: ret = NOTDONE;
3393 break;
3394 }
3395 if (ret == FAIL)
3396 return FAIL;
3397
Bram Moolenaar1c747212020-05-09 18:28:34 +02003398 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003400 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003401 clear_tv(rettv);
3402 else
3403 // A constant expression can possibly be handled compile time,
3404 // return the value instead of generating code.
3405 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 }
3407 else if (ret == NOTDONE)
3408 {
3409 char_u *p;
3410 int r;
3411
3412 if (!eval_isnamec1(**arg))
3413 {
3414 semsg(_("E1015: Name expected: %s"), *arg);
3415 return FAIL;
3416 }
3417
3418 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003419 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 {
3422 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425 {
3426 if (generate_ppconst(cctx, ppconst) == FAIL)
3427 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 if (r == FAIL)
3431 return FAIL;
3432 }
3433
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003434 // Handle following "[]", ".member", etc.
3435 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003436 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003437 ppconst) == FAIL)
3438 return FAIL;
3439 if (ppconst->pp_used > 0)
3440 {
3441 // apply the '!', '-' and '+' before the constant
3442 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003443 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003444 return FAIL;
3445 return OK;
3446 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003447 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003449 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450}
3451
3452/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003453 * Give the "white on both sides" error, taking the operator from "p[len]".
3454 */
3455 void
3456error_white_both(char_u *op, int len)
3457{
3458 char_u buf[10];
3459
3460 vim_strncpy(buf, op, len);
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003461 semsg(_(e_white_space_required_before_and_after), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003462}
3463
3464/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003465 * <type>expr7: runtime type check / conversion
3466 */
3467 static int
3468compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3469{
3470 type_T *want_type = NULL;
3471
3472 // Recognize <type>
3473 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3474 {
3475 int called_emsg_before = called_emsg;
3476
3477 ++*arg;
3478 want_type = parse_type(arg, cctx->ctx_type_list);
3479 if (called_emsg != called_emsg_before)
3480 return FAIL;
3481
3482 if (**arg != '>')
3483 {
3484 if (*skipwhite(*arg) == '>')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003485 semsg(_(e_no_white_space_allowed_before), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003486 else
3487 emsg(_("E1104: Missing >"));
3488 return FAIL;
3489 }
3490 ++*arg;
3491 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3492 return FAIL;
3493 }
3494
3495 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3496 return FAIL;
3497
3498 if (want_type != NULL)
3499 {
3500 garray_T *stack = &cctx->ctx_type_stack;
3501 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3502
3503 if (check_type(want_type, actual, FALSE) == FAIL)
3504 {
3505 generate_ppconst(cctx, ppconst);
3506 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3507 return FAIL;
3508 }
3509 }
3510
3511 return OK;
3512}
3513
3514/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 * * number multiplication
3516 * / number division
3517 * % number modulo
3518 */
3519 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003520compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521{
3522 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003523 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003524 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003526 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003527 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 return FAIL;
3529
3530 /*
3531 * Repeat computing, until no "*", "/" or "%" is following.
3532 */
3533 for (;;)
3534 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003535 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 if (*op != '*' && *op != '/' && *op != '%')
3537 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003538 if (next != NULL)
3539 {
3540 *arg = next_line_from_context(cctx, TRUE);
3541 op = skipwhite(*arg);
3542 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003543
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003544 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003546 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003547 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 }
3549 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003550 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003553 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003554 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003556
3557 if (ppconst->pp_used == ppconst_used + 2
3558 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3559 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003560 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003561 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3562 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003563 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003565 // both are numbers: compute the result
3566 switch (*op)
3567 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003568 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003569 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003570 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003571 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003572 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003573 break;
3574 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003575 tv1->vval.v_number = res;
3576 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003577 }
3578 else
3579 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003580 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003581 generate_two_op(cctx, op);
3582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 }
3584
3585 return OK;
3586}
3587
3588/*
3589 * + number addition
3590 * - number subtraction
3591 * .. string concatenation
3592 */
3593 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003594compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595{
3596 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003597 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003599 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600
3601 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003602 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 return FAIL;
3604
3605 /*
3606 * Repeat computing, until no "+", "-" or ".." is following.
3607 */
3608 for (;;)
3609 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003610 op = may_peek_next_line(cctx, *arg, &next);
3611 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 break;
3613 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003614 if (next != NULL)
3615 {
3616 *arg = next_line_from_context(cctx, TRUE);
3617 op = skipwhite(*arg);
3618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003620 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003622 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
3625
3626 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003627 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003630 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003631 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 return FAIL;
3633
Bram Moolenaara5565e42020-05-09 15:44:01 +02003634 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003635 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003636 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3637 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3638 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3639 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003641 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3642 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003643
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003644 // concat/subtract/add constant numbers
3645 if (*op == '+')
3646 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3647 else if (*op == '-')
3648 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3649 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003650 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003651 // concatenate constant strings
3652 char_u *s1 = tv1->vval.v_string;
3653 char_u *s2 = tv2->vval.v_string;
3654 size_t len1 = STRLEN(s1);
3655
3656 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3657 if (tv1->vval.v_string == NULL)
3658 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003659 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003660 return FAIL;
3661 }
3662 mch_memmove(tv1->vval.v_string, s1, len1);
3663 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003664 vim_free(s1);
3665 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003666 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003667 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 }
3669 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003670 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003671 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003672 if (*op == '.')
3673 {
3674 if (may_generate_2STRING(-2, cctx) == FAIL
3675 || may_generate_2STRING(-1, cctx) == FAIL)
3676 return FAIL;
3677 generate_instr_drop(cctx, ISN_CONCAT, 1);
3678 }
3679 else
3680 generate_two_op(cctx, op);
3681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
3683
3684 return OK;
3685}
3686
3687/*
3688 * expr5a == expr5b
3689 * expr5a =~ expr5b
3690 * expr5a != expr5b
3691 * expr5a !~ expr5b
3692 * expr5a > expr5b
3693 * expr5a >= expr5b
3694 * expr5a < expr5b
3695 * expr5a <= expr5b
3696 * expr5a is expr5b
3697 * expr5a isnot expr5b
3698 *
3699 * Produces instructions:
3700 * EVAL expr5a Push result of "expr5a"
3701 * EVAL expr5b Push result of "expr5b"
3702 * COMPARE one of the compare instructions
3703 */
3704 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003705compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706{
3707 exptype_T type = EXPR_UNKNOWN;
3708 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003709 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003712 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713
3714 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003715 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 return FAIL;
3717
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003718 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003719 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
3721 /*
3722 * If there is a comparative operator, use it.
3723 */
3724 if (type != EXPR_UNKNOWN)
3725 {
3726 int ic = FALSE; // Default: do not ignore case
3727
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003728 if (next != NULL)
3729 {
3730 *arg = next_line_from_context(cctx, TRUE);
3731 p = skipwhite(*arg);
3732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 if (type_is && (p[len] == '?' || p[len] == '#'))
3734 {
3735 semsg(_(e_invexpr2), *arg);
3736 return FAIL;
3737 }
3738 // extra question mark appended: ignore case
3739 if (p[len] == '?')
3740 {
3741 ic = TRUE;
3742 ++len;
3743 }
3744 // extra '#' appended: match case (ignored)
3745 else if (p[len] == '#')
3746 ++len;
3747 // nothing appended: match case
3748
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003749 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003751 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754
3755 // get the second variable
3756 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003757 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003758 return FAIL;
3759
Bram Moolenaara5565e42020-05-09 15:44:01 +02003760 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 return FAIL;
3762
Bram Moolenaara5565e42020-05-09 15:44:01 +02003763 if (ppconst->pp_used == ppconst_used + 2)
3764 {
3765 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3766 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3767 int ret;
3768
3769 // Both sides are a constant, compute the result now.
3770 // First check for a valid combination of types, this is more
3771 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003772 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003773 ret = FAIL;
3774 else
3775 {
3776 ret = typval_compare(tv1, tv2, type, ic);
3777 tv1->v_type = VAR_BOOL;
3778 tv1->vval.v_number = tv1->vval.v_number
3779 ? VVAL_TRUE : VVAL_FALSE;
3780 clear_tv(tv2);
3781 --ppconst->pp_used;
3782 }
3783 return ret;
3784 }
3785
3786 generate_ppconst(cctx, ppconst);
3787 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
3789
3790 return OK;
3791}
3792
Bram Moolenaar7f141552020-05-09 17:35:53 +02003793static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795/*
3796 * Compile || or &&.
3797 */
3798 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003799compile_and_or(
3800 char_u **arg,
3801 cctx_T *cctx,
3802 char *op,
3803 ppconst_T *ppconst,
3804 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003806 char_u *next;
3807 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 int opchar = *op;
3809
3810 if (p[0] == opchar && p[1] == opchar)
3811 {
3812 garray_T *instr = &cctx->ctx_instr;
3813 garray_T end_ga;
3814
3815 /*
3816 * Repeat until there is no following "||" or "&&"
3817 */
3818 ga_init2(&end_ga, sizeof(int), 10);
3819 while (p[0] == opchar && p[1] == opchar)
3820 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003821 if (next != NULL)
3822 {
3823 *arg = next_line_from_context(cctx, TRUE);
3824 p = skipwhite(*arg);
3825 }
3826
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003827 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3828 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003829 semsg(_(e_white_space_required_before_and_after), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003830 return FAIL;
3831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833 // TODO: use ppconst if the value is a constant
3834 generate_ppconst(cctx, ppconst);
3835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 if (ga_grow(&end_ga, 1) == FAIL)
3837 {
3838 ga_clear(&end_ga);
3839 return FAIL;
3840 }
3841 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3842 ++end_ga.ga_len;
3843 generate_JUMP(cctx, opchar == '|'
3844 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3845
3846 // eval the next expression
3847 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003848 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003849 return FAIL;
3850
Bram Moolenaara5565e42020-05-09 15:44:01 +02003851 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3852 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
3854 ga_clear(&end_ga);
3855 return FAIL;
3856 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003857
3858 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003860 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
3862 // Fill in the end label in all jumps.
3863 while (end_ga.ga_len > 0)
3864 {
3865 isn_T *isn;
3866
3867 --end_ga.ga_len;
3868 isn = ((isn_T *)instr->ga_data)
3869 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3870 isn->isn_arg.jump.jump_where = instr->ga_len;
3871 }
3872 ga_clear(&end_ga);
3873 }
3874
3875 return OK;
3876}
3877
3878/*
3879 * expr4a && expr4a && expr4a logical AND
3880 *
3881 * Produces instructions:
3882 * EVAL expr4a Push result of "expr4a"
3883 * JUMP_AND_KEEP_IF_FALSE end
3884 * EVAL expr4b Push result of "expr4b"
3885 * JUMP_AND_KEEP_IF_FALSE end
3886 * EVAL expr4c Push result of "expr4c"
3887 * end:
3888 */
3889 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003892 int ppconst_used = ppconst->pp_used;
3893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 return FAIL;
3897
3898 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900}
3901
3902/*
3903 * expr3a || expr3b || expr3c logical OR
3904 *
3905 * Produces instructions:
3906 * EVAL expr3a Push result of "expr3a"
3907 * JUMP_AND_KEEP_IF_TRUE end
3908 * EVAL expr3b Push result of "expr3b"
3909 * JUMP_AND_KEEP_IF_TRUE end
3910 * EVAL expr3c Push result of "expr3c"
3911 * end:
3912 */
3913 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003916 int ppconst_used = ppconst->pp_used;
3917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 return FAIL;
3921
3922 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924}
3925
3926/*
3927 * Toplevel expression: expr2 ? expr1a : expr1b
3928 *
3929 * Produces instructions:
3930 * EVAL expr2 Push result of "expr"
3931 * JUMP_IF_FALSE alt jump if false
3932 * EVAL expr1a
3933 * JUMP_ALWAYS end
3934 * alt: EVAL expr1b
3935 * end:
3936 */
3937 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939{
3940 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003942 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943
Bram Moolenaar61a89812020-05-07 16:58:17 +02003944 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 return FAIL;
3947
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003948 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 if (*p == '?')
3950 {
3951 garray_T *instr = &cctx->ctx_instr;
3952 garray_T *stack = &cctx->ctx_type_stack;
3953 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003954 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003956 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003958 int has_const_expr = FALSE;
3959 int const_value = FALSE;
3960 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003962 if (next != NULL)
3963 {
3964 *arg = next_line_from_context(cctx, TRUE);
3965 p = skipwhite(*arg);
3966 }
3967
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003968 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3969 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003970 semsg(_(e_white_space_required_before_and_after), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003971 return FAIL;
3972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973
Bram Moolenaara5565e42020-05-09 15:44:01 +02003974 if (ppconst->pp_used == ppconst_used + 1)
3975 {
3976 // the condition is a constant, we know whether the ? or the :
3977 // expression is to be evaluated.
3978 has_const_expr = TRUE;
3979 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3980 clear_tv(&ppconst->pp_tv[ppconst_used]);
3981 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003982 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3983 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 }
3985 else
3986 {
3987 generate_ppconst(cctx, ppconst);
3988 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990
3991 // evaluate the second expression; any type is accepted
3992 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003993 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003994 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003995 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003996 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997
Bram Moolenaara5565e42020-05-09 15:44:01 +02003998 if (!has_const_expr)
3999 {
4000 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002 // remember the type and drop it
4003 --stack->ga_len;
4004 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 end_idx = instr->ga_len;
4007 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4008
4009 // jump here from JUMP_IF_FALSE
4010 isn = ((isn_T *)instr->ga_data) + alt_idx;
4011 isn->isn_arg.jump.jump_where = instr->ga_len;
4012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013
4014 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004015 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 if (*p != ':')
4017 {
4018 emsg(_(e_missing_colon));
4019 return FAIL;
4020 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004021 if (next != NULL)
4022 {
4023 *arg = next_line_from_context(cctx, TRUE);
4024 p = skipwhite(*arg);
4025 }
4026
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004027 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4028 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004029 semsg(_(e_white_space_required_before_and_after), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004030 return FAIL;
4031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032
4033 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004034 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004035 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4036 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004038 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004039 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004040 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaara5565e42020-05-09 15:44:01 +02004043 if (!has_const_expr)
4044 {
4045 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 // If the types differ, the result has a more generic type.
4048 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4049 common_type(type1, type2, &type2, cctx->ctx_type_list);
4050
4051 // jump here from JUMP_ALWAYS
4052 isn = ((isn_T *)instr->ga_data) + end_idx;
4053 isn->isn_arg.jump.jump_where = instr->ga_len;
4054 }
4055
4056 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 }
4058 return OK;
4059}
4060
4061/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062 * Toplevel expression.
4063 */
4064 static int
4065compile_expr0(char_u **arg, cctx_T *cctx)
4066{
4067 ppconst_T ppconst;
4068
4069 CLEAR_FIELD(ppconst);
4070 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4071 {
4072 clear_ppconst(&ppconst);
4073 return FAIL;
4074 }
4075 if (generate_ppconst(cctx, &ppconst) == FAIL)
4076 return FAIL;
4077 return OK;
4078}
4079
4080/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 * compile "return [expr]"
4082 */
4083 static char_u *
4084compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4085{
4086 char_u *p = arg;
4087 garray_T *stack = &cctx->ctx_type_stack;
4088 type_T *stack_type;
4089
4090 if (*p != NUL && *p != '|' && *p != '\n')
4091 {
4092 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 return NULL;
4095
4096 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4097 if (set_return_type)
4098 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004099 else
4100 {
4101 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4102 && stack_type->tt_type != VAR_VOID
4103 && stack_type->tt_type != VAR_UNKNOWN)
4104 {
4105 emsg(_("E1096: Returning a value in a function without a return type"));
4106 return NULL;
4107 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004108 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4109 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 }
4113 else
4114 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004115 // "set_return_type" cannot be TRUE, only used for a lambda which
4116 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004117 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4118 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 {
4120 emsg(_("E1003: Missing return value"));
4121 return NULL;
4122 }
4123
4124 // No argument, return zero.
4125 generate_PUSHNR(cctx, 0);
4126 }
4127
4128 if (generate_instr(cctx, ISN_RETURN) == NULL)
4129 return NULL;
4130
4131 // "return val | endif" is possible
4132 return skipwhite(p);
4133}
4134
4135/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004136 * Get a line from the compilation context, compatible with exarg_T getline().
4137 * Return a pointer to the line in allocated memory.
4138 * Return NULL for end-of-file or some error.
4139 */
4140 static char_u *
4141exarg_getline(
4142 int c UNUSED,
4143 void *cookie,
4144 int indent UNUSED,
4145 int do_concat UNUSED)
4146{
4147 cctx_T *cctx = (cctx_T *)cookie;
4148
4149 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4150 {
4151 iemsg("Heredoc got to end");
4152 return NULL;
4153 }
4154 ++cctx->ctx_lnum;
4155 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4156 [cctx->ctx_lnum]);
4157}
4158
4159/*
4160 * Compile a nested :def command.
4161 */
4162 static char_u *
4163compile_nested_function(exarg_T *eap, cctx_T *cctx)
4164{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004165 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004166 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004167 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004168 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004169 lvar_T *lvar;
4170 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004171 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004172
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004173 // Only g:Func() can use a namespace.
4174 if (name_start[1] == ':' && !is_global)
4175 {
4176 semsg(_(e_namespace), name_start);
4177 return NULL;
4178 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004179 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4180 return NULL;
4181
Bram Moolenaar04b12692020-05-04 23:24:44 +02004182 eap->arg = name_end;
4183 eap->getline = exarg_getline;
4184 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004185 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004186 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004187 lambda_name = get_lambda_name();
4188 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004189
Bram Moolenaar822ba242020-05-24 23:00:18 +02004190 if (ufunc == NULL)
4191 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004192 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004193 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004194 return NULL;
4195
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004196 if (is_global)
4197 {
4198 char_u *func_name = vim_strnsave(name_start + 2,
4199 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004200
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004201 if (func_name == NULL)
4202 r = FAIL;
4203 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004204 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004205 }
4206 else
4207 {
4208 // Define a local variable for the function reference.
4209 lvar = reserve_local(cctx, name_start, name_end - name_start,
4210 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004211 if (lvar == NULL)
4212 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004213 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004214 return NULL;
4215 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4216 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004217
Bram Moolenaar61a89812020-05-07 16:58:17 +02004218 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004219 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004220}
4221
4222/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 * Return the length of an assignment operator, or zero if there isn't one.
4224 */
4225 int
4226assignment_len(char_u *p, int *heredoc)
4227{
4228 if (*p == '=')
4229 {
4230 if (p[1] == '<' && p[2] == '<')
4231 {
4232 *heredoc = TRUE;
4233 return 3;
4234 }
4235 return 1;
4236 }
4237 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4238 return 2;
4239 if (STRNCMP(p, "..=", 3) == 0)
4240 return 3;
4241 return 0;
4242}
4243
4244// words that cannot be used as a variable
4245static char *reserved[] = {
4246 "true",
4247 "false",
4248 NULL
4249};
4250
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004251typedef enum {
4252 dest_local,
4253 dest_option,
4254 dest_env,
4255 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004256 dest_buffer,
4257 dest_window,
4258 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004259 dest_vimvar,
4260 dest_script,
4261 dest_reg,
4262} assign_dest_T;
4263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004265 * Generate the load instruction for "name".
4266 */
4267 static void
4268generate_loadvar(
4269 cctx_T *cctx,
4270 assign_dest_T dest,
4271 char_u *name,
4272 lvar_T *lvar,
4273 type_T *type)
4274{
4275 switch (dest)
4276 {
4277 case dest_option:
4278 // TODO: check the option exists
4279 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4280 break;
4281 case dest_global:
4282 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4283 break;
4284 case dest_buffer:
4285 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4286 break;
4287 case dest_window:
4288 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4289 break;
4290 case dest_tab:
4291 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4292 break;
4293 case dest_script:
4294 compile_load_scriptvar(cctx,
4295 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4296 break;
4297 case dest_env:
4298 // Include $ in the name here
4299 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4300 break;
4301 case dest_reg:
4302 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4303 break;
4304 case dest_vimvar:
4305 generate_LOADV(cctx, name + 2, TRUE);
4306 break;
4307 case dest_local:
4308 if (lvar->lv_from_outer)
4309 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4310 NULL, type);
4311 else
4312 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4313 break;
4314 }
4315}
4316
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004317 void
4318vim9_declare_error(char_u *name)
4319{
4320 char *scope = "";
4321
4322 switch (*name)
4323 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004324 case 'g': scope = _("global"); break;
4325 case 'b': scope = _("buffer"); break;
4326 case 'w': scope = _("window"); break;
4327 case 't': scope = _("tab"); break;
4328 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004329 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004330 return;
4331 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4332 return;
4333 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4334 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004335 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004336 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004337 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004338}
4339
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004340/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004341 * Compile declaration and assignment:
4342 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004344 * Return NULL for an error.
4345 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 */
4347 static char_u *
4348compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4349{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004350 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004352 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 char_u *ret = NULL;
4354 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004355 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004356 int scriptvar_sid = 0;
4357 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004360 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 int oplen = 0;
4363 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004364 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004365 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004366 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004370 // Skip over the "var" or "[var, var]" to get to any "=".
4371 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4372 if (p == NULL)
4373 return *arg == '[' ? arg : NULL;
4374
4375 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004377 // TODO: should we allow this, and figure out type inference from list
4378 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004379 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 return NULL;
4381 }
4382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 sp = p;
4384 p = skipwhite(p);
4385 op = p;
4386 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004387
4388 if (var_count > 0 && oplen == 0)
4389 // can be something like "[1, 2]->func()"
4390 return arg;
4391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4393 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004394 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004395 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004396 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 if (heredoc)
4399 {
4400 list_T *l;
4401 listitem_T *li;
4402
4403 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004404 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004406 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407
4408 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004409 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 {
4411 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4412 li->li_tv.vval.v_string = NULL;
4413 }
4414 generate_NEWLIST(cctx, l->lv_len);
4415 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004416 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 list_free(l);
4418 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004419 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004421 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004423 // for "[var, var] = expr" evaluate the expression here, loop over the
4424 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004425
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004426 p = skipwhite(op + oplen);
4427 if (compile_expr0(&p, cctx) == FAIL)
4428 return NULL;
4429 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004431 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004433 type_T *stacktype;
4434
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004435 stacktype = stack->ga_len == 0 ? &t_void
4436 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004437 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004439 emsg(_(e_cannot_use_void));
4440 goto theend;
4441 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004442 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004443 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004444 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004445 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4446 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004447 }
4448 }
4449
4450 /*
4451 * Loop over variables in "[var, var] = expr".
4452 * For "var = expr" and "let var: type" this is done only once.
4453 */
4454 if (var_count > 0)
4455 var_start = skipwhite(arg + 1); // skip over the "["
4456 else
4457 var_start = arg;
4458 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4459 {
4460 char_u *var_end = skip_var_one(var_start, FALSE);
4461 size_t varlen;
4462 int new_local = FALSE;
4463 int opt_type;
4464 int opt_flags = 0;
4465 assign_dest_T dest = dest_local;
4466 int vimvaridx = -1;
4467 lvar_T *lvar = NULL;
4468 lvar_T arg_lvar;
4469 int has_type = FALSE;
4470 int has_index = FALSE;
4471 int instr_count = -1;
4472
Bram Moolenaar65821722020-08-02 18:58:54 +02004473 if (*var_start == '@')
4474 p = var_start + 2;
4475 else
4476 {
4477 p = (*var_start == '&' || *var_start == '$')
4478 ? var_start + 1 : var_start;
4479 p = to_name_end(p, TRUE);
4480 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004481
4482 // "a: type" is declaring variable "a" with a type, not "a:".
4483 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4484 --var_end;
4485 if (is_decl && p == var_start + 2 && p[-1] == ':')
4486 --p;
4487
4488 varlen = p - var_start;
4489 vim_free(name);
4490 name = vim_strnsave(var_start, varlen);
4491 if (name == NULL)
4492 return NULL;
4493 if (!heredoc)
4494 type = &t_any;
4495
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004496 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004497 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004498 int declare_error = FALSE;
4499
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004500 if (*var_start == '&')
4501 {
4502 int cc;
4503 long numval;
4504
4505 dest = dest_option;
4506 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004508 emsg(_(e_const_option));
4509 goto theend;
4510 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004511 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004512 p = var_start;
4513 p = find_option_end(&p, &opt_flags);
4514 if (p == NULL)
4515 {
4516 // cannot happen?
4517 emsg(_(e_letunexp));
4518 goto theend;
4519 }
4520 cc = *p;
4521 *p = NUL;
4522 opt_type = get_option_value(var_start + 1, &numval,
4523 NULL, opt_flags);
4524 *p = cc;
4525 if (opt_type == -3)
4526 {
4527 semsg(_(e_unknown_option), var_start);
4528 goto theend;
4529 }
4530 if (opt_type == -2 || opt_type == 0)
4531 type = &t_string;
4532 else
4533 type = &t_number; // both number and boolean option
4534 }
4535 else if (*var_start == '$')
4536 {
4537 dest = dest_env;
4538 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004539 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004540 }
4541 else if (*var_start == '@')
4542 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004543 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 {
4545 emsg_invreg(var_start[1]);
4546 goto theend;
4547 }
4548 dest = dest_reg;
4549 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004550 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004551 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004552 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004553 {
4554 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004555 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004556 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004557 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 {
4559 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004560 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004561 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004562 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004563 {
4564 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004565 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004566 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004567 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004568 {
4569 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004570 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004571 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004572 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004573 {
4574 typval_T *vtv;
4575 int di_flags;
4576
4577 vimvaridx = find_vim_var(name + 2, &di_flags);
4578 if (vimvaridx < 0)
4579 {
4580 semsg(_(e_var_notfound), var_start);
4581 goto theend;
4582 }
4583 // We use the current value of "sandbox" here, is that OK?
4584 if (var_check_ro(di_flags, name, FALSE))
4585 goto theend;
4586 dest = dest_vimvar;
4587 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004588 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004589 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004590 }
4591 else
4592 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004593 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004594
4595 for (idx = 0; reserved[idx] != NULL; ++idx)
4596 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004597 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004599 goto theend;
4600 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601
4602 lvar = lookup_local(var_start, varlen, cctx);
4603 if (lvar == NULL)
4604 {
4605 CLEAR_FIELD(arg_lvar);
4606 if (lookup_arg(var_start, varlen,
4607 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4608 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004609 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004610 if (is_decl)
4611 {
4612 semsg(_(e_used_as_arg), name);
4613 goto theend;
4614 }
4615 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004616 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004617 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004618 if (lvar != NULL)
4619 {
4620 if (is_decl)
4621 {
4622 semsg(_("E1017: Variable already declared: %s"), name);
4623 goto theend;
4624 }
4625 else if (lvar->lv_const)
4626 {
4627 semsg(_("E1018: Cannot assign to a constant: %s"),
4628 name);
4629 goto theend;
4630 }
4631 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004632 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004633 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004634 int script_namespace = varlen > 1
4635 && STRNCMP(var_start, "s:", 2) == 0;
4636 int script_var = (script_namespace
4637 ? lookup_script(var_start + 2, varlen - 2)
4638 : lookup_script(var_start, varlen)) == OK;
4639 imported_T *import =
4640 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004641
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004642 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004644 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4645
4646 if (is_decl)
4647 {
4648 if (script_namespace)
4649 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004650 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004651 else
4652 semsg(_("E1054: Variable already declared in the script: %s"),
4653 name);
4654 goto theend;
4655 }
4656 else if (cctx->ctx_ufunc->uf_script_ctx_version
4657 == SCRIPT_VERSION_VIM9
4658 && script_namespace
4659 && !script_var && import == NULL)
4660 {
4661 semsg(_(e_unknown_var), name);
4662 goto theend;
4663 }
4664
4665 dest = dest_script;
4666
4667 // existing script-local variables should have a type
4668 scriptvar_sid = current_sctx.sc_sid;
4669 if (import != NULL)
4670 scriptvar_sid = import->imp_sid;
4671 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4672 rawname, TRUE);
4673 if (scriptvar_idx >= 0)
4674 {
4675 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4676 svar_T *sv =
4677 ((svar_T *)si->sn_var_vals.ga_data)
4678 + scriptvar_idx;
4679 type = sv->sv_type;
4680 }
4681 }
4682 else if (name[1] == ':' && name[2] != NUL)
4683 {
4684 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004685 name);
4686 goto theend;
4687 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004688 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004689 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004690 semsg(_(e_unknown_var), name);
4691 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004692 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004693 else if (check_defined(var_start, varlen, cctx) == FAIL)
4694 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004695 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004697
4698 if (declare_error)
4699 {
4700 vim9_declare_error(name);
4701 goto theend;
4702 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004703 }
4704
4705 // handle "a:name" as a name, not index "name" on "a"
4706 if (varlen > 1 || var_start[varlen] != ':')
4707 p = var_end;
4708
4709 if (dest != dest_option)
4710 {
4711 if (is_decl && *p == ':')
4712 {
4713 // parse optional type: "let var: type = expr"
4714 if (!VIM_ISWHITE(p[1]))
4715 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004716 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 goto theend;
4718 }
4719 p = skipwhite(p + 1);
4720 type = parse_type(&p, cctx->ctx_type_list);
4721 has_type = TRUE;
4722 }
4723 else if (lvar != NULL)
4724 type = lvar->lv_type;
4725 }
4726
4727 if (oplen == 3 && !heredoc && dest != dest_global
4728 && type->tt_type != VAR_STRING
4729 && type->tt_type != VAR_ANY)
4730 {
4731 emsg(_("E1019: Can only concatenate to string"));
4732 goto theend;
4733 }
4734
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004735 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004736 {
4737 if (oplen > 1 && !heredoc)
4738 {
4739 // +=, /=, etc. require an existing variable
4740 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4741 name);
4742 goto theend;
4743 }
4744
4745 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004746 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4747 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004748 goto theend;
4749 lvar = reserve_local(cctx, var_start, varlen,
4750 cmdidx == CMD_const, type);
4751 if (lvar == NULL)
4752 goto theend;
4753 new_local = TRUE;
4754 }
4755
4756 member_type = type;
4757 if (var_end > var_start + varlen)
4758 {
4759 // Something follows after the variable: "var[idx]".
4760 if (is_decl)
4761 {
4762 emsg(_("E1087: cannot use an index when declaring a variable"));
4763 goto theend;
4764 }
4765
4766 if (var_start[varlen] == '[')
4767 {
4768 has_index = TRUE;
4769 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004770 member_type = &t_any;
4771 else
4772 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773 }
4774 else
4775 {
4776 semsg("Not supported yet: %s", var_start);
4777 goto theend;
4778 }
4779 }
4780 else if (lvar == &arg_lvar)
4781 {
4782 semsg(_("E1090: Cannot assign to argument %s"), name);
4783 goto theend;
4784 }
4785
4786 if (!heredoc)
4787 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004788 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004789 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004790 if (oplen > 0 && var_count == 0)
4791 {
4792 // skip over the "=" and the expression
4793 p = skipwhite(op + oplen);
4794 compile_expr0(&p, cctx);
4795 }
4796 }
4797 else if (oplen > 0)
4798 {
4799 type_T *stacktype;
4800
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 // For "var = expr" evaluate the expression.
4802 if (var_count == 0)
4803 {
4804 int r;
4805
4806 // for "+=", "*=", "..=" etc. first load the current value
4807 if (*op != '=')
4808 {
4809 generate_loadvar(cctx, dest, name, lvar, type);
4810
4811 if (has_index)
4812 {
4813 // TODO: get member from list or dict
4814 emsg("Index with operation not supported yet");
4815 goto theend;
4816 }
4817 }
4818
4819 // Compile the expression. Temporarily hide the new local
4820 // variable here, it is not available to this expression.
4821 if (new_local)
4822 --cctx->ctx_locals.ga_len;
4823 instr_count = instr->ga_len;
4824 p = skipwhite(op + oplen);
4825 r = compile_expr0(&p, cctx);
4826 if (new_local)
4827 ++cctx->ctx_locals.ga_len;
4828 if (r == FAIL)
4829 goto theend;
4830 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004831 else if (semicolon && var_idx == var_count - 1)
4832 {
4833 // For "[var; var] = expr" get the rest of the list
4834 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4835 goto theend;
4836 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004837 else
4838 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004839 // For "[var, var] = expr" get the "var_idx" item from the
4840 // list.
4841 if (generate_GETITEM(cctx, var_idx) == FAIL)
4842 return FAIL;
4843 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004844
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004845 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004846 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004847 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004849 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004850 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004851 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004852 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004853 emsg(_(e_cannot_use_void));
4854 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004856 else if ((stacktype->tt_type == VAR_FUNC
4857 || stacktype->tt_type == VAR_PARTIAL)
4858 && var_wrong_func_name(name, TRUE))
4859 {
4860 goto theend;
4861 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 else
4863 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004864 // An empty list or dict has a &t_void member,
4865 // for a variable that implies &t_any.
4866 if (stacktype == &t_list_empty)
4867 lvar->lv_type = &t_list_any;
4868 else if (stacktype == &t_dict_empty)
4869 lvar->lv_type = &t_dict_any;
4870 else
4871 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004873 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004874 else
4875 {
4876 type_T *use_type = lvar->lv_type;
4877
4878 if (has_index)
4879 {
4880 use_type = use_type->tt_member;
4881 if (use_type == NULL)
4882 use_type = &t_void;
4883 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004884 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004885 == FAIL)
4886 goto theend;
4887 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004888 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004889 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004890 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004893 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004895 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 goto theend;
4897 }
4898 else if (!has_type || dest == dest_option)
4899 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004900 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 goto theend;
4902 }
4903 else
4904 {
4905 // variables are always initialized
4906 if (ga_grow(instr, 1) == FAIL)
4907 goto theend;
4908 switch (member_type->tt_type)
4909 {
4910 case VAR_BOOL:
4911 generate_PUSHBOOL(cctx, VVAL_FALSE);
4912 break;
4913 case VAR_FLOAT:
4914#ifdef FEAT_FLOAT
4915 generate_PUSHF(cctx, 0.0);
4916#endif
4917 break;
4918 case VAR_STRING:
4919 generate_PUSHS(cctx, NULL);
4920 break;
4921 case VAR_BLOB:
4922 generate_PUSHBLOB(cctx, NULL);
4923 break;
4924 case VAR_FUNC:
4925 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4926 break;
4927 case VAR_LIST:
4928 generate_NEWLIST(cctx, 0);
4929 break;
4930 case VAR_DICT:
4931 generate_NEWDICT(cctx, 0);
4932 break;
4933 case VAR_JOB:
4934 generate_PUSHJOB(cctx, NULL);
4935 break;
4936 case VAR_CHANNEL:
4937 generate_PUSHCHANNEL(cctx, NULL);
4938 break;
4939 case VAR_NUMBER:
4940 case VAR_UNKNOWN:
4941 case VAR_ANY:
4942 case VAR_PARTIAL:
4943 case VAR_VOID:
4944 case VAR_SPECIAL: // cannot happen
4945 generate_PUSHNR(cctx, 0);
4946 break;
4947 }
4948 }
4949 if (var_count == 0)
4950 end = p;
4951 }
4952
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004953 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004954 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004955 break;
4956
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 if (oplen > 0 && *op != '=')
4958 {
4959 type_T *expected = &t_number;
4960 type_T *stacktype;
4961
4962 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004963 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964
4965 if (*op == '.')
4966 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004967 else if (*op == '+')
4968 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004970 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 goto theend;
4972
4973 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004974 {
4975 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4976 goto theend;
4977 }
4978 else if (*op == '+')
4979 {
4980 if (generate_add_instr(cctx,
4981 operator_type(member_type, stacktype),
4982 member_type, stacktype) == FAIL)
4983 goto theend;
4984 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985 else
4986 {
4987 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4988
4989 if (isn == NULL)
4990 goto theend;
4991 switch (*op)
4992 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4994 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4995 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4996 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 }
4999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005002 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 int r;
5004
5005 // Compile the "idx" in "var[idx]".
5006 if (new_local)
5007 --cctx->ctx_locals.ga_len;
5008 p = skipwhite(var_start + varlen + 1);
5009 r = compile_expr0(&p, cctx);
5010 if (new_local)
5011 ++cctx->ctx_locals.ga_len;
5012 if (r == FAIL)
5013 goto theend;
5014 if (*skipwhite(p) != ']')
5015 {
5016 emsg(_(e_missbrac));
5017 goto theend;
5018 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005019 if (type == &t_any)
5020 {
5021 type_T *idx_type = ((type_T **)stack->ga_data)[
5022 stack->ga_len - 1];
5023 // Index on variable of unknown type: guess the type from the
5024 // index type: number is dict, otherwise dict.
5025 // TODO: should do the assignment at runtime
5026 if (idx_type->tt_type == VAR_NUMBER)
5027 type = &t_list_any;
5028 else
5029 type = &t_dict_any;
5030 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 if (type->tt_type == VAR_DICT
5032 && may_generate_2STRING(-1, cctx) == FAIL)
5033 goto theend;
5034 if (type->tt_type == VAR_LIST
5035 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005036 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 {
5038 emsg(_(e_number_exp));
5039 goto theend;
5040 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005041
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 // Load the dict or list. On the stack we then have:
5043 // - value
5044 // - index
5045 // - variable
5046 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005047
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 if (type->tt_type == VAR_LIST)
5049 {
5050 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5051 return FAIL;
5052 }
5053 else if (type->tt_type == VAR_DICT)
5054 {
5055 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5056 return FAIL;
5057 }
5058 else
5059 {
5060 emsg(_(e_listreq));
5061 goto theend;
5062 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005063 }
5064 else
5065 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 switch (dest)
5067 {
5068 case dest_option:
5069 generate_STOREOPT(cctx, name + 1, opt_flags);
5070 break;
5071 case dest_global:
5072 // include g: with the name, easier to execute that way
5073 generate_STORE(cctx, ISN_STOREG, 0, name);
5074 break;
5075 case dest_buffer:
5076 // include b: with the name, easier to execute that way
5077 generate_STORE(cctx, ISN_STOREB, 0, name);
5078 break;
5079 case dest_window:
5080 // include w: with the name, easier to execute that way
5081 generate_STORE(cctx, ISN_STOREW, 0, name);
5082 break;
5083 case dest_tab:
5084 // include t: with the name, easier to execute that way
5085 generate_STORE(cctx, ISN_STORET, 0, name);
5086 break;
5087 case dest_env:
5088 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5089 break;
5090 case dest_reg:
5091 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5092 break;
5093 case dest_vimvar:
5094 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5095 break;
5096 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005097 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005098 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 {
5100 char_u *name_s = name;
5101
5102 // Include s: in the name for store_var()
5103 if (name[1] != ':')
5104 {
5105 int len = (int)STRLEN(name) + 3;
5106
5107 name_s = alloc(len);
5108 if (name_s == NULL)
5109 name_s = name;
5110 else
5111 vim_snprintf((char *)name_s, len,
5112 "s:%s", name);
5113 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005114 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5115 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005116 if (name_s != name)
5117 vim_free(name_s);
5118 }
5119 else
5120 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005121 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005122 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005123 break;
5124 case dest_local:
5125 if (lvar != NULL)
5126 {
5127 isn_T *isn = ((isn_T *)instr->ga_data)
5128 + instr->ga_len - 1;
5129
5130 // optimization: turn "var = 123" from ISN_PUSHNR +
5131 // ISN_STORE into ISN_STORENR
5132 if (!lvar->lv_from_outer
5133 && instr->ga_len == instr_count + 1
5134 && isn->isn_type == ISN_PUSHNR)
5135 {
5136 varnumber_T val = isn->isn_arg.number;
5137
5138 isn->isn_type = ISN_STORENR;
5139 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5140 isn->isn_arg.storenr.stnr_val = val;
5141 if (stack->ga_len > 0)
5142 --stack->ga_len;
5143 }
5144 else if (lvar->lv_from_outer)
5145 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005146 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005147 else
5148 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5149 }
5150 break;
5151 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005152 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153
5154 if (var_idx + 1 < var_count)
5155 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157
5158 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005159 if (var_count > 0 && !semicolon)
5160 {
5161 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5162 goto theend;
5163 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164
Bram Moolenaarb2097502020-07-19 17:17:02 +02005165 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166
5167theend:
5168 vim_free(name);
5169 return ret;
5170}
5171
5172/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005173 * Check if "name" can be "unlet".
5174 */
5175 int
5176check_vim9_unlet(char_u *name)
5177{
5178 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5179 {
5180 semsg(_("E1081: Cannot unlet %s"), name);
5181 return FAIL;
5182 }
5183 return OK;
5184}
5185
5186/*
5187 * Callback passed to ex_unletlock().
5188 */
5189 static int
5190compile_unlet(
5191 lval_T *lvp,
5192 char_u *name_end,
5193 exarg_T *eap,
5194 int deep UNUSED,
5195 void *coookie)
5196{
5197 cctx_T *cctx = coookie;
5198
5199 if (lvp->ll_tv == NULL)
5200 {
5201 char_u *p = lvp->ll_name;
5202 int cc = *name_end;
5203 int ret = OK;
5204
5205 // Normal name. Only supports g:, w:, t: and b: namespaces.
5206 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005207 if (*p == '$')
5208 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5209 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005210 ret = FAIL;
5211 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005212 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005213
5214 *name_end = cc;
5215 return ret;
5216 }
5217
5218 // TODO: unlet {list}[idx]
5219 // TODO: unlet {dict}[key]
5220 emsg("Sorry, :unlet not fully implemented yet");
5221 return FAIL;
5222}
5223
5224/*
5225 * compile "unlet var", "lock var" and "unlock var"
5226 * "arg" points to "var".
5227 */
5228 static char_u *
5229compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5230{
5231 char_u *p = arg;
5232
5233 if (eap->cmdidx != CMD_unlet)
5234 {
5235 emsg("Sorry, :lock and unlock not implemented yet");
5236 return NULL;
5237 }
5238
5239 if (*p == '!')
5240 {
5241 p = skipwhite(p + 1);
5242 eap->forceit = TRUE;
5243 }
5244
5245 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5246 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5247}
5248
5249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 * Compile an :import command.
5251 */
5252 static char_u *
5253compile_import(char_u *arg, cctx_T *cctx)
5254{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005255 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256}
5257
5258/*
5259 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5260 */
5261 static int
5262compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5263{
5264 garray_T *instr = &cctx->ctx_instr;
5265 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5266
5267 if (endlabel == NULL)
5268 return FAIL;
5269 endlabel->el_next = *el;
5270 *el = endlabel;
5271 endlabel->el_end_label = instr->ga_len;
5272
5273 generate_JUMP(cctx, when, 0);
5274 return OK;
5275}
5276
5277 static void
5278compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5279{
5280 garray_T *instr = &cctx->ctx_instr;
5281
5282 while (*el != NULL)
5283 {
5284 endlabel_T *cur = (*el);
5285 isn_T *isn;
5286
5287 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5288 isn->isn_arg.jump.jump_where = instr->ga_len;
5289 *el = cur->el_next;
5290 vim_free(cur);
5291 }
5292}
5293
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005294 static void
5295compile_free_jump_to_end(endlabel_T **el)
5296{
5297 while (*el != NULL)
5298 {
5299 endlabel_T *cur = (*el);
5300
5301 *el = cur->el_next;
5302 vim_free(cur);
5303 }
5304}
5305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306/*
5307 * Create a new scope and set up the generic items.
5308 */
5309 static scope_T *
5310new_scope(cctx_T *cctx, scopetype_T type)
5311{
5312 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5313
5314 if (scope == NULL)
5315 return NULL;
5316 scope->se_outer = cctx->ctx_scope;
5317 cctx->ctx_scope = scope;
5318 scope->se_type = type;
5319 scope->se_local_count = cctx->ctx_locals.ga_len;
5320 return scope;
5321}
5322
5323/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005324 * Free the current scope and go back to the outer scope.
5325 */
5326 static void
5327drop_scope(cctx_T *cctx)
5328{
5329 scope_T *scope = cctx->ctx_scope;
5330
5331 if (scope == NULL)
5332 {
5333 iemsg("calling drop_scope() without a scope");
5334 return;
5335 }
5336 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005337 switch (scope->se_type)
5338 {
5339 case IF_SCOPE:
5340 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5341 case FOR_SCOPE:
5342 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5343 case WHILE_SCOPE:
5344 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5345 case TRY_SCOPE:
5346 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5347 case NO_SCOPE:
5348 case BLOCK_SCOPE:
5349 break;
5350 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005351 vim_free(scope);
5352}
5353
5354/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 * compile "if expr"
5356 *
5357 * "if expr" Produces instructions:
5358 * EVAL expr Push result of "expr"
5359 * JUMP_IF_FALSE end
5360 * ... body ...
5361 * end:
5362 *
5363 * "if expr | else" Produces instructions:
5364 * EVAL expr Push result of "expr"
5365 * JUMP_IF_FALSE else
5366 * ... body ...
5367 * JUMP_ALWAYS end
5368 * else:
5369 * ... body ...
5370 * end:
5371 *
5372 * "if expr1 | elseif expr2 | else" Produces instructions:
5373 * EVAL expr Push result of "expr"
5374 * JUMP_IF_FALSE elseif
5375 * ... body ...
5376 * JUMP_ALWAYS end
5377 * elseif:
5378 * EVAL expr Push result of "expr"
5379 * JUMP_IF_FALSE else
5380 * ... body ...
5381 * JUMP_ALWAYS end
5382 * else:
5383 * ... body ...
5384 * end:
5385 */
5386 static char_u *
5387compile_if(char_u *arg, cctx_T *cctx)
5388{
5389 char_u *p = arg;
5390 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005391 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005393 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005394 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395
Bram Moolenaara5565e42020-05-09 15:44:01 +02005396 CLEAR_FIELD(ppconst);
5397 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005398 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 clear_ppconst(&ppconst);
5400 return NULL;
5401 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005402 if (cctx->ctx_skip == SKIP_YES)
5403 clear_ppconst(&ppconst);
5404 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005405 {
5406 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005407 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005408 clear_ppconst(&ppconst);
5409 }
5410 else
5411 {
5412 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005413 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005414 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005415 return NULL;
5416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417
5418 scope = new_scope(cctx, IF_SCOPE);
5419 if (scope == NULL)
5420 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005421 scope->se_skip_save = skip_save;
5422 // "is_had_return" will be reset if any block does not end in :return
5423 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005425 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005426 {
5427 // "where" is set when ":elseif", "else" or ":endif" is found
5428 scope->se_u.se_if.is_if_label = instr->ga_len;
5429 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5430 }
5431 else
5432 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433
5434 return p;
5435}
5436
5437 static char_u *
5438compile_elseif(char_u *arg, cctx_T *cctx)
5439{
5440 char_u *p = arg;
5441 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005442 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 isn_T *isn;
5444 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005445 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446
5447 if (scope == NULL || scope->se_type != IF_SCOPE)
5448 {
5449 emsg(_(e_elseif_without_if));
5450 return NULL;
5451 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005452 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005453 if (!cctx->ctx_had_return)
5454 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005456 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005457 {
5458 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005460 return NULL;
5461 // previous "if" or "elseif" jumps here
5462 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5463 isn->isn_arg.jump.jump_where = instr->ga_len;
5464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005466 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005467 CLEAR_FIELD(ppconst);
5468 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005469 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005470 clear_ppconst(&ppconst);
5471 return NULL;
5472 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005473 if (scope->se_skip_save == SKIP_YES)
5474 clear_ppconst(&ppconst);
5475 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005476 {
5477 // The expression results in a constant.
5478 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005479 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005480 clear_ppconst(&ppconst);
5481 scope->se_u.se_if.is_if_label = -1;
5482 }
5483 else
5484 {
5485 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005486 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005487 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005488 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005490 // "where" is set when ":elseif", "else" or ":endif" is found
5491 scope->se_u.se_if.is_if_label = instr->ga_len;
5492 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494
5495 return p;
5496}
5497
5498 static char_u *
5499compile_else(char_u *arg, cctx_T *cctx)
5500{
5501 char_u *p = arg;
5502 garray_T *instr = &cctx->ctx_instr;
5503 isn_T *isn;
5504 scope_T *scope = cctx->ctx_scope;
5505
5506 if (scope == NULL || scope->se_type != IF_SCOPE)
5507 {
5508 emsg(_(e_else_without_if));
5509 return NULL;
5510 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005511 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005512 if (!cctx->ctx_had_return)
5513 scope->se_u.se_if.is_had_return = FALSE;
5514 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
Bram Moolenaarefd88552020-06-18 20:50:10 +02005516 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005517 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005518 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005519 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005520 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005521 if (!cctx->ctx_had_return
5522 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5523 JUMP_ALWAYS, cctx) == FAIL)
5524 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005525 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005526
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005527 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005528 {
5529 if (scope->se_u.se_if.is_if_label >= 0)
5530 {
5531 // previous "if" or "elseif" jumps here
5532 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5533 isn->isn_arg.jump.jump_where = instr->ga_len;
5534 scope->se_u.se_if.is_if_label = -1;
5535 }
5536 }
5537
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005538 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005539 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
5542 return p;
5543}
5544
5545 static char_u *
5546compile_endif(char_u *arg, cctx_T *cctx)
5547{
5548 scope_T *scope = cctx->ctx_scope;
5549 ifscope_T *ifscope;
5550 garray_T *instr = &cctx->ctx_instr;
5551 isn_T *isn;
5552
5553 if (scope == NULL || scope->se_type != IF_SCOPE)
5554 {
5555 emsg(_(e_endif_without_if));
5556 return NULL;
5557 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005558 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005559 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005560 if (!cctx->ctx_had_return)
5561 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005563 if (scope->se_u.se_if.is_if_label >= 0)
5564 {
5565 // previous "if" or "elseif" jumps here
5566 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5567 isn->isn_arg.jump.jump_where = instr->ga_len;
5568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 // Fill in the "end" label in jumps at the end of the blocks.
5570 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005571 cctx->ctx_skip = scope->se_skip_save;
5572
5573 // If all the blocks end in :return and there is an :else then the
5574 // had_return flag is set.
5575 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005577 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 return arg;
5579}
5580
5581/*
5582 * compile "for var in expr"
5583 *
5584 * Produces instructions:
5585 * PUSHNR -1
5586 * STORE loop-idx Set index to -1
5587 * EVAL expr Push result of "expr"
5588 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5589 * - if beyond end, jump to "end"
5590 * - otherwise get item from list and push it
5591 * STORE var Store item in "var"
5592 * ... body ...
5593 * JUMP top Jump back to repeat
5594 * end: DROP Drop the result of "expr"
5595 *
5596 */
5597 static char_u *
5598compile_for(char_u *arg, cctx_T *cctx)
5599{
5600 char_u *p;
5601 size_t varlen;
5602 garray_T *instr = &cctx->ctx_instr;
5603 garray_T *stack = &cctx->ctx_type_stack;
5604 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005605 lvar_T *loop_lvar; // loop iteration variable
5606 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 type_T *vartype;
5608
5609 // TODO: list of variables: "for [key, value] in dict"
5610 // parse "var"
5611 for (p = arg; eval_isnamec1(*p); ++p)
5612 ;
5613 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005614 var_lvar = lookup_local(arg, varlen, cctx);
5615 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 {
5617 semsg(_("E1023: variable already defined: %s"), arg);
5618 return NULL;
5619 }
5620
5621 // consume "in"
5622 p = skipwhite(p);
5623 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5624 {
5625 emsg(_(e_missing_in));
5626 return NULL;
5627 }
5628 p = skipwhite(p + 2);
5629
5630
5631 scope = new_scope(cctx, FOR_SCOPE);
5632 if (scope == NULL)
5633 return NULL;
5634
5635 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005636 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5637 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005638 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005639 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005640 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643
5644 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005645 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5646 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005647 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005648 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005649 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005653 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 // compile "expr", it remains on the stack until "endfor"
5656 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005657 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005658 {
5659 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005662
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005663 // Now that we know the type of "var", check that it is a list, now or at
5664 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005666 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005668 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669 return NULL;
5670 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005671 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005672 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673
5674 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005675 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005677 generate_FOR(cctx, loop_lvar->lv_idx);
5678 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005679
5680 return arg;
5681}
5682
5683/*
5684 * compile "endfor"
5685 */
5686 static char_u *
5687compile_endfor(char_u *arg, cctx_T *cctx)
5688{
5689 garray_T *instr = &cctx->ctx_instr;
5690 scope_T *scope = cctx->ctx_scope;
5691 forscope_T *forscope;
5692 isn_T *isn;
5693
5694 if (scope == NULL || scope->se_type != FOR_SCOPE)
5695 {
5696 emsg(_(e_for));
5697 return NULL;
5698 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005699 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005701 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702
5703 // At end of ":for" scope jump back to the FOR instruction.
5704 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5705
5706 // Fill in the "end" label in the FOR statement so it can jump here
5707 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5708 isn->isn_arg.forloop.for_end = instr->ga_len;
5709
5710 // Fill in the "end" label any BREAK statements
5711 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5712
5713 // Below the ":for" scope drop the "expr" list from the stack.
5714 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5715 return NULL;
5716
5717 vim_free(scope);
5718
5719 return arg;
5720}
5721
5722/*
5723 * compile "while expr"
5724 *
5725 * Produces instructions:
5726 * top: EVAL expr Push result of "expr"
5727 * JUMP_IF_FALSE end jump if false
5728 * ... body ...
5729 * JUMP top Jump back to repeat
5730 * end:
5731 *
5732 */
5733 static char_u *
5734compile_while(char_u *arg, cctx_T *cctx)
5735{
5736 char_u *p = arg;
5737 garray_T *instr = &cctx->ctx_instr;
5738 scope_T *scope;
5739
5740 scope = new_scope(cctx, WHILE_SCOPE);
5741 if (scope == NULL)
5742 return NULL;
5743
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005744 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005745
5746 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005747 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748 return NULL;
5749
5750 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005751 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752 JUMP_IF_FALSE, cctx) == FAIL)
5753 return FAIL;
5754
5755 return p;
5756}
5757
5758/*
5759 * compile "endwhile"
5760 */
5761 static char_u *
5762compile_endwhile(char_u *arg, cctx_T *cctx)
5763{
5764 scope_T *scope = cctx->ctx_scope;
5765
5766 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5767 {
5768 emsg(_(e_while));
5769 return NULL;
5770 }
5771 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005772 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773
5774 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005775 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776
5777 // Fill in the "end" label in the WHILE statement so it can jump here.
5778 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005779 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780
5781 vim_free(scope);
5782
5783 return arg;
5784}
5785
5786/*
5787 * compile "continue"
5788 */
5789 static char_u *
5790compile_continue(char_u *arg, cctx_T *cctx)
5791{
5792 scope_T *scope = cctx->ctx_scope;
5793
5794 for (;;)
5795 {
5796 if (scope == NULL)
5797 {
5798 emsg(_(e_continue));
5799 return NULL;
5800 }
5801 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5802 break;
5803 scope = scope->se_outer;
5804 }
5805
5806 // Jump back to the FOR or WHILE instruction.
5807 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005808 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5809 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810 return arg;
5811}
5812
5813/*
5814 * compile "break"
5815 */
5816 static char_u *
5817compile_break(char_u *arg, cctx_T *cctx)
5818{
5819 scope_T *scope = cctx->ctx_scope;
5820 endlabel_T **el;
5821
5822 for (;;)
5823 {
5824 if (scope == NULL)
5825 {
5826 emsg(_(e_break));
5827 return NULL;
5828 }
5829 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5830 break;
5831 scope = scope->se_outer;
5832 }
5833
5834 // Jump to the end of the FOR or WHILE loop.
5835 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005836 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005838 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5840 return FAIL;
5841
5842 return arg;
5843}
5844
5845/*
5846 * compile "{" start of block
5847 */
5848 static char_u *
5849compile_block(char_u *arg, cctx_T *cctx)
5850{
5851 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5852 return NULL;
5853 return skipwhite(arg + 1);
5854}
5855
5856/*
5857 * compile end of block: drop one scope
5858 */
5859 static void
5860compile_endblock(cctx_T *cctx)
5861{
5862 scope_T *scope = cctx->ctx_scope;
5863
5864 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005865 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 vim_free(scope);
5867}
5868
5869/*
5870 * compile "try"
5871 * Creates a new scope for the try-endtry, pointing to the first catch and
5872 * finally.
5873 * Creates another scope for the "try" block itself.
5874 * TRY instruction sets up exception handling at runtime.
5875 *
5876 * "try"
5877 * TRY -> catch1, -> finally push trystack entry
5878 * ... try block
5879 * "throw {exception}"
5880 * EVAL {exception}
5881 * THROW create exception
5882 * ... try block
5883 * " catch {expr}"
5884 * JUMP -> finally
5885 * catch1: PUSH exeception
5886 * EVAL {expr}
5887 * MATCH
5888 * JUMP nomatch -> catch2
5889 * CATCH remove exception
5890 * ... catch block
5891 * " catch"
5892 * JUMP -> finally
5893 * catch2: CATCH remove exception
5894 * ... catch block
5895 * " finally"
5896 * finally:
5897 * ... finally block
5898 * " endtry"
5899 * ENDTRY pop trystack entry, may rethrow
5900 */
5901 static char_u *
5902compile_try(char_u *arg, cctx_T *cctx)
5903{
5904 garray_T *instr = &cctx->ctx_instr;
5905 scope_T *try_scope;
5906 scope_T *scope;
5907
5908 // scope that holds the jumps that go to catch/finally/endtry
5909 try_scope = new_scope(cctx, TRY_SCOPE);
5910 if (try_scope == NULL)
5911 return NULL;
5912
5913 // "catch" is set when the first ":catch" is found.
5914 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005915 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 if (generate_instr(cctx, ISN_TRY) == NULL)
5917 return NULL;
5918
5919 // scope for the try block itself
5920 scope = new_scope(cctx, BLOCK_SCOPE);
5921 if (scope == NULL)
5922 return NULL;
5923
5924 return arg;
5925}
5926
5927/*
5928 * compile "catch {expr}"
5929 */
5930 static char_u *
5931compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5932{
5933 scope_T *scope = cctx->ctx_scope;
5934 garray_T *instr = &cctx->ctx_instr;
5935 char_u *p;
5936 isn_T *isn;
5937
5938 // end block scope from :try or :catch
5939 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5940 compile_endblock(cctx);
5941 scope = cctx->ctx_scope;
5942
5943 // Error if not in a :try scope
5944 if (scope == NULL || scope->se_type != TRY_SCOPE)
5945 {
5946 emsg(_(e_catch));
5947 return NULL;
5948 }
5949
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005950 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 {
5952 emsg(_("E1033: catch unreachable after catch-all"));
5953 return NULL;
5954 }
5955
5956 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005957 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 JUMP_ALWAYS, cctx) == FAIL)
5959 return NULL;
5960
5961 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005962 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 if (isn->isn_arg.try.try_catch == 0)
5964 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005965 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 {
5967 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005968 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 isn->isn_arg.jump.jump_where = instr->ga_len;
5970 }
5971
5972 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005973 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005975 scope->se_u.se_try.ts_caught_all = TRUE;
5976 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 }
5978 else
5979 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005980 char_u *end;
5981 char_u *pat;
5982 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005983 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005984 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 // Push v:exception, push {expr} and MATCH
5987 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5988
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005989 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005990 if (*end != *p)
5991 {
5992 semsg(_("E1067: Separator mismatch: %s"), p);
5993 vim_free(tofree);
5994 return FAIL;
5995 }
5996 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005997 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005998 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005999 len = (int)(end - tofree);
6000 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006001 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006002 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006003 if (pat == NULL)
6004 return FAIL;
6005 if (generate_PUSHS(cctx, pat) == FAIL)
6006 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6009 return NULL;
6010
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006011 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6013 return NULL;
6014 }
6015
6016 if (generate_instr(cctx, ISN_CATCH) == NULL)
6017 return NULL;
6018
6019 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6020 return NULL;
6021 return p;
6022}
6023
6024 static char_u *
6025compile_finally(char_u *arg, cctx_T *cctx)
6026{
6027 scope_T *scope = cctx->ctx_scope;
6028 garray_T *instr = &cctx->ctx_instr;
6029 isn_T *isn;
6030
6031 // end block scope from :try or :catch
6032 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6033 compile_endblock(cctx);
6034 scope = cctx->ctx_scope;
6035
6036 // Error if not in a :try scope
6037 if (scope == NULL || scope->se_type != TRY_SCOPE)
6038 {
6039 emsg(_(e_finally));
6040 return NULL;
6041 }
6042
6043 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006044 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 if (isn->isn_arg.try.try_finally != 0)
6046 {
6047 emsg(_(e_finally_dup));
6048 return NULL;
6049 }
6050
6051 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006052 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053
Bram Moolenaar585fea72020-04-02 22:33:21 +02006054 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006055 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 {
6057 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006058 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006060 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 }
6062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 // TODO: set index in ts_finally_label jumps
6064
6065 return arg;
6066}
6067
6068 static char_u *
6069compile_endtry(char_u *arg, cctx_T *cctx)
6070{
6071 scope_T *scope = cctx->ctx_scope;
6072 garray_T *instr = &cctx->ctx_instr;
6073 isn_T *isn;
6074
6075 // end block scope from :catch or :finally
6076 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6077 compile_endblock(cctx);
6078 scope = cctx->ctx_scope;
6079
6080 // Error if not in a :try scope
6081 if (scope == NULL || scope->se_type != TRY_SCOPE)
6082 {
6083 if (scope == NULL)
6084 emsg(_(e_no_endtry));
6085 else if (scope->se_type == WHILE_SCOPE)
6086 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006087 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 emsg(_(e_endfor));
6089 else
6090 emsg(_(e_endif));
6091 return NULL;
6092 }
6093
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006094 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6096 {
6097 emsg(_("E1032: missing :catch or :finally"));
6098 return NULL;
6099 }
6100
6101 // Fill in the "end" label in jumps at the end of the blocks, if not done
6102 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006103 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104
6105 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006106 if (isn->isn_arg.try.try_catch == 0)
6107 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 if (isn->isn_arg.try.try_finally == 0)
6109 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006110
6111 if (scope->se_u.se_try.ts_catch_label != 0)
6112 {
6113 // Last catch without match jumps here
6114 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6115 isn->isn_arg.jump.jump_where = instr->ga_len;
6116 }
6117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 compile_endblock(cctx);
6119
6120 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6121 return NULL;
6122 return arg;
6123}
6124
6125/*
6126 * compile "throw {expr}"
6127 */
6128 static char_u *
6129compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6130{
6131 char_u *p = skipwhite(arg);
6132
Bram Moolenaara5565e42020-05-09 15:44:01 +02006133 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 return NULL;
6135 if (may_generate_2STRING(-1, cctx) == FAIL)
6136 return NULL;
6137 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6138 return NULL;
6139
6140 return p;
6141}
6142
6143/*
6144 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006145 * compile "echomsg expr"
6146 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006147 * compile "execute expr"
6148 */
6149 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006150compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006151{
6152 char_u *p = arg;
6153 int count = 0;
6154
6155 for (;;)
6156 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006157 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006158 return NULL;
6159 ++count;
6160 p = skipwhite(p);
6161 if (ends_excmd(*p))
6162 break;
6163 }
6164
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006165 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6166 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6167 else if (cmdidx == CMD_execute)
6168 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6169 else if (cmdidx == CMD_echomsg)
6170 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6171 else
6172 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 return p;
6174}
6175
6176/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006177 * A command that is not compiled, execute with legacy code.
6178 */
6179 static char_u *
6180compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6181{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006182 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006183 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006184 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006185
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006186 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006187 goto theend;
6188
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006189 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006190 {
6191 long argt = excmd_get_argt(eap->cmdidx);
6192 int usefilter = FALSE;
6193
6194 has_expr = argt & (EX_XFILE | EX_EXPAND);
6195
6196 // If the command can be followed by a bar, find the bar and truncate
6197 // it, so that the following command can be compiled.
6198 // The '|' is overwritten with a NUL, it is put back below.
6199 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6200 && *eap->arg == '!')
6201 // :w !filter or :r !filter or :r! filter
6202 usefilter = TRUE;
6203 if ((argt & EX_TRLBAR) && !usefilter)
6204 {
6205 separate_nextcmd(eap);
6206 if (eap->nextcmd != NULL)
6207 nextcmd = eap->nextcmd;
6208 }
6209 }
6210
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006211 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6212 {
6213 // expand filename in "syntax include [@group] filename"
6214 has_expr = TRUE;
6215 eap->arg = skipwhite(eap->arg + 7);
6216 if (*eap->arg == '@')
6217 eap->arg = skiptowhite(eap->arg);
6218 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006219
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006220 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006221 {
6222 int count = 0;
6223 char_u *start = skipwhite(line);
6224
6225 // :cmd xxx`=expr1`yyy`=expr2`zzz
6226 // PUSHS ":cmd xxx"
6227 // eval expr1
6228 // PUSHS "yyy"
6229 // eval expr2
6230 // PUSHS "zzz"
6231 // EXECCONCAT 5
6232 for (;;)
6233 {
6234 if (p > start)
6235 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006236 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006237 ++count;
6238 }
6239 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006240 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006241 return NULL;
6242 may_generate_2STRING(-1, cctx);
6243 ++count;
6244 p = skipwhite(p);
6245 if (*p != '`')
6246 {
6247 emsg(_("E1083: missing backtick"));
6248 return NULL;
6249 }
6250 start = p + 1;
6251
6252 p = (char_u *)strstr((char *)start, "`=");
6253 if (p == NULL)
6254 {
6255 if (*skipwhite(start) != NUL)
6256 {
6257 generate_PUSHS(cctx, vim_strsave(start));
6258 ++count;
6259 }
6260 break;
6261 }
6262 }
6263 generate_EXECCONCAT(cctx, count);
6264 }
6265 else
6266 generate_EXEC(cctx, line);
6267
6268theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006269 if (*nextcmd != NUL)
6270 {
6271 // the parser expects a pointer to the bar, put it back
6272 --nextcmd;
6273 *nextcmd = '|';
6274 }
6275
6276 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006277}
6278
6279/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006280 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006281 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006282 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006283 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006284add_def_function(ufunc_T *ufunc)
6285{
6286 dfunc_T *dfunc;
6287
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006288 if (def_functions.ga_len == 0)
6289 {
6290 // The first position is not used, so that a zero uf_dfunc_idx means it
6291 // wasn't set.
6292 if (ga_grow(&def_functions, 1) == FAIL)
6293 return FAIL;
6294 ++def_functions.ga_len;
6295 }
6296
Bram Moolenaar09689a02020-05-09 22:50:08 +02006297 // Add the function to "def_functions".
6298 if (ga_grow(&def_functions, 1) == FAIL)
6299 return FAIL;
6300 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6301 CLEAR_POINTER(dfunc);
6302 dfunc->df_idx = def_functions.ga_len;
6303 ufunc->uf_dfunc_idx = dfunc->df_idx;
6304 dfunc->df_ufunc = ufunc;
6305 ++def_functions.ga_len;
6306 return OK;
6307}
6308
6309/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 * After ex_function() has collected all the function lines: parse and compile
6311 * the lines into instructions.
6312 * Adds the function to "def_functions".
6313 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6314 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006315 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006316 * This can be used recursively through compile_lambda(), which may reallocate
6317 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006318 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006320 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006321compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 char_u *line = NULL;
6324 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 cctx_T cctx;
6327 garray_T *instr;
6328 int called_emsg_before = called_emsg;
6329 int ret = FAIL;
6330 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006331 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006332 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006333 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006335 // When using a function that was compiled before: Free old instructions.
6336 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006337 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006339 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6340 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006341 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006343 else
6344 {
6345 if (add_def_function(ufunc) == FAIL)
6346 return FAIL;
6347 new_def_function = TRUE;
6348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349
Bram Moolenaar985116a2020-07-12 17:31:09 +02006350 ufunc->uf_def_status = UF_COMPILING;
6351
Bram Moolenaara80faa82020-04-12 19:37:17 +02006352 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 cctx.ctx_ufunc = ufunc;
6354 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006355 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6357 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6358 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6359 cctx.ctx_type_list = &ufunc->uf_type_list;
6360 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6361 instr = &cctx.ctx_instr;
6362
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006363 // Set the context to the function, it may be compiled when called from
6364 // another script. Set the script version to the most modern one.
6365 // The line number will be set in next_line_from_context().
6366 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6368
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006369 // Make sure error messages are OK.
6370 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6371 if (do_estack_push)
6372 estack_push_ufunc(ufunc, 1);
6373
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006374 if (ufunc->uf_def_args.ga_len > 0)
6375 {
6376 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006377 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006378 int i;
6379 char_u *arg;
6380 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006381 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006382
6383 // Produce instructions for the default values of optional arguments.
6384 // Store the instruction index in uf_def_arg_idx[] so that we know
6385 // where to start when the function is called, depending on the number
6386 // of arguments.
6387 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6388 if (ufunc->uf_def_arg_idx == NULL)
6389 goto erret;
6390 for (i = 0; i < count; ++i)
6391 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006392 garray_T *stack = &cctx.ctx_type_stack;
6393 type_T *val_type;
6394 int arg_idx = first_def_arg + i;
6395
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006396 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6397 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006398 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006399 goto erret;
6400
6401 // If no type specified use the type of the default value.
6402 // Otherwise check that the default value type matches the
6403 // specified type.
6404 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6405 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006406 {
6407 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006408 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006409 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006410 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006411 == FAIL)
6412 {
6413 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6414 arg_idx + 1);
6415 goto erret;
6416 }
6417
6418 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006419 goto erret;
6420 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006421 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006422
6423 if (did_set_arg_type)
6424 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006425 }
6426
6427 /*
6428 * Loop over all the lines of the function and generate instructions.
6429 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 for (;;)
6431 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006432 exarg_T ea;
6433 cmdmod_T save_cmdmod;
6434 int starts_with_colon = FALSE;
6435 char_u *cmd;
6436 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006437
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006438 // Bail out on the first error to avoid a flood of errors and report
6439 // the right line number when inside try/catch.
6440 if (emsg_before != called_emsg)
6441 goto erret;
6442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443 if (line != NULL && *line == '|')
6444 // the line continues after a '|'
6445 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006446 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006447 && !(*line == '#' && (line == cctx.ctx_line_start
6448 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006450 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451 goto erret;
6452 }
6453 else
6454 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006455 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006456 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006457 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006460 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461
Bram Moolenaara80faa82020-04-12 19:37:17 +02006462 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 ea.cmdlinep = &line;
6464 ea.cmd = skipwhite(line);
6465
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006466 // Some things can be recognized by the first character.
6467 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006469 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006470 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006471 if (ea.cmd[1] != '{')
6472 {
6473 line = (char_u *)"";
6474 continue;
6475 }
6476 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006478 case '}':
6479 {
6480 // "}" ends a block scope
6481 scopetype_T stype = cctx.ctx_scope == NULL
6482 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006484 if (stype == BLOCK_SCOPE)
6485 {
6486 compile_endblock(&cctx);
6487 line = ea.cmd;
6488 }
6489 else
6490 {
6491 emsg(_("E1025: using } outside of a block scope"));
6492 goto erret;
6493 }
6494 if (line != NULL)
6495 line = skipwhite(ea.cmd + 1);
6496 continue;
6497 }
6498
6499 case '{':
6500 // "{" starts a block scope
6501 // "{'a': 1}->func() is something else
6502 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6503 {
6504 line = compile_block(ea.cmd, &cctx);
6505 continue;
6506 }
6507 break;
6508
6509 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006510 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006511 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 }
6513
6514 /*
6515 * COMMAND MODIFIERS
6516 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006517 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6519 {
6520 if (errormsg != NULL)
6521 goto erret;
6522 // empty line or comment
6523 line = (char_u *)"";
6524 continue;
6525 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006526 // TODO: use modifiers in the command
6527 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006528 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529
6530 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006531 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006533 {
6534 if (*ea.cmd == '(')
6535 // not for "call()"
6536 ea.cmd = p;
6537 else
6538 ea.cmd = skipwhite(ea.cmd);
6539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006541 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006543 char_u *pskip;
6544
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006545 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006546 // find what follows.
6547 // Skip over "var.member", "var[idx]" and the like.
6548 // Also "&opt = val", "$ENV = val" and "@r = val".
6549 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006551 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006552 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006554 char_u *var_end;
6555 int oplen;
6556 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557
Bram Moolenaar65821722020-08-02 18:58:54 +02006558 if (ea.cmd[0] == '@')
6559 var_end = ea.cmd + 2;
6560 else
6561 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006562 FNE_CHECK_START | FNE_INCL_BR);
6563 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006564 if (oplen > 0)
6565 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006566 size_t len = p - ea.cmd;
6567
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006568 // Recognize an assignment if we recognize the variable
6569 // name:
6570 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006571 // "local = expr" where "local" is a local var.
6572 // "script = expr" where "script" is a script-local var.
6573 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006574 // "&opt = expr"
6575 // "$ENV = expr"
6576 // "@r = expr"
6577 if (*ea.cmd == '&'
6578 || *ea.cmd == '$'
6579 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006580 || ((len) > 2 && ea.cmd[1] == ':')
6581 || lookup_local(ea.cmd, len, &cctx) != NULL
6582 || lookup_arg(ea.cmd, len, NULL, NULL,
6583 NULL, &cctx) == OK
6584 || lookup_script(ea.cmd, len) == OK
6585 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006586 {
6587 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006589 goto erret;
6590 continue;
6591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 }
6593 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006594
6595 if (*ea.cmd == '[')
6596 {
6597 // [var, var] = expr
6598 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6599 if (line == NULL)
6600 goto erret;
6601 if (line != ea.cmd)
6602 continue;
6603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 }
6605
6606 /*
6607 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006608 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006610 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006611 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006612 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006613 ea.cmd = skip_range(ea.cmd, NULL);
6614 if (ea.cmd > cmd && !starts_with_colon)
6615 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006616 emsg(_(e_colon_required_before_a_range));
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006617 goto erret;
6618 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006619 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006620 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006621 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006622 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623
6624 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6625 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006626 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006627 {
6628 line += STRLEN(line);
6629 continue;
6630 }
6631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006633 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006635 // CMD_let cannot happen, compile_assignment() above is used
6636 iemsg("Command from find_ex_command() not handled");
6637 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 }
6640
6641 p = skipwhite(p);
6642
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006643 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006644 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006645 && ea.cmdidx != CMD_elseif
6646 && ea.cmdidx != CMD_else
6647 && ea.cmdidx != CMD_endif)
6648 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006649 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006650 continue;
6651 }
6652
Bram Moolenaarefd88552020-06-18 20:50:10 +02006653 if (ea.cmdidx != CMD_elseif
6654 && ea.cmdidx != CMD_else
6655 && ea.cmdidx != CMD_endif
6656 && ea.cmdidx != CMD_endfor
6657 && ea.cmdidx != CMD_endwhile
6658 && ea.cmdidx != CMD_catch
6659 && ea.cmdidx != CMD_finally
6660 && ea.cmdidx != CMD_endtry)
6661 {
6662 if (cctx.ctx_had_return)
6663 {
6664 emsg(_("E1095: Unreachable code after :return"));
6665 goto erret;
6666 }
6667 }
6668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 switch (ea.cmdidx)
6670 {
6671 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006672 ea.arg = p;
6673 line = compile_nested_function(&ea, &cctx);
6674 break;
6675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006677 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 goto erret;
6679
6680 case CMD_return:
6681 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006682 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 break;
6684
6685 case CMD_let:
6686 case CMD_const:
6687 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006688 if (line == p)
6689 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 break;
6691
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006692 case CMD_unlet:
6693 case CMD_unlockvar:
6694 case CMD_lockvar:
6695 line = compile_unletlock(p, &ea, &cctx);
6696 break;
6697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 case CMD_import:
6699 line = compile_import(p, &cctx);
6700 break;
6701
6702 case CMD_if:
6703 line = compile_if(p, &cctx);
6704 break;
6705 case CMD_elseif:
6706 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006707 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708 break;
6709 case CMD_else:
6710 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006711 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 break;
6713 case CMD_endif:
6714 line = compile_endif(p, &cctx);
6715 break;
6716
6717 case CMD_while:
6718 line = compile_while(p, &cctx);
6719 break;
6720 case CMD_endwhile:
6721 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006722 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 break;
6724
6725 case CMD_for:
6726 line = compile_for(p, &cctx);
6727 break;
6728 case CMD_endfor:
6729 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006730 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 break;
6732 case CMD_continue:
6733 line = compile_continue(p, &cctx);
6734 break;
6735 case CMD_break:
6736 line = compile_break(p, &cctx);
6737 break;
6738
6739 case CMD_try:
6740 line = compile_try(p, &cctx);
6741 break;
6742 case CMD_catch:
6743 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006744 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 break;
6746 case CMD_finally:
6747 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006748 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 break;
6750 case CMD_endtry:
6751 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006752 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 break;
6754 case CMD_throw:
6755 line = compile_throw(p, &cctx);
6756 break;
6757
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006758 case CMD_eval:
6759 if (compile_expr0(&p, &cctx) == FAIL)
6760 goto erret;
6761
6762 // drop the return value
6763 generate_instr_drop(&cctx, ISN_DROP, 1);
6764
6765 line = skipwhite(p);
6766 break;
6767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006770 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006771 case CMD_echomsg:
6772 case CMD_echoerr:
6773 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006774 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006776 // TODO: other commands with an expression argument
6777
Bram Moolenaarae616492020-07-28 20:07:27 +02006778 case CMD_append:
6779 case CMD_change:
6780 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006781 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006782 case CMD_xit:
6783 not_in_vim9(&ea);
6784 goto erret;
6785
Bram Moolenaar002262f2020-07-08 17:47:57 +02006786 case CMD_SIZE:
6787 semsg(_("E476: Invalid command: %s"), ea.cmd);
6788 goto erret;
6789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006791 // Not recognized, execute with do_cmdline_cmd().
6792 ea.arg = p;
6793 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 break;
6795 }
6796 if (line == NULL)
6797 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006798 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799
6800 if (cctx.ctx_type_stack.ga_len < 0)
6801 {
6802 iemsg("Type stack underflow");
6803 goto erret;
6804 }
6805 }
6806
6807 if (cctx.ctx_scope != NULL)
6808 {
6809 if (cctx.ctx_scope->se_type == IF_SCOPE)
6810 emsg(_(e_endif));
6811 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6812 emsg(_(e_endwhile));
6813 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6814 emsg(_(e_endfor));
6815 else
6816 emsg(_("E1026: Missing }"));
6817 goto erret;
6818 }
6819
Bram Moolenaarefd88552020-06-18 20:50:10 +02006820 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 {
6822 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6823 {
6824 emsg(_("E1027: Missing return statement"));
6825 goto erret;
6826 }
6827
6828 // Return zero if there is no return at the end.
6829 generate_PUSHNR(&cctx, 0);
6830 generate_instr(&cctx, ISN_RETURN);
6831 }
6832
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006833 {
6834 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6835 + ufunc->uf_dfunc_idx;
6836 dfunc->df_deleted = FALSE;
6837 dfunc->df_instr = instr->ga_data;
6838 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006839 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006840 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006841 if (cctx.ctx_outer_used)
6842 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006843 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845
6846 ret = OK;
6847
6848erret:
6849 if (ret == FAIL)
6850 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006851 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006852 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6853 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006854
6855 for (idx = 0; idx < instr->ga_len; ++idx)
6856 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006858
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006859 // If using the last entry in the table and it was added above, we
6860 // might as well remove it.
6861 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006862 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006863 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006864 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006865 ufunc->uf_dfunc_idx = 0;
6866 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006867 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006868
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006869 while (cctx.ctx_scope != NULL)
6870 drop_scope(&cctx);
6871
Bram Moolenaar20431c92020-03-20 18:39:46 +01006872 // Don't execute this function body.
6873 ga_clear_strings(&ufunc->uf_lines);
6874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 if (errormsg != NULL)
6876 emsg(errormsg);
6877 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006878 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 }
6880
6881 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006882 if (do_estack_push)
6883 estack_pop();
6884
Bram Moolenaar20431c92020-03-20 18:39:46 +01006885 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006886 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006888 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889}
6890
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006891 void
6892set_function_type(ufunc_T *ufunc)
6893{
6894 int varargs = ufunc->uf_va_name != NULL;
6895 int argcount = ufunc->uf_args.ga_len;
6896
6897 // Create a type for the function, with the return type and any
6898 // argument types.
6899 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6900 // The type is included in "tt_args".
6901 if (argcount > 0 || varargs)
6902 {
6903 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6904 argcount, &ufunc->uf_type_list);
6905 // Add argument types to the function type.
6906 if (func_type_add_arg_types(ufunc->uf_func_type,
6907 argcount + varargs,
6908 &ufunc->uf_type_list) == FAIL)
6909 return;
6910 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6911 ufunc->uf_func_type->tt_min_argcount =
6912 argcount - ufunc->uf_def_args.ga_len;
6913 if (ufunc->uf_arg_types == NULL)
6914 {
6915 int i;
6916
6917 // lambda does not have argument types.
6918 for (i = 0; i < argcount; ++i)
6919 ufunc->uf_func_type->tt_args[i] = &t_any;
6920 }
6921 else
6922 mch_memmove(ufunc->uf_func_type->tt_args,
6923 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6924 if (varargs)
6925 {
6926 ufunc->uf_func_type->tt_args[argcount] =
6927 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6928 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6929 }
6930 }
6931 else
6932 // No arguments, can use a predefined type.
6933 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6934 argcount, &ufunc->uf_type_list);
6935}
6936
6937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938/*
6939 * Delete an instruction, free what it contains.
6940 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006941 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942delete_instr(isn_T *isn)
6943{
6944 switch (isn->isn_type)
6945 {
6946 case ISN_EXEC:
6947 case ISN_LOADENV:
6948 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006949 case ISN_LOADB:
6950 case ISN_LOADW:
6951 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006953 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 case ISN_PUSHEXC:
6955 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006956 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006958 case ISN_STOREB:
6959 case ISN_STOREW:
6960 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006961 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 vim_free(isn->isn_arg.string);
6963 break;
6964
6965 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006966 case ISN_STORES:
6967 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 break;
6969
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006970 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006971 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006972 vim_free(isn->isn_arg.unlet.ul_name);
6973 break;
6974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 case ISN_STOREOPT:
6976 vim_free(isn->isn_arg.storeopt.so_name);
6977 break;
6978
6979 case ISN_PUSHBLOB: // push blob isn_arg.blob
6980 blob_unref(isn->isn_arg.blob);
6981 break;
6982
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006983 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006985 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006986#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006987 break;
6988
6989 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006990#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006991 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006992#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006993 break;
6994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 case ISN_UCALL:
6996 vim_free(isn->isn_arg.ufunc.cuf_name);
6997 break;
6998
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006999 case ISN_FUNCREF:
7000 {
7001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7002 + isn->isn_arg.funcref.fr_func;
7003 func_ptr_unref(dfunc->df_ufunc);
7004 }
7005 break;
7006
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007007 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007008 {
7009 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7010 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7011
7012 if (ufunc != NULL)
7013 {
7014 // Clear uf_dfunc_idx so that the function is deleted.
7015 clear_def_function(ufunc);
7016 ufunc->uf_dfunc_idx = 0;
7017 func_ptr_unref(ufunc);
7018 }
7019
7020 vim_free(lambda);
7021 vim_free(isn->isn_arg.newfunc.nf_global);
7022 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007023 break;
7024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 case ISN_2BOOL:
7026 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007027 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 case ISN_ADDBLOB:
7029 case ISN_ADDLIST:
7030 case ISN_BCALL:
7031 case ISN_CATCH:
7032 case ISN_CHECKNR:
7033 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007034 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 case ISN_COMPAREANY:
7036 case ISN_COMPAREBLOB:
7037 case ISN_COMPAREBOOL:
7038 case ISN_COMPAREDICT:
7039 case ISN_COMPAREFLOAT:
7040 case ISN_COMPAREFUNC:
7041 case ISN_COMPARELIST:
7042 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 case ISN_COMPARESPECIAL:
7044 case ISN_COMPARESTRING:
7045 case ISN_CONCAT:
7046 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007047 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 case ISN_DROP:
7049 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007050 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007051 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007053 case ISN_EXECCONCAT:
7054 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007056 case ISN_LISTINDEX:
7057 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007058 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007059 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007060 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 case ISN_JUMP:
7062 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007063 case ISN_LOADBDICT:
7064 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007065 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007067 case ISN_LOADSCRIPT:
7068 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007070 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 case ISN_NEGATENR:
7072 case ISN_NEWDICT:
7073 case ISN_NEWLIST:
7074 case ISN_OPNR:
7075 case ISN_OPFLOAT:
7076 case ISN_OPANY:
7077 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007078 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 case ISN_PUSHF:
7080 case ISN_PUSHNR:
7081 case ISN_PUSHBOOL:
7082 case ISN_PUSHSPEC:
7083 case ISN_RETURN:
7084 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007085 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007086 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007088 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007090 case ISN_STOREDICT:
7091 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 case ISN_THROW:
7093 case ISN_TRY:
7094 // nothing allocated
7095 break;
7096 }
7097}
7098
7099/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007100 * Free all instructions for "dfunc".
7101 */
7102 static void
7103delete_def_function_contents(dfunc_T *dfunc)
7104{
7105 int idx;
7106
7107 ga_clear(&dfunc->df_def_args_isn);
7108
7109 if (dfunc->df_instr != NULL)
7110 {
7111 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7112 delete_instr(dfunc->df_instr + idx);
7113 VIM_CLEAR(dfunc->df_instr);
7114 }
7115
7116 dfunc->df_deleted = TRUE;
7117}
7118
7119/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007120 * When a user function is deleted, clear the contents of any associated def
7121 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 */
7123 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007124clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007126 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 {
7128 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7129 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130
Bram Moolenaar20431c92020-03-20 18:39:46 +01007131 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007132 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 }
7134}
7135
7136#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007137/*
7138 * Free all functions defined with ":def".
7139 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 void
7141free_def_functions(void)
7142{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007143 int idx;
7144
7145 for (idx = 0; idx < def_functions.ga_len; ++idx)
7146 {
7147 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7148
7149 delete_def_function_contents(dfunc);
7150 }
7151
7152 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153}
7154#endif
7155
7156
7157#endif // FEAT_EVAL