blob: 920628444ef75dc1d80566568d8e4884591c0c1c [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaarf9b2b492020-08-05 14:34:14 +0200151static char e_unknown_var[] = N_("E1089: unknown variable: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
Bram Moolenaar20431c92020-03-20 18:39:46 +0100153static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200294 int c = p[len];
295
296 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 if (lookup_script(p, len) == OK
298 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200299 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200300 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200301 || find_imported(p, len, cctx) != NULL
302 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200304 p[len] = c;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200305 semsg(_(e_name_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 return FAIL;
307 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200308 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 return OK;
310}
311
Bram Moolenaar65b95452020-07-19 14:03:09 +0200312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313/////////////////////////////////////////////////////////////////////
314// Following generate_ functions expect the caller to call ga_grow().
315
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200316#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
317#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/*
320 * Generate an instruction without arguments.
321 * Returns a pointer to the new instruction, NULL if failed.
322 */
323 static isn_T *
324generate_instr(cctx_T *cctx, isntype_T isn_type)
325{
326 garray_T *instr = &cctx->ctx_instr;
327 isn_T *isn;
328
Bram Moolenaar080457c2020-03-03 21:53:32 +0100329 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 if (ga_grow(instr, 1) == FAIL)
331 return NULL;
332 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
333 isn->isn_type = isn_type;
334 isn->isn_lnum = cctx->ctx_lnum + 1;
335 ++instr->ga_len;
336
337 return isn;
338}
339
340/*
341 * Generate an instruction without arguments.
342 * "drop" will be removed from the stack.
343 * Returns a pointer to the new instruction, NULL if failed.
344 */
345 static isn_T *
346generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
347{
348 garray_T *stack = &cctx->ctx_type_stack;
349
Bram Moolenaar080457c2020-03-03 21:53:32 +0100350 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351 stack->ga_len -= drop;
352 return generate_instr(cctx, isn_type);
353}
354
355/*
356 * Generate instruction "isn_type" and put "type" on the type stack.
357 */
358 static isn_T *
359generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
360{
361 isn_T *isn;
362 garray_T *stack = &cctx->ctx_type_stack;
363
364 if ((isn = generate_instr(cctx, isn_type)) == NULL)
365 return NULL;
366
367 if (ga_grow(stack, 1) == FAIL)
368 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200369 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 ++stack->ga_len;
371
372 return isn;
373}
374
375/*
376 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200377 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 */
379 static int
380may_generate_2STRING(int offset, cctx_T *cctx)
381{
382 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 garray_T *stack = &cctx->ctx_type_stack;
385 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
386
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200387 switch ((*type)->tt_type)
388 {
389 // nothing to be done
390 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200392 // conversion possible
393 case VAR_SPECIAL:
394 case VAR_BOOL:
395 case VAR_NUMBER:
396 case VAR_FLOAT:
397 break;
398
399 // conversion possible (with runtime check)
400 case VAR_ANY:
401 case VAR_UNKNOWN:
402 isntype = ISN_2STRING_ANY;
403 break;
404
405 // conversion not possible
406 case VAR_VOID:
407 case VAR_BLOB:
408 case VAR_FUNC:
409 case VAR_PARTIAL:
410 case VAR_LIST:
411 case VAR_DICT:
412 case VAR_JOB:
413 case VAR_CHANNEL:
414 to_string_error((*type)->tt_type);
415 return FAIL;
416 }
417
418 *type = &t_string;
419 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 return FAIL;
421 isn->isn_arg.number = offset;
422
423 return OK;
424}
425
426 static int
427check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
428{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200429 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200431 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 {
433 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200434 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 else
436 semsg(_("E1036: %c requires number or float arguments"), *op);
437 return FAIL;
438 }
439 return OK;
440}
441
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200442 static int
443generate_add_instr(
444 cctx_T *cctx,
445 vartype_T vartype,
446 type_T *type1,
447 type_T *type2)
448{
449 isn_T *isn = generate_instr_drop(cctx,
450 vartype == VAR_NUMBER ? ISN_OPNR
451 : vartype == VAR_LIST ? ISN_ADDLIST
452 : vartype == VAR_BLOB ? ISN_ADDBLOB
453#ifdef FEAT_FLOAT
454 : vartype == VAR_FLOAT ? ISN_OPFLOAT
455#endif
456 : ISN_OPANY, 1);
457
458 if (vartype != VAR_LIST && vartype != VAR_BLOB
459 && type1->tt_type != VAR_ANY
460 && type2->tt_type != VAR_ANY
461 && check_number_or_float(
462 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
463 return FAIL;
464
465 if (isn != NULL)
466 isn->isn_arg.op.op_type = EXPR_ADD;
467 return isn == NULL ? FAIL : OK;
468}
469
470/*
471 * Get the type to use for an instruction for an operation on "type1" and
472 * "type2". If they are matching use a type-specific instruction. Otherwise
473 * fall back to runtime type checking.
474 */
475 static vartype_T
476operator_type(type_T *type1, type_T *type2)
477{
478 if (type1->tt_type == type2->tt_type
479 && (type1->tt_type == VAR_NUMBER
480 || type1->tt_type == VAR_LIST
481#ifdef FEAT_FLOAT
482 || type1->tt_type == VAR_FLOAT
483#endif
484 || type1->tt_type == VAR_BLOB))
485 return type1->tt_type;
486 return VAR_ANY;
487}
488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489/*
490 * Generate an instruction with two arguments. The instruction depends on the
491 * type of the arguments.
492 */
493 static int
494generate_two_op(cctx_T *cctx, char_u *op)
495{
496 garray_T *stack = &cctx->ctx_type_stack;
497 type_T *type1;
498 type_T *type2;
499 vartype_T vartype;
500 isn_T *isn;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_OK_IF_SKIP(cctx);
503
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200504 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
506 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200507 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508
509 switch (*op)
510 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200511 case '+':
512 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 break;
515
516 case '-':
517 case '*':
518 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
519 op) == FAIL)
520 return FAIL;
521 if (vartype == VAR_NUMBER)
522 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
523#ifdef FEAT_FLOAT
524 else if (vartype == VAR_FLOAT)
525 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
526#endif
527 else
528 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
529 if (isn != NULL)
530 isn->isn_arg.op.op_type = *op == '*'
531 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
532 break;
533
Bram Moolenaar4c683752020-04-05 21:38:23 +0200534 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200536 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 && type2->tt_type != VAR_NUMBER))
538 {
539 emsg(_("E1035: % requires number arguments"));
540 return FAIL;
541 }
542 isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
544 if (isn != NULL)
545 isn->isn_arg.op.op_type = EXPR_REM;
546 break;
547 }
548
549 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 {
552 type_T *type = &t_any;
553
554#ifdef FEAT_FLOAT
555 // float+number and number+float results in float
556 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
557 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
558 type = &t_float;
559#endif
560 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
561 }
562
563 return OK;
564}
565
566/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200567 * Get the instruction to use for comparing "type1" with "type2"
568 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200570 static isntype_T
571get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572{
573 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574
Bram Moolenaar4c683752020-04-05 21:38:23 +0200575 if (type1 == VAR_UNKNOWN)
576 type1 = VAR_ANY;
577 if (type2 == VAR_UNKNOWN)
578 type2 = VAR_ANY;
579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (type1 == type2)
581 {
582 switch (type1)
583 {
584 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
585 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
586 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
587 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
588 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
589 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
590 case VAR_LIST: isntype = ISN_COMPARELIST; break;
591 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
592 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 default: isntype = ISN_COMPAREANY; break;
594 }
595 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200596 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
598 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
599 isntype = ISN_COMPAREANY;
600
601 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
602 && (isntype == ISN_COMPAREBOOL
603 || isntype == ISN_COMPARESPECIAL
604 || isntype == ISN_COMPARENR
605 || isntype == ISN_COMPAREFLOAT))
606 {
607 semsg(_("E1037: Cannot use \"%s\" with %s"),
608 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200609 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 }
611 if (isntype == ISN_DROP
612 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
613 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
614 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
615 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
616 && exptype != EXPR_IS && exptype != EXPR_ISNOT
617 && (type1 == VAR_BLOB || type2 == VAR_BLOB
618 || type1 == VAR_LIST || type2 == VAR_LIST))))
619 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100620 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200624 return isntype;
625}
626
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200627 int
628check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
629{
630 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
631 return FAIL;
632 return OK;
633}
634
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635/*
636 * Generate an ISN_COMPARE* instruction with a boolean result.
637 */
638 static int
639generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
640{
641 isntype_T isntype;
642 isn_T *isn;
643 garray_T *stack = &cctx->ctx_type_stack;
644 vartype_T type1;
645 vartype_T type2;
646
647 RETURN_OK_IF_SKIP(cctx);
648
649 // Get the known type of the two items on the stack. If they are matching
650 // use a type-specific instruction. Otherwise fall back to runtime type
651 // checking.
652 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
653 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
654 isntype = get_compare_isn(exptype, type1, type2);
655 if (isntype == ISN_DROP)
656 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 if ((isn = generate_instr(cctx, isntype)) == NULL)
659 return FAIL;
660 isn->isn_arg.op.op_type = exptype;
661 isn->isn_arg.op.op_ic = ic;
662
663 // takes two arguments, puts one bool back
664 if (stack->ga_len >= 2)
665 {
666 --stack->ga_len;
667 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
668 }
669
670 return OK;
671}
672
673/*
674 * Generate an ISN_2BOOL instruction.
675 */
676 static int
677generate_2BOOL(cctx_T *cctx, int invert)
678{
679 isn_T *isn;
680 garray_T *stack = &cctx->ctx_type_stack;
681
Bram Moolenaar080457c2020-03-03 21:53:32 +0100682 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
684 return FAIL;
685 isn->isn_arg.number = invert;
686
687 // type becomes bool
688 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
689
690 return OK;
691}
692
693 static int
694generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
695{
696 isn_T *isn;
697 garray_T *stack = &cctx->ctx_type_stack;
698
Bram Moolenaar080457c2020-03-03 21:53:32 +0100699 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
701 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200702 // TODO: whole type, e.g. for a function also arg and return types
703 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 isn->isn_arg.type.ct_off = offset;
705
706 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200707 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708
709 return OK;
710}
711
712/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200713 * Check that
714 * - "actual" is "expected" type or
715 * - "actual" is a type that can be "expected" type: add a runtime check; or
716 * - return FAIL.
717 */
718 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200719need_type(
720 type_T *actual,
721 type_T *expected,
722 int offset,
723 cctx_T *cctx,
724 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200725{
726 if (check_type(expected, actual, FALSE) == OK)
727 return OK;
728 if (actual->tt_type != VAR_ANY
729 && actual->tt_type != VAR_UNKNOWN
730 && !(actual->tt_type == VAR_FUNC
731 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
732 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200733 if (!silent)
734 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200735 return FAIL;
736 }
737 generate_TYPECHECK(cctx, expected, offset);
738 return OK;
739}
740
741/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 * Generate an ISN_PUSHNR instruction.
743 */
744 static int
745generate_PUSHNR(cctx_T *cctx, varnumber_T number)
746{
747 isn_T *isn;
748
Bram Moolenaar080457c2020-03-03 21:53:32 +0100749 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
751 return FAIL;
752 isn->isn_arg.number = number;
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_PUSHBOOL instruction.
759 */
760 static int
761generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
762{
763 isn_T *isn;
764
Bram Moolenaar080457c2020-03-03 21:53:32 +0100765 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
767 return FAIL;
768 isn->isn_arg.number = number;
769
770 return OK;
771}
772
773/*
774 * Generate an ISN_PUSHSPEC instruction.
775 */
776 static int
777generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
778{
779 isn_T *isn;
780
Bram Moolenaar080457c2020-03-03 21:53:32 +0100781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
783 return FAIL;
784 isn->isn_arg.number = number;
785
786 return OK;
787}
788
789#ifdef FEAT_FLOAT
790/*
791 * Generate an ISN_PUSHF instruction.
792 */
793 static int
794generate_PUSHF(cctx_T *cctx, float_T fnumber)
795{
796 isn_T *isn;
797
Bram Moolenaar080457c2020-03-03 21:53:32 +0100798 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
800 return FAIL;
801 isn->isn_arg.fnumber = fnumber;
802
803 return OK;
804}
805#endif
806
807/*
808 * Generate an ISN_PUSHS instruction.
809 * Consumes "str".
810 */
811 static int
812generate_PUSHS(cctx_T *cctx, char_u *str)
813{
814 isn_T *isn;
815
Bram Moolenaar080457c2020-03-03 21:53:32 +0100816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
818 return FAIL;
819 isn->isn_arg.string = str;
820
821 return OK;
822}
823
824/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100825 * Generate an ISN_PUSHCHANNEL instruction.
826 * Consumes "channel".
827 */
828 static int
829generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
830{
831 isn_T *isn;
832
Bram Moolenaar080457c2020-03-03 21:53:32 +0100833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100834 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
835 return FAIL;
836 isn->isn_arg.channel = channel;
837
838 return OK;
839}
840
841/*
842 * Generate an ISN_PUSHJOB instruction.
843 * Consumes "job".
844 */
845 static int
846generate_PUSHJOB(cctx_T *cctx, job_T *job)
847{
848 isn_T *isn;
849
Bram Moolenaar080457c2020-03-03 21:53:32 +0100850 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100851 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100852 return FAIL;
853 isn->isn_arg.job = job;
854
855 return OK;
856}
857
858/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 * Generate an ISN_PUSHBLOB instruction.
860 * Consumes "blob".
861 */
862 static int
863generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
864{
865 isn_T *isn;
866
Bram Moolenaar080457c2020-03-03 21:53:32 +0100867 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
869 return FAIL;
870 isn->isn_arg.blob = blob;
871
872 return OK;
873}
874
875/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100876 * Generate an ISN_PUSHFUNC instruction with name "name".
877 * Consumes "name".
878 */
879 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200880generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100881{
882 isn_T *isn;
883
Bram Moolenaar080457c2020-03-03 21:53:32 +0100884 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200885 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100886 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200887 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100888
889 return OK;
890}
891
892/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200893 * Generate an ISN_GETITEM instruction with "index".
894 */
895 static int
896generate_GETITEM(cctx_T *cctx, int index)
897{
898 isn_T *isn;
899 garray_T *stack = &cctx->ctx_type_stack;
900 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
901 type_T *item_type = &t_any;
902
903 RETURN_OK_IF_SKIP(cctx);
904
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200905 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200906 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200907 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200908 emsg(_(e_listreq));
909 return FAIL;
910 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200911 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
913 return FAIL;
914 isn->isn_arg.number = index;
915
916 // add the item type to the type stack
917 if (ga_grow(stack, 1) == FAIL)
918 return FAIL;
919 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
920 ++stack->ga_len;
921 return OK;
922}
923
924/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200925 * Generate an ISN_SLICE instruction with "count".
926 */
927 static int
928generate_SLICE(cctx_T *cctx, int count)
929{
930 isn_T *isn;
931
932 RETURN_OK_IF_SKIP(cctx);
933 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
934 return FAIL;
935 isn->isn_arg.number = count;
936 return OK;
937}
938
939/*
940 * Generate an ISN_CHECKLEN instruction with "min_len".
941 */
942 static int
943generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
944{
945 isn_T *isn;
946
947 RETURN_OK_IF_SKIP(cctx);
948
949 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
950 return FAIL;
951 isn->isn_arg.checklen.cl_min_len = min_len;
952 isn->isn_arg.checklen.cl_more_OK = more_OK;
953
954 return OK;
955}
956
957/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 * Generate an ISN_STORE instruction.
959 */
960 static int
961generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
962{
963 isn_T *isn;
964
Bram Moolenaar080457c2020-03-03 21:53:32 +0100965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
967 return FAIL;
968 if (name != NULL)
969 isn->isn_arg.string = vim_strsave(name);
970 else
971 isn->isn_arg.number = idx;
972
973 return OK;
974}
975
976/*
977 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
978 */
979 static int
980generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
986 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100987 isn->isn_arg.storenr.stnr_idx = idx;
988 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989
990 return OK;
991}
992
993/*
994 * Generate an ISN_STOREOPT instruction
995 */
996 static int
997generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001002 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 return FAIL;
1004 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1005 isn->isn_arg.storeopt.so_flags = opt_flags;
1006
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_LOAD or similar instruction.
1012 */
1013 static int
1014generate_LOAD(
1015 cctx_T *cctx,
1016 isntype_T isn_type,
1017 int idx,
1018 char_u *name,
1019 type_T *type)
1020{
1021 isn_T *isn;
1022
Bram Moolenaar080457c2020-03-03 21:53:32 +01001023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1025 return FAIL;
1026 if (name != NULL)
1027 isn->isn_arg.string = vim_strsave(name);
1028 else
1029 isn->isn_arg.number = idx;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001035 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001036 */
1037 static int
1038generate_LOADV(
1039 cctx_T *cctx,
1040 char_u *name,
1041 int error)
1042{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001043 int di_flags;
1044 int vidx = find_vim_var(name, &di_flags);
1045 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001048 if (vidx < 0)
1049 {
1050 if (error)
1051 semsg(_(e_var_notfound), name);
1052 return FAIL;
1053 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001054 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001056 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001057}
1058
1059/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001060 * Generate an ISN_UNLET instruction.
1061 */
1062 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001063generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001064{
1065 isn_T *isn;
1066
1067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001068 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001069 return FAIL;
1070 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1071 isn->isn_arg.unlet.ul_forceit = forceit;
1072
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 * Generate an ISN_LOADS instruction.
1078 */
1079 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001080generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001084 int sid,
1085 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086{
1087 isn_T *isn;
1088
Bram Moolenaar080457c2020-03-03 21:53:32 +01001089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 if (isn_type == ISN_LOADS)
1091 isn = generate_instr_type(cctx, isn_type, type);
1092 else
1093 isn = generate_instr_drop(cctx, isn_type, 1);
1094 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001096 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1097 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
1099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1104 */
1105 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 cctx_T *cctx,
1108 isntype_T isn_type,
1109 int sid,
1110 int idx,
1111 type_T *type)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if (isn_type == ISN_LOADSCRIPT)
1117 isn = generate_instr_type(cctx, isn_type, type);
1118 else
1119 isn = generate_instr_drop(cctx, isn_type, 1);
1120 if (isn == NULL)
1121 return FAIL;
1122 isn->isn_arg.script.script_sid = sid;
1123 isn->isn_arg.script.script_idx = idx;
1124 return OK;
1125}
1126
1127/*
1128 * Generate an ISN_NEWLIST instruction.
1129 */
1130 static int
1131generate_NEWLIST(cctx_T *cctx, int count)
1132{
1133 isn_T *isn;
1134 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 type_T *type;
1136 type_T *member;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.number = count;
1142
Bram Moolenaar127542b2020-08-09 17:22:04 +02001143 // get the member type from all the items on the stack.
1144 member = get_member_type_from_stack(
1145 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1146 cctx->ctx_type_list);
1147 type = get_list_type(member, cctx->ctx_type_list);
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 // drop the value types
1150 stack->ga_len -= count;
1151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 // add the list type to the type stack
1153 if (ga_grow(stack, 1) == FAIL)
1154 return FAIL;
1155 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1156 ++stack->ga_len;
1157
1158 return OK;
1159}
1160
1161/*
1162 * Generate an ISN_NEWDICT instruction.
1163 */
1164 static int
1165generate_NEWDICT(cctx_T *cctx, int count)
1166{
1167 isn_T *isn;
1168 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 type_T *type;
1170 type_T *member;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = count;
1176
Bram Moolenaar127542b2020-08-09 17:22:04 +02001177 member = get_member_type_from_stack(
1178 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1179 cctx->ctx_type_list);
1180 type = get_dict_type(member, cctx->ctx_type_list);
1181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 // drop the key and value types
1183 stack->ga_len -= 2 * count;
1184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 // add the dict type to the type stack
1186 if (ga_grow(stack, 1) == FAIL)
1187 return FAIL;
1188 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1189 ++stack->ga_len;
1190
1191 return OK;
1192}
1193
1194/*
1195 * Generate an ISN_FUNCREF instruction.
1196 */
1197 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001198generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199{
1200 isn_T *isn;
1201 garray_T *stack = &cctx->ctx_type_stack;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1205 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001206 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001207 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208
1209 if (ga_grow(stack, 1) == FAIL)
1210 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001211 ((type_T **)stack->ga_data)[stack->ga_len] =
1212 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 ++stack->ga_len;
1214
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001219 * Generate an ISN_NEWFUNC instruction.
1220 */
1221 static int
1222generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1223{
1224 isn_T *isn;
1225 char_u *name;
1226
1227 RETURN_OK_IF_SKIP(cctx);
1228 name = vim_strsave(lambda_name);
1229 if (name == NULL)
1230 return FAIL;
1231 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1232 return FAIL;
1233 isn->isn_arg.newfunc.nf_lambda = name;
1234 isn->isn_arg.newfunc.nf_global = func_name;
1235
1236 return OK;
1237}
1238
1239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 * Generate an ISN_JUMP instruction.
1241 */
1242 static int
1243generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1244{
1245 isn_T *isn;
1246 garray_T *stack = &cctx->ctx_type_stack;
1247
Bram Moolenaar080457c2020-03-03 21:53:32 +01001248 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.jump.jump_when = when;
1252 isn->isn_arg.jump.jump_where = where;
1253
1254 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1255 --stack->ga_len;
1256
1257 return OK;
1258}
1259
1260 static int
1261generate_FOR(cctx_T *cctx, int loop_idx)
1262{
1263 isn_T *isn;
1264 garray_T *stack = &cctx->ctx_type_stack;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.forloop.for_idx = loop_idx;
1270
1271 if (ga_grow(stack, 1) == FAIL)
1272 return FAIL;
1273 // type doesn't matter, will be stored next
1274 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1275 ++stack->ga_len;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001282 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 * Return FAIL if the number of arguments is wrong.
1284 */
1285 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001286generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287{
1288 isn_T *isn;
1289 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001290 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001291 type_T *argtypes[MAX_FUNC_ARGS];
1292 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001295 argoff = check_internal_func(func_idx, argcount);
1296 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 return FAIL;
1298
Bram Moolenaar389df252020-07-09 21:20:47 +02001299 if (method_call && argoff > 1)
1300 {
1301 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1302 return FAIL;
1303 isn->isn_arg.shuffle.shfl_item = argcount;
1304 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1305 }
1306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.bfunc.cbf_idx = func_idx;
1310 isn->isn_arg.bfunc.cbf_argcount = argcount;
1311
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001312 for (i = 0; i < argcount; ++i)
1313 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 stack->ga_len -= argcount; // drop the arguments
1316 if (ga_grow(stack, 1) == FAIL)
1317 return FAIL;
1318 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001319 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 ++stack->ga_len; // add return value
1321
1322 return OK;
1323}
1324
1325/*
1326 * Generate an ISN_DCALL or ISN_UCALL instruction.
1327 * Return FAIL if the number of arguments is wrong.
1328 */
1329 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001330generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001335 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
Bram Moolenaar080457c2020-03-03 21:53:32 +01001337 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 if (argcount > regular_args && !has_varargs(ufunc))
1339 {
1340 semsg(_(e_toomanyarg), ufunc->uf_name);
1341 return FAIL;
1342 }
1343 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1344 {
1345 semsg(_(e_toofewarg), ufunc->uf_name);
1346 return FAIL;
1347 }
1348
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001349 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001350 {
1351 int i;
1352
1353 for (i = 0; i < argcount; ++i)
1354 {
1355 type_T *expected;
1356 type_T *actual;
1357
1358 if (i < regular_args)
1359 {
1360 if (ufunc->uf_arg_types == NULL)
1361 continue;
1362 expected = ufunc->uf_arg_types[i];
1363 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001364 else if (ufunc->uf_va_type == NULL)
1365 // possibly a lambda
1366 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001367 else
1368 expected = ufunc->uf_va_type->tt_member;
1369 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001370 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001371 {
1372 arg_type_mismatch(expected, actual, i + 1);
1373 return FAIL;
1374 }
1375 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001376 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001377 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1378 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001379 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001380 }
1381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001383 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001384 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001386 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 {
1388 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1389 isn->isn_arg.dfunc.cdf_argcount = argcount;
1390 }
1391 else
1392 {
1393 // A user function may be deleted and redefined later, can't use the
1394 // ufunc pointer, need to look it up again at runtime.
1395 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1396 isn->isn_arg.ufunc.cuf_argcount = argcount;
1397 }
1398
1399 stack->ga_len -= argcount; // drop the arguments
1400 if (ga_grow(stack, 1) == FAIL)
1401 return FAIL;
1402 // add return value
1403 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1404 ++stack->ga_len;
1405
1406 return OK;
1407}
1408
1409/*
1410 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1411 */
1412 static int
1413generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1414{
1415 isn_T *isn;
1416 garray_T *stack = &cctx->ctx_type_stack;
1417
Bram Moolenaar080457c2020-03-03 21:53:32 +01001418 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1420 return FAIL;
1421 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1422 isn->isn_arg.ufunc.cuf_argcount = argcount;
1423
1424 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001425 if (ga_grow(stack, 1) == FAIL)
1426 return FAIL;
1427 // add return value
1428 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1429 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430
1431 return OK;
1432}
1433
1434/*
1435 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001436 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 */
1438 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001439generate_PCALL(
1440 cctx_T *cctx,
1441 int argcount,
1442 char_u *name,
1443 type_T *type,
1444 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445{
1446 isn_T *isn;
1447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001448 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449
Bram Moolenaar080457c2020-03-03 21:53:32 +01001450 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001451
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001452 if (type->tt_type == VAR_ANY)
1453 ret_type = &t_any;
1454 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001455 {
1456 if (type->tt_argcount != -1)
1457 {
1458 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1459
1460 if (argcount < type->tt_min_argcount - varargs)
1461 {
1462 semsg(_(e_toofewarg), "[reference]");
1463 return FAIL;
1464 }
1465 if (!varargs && argcount > type->tt_argcount)
1466 {
1467 semsg(_(e_toomanyarg), "[reference]");
1468 return FAIL;
1469 }
1470 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001471 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001472 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001473 else
1474 {
1475 semsg(_("E1085: Not a callable type: %s"), name);
1476 return FAIL;
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.pfunc.cpf_top = at_top;
1482 isn->isn_arg.pfunc.cpf_argcount = argcount;
1483
1484 stack->ga_len -= argcount; // drop the arguments
1485
1486 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001487 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001489 // If partial is above the arguments it must be cleared and replaced with
1490 // the return value.
1491 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1492 return FAIL;
1493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 return OK;
1495}
1496
1497/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001498 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 */
1500 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001501generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502{
1503 isn_T *isn;
1504 garray_T *stack = &cctx->ctx_type_stack;
1505 type_T *type;
1506
Bram Moolenaar080457c2020-03-03 21:53:32 +01001507 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001508 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001510 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001512 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001514 if (type->tt_type != VAR_DICT && type != &t_any)
1515 {
1516 emsg(_(e_dictreq));
1517 return FAIL;
1518 }
1519 // change dict type to dict member type
1520 if (type->tt_type == VAR_DICT)
1521 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 return OK;
1524}
1525
1526/*
1527 * Generate an ISN_ECHO instruction.
1528 */
1529 static int
1530generate_ECHO(cctx_T *cctx, int with_white, int count)
1531{
1532 isn_T *isn;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1536 return FAIL;
1537 isn->isn_arg.echo.echo_with_white = with_white;
1538 isn->isn_arg.echo.echo_count = count;
1539
1540 return OK;
1541}
1542
Bram Moolenaarad39c092020-02-26 18:23:43 +01001543/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545 */
1546 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001547generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001548{
1549 isn_T *isn;
1550
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001551 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001552 return FAIL;
1553 isn->isn_arg.number = count;
1554
1555 return OK;
1556}
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 static int
1559generate_EXEC(cctx_T *cctx, char_u *line)
1560{
1561 isn_T *isn;
1562
Bram Moolenaar080457c2020-03-03 21:53:32 +01001563 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1565 return FAIL;
1566 isn->isn_arg.string = vim_strsave(line);
1567 return OK;
1568}
1569
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001570 static int
1571generate_EXECCONCAT(cctx_T *cctx, int count)
1572{
1573 isn_T *isn;
1574
1575 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1576 return FAIL;
1577 isn->isn_arg.number = count;
1578 return OK;
1579}
1580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581/*
1582 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001583 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001585 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1587{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 lvar_T *lvar;
1589
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001590 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001592 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001593 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 }
1595
1596 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 return NULL;
1598 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001600 // Every local variable uses the next entry on the stack. We could re-use
1601 // the last ones when leaving a scope, but then variables used in a closure
1602 // might get overwritten. To keep things simple do not re-use stack
1603 // entries. This is less efficient, but memory is cheap these days.
1604 lvar->lv_idx = cctx->ctx_locals_count++;
1605
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001606 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 lvar->lv_const = isConst;
1608 lvar->lv_type = type;
1609
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001610 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611}
1612
1613/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001614 * Remove local variables above "new_top".
1615 */
1616 static void
1617unwind_locals(cctx_T *cctx, int new_top)
1618{
1619 if (cctx->ctx_locals.ga_len > new_top)
1620 {
1621 int idx;
1622 lvar_T *lvar;
1623
1624 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1625 {
1626 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1627 vim_free(lvar->lv_name);
1628 }
1629 }
1630 cctx->ctx_locals.ga_len = new_top;
1631}
1632
1633/*
1634 * Free all local variables.
1635 */
1636 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001637free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001638{
1639 unwind_locals(cctx, 0);
1640 ga_clear(&cctx->ctx_locals);
1641}
1642
1643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 * Find "name" in script-local items of script "sid".
1645 * Returns the index in "sn_var_vals" if found.
1646 * If found but not in "sn_var_vals" returns -1.
1647 * If not found returns -2.
1648 */
1649 int
1650get_script_item_idx(int sid, char_u *name, int check_writable)
1651{
1652 hashtab_T *ht;
1653 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001654 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 int idx;
1656
1657 // First look the name up in the hashtable.
1658 if (sid <= 0 || sid > script_items.ga_len)
1659 return -1;
1660 ht = &SCRIPT_VARS(sid);
1661 di = find_var_in_ht(ht, 0, name, TRUE);
1662 if (di == NULL)
1663 return -2;
1664
1665 // Now find the svar_T index in sn_var_vals.
1666 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1667 {
1668 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1669
1670 if (sv->sv_tv == &di->di_tv)
1671 {
1672 if (check_writable && sv->sv_const)
1673 semsg(_(e_readonlyvar), name);
1674 return idx;
1675 }
1676 }
1677 return -1;
1678}
1679
1680/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001681 * Find "name" in imported items of the current script or in "cctx" if not
1682 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 */
1684 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001685find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 int idx;
1688
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001689 if (current_sctx.sc_sid <= 0)
1690 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 if (cctx != NULL)
1692 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1693 {
1694 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1695 + idx;
1696
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001697 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1698 : STRLEN(import->imp_name) == len
1699 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 return import;
1701 }
1702
Bram Moolenaarefa94442020-08-08 22:16:00 +02001703 return find_imported_in_script(name, len, current_sctx.sc_sid);
1704}
1705
1706 imported_T *
1707find_imported_in_script(char_u *name, size_t len, int sid)
1708{
1709 scriptitem_T *si = SCRIPT_ITEM(sid);
1710 int idx;
1711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1713 {
1714 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1715
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001716 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1717 : STRLEN(import->imp_name) == len
1718 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 return import;
1720 }
1721 return NULL;
1722}
1723
1724/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001725 * Free all imported variables.
1726 */
1727 static void
1728free_imported(cctx_T *cctx)
1729{
1730 int idx;
1731
1732 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1733 {
1734 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1735
1736 vim_free(import->imp_name);
1737 }
1738 ga_clear(&cctx->ctx_imports);
1739}
1740
1741/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001742 * Return TRUE if "p" points at a "#" but not at "#{".
1743 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001744 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001745vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001746{
1747 return p[0] == '#' && p[1] != '{';
1748}
1749
1750/*
1751 * Return a pointer to the next line that isn't empty or only contains a
1752 * comment. Skips over white space.
1753 * Returns NULL if there is none.
1754 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001755 char_u *
1756peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001757{
1758 int lnum = cctx->ctx_lnum;
1759
1760 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1761 {
1762 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001763 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001764
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001765 // ignore NULLs inserted for continuation lines
1766 if (line != NULL)
1767 {
1768 p = skipwhite(line);
1769 if (*p != NUL && !vim9_comment_start(p))
1770 return p;
1771 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001772 }
1773 return NULL;
1774}
1775
1776/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001777 * Called when checking for a following operator at "arg". When the rest of
1778 * the line is empty or only a comment, peek the next line. If there is a next
1779 * line return a pointer to it and set "nextp".
1780 * Otherwise skip over white space.
1781 */
1782 static char_u *
1783may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1784{
1785 char_u *p = skipwhite(arg);
1786
1787 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001788 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001789 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001790 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001791 if (*nextp != NULL)
1792 return *nextp;
1793 }
1794 return p;
1795}
1796
1797/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001798 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001799 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001800 * Returns NULL when at the end.
1801 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001802 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001803next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001804{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001805 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001806
1807 do
1808 {
1809 ++cctx->ctx_lnum;
1810 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001811 {
1812 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001814 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001815 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001816 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001817 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001818 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001819 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001820 return line;
1821}
1822
1823/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001824 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001825 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001826 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1827 */
1828 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001829may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001830{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001831 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001832 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001833 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001834
1835 if (next == NULL)
1836 return FAIL;
1837 *arg = skipwhite(next);
1838 }
1839 return OK;
1840}
1841
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001842/*
1843 * Idem, and give an error when failed.
1844 */
1845 static int
1846may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1847{
1848 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1849 {
1850 emsg(_("E1097: line incomplete"));
1851 return FAIL;
1852 }
1853 return OK;
1854}
1855
1856
Bram Moolenaara5565e42020-05-09 15:44:01 +02001857// Structure passed between the compile_expr* functions to keep track of
1858// constants that have been parsed but for which no code was produced yet. If
1859// possible expressions on these constants are applied at compile time. If
1860// that is not possible, the code to push the constants needs to be generated
1861// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001862// Using 50 should be more than enough of 5 levels of ().
1863#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001864typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001865 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001866 int pp_used; // active entries in pp_tv[]
1867} ppconst_T;
1868
Bram Moolenaar1c747212020-05-09 18:28:34 +02001869static int compile_expr0(char_u **arg, cctx_T *cctx);
1870static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1871
Bram Moolenaara5565e42020-05-09 15:44:01 +02001872/*
1873 * Generate a PUSH instruction for "tv".
1874 * "tv" will be consumed or cleared.
1875 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1876 */
1877 static int
1878generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1879{
1880 if (tv != NULL)
1881 {
1882 switch (tv->v_type)
1883 {
1884 case VAR_UNKNOWN:
1885 break;
1886 case VAR_BOOL:
1887 generate_PUSHBOOL(cctx, tv->vval.v_number);
1888 break;
1889 case VAR_SPECIAL:
1890 generate_PUSHSPEC(cctx, tv->vval.v_number);
1891 break;
1892 case VAR_NUMBER:
1893 generate_PUSHNR(cctx, tv->vval.v_number);
1894 break;
1895#ifdef FEAT_FLOAT
1896 case VAR_FLOAT:
1897 generate_PUSHF(cctx, tv->vval.v_float);
1898 break;
1899#endif
1900 case VAR_BLOB:
1901 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1902 tv->vval.v_blob = NULL;
1903 break;
1904 case VAR_STRING:
1905 generate_PUSHS(cctx, tv->vval.v_string);
1906 tv->vval.v_string = NULL;
1907 break;
1908 default:
1909 iemsg("constant type not supported");
1910 clear_tv(tv);
1911 return FAIL;
1912 }
1913 tv->v_type = VAR_UNKNOWN;
1914 }
1915 return OK;
1916}
1917
1918/*
1919 * Generate code for any ppconst entries.
1920 */
1921 static int
1922generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1923{
1924 int i;
1925 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001926 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001927
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001928 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001929 for (i = 0; i < ppconst->pp_used; ++i)
1930 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1931 ret = FAIL;
1932 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001933 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001934 return ret;
1935}
1936
1937/*
1938 * Clear ppconst constants. Used when failing.
1939 */
1940 static void
1941clear_ppconst(ppconst_T *ppconst)
1942{
1943 int i;
1944
1945 for (i = 0; i < ppconst->pp_used; ++i)
1946 clear_tv(&ppconst->pp_tv[i]);
1947 ppconst->pp_used = 0;
1948}
1949
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001950/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001951 * Generate an instruction to load script-local variable "name", without the
1952 * leading "s:".
1953 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 */
1955 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001956compile_load_scriptvar(
1957 cctx_T *cctx,
1958 char_u *name, // variable NUL terminated
1959 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001960 char_u **end, // end of variable
1961 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001963 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1965 imported_T *import;
1966
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001967 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001969 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001970 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1971 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 }
1973 if (idx >= 0)
1974 {
1975 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1976
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001977 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 current_sctx.sc_sid, idx, sv->sv_type);
1979 return OK;
1980 }
1981
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001982 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 if (import != NULL)
1984 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001985 if (import->imp_all)
1986 {
1987 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001988 char_u *exp_name;
1989 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001990 ufunc_T *ufunc;
1991 type_T *type;
1992
1993 // Used "import * as Name", need to lookup the member.
1994 if (*p != '.')
1995 {
1996 semsg(_("E1060: expected dot after name: %s"), start);
1997 return FAIL;
1998 }
1999 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002000 if (VIM_ISWHITE(*p))
2001 {
2002 emsg(_("E1074: no white space allowed after dot"));
2003 return FAIL;
2004 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002005
Bram Moolenaar1c991142020-07-04 13:15:31 +02002006 // isolate one name
2007 exp_name = p;
2008 while (eval_isnamec(*p))
2009 ++p;
2010 cc = *p;
2011 *p = NUL;
2012
2013 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2014 *p = cc;
2015 p = skipwhite(p);
2016
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002017 // TODO: what if it is a function?
2018 if (idx < 0)
2019 return FAIL;
2020 *end = p;
2021
2022 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2023 import->imp_sid,
2024 idx,
2025 type);
2026 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002027 else if (import->imp_funcname != NULL)
2028 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002029 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2031 import->imp_sid,
2032 import->imp_var_vals_idx,
2033 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 return OK;
2035 }
2036
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002037 if (error)
2038 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 return FAIL;
2040}
2041
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002042 static int
2043generate_funcref(cctx_T *cctx, char_u *name)
2044{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002045 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002046
2047 if (ufunc == NULL)
2048 return FAIL;
2049
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002050 // Need to compile any default values to get the argument types.
2051 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2052 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2053 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002054 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002055}
2056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057/*
2058 * Compile a variable name into a load instruction.
2059 * "end" points to just after the name.
2060 * When "error" is FALSE do not give an error when not found.
2061 */
2062 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002063compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064{
2065 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002066 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002067 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002069 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070
2071 if (*(*arg + 1) == ':')
2072 {
2073 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002074 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002075 {
2076 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002078 switch (**arg)
2079 {
2080 case 'g': isn_type = ISN_LOADGDICT; break;
2081 case 'w': isn_type = ISN_LOADWDICT; break;
2082 case 't': isn_type = ISN_LOADTDICT; break;
2083 case 'b': isn_type = ISN_LOADBDICT; break;
2084 default:
2085 semsg(_(e_namespace), *arg);
2086 goto theend;
2087 }
2088 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2089 goto theend;
2090 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 else
2093 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002094 isntype_T isn_type = ISN_DROP;
2095
2096 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2097 if (name == NULL)
2098 return FAIL;
2099
2100 switch (**arg)
2101 {
2102 case 'v': res = generate_LOADV(cctx, name, error);
2103 break;
2104 case 's': res = compile_load_scriptvar(cctx, name,
2105 NULL, NULL, error);
2106 break;
2107 case 'g': isn_type = ISN_LOADG; break;
2108 case 'w': isn_type = ISN_LOADW; break;
2109 case 't': isn_type = ISN_LOADT; break;
2110 case 'b': isn_type = ISN_LOADB; break;
2111 default: semsg(_(e_namespace), *arg);
2112 goto theend;
2113 }
2114 if (isn_type != ISN_DROP)
2115 {
2116 // Global, Buffer-local, Window-local and Tabpage-local
2117 // variables can be defined later, thus we don't check if it
2118 // exists, give error at runtime.
2119 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122 }
2123 else
2124 {
2125 size_t len = end - *arg;
2126 int idx;
2127 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002128 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129
2130 name = vim_strnsave(*arg, end - *arg);
2131 if (name == NULL)
2132 return FAIL;
2133
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002134 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002136 if (!gen_load_outer)
2137 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 }
2139 else
2140 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002141 lvar_T *lvar = lookup_local(*arg, len, cctx);
2142
2143 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002145 type = lvar->lv_type;
2146 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002147 if (lvar->lv_from_outer)
2148 gen_load_outer = TRUE;
2149 else
2150 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 }
2152 else
2153 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002154 // "var" can be script-local even without using "s:" if it
2155 // already exists.
2156 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2157 == SCRIPT_VERSION_VIM9
2158 || lookup_script(*arg, len) == OK)
2159 res = compile_load_scriptvar(cctx, name, *arg, &end,
2160 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161
Bram Moolenaara5565e42020-05-09 15:44:01 +02002162 // When the name starts with an uppercase letter or "x:" it
2163 // can be a user defined function.
2164 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2165 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 }
2167 }
2168 if (gen_load)
2169 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002170 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002171 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002172 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002173 cctx->ctx_outer_used = TRUE;
2174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 }
2176
2177 *arg = end;
2178
2179theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002180 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 semsg(_(e_var_notfound), name);
2182 vim_free(name);
2183 return res;
2184}
2185
2186/*
2187 * Compile the argument expressions.
2188 * "arg" points to just after the "(" and is advanced to after the ")"
2189 */
2190 static int
2191compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2192{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002193 char_u *p = *arg;
2194 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195
Bram Moolenaare6085c52020-04-12 20:19:16 +02002196 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002198 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2199 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002200 if (*p == ')')
2201 {
2202 *arg = p + 1;
2203 return OK;
2204 }
2205
Bram Moolenaara5565e42020-05-09 15:44:01 +02002206 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 return FAIL;
2208 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002209
2210 if (*p != ',' && *skipwhite(p) == ',')
2211 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002212 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 p = skipwhite(p);
2214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002216 {
2217 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002218 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002219 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002220 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002221 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002222 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002224failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002225 emsg(_(e_missing_close));
2226 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227}
2228
2229/*
2230 * Compile a function call: name(arg1, arg2)
2231 * "arg" points to "name", "arg + varlen" to the "(".
2232 * "argcount_init" is 1 for "value->method()"
2233 * Instructions:
2234 * EVAL arg1
2235 * EVAL arg2
2236 * BCALL / DCALL / UCALL
2237 */
2238 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002239compile_call(
2240 char_u **arg,
2241 size_t varlen,
2242 cctx_T *cctx,
2243 ppconst_T *ppconst,
2244 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
2246 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002247 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 int argcount = argcount_init;
2249 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002250 char_u fname_buf[FLEN_FIXED + 1];
2251 char_u *tofree = NULL;
2252 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002254 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002255 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
Bram Moolenaara5565e42020-05-09 15:44:01 +02002257 // we can evaluate "has('name')" at compile time
2258 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2259 {
2260 char_u *s = skipwhite(*arg + varlen + 1);
2261 typval_T argvars[2];
2262
2263 argvars[0].v_type = VAR_UNKNOWN;
2264 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002265 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002266 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002267 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002268 s = skipwhite(s);
2269 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2270 {
2271 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2272
2273 *arg = s + 1;
2274 argvars[1].v_type = VAR_UNKNOWN;
2275 tv->v_type = VAR_NUMBER;
2276 tv->vval.v_number = 0;
2277 f_has(argvars, tv);
2278 clear_tv(&argvars[0]);
2279 ++ppconst->pp_used;
2280 return OK;
2281 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002282 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002283 }
2284
2285 if (generate_ppconst(cctx, ppconst) == FAIL)
2286 return FAIL;
2287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 if (varlen >= sizeof(namebuf))
2289 {
2290 semsg(_("E1011: name too long: %s"), name);
2291 return FAIL;
2292 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002293 vim_strncpy(namebuf, *arg, varlen);
2294 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295
2296 *arg = skipwhite(*arg + varlen + 1);
2297 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
Bram Moolenaara1773442020-08-12 15:21:22 +02002300 is_autoload = vim_strchr(name, '#') != NULL;
2301 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 {
2303 int idx;
2304
2305 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002306 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002308 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002309 else
2310 semsg(_(e_unknownfunc), namebuf);
2311 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 }
2313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002315 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002317 {
2318 res = generate_CALL(cctx, ufunc, argcount);
2319 goto theend;
2320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321
2322 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002323 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002324 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002326 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002327 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002328 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002329 garray_T *stack = &cctx->ctx_type_stack;
2330 type_T *type;
2331
2332 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2333 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002334 goto theend;
2335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002337 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002338 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002339 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002340 res = generate_UCALL(cctx, name, argcount);
2341 else
2342 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002343
2344theend:
2345 vim_free(tofree);
2346 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347}
2348
2349// like NAMESPACE_CHAR but with 'a' and 'l'.
2350#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2351
2352/*
2353 * Find the end of a variable or function name. Unlike find_name_end() this
2354 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002355 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 * Return a pointer to just after the name. Equal to "arg" if there is no
2357 * valid name.
2358 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002359 static char_u *
2360to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361{
2362 char_u *p;
2363
2364 // Quick check for valid starting character.
2365 if (!eval_isnamec1(*arg))
2366 return arg;
2367
2368 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2369 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2370 // and can be used in slice "[n:]".
2371 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002372 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2374 break;
2375 return p;
2376}
2377
2378/*
2379 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002380 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
2382 char_u *
2383to_name_const_end(char_u *arg)
2384{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002385 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 typval_T rettv;
2387
2388 if (p == arg && *arg == '[')
2389 {
2390
2391 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002392 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 p = arg;
2394 }
2395 else if (p == arg && *arg == '#' && arg[1] == '{')
2396 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002397 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002399 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 p = arg;
2401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402
2403 return p;
2404}
2405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406/*
2407 * parse a list: [expr, expr]
2408 * "*arg" points to the '['.
2409 */
2410 static int
2411compile_list(char_u **arg, cctx_T *cctx)
2412{
2413 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002414 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 int count = 0;
2416
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002417 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002419 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002420 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002421 semsg(_(e_list_end), *arg);
2422 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002423 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002424 if (*p == ',')
2425 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002426 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002427 return FAIL;
2428 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002429 if (*p == ']')
2430 {
2431 ++p;
2432 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002433 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002434 p += STRLEN(p);
2435 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002436 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002437 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 break;
2439 ++count;
2440 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002441 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002443 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2444 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002445 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002446 return FAIL;
2447 }
2448 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002449 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 p = skipwhite(p);
2451 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002452 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453
2454 generate_NEWLIST(cctx, count);
2455 return OK;
2456}
2457
2458/*
2459 * parse a lambda: {arg, arg -> expr}
2460 * "*arg" points to the '{'.
2461 */
2462 static int
2463compile_lambda(char_u **arg, cctx_T *cctx)
2464{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 typval_T rettv;
2466 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002467 evalarg_T evalarg;
2468
2469 CLEAR_FIELD(evalarg);
2470 evalarg.eval_flags = EVAL_EVALUATE;
2471 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
2473 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002474 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002478 ++ufunc->uf_refcount;
2479 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002480 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481
2482 // The function will have one line: "return {expr}".
2483 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002484 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002486 clear_evalarg(&evalarg, NULL);
2487
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002488 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002489 {
2490 // The return type will now be known.
2491 set_function_type(ufunc);
2492
2493 return generate_FUNCREF(cctx, ufunc);
2494 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002495
2496 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 return FAIL;
2498}
2499
2500/*
2501 * Compile a lamda call: expr->{lambda}(args)
2502 * "arg" points to the "{".
2503 */
2504 static int
2505compile_lambda_call(char_u **arg, cctx_T *cctx)
2506{
2507 ufunc_T *ufunc;
2508 typval_T rettv;
2509 int argcount = 1;
2510 int ret = FAIL;
2511
2512 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002513 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 return FAIL;
2515
2516 if (**arg != '(')
2517 {
2518 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002519 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 else
2521 semsg(_(e_missing_paren), "lambda");
2522 clear_tv(&rettv);
2523 return FAIL;
2524 }
2525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 ufunc = rettv.vval.v_partial->pt_func;
2527 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002528 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002529 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002530
2531 // The function will have one line: "return {expr}".
2532 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002533 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534
2535 // compile the arguments
2536 *arg = skipwhite(*arg + 1);
2537 if (compile_arguments(arg, cctx, &argcount) == OK)
2538 // call the compiled function
2539 ret = generate_CALL(cctx, ufunc, argcount);
2540
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002541 if (ret == FAIL)
2542 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 return ret;
2544}
2545
2546/*
2547 * parse a dict: {'key': val} or #{key: val}
2548 * "*arg" points to the '{'.
2549 */
2550 static int
2551compile_dict(char_u **arg, cctx_T *cctx, int literal)
2552{
2553 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 int count = 0;
2556 dict_T *d = dict_alloc();
2557 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002558 char_u *whitep = *arg;
2559 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560
2561 if (d == NULL)
2562 return FAIL;
2563 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002564 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 {
2566 char_u *key = NULL;
2567
Bram Moolenaar23c55272020-06-21 16:58:13 +02002568 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002569 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 *arg = NULL;
2571 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002572 }
2573
2574 if (**arg == '}')
2575 break;
2576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (literal)
2578 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002579 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 {
2583 semsg(_("E1014: Invalid key: %s"), *arg);
2584 return FAIL;
2585 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002586 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (generate_PUSHS(cctx, key) == FAIL)
2588 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002589 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 }
2591 else
2592 {
2593 isn_T *isn;
2594
Bram Moolenaara5565e42020-05-09 15:44:01 +02002595 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2598 if (isn->isn_type == ISN_PUSHS)
2599 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002600 else
2601 {
2602 type_T *keytype = ((type_T **)stack->ga_data)
2603 [stack->ga_len - 1];
2604 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2605 return FAIL;
2606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 }
2608
2609 // Check for duplicate keys, if using string keys.
2610 if (key != NULL)
2611 {
2612 item = dict_find(d, key, -1);
2613 if (item != NULL)
2614 {
2615 semsg(_(e_duplicate_key), key);
2616 goto failret;
2617 }
2618 item = dictitem_alloc(key);
2619 if (item != NULL)
2620 {
2621 item->di_tv.v_type = VAR_UNKNOWN;
2622 item->di_tv.v_lock = 0;
2623 if (dict_add(d, item) == FAIL)
2624 dictitem_free(item);
2625 }
2626 }
2627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (**arg != ':')
2629 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002630 if (*skipwhite(*arg) == ':')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002631 semsg(_(e_no_white_space_allowed_before), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002632 else
2633 semsg(_(e_missing_dict_colon), *arg);
2634 return FAIL;
2635 }
2636 whitep = *arg + 1;
2637 if (!IS_WHITE_OR_NUL(*whitep))
2638 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002639 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 return FAIL;
2641 }
2642
2643 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002644 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002645 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002646 *arg = NULL;
2647 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002648 }
2649
Bram Moolenaara5565e42020-05-09 15:44:01 +02002650 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 return FAIL;
2652 ++count;
2653
Bram Moolenaar2c330432020-04-13 14:41:35 +02002654 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655 *arg = skipwhite(*arg);
2656 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658 *arg = NULL;
2659 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 if (**arg == '}')
2662 break;
2663 if (**arg != ',')
2664 {
2665 semsg(_(e_missing_dict_comma), *arg);
2666 goto failret;
2667 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002668 if (IS_WHITE_OR_NUL(*whitep))
2669 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002670 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002671 return FAIL;
2672 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002673 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 *arg = skipwhite(*arg + 1);
2675 }
2676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 *arg = *arg + 1;
2678
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002679 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002680 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002681 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002682 *arg += STRLEN(*arg);
2683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 dict_unref(d);
2685 return generate_NEWDICT(cctx, count);
2686
2687failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002688 if (*arg == NULL)
2689 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 dict_unref(d);
2691 return FAIL;
2692}
2693
2694/*
2695 * Compile "&option".
2696 */
2697 static int
2698compile_get_option(char_u **arg, cctx_T *cctx)
2699{
2700 typval_T rettv;
2701 char_u *start = *arg;
2702 int ret;
2703
2704 // parse the option and get the current value to get the type.
2705 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002706 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 if (ret == OK)
2708 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002709 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 char_u *name = vim_strnsave(start, *arg - start);
2711 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2712
2713 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2714 vim_free(name);
2715 }
2716 clear_tv(&rettv);
2717
2718 return ret;
2719}
2720
2721/*
2722 * Compile "$VAR".
2723 */
2724 static int
2725compile_get_env(char_u **arg, cctx_T *cctx)
2726{
2727 char_u *start = *arg;
2728 int len;
2729 int ret;
2730 char_u *name;
2731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 ++*arg;
2733 len = get_env_len(arg);
2734 if (len == 0)
2735 {
2736 semsg(_(e_syntax_at), start - 1);
2737 return FAIL;
2738 }
2739
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002740 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 name = vim_strnsave(start, len + 1);
2742 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2743 vim_free(name);
2744 return ret;
2745}
2746
2747/*
2748 * Compile "@r".
2749 */
2750 static int
2751compile_get_register(char_u **arg, cctx_T *cctx)
2752{
2753 int ret;
2754
2755 ++*arg;
2756 if (**arg == NUL)
2757 {
2758 semsg(_(e_syntax_at), *arg - 1);
2759 return FAIL;
2760 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002761 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
2763 emsg_invreg(**arg);
2764 return FAIL;
2765 }
2766 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2767 ++*arg;
2768 return ret;
2769}
2770
2771/*
2772 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002773 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 */
2775 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002776apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002778 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779
2780 // this works from end to start
2781 while (p > start)
2782 {
2783 --p;
2784 if (*p == '-' || *p == '+')
2785 {
2786 // only '-' has an effect, for '+' we only check the type
2787#ifdef FEAT_FLOAT
2788 if (rettv->v_type == VAR_FLOAT)
2789 {
2790 if (*p == '-')
2791 rettv->vval.v_float = -rettv->vval.v_float;
2792 }
2793 else
2794#endif
2795 {
2796 varnumber_T val;
2797 int error = FALSE;
2798
2799 // tv_get_number_chk() accepts a string, but we don't want that
2800 // here
2801 if (check_not_string(rettv) == FAIL)
2802 return FAIL;
2803 val = tv_get_number_chk(rettv, &error);
2804 clear_tv(rettv);
2805 if (error)
2806 return FAIL;
2807 if (*p == '-')
2808 val = -val;
2809 rettv->v_type = VAR_NUMBER;
2810 rettv->vval.v_number = val;
2811 }
2812 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002813 else if (numeric_only)
2814 {
2815 ++p;
2816 break;
2817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 else
2819 {
2820 int v = tv2bool(rettv);
2821
2822 // '!' is permissive in the type.
2823 clear_tv(rettv);
2824 rettv->v_type = VAR_BOOL;
2825 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2826 }
2827 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002828 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 return OK;
2830}
2831
2832/*
2833 * Recognize v: variables that are constants and set "rettv".
2834 */
2835 static void
2836get_vim_constant(char_u **arg, typval_T *rettv)
2837{
2838 if (STRNCMP(*arg, "v:true", 6) == 0)
2839 {
2840 rettv->v_type = VAR_BOOL;
2841 rettv->vval.v_number = VVAL_TRUE;
2842 *arg += 6;
2843 }
2844 else if (STRNCMP(*arg, "v:false", 7) == 0)
2845 {
2846 rettv->v_type = VAR_BOOL;
2847 rettv->vval.v_number = VVAL_FALSE;
2848 *arg += 7;
2849 }
2850 else if (STRNCMP(*arg, "v:null", 6) == 0)
2851 {
2852 rettv->v_type = VAR_SPECIAL;
2853 rettv->vval.v_number = VVAL_NULL;
2854 *arg += 6;
2855 }
2856 else if (STRNCMP(*arg, "v:none", 6) == 0)
2857 {
2858 rettv->v_type = VAR_SPECIAL;
2859 rettv->vval.v_number = VVAL_NONE;
2860 *arg += 6;
2861 }
2862}
2863
Bram Moolenaar696ba232020-07-29 21:20:41 +02002864 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002865get_compare_type(char_u *p, int *len, int *type_is)
2866{
2867 exptype_T type = EXPR_UNKNOWN;
2868 int i;
2869
2870 switch (p[0])
2871 {
2872 case '=': if (p[1] == '=')
2873 type = EXPR_EQUAL;
2874 else if (p[1] == '~')
2875 type = EXPR_MATCH;
2876 break;
2877 case '!': if (p[1] == '=')
2878 type = EXPR_NEQUAL;
2879 else if (p[1] == '~')
2880 type = EXPR_NOMATCH;
2881 break;
2882 case '>': if (p[1] != '=')
2883 {
2884 type = EXPR_GREATER;
2885 *len = 1;
2886 }
2887 else
2888 type = EXPR_GEQUAL;
2889 break;
2890 case '<': if (p[1] != '=')
2891 {
2892 type = EXPR_SMALLER;
2893 *len = 1;
2894 }
2895 else
2896 type = EXPR_SEQUAL;
2897 break;
2898 case 'i': if (p[1] == 's')
2899 {
2900 // "is" and "isnot"; but not a prefix of a name
2901 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2902 *len = 5;
2903 i = p[*len];
2904 if (!isalnum(i) && i != '_')
2905 {
2906 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2907 *type_is = TRUE;
2908 }
2909 }
2910 break;
2911 }
2912 return type;
2913}
2914
2915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002917 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 */
2919 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002920compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002922 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
2924 // this works from end to start
2925 while (p > start)
2926 {
2927 --p;
2928 if (*p == '-' || *p == '+')
2929 {
2930 int negate = *p == '-';
2931 isn_T *isn;
2932
2933 // TODO: check type
2934 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2935 {
2936 --p;
2937 if (*p == '-')
2938 negate = !negate;
2939 }
2940 // only '-' has an effect, for '+' we only check the type
2941 if (negate)
2942 isn = generate_instr(cctx, ISN_NEGATENR);
2943 else
2944 isn = generate_instr(cctx, ISN_CHECKNR);
2945 if (isn == NULL)
2946 return FAIL;
2947 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002948 else if (numeric_only)
2949 {
2950 ++p;
2951 break;
2952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 else
2954 {
2955 int invert = TRUE;
2956
2957 while (p > start && p[-1] == '!')
2958 {
2959 --p;
2960 invert = !invert;
2961 }
2962 if (generate_2BOOL(cctx, invert) == FAIL)
2963 return FAIL;
2964 }
2965 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002966 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 return OK;
2968}
2969
2970/*
2971 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002972 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 */
2974 static int
2975compile_subscript(
2976 char_u **arg,
2977 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002978 char_u *start_leader,
2979 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002980 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002982 char_u *name_start = *end_leader;
2983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 for (;;)
2985 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002986 char_u *p = skipwhite(*arg);
2987
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002988 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002989 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002990 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002991
2992 // If a following line starts with "->{" or "->X" advance to that
2993 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002994 // Also if a following line starts with ".x".
2995 if (next != NULL &&
2996 ((next[0] == '-' && next[1] == '>'
2997 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002998 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002999 {
3000 next = next_line_from_context(cctx, TRUE);
3001 if (next == NULL)
3002 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003003 *arg = next;
3004 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003005 }
3006 }
3007
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003008 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3009 // not a function call.
3010 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003012 garray_T *stack = &cctx->ctx_type_stack;
3013 type_T *type;
3014 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003016 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003017 return FAIL;
3018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003020 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3021
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003022 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3024 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003025 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 return FAIL;
3027 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003028 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003030 char_u *pstart = p;
3031
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003032 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003033 return FAIL;
3034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 // something->method()
3036 // Apply the '!', '-' and '+' first:
3037 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003038 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003041 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003042 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003043 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 if (**arg == '{')
3045 {
3046 // lambda call: list->{lambda}
3047 if (compile_lambda_call(arg, cctx) == FAIL)
3048 return FAIL;
3049 }
3050 else
3051 {
3052 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003053 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003054 if (!eval_isnamec1(*p))
3055 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003056 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003057 return FAIL;
3058 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003059 if (ASCII_ISALPHA(*p) && p[1] == ':')
3060 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003061 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 ;
3063 if (*p != '(')
3064 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003065 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003069 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 return FAIL;
3071 }
3072 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003073 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003075 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003076 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003077 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003078
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003079 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003080 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003081 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003082 // TODO: blob index
3083 // TODO: more arguments
3084 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003085 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003086 return FAIL;
3087
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003088 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003089 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003090 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003091 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003092 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 return FAIL;
3094
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003095 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 if (**arg != ']')
3098 {
3099 emsg(_(e_missbrac));
3100 return FAIL;
3101 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003102 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003104 // We can index a list and a dict. If we don't know the type
3105 // we can use the index value type.
3106 // TODO: If we don't know use an instruction to figure it out at
3107 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003108 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003109 vtype = (*typep)->tt_type;
3110 if (*typep == &t_any)
3111 {
3112 type_T *valtype = ((type_T **)stack->ga_data)
3113 [stack->ga_len - 1];
3114 if (valtype == &t_string)
3115 vtype = VAR_DICT;
3116 }
3117 if (vtype == VAR_DICT)
3118 {
3119 if ((*typep)->tt_type == VAR_DICT)
3120 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003121 else
3122 {
3123 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3124 return FAIL;
3125 *typep = &t_any;
3126 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003127 if (may_generate_2STRING(-1, cctx) == FAIL)
3128 return FAIL;
3129 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3130 return FAIL;
3131 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003132 else if (vtype == VAR_STRING)
3133 {
3134 *typep = &t_number;
3135 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3136 return FAIL;
3137 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003138 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003139 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 if ((*typep)->tt_type == VAR_LIST)
3141 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003142 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003143 return FAIL;
3144 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003145 else
3146 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003147 emsg(_(e_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003148 return FAIL;
3149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003151 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003153 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003154 return FAIL;
3155
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003156 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003157 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3158 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003160 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003161 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 while (eval_isnamec(*p))
3163 MB_PTR_ADV(p);
3164 if (p == *arg)
3165 {
3166 semsg(_(e_syntax_at), *arg);
3167 return FAIL;
3168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003169 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 return FAIL;
3171 *arg = p;
3172 }
3173 else
3174 break;
3175 }
3176
3177 // TODO - see handle_subscript():
3178 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3179 // Don't do this when "Func" is already a partial that was bound
3180 // explicitly (pt_auto is FALSE).
3181
3182 return OK;
3183}
3184
3185/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003186 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3187 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003189 * If the value is a constant "ppconst->pp_ret" will be set.
3190 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003191 *
3192 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 */
3194
3195/*
3196 * number number constant
3197 * 0zFFFFFFFF Blob constant
3198 * "string" string constant
3199 * 'string' literal string constant
3200 * &option-name option value
3201 * @r register contents
3202 * identifier variable value
3203 * function() function call
3204 * $VAR environment variable
3205 * (expression) nested expression
3206 * [expr, expr] List
3207 * {key: val, key: val} Dictionary
3208 * #{key: val, key: val} Dictionary with literal keys
3209 *
3210 * Also handle:
3211 * ! in front logical NOT
3212 * - in front unary minus
3213 * + in front unary plus (ignored)
3214 * trailing (arg) funcref/partial call
3215 * trailing [] subscript in String or List
3216 * trailing .name entry in Dictionary
3217 * trailing ->name() method call
3218 */
3219 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003220compile_expr7(
3221 char_u **arg,
3222 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003223 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 char_u *start_leader, *end_leader;
3226 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003227 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003228 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229
3230 /*
3231 * Skip '!', '-' and '+' characters. They are handled later.
3232 */
3233 start_leader = *arg;
3234 while (**arg == '!' || **arg == '-' || **arg == '+')
3235 *arg = skipwhite(*arg + 1);
3236 end_leader = *arg;
3237
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003238 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 switch (**arg)
3240 {
3241 /*
3242 * Number constant.
3243 */
3244 case '0': // also for blob starting with 0z
3245 case '1':
3246 case '2':
3247 case '3':
3248 case '4':
3249 case '5':
3250 case '6':
3251 case '7':
3252 case '8':
3253 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003254 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003256 // Apply "-" and "+" just before the number now, right to
3257 // left. Matters especially when "->" follows. Stops at
3258 // '!'.
3259 if (apply_leader(rettv, TRUE,
3260 start_leader, &end_leader) == FAIL)
3261 {
3262 clear_tv(rettv);
3263 return FAIL;
3264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 break;
3266
3267 /*
3268 * String constant: "string".
3269 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003270 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 return FAIL;
3272 break;
3273
3274 /*
3275 * Literal string constant: 'str''ing'.
3276 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003277 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
3279 break;
3280
3281 /*
3282 * Constant Vim variable.
3283 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003284 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 ret = NOTDONE;
3286 break;
3287
3288 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003289 * "true" constant
3290 */
3291 case 't': if (STRNCMP(*arg, "true", 4) == 0
3292 && !eval_isnamec((*arg)[4]))
3293 {
3294 *arg += 4;
3295 rettv->v_type = VAR_BOOL;
3296 rettv->vval.v_number = VVAL_TRUE;
3297 }
3298 else
3299 ret = NOTDONE;
3300 break;
3301
3302 /*
3303 * "false" constant
3304 */
3305 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3306 && !eval_isnamec((*arg)[5]))
3307 {
3308 *arg += 5;
3309 rettv->v_type = VAR_BOOL;
3310 rettv->vval.v_number = VVAL_FALSE;
3311 }
3312 else
3313 ret = NOTDONE;
3314 break;
3315
3316 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 * List: [expr, expr]
3318 */
3319 case '[': ret = compile_list(arg, cctx);
3320 break;
3321
3322 /*
3323 * Dictionary: #{key: val, key: val}
3324 */
3325 case '#': if ((*arg)[1] == '{')
3326 {
3327 ++*arg;
3328 ret = compile_dict(arg, cctx, TRUE);
3329 }
3330 else
3331 ret = NOTDONE;
3332 break;
3333
3334 /*
3335 * Lambda: {arg, arg -> expr}
3336 * Dictionary: {'key': val, 'key': val}
3337 */
3338 case '{': {
3339 char_u *start = skipwhite(*arg + 1);
3340
3341 // Find out what comes after the arguments.
3342 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003343 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 if (ret != FAIL && *start == '>')
3345 ret = compile_lambda(arg, cctx);
3346 else
3347 ret = compile_dict(arg, cctx, FALSE);
3348 }
3349 break;
3350
3351 /*
3352 * Option value: &name
3353 */
3354 case '&': ret = compile_get_option(arg, cctx);
3355 break;
3356
3357 /*
3358 * Environment variable: $VAR.
3359 */
3360 case '$': ret = compile_get_env(arg, cctx);
3361 break;
3362
3363 /*
3364 * Register contents: @r.
3365 */
3366 case '@': ret = compile_get_register(arg, cctx);
3367 break;
3368 /*
3369 * nested expression: (expression).
3370 */
3371 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003372
3373 // recursive!
3374 if (ppconst->pp_used <= PPSIZE - 10)
3375 {
3376 ret = compile_expr1(arg, cctx, ppconst);
3377 }
3378 else
3379 {
3380 // Not enough space in ppconst, flush constants.
3381 if (generate_ppconst(cctx, ppconst) == FAIL)
3382 return FAIL;
3383 ret = compile_expr0(arg, cctx);
3384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 *arg = skipwhite(*arg);
3386 if (**arg == ')')
3387 ++*arg;
3388 else if (ret == OK)
3389 {
3390 emsg(_(e_missing_close));
3391 ret = FAIL;
3392 }
3393 break;
3394
3395 default: ret = NOTDONE;
3396 break;
3397 }
3398 if (ret == FAIL)
3399 return FAIL;
3400
Bram Moolenaar1c747212020-05-09 18:28:34 +02003401 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003403 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003404 clear_tv(rettv);
3405 else
3406 // A constant expression can possibly be handled compile time,
3407 // return the value instead of generating code.
3408 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 }
3410 else if (ret == NOTDONE)
3411 {
3412 char_u *p;
3413 int r;
3414
3415 if (!eval_isnamec1(**arg))
3416 {
3417 semsg(_("E1015: Name expected: %s"), *arg);
3418 return FAIL;
3419 }
3420
3421 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003422 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003424 {
3425 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003428 {
3429 if (generate_ppconst(cctx, ppconst) == FAIL)
3430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (r == FAIL)
3434 return FAIL;
3435 }
3436
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003437 // Handle following "[]", ".member", etc.
3438 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003439 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003440 ppconst) == FAIL)
3441 return FAIL;
3442 if (ppconst->pp_used > 0)
3443 {
3444 // apply the '!', '-' and '+' before the constant
3445 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003446 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003447 return FAIL;
3448 return OK;
3449 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003450 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003452 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453}
3454
3455/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003456 * Give the "white on both sides" error, taking the operator from "p[len]".
3457 */
3458 void
3459error_white_both(char_u *op, int len)
3460{
3461 char_u buf[10];
3462
3463 vim_strncpy(buf, op, len);
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003464 semsg(_(e_white_space_required_before_and_after), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003465}
3466
3467/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003468 * <type>expr7: runtime type check / conversion
3469 */
3470 static int
3471compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3472{
3473 type_T *want_type = NULL;
3474
3475 // Recognize <type>
3476 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3477 {
3478 int called_emsg_before = called_emsg;
3479
3480 ++*arg;
3481 want_type = parse_type(arg, cctx->ctx_type_list);
3482 if (called_emsg != called_emsg_before)
3483 return FAIL;
3484
3485 if (**arg != '>')
3486 {
3487 if (*skipwhite(*arg) == '>')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003488 semsg(_(e_no_white_space_allowed_before), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003489 else
3490 emsg(_("E1104: Missing >"));
3491 return FAIL;
3492 }
3493 ++*arg;
3494 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3495 return FAIL;
3496 }
3497
3498 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3499 return FAIL;
3500
3501 if (want_type != NULL)
3502 {
3503 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003504 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003505
Bram Moolenaard1103582020-08-14 22:44:25 +02003506 generate_ppconst(cctx, ppconst);
3507 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003508 if (check_type(want_type, actual, FALSE) == FAIL)
3509 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003510 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3511 return FAIL;
3512 }
3513 }
3514
3515 return OK;
3516}
3517
3518/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 * * number multiplication
3520 * / number division
3521 * % number modulo
3522 */
3523 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003524compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525{
3526 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003527 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003528 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003530 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003531 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return FAIL;
3533
3534 /*
3535 * Repeat computing, until no "*", "/" or "%" is following.
3536 */
3537 for (;;)
3538 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003539 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 if (*op != '*' && *op != '/' && *op != '%')
3541 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003542 if (next != NULL)
3543 {
3544 *arg = next_line_from_context(cctx, TRUE);
3545 op = skipwhite(*arg);
3546 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003547
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003548 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003550 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 }
3553 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003554 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003555 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003557 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003558 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003560
3561 if (ppconst->pp_used == ppconst_used + 2
3562 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3563 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003564 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003565 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3566 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003567 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003569 // both are numbers: compute the result
3570 switch (*op)
3571 {
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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003574 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003575 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003576 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003577 break;
3578 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003579 tv1->vval.v_number = res;
3580 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003581 }
3582 else
3583 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003584 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003585 generate_two_op(cctx, op);
3586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 }
3588
3589 return OK;
3590}
3591
3592/*
3593 * + number addition
3594 * - number subtraction
3595 * .. string concatenation
3596 */
3597 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003598compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599{
3600 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003601 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003603 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003606 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 return FAIL;
3608
3609 /*
3610 * Repeat computing, until no "+", "-" or ".." is following.
3611 */
3612 for (;;)
3613 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003614 op = may_peek_next_line(cctx, *arg, &next);
3615 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 break;
3617 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003618 if (next != NULL)
3619 {
3620 *arg = next_line_from_context(cctx, TRUE);
3621 op = skipwhite(*arg);
3622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003624 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003626 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003627 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
3629
3630 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003631 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003632 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003634 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003635 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 return FAIL;
3637
Bram Moolenaara5565e42020-05-09 15:44:01 +02003638 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003639 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003640 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3641 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3642 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3643 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003645 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3646 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003647
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003648 // concat/subtract/add constant numbers
3649 if (*op == '+')
3650 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3651 else if (*op == '-')
3652 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3653 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003655 // concatenate constant strings
3656 char_u *s1 = tv1->vval.v_string;
3657 char_u *s2 = tv2->vval.v_string;
3658 size_t len1 = STRLEN(s1);
3659
3660 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3661 if (tv1->vval.v_string == NULL)
3662 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003663 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003664 return FAIL;
3665 }
3666 mch_memmove(tv1->vval.v_string, s1, len1);
3667 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003668 vim_free(s1);
3669 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003670 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003671 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003674 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003675 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003676 if (*op == '.')
3677 {
3678 if (may_generate_2STRING(-2, cctx) == FAIL
3679 || may_generate_2STRING(-1, cctx) == FAIL)
3680 return FAIL;
3681 generate_instr_drop(cctx, ISN_CONCAT, 1);
3682 }
3683 else
3684 generate_two_op(cctx, op);
3685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 }
3687
3688 return OK;
3689}
3690
3691/*
3692 * expr5a == expr5b
3693 * expr5a =~ expr5b
3694 * expr5a != expr5b
3695 * expr5a !~ expr5b
3696 * expr5a > expr5b
3697 * expr5a >= expr5b
3698 * expr5a < expr5b
3699 * expr5a <= expr5b
3700 * expr5a is expr5b
3701 * expr5a isnot expr5b
3702 *
3703 * Produces instructions:
3704 * EVAL expr5a Push result of "expr5a"
3705 * EVAL expr5b Push result of "expr5b"
3706 * COMPARE one of the compare instructions
3707 */
3708 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003709compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710{
3711 exptype_T type = EXPR_UNKNOWN;
3712 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003713 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003716 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717
3718 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003719 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 return FAIL;
3721
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003722 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003723 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
3725 /*
3726 * If there is a comparative operator, use it.
3727 */
3728 if (type != EXPR_UNKNOWN)
3729 {
3730 int ic = FALSE; // Default: do not ignore case
3731
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003732 if (next != NULL)
3733 {
3734 *arg = next_line_from_context(cctx, TRUE);
3735 p = skipwhite(*arg);
3736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 if (type_is && (p[len] == '?' || p[len] == '#'))
3738 {
3739 semsg(_(e_invexpr2), *arg);
3740 return FAIL;
3741 }
3742 // extra question mark appended: ignore case
3743 if (p[len] == '?')
3744 {
3745 ic = TRUE;
3746 ++len;
3747 }
3748 // extra '#' appended: match case (ignored)
3749 else if (p[len] == '#')
3750 ++len;
3751 // nothing appended: match case
3752
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003753 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003755 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 }
3758
3759 // get the second variable
3760 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003761 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003762 return FAIL;
3763
Bram Moolenaara5565e42020-05-09 15:44:01 +02003764 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 return FAIL;
3766
Bram Moolenaara5565e42020-05-09 15:44:01 +02003767 if (ppconst->pp_used == ppconst_used + 2)
3768 {
3769 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3770 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3771 int ret;
3772
3773 // Both sides are a constant, compute the result now.
3774 // First check for a valid combination of types, this is more
3775 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003776 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003777 ret = FAIL;
3778 else
3779 {
3780 ret = typval_compare(tv1, tv2, type, ic);
3781 tv1->v_type = VAR_BOOL;
3782 tv1->vval.v_number = tv1->vval.v_number
3783 ? VVAL_TRUE : VVAL_FALSE;
3784 clear_tv(tv2);
3785 --ppconst->pp_used;
3786 }
3787 return ret;
3788 }
3789
3790 generate_ppconst(cctx, ppconst);
3791 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 }
3793
3794 return OK;
3795}
3796
Bram Moolenaar7f141552020-05-09 17:35:53 +02003797static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799/*
3800 * Compile || or &&.
3801 */
3802 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003803compile_and_or(
3804 char_u **arg,
3805 cctx_T *cctx,
3806 char *op,
3807 ppconst_T *ppconst,
3808 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003810 char_u *next;
3811 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 int opchar = *op;
3813
3814 if (p[0] == opchar && p[1] == opchar)
3815 {
3816 garray_T *instr = &cctx->ctx_instr;
3817 garray_T end_ga;
3818
3819 /*
3820 * Repeat until there is no following "||" or "&&"
3821 */
3822 ga_init2(&end_ga, sizeof(int), 10);
3823 while (p[0] == opchar && p[1] == opchar)
3824 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003825 if (next != NULL)
3826 {
3827 *arg = next_line_from_context(cctx, TRUE);
3828 p = skipwhite(*arg);
3829 }
3830
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003831 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3832 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003833 semsg(_(e_white_space_required_before_and_after), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003834 return FAIL;
3835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836
Bram Moolenaara5565e42020-05-09 15:44:01 +02003837 // TODO: use ppconst if the value is a constant
3838 generate_ppconst(cctx, ppconst);
3839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (ga_grow(&end_ga, 1) == FAIL)
3841 {
3842 ga_clear(&end_ga);
3843 return FAIL;
3844 }
3845 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3846 ++end_ga.ga_len;
3847 generate_JUMP(cctx, opchar == '|'
3848 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3849
3850 // eval the next expression
3851 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003852 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003853 return FAIL;
3854
Bram Moolenaara5565e42020-05-09 15:44:01 +02003855 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3856 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 {
3858 ga_clear(&end_ga);
3859 return FAIL;
3860 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003861
3862 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003864 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
3866 // Fill in the end label in all jumps.
3867 while (end_ga.ga_len > 0)
3868 {
3869 isn_T *isn;
3870
3871 --end_ga.ga_len;
3872 isn = ((isn_T *)instr->ga_data)
3873 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3874 isn->isn_arg.jump.jump_where = instr->ga_len;
3875 }
3876 ga_clear(&end_ga);
3877 }
3878
3879 return OK;
3880}
3881
3882/*
3883 * expr4a && expr4a && expr4a logical AND
3884 *
3885 * Produces instructions:
3886 * EVAL expr4a Push result of "expr4a"
3887 * JUMP_AND_KEEP_IF_FALSE end
3888 * EVAL expr4b Push result of "expr4b"
3889 * JUMP_AND_KEEP_IF_FALSE end
3890 * EVAL expr4c Push result of "expr4c"
3891 * end:
3892 */
3893 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003894compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003896 int ppconst_used = ppconst->pp_used;
3897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 return FAIL;
3901
3902 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904}
3905
3906/*
3907 * expr3a || expr3b || expr3c logical OR
3908 *
3909 * Produces instructions:
3910 * EVAL expr3a Push result of "expr3a"
3911 * JUMP_AND_KEEP_IF_TRUE end
3912 * EVAL expr3b Push result of "expr3b"
3913 * JUMP_AND_KEEP_IF_TRUE end
3914 * EVAL expr3c Push result of "expr3c"
3915 * end:
3916 */
3917 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003920 int ppconst_used = ppconst->pp_used;
3921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 return FAIL;
3925
3926 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003927 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928}
3929
3930/*
3931 * Toplevel expression: expr2 ? expr1a : expr1b
3932 *
3933 * Produces instructions:
3934 * EVAL expr2 Push result of "expr"
3935 * JUMP_IF_FALSE alt jump if false
3936 * EVAL expr1a
3937 * JUMP_ALWAYS end
3938 * alt: EVAL expr1b
3939 * end:
3940 */
3941 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003942compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943{
3944 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003946 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
Bram Moolenaar61a89812020-05-07 16:58:17 +02003948 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003949 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 return FAIL;
3951
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003952 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 if (*p == '?')
3954 {
3955 garray_T *instr = &cctx->ctx_instr;
3956 garray_T *stack = &cctx->ctx_type_stack;
3957 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003958 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003960 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003962 int has_const_expr = FALSE;
3963 int const_value = FALSE;
3964 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003966 if (next != NULL)
3967 {
3968 *arg = next_line_from_context(cctx, TRUE);
3969 p = skipwhite(*arg);
3970 }
3971
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003972 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3973 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003974 semsg(_(e_white_space_required_before_and_after), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003975 return FAIL;
3976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 if (ppconst->pp_used == ppconst_used + 1)
3979 {
3980 // the condition is a constant, we know whether the ? or the :
3981 // expression is to be evaluated.
3982 has_const_expr = TRUE;
3983 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3984 clear_tv(&ppconst->pp_tv[ppconst_used]);
3985 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003986 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3987 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 }
3989 else
3990 {
3991 generate_ppconst(cctx, ppconst);
3992 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
3995 // evaluate the second expression; any type is accepted
3996 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003997 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003998 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003999 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004000 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002 if (!has_const_expr)
4003 {
4004 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 // remember the type and drop it
4007 --stack->ga_len;
4008 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
Bram Moolenaara5565e42020-05-09 15:44:01 +02004010 end_idx = instr->ga_len;
4011 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4012
4013 // jump here from JUMP_IF_FALSE
4014 isn = ((isn_T *)instr->ga_data) + alt_idx;
4015 isn->isn_arg.jump.jump_where = instr->ga_len;
4016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
4018 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004019 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 if (*p != ':')
4021 {
4022 emsg(_(e_missing_colon));
4023 return FAIL;
4024 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004025 if (next != NULL)
4026 {
4027 *arg = next_line_from_context(cctx, TRUE);
4028 p = skipwhite(*arg);
4029 }
4030
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004031 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4032 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004033 semsg(_(e_white_space_required_before_and_after), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004034 return FAIL;
4035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036
4037 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004039 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4040 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004042 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004043 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004044 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004045 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 if (!has_const_expr)
4048 {
4049 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
Bram Moolenaara5565e42020-05-09 15:44:01 +02004051 // If the types differ, the result has a more generic type.
4052 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4053 common_type(type1, type2, &type2, cctx->ctx_type_list);
4054
4055 // jump here from JUMP_ALWAYS
4056 isn = ((isn_T *)instr->ga_data) + end_idx;
4057 isn->isn_arg.jump.jump_where = instr->ga_len;
4058 }
4059
4060 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 }
4062 return OK;
4063}
4064
4065/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004066 * Toplevel expression.
4067 */
4068 static int
4069compile_expr0(char_u **arg, cctx_T *cctx)
4070{
4071 ppconst_T ppconst;
4072
4073 CLEAR_FIELD(ppconst);
4074 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4075 {
4076 clear_ppconst(&ppconst);
4077 return FAIL;
4078 }
4079 if (generate_ppconst(cctx, &ppconst) == FAIL)
4080 return FAIL;
4081 return OK;
4082}
4083
4084/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 * compile "return [expr]"
4086 */
4087 static char_u *
4088compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4089{
4090 char_u *p = arg;
4091 garray_T *stack = &cctx->ctx_type_stack;
4092 type_T *stack_type;
4093
4094 if (*p != NUL && *p != '|' && *p != '\n')
4095 {
4096 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004097 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 return NULL;
4099
4100 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4101 if (set_return_type)
4102 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004103 else
4104 {
4105 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4106 && stack_type->tt_type != VAR_VOID
4107 && stack_type->tt_type != VAR_UNKNOWN)
4108 {
4109 emsg(_("E1096: Returning a value in a function without a return type"));
4110 return NULL;
4111 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004112 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4113 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 }
4117 else
4118 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004119 // "set_return_type" cannot be TRUE, only used for a lambda which
4120 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004121 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4122 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 {
4124 emsg(_("E1003: Missing return value"));
4125 return NULL;
4126 }
4127
4128 // No argument, return zero.
4129 generate_PUSHNR(cctx, 0);
4130 }
4131
4132 if (generate_instr(cctx, ISN_RETURN) == NULL)
4133 return NULL;
4134
4135 // "return val | endif" is possible
4136 return skipwhite(p);
4137}
4138
4139/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004140 * Get a line from the compilation context, compatible with exarg_T getline().
4141 * Return a pointer to the line in allocated memory.
4142 * Return NULL for end-of-file or some error.
4143 */
4144 static char_u *
4145exarg_getline(
4146 int c UNUSED,
4147 void *cookie,
4148 int indent UNUSED,
4149 int do_concat UNUSED)
4150{
4151 cctx_T *cctx = (cctx_T *)cookie;
4152
4153 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4154 {
4155 iemsg("Heredoc got to end");
4156 return NULL;
4157 }
4158 ++cctx->ctx_lnum;
4159 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4160 [cctx->ctx_lnum]);
4161}
4162
4163/*
4164 * Compile a nested :def command.
4165 */
4166 static char_u *
4167compile_nested_function(exarg_T *eap, cctx_T *cctx)
4168{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004169 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004170 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004171 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004172 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004173 lvar_T *lvar;
4174 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004175 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004176
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004177 // Only g:Func() can use a namespace.
4178 if (name_start[1] == ':' && !is_global)
4179 {
4180 semsg(_(e_namespace), name_start);
4181 return NULL;
4182 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004183 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4184 return NULL;
4185
Bram Moolenaar04b12692020-05-04 23:24:44 +02004186 eap->arg = name_end;
4187 eap->getline = exarg_getline;
4188 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004189 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004190 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004191 lambda_name = get_lambda_name();
4192 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004193
Bram Moolenaar822ba242020-05-24 23:00:18 +02004194 if (ufunc == NULL)
4195 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004196 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004197 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004198 return NULL;
4199
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004200 if (is_global)
4201 {
4202 char_u *func_name = vim_strnsave(name_start + 2,
4203 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004204
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004205 if (func_name == NULL)
4206 r = FAIL;
4207 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004208 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004209 }
4210 else
4211 {
4212 // Define a local variable for the function reference.
4213 lvar = reserve_local(cctx, name_start, name_end - name_start,
4214 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004215 if (lvar == NULL)
4216 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004217 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004218 return NULL;
4219 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4220 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004221
Bram Moolenaar61a89812020-05-07 16:58:17 +02004222 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004223 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004224}
4225
4226/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 * Return the length of an assignment operator, or zero if there isn't one.
4228 */
4229 int
4230assignment_len(char_u *p, int *heredoc)
4231{
4232 if (*p == '=')
4233 {
4234 if (p[1] == '<' && p[2] == '<')
4235 {
4236 *heredoc = TRUE;
4237 return 3;
4238 }
4239 return 1;
4240 }
4241 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4242 return 2;
4243 if (STRNCMP(p, "..=", 3) == 0)
4244 return 3;
4245 return 0;
4246}
4247
4248// words that cannot be used as a variable
4249static char *reserved[] = {
4250 "true",
4251 "false",
4252 NULL
4253};
4254
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004255typedef enum {
4256 dest_local,
4257 dest_option,
4258 dest_env,
4259 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004260 dest_buffer,
4261 dest_window,
4262 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004263 dest_vimvar,
4264 dest_script,
4265 dest_reg,
4266} assign_dest_T;
4267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004269 * Generate the load instruction for "name".
4270 */
4271 static void
4272generate_loadvar(
4273 cctx_T *cctx,
4274 assign_dest_T dest,
4275 char_u *name,
4276 lvar_T *lvar,
4277 type_T *type)
4278{
4279 switch (dest)
4280 {
4281 case dest_option:
4282 // TODO: check the option exists
4283 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4284 break;
4285 case dest_global:
4286 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4287 break;
4288 case dest_buffer:
4289 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4290 break;
4291 case dest_window:
4292 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4293 break;
4294 case dest_tab:
4295 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4296 break;
4297 case dest_script:
4298 compile_load_scriptvar(cctx,
4299 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4300 break;
4301 case dest_env:
4302 // Include $ in the name here
4303 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4304 break;
4305 case dest_reg:
4306 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4307 break;
4308 case dest_vimvar:
4309 generate_LOADV(cctx, name + 2, TRUE);
4310 break;
4311 case dest_local:
4312 if (lvar->lv_from_outer)
4313 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4314 NULL, type);
4315 else
4316 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4317 break;
4318 }
4319}
4320
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004321 void
4322vim9_declare_error(char_u *name)
4323{
4324 char *scope = "";
4325
4326 switch (*name)
4327 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004328 case 'g': scope = _("global"); break;
4329 case 'b': scope = _("buffer"); break;
4330 case 'w': scope = _("window"); break;
4331 case 't': scope = _("tab"); break;
4332 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004333 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004334 return;
4335 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4336 return;
4337 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4338 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004339 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004340 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004341 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004342}
4343
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004344/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004345 * Compile declaration and assignment:
4346 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004348 * Return NULL for an error.
4349 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 */
4351 static char_u *
4352compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4353{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004354 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004356 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 char_u *ret = NULL;
4358 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004359 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004360 int scriptvar_sid = 0;
4361 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004364 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 int oplen = 0;
4367 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004368 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004369 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004370 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004374 // Skip over the "var" or "[var, var]" to get to any "=".
4375 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4376 if (p == NULL)
4377 return *arg == '[' ? arg : NULL;
4378
4379 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004381 // TODO: should we allow this, and figure out type inference from list
4382 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004383 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 return NULL;
4385 }
4386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 sp = p;
4388 p = skipwhite(p);
4389 op = p;
4390 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004391
4392 if (var_count > 0 && oplen == 0)
4393 // can be something like "[1, 2]->func()"
4394 return arg;
4395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4397 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004398 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004399 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004400 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 if (heredoc)
4403 {
4404 list_T *l;
4405 listitem_T *li;
4406
4407 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004408 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004410 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
4412 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004413 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 {
4415 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4416 li->li_tv.vval.v_string = NULL;
4417 }
4418 generate_NEWLIST(cctx, l->lv_len);
4419 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004420 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 list_free(l);
4422 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004423 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004425 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004427 // for "[var, var] = expr" evaluate the expression here, loop over the
4428 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004429
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004430 p = skipwhite(op + oplen);
4431 if (compile_expr0(&p, cctx) == FAIL)
4432 return NULL;
4433 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004435 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004437 type_T *stacktype;
4438
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004439 stacktype = stack->ga_len == 0 ? &t_void
4440 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004441 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004443 emsg(_(e_cannot_use_void));
4444 goto theend;
4445 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004446 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004447 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004448 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004449 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4450 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004451 }
4452 }
4453
4454 /*
4455 * Loop over variables in "[var, var] = expr".
4456 * For "var = expr" and "let var: type" this is done only once.
4457 */
4458 if (var_count > 0)
4459 var_start = skipwhite(arg + 1); // skip over the "["
4460 else
4461 var_start = arg;
4462 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4463 {
4464 char_u *var_end = skip_var_one(var_start, FALSE);
4465 size_t varlen;
4466 int new_local = FALSE;
4467 int opt_type;
4468 int opt_flags = 0;
4469 assign_dest_T dest = dest_local;
4470 int vimvaridx = -1;
4471 lvar_T *lvar = NULL;
4472 lvar_T arg_lvar;
4473 int has_type = FALSE;
4474 int has_index = FALSE;
4475 int instr_count = -1;
4476
Bram Moolenaar65821722020-08-02 18:58:54 +02004477 if (*var_start == '@')
4478 p = var_start + 2;
4479 else
4480 {
4481 p = (*var_start == '&' || *var_start == '$')
4482 ? var_start + 1 : var_start;
4483 p = to_name_end(p, TRUE);
4484 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004485
4486 // "a: type" is declaring variable "a" with a type, not "a:".
4487 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4488 --var_end;
4489 if (is_decl && p == var_start + 2 && p[-1] == ':')
4490 --p;
4491
4492 varlen = p - var_start;
4493 vim_free(name);
4494 name = vim_strnsave(var_start, varlen);
4495 if (name == NULL)
4496 return NULL;
4497 if (!heredoc)
4498 type = &t_any;
4499
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004500 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004501 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004502 int declare_error = FALSE;
4503
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004504 if (*var_start == '&')
4505 {
4506 int cc;
4507 long numval;
4508
4509 dest = dest_option;
4510 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004512 emsg(_(e_const_option));
4513 goto theend;
4514 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004515 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004516 p = var_start;
4517 p = find_option_end(&p, &opt_flags);
4518 if (p == NULL)
4519 {
4520 // cannot happen?
4521 emsg(_(e_letunexp));
4522 goto theend;
4523 }
4524 cc = *p;
4525 *p = NUL;
4526 opt_type = get_option_value(var_start + 1, &numval,
4527 NULL, opt_flags);
4528 *p = cc;
4529 if (opt_type == -3)
4530 {
4531 semsg(_(e_unknown_option), var_start);
4532 goto theend;
4533 }
4534 if (opt_type == -2 || opt_type == 0)
4535 type = &t_string;
4536 else
4537 type = &t_number; // both number and boolean option
4538 }
4539 else if (*var_start == '$')
4540 {
4541 dest = dest_env;
4542 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004543 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 }
4545 else if (*var_start == '@')
4546 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004547 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548 {
4549 emsg_invreg(var_start[1]);
4550 goto theend;
4551 }
4552 dest = dest_reg;
4553 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004554 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004555 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004556 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004557 {
4558 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004559 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004560 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004561 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004562 {
4563 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004564 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004565 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004566 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004567 {
4568 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004569 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004570 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004571 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572 {
4573 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004574 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004575 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004576 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577 {
4578 typval_T *vtv;
4579 int di_flags;
4580
4581 vimvaridx = find_vim_var(name + 2, &di_flags);
4582 if (vimvaridx < 0)
4583 {
4584 semsg(_(e_var_notfound), var_start);
4585 goto theend;
4586 }
4587 // We use the current value of "sandbox" here, is that OK?
4588 if (var_check_ro(di_flags, name, FALSE))
4589 goto theend;
4590 dest = dest_vimvar;
4591 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004592 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004593 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004594 }
4595 else
4596 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004597 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598
4599 for (idx = 0; reserved[idx] != NULL; ++idx)
4600 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004601 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004602 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004603 goto theend;
4604 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004605
4606 lvar = lookup_local(var_start, varlen, cctx);
4607 if (lvar == NULL)
4608 {
4609 CLEAR_FIELD(arg_lvar);
4610 if (lookup_arg(var_start, varlen,
4611 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4612 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004613 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004614 if (is_decl)
4615 {
4616 semsg(_(e_used_as_arg), name);
4617 goto theend;
4618 }
4619 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004620 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004621 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004622 if (lvar != NULL)
4623 {
4624 if (is_decl)
4625 {
4626 semsg(_("E1017: Variable already declared: %s"), name);
4627 goto theend;
4628 }
4629 else if (lvar->lv_const)
4630 {
4631 semsg(_("E1018: Cannot assign to a constant: %s"),
4632 name);
4633 goto theend;
4634 }
4635 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004636 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004637 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004638 int script_namespace = varlen > 1
4639 && STRNCMP(var_start, "s:", 2) == 0;
4640 int script_var = (script_namespace
4641 ? lookup_script(var_start + 2, varlen - 2)
4642 : lookup_script(var_start, varlen)) == OK;
4643 imported_T *import =
4644 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004645
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004646 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004647 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004648 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4649
4650 if (is_decl)
4651 {
4652 if (script_namespace)
4653 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004654 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004655 else
4656 semsg(_("E1054: Variable already declared in the script: %s"),
4657 name);
4658 goto theend;
4659 }
4660 else if (cctx->ctx_ufunc->uf_script_ctx_version
4661 == SCRIPT_VERSION_VIM9
4662 && script_namespace
4663 && !script_var && import == NULL)
4664 {
4665 semsg(_(e_unknown_var), name);
4666 goto theend;
4667 }
4668
4669 dest = dest_script;
4670
4671 // existing script-local variables should have a type
4672 scriptvar_sid = current_sctx.sc_sid;
4673 if (import != NULL)
4674 scriptvar_sid = import->imp_sid;
4675 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4676 rawname, TRUE);
4677 if (scriptvar_idx >= 0)
4678 {
4679 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4680 svar_T *sv =
4681 ((svar_T *)si->sn_var_vals.ga_data)
4682 + scriptvar_idx;
4683 type = sv->sv_type;
4684 }
4685 }
4686 else if (name[1] == ':' && name[2] != NUL)
4687 {
4688 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004689 name);
4690 goto theend;
4691 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004692 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004693 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004694 semsg(_(e_unknown_var), name);
4695 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004696 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004697 else if (check_defined(var_start, varlen, cctx) == FAIL)
4698 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004699 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004701
4702 if (declare_error)
4703 {
4704 vim9_declare_error(name);
4705 goto theend;
4706 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004707 }
4708
4709 // handle "a:name" as a name, not index "name" on "a"
4710 if (varlen > 1 || var_start[varlen] != ':')
4711 p = var_end;
4712
4713 if (dest != dest_option)
4714 {
4715 if (is_decl && *p == ':')
4716 {
4717 // parse optional type: "let var: type = expr"
4718 if (!VIM_ISWHITE(p[1]))
4719 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004720 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004721 goto theend;
4722 }
4723 p = skipwhite(p + 1);
4724 type = parse_type(&p, cctx->ctx_type_list);
4725 has_type = TRUE;
4726 }
4727 else if (lvar != NULL)
4728 type = lvar->lv_type;
4729 }
4730
4731 if (oplen == 3 && !heredoc && dest != dest_global
4732 && type->tt_type != VAR_STRING
4733 && type->tt_type != VAR_ANY)
4734 {
4735 emsg(_("E1019: Can only concatenate to string"));
4736 goto theend;
4737 }
4738
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004739 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004740 {
4741 if (oplen > 1 && !heredoc)
4742 {
4743 // +=, /=, etc. require an existing variable
4744 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4745 name);
4746 goto theend;
4747 }
4748
4749 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004750 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4751 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004752 goto theend;
4753 lvar = reserve_local(cctx, var_start, varlen,
4754 cmdidx == CMD_const, type);
4755 if (lvar == NULL)
4756 goto theend;
4757 new_local = TRUE;
4758 }
4759
4760 member_type = type;
4761 if (var_end > var_start + varlen)
4762 {
4763 // Something follows after the variable: "var[idx]".
4764 if (is_decl)
4765 {
4766 emsg(_("E1087: cannot use an index when declaring a variable"));
4767 goto theend;
4768 }
4769
4770 if (var_start[varlen] == '[')
4771 {
4772 has_index = TRUE;
4773 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004774 member_type = &t_any;
4775 else
4776 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 }
4778 else
4779 {
4780 semsg("Not supported yet: %s", var_start);
4781 goto theend;
4782 }
4783 }
4784 else if (lvar == &arg_lvar)
4785 {
4786 semsg(_("E1090: Cannot assign to argument %s"), name);
4787 goto theend;
4788 }
4789
4790 if (!heredoc)
4791 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004792 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004793 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004794 if (oplen > 0 && var_count == 0)
4795 {
4796 // skip over the "=" and the expression
4797 p = skipwhite(op + oplen);
4798 compile_expr0(&p, cctx);
4799 }
4800 }
4801 else if (oplen > 0)
4802 {
4803 type_T *stacktype;
4804
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004805 // For "var = expr" evaluate the expression.
4806 if (var_count == 0)
4807 {
4808 int r;
4809
4810 // for "+=", "*=", "..=" etc. first load the current value
4811 if (*op != '=')
4812 {
4813 generate_loadvar(cctx, dest, name, lvar, type);
4814
4815 if (has_index)
4816 {
4817 // TODO: get member from list or dict
4818 emsg("Index with operation not supported yet");
4819 goto theend;
4820 }
4821 }
4822
4823 // Compile the expression. Temporarily hide the new local
4824 // variable here, it is not available to this expression.
4825 if (new_local)
4826 --cctx->ctx_locals.ga_len;
4827 instr_count = instr->ga_len;
4828 p = skipwhite(op + oplen);
4829 r = compile_expr0(&p, cctx);
4830 if (new_local)
4831 ++cctx->ctx_locals.ga_len;
4832 if (r == FAIL)
4833 goto theend;
4834 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004835 else if (semicolon && var_idx == var_count - 1)
4836 {
4837 // For "[var; var] = expr" get the rest of the list
4838 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4839 goto theend;
4840 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004841 else
4842 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 // For "[var, var] = expr" get the "var_idx" item from the
4844 // list.
4845 if (generate_GETITEM(cctx, var_idx) == FAIL)
4846 return FAIL;
4847 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004848
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004849 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004850 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004851 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004852 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004853 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004854 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004855 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004857 emsg(_(e_cannot_use_void));
4858 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004860 else if ((stacktype->tt_type == VAR_FUNC
4861 || stacktype->tt_type == VAR_PARTIAL)
4862 && var_wrong_func_name(name, TRUE))
4863 {
4864 goto theend;
4865 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 else
4867 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004868 // An empty list or dict has a &t_void member,
4869 // for a variable that implies &t_any.
4870 if (stacktype == &t_list_empty)
4871 lvar->lv_type = &t_list_any;
4872 else if (stacktype == &t_dict_empty)
4873 lvar->lv_type = &t_dict_any;
4874 else
4875 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004877 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004878 else
4879 {
4880 type_T *use_type = lvar->lv_type;
4881
4882 if (has_index)
4883 {
4884 use_type = use_type->tt_member;
4885 if (use_type == NULL)
4886 use_type = &t_void;
4887 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004888 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004889 == FAIL)
4890 goto theend;
4891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004892 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004893 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004894 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004895 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004897 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004899 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004900 goto theend;
4901 }
4902 else if (!has_type || dest == dest_option)
4903 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004904 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 goto theend;
4906 }
4907 else
4908 {
4909 // variables are always initialized
4910 if (ga_grow(instr, 1) == FAIL)
4911 goto theend;
4912 switch (member_type->tt_type)
4913 {
4914 case VAR_BOOL:
4915 generate_PUSHBOOL(cctx, VVAL_FALSE);
4916 break;
4917 case VAR_FLOAT:
4918#ifdef FEAT_FLOAT
4919 generate_PUSHF(cctx, 0.0);
4920#endif
4921 break;
4922 case VAR_STRING:
4923 generate_PUSHS(cctx, NULL);
4924 break;
4925 case VAR_BLOB:
4926 generate_PUSHBLOB(cctx, NULL);
4927 break;
4928 case VAR_FUNC:
4929 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4930 break;
4931 case VAR_LIST:
4932 generate_NEWLIST(cctx, 0);
4933 break;
4934 case VAR_DICT:
4935 generate_NEWDICT(cctx, 0);
4936 break;
4937 case VAR_JOB:
4938 generate_PUSHJOB(cctx, NULL);
4939 break;
4940 case VAR_CHANNEL:
4941 generate_PUSHCHANNEL(cctx, NULL);
4942 break;
4943 case VAR_NUMBER:
4944 case VAR_UNKNOWN:
4945 case VAR_ANY:
4946 case VAR_PARTIAL:
4947 case VAR_VOID:
4948 case VAR_SPECIAL: // cannot happen
4949 generate_PUSHNR(cctx, 0);
4950 break;
4951 }
4952 }
4953 if (var_count == 0)
4954 end = p;
4955 }
4956
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004957 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004958 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004959 break;
4960
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 if (oplen > 0 && *op != '=')
4962 {
4963 type_T *expected = &t_number;
4964 type_T *stacktype;
4965
4966 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004967 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968
4969 if (*op == '.')
4970 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004971 else if (*op == '+')
4972 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004974 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975 goto theend;
4976
4977 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004978 {
4979 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4980 goto theend;
4981 }
4982 else if (*op == '+')
4983 {
4984 if (generate_add_instr(cctx,
4985 operator_type(member_type, stacktype),
4986 member_type, stacktype) == FAIL)
4987 goto theend;
4988 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 else
4990 {
4991 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4992
4993 if (isn == NULL)
4994 goto theend;
4995 switch (*op)
4996 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4998 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4999 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5000 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005006 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 int r;
5008
5009 // Compile the "idx" in "var[idx]".
5010 if (new_local)
5011 --cctx->ctx_locals.ga_len;
5012 p = skipwhite(var_start + varlen + 1);
5013 r = compile_expr0(&p, cctx);
5014 if (new_local)
5015 ++cctx->ctx_locals.ga_len;
5016 if (r == FAIL)
5017 goto theend;
5018 if (*skipwhite(p) != ']')
5019 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005020 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 emsg(_(e_missbrac));
5022 goto theend;
5023 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005024 if (type == &t_any)
5025 {
5026 type_T *idx_type = ((type_T **)stack->ga_data)[
5027 stack->ga_len - 1];
5028 // Index on variable of unknown type: guess the type from the
5029 // index type: number is dict, otherwise dict.
5030 // TODO: should do the assignment at runtime
5031 if (idx_type->tt_type == VAR_NUMBER)
5032 type = &t_list_any;
5033 else
5034 type = &t_dict_any;
5035 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036 if (type->tt_type == VAR_DICT
5037 && may_generate_2STRING(-1, cctx) == FAIL)
5038 goto theend;
5039 if (type->tt_type == VAR_LIST
5040 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005041 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 {
5043 emsg(_(e_number_exp));
5044 goto theend;
5045 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005046
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 // Load the dict or list. On the stack we then have:
5048 // - value
5049 // - index
5050 // - variable
5051 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005052
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 if (type->tt_type == VAR_LIST)
5054 {
5055 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5056 return FAIL;
5057 }
5058 else if (type->tt_type == VAR_DICT)
5059 {
5060 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5061 return FAIL;
5062 }
5063 else
5064 {
5065 emsg(_(e_listreq));
5066 goto theend;
5067 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005068 }
5069 else
5070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 switch (dest)
5072 {
5073 case dest_option:
5074 generate_STOREOPT(cctx, name + 1, opt_flags);
5075 break;
5076 case dest_global:
5077 // include g: with the name, easier to execute that way
5078 generate_STORE(cctx, ISN_STOREG, 0, name);
5079 break;
5080 case dest_buffer:
5081 // include b: with the name, easier to execute that way
5082 generate_STORE(cctx, ISN_STOREB, 0, name);
5083 break;
5084 case dest_window:
5085 // include w: with the name, easier to execute that way
5086 generate_STORE(cctx, ISN_STOREW, 0, name);
5087 break;
5088 case dest_tab:
5089 // include t: with the name, easier to execute that way
5090 generate_STORE(cctx, ISN_STORET, 0, name);
5091 break;
5092 case dest_env:
5093 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5094 break;
5095 case dest_reg:
5096 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5097 break;
5098 case dest_vimvar:
5099 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5100 break;
5101 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005102 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005103 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 {
5105 char_u *name_s = name;
5106
5107 // Include s: in the name for store_var()
5108 if (name[1] != ':')
5109 {
5110 int len = (int)STRLEN(name) + 3;
5111
5112 name_s = alloc(len);
5113 if (name_s == NULL)
5114 name_s = name;
5115 else
5116 vim_snprintf((char *)name_s, len,
5117 "s:%s", name);
5118 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005119 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5120 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 if (name_s != name)
5122 vim_free(name_s);
5123 }
5124 else
5125 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005126 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005127 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 break;
5129 case dest_local:
5130 if (lvar != NULL)
5131 {
5132 isn_T *isn = ((isn_T *)instr->ga_data)
5133 + instr->ga_len - 1;
5134
5135 // optimization: turn "var = 123" from ISN_PUSHNR +
5136 // ISN_STORE into ISN_STORENR
5137 if (!lvar->lv_from_outer
5138 && instr->ga_len == instr_count + 1
5139 && isn->isn_type == ISN_PUSHNR)
5140 {
5141 varnumber_T val = isn->isn_arg.number;
5142
5143 isn->isn_type = ISN_STORENR;
5144 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5145 isn->isn_arg.storenr.stnr_val = val;
5146 if (stack->ga_len > 0)
5147 --stack->ga_len;
5148 }
5149 else if (lvar->lv_from_outer)
5150 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005151 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005152 else
5153 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5154 }
5155 break;
5156 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005157 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158
5159 if (var_idx + 1 < var_count)
5160 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162
5163 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005164 if (var_count > 0 && !semicolon)
5165 {
5166 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5167 goto theend;
5168 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169
Bram Moolenaarb2097502020-07-19 17:17:02 +02005170 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171
5172theend:
5173 vim_free(name);
5174 return ret;
5175}
5176
5177/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005178 * Check if "name" can be "unlet".
5179 */
5180 int
5181check_vim9_unlet(char_u *name)
5182{
5183 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5184 {
5185 semsg(_("E1081: Cannot unlet %s"), name);
5186 return FAIL;
5187 }
5188 return OK;
5189}
5190
5191/*
5192 * Callback passed to ex_unletlock().
5193 */
5194 static int
5195compile_unlet(
5196 lval_T *lvp,
5197 char_u *name_end,
5198 exarg_T *eap,
5199 int deep UNUSED,
5200 void *coookie)
5201{
5202 cctx_T *cctx = coookie;
5203
5204 if (lvp->ll_tv == NULL)
5205 {
5206 char_u *p = lvp->ll_name;
5207 int cc = *name_end;
5208 int ret = OK;
5209
5210 // Normal name. Only supports g:, w:, t: and b: namespaces.
5211 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005212 if (*p == '$')
5213 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5214 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005215 ret = FAIL;
5216 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005217 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005218
5219 *name_end = cc;
5220 return ret;
5221 }
5222
5223 // TODO: unlet {list}[idx]
5224 // TODO: unlet {dict}[key]
5225 emsg("Sorry, :unlet not fully implemented yet");
5226 return FAIL;
5227}
5228
5229/*
5230 * compile "unlet var", "lock var" and "unlock var"
5231 * "arg" points to "var".
5232 */
5233 static char_u *
5234compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5235{
5236 char_u *p = arg;
5237
5238 if (eap->cmdidx != CMD_unlet)
5239 {
5240 emsg("Sorry, :lock and unlock not implemented yet");
5241 return NULL;
5242 }
5243
5244 if (*p == '!')
5245 {
5246 p = skipwhite(p + 1);
5247 eap->forceit = TRUE;
5248 }
5249
5250 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5251 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5252}
5253
5254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 * Compile an :import command.
5256 */
5257 static char_u *
5258compile_import(char_u *arg, cctx_T *cctx)
5259{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005260 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261}
5262
5263/*
5264 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5265 */
5266 static int
5267compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5268{
5269 garray_T *instr = &cctx->ctx_instr;
5270 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5271
5272 if (endlabel == NULL)
5273 return FAIL;
5274 endlabel->el_next = *el;
5275 *el = endlabel;
5276 endlabel->el_end_label = instr->ga_len;
5277
5278 generate_JUMP(cctx, when, 0);
5279 return OK;
5280}
5281
5282 static void
5283compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5284{
5285 garray_T *instr = &cctx->ctx_instr;
5286
5287 while (*el != NULL)
5288 {
5289 endlabel_T *cur = (*el);
5290 isn_T *isn;
5291
5292 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5293 isn->isn_arg.jump.jump_where = instr->ga_len;
5294 *el = cur->el_next;
5295 vim_free(cur);
5296 }
5297}
5298
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005299 static void
5300compile_free_jump_to_end(endlabel_T **el)
5301{
5302 while (*el != NULL)
5303 {
5304 endlabel_T *cur = (*el);
5305
5306 *el = cur->el_next;
5307 vim_free(cur);
5308 }
5309}
5310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311/*
5312 * Create a new scope and set up the generic items.
5313 */
5314 static scope_T *
5315new_scope(cctx_T *cctx, scopetype_T type)
5316{
5317 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5318
5319 if (scope == NULL)
5320 return NULL;
5321 scope->se_outer = cctx->ctx_scope;
5322 cctx->ctx_scope = scope;
5323 scope->se_type = type;
5324 scope->se_local_count = cctx->ctx_locals.ga_len;
5325 return scope;
5326}
5327
5328/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005329 * Free the current scope and go back to the outer scope.
5330 */
5331 static void
5332drop_scope(cctx_T *cctx)
5333{
5334 scope_T *scope = cctx->ctx_scope;
5335
5336 if (scope == NULL)
5337 {
5338 iemsg("calling drop_scope() without a scope");
5339 return;
5340 }
5341 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005342 switch (scope->se_type)
5343 {
5344 case IF_SCOPE:
5345 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5346 case FOR_SCOPE:
5347 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5348 case WHILE_SCOPE:
5349 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5350 case TRY_SCOPE:
5351 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5352 case NO_SCOPE:
5353 case BLOCK_SCOPE:
5354 break;
5355 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005356 vim_free(scope);
5357}
5358
5359/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 * compile "if expr"
5361 *
5362 * "if expr" Produces instructions:
5363 * EVAL expr Push result of "expr"
5364 * JUMP_IF_FALSE end
5365 * ... body ...
5366 * end:
5367 *
5368 * "if expr | else" Produces instructions:
5369 * EVAL expr Push result of "expr"
5370 * JUMP_IF_FALSE else
5371 * ... body ...
5372 * JUMP_ALWAYS end
5373 * else:
5374 * ... body ...
5375 * end:
5376 *
5377 * "if expr1 | elseif expr2 | else" Produces instructions:
5378 * EVAL expr Push result of "expr"
5379 * JUMP_IF_FALSE elseif
5380 * ... body ...
5381 * JUMP_ALWAYS end
5382 * elseif:
5383 * EVAL expr Push result of "expr"
5384 * JUMP_IF_FALSE else
5385 * ... body ...
5386 * JUMP_ALWAYS end
5387 * else:
5388 * ... body ...
5389 * end:
5390 */
5391 static char_u *
5392compile_if(char_u *arg, cctx_T *cctx)
5393{
5394 char_u *p = arg;
5395 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005396 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005398 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
Bram Moolenaara5565e42020-05-09 15:44:01 +02005401 CLEAR_FIELD(ppconst);
5402 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005403 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005404 clear_ppconst(&ppconst);
5405 return NULL;
5406 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005407 if (cctx->ctx_skip == SKIP_YES)
5408 clear_ppconst(&ppconst);
5409 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005410 {
5411 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005412 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005413 clear_ppconst(&ppconst);
5414 }
5415 else
5416 {
5417 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005418 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005419 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005420 return NULL;
5421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
5423 scope = new_scope(cctx, IF_SCOPE);
5424 if (scope == NULL)
5425 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005426 scope->se_skip_save = skip_save;
5427 // "is_had_return" will be reset if any block does not end in :return
5428 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005430 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005431 {
5432 // "where" is set when ":elseif", "else" or ":endif" is found
5433 scope->se_u.se_if.is_if_label = instr->ga_len;
5434 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5435 }
5436 else
5437 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438
5439 return p;
5440}
5441
5442 static char_u *
5443compile_elseif(char_u *arg, cctx_T *cctx)
5444{
5445 char_u *p = arg;
5446 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005447 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 isn_T *isn;
5449 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005450 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451
5452 if (scope == NULL || scope->se_type != IF_SCOPE)
5453 {
5454 emsg(_(e_elseif_without_if));
5455 return NULL;
5456 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005457 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005458 if (!cctx->ctx_had_return)
5459 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005461 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005462 {
5463 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005465 return NULL;
5466 // previous "if" or "elseif" jumps here
5467 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5468 isn->isn_arg.jump.jump_where = instr->ga_len;
5469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005471 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005472 CLEAR_FIELD(ppconst);
5473 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005474 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005475 clear_ppconst(&ppconst);
5476 return NULL;
5477 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005478 if (scope->se_skip_save == SKIP_YES)
5479 clear_ppconst(&ppconst);
5480 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005481 {
5482 // The expression results in a constant.
5483 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005484 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005485 clear_ppconst(&ppconst);
5486 scope->se_u.se_if.is_if_label = -1;
5487 }
5488 else
5489 {
5490 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005491 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005492 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005493 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005495 // "where" is set when ":elseif", "else" or ":endif" is found
5496 scope->se_u.se_if.is_if_label = instr->ga_len;
5497 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499
5500 return p;
5501}
5502
5503 static char_u *
5504compile_else(char_u *arg, cctx_T *cctx)
5505{
5506 char_u *p = arg;
5507 garray_T *instr = &cctx->ctx_instr;
5508 isn_T *isn;
5509 scope_T *scope = cctx->ctx_scope;
5510
5511 if (scope == NULL || scope->se_type != IF_SCOPE)
5512 {
5513 emsg(_(e_else_without_if));
5514 return NULL;
5515 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005516 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005517 if (!cctx->ctx_had_return)
5518 scope->se_u.se_if.is_had_return = FALSE;
5519 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520
Bram Moolenaarefd88552020-06-18 20:50:10 +02005521 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005522 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005523 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005524 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005525 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005526 if (!cctx->ctx_had_return
5527 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5528 JUMP_ALWAYS, cctx) == FAIL)
5529 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005530 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005531
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005532 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005533 {
5534 if (scope->se_u.se_if.is_if_label >= 0)
5535 {
5536 // previous "if" or "elseif" jumps here
5537 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5538 isn->isn_arg.jump.jump_where = instr->ga_len;
5539 scope->se_u.se_if.is_if_label = -1;
5540 }
5541 }
5542
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005543 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005544 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
5547 return p;
5548}
5549
5550 static char_u *
5551compile_endif(char_u *arg, cctx_T *cctx)
5552{
5553 scope_T *scope = cctx->ctx_scope;
5554 ifscope_T *ifscope;
5555 garray_T *instr = &cctx->ctx_instr;
5556 isn_T *isn;
5557
5558 if (scope == NULL || scope->se_type != IF_SCOPE)
5559 {
5560 emsg(_(e_endif_without_if));
5561 return NULL;
5562 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005563 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005564 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005565 if (!cctx->ctx_had_return)
5566 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005568 if (scope->se_u.se_if.is_if_label >= 0)
5569 {
5570 // previous "if" or "elseif" jumps here
5571 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5572 isn->isn_arg.jump.jump_where = instr->ga_len;
5573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574 // Fill in the "end" label in jumps at the end of the blocks.
5575 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005576 cctx->ctx_skip = scope->se_skip_save;
5577
5578 // If all the blocks end in :return and there is an :else then the
5579 // had_return flag is set.
5580 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005582 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583 return arg;
5584}
5585
5586/*
5587 * compile "for var in expr"
5588 *
5589 * Produces instructions:
5590 * PUSHNR -1
5591 * STORE loop-idx Set index to -1
5592 * EVAL expr Push result of "expr"
5593 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5594 * - if beyond end, jump to "end"
5595 * - otherwise get item from list and push it
5596 * STORE var Store item in "var"
5597 * ... body ...
5598 * JUMP top Jump back to repeat
5599 * end: DROP Drop the result of "expr"
5600 *
5601 */
5602 static char_u *
5603compile_for(char_u *arg, cctx_T *cctx)
5604{
5605 char_u *p;
5606 size_t varlen;
5607 garray_T *instr = &cctx->ctx_instr;
5608 garray_T *stack = &cctx->ctx_type_stack;
5609 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005610 lvar_T *loop_lvar; // loop iteration variable
5611 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 type_T *vartype;
5613
5614 // TODO: list of variables: "for [key, value] in dict"
5615 // parse "var"
5616 for (p = arg; eval_isnamec1(*p); ++p)
5617 ;
5618 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005619 var_lvar = lookup_local(arg, varlen, cctx);
5620 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 {
5622 semsg(_("E1023: variable already defined: %s"), arg);
5623 return NULL;
5624 }
5625
5626 // consume "in"
5627 p = skipwhite(p);
5628 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5629 {
5630 emsg(_(e_missing_in));
5631 return NULL;
5632 }
5633 p = skipwhite(p + 2);
5634
5635
5636 scope = new_scope(cctx, FOR_SCOPE);
5637 if (scope == NULL)
5638 return NULL;
5639
5640 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005641 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5642 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005643 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005644 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005645 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648
5649 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005650 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5651 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005652 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005653 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005654 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005658 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659
5660 // compile "expr", it remains on the stack until "endfor"
5661 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005662 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005663 {
5664 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005668 // Now that we know the type of "var", check that it is a list, now or at
5669 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005671 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005673 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 return NULL;
5675 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005676 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005677 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678
5679 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005680 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005682 generate_FOR(cctx, loop_lvar->lv_idx);
5683 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684
5685 return arg;
5686}
5687
5688/*
5689 * compile "endfor"
5690 */
5691 static char_u *
5692compile_endfor(char_u *arg, cctx_T *cctx)
5693{
5694 garray_T *instr = &cctx->ctx_instr;
5695 scope_T *scope = cctx->ctx_scope;
5696 forscope_T *forscope;
5697 isn_T *isn;
5698
5699 if (scope == NULL || scope->se_type != FOR_SCOPE)
5700 {
5701 emsg(_(e_for));
5702 return NULL;
5703 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005704 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005706 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707
5708 // At end of ":for" scope jump back to the FOR instruction.
5709 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5710
5711 // Fill in the "end" label in the FOR statement so it can jump here
5712 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5713 isn->isn_arg.forloop.for_end = instr->ga_len;
5714
5715 // Fill in the "end" label any BREAK statements
5716 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5717
5718 // Below the ":for" scope drop the "expr" list from the stack.
5719 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5720 return NULL;
5721
5722 vim_free(scope);
5723
5724 return arg;
5725}
5726
5727/*
5728 * compile "while expr"
5729 *
5730 * Produces instructions:
5731 * top: EVAL expr Push result of "expr"
5732 * JUMP_IF_FALSE end jump if false
5733 * ... body ...
5734 * JUMP top Jump back to repeat
5735 * end:
5736 *
5737 */
5738 static char_u *
5739compile_while(char_u *arg, cctx_T *cctx)
5740{
5741 char_u *p = arg;
5742 garray_T *instr = &cctx->ctx_instr;
5743 scope_T *scope;
5744
5745 scope = new_scope(cctx, WHILE_SCOPE);
5746 if (scope == NULL)
5747 return NULL;
5748
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005749 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750
5751 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005752 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753 return NULL;
5754
5755 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005756 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 JUMP_IF_FALSE, cctx) == FAIL)
5758 return FAIL;
5759
5760 return p;
5761}
5762
5763/*
5764 * compile "endwhile"
5765 */
5766 static char_u *
5767compile_endwhile(char_u *arg, cctx_T *cctx)
5768{
5769 scope_T *scope = cctx->ctx_scope;
5770
5771 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5772 {
5773 emsg(_(e_while));
5774 return NULL;
5775 }
5776 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005777 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778
5779 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005780 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781
5782 // Fill in the "end" label in the WHILE statement so it can jump here.
5783 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005784 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785
5786 vim_free(scope);
5787
5788 return arg;
5789}
5790
5791/*
5792 * compile "continue"
5793 */
5794 static char_u *
5795compile_continue(char_u *arg, cctx_T *cctx)
5796{
5797 scope_T *scope = cctx->ctx_scope;
5798
5799 for (;;)
5800 {
5801 if (scope == NULL)
5802 {
5803 emsg(_(e_continue));
5804 return NULL;
5805 }
5806 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5807 break;
5808 scope = scope->se_outer;
5809 }
5810
5811 // Jump back to the FOR or WHILE instruction.
5812 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005813 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5814 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 return arg;
5816}
5817
5818/*
5819 * compile "break"
5820 */
5821 static char_u *
5822compile_break(char_u *arg, cctx_T *cctx)
5823{
5824 scope_T *scope = cctx->ctx_scope;
5825 endlabel_T **el;
5826
5827 for (;;)
5828 {
5829 if (scope == NULL)
5830 {
5831 emsg(_(e_break));
5832 return NULL;
5833 }
5834 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5835 break;
5836 scope = scope->se_outer;
5837 }
5838
5839 // Jump to the end of the FOR or WHILE loop.
5840 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005841 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005843 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5845 return FAIL;
5846
5847 return arg;
5848}
5849
5850/*
5851 * compile "{" start of block
5852 */
5853 static char_u *
5854compile_block(char_u *arg, cctx_T *cctx)
5855{
5856 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5857 return NULL;
5858 return skipwhite(arg + 1);
5859}
5860
5861/*
5862 * compile end of block: drop one scope
5863 */
5864 static void
5865compile_endblock(cctx_T *cctx)
5866{
5867 scope_T *scope = cctx->ctx_scope;
5868
5869 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005870 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871 vim_free(scope);
5872}
5873
5874/*
5875 * compile "try"
5876 * Creates a new scope for the try-endtry, pointing to the first catch and
5877 * finally.
5878 * Creates another scope for the "try" block itself.
5879 * TRY instruction sets up exception handling at runtime.
5880 *
5881 * "try"
5882 * TRY -> catch1, -> finally push trystack entry
5883 * ... try block
5884 * "throw {exception}"
5885 * EVAL {exception}
5886 * THROW create exception
5887 * ... try block
5888 * " catch {expr}"
5889 * JUMP -> finally
5890 * catch1: PUSH exeception
5891 * EVAL {expr}
5892 * MATCH
5893 * JUMP nomatch -> catch2
5894 * CATCH remove exception
5895 * ... catch block
5896 * " catch"
5897 * JUMP -> finally
5898 * catch2: CATCH remove exception
5899 * ... catch block
5900 * " finally"
5901 * finally:
5902 * ... finally block
5903 * " endtry"
5904 * ENDTRY pop trystack entry, may rethrow
5905 */
5906 static char_u *
5907compile_try(char_u *arg, cctx_T *cctx)
5908{
5909 garray_T *instr = &cctx->ctx_instr;
5910 scope_T *try_scope;
5911 scope_T *scope;
5912
5913 // scope that holds the jumps that go to catch/finally/endtry
5914 try_scope = new_scope(cctx, TRY_SCOPE);
5915 if (try_scope == NULL)
5916 return NULL;
5917
5918 // "catch" is set when the first ":catch" is found.
5919 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005920 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921 if (generate_instr(cctx, ISN_TRY) == NULL)
5922 return NULL;
5923
5924 // scope for the try block itself
5925 scope = new_scope(cctx, BLOCK_SCOPE);
5926 if (scope == NULL)
5927 return NULL;
5928
5929 return arg;
5930}
5931
5932/*
5933 * compile "catch {expr}"
5934 */
5935 static char_u *
5936compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5937{
5938 scope_T *scope = cctx->ctx_scope;
5939 garray_T *instr = &cctx->ctx_instr;
5940 char_u *p;
5941 isn_T *isn;
5942
5943 // end block scope from :try or :catch
5944 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5945 compile_endblock(cctx);
5946 scope = cctx->ctx_scope;
5947
5948 // Error if not in a :try scope
5949 if (scope == NULL || scope->se_type != TRY_SCOPE)
5950 {
5951 emsg(_(e_catch));
5952 return NULL;
5953 }
5954
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005955 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 {
5957 emsg(_("E1033: catch unreachable after catch-all"));
5958 return NULL;
5959 }
5960
5961 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005962 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 JUMP_ALWAYS, cctx) == FAIL)
5964 return NULL;
5965
5966 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005967 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 if (isn->isn_arg.try.try_catch == 0)
5969 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005970 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 {
5972 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005973 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 isn->isn_arg.jump.jump_where = instr->ga_len;
5975 }
5976
5977 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005978 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005980 scope->se_u.se_try.ts_caught_all = TRUE;
5981 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982 }
5983 else
5984 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005985 char_u *end;
5986 char_u *pat;
5987 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005988 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005989 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 // Push v:exception, push {expr} and MATCH
5992 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5993
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005994 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005995 if (*end != *p)
5996 {
5997 semsg(_("E1067: Separator mismatch: %s"), p);
5998 vim_free(tofree);
5999 return FAIL;
6000 }
6001 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006002 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006003 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006004 len = (int)(end - tofree);
6005 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006006 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006007 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006008 if (pat == NULL)
6009 return FAIL;
6010 if (generate_PUSHS(cctx, pat) == FAIL)
6011 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6014 return NULL;
6015
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006016 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6018 return NULL;
6019 }
6020
6021 if (generate_instr(cctx, ISN_CATCH) == NULL)
6022 return NULL;
6023
6024 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6025 return NULL;
6026 return p;
6027}
6028
6029 static char_u *
6030compile_finally(char_u *arg, cctx_T *cctx)
6031{
6032 scope_T *scope = cctx->ctx_scope;
6033 garray_T *instr = &cctx->ctx_instr;
6034 isn_T *isn;
6035
6036 // end block scope from :try or :catch
6037 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6038 compile_endblock(cctx);
6039 scope = cctx->ctx_scope;
6040
6041 // Error if not in a :try scope
6042 if (scope == NULL || scope->se_type != TRY_SCOPE)
6043 {
6044 emsg(_(e_finally));
6045 return NULL;
6046 }
6047
6048 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006049 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 if (isn->isn_arg.try.try_finally != 0)
6051 {
6052 emsg(_(e_finally_dup));
6053 return NULL;
6054 }
6055
6056 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006057 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058
Bram Moolenaar585fea72020-04-02 22:33:21 +02006059 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006060 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 {
6062 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006063 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006065 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 }
6067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068 // TODO: set index in ts_finally_label jumps
6069
6070 return arg;
6071}
6072
6073 static char_u *
6074compile_endtry(char_u *arg, cctx_T *cctx)
6075{
6076 scope_T *scope = cctx->ctx_scope;
6077 garray_T *instr = &cctx->ctx_instr;
6078 isn_T *isn;
6079
6080 // end block scope from :catch or :finally
6081 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6082 compile_endblock(cctx);
6083 scope = cctx->ctx_scope;
6084
6085 // Error if not in a :try scope
6086 if (scope == NULL || scope->se_type != TRY_SCOPE)
6087 {
6088 if (scope == NULL)
6089 emsg(_(e_no_endtry));
6090 else if (scope->se_type == WHILE_SCOPE)
6091 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006092 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 emsg(_(e_endfor));
6094 else
6095 emsg(_(e_endif));
6096 return NULL;
6097 }
6098
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006099 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6101 {
6102 emsg(_("E1032: missing :catch or :finally"));
6103 return NULL;
6104 }
6105
6106 // Fill in the "end" label in jumps at the end of the blocks, if not done
6107 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006108 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
6110 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006111 if (isn->isn_arg.try.try_catch == 0)
6112 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 if (isn->isn_arg.try.try_finally == 0)
6114 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006115
6116 if (scope->se_u.se_try.ts_catch_label != 0)
6117 {
6118 // Last catch without match jumps here
6119 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6120 isn->isn_arg.jump.jump_where = instr->ga_len;
6121 }
6122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 compile_endblock(cctx);
6124
6125 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6126 return NULL;
6127 return arg;
6128}
6129
6130/*
6131 * compile "throw {expr}"
6132 */
6133 static char_u *
6134compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6135{
6136 char_u *p = skipwhite(arg);
6137
Bram Moolenaara5565e42020-05-09 15:44:01 +02006138 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 return NULL;
6140 if (may_generate_2STRING(-1, cctx) == FAIL)
6141 return NULL;
6142 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6143 return NULL;
6144
6145 return p;
6146}
6147
6148/*
6149 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006150 * compile "echomsg expr"
6151 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006152 * compile "execute expr"
6153 */
6154 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006155compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006156{
6157 char_u *p = arg;
6158 int count = 0;
6159
6160 for (;;)
6161 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006162 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006163 return NULL;
6164 ++count;
6165 p = skipwhite(p);
6166 if (ends_excmd(*p))
6167 break;
6168 }
6169
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006170 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6171 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6172 else if (cmdidx == CMD_execute)
6173 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6174 else if (cmdidx == CMD_echomsg)
6175 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6176 else
6177 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 return p;
6179}
6180
6181/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006182 * A command that is not compiled, execute with legacy code.
6183 */
6184 static char_u *
6185compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6186{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006187 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006188 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006189 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006190
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006191 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006192 goto theend;
6193
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006194 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006195 {
6196 long argt = excmd_get_argt(eap->cmdidx);
6197 int usefilter = FALSE;
6198
6199 has_expr = argt & (EX_XFILE | EX_EXPAND);
6200
6201 // If the command can be followed by a bar, find the bar and truncate
6202 // it, so that the following command can be compiled.
6203 // The '|' is overwritten with a NUL, it is put back below.
6204 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6205 && *eap->arg == '!')
6206 // :w !filter or :r !filter or :r! filter
6207 usefilter = TRUE;
6208 if ((argt & EX_TRLBAR) && !usefilter)
6209 {
6210 separate_nextcmd(eap);
6211 if (eap->nextcmd != NULL)
6212 nextcmd = eap->nextcmd;
6213 }
6214 }
6215
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006216 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6217 {
6218 // expand filename in "syntax include [@group] filename"
6219 has_expr = TRUE;
6220 eap->arg = skipwhite(eap->arg + 7);
6221 if (*eap->arg == '@')
6222 eap->arg = skiptowhite(eap->arg);
6223 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006224
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006225 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006226 {
6227 int count = 0;
6228 char_u *start = skipwhite(line);
6229
6230 // :cmd xxx`=expr1`yyy`=expr2`zzz
6231 // PUSHS ":cmd xxx"
6232 // eval expr1
6233 // PUSHS "yyy"
6234 // eval expr2
6235 // PUSHS "zzz"
6236 // EXECCONCAT 5
6237 for (;;)
6238 {
6239 if (p > start)
6240 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006241 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006242 ++count;
6243 }
6244 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006245 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006246 return NULL;
6247 may_generate_2STRING(-1, cctx);
6248 ++count;
6249 p = skipwhite(p);
6250 if (*p != '`')
6251 {
6252 emsg(_("E1083: missing backtick"));
6253 return NULL;
6254 }
6255 start = p + 1;
6256
6257 p = (char_u *)strstr((char *)start, "`=");
6258 if (p == NULL)
6259 {
6260 if (*skipwhite(start) != NUL)
6261 {
6262 generate_PUSHS(cctx, vim_strsave(start));
6263 ++count;
6264 }
6265 break;
6266 }
6267 }
6268 generate_EXECCONCAT(cctx, count);
6269 }
6270 else
6271 generate_EXEC(cctx, line);
6272
6273theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006274 if (*nextcmd != NUL)
6275 {
6276 // the parser expects a pointer to the bar, put it back
6277 --nextcmd;
6278 *nextcmd = '|';
6279 }
6280
6281 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006282}
6283
6284/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006285 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006286 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006287 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006288 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006289add_def_function(ufunc_T *ufunc)
6290{
6291 dfunc_T *dfunc;
6292
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006293 if (def_functions.ga_len == 0)
6294 {
6295 // The first position is not used, so that a zero uf_dfunc_idx means it
6296 // wasn't set.
6297 if (ga_grow(&def_functions, 1) == FAIL)
6298 return FAIL;
6299 ++def_functions.ga_len;
6300 }
6301
Bram Moolenaar09689a02020-05-09 22:50:08 +02006302 // Add the function to "def_functions".
6303 if (ga_grow(&def_functions, 1) == FAIL)
6304 return FAIL;
6305 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6306 CLEAR_POINTER(dfunc);
6307 dfunc->df_idx = def_functions.ga_len;
6308 ufunc->uf_dfunc_idx = dfunc->df_idx;
6309 dfunc->df_ufunc = ufunc;
6310 ++def_functions.ga_len;
6311 return OK;
6312}
6313
6314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 * After ex_function() has collected all the function lines: parse and compile
6316 * the lines into instructions.
6317 * Adds the function to "def_functions".
6318 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6319 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006320 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006321 * This can be used recursively through compile_lambda(), which may reallocate
6322 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006323 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006325 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006326compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 char_u *line = NULL;
6329 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 cctx_T cctx;
6332 garray_T *instr;
6333 int called_emsg_before = called_emsg;
6334 int ret = FAIL;
6335 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006336 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006337 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006338 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006340 // When using a function that was compiled before: Free old instructions.
6341 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006342 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6345 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006346 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006348 else
6349 {
6350 if (add_def_function(ufunc) == FAIL)
6351 return FAIL;
6352 new_def_function = TRUE;
6353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
Bram Moolenaar985116a2020-07-12 17:31:09 +02006355 ufunc->uf_def_status = UF_COMPILING;
6356
Bram Moolenaara80faa82020-04-12 19:37:17 +02006357 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 cctx.ctx_ufunc = ufunc;
6359 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006360 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6362 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6363 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6364 cctx.ctx_type_list = &ufunc->uf_type_list;
6365 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6366 instr = &cctx.ctx_instr;
6367
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006368 // Set the context to the function, it may be compiled when called from
6369 // another script. Set the script version to the most modern one.
6370 // The line number will be set in next_line_from_context().
6371 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6373
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006374 // Make sure error messages are OK.
6375 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6376 if (do_estack_push)
6377 estack_push_ufunc(ufunc, 1);
6378
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006379 if (ufunc->uf_def_args.ga_len > 0)
6380 {
6381 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006382 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006383 int i;
6384 char_u *arg;
6385 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006386 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006387
6388 // Produce instructions for the default values of optional arguments.
6389 // Store the instruction index in uf_def_arg_idx[] so that we know
6390 // where to start when the function is called, depending on the number
6391 // of arguments.
6392 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6393 if (ufunc->uf_def_arg_idx == NULL)
6394 goto erret;
6395 for (i = 0; i < count; ++i)
6396 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006397 garray_T *stack = &cctx.ctx_type_stack;
6398 type_T *val_type;
6399 int arg_idx = first_def_arg + i;
6400
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006401 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6402 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006403 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006404 goto erret;
6405
6406 // If no type specified use the type of the default value.
6407 // Otherwise check that the default value type matches the
6408 // specified type.
6409 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6410 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006411 {
6412 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006413 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006414 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006415 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006416 == FAIL)
6417 {
6418 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6419 arg_idx + 1);
6420 goto erret;
6421 }
6422
6423 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006424 goto erret;
6425 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006426 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006427
6428 if (did_set_arg_type)
6429 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006430 }
6431
6432 /*
6433 * Loop over all the lines of the function and generate instructions.
6434 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 for (;;)
6436 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006437 exarg_T ea;
6438 cmdmod_T save_cmdmod;
6439 int starts_with_colon = FALSE;
6440 char_u *cmd;
6441 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006442
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006443 // Bail out on the first error to avoid a flood of errors and report
6444 // the right line number when inside try/catch.
6445 if (emsg_before != called_emsg)
6446 goto erret;
6447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448 if (line != NULL && *line == '|')
6449 // the line continues after a '|'
6450 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006451 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006452 && !(*line == '#' && (line == cctx.ctx_line_start
6453 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006455 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456 goto erret;
6457 }
6458 else
6459 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006460 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006461 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006462 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006465 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466
Bram Moolenaara80faa82020-04-12 19:37:17 +02006467 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 ea.cmdlinep = &line;
6469 ea.cmd = skipwhite(line);
6470
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006471 // Some things can be recognized by the first character.
6472 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006474 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006475 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006476 if (ea.cmd[1] != '{')
6477 {
6478 line = (char_u *)"";
6479 continue;
6480 }
6481 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006483 case '}':
6484 {
6485 // "}" ends a block scope
6486 scopetype_T stype = cctx.ctx_scope == NULL
6487 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006489 if (stype == BLOCK_SCOPE)
6490 {
6491 compile_endblock(&cctx);
6492 line = ea.cmd;
6493 }
6494 else
6495 {
6496 emsg(_("E1025: using } outside of a block scope"));
6497 goto erret;
6498 }
6499 if (line != NULL)
6500 line = skipwhite(ea.cmd + 1);
6501 continue;
6502 }
6503
6504 case '{':
6505 // "{" starts a block scope
6506 // "{'a': 1}->func() is something else
6507 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6508 {
6509 line = compile_block(ea.cmd, &cctx);
6510 continue;
6511 }
6512 break;
6513
6514 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006515 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006516 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517 }
6518
6519 /*
6520 * COMMAND MODIFIERS
6521 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006522 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6524 {
6525 if (errormsg != NULL)
6526 goto erret;
6527 // empty line or comment
6528 line = (char_u *)"";
6529 continue;
6530 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006531 // TODO: use modifiers in the command
6532 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006533 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534
6535 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006536 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006538 {
6539 if (*ea.cmd == '(')
6540 // not for "call()"
6541 ea.cmd = p;
6542 else
6543 ea.cmd = skipwhite(ea.cmd);
6544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006546 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006548 char_u *pskip;
6549
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006551 // find what follows.
6552 // Skip over "var.member", "var[idx]" and the like.
6553 // Also "&opt = val", "$ENV = val" and "@r = val".
6554 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006555 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006556 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006557 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006559 char_u *var_end;
6560 int oplen;
6561 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562
Bram Moolenaar65821722020-08-02 18:58:54 +02006563 if (ea.cmd[0] == '@')
6564 var_end = ea.cmd + 2;
6565 else
6566 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006567 FNE_CHECK_START | FNE_INCL_BR);
6568 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006569 if (oplen > 0)
6570 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006571 size_t len = p - ea.cmd;
6572
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006573 // Recognize an assignment if we recognize the variable
6574 // name:
6575 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006576 // "local = expr" where "local" is a local var.
6577 // "script = expr" where "script" is a script-local var.
6578 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006579 // "&opt = expr"
6580 // "$ENV = expr"
6581 // "@r = expr"
6582 if (*ea.cmd == '&'
6583 || *ea.cmd == '$'
6584 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006585 || ((len) > 2 && ea.cmd[1] == ':')
6586 || lookup_local(ea.cmd, len, &cctx) != NULL
6587 || lookup_arg(ea.cmd, len, NULL, NULL,
6588 NULL, &cctx) == OK
6589 || lookup_script(ea.cmd, len) == OK
6590 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006591 {
6592 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006593 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006594 goto erret;
6595 continue;
6596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 }
6598 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006599
6600 if (*ea.cmd == '[')
6601 {
6602 // [var, var] = expr
6603 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6604 if (line == NULL)
6605 goto erret;
6606 if (line != ea.cmd)
6607 continue;
6608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 }
6610
6611 /*
6612 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006613 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006615 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006616 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006617 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006618 ea.cmd = skip_range(ea.cmd, NULL);
6619 if (ea.cmd > cmd && !starts_with_colon)
6620 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006621 emsg(_(e_colon_required_before_a_range));
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006622 goto erret;
6623 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006624 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006626 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006627 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
6629 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6630 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006631 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006632 {
6633 line += STRLEN(line);
6634 continue;
6635 }
6636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006638 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006640 // CMD_let cannot happen, compile_assignment() above is used
6641 iemsg("Command from find_ex_command() not handled");
6642 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 }
6645
6646 p = skipwhite(p);
6647
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006648 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006649 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006650 && ea.cmdidx != CMD_elseif
6651 && ea.cmdidx != CMD_else
6652 && ea.cmdidx != CMD_endif)
6653 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006654 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006655 continue;
6656 }
6657
Bram Moolenaarefd88552020-06-18 20:50:10 +02006658 if (ea.cmdidx != CMD_elseif
6659 && ea.cmdidx != CMD_else
6660 && ea.cmdidx != CMD_endif
6661 && ea.cmdidx != CMD_endfor
6662 && ea.cmdidx != CMD_endwhile
6663 && ea.cmdidx != CMD_catch
6664 && ea.cmdidx != CMD_finally
6665 && ea.cmdidx != CMD_endtry)
6666 {
6667 if (cctx.ctx_had_return)
6668 {
6669 emsg(_("E1095: Unreachable code after :return"));
6670 goto erret;
6671 }
6672 }
6673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 switch (ea.cmdidx)
6675 {
6676 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006677 ea.arg = p;
6678 line = compile_nested_function(&ea, &cctx);
6679 break;
6680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006682 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 goto erret;
6684
6685 case CMD_return:
6686 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006687 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 break;
6689
6690 case CMD_let:
6691 case CMD_const:
6692 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006693 if (line == p)
6694 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 break;
6696
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006697 case CMD_unlet:
6698 case CMD_unlockvar:
6699 case CMD_lockvar:
6700 line = compile_unletlock(p, &ea, &cctx);
6701 break;
6702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 case CMD_import:
6704 line = compile_import(p, &cctx);
6705 break;
6706
6707 case CMD_if:
6708 line = compile_if(p, &cctx);
6709 break;
6710 case CMD_elseif:
6711 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006712 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 break;
6714 case CMD_else:
6715 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006716 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 break;
6718 case CMD_endif:
6719 line = compile_endif(p, &cctx);
6720 break;
6721
6722 case CMD_while:
6723 line = compile_while(p, &cctx);
6724 break;
6725 case CMD_endwhile:
6726 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006727 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 break;
6729
6730 case CMD_for:
6731 line = compile_for(p, &cctx);
6732 break;
6733 case CMD_endfor:
6734 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006735 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 break;
6737 case CMD_continue:
6738 line = compile_continue(p, &cctx);
6739 break;
6740 case CMD_break:
6741 line = compile_break(p, &cctx);
6742 break;
6743
6744 case CMD_try:
6745 line = compile_try(p, &cctx);
6746 break;
6747 case CMD_catch:
6748 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006749 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 break;
6751 case CMD_finally:
6752 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006753 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 break;
6755 case CMD_endtry:
6756 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006757 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758 break;
6759 case CMD_throw:
6760 line = compile_throw(p, &cctx);
6761 break;
6762
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006763 case CMD_eval:
6764 if (compile_expr0(&p, &cctx) == FAIL)
6765 goto erret;
6766
6767 // drop the return value
6768 generate_instr_drop(&cctx, ISN_DROP, 1);
6769
6770 line = skipwhite(p);
6771 break;
6772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006775 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006776 case CMD_echomsg:
6777 case CMD_echoerr:
6778 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006779 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006781 // TODO: other commands with an expression argument
6782
Bram Moolenaarae616492020-07-28 20:07:27 +02006783 case CMD_append:
6784 case CMD_change:
6785 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006786 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006787 case CMD_xit:
6788 not_in_vim9(&ea);
6789 goto erret;
6790
Bram Moolenaar002262f2020-07-08 17:47:57 +02006791 case CMD_SIZE:
6792 semsg(_("E476: Invalid command: %s"), ea.cmd);
6793 goto erret;
6794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006796 // Not recognized, execute with do_cmdline_cmd().
6797 ea.arg = p;
6798 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 break;
6800 }
6801 if (line == NULL)
6802 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006803 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804
6805 if (cctx.ctx_type_stack.ga_len < 0)
6806 {
6807 iemsg("Type stack underflow");
6808 goto erret;
6809 }
6810 }
6811
6812 if (cctx.ctx_scope != NULL)
6813 {
6814 if (cctx.ctx_scope->se_type == IF_SCOPE)
6815 emsg(_(e_endif));
6816 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6817 emsg(_(e_endwhile));
6818 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6819 emsg(_(e_endfor));
6820 else
6821 emsg(_("E1026: Missing }"));
6822 goto erret;
6823 }
6824
Bram Moolenaarefd88552020-06-18 20:50:10 +02006825 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 {
6827 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6828 {
6829 emsg(_("E1027: Missing return statement"));
6830 goto erret;
6831 }
6832
6833 // Return zero if there is no return at the end.
6834 generate_PUSHNR(&cctx, 0);
6835 generate_instr(&cctx, ISN_RETURN);
6836 }
6837
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006838 {
6839 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6840 + ufunc->uf_dfunc_idx;
6841 dfunc->df_deleted = FALSE;
6842 dfunc->df_instr = instr->ga_data;
6843 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006844 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006845 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006846 if (cctx.ctx_outer_used)
6847 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006848 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850
6851 ret = OK;
6852
6853erret:
6854 if (ret == FAIL)
6855 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006856 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6858 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006859
6860 for (idx = 0; idx < instr->ga_len; ++idx)
6861 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006863
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006864 // If using the last entry in the table and it was added above, we
6865 // might as well remove it.
6866 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006867 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006868 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006869 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006870 ufunc->uf_dfunc_idx = 0;
6871 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006872 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006873
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006874 while (cctx.ctx_scope != NULL)
6875 drop_scope(&cctx);
6876
Bram Moolenaar20431c92020-03-20 18:39:46 +01006877 // Don't execute this function body.
6878 ga_clear_strings(&ufunc->uf_lines);
6879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 if (errormsg != NULL)
6881 emsg(errormsg);
6882 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006883 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 }
6885
6886 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006887 if (do_estack_push)
6888 estack_pop();
6889
Bram Moolenaar20431c92020-03-20 18:39:46 +01006890 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006891 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006893 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894}
6895
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006896 void
6897set_function_type(ufunc_T *ufunc)
6898{
6899 int varargs = ufunc->uf_va_name != NULL;
6900 int argcount = ufunc->uf_args.ga_len;
6901
6902 // Create a type for the function, with the return type and any
6903 // argument types.
6904 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6905 // The type is included in "tt_args".
6906 if (argcount > 0 || varargs)
6907 {
6908 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6909 argcount, &ufunc->uf_type_list);
6910 // Add argument types to the function type.
6911 if (func_type_add_arg_types(ufunc->uf_func_type,
6912 argcount + varargs,
6913 &ufunc->uf_type_list) == FAIL)
6914 return;
6915 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6916 ufunc->uf_func_type->tt_min_argcount =
6917 argcount - ufunc->uf_def_args.ga_len;
6918 if (ufunc->uf_arg_types == NULL)
6919 {
6920 int i;
6921
6922 // lambda does not have argument types.
6923 for (i = 0; i < argcount; ++i)
6924 ufunc->uf_func_type->tt_args[i] = &t_any;
6925 }
6926 else
6927 mch_memmove(ufunc->uf_func_type->tt_args,
6928 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6929 if (varargs)
6930 {
6931 ufunc->uf_func_type->tt_args[argcount] =
6932 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6933 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6934 }
6935 }
6936 else
6937 // No arguments, can use a predefined type.
6938 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6939 argcount, &ufunc->uf_type_list);
6940}
6941
6942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943/*
6944 * Delete an instruction, free what it contains.
6945 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006946 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947delete_instr(isn_T *isn)
6948{
6949 switch (isn->isn_type)
6950 {
6951 case ISN_EXEC:
6952 case ISN_LOADENV:
6953 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006954 case ISN_LOADB:
6955 case ISN_LOADW:
6956 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006958 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 case ISN_PUSHEXC:
6960 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006961 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006963 case ISN_STOREB:
6964 case ISN_STOREW:
6965 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006966 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 vim_free(isn->isn_arg.string);
6968 break;
6969
6970 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006971 case ISN_STORES:
6972 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 break;
6974
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006975 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006976 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006977 vim_free(isn->isn_arg.unlet.ul_name);
6978 break;
6979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 case ISN_STOREOPT:
6981 vim_free(isn->isn_arg.storeopt.so_name);
6982 break;
6983
6984 case ISN_PUSHBLOB: // push blob isn_arg.blob
6985 blob_unref(isn->isn_arg.blob);
6986 break;
6987
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006988 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006989#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006990 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006991#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006992 break;
6993
6994 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006996 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006997#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006998 break;
6999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 case ISN_UCALL:
7001 vim_free(isn->isn_arg.ufunc.cuf_name);
7002 break;
7003
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007004 case ISN_FUNCREF:
7005 {
7006 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7007 + isn->isn_arg.funcref.fr_func;
7008 func_ptr_unref(dfunc->df_ufunc);
7009 }
7010 break;
7011
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007012 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007013 {
7014 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7015 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7016
7017 if (ufunc != NULL)
7018 {
7019 // Clear uf_dfunc_idx so that the function is deleted.
7020 clear_def_function(ufunc);
7021 ufunc->uf_dfunc_idx = 0;
7022 func_ptr_unref(ufunc);
7023 }
7024
7025 vim_free(lambda);
7026 vim_free(isn->isn_arg.newfunc.nf_global);
7027 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007028 break;
7029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 case ISN_2BOOL:
7031 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007032 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 case ISN_ADDBLOB:
7034 case ISN_ADDLIST:
7035 case ISN_BCALL:
7036 case ISN_CATCH:
7037 case ISN_CHECKNR:
7038 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007039 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 case ISN_COMPAREANY:
7041 case ISN_COMPAREBLOB:
7042 case ISN_COMPAREBOOL:
7043 case ISN_COMPAREDICT:
7044 case ISN_COMPAREFLOAT:
7045 case ISN_COMPAREFUNC:
7046 case ISN_COMPARELIST:
7047 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 case ISN_COMPARESPECIAL:
7049 case ISN_COMPARESTRING:
7050 case ISN_CONCAT:
7051 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007052 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 case ISN_DROP:
7054 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007055 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007056 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007058 case ISN_EXECCONCAT:
7059 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007061 case ISN_LISTINDEX:
7062 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007063 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007064 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007065 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_JUMP:
7067 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007068 case ISN_LOADBDICT:
7069 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007070 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007072 case ISN_LOADSCRIPT:
7073 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007075 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 case ISN_NEGATENR:
7077 case ISN_NEWDICT:
7078 case ISN_NEWLIST:
7079 case ISN_OPNR:
7080 case ISN_OPFLOAT:
7081 case ISN_OPANY:
7082 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007083 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 case ISN_PUSHF:
7085 case ISN_PUSHNR:
7086 case ISN_PUSHBOOL:
7087 case ISN_PUSHSPEC:
7088 case ISN_RETURN:
7089 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007090 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007091 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007093 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007095 case ISN_STOREDICT:
7096 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 case ISN_THROW:
7098 case ISN_TRY:
7099 // nothing allocated
7100 break;
7101 }
7102}
7103
7104/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007105 * Free all instructions for "dfunc".
7106 */
7107 static void
7108delete_def_function_contents(dfunc_T *dfunc)
7109{
7110 int idx;
7111
7112 ga_clear(&dfunc->df_def_args_isn);
7113
7114 if (dfunc->df_instr != NULL)
7115 {
7116 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7117 delete_instr(dfunc->df_instr + idx);
7118 VIM_CLEAR(dfunc->df_instr);
7119 }
7120
7121 dfunc->df_deleted = TRUE;
7122}
7123
7124/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007125 * When a user function is deleted, clear the contents of any associated def
7126 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 */
7128 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007129clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007131 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 {
7133 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7134 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135
Bram Moolenaar20431c92020-03-20 18:39:46 +01007136 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007137 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 }
7139}
7140
7141#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007142/*
7143 * Free all functions defined with ":def".
7144 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 void
7146free_def_functions(void)
7147{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007148 int idx;
7149
7150 for (idx = 0; idx < def_functions.ga_len; ++idx)
7151 {
7152 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7153
7154 delete_def_function_contents(dfunc);
7155 }
7156
7157 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158}
7159#endif
7160
7161
7162#endif // FEAT_EVAL