blob: f1936452a94d39940d1e85278b90d94c20bd978b [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
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
262 * Lookup a variable in the current script.
263 * Returns OK or FAIL.
264 */
265 static int
266lookup_script(char_u *name, size_t len)
267{
268 int cc;
269 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
270 dictitem_T *di;
271
272 cc = name[len];
273 name[len] = NUL;
274 di = find_var_in_ht(ht, 0, name, TRUE);
275 name[len] = cc;
276 return di == NULL ? FAIL: OK;
277}
278
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100279/*
280 * Check if "p[len]" is already defined, either in script "import_sid" or in
281 * compilation context "cctx".
282 * Return FAIL and give an error if it defined.
283 */
284 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200285check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200287 int c = p[len];
288
289 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100290 if (lookup_script(p, len) == OK
291 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200292 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200293 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200294 || find_imported(p, len, cctx) != NULL
295 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200297 p[len] = c;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200298 semsg(_(e_name_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299 return FAIL;
300 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200301 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100302 return OK;
303}
304
Bram Moolenaar65b95452020-07-19 14:03:09 +0200305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306/////////////////////////////////////////////////////////////////////
307// Following generate_ functions expect the caller to call ga_grow().
308
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200309#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
310#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312/*
313 * Generate an instruction without arguments.
314 * Returns a pointer to the new instruction, NULL if failed.
315 */
316 static isn_T *
317generate_instr(cctx_T *cctx, isntype_T isn_type)
318{
319 garray_T *instr = &cctx->ctx_instr;
320 isn_T *isn;
321
Bram Moolenaar080457c2020-03-03 21:53:32 +0100322 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 if (ga_grow(instr, 1) == FAIL)
324 return NULL;
325 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
326 isn->isn_type = isn_type;
327 isn->isn_lnum = cctx->ctx_lnum + 1;
328 ++instr->ga_len;
329
330 return isn;
331}
332
333/*
334 * Generate an instruction without arguments.
335 * "drop" will be removed from the stack.
336 * Returns a pointer to the new instruction, NULL if failed.
337 */
338 static isn_T *
339generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
340{
341 garray_T *stack = &cctx->ctx_type_stack;
342
Bram Moolenaar080457c2020-03-03 21:53:32 +0100343 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 stack->ga_len -= drop;
345 return generate_instr(cctx, isn_type);
346}
347
348/*
349 * Generate instruction "isn_type" and put "type" on the type stack.
350 */
351 static isn_T *
352generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
353{
354 isn_T *isn;
355 garray_T *stack = &cctx->ctx_type_stack;
356
357 if ((isn = generate_instr(cctx, isn_type)) == NULL)
358 return NULL;
359
360 if (ga_grow(stack, 1) == FAIL)
361 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200362 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 ++stack->ga_len;
364
365 return isn;
366}
367
368/*
369 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200370 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 */
372 static int
373may_generate_2STRING(int offset, cctx_T *cctx)
374{
375 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200376 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 garray_T *stack = &cctx->ctx_type_stack;
378 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
379
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200380 switch ((*type)->tt_type)
381 {
382 // nothing to be done
383 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200385 // conversion possible
386 case VAR_SPECIAL:
387 case VAR_BOOL:
388 case VAR_NUMBER:
389 case VAR_FLOAT:
390 break;
391
392 // conversion possible (with runtime check)
393 case VAR_ANY:
394 case VAR_UNKNOWN:
395 isntype = ISN_2STRING_ANY;
396 break;
397
398 // conversion not possible
399 case VAR_VOID:
400 case VAR_BLOB:
401 case VAR_FUNC:
402 case VAR_PARTIAL:
403 case VAR_LIST:
404 case VAR_DICT:
405 case VAR_JOB:
406 case VAR_CHANNEL:
407 to_string_error((*type)->tt_type);
408 return FAIL;
409 }
410
411 *type = &t_string;
412 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 return FAIL;
414 isn->isn_arg.number = offset;
415
416 return OK;
417}
418
419 static int
420check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
421{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200422 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200424 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 {
426 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200427 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200429 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 return FAIL;
431 }
432 return OK;
433}
434
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200435 static int
436generate_add_instr(
437 cctx_T *cctx,
438 vartype_T vartype,
439 type_T *type1,
440 type_T *type2)
441{
442 isn_T *isn = generate_instr_drop(cctx,
443 vartype == VAR_NUMBER ? ISN_OPNR
444 : vartype == VAR_LIST ? ISN_ADDLIST
445 : vartype == VAR_BLOB ? ISN_ADDBLOB
446#ifdef FEAT_FLOAT
447 : vartype == VAR_FLOAT ? ISN_OPFLOAT
448#endif
449 : ISN_OPANY, 1);
450
451 if (vartype != VAR_LIST && vartype != VAR_BLOB
452 && type1->tt_type != VAR_ANY
453 && type2->tt_type != VAR_ANY
454 && check_number_or_float(
455 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
456 return FAIL;
457
458 if (isn != NULL)
459 isn->isn_arg.op.op_type = EXPR_ADD;
460 return isn == NULL ? FAIL : OK;
461}
462
463/*
464 * Get the type to use for an instruction for an operation on "type1" and
465 * "type2". If they are matching use a type-specific instruction. Otherwise
466 * fall back to runtime type checking.
467 */
468 static vartype_T
469operator_type(type_T *type1, type_T *type2)
470{
471 if (type1->tt_type == type2->tt_type
472 && (type1->tt_type == VAR_NUMBER
473 || type1->tt_type == VAR_LIST
474#ifdef FEAT_FLOAT
475 || type1->tt_type == VAR_FLOAT
476#endif
477 || type1->tt_type == VAR_BLOB))
478 return type1->tt_type;
479 return VAR_ANY;
480}
481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482/*
483 * Generate an instruction with two arguments. The instruction depends on the
484 * type of the arguments.
485 */
486 static int
487generate_two_op(cctx_T *cctx, char_u *op)
488{
489 garray_T *stack = &cctx->ctx_type_stack;
490 type_T *type1;
491 type_T *type2;
492 vartype_T vartype;
493 isn_T *isn;
494
Bram Moolenaar080457c2020-03-03 21:53:32 +0100495 RETURN_OK_IF_SKIP(cctx);
496
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200497 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
499 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200500 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501
502 switch (*op)
503 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200504 case '+':
505 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 break;
508
509 case '-':
510 case '*':
511 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
512 op) == FAIL)
513 return FAIL;
514 if (vartype == VAR_NUMBER)
515 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
516#ifdef FEAT_FLOAT
517 else if (vartype == VAR_FLOAT)
518 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
519#endif
520 else
521 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
522 if (isn != NULL)
523 isn->isn_arg.op.op_type = *op == '*'
524 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
525 break;
526
Bram Moolenaar4c683752020-04-05 21:38:23 +0200527 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200529 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 && type2->tt_type != VAR_NUMBER))
531 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200532 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return FAIL;
534 }
535 isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
537 if (isn != NULL)
538 isn->isn_arg.op.op_type = EXPR_REM;
539 break;
540 }
541
542 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200543 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544 {
545 type_T *type = &t_any;
546
547#ifdef FEAT_FLOAT
548 // float+number and number+float results in float
549 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
550 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
551 type = &t_float;
552#endif
553 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
554 }
555
556 return OK;
557}
558
559/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200560 * Get the instruction to use for comparing "type1" with "type2"
561 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200563 static isntype_T
564get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565{
566 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567
Bram Moolenaar4c683752020-04-05 21:38:23 +0200568 if (type1 == VAR_UNKNOWN)
569 type1 = VAR_ANY;
570 if (type2 == VAR_UNKNOWN)
571 type2 = VAR_ANY;
572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 if (type1 == type2)
574 {
575 switch (type1)
576 {
577 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
578 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
579 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
580 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
581 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
582 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
583 case VAR_LIST: isntype = ISN_COMPARELIST; break;
584 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
585 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 default: isntype = ISN_COMPAREANY; break;
587 }
588 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200589 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
591 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
592 isntype = ISN_COMPAREANY;
593
594 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
595 && (isntype == ISN_COMPAREBOOL
596 || isntype == ISN_COMPARESPECIAL
597 || isntype == ISN_COMPARENR
598 || isntype == ISN_COMPAREFLOAT))
599 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200600 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200602 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 }
604 if (isntype == ISN_DROP
605 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
606 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
607 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
608 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
609 && exptype != EXPR_IS && exptype != EXPR_ISNOT
610 && (type1 == VAR_BLOB || type2 == VAR_BLOB
611 || type1 == VAR_LIST || type2 == VAR_LIST))))
612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200613 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200615 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200617 return isntype;
618}
619
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200620 int
621check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
622{
623 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
624 return FAIL;
625 return OK;
626}
627
Bram Moolenaara5565e42020-05-09 15:44:01 +0200628/*
629 * Generate an ISN_COMPARE* instruction with a boolean result.
630 */
631 static int
632generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
633{
634 isntype_T isntype;
635 isn_T *isn;
636 garray_T *stack = &cctx->ctx_type_stack;
637 vartype_T type1;
638 vartype_T type2;
639
640 RETURN_OK_IF_SKIP(cctx);
641
642 // Get the known type of the two items on the stack. If they are matching
643 // use a type-specific instruction. Otherwise fall back to runtime type
644 // checking.
645 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
646 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
647 isntype = get_compare_isn(exptype, type1, type2);
648 if (isntype == ISN_DROP)
649 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650
651 if ((isn = generate_instr(cctx, isntype)) == NULL)
652 return FAIL;
653 isn->isn_arg.op.op_type = exptype;
654 isn->isn_arg.op.op_ic = ic;
655
656 // takes two arguments, puts one bool back
657 if (stack->ga_len >= 2)
658 {
659 --stack->ga_len;
660 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
661 }
662
663 return OK;
664}
665
666/*
667 * Generate an ISN_2BOOL instruction.
668 */
669 static int
670generate_2BOOL(cctx_T *cctx, int invert)
671{
672 isn_T *isn;
673 garray_T *stack = &cctx->ctx_type_stack;
674
Bram Moolenaar080457c2020-03-03 21:53:32 +0100675 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
677 return FAIL;
678 isn->isn_arg.number = invert;
679
680 // type becomes bool
681 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
682
683 return OK;
684}
685
686 static int
687generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
688{
689 isn_T *isn;
690 garray_T *stack = &cctx->ctx_type_stack;
691
Bram Moolenaar080457c2020-03-03 21:53:32 +0100692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
694 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200695 // TODO: whole type, e.g. for a function also arg and return types
696 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 isn->isn_arg.type.ct_off = offset;
698
699 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200700 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
702 return OK;
703}
704
705/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200706 * Check that
707 * - "actual" is "expected" type or
708 * - "actual" is a type that can be "expected" type: add a runtime check; or
709 * - return FAIL.
710 */
711 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200712need_type(
713 type_T *actual,
714 type_T *expected,
715 int offset,
716 cctx_T *cctx,
717 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200718{
719 if (check_type(expected, actual, FALSE) == OK)
720 return OK;
721 if (actual->tt_type != VAR_ANY
722 && actual->tt_type != VAR_UNKNOWN
723 && !(actual->tt_type == VAR_FUNC
724 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
725 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200726 if (!silent)
727 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200728 return FAIL;
729 }
730 generate_TYPECHECK(cctx, expected, offset);
731 return OK;
732}
733
734/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 * Generate an ISN_PUSHNR instruction.
736 */
737 static int
738generate_PUSHNR(cctx_T *cctx, varnumber_T number)
739{
740 isn_T *isn;
741
Bram Moolenaar080457c2020-03-03 21:53:32 +0100742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
744 return FAIL;
745 isn->isn_arg.number = number;
746
747 return OK;
748}
749
750/*
751 * Generate an ISN_PUSHBOOL instruction.
752 */
753 static int
754generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
755{
756 isn_T *isn;
757
Bram Moolenaar080457c2020-03-03 21:53:32 +0100758 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
760 return FAIL;
761 isn->isn_arg.number = number;
762
763 return OK;
764}
765
766/*
767 * Generate an ISN_PUSHSPEC instruction.
768 */
769 static int
770generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
771{
772 isn_T *isn;
773
Bram Moolenaar080457c2020-03-03 21:53:32 +0100774 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
776 return FAIL;
777 isn->isn_arg.number = number;
778
779 return OK;
780}
781
782#ifdef FEAT_FLOAT
783/*
784 * Generate an ISN_PUSHF instruction.
785 */
786 static int
787generate_PUSHF(cctx_T *cctx, float_T fnumber)
788{
789 isn_T *isn;
790
Bram Moolenaar080457c2020-03-03 21:53:32 +0100791 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
793 return FAIL;
794 isn->isn_arg.fnumber = fnumber;
795
796 return OK;
797}
798#endif
799
800/*
801 * Generate an ISN_PUSHS instruction.
802 * Consumes "str".
803 */
804 static int
805generate_PUSHS(cctx_T *cctx, char_u *str)
806{
807 isn_T *isn;
808
Bram Moolenaar080457c2020-03-03 21:53:32 +0100809 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
811 return FAIL;
812 isn->isn_arg.string = str;
813
814 return OK;
815}
816
817/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100818 * Generate an ISN_PUSHCHANNEL instruction.
819 * Consumes "channel".
820 */
821 static int
822generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
823{
824 isn_T *isn;
825
Bram Moolenaar080457c2020-03-03 21:53:32 +0100826 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100827 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
828 return FAIL;
829 isn->isn_arg.channel = channel;
830
831 return OK;
832}
833
834/*
835 * Generate an ISN_PUSHJOB instruction.
836 * Consumes "job".
837 */
838 static int
839generate_PUSHJOB(cctx_T *cctx, job_T *job)
840{
841 isn_T *isn;
842
Bram Moolenaar080457c2020-03-03 21:53:32 +0100843 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100844 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100845 return FAIL;
846 isn->isn_arg.job = job;
847
848 return OK;
849}
850
851/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 * Generate an ISN_PUSHBLOB instruction.
853 * Consumes "blob".
854 */
855 static int
856generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
857{
858 isn_T *isn;
859
Bram Moolenaar080457c2020-03-03 21:53:32 +0100860 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
862 return FAIL;
863 isn->isn_arg.blob = blob;
864
865 return OK;
866}
867
868/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100869 * Generate an ISN_PUSHFUNC instruction with name "name".
870 * Consumes "name".
871 */
872 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200873generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100874{
875 isn_T *isn;
876
Bram Moolenaar080457c2020-03-03 21:53:32 +0100877 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200878 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100879 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200880 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100881
882 return OK;
883}
884
885/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200886 * Generate an ISN_GETITEM instruction with "index".
887 */
888 static int
889generate_GETITEM(cctx_T *cctx, int index)
890{
891 isn_T *isn;
892 garray_T *stack = &cctx->ctx_type_stack;
893 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
894 type_T *item_type = &t_any;
895
896 RETURN_OK_IF_SKIP(cctx);
897
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200898 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200899 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200900 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200901 emsg(_(e_listreq));
902 return FAIL;
903 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200904 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200905 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
906 return FAIL;
907 isn->isn_arg.number = index;
908
909 // add the item type to the type stack
910 if (ga_grow(stack, 1) == FAIL)
911 return FAIL;
912 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
913 ++stack->ga_len;
914 return OK;
915}
916
917/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200918 * Generate an ISN_SLICE instruction with "count".
919 */
920 static int
921generate_SLICE(cctx_T *cctx, int count)
922{
923 isn_T *isn;
924
925 RETURN_OK_IF_SKIP(cctx);
926 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
927 return FAIL;
928 isn->isn_arg.number = count;
929 return OK;
930}
931
932/*
933 * Generate an ISN_CHECKLEN instruction with "min_len".
934 */
935 static int
936generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
937{
938 isn_T *isn;
939
940 RETURN_OK_IF_SKIP(cctx);
941
942 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
943 return FAIL;
944 isn->isn_arg.checklen.cl_min_len = min_len;
945 isn->isn_arg.checklen.cl_more_OK = more_OK;
946
947 return OK;
948}
949
950/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 * Generate an ISN_STORE instruction.
952 */
953 static int
954generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
955{
956 isn_T *isn;
957
Bram Moolenaar080457c2020-03-03 21:53:32 +0100958 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
960 return FAIL;
961 if (name != NULL)
962 isn->isn_arg.string = vim_strsave(name);
963 else
964 isn->isn_arg.number = idx;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
971 */
972 static int
973generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
974{
975 isn_T *isn;
976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
979 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100980 isn->isn_arg.storenr.stnr_idx = idx;
981 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982
983 return OK;
984}
985
986/*
987 * Generate an ISN_STOREOPT instruction
988 */
989 static int
990generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
991{
992 isn_T *isn;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +0200995 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 return FAIL;
997 isn->isn_arg.storeopt.so_name = vim_strsave(name);
998 isn->isn_arg.storeopt.so_flags = opt_flags;
999
1000 return OK;
1001}
1002
1003/*
1004 * Generate an ISN_LOAD or similar instruction.
1005 */
1006 static int
1007generate_LOAD(
1008 cctx_T *cctx,
1009 isntype_T isn_type,
1010 int idx,
1011 char_u *name,
1012 type_T *type)
1013{
1014 isn_T *isn;
1015
Bram Moolenaar080457c2020-03-03 21:53:32 +01001016 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1018 return FAIL;
1019 if (name != NULL)
1020 isn->isn_arg.string = vim_strsave(name);
1021 else
1022 isn->isn_arg.number = idx;
1023
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001028 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001029 */
1030 static int
1031generate_LOADV(
1032 cctx_T *cctx,
1033 char_u *name,
1034 int error)
1035{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001036 int di_flags;
1037 int vidx = find_vim_var(name, &di_flags);
1038 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001039
Bram Moolenaar080457c2020-03-03 21:53:32 +01001040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001041 if (vidx < 0)
1042 {
1043 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001044 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001045 return FAIL;
1046 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001047 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001048
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001049 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001050}
1051
1052/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001053 * Generate an ISN_UNLET instruction.
1054 */
1055 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001056generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001057{
1058 isn_T *isn;
1059
1060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001061 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001062 return FAIL;
1063 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1064 isn->isn_arg.unlet.ul_forceit = forceit;
1065
1066 return OK;
1067}
1068
1069/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 * Generate an ISN_LOADS instruction.
1071 */
1072 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001073generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001075 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001077 int sid,
1078 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079{
1080 isn_T *isn;
1081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001083 if (isn_type == ISN_LOADS)
1084 isn = generate_instr_type(cctx, isn_type, type);
1085 else
1086 isn = generate_instr_drop(cctx, isn_type, 1);
1087 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001089 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1090 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091
1092 return OK;
1093}
1094
1095/*
1096 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1097 */
1098 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001099generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 cctx_T *cctx,
1101 isntype_T isn_type,
1102 int sid,
1103 int idx,
1104 type_T *type)
1105{
1106 isn_T *isn;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if (isn_type == ISN_LOADSCRIPT)
1110 isn = generate_instr_type(cctx, isn_type, type);
1111 else
1112 isn = generate_instr_drop(cctx, isn_type, 1);
1113 if (isn == NULL)
1114 return FAIL;
1115 isn->isn_arg.script.script_sid = sid;
1116 isn->isn_arg.script.script_idx = idx;
1117 return OK;
1118}
1119
1120/*
1121 * Generate an ISN_NEWLIST instruction.
1122 */
1123 static int
1124generate_NEWLIST(cctx_T *cctx, int count)
1125{
1126 isn_T *isn;
1127 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 type_T *type;
1129 type_T *member;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = count;
1135
Bram Moolenaar127542b2020-08-09 17:22:04 +02001136 // get the member type from all the items on the stack.
1137 member = get_member_type_from_stack(
1138 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1139 cctx->ctx_type_list);
1140 type = get_list_type(member, cctx->ctx_type_list);
1141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 // drop the value types
1143 stack->ga_len -= count;
1144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 // add the list type to the type stack
1146 if (ga_grow(stack, 1) == FAIL)
1147 return FAIL;
1148 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1149 ++stack->ga_len;
1150
1151 return OK;
1152}
1153
1154/*
1155 * Generate an ISN_NEWDICT instruction.
1156 */
1157 static int
1158generate_NEWDICT(cctx_T *cctx, int count)
1159{
1160 isn_T *isn;
1161 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 type_T *type;
1163 type_T *member;
1164
Bram Moolenaar080457c2020-03-03 21:53:32 +01001165 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1167 return FAIL;
1168 isn->isn_arg.number = count;
1169
Bram Moolenaar127542b2020-08-09 17:22:04 +02001170 member = get_member_type_from_stack(
1171 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1172 cctx->ctx_type_list);
1173 type = get_dict_type(member, cctx->ctx_type_list);
1174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 // drop the key and value types
1176 stack->ga_len -= 2 * count;
1177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 // add the dict type to the type stack
1179 if (ga_grow(stack, 1) == FAIL)
1180 return FAIL;
1181 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1182 ++stack->ga_len;
1183
1184 return OK;
1185}
1186
1187/*
1188 * Generate an ISN_FUNCREF instruction.
1189 */
1190 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001191generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192{
1193 isn_T *isn;
1194 garray_T *stack = &cctx->ctx_type_stack;
1195
Bram Moolenaar080457c2020-03-03 21:53:32 +01001196 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1198 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001199 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001200 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201
1202 if (ga_grow(stack, 1) == FAIL)
1203 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001204 ((type_T **)stack->ga_data)[stack->ga_len] =
1205 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 ++stack->ga_len;
1207
1208 return OK;
1209}
1210
1211/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001212 * Generate an ISN_NEWFUNC instruction.
1213 */
1214 static int
1215generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1216{
1217 isn_T *isn;
1218 char_u *name;
1219
1220 RETURN_OK_IF_SKIP(cctx);
1221 name = vim_strsave(lambda_name);
1222 if (name == NULL)
1223 return FAIL;
1224 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.newfunc.nf_lambda = name;
1227 isn->isn_arg.newfunc.nf_global = func_name;
1228
1229 return OK;
1230}
1231
1232/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 * Generate an ISN_JUMP instruction.
1234 */
1235 static int
1236generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1237{
1238 isn_T *isn;
1239 garray_T *stack = &cctx->ctx_type_stack;
1240
Bram Moolenaar080457c2020-03-03 21:53:32 +01001241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1243 return FAIL;
1244 isn->isn_arg.jump.jump_when = when;
1245 isn->isn_arg.jump.jump_where = where;
1246
1247 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1248 --stack->ga_len;
1249
1250 return OK;
1251}
1252
1253 static int
1254generate_FOR(cctx_T *cctx, int loop_idx)
1255{
1256 isn_T *isn;
1257 garray_T *stack = &cctx->ctx_type_stack;
1258
Bram Moolenaar080457c2020-03-03 21:53:32 +01001259 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1261 return FAIL;
1262 isn->isn_arg.forloop.for_idx = loop_idx;
1263
1264 if (ga_grow(stack, 1) == FAIL)
1265 return FAIL;
1266 // type doesn't matter, will be stored next
1267 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1268 ++stack->ga_len;
1269
1270 return OK;
1271}
1272
1273/*
1274 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001275 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 * Return FAIL if the number of arguments is wrong.
1277 */
1278 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001279generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280{
1281 isn_T *isn;
1282 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001283 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001284 type_T *argtypes[MAX_FUNC_ARGS];
1285 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001288 argoff = check_internal_func(func_idx, argcount);
1289 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 return FAIL;
1291
Bram Moolenaar389df252020-07-09 21:20:47 +02001292 if (method_call && argoff > 1)
1293 {
1294 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1295 return FAIL;
1296 isn->isn_arg.shuffle.shfl_item = argcount;
1297 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1298 }
1299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1301 return FAIL;
1302 isn->isn_arg.bfunc.cbf_idx = func_idx;
1303 isn->isn_arg.bfunc.cbf_argcount = argcount;
1304
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001305 for (i = 0; i < argcount; ++i)
1306 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 stack->ga_len -= argcount; // drop the arguments
1309 if (ga_grow(stack, 1) == FAIL)
1310 return FAIL;
1311 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001312 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 ++stack->ga_len; // add return value
1314
1315 return OK;
1316}
1317
1318/*
1319 * Generate an ISN_DCALL or ISN_UCALL instruction.
1320 * Return FAIL if the number of arguments is wrong.
1321 */
1322 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001323generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324{
1325 isn_T *isn;
1326 garray_T *stack = &cctx->ctx_type_stack;
1327 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001328 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329
Bram Moolenaar080457c2020-03-03 21:53:32 +01001330 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 if (argcount > regular_args && !has_varargs(ufunc))
1332 {
1333 semsg(_(e_toomanyarg), ufunc->uf_name);
1334 return FAIL;
1335 }
1336 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1337 {
1338 semsg(_(e_toofewarg), ufunc->uf_name);
1339 return FAIL;
1340 }
1341
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001342 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001343 {
1344 int i;
1345
1346 for (i = 0; i < argcount; ++i)
1347 {
1348 type_T *expected;
1349 type_T *actual;
1350
1351 if (i < regular_args)
1352 {
1353 if (ufunc->uf_arg_types == NULL)
1354 continue;
1355 expected = ufunc->uf_arg_types[i];
1356 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001357 else if (ufunc->uf_va_type == NULL)
1358 // possibly a lambda
1359 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001360 else
1361 expected = ufunc->uf_va_type->tt_member;
1362 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001363 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001364 {
1365 arg_type_mismatch(expected, actual, i + 1);
1366 return FAIL;
1367 }
1368 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001369 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1371 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001372 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001373 }
1374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001376 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001377 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001379 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 {
1381 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1382 isn->isn_arg.dfunc.cdf_argcount = argcount;
1383 }
1384 else
1385 {
1386 // A user function may be deleted and redefined later, can't use the
1387 // ufunc pointer, need to look it up again at runtime.
1388 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1389 isn->isn_arg.ufunc.cuf_argcount = argcount;
1390 }
1391
1392 stack->ga_len -= argcount; // drop the arguments
1393 if (ga_grow(stack, 1) == FAIL)
1394 return FAIL;
1395 // add return value
1396 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1397 ++stack->ga_len;
1398
1399 return OK;
1400}
1401
1402/*
1403 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1404 */
1405 static int
1406generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1407{
1408 isn_T *isn;
1409 garray_T *stack = &cctx->ctx_type_stack;
1410
Bram Moolenaar080457c2020-03-03 21:53:32 +01001411 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1413 return FAIL;
1414 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1415 isn->isn_arg.ufunc.cuf_argcount = argcount;
1416
1417 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001418 if (ga_grow(stack, 1) == FAIL)
1419 return FAIL;
1420 // add return value
1421 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1422 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423
1424 return OK;
1425}
1426
1427/*
1428 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001429 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 */
1431 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001432generate_PCALL(
1433 cctx_T *cctx,
1434 int argcount,
1435 char_u *name,
1436 type_T *type,
1437 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438{
1439 isn_T *isn;
1440 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001441 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001444
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445 if (type->tt_type == VAR_ANY)
1446 ret_type = &t_any;
1447 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001448 {
1449 if (type->tt_argcount != -1)
1450 {
1451 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1452
1453 if (argcount < type->tt_min_argcount - varargs)
1454 {
1455 semsg(_(e_toofewarg), "[reference]");
1456 return FAIL;
1457 }
1458 if (!varargs && argcount > type->tt_argcount)
1459 {
1460 semsg(_(e_toomanyarg), "[reference]");
1461 return FAIL;
1462 }
1463 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001464 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001465 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001466 else
1467 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001468 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001469 return FAIL;
1470 }
1471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1473 return FAIL;
1474 isn->isn_arg.pfunc.cpf_top = at_top;
1475 isn->isn_arg.pfunc.cpf_argcount = argcount;
1476
1477 stack->ga_len -= argcount; // drop the arguments
1478
1479 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001480 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001482 // If partial is above the arguments it must be cleared and replaced with
1483 // the return value.
1484 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1485 return FAIL;
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 return OK;
1488}
1489
1490/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001491 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 */
1493 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001494generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495{
1496 isn_T *isn;
1497 garray_T *stack = &cctx->ctx_type_stack;
1498 type_T *type;
1499
Bram Moolenaar080457c2020-03-03 21:53:32 +01001500 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001501 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001503 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001505 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001507 if (type->tt_type != VAR_DICT && type != &t_any)
1508 {
1509 emsg(_(e_dictreq));
1510 return FAIL;
1511 }
1512 // change dict type to dict member type
1513 if (type->tt_type == VAR_DICT)
1514 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515
1516 return OK;
1517}
1518
1519/*
1520 * Generate an ISN_ECHO instruction.
1521 */
1522 static int
1523generate_ECHO(cctx_T *cctx, int with_white, int count)
1524{
1525 isn_T *isn;
1526
Bram Moolenaar080457c2020-03-03 21:53:32 +01001527 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1529 return FAIL;
1530 isn->isn_arg.echo.echo_with_white = with_white;
1531 isn->isn_arg.echo.echo_count = count;
1532
1533 return OK;
1534}
1535
Bram Moolenaarad39c092020-02-26 18:23:43 +01001536/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001537 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001538 */
1539 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001540generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001541{
1542 isn_T *isn;
1543
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545 return FAIL;
1546 isn->isn_arg.number = count;
1547
1548 return OK;
1549}
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 static int
1552generate_EXEC(cctx_T *cctx, char_u *line)
1553{
1554 isn_T *isn;
1555
Bram Moolenaar080457c2020-03-03 21:53:32 +01001556 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1558 return FAIL;
1559 isn->isn_arg.string = vim_strsave(line);
1560 return OK;
1561}
1562
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001563 static int
1564generate_EXECCONCAT(cctx_T *cctx, int count)
1565{
1566 isn_T *isn;
1567
1568 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1569 return FAIL;
1570 isn->isn_arg.number = count;
1571 return OK;
1572}
1573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574/*
1575 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001576 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001578 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1580{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 lvar_T *lvar;
1582
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001583 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001585 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001586 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 }
1588
1589 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001590 return NULL;
1591 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001593 // Every local variable uses the next entry on the stack. We could re-use
1594 // the last ones when leaving a scope, but then variables used in a closure
1595 // might get overwritten. To keep things simple do not re-use stack
1596 // entries. This is less efficient, but memory is cheap these days.
1597 lvar->lv_idx = cctx->ctx_locals_count++;
1598
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001599 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 lvar->lv_const = isConst;
1601 lvar->lv_type = type;
1602
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001603 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604}
1605
1606/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001607 * Remove local variables above "new_top".
1608 */
1609 static void
1610unwind_locals(cctx_T *cctx, int new_top)
1611{
1612 if (cctx->ctx_locals.ga_len > new_top)
1613 {
1614 int idx;
1615 lvar_T *lvar;
1616
1617 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1618 {
1619 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1620 vim_free(lvar->lv_name);
1621 }
1622 }
1623 cctx->ctx_locals.ga_len = new_top;
1624}
1625
1626/*
1627 * Free all local variables.
1628 */
1629 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001630free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001631{
1632 unwind_locals(cctx, 0);
1633 ga_clear(&cctx->ctx_locals);
1634}
1635
1636/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 * Find "name" in script-local items of script "sid".
1638 * Returns the index in "sn_var_vals" if found.
1639 * If found but not in "sn_var_vals" returns -1.
1640 * If not found returns -2.
1641 */
1642 int
1643get_script_item_idx(int sid, char_u *name, int check_writable)
1644{
1645 hashtab_T *ht;
1646 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001647 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 int idx;
1649
1650 // First look the name up in the hashtable.
1651 if (sid <= 0 || sid > script_items.ga_len)
1652 return -1;
1653 ht = &SCRIPT_VARS(sid);
1654 di = find_var_in_ht(ht, 0, name, TRUE);
1655 if (di == NULL)
1656 return -2;
1657
1658 // Now find the svar_T index in sn_var_vals.
1659 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1660 {
1661 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1662
1663 if (sv->sv_tv == &di->di_tv)
1664 {
1665 if (check_writable && sv->sv_const)
1666 semsg(_(e_readonlyvar), name);
1667 return idx;
1668 }
1669 }
1670 return -1;
1671}
1672
1673/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001674 * Find "name" in imported items of the current script or in "cctx" if not
1675 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 */
1677 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001678find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 int idx;
1681
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001682 if (current_sctx.sc_sid <= 0)
1683 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 if (cctx != NULL)
1685 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1686 {
1687 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1688 + idx;
1689
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001690 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1691 : STRLEN(import->imp_name) == len
1692 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 return import;
1694 }
1695
Bram Moolenaarefa94442020-08-08 22:16:00 +02001696 return find_imported_in_script(name, len, current_sctx.sc_sid);
1697}
1698
1699 imported_T *
1700find_imported_in_script(char_u *name, size_t len, int sid)
1701{
1702 scriptitem_T *si = SCRIPT_ITEM(sid);
1703 int idx;
1704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1706 {
1707 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1708
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001709 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1710 : STRLEN(import->imp_name) == len
1711 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 return import;
1713 }
1714 return NULL;
1715}
1716
1717/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001718 * Free all imported variables.
1719 */
1720 static void
1721free_imported(cctx_T *cctx)
1722{
1723 int idx;
1724
1725 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1726 {
1727 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1728
1729 vim_free(import->imp_name);
1730 }
1731 ga_clear(&cctx->ctx_imports);
1732}
1733
1734/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001735 * Return TRUE if "p" points at a "#" but not at "#{".
1736 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001737 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001738vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001739{
1740 return p[0] == '#' && p[1] != '{';
1741}
1742
1743/*
1744 * Return a pointer to the next line that isn't empty or only contains a
1745 * comment. Skips over white space.
1746 * Returns NULL if there is none.
1747 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001748 char_u *
1749peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001750{
1751 int lnum = cctx->ctx_lnum;
1752
1753 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1754 {
1755 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001756 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001757
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001758 // ignore NULLs inserted for continuation lines
1759 if (line != NULL)
1760 {
1761 p = skipwhite(line);
1762 if (*p != NUL && !vim9_comment_start(p))
1763 return p;
1764 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001765 }
1766 return NULL;
1767}
1768
1769/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001770 * Called when checking for a following operator at "arg". When the rest of
1771 * the line is empty or only a comment, peek the next line. If there is a next
1772 * line return a pointer to it and set "nextp".
1773 * Otherwise skip over white space.
1774 */
1775 static char_u *
1776may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1777{
1778 char_u *p = skipwhite(arg);
1779
1780 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001781 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001782 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001783 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001784 if (*nextp != NULL)
1785 return *nextp;
1786 }
1787 return p;
1788}
1789
1790/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001791 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001792 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001793 * Returns NULL when at the end.
1794 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001795 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001796next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001797{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001798 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001799
1800 do
1801 {
1802 ++cctx->ctx_lnum;
1803 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001804 {
1805 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001806 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001807 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001808 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001809 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001810 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001811 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001812 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813 return line;
1814}
1815
1816/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001817 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001818 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001819 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1820 */
1821 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001822may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001823{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001824 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001825 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001826 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001827
1828 if (next == NULL)
1829 return FAIL;
1830 *arg = skipwhite(next);
1831 }
1832 return OK;
1833}
1834
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001835/*
1836 * Idem, and give an error when failed.
1837 */
1838 static int
1839may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1840{
1841 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1842 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001843 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001844 return FAIL;
1845 }
1846 return OK;
1847}
1848
1849
Bram Moolenaara5565e42020-05-09 15:44:01 +02001850// Structure passed between the compile_expr* functions to keep track of
1851// constants that have been parsed but for which no code was produced yet. If
1852// possible expressions on these constants are applied at compile time. If
1853// that is not possible, the code to push the constants needs to be generated
1854// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001855// Using 50 should be more than enough of 5 levels of ().
1856#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001857typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001858 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001859 int pp_used; // active entries in pp_tv[]
1860} ppconst_T;
1861
Bram Moolenaar1c747212020-05-09 18:28:34 +02001862static int compile_expr0(char_u **arg, cctx_T *cctx);
1863static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1864
Bram Moolenaara5565e42020-05-09 15:44:01 +02001865/*
1866 * Generate a PUSH instruction for "tv".
1867 * "tv" will be consumed or cleared.
1868 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1869 */
1870 static int
1871generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1872{
1873 if (tv != NULL)
1874 {
1875 switch (tv->v_type)
1876 {
1877 case VAR_UNKNOWN:
1878 break;
1879 case VAR_BOOL:
1880 generate_PUSHBOOL(cctx, tv->vval.v_number);
1881 break;
1882 case VAR_SPECIAL:
1883 generate_PUSHSPEC(cctx, tv->vval.v_number);
1884 break;
1885 case VAR_NUMBER:
1886 generate_PUSHNR(cctx, tv->vval.v_number);
1887 break;
1888#ifdef FEAT_FLOAT
1889 case VAR_FLOAT:
1890 generate_PUSHF(cctx, tv->vval.v_float);
1891 break;
1892#endif
1893 case VAR_BLOB:
1894 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1895 tv->vval.v_blob = NULL;
1896 break;
1897 case VAR_STRING:
1898 generate_PUSHS(cctx, tv->vval.v_string);
1899 tv->vval.v_string = NULL;
1900 break;
1901 default:
1902 iemsg("constant type not supported");
1903 clear_tv(tv);
1904 return FAIL;
1905 }
1906 tv->v_type = VAR_UNKNOWN;
1907 }
1908 return OK;
1909}
1910
1911/*
1912 * Generate code for any ppconst entries.
1913 */
1914 static int
1915generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1916{
1917 int i;
1918 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001919 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001920
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001921 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001922 for (i = 0; i < ppconst->pp_used; ++i)
1923 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1924 ret = FAIL;
1925 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001926 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001927 return ret;
1928}
1929
1930/*
1931 * Clear ppconst constants. Used when failing.
1932 */
1933 static void
1934clear_ppconst(ppconst_T *ppconst)
1935{
1936 int i;
1937
1938 for (i = 0; i < ppconst->pp_used; ++i)
1939 clear_tv(&ppconst->pp_tv[i]);
1940 ppconst->pp_used = 0;
1941}
1942
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001943/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001944 * Generate an instruction to load script-local variable "name", without the
1945 * leading "s:".
1946 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 */
1948 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001949compile_load_scriptvar(
1950 cctx_T *cctx,
1951 char_u *name, // variable NUL terminated
1952 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001953 char_u **end, // end of variable
1954 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001956 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1958 imported_T *import;
1959
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001960 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001962 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001963 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1964 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 }
1966 if (idx >= 0)
1967 {
1968 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1969
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001970 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 current_sctx.sc_sid, idx, sv->sv_type);
1972 return OK;
1973 }
1974
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001975 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if (import != NULL)
1977 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001978 if (import->imp_all)
1979 {
1980 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001981 char_u *exp_name;
1982 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001983 ufunc_T *ufunc;
1984 type_T *type;
1985
1986 // Used "import * as Name", need to lookup the member.
1987 if (*p != '.')
1988 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001989 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001990 return FAIL;
1991 }
1992 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001993 if (VIM_ISWHITE(*p))
1994 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001995 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001996 return FAIL;
1997 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001998
Bram Moolenaar1c991142020-07-04 13:15:31 +02001999 // isolate one name
2000 exp_name = p;
2001 while (eval_isnamec(*p))
2002 ++p;
2003 cc = *p;
2004 *p = NUL;
2005
2006 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2007 *p = cc;
2008 p = skipwhite(p);
2009
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002010 // TODO: what if it is a function?
2011 if (idx < 0)
2012 return FAIL;
2013 *end = p;
2014
2015 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2016 import->imp_sid,
2017 idx,
2018 type);
2019 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002020 else if (import->imp_funcname != NULL)
2021 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002022 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002023 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2024 import->imp_sid,
2025 import->imp_var_vals_idx,
2026 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 return OK;
2028 }
2029
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002030 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002031 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 return FAIL;
2033}
2034
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002035 static int
2036generate_funcref(cctx_T *cctx, char_u *name)
2037{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002038 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039
2040 if (ufunc == NULL)
2041 return FAIL;
2042
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002043 // Need to compile any default values to get the argument types.
2044 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2045 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2046 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002047 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002048}
2049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050/*
2051 * Compile a variable name into a load instruction.
2052 * "end" points to just after the name.
2053 * When "error" is FALSE do not give an error when not found.
2054 */
2055 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002056compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057{
2058 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002059 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002060 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002062 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063
2064 if (*(*arg + 1) == ':')
2065 {
2066 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002067 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002068 {
2069 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002071 switch (**arg)
2072 {
2073 case 'g': isn_type = ISN_LOADGDICT; break;
2074 case 'w': isn_type = ISN_LOADWDICT; break;
2075 case 't': isn_type = ISN_LOADTDICT; break;
2076 case 'b': isn_type = ISN_LOADBDICT; break;
2077 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002078 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002079 goto theend;
2080 }
2081 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2082 goto theend;
2083 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 else
2086 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002087 isntype_T isn_type = ISN_DROP;
2088
2089 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2090 if (name == NULL)
2091 return FAIL;
2092
2093 switch (**arg)
2094 {
2095 case 'v': res = generate_LOADV(cctx, name, error);
2096 break;
2097 case 's': res = compile_load_scriptvar(cctx, name,
2098 NULL, NULL, error);
2099 break;
2100 case 'g': isn_type = ISN_LOADG; break;
2101 case 'w': isn_type = ISN_LOADW; break;
2102 case 't': isn_type = ISN_LOADT; break;
2103 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002104 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002105 goto theend;
2106 }
2107 if (isn_type != ISN_DROP)
2108 {
2109 // Global, Buffer-local, Window-local and Tabpage-local
2110 // variables can be defined later, thus we don't check if it
2111 // exists, give error at runtime.
2112 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 }
2115 }
2116 else
2117 {
2118 size_t len = end - *arg;
2119 int idx;
2120 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002121 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
2123 name = vim_strnsave(*arg, end - *arg);
2124 if (name == NULL)
2125 return FAIL;
2126
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002127 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002129 if (!gen_load_outer)
2130 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 }
2132 else
2133 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002134 lvar_T *lvar = lookup_local(*arg, len, cctx);
2135
2136 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002138 type = lvar->lv_type;
2139 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002140 if (lvar->lv_from_outer)
2141 gen_load_outer = TRUE;
2142 else
2143 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 }
2145 else
2146 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002147 // "var" can be script-local even without using "s:" if it
2148 // already exists.
2149 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2150 == SCRIPT_VERSION_VIM9
2151 || lookup_script(*arg, len) == OK)
2152 res = compile_load_scriptvar(cctx, name, *arg, &end,
2153 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002154
Bram Moolenaara5565e42020-05-09 15:44:01 +02002155 // When the name starts with an uppercase letter or "x:" it
2156 // can be a user defined function.
2157 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2158 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 }
2160 }
2161 if (gen_load)
2162 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002163 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002164 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002165 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002166 cctx->ctx_outer_used = TRUE;
2167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 }
2169
2170 *arg = end;
2171
2172theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002173 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002174 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 vim_free(name);
2176 return res;
2177}
2178
2179/*
2180 * Compile the argument expressions.
2181 * "arg" points to just after the "(" and is advanced to after the ")"
2182 */
2183 static int
2184compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2185{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002186 char_u *p = *arg;
2187 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188
Bram Moolenaare6085c52020-04-12 20:19:16 +02002189 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002191 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2192 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002193 if (*p == ')')
2194 {
2195 *arg = p + 1;
2196 return OK;
2197 }
2198
Bram Moolenaara5565e42020-05-09 15:44:01 +02002199 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 return FAIL;
2201 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002202
2203 if (*p != ',' && *skipwhite(p) == ',')
2204 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002205 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002206 p = skipwhite(p);
2207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002209 {
2210 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002211 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002212 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002214 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002215 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002217failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002218 emsg(_(e_missing_close));
2219 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220}
2221
2222/*
2223 * Compile a function call: name(arg1, arg2)
2224 * "arg" points to "name", "arg + varlen" to the "(".
2225 * "argcount_init" is 1 for "value->method()"
2226 * Instructions:
2227 * EVAL arg1
2228 * EVAL arg2
2229 * BCALL / DCALL / UCALL
2230 */
2231 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002232compile_call(
2233 char_u **arg,
2234 size_t varlen,
2235 cctx_T *cctx,
2236 ppconst_T *ppconst,
2237 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238{
2239 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002240 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 int argcount = argcount_init;
2242 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002243 char_u fname_buf[FLEN_FIXED + 1];
2244 char_u *tofree = NULL;
2245 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002247 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002248 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249
Bram Moolenaara5565e42020-05-09 15:44:01 +02002250 // we can evaluate "has('name')" at compile time
2251 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2252 {
2253 char_u *s = skipwhite(*arg + varlen + 1);
2254 typval_T argvars[2];
2255
2256 argvars[0].v_type = VAR_UNKNOWN;
2257 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002258 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002259 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002260 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002261 s = skipwhite(s);
2262 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2263 {
2264 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2265
2266 *arg = s + 1;
2267 argvars[1].v_type = VAR_UNKNOWN;
2268 tv->v_type = VAR_NUMBER;
2269 tv->vval.v_number = 0;
2270 f_has(argvars, tv);
2271 clear_tv(&argvars[0]);
2272 ++ppconst->pp_used;
2273 return OK;
2274 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002275 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002276 }
2277
2278 if (generate_ppconst(cctx, ppconst) == FAIL)
2279 return FAIL;
2280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 if (varlen >= sizeof(namebuf))
2282 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002283 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 return FAIL;
2285 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002286 vim_strncpy(namebuf, *arg, varlen);
2287 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288
2289 *arg = skipwhite(*arg + varlen + 1);
2290 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002291 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292
Bram Moolenaara1773442020-08-12 15:21:22 +02002293 is_autoload = vim_strchr(name, '#') != NULL;
2294 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 {
2296 int idx;
2297
2298 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002299 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002301 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002302 else
2303 semsg(_(e_unknownfunc), namebuf);
2304 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 }
2306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002308 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002310 {
2311 res = generate_CALL(cctx, ufunc, argcount);
2312 goto theend;
2313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314
2315 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002316 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002317 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002319 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002320 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002321 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002322 garray_T *stack = &cctx->ctx_type_stack;
2323 type_T *type;
2324
2325 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2326 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002327 goto theend;
2328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002330 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002331 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002332 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002333 res = generate_UCALL(cctx, name, argcount);
2334 else
2335 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002336
2337theend:
2338 vim_free(tofree);
2339 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340}
2341
2342// like NAMESPACE_CHAR but with 'a' and 'l'.
2343#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2344
2345/*
2346 * Find the end of a variable or function name. Unlike find_name_end() this
2347 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002348 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 * Return a pointer to just after the name. Equal to "arg" if there is no
2350 * valid name.
2351 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 static char_u *
2353to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354{
2355 char_u *p;
2356
2357 // Quick check for valid starting character.
2358 if (!eval_isnamec1(*arg))
2359 return arg;
2360
2361 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2362 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2363 // and can be used in slice "[n:]".
2364 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002365 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2367 break;
2368 return p;
2369}
2370
2371/*
2372 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002373 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 */
2375 char_u *
2376to_name_const_end(char_u *arg)
2377{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002378 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 typval_T rettv;
2380
2381 if (p == arg && *arg == '[')
2382 {
2383
2384 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002385 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 p = arg;
2387 }
2388 else if (p == arg && *arg == '#' && arg[1] == '{')
2389 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002390 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002392 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 p = arg;
2394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395
2396 return p;
2397}
2398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399/*
2400 * parse a list: [expr, expr]
2401 * "*arg" points to the '['.
2402 */
2403 static int
2404compile_list(char_u **arg, cctx_T *cctx)
2405{
2406 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002407 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 int count = 0;
2409
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002410 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002412 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002413 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002414 semsg(_(e_list_end), *arg);
2415 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002416 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002417 if (*p == ',')
2418 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002419 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002420 return FAIL;
2421 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002422 if (*p == ']')
2423 {
2424 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002425 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002426 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002427 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 break;
2429 ++count;
2430 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002433 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2434 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002435 semsg(_(e_white_space_required_after), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002436 return FAIL;
2437 }
2438 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002439 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 p = skipwhite(p);
2441 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002442 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
2444 generate_NEWLIST(cctx, count);
2445 return OK;
2446}
2447
2448/*
2449 * parse a lambda: {arg, arg -> expr}
2450 * "*arg" points to the '{'.
2451 */
2452 static int
2453compile_lambda(char_u **arg, cctx_T *cctx)
2454{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 typval_T rettv;
2456 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002457 evalarg_T evalarg;
2458
2459 CLEAR_FIELD(evalarg);
2460 evalarg.eval_flags = EVAL_EVALUATE;
2461 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462
2463 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002464 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002468 ++ufunc->uf_refcount;
2469 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002470 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
2472 // The function will have one line: "return {expr}".
2473 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002474 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002476 clear_evalarg(&evalarg, NULL);
2477
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002478 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002479 {
2480 // The return type will now be known.
2481 set_function_type(ufunc);
2482
2483 return generate_FUNCREF(cctx, ufunc);
2484 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002485
2486 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 return FAIL;
2488}
2489
2490/*
2491 * Compile a lamda call: expr->{lambda}(args)
2492 * "arg" points to the "{".
2493 */
2494 static int
2495compile_lambda_call(char_u **arg, cctx_T *cctx)
2496{
2497 ufunc_T *ufunc;
2498 typval_T rettv;
2499 int argcount = 1;
2500 int ret = FAIL;
2501
2502 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002503 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 return FAIL;
2505
2506 if (**arg != '(')
2507 {
2508 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002509 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 else
2511 semsg(_(e_missing_paren), "lambda");
2512 clear_tv(&rettv);
2513 return FAIL;
2514 }
2515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 ufunc = rettv.vval.v_partial->pt_func;
2517 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002518 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002519 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002520
2521 // The function will have one line: "return {expr}".
2522 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002523 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
2525 // compile the arguments
2526 *arg = skipwhite(*arg + 1);
2527 if (compile_arguments(arg, cctx, &argcount) == OK)
2528 // call the compiled function
2529 ret = generate_CALL(cctx, ufunc, argcount);
2530
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002531 if (ret == FAIL)
2532 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 return ret;
2534}
2535
2536/*
2537 * parse a dict: {'key': val} or #{key: val}
2538 * "*arg" points to the '{'.
2539 */
2540 static int
2541compile_dict(char_u **arg, cctx_T *cctx, int literal)
2542{
2543 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002544 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 int count = 0;
2546 dict_T *d = dict_alloc();
2547 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002548 char_u *whitep = *arg;
2549 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550
2551 if (d == NULL)
2552 return FAIL;
2553 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002554 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
2556 char_u *key = NULL;
2557
Bram Moolenaar23c55272020-06-21 16:58:13 +02002558 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002559 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002560 *arg = NULL;
2561 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002562 }
2563
2564 if (**arg == '}')
2565 break;
2566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (literal)
2568 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002569 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570
Bram Moolenaar2c330432020-04-13 14:41:35 +02002571 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002573 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 return FAIL;
2575 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002576 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (generate_PUSHS(cctx, key) == FAIL)
2578 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002579 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 }
2581 else
2582 {
2583 isn_T *isn;
2584
Bram Moolenaara5565e42020-05-09 15:44:01 +02002585 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2588 if (isn->isn_type == ISN_PUSHS)
2589 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002590 else
2591 {
2592 type_T *keytype = ((type_T **)stack->ga_data)
2593 [stack->ga_len - 1];
2594 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2595 return FAIL;
2596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 }
2598
2599 // Check for duplicate keys, if using string keys.
2600 if (key != NULL)
2601 {
2602 item = dict_find(d, key, -1);
2603 if (item != NULL)
2604 {
2605 semsg(_(e_duplicate_key), key);
2606 goto failret;
2607 }
2608 item = dictitem_alloc(key);
2609 if (item != NULL)
2610 {
2611 item->di_tv.v_type = VAR_UNKNOWN;
2612 item->di_tv.v_lock = 0;
2613 if (dict_add(d, item) == FAIL)
2614 dictitem_free(item);
2615 }
2616 }
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 if (**arg != ':')
2619 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002620 if (*skipwhite(*arg) == ':')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002621 semsg(_(e_no_white_space_allowed_before), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002622 else
2623 semsg(_(e_missing_dict_colon), *arg);
2624 return FAIL;
2625 }
2626 whitep = *arg + 1;
2627 if (!IS_WHITE_OR_NUL(*whitep))
2628 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002629 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 return FAIL;
2631 }
2632
2633 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002634 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002635 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002636 *arg = NULL;
2637 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002638 }
2639
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 return FAIL;
2642 ++count;
2643
Bram Moolenaar2c330432020-04-13 14:41:35 +02002644 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002645 *arg = skipwhite(*arg);
2646 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002647 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002648 *arg = NULL;
2649 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 if (**arg == '}')
2652 break;
2653 if (**arg != ',')
2654 {
2655 semsg(_(e_missing_dict_comma), *arg);
2656 goto failret;
2657 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002658 if (IS_WHITE_OR_NUL(*whitep))
2659 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002660 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002661 return FAIL;
2662 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002663 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 *arg = skipwhite(*arg + 1);
2665 }
2666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 *arg = *arg + 1;
2668
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002669 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002670 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002671 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002672 *arg += STRLEN(*arg);
2673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 dict_unref(d);
2675 return generate_NEWDICT(cctx, count);
2676
2677failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002678 if (*arg == NULL)
2679 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 dict_unref(d);
2681 return FAIL;
2682}
2683
2684/*
2685 * Compile "&option".
2686 */
2687 static int
2688compile_get_option(char_u **arg, cctx_T *cctx)
2689{
2690 typval_T rettv;
2691 char_u *start = *arg;
2692 int ret;
2693
2694 // parse the option and get the current value to get the type.
2695 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002696 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 if (ret == OK)
2698 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002699 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 char_u *name = vim_strnsave(start, *arg - start);
2701 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2702
2703 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2704 vim_free(name);
2705 }
2706 clear_tv(&rettv);
2707
2708 return ret;
2709}
2710
2711/*
2712 * Compile "$VAR".
2713 */
2714 static int
2715compile_get_env(char_u **arg, cctx_T *cctx)
2716{
2717 char_u *start = *arg;
2718 int len;
2719 int ret;
2720 char_u *name;
2721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 ++*arg;
2723 len = get_env_len(arg);
2724 if (len == 0)
2725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002726 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 return FAIL;
2728 }
2729
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002730 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 name = vim_strnsave(start, len + 1);
2732 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2733 vim_free(name);
2734 return ret;
2735}
2736
2737/*
2738 * Compile "@r".
2739 */
2740 static int
2741compile_get_register(char_u **arg, cctx_T *cctx)
2742{
2743 int ret;
2744
2745 ++*arg;
2746 if (**arg == NUL)
2747 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002748 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 return FAIL;
2750 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002751 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 {
2753 emsg_invreg(**arg);
2754 return FAIL;
2755 }
2756 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2757 ++*arg;
2758 return ret;
2759}
2760
2761/*
2762 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002763 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 */
2765 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002766apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002768 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769
2770 // this works from end to start
2771 while (p > start)
2772 {
2773 --p;
2774 if (*p == '-' || *p == '+')
2775 {
2776 // only '-' has an effect, for '+' we only check the type
2777#ifdef FEAT_FLOAT
2778 if (rettv->v_type == VAR_FLOAT)
2779 {
2780 if (*p == '-')
2781 rettv->vval.v_float = -rettv->vval.v_float;
2782 }
2783 else
2784#endif
2785 {
2786 varnumber_T val;
2787 int error = FALSE;
2788
2789 // tv_get_number_chk() accepts a string, but we don't want that
2790 // here
2791 if (check_not_string(rettv) == FAIL)
2792 return FAIL;
2793 val = tv_get_number_chk(rettv, &error);
2794 clear_tv(rettv);
2795 if (error)
2796 return FAIL;
2797 if (*p == '-')
2798 val = -val;
2799 rettv->v_type = VAR_NUMBER;
2800 rettv->vval.v_number = val;
2801 }
2802 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002803 else if (numeric_only)
2804 {
2805 ++p;
2806 break;
2807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 else
2809 {
2810 int v = tv2bool(rettv);
2811
2812 // '!' is permissive in the type.
2813 clear_tv(rettv);
2814 rettv->v_type = VAR_BOOL;
2815 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2816 }
2817 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002818 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 return OK;
2820}
2821
2822/*
2823 * Recognize v: variables that are constants and set "rettv".
2824 */
2825 static void
2826get_vim_constant(char_u **arg, typval_T *rettv)
2827{
2828 if (STRNCMP(*arg, "v:true", 6) == 0)
2829 {
2830 rettv->v_type = VAR_BOOL;
2831 rettv->vval.v_number = VVAL_TRUE;
2832 *arg += 6;
2833 }
2834 else if (STRNCMP(*arg, "v:false", 7) == 0)
2835 {
2836 rettv->v_type = VAR_BOOL;
2837 rettv->vval.v_number = VVAL_FALSE;
2838 *arg += 7;
2839 }
2840 else if (STRNCMP(*arg, "v:null", 6) == 0)
2841 {
2842 rettv->v_type = VAR_SPECIAL;
2843 rettv->vval.v_number = VVAL_NULL;
2844 *arg += 6;
2845 }
2846 else if (STRNCMP(*arg, "v:none", 6) == 0)
2847 {
2848 rettv->v_type = VAR_SPECIAL;
2849 rettv->vval.v_number = VVAL_NONE;
2850 *arg += 6;
2851 }
2852}
2853
Bram Moolenaar696ba232020-07-29 21:20:41 +02002854 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002855get_compare_type(char_u *p, int *len, int *type_is)
2856{
2857 exptype_T type = EXPR_UNKNOWN;
2858 int i;
2859
2860 switch (p[0])
2861 {
2862 case '=': if (p[1] == '=')
2863 type = EXPR_EQUAL;
2864 else if (p[1] == '~')
2865 type = EXPR_MATCH;
2866 break;
2867 case '!': if (p[1] == '=')
2868 type = EXPR_NEQUAL;
2869 else if (p[1] == '~')
2870 type = EXPR_NOMATCH;
2871 break;
2872 case '>': if (p[1] != '=')
2873 {
2874 type = EXPR_GREATER;
2875 *len = 1;
2876 }
2877 else
2878 type = EXPR_GEQUAL;
2879 break;
2880 case '<': if (p[1] != '=')
2881 {
2882 type = EXPR_SMALLER;
2883 *len = 1;
2884 }
2885 else
2886 type = EXPR_SEQUAL;
2887 break;
2888 case 'i': if (p[1] == 's')
2889 {
2890 // "is" and "isnot"; but not a prefix of a name
2891 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2892 *len = 5;
2893 i = p[*len];
2894 if (!isalnum(i) && i != '_')
2895 {
2896 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2897 *type_is = TRUE;
2898 }
2899 }
2900 break;
2901 }
2902 return type;
2903}
2904
2905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002907 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 */
2909 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002910compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002912 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913
2914 // this works from end to start
2915 while (p > start)
2916 {
2917 --p;
2918 if (*p == '-' || *p == '+')
2919 {
2920 int negate = *p == '-';
2921 isn_T *isn;
2922
2923 // TODO: check type
2924 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2925 {
2926 --p;
2927 if (*p == '-')
2928 negate = !negate;
2929 }
2930 // only '-' has an effect, for '+' we only check the type
2931 if (negate)
2932 isn = generate_instr(cctx, ISN_NEGATENR);
2933 else
2934 isn = generate_instr(cctx, ISN_CHECKNR);
2935 if (isn == NULL)
2936 return FAIL;
2937 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002938 else if (numeric_only)
2939 {
2940 ++p;
2941 break;
2942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 else
2944 {
2945 int invert = TRUE;
2946
2947 while (p > start && p[-1] == '!')
2948 {
2949 --p;
2950 invert = !invert;
2951 }
2952 if (generate_2BOOL(cctx, invert) == FAIL)
2953 return FAIL;
2954 }
2955 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002956 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 return OK;
2958}
2959
2960/*
2961 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002962 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 */
2964 static int
2965compile_subscript(
2966 char_u **arg,
2967 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002968 char_u *start_leader,
2969 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002970 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002972 char_u *name_start = *end_leader;
2973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 for (;;)
2975 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002976 char_u *p = skipwhite(*arg);
2977
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002978 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002979 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002980 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002981
2982 // If a following line starts with "->{" or "->X" advance to that
2983 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002984 // Also if a following line starts with ".x".
2985 if (next != NULL &&
2986 ((next[0] == '-' && next[1] == '>'
2987 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002988 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002989 {
2990 next = next_line_from_context(cctx, TRUE);
2991 if (next == NULL)
2992 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002993 *arg = next;
2994 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002995 }
2996 }
2997
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002998 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2999 // not a function call.
3000 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003002 garray_T *stack = &cctx->ctx_type_stack;
3003 type_T *type;
3004 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003006 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003007 return FAIL;
3008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003010 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3011
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003012 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3014 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003015 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 return FAIL;
3017 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003018 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003020 char_u *pstart = p;
3021
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003022 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003023 return FAIL;
3024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 // something->method()
3026 // Apply the '!', '-' and '+' first:
3027 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003028 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003031 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003032 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003033 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (**arg == '{')
3035 {
3036 // lambda call: list->{lambda}
3037 if (compile_lambda_call(arg, cctx) == FAIL)
3038 return FAIL;
3039 }
3040 else
3041 {
3042 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003043 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003044 if (!eval_isnamec1(*p))
3045 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003046 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003047 return FAIL;
3048 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003049 if (ASCII_ISALPHA(*p) && p[1] == ':')
3050 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003051 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 ;
3053 if (*p != '(')
3054 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003055 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 return FAIL;
3057 }
3058 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003059 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return FAIL;
3061 }
3062 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003063 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003065 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003066 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003067 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003068 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003069 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003070
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003071 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003072 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003073 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003074 // TODO: blob index
3075 // TODO: more arguments
3076 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003077 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003078 return FAIL;
3079
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003080 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003081 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003082 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003083 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003084 if (**arg == ':')
3085 // missing first index is equal to zero
3086 generate_PUSHNR(cctx, 0);
3087 else
3088 {
3089 if (compile_expr0(arg, cctx) == FAIL)
3090 return FAIL;
3091 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3092 return FAIL;
3093 *arg = skipwhite(*arg);
3094 }
3095 if (**arg == ':')
3096 {
3097 *arg = skipwhite(*arg + 1);
3098 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3099 return FAIL;
3100 if (**arg == ']')
3101 // missing second index is equal to end of string
3102 generate_PUSHNR(cctx, -1);
3103 else
3104 {
3105 if (compile_expr0(arg, cctx) == FAIL)
3106 return FAIL;
3107 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3108 return FAIL;
3109 *arg = skipwhite(*arg);
3110 }
3111 is_slice = TRUE;
3112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
3114 if (**arg != ']')
3115 {
3116 emsg(_(e_missbrac));
3117 return FAIL;
3118 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003119 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003121 // We can index a list and a dict. If we don't know the type
3122 // we can use the index value type.
3123 // TODO: If we don't know use an instruction to figure it out at
3124 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003125 typep = ((type_T **)stack->ga_data) + stack->ga_len
3126 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003127 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003128 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3129 // If the index is a string, the variable must be a Dict.
3130 if (*typep == &t_any && valtype == &t_string)
3131 vtype = VAR_DICT;
3132 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003133 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003134 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3135 return FAIL;
3136 if (is_slice)
3137 {
3138 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3139 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3140 return FAIL;
3141 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003142 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003143
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003144 if (vtype == VAR_DICT)
3145 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003146 if (is_slice)
3147 {
3148 emsg(_(e_cannot_slice_dictionary));
3149 return FAIL;
3150 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003151 if ((*typep)->tt_type == VAR_DICT)
3152 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003153 else
3154 {
3155 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3156 return FAIL;
3157 *typep = &t_any;
3158 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003159 if (may_generate_2STRING(-1, cctx) == FAIL)
3160 return FAIL;
3161 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3162 return FAIL;
3163 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003164 else if (vtype == VAR_STRING)
3165 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003166 *typep = &t_string;
3167 if ((is_slice
3168 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3169 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003170 return FAIL;
3171 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003172 else if (vtype == VAR_BLOB)
3173 {
3174 emsg("Sorry, blob index and slice not implemented yet");
3175 return FAIL;
3176 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003177 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003178 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003179 if (is_slice)
3180 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003181 if (generate_instr_drop(cctx,
3182 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3183 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003184 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003185 }
Bram Moolenaared591872020-08-15 22:14:53 +02003186 else
3187 {
3188 if ((*typep)->tt_type == VAR_LIST)
3189 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003190 if (generate_instr_drop(cctx,
3191 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3192 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003193 return FAIL;
3194 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003195 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003196 else
3197 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003198 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003199 return FAIL;
3200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003202 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003204 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003205 return FAIL;
3206
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003207 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003208 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3209 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003211 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003212 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 while (eval_isnamec(*p))
3214 MB_PTR_ADV(p);
3215 if (p == *arg)
3216 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003217 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 return FAIL;
3219 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003220 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 return FAIL;
3222 *arg = p;
3223 }
3224 else
3225 break;
3226 }
3227
3228 // TODO - see handle_subscript():
3229 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3230 // Don't do this when "Func" is already a partial that was bound
3231 // explicitly (pt_auto is FALSE).
3232
3233 return OK;
3234}
3235
3236/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003237 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3238 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003240 * If the value is a constant "ppconst->pp_ret" will be set.
3241 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003242 *
3243 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 */
3245
3246/*
3247 * number number constant
3248 * 0zFFFFFFFF Blob constant
3249 * "string" string constant
3250 * 'string' literal string constant
3251 * &option-name option value
3252 * @r register contents
3253 * identifier variable value
3254 * function() function call
3255 * $VAR environment variable
3256 * (expression) nested expression
3257 * [expr, expr] List
3258 * {key: val, key: val} Dictionary
3259 * #{key: val, key: val} Dictionary with literal keys
3260 *
3261 * Also handle:
3262 * ! in front logical NOT
3263 * - in front unary minus
3264 * + in front unary plus (ignored)
3265 * trailing (arg) funcref/partial call
3266 * trailing [] subscript in String or List
3267 * trailing .name entry in Dictionary
3268 * trailing ->name() method call
3269 */
3270 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003271compile_expr7(
3272 char_u **arg,
3273 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003274 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 char_u *start_leader, *end_leader;
3277 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003278 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003279 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280
3281 /*
3282 * Skip '!', '-' and '+' characters. They are handled later.
3283 */
3284 start_leader = *arg;
3285 while (**arg == '!' || **arg == '-' || **arg == '+')
3286 *arg = skipwhite(*arg + 1);
3287 end_leader = *arg;
3288
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003289 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 switch (**arg)
3291 {
3292 /*
3293 * Number constant.
3294 */
3295 case '0': // also for blob starting with 0z
3296 case '1':
3297 case '2':
3298 case '3':
3299 case '4':
3300 case '5':
3301 case '6':
3302 case '7':
3303 case '8':
3304 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003305 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003307 // Apply "-" and "+" just before the number now, right to
3308 // left. Matters especially when "->" follows. Stops at
3309 // '!'.
3310 if (apply_leader(rettv, TRUE,
3311 start_leader, &end_leader) == FAIL)
3312 {
3313 clear_tv(rettv);
3314 return FAIL;
3315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 break;
3317
3318 /*
3319 * String constant: "string".
3320 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003321 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 return FAIL;
3323 break;
3324
3325 /*
3326 * Literal string constant: 'str''ing'.
3327 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003328 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 return FAIL;
3330 break;
3331
3332 /*
3333 * Constant Vim variable.
3334 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003335 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 ret = NOTDONE;
3337 break;
3338
3339 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003340 * "true" constant
3341 */
3342 case 't': if (STRNCMP(*arg, "true", 4) == 0
3343 && !eval_isnamec((*arg)[4]))
3344 {
3345 *arg += 4;
3346 rettv->v_type = VAR_BOOL;
3347 rettv->vval.v_number = VVAL_TRUE;
3348 }
3349 else
3350 ret = NOTDONE;
3351 break;
3352
3353 /*
3354 * "false" constant
3355 */
3356 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3357 && !eval_isnamec((*arg)[5]))
3358 {
3359 *arg += 5;
3360 rettv->v_type = VAR_BOOL;
3361 rettv->vval.v_number = VVAL_FALSE;
3362 }
3363 else
3364 ret = NOTDONE;
3365 break;
3366
3367 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 * List: [expr, expr]
3369 */
3370 case '[': ret = compile_list(arg, cctx);
3371 break;
3372
3373 /*
3374 * Dictionary: #{key: val, key: val}
3375 */
3376 case '#': if ((*arg)[1] == '{')
3377 {
3378 ++*arg;
3379 ret = compile_dict(arg, cctx, TRUE);
3380 }
3381 else
3382 ret = NOTDONE;
3383 break;
3384
3385 /*
3386 * Lambda: {arg, arg -> expr}
3387 * Dictionary: {'key': val, 'key': val}
3388 */
3389 case '{': {
3390 char_u *start = skipwhite(*arg + 1);
3391
3392 // Find out what comes after the arguments.
3393 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003394 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 if (ret != FAIL && *start == '>')
3396 ret = compile_lambda(arg, cctx);
3397 else
3398 ret = compile_dict(arg, cctx, FALSE);
3399 }
3400 break;
3401
3402 /*
3403 * Option value: &name
3404 */
3405 case '&': ret = compile_get_option(arg, cctx);
3406 break;
3407
3408 /*
3409 * Environment variable: $VAR.
3410 */
3411 case '$': ret = compile_get_env(arg, cctx);
3412 break;
3413
3414 /*
3415 * Register contents: @r.
3416 */
3417 case '@': ret = compile_get_register(arg, cctx);
3418 break;
3419 /*
3420 * nested expression: (expression).
3421 */
3422 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003423
3424 // recursive!
3425 if (ppconst->pp_used <= PPSIZE - 10)
3426 {
3427 ret = compile_expr1(arg, cctx, ppconst);
3428 }
3429 else
3430 {
3431 // Not enough space in ppconst, flush constants.
3432 if (generate_ppconst(cctx, ppconst) == FAIL)
3433 return FAIL;
3434 ret = compile_expr0(arg, cctx);
3435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 *arg = skipwhite(*arg);
3437 if (**arg == ')')
3438 ++*arg;
3439 else if (ret == OK)
3440 {
3441 emsg(_(e_missing_close));
3442 ret = FAIL;
3443 }
3444 break;
3445
3446 default: ret = NOTDONE;
3447 break;
3448 }
3449 if (ret == FAIL)
3450 return FAIL;
3451
Bram Moolenaar1c747212020-05-09 18:28:34 +02003452 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003454 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003455 clear_tv(rettv);
3456 else
3457 // A constant expression can possibly be handled compile time,
3458 // return the value instead of generating code.
3459 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 }
3461 else if (ret == NOTDONE)
3462 {
3463 char_u *p;
3464 int r;
3465
3466 if (!eval_isnamec1(**arg))
3467 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003468 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 return FAIL;
3470 }
3471
3472 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003473 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003475 {
3476 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003479 {
3480 if (generate_ppconst(cctx, ppconst) == FAIL)
3481 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (r == FAIL)
3485 return FAIL;
3486 }
3487
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003488 // Handle following "[]", ".member", etc.
3489 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003490 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003491 ppconst) == FAIL)
3492 return FAIL;
3493 if (ppconst->pp_used > 0)
3494 {
3495 // apply the '!', '-' and '+' before the constant
3496 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003497 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003498 return FAIL;
3499 return OK;
3500 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003501 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003503 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504}
3505
3506/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003507 * Give the "white on both sides" error, taking the operator from "p[len]".
3508 */
3509 void
3510error_white_both(char_u *op, int len)
3511{
3512 char_u buf[10];
3513
3514 vim_strncpy(buf, op, len);
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003515 semsg(_(e_white_space_required_before_and_after), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003516}
3517
3518/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003519 * <type>expr7: runtime type check / conversion
3520 */
3521 static int
3522compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3523{
3524 type_T *want_type = NULL;
3525
3526 // Recognize <type>
3527 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3528 {
3529 int called_emsg_before = called_emsg;
3530
3531 ++*arg;
3532 want_type = parse_type(arg, cctx->ctx_type_list);
3533 if (called_emsg != called_emsg_before)
3534 return FAIL;
3535
3536 if (**arg != '>')
3537 {
3538 if (*skipwhite(*arg) == '>')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003539 semsg(_(e_no_white_space_allowed_before), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003540 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003541 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003542 return FAIL;
3543 }
3544 ++*arg;
3545 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3546 return FAIL;
3547 }
3548
3549 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3550 return FAIL;
3551
3552 if (want_type != NULL)
3553 {
3554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003555 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003556
Bram Moolenaard1103582020-08-14 22:44:25 +02003557 generate_ppconst(cctx, ppconst);
3558 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003559 if (check_type(want_type, actual, FALSE) == FAIL)
3560 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003561 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3562 return FAIL;
3563 }
3564 }
3565
3566 return OK;
3567}
3568
3569/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 * * number multiplication
3571 * / number division
3572 * % number modulo
3573 */
3574 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003575compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576{
3577 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003578 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003579 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003581 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003582 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 return FAIL;
3584
3585 /*
3586 * Repeat computing, until no "*", "/" or "%" is following.
3587 */
3588 for (;;)
3589 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003590 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 if (*op != '*' && *op != '/' && *op != '%')
3592 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003593 if (next != NULL)
3594 {
3595 *arg = next_line_from_context(cctx, TRUE);
3596 op = skipwhite(*arg);
3597 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003598
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003599 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003601 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
3604 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003605 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003606 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003608 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003609 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003611
3612 if (ppconst->pp_used == ppconst_used + 2
3613 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3614 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003615 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003616 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3617 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003618 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003620 // both are numbers: compute the result
3621 switch (*op)
3622 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003623 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003624 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003625 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003627 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003628 break;
3629 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003630 tv1->vval.v_number = res;
3631 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003632 }
3633 else
3634 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003635 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003636 generate_two_op(cctx, op);
3637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 }
3639
3640 return OK;
3641}
3642
3643/*
3644 * + number addition
3645 * - number subtraction
3646 * .. string concatenation
3647 */
3648 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003649compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650{
3651 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003652 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003654 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
3656 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003657 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 return FAIL;
3659
3660 /*
3661 * Repeat computing, until no "+", "-" or ".." is following.
3662 */
3663 for (;;)
3664 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003665 op = may_peek_next_line(cctx, *arg, &next);
3666 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 break;
3668 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003669 if (next != NULL)
3670 {
3671 *arg = next_line_from_context(cctx, TRUE);
3672 op = skipwhite(*arg);
3673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003675 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003677 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003678 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 }
3680
3681 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003682 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003683 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003685 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003686 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 return FAIL;
3688
Bram Moolenaara5565e42020-05-09 15:44:01 +02003689 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003690 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003691 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3692 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3693 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3694 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003696 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3697 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003698
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 // concat/subtract/add constant numbers
3700 if (*op == '+')
3701 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3702 else if (*op == '-')
3703 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3704 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003705 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003706 // concatenate constant strings
3707 char_u *s1 = tv1->vval.v_string;
3708 char_u *s2 = tv2->vval.v_string;
3709 size_t len1 = STRLEN(s1);
3710
3711 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3712 if (tv1->vval.v_string == NULL)
3713 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003714 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003715 return FAIL;
3716 }
3717 mch_memmove(tv1->vval.v_string, s1, len1);
3718 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003719 vim_free(s1);
3720 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003721 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003722 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 }
3724 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003725 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003726 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003727 if (*op == '.')
3728 {
3729 if (may_generate_2STRING(-2, cctx) == FAIL
3730 || may_generate_2STRING(-1, cctx) == FAIL)
3731 return FAIL;
3732 generate_instr_drop(cctx, ISN_CONCAT, 1);
3733 }
3734 else
3735 generate_two_op(cctx, op);
3736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 }
3738
3739 return OK;
3740}
3741
3742/*
3743 * expr5a == expr5b
3744 * expr5a =~ expr5b
3745 * expr5a != expr5b
3746 * expr5a !~ expr5b
3747 * expr5a > expr5b
3748 * expr5a >= expr5b
3749 * expr5a < expr5b
3750 * expr5a <= expr5b
3751 * expr5a is expr5b
3752 * expr5a isnot expr5b
3753 *
3754 * Produces instructions:
3755 * EVAL expr5a Push result of "expr5a"
3756 * EVAL expr5b Push result of "expr5b"
3757 * COMPARE one of the compare instructions
3758 */
3759 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003760compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761{
3762 exptype_T type = EXPR_UNKNOWN;
3763 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003764 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003767 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768
3769 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003770 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 return FAIL;
3772
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003773 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003774 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
3776 /*
3777 * If there is a comparative operator, use it.
3778 */
3779 if (type != EXPR_UNKNOWN)
3780 {
3781 int ic = FALSE; // Default: do not ignore case
3782
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003783 if (next != NULL)
3784 {
3785 *arg = next_line_from_context(cctx, TRUE);
3786 p = skipwhite(*arg);
3787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 if (type_is && (p[len] == '?' || p[len] == '#'))
3789 {
3790 semsg(_(e_invexpr2), *arg);
3791 return FAIL;
3792 }
3793 // extra question mark appended: ignore case
3794 if (p[len] == '?')
3795 {
3796 ic = TRUE;
3797 ++len;
3798 }
3799 // extra '#' appended: match case (ignored)
3800 else if (p[len] == '#')
3801 ++len;
3802 // nothing appended: match case
3803
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003804 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003806 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003807 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 }
3809
3810 // get the second variable
3811 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003812 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003813 return FAIL;
3814
Bram Moolenaara5565e42020-05-09 15:44:01 +02003815 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 return FAIL;
3817
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 if (ppconst->pp_used == ppconst_used + 2)
3819 {
3820 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3821 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3822 int ret;
3823
3824 // Both sides are a constant, compute the result now.
3825 // First check for a valid combination of types, this is more
3826 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003827 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003828 ret = FAIL;
3829 else
3830 {
3831 ret = typval_compare(tv1, tv2, type, ic);
3832 tv1->v_type = VAR_BOOL;
3833 tv1->vval.v_number = tv1->vval.v_number
3834 ? VVAL_TRUE : VVAL_FALSE;
3835 clear_tv(tv2);
3836 --ppconst->pp_used;
3837 }
3838 return ret;
3839 }
3840
3841 generate_ppconst(cctx, ppconst);
3842 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 }
3844
3845 return OK;
3846}
3847
Bram Moolenaar7f141552020-05-09 17:35:53 +02003848static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850/*
3851 * Compile || or &&.
3852 */
3853 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854compile_and_or(
3855 char_u **arg,
3856 cctx_T *cctx,
3857 char *op,
3858 ppconst_T *ppconst,
3859 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003861 char_u *next;
3862 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 int opchar = *op;
3864
3865 if (p[0] == opchar && p[1] == opchar)
3866 {
3867 garray_T *instr = &cctx->ctx_instr;
3868 garray_T end_ga;
3869
3870 /*
3871 * Repeat until there is no following "||" or "&&"
3872 */
3873 ga_init2(&end_ga, sizeof(int), 10);
3874 while (p[0] == opchar && p[1] == opchar)
3875 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003876 if (next != NULL)
3877 {
3878 *arg = next_line_from_context(cctx, TRUE);
3879 p = skipwhite(*arg);
3880 }
3881
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003882 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3883 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02003884 semsg(_(e_white_space_required_before_and_after), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003885 return FAIL;
3886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887
Bram Moolenaara5565e42020-05-09 15:44:01 +02003888 // TODO: use ppconst if the value is a constant
3889 generate_ppconst(cctx, ppconst);
3890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 if (ga_grow(&end_ga, 1) == FAIL)
3892 {
3893 ga_clear(&end_ga);
3894 return FAIL;
3895 }
3896 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3897 ++end_ga.ga_len;
3898 generate_JUMP(cctx, opchar == '|'
3899 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3900
3901 // eval the next expression
3902 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003903 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003904 return FAIL;
3905
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3907 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 {
3909 ga_clear(&end_ga);
3910 return FAIL;
3911 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003912
3913 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003915 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916
3917 // Fill in the end label in all jumps.
3918 while (end_ga.ga_len > 0)
3919 {
3920 isn_T *isn;
3921
3922 --end_ga.ga_len;
3923 isn = ((isn_T *)instr->ga_data)
3924 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3925 isn->isn_arg.jump.jump_where = instr->ga_len;
3926 }
3927 ga_clear(&end_ga);
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * expr4a && expr4a && expr4a logical AND
3935 *
3936 * Produces instructions:
3937 * EVAL expr4a Push result of "expr4a"
3938 * JUMP_AND_KEEP_IF_FALSE end
3939 * EVAL expr4b Push result of "expr4b"
3940 * JUMP_AND_KEEP_IF_FALSE end
3941 * EVAL expr4c Push result of "expr4c"
3942 * end:
3943 */
3944 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003947 int ppconst_used = ppconst->pp_used;
3948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 return FAIL;
3952
3953 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003954 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955}
3956
3957/*
3958 * expr3a || expr3b || expr3c logical OR
3959 *
3960 * Produces instructions:
3961 * EVAL expr3a Push result of "expr3a"
3962 * JUMP_AND_KEEP_IF_TRUE end
3963 * EVAL expr3b Push result of "expr3b"
3964 * JUMP_AND_KEEP_IF_TRUE end
3965 * EVAL expr3c Push result of "expr3c"
3966 * end:
3967 */
3968 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003969compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971 int ppconst_used = ppconst->pp_used;
3972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003974 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 return FAIL;
3976
3977 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979}
3980
3981/*
3982 * Toplevel expression: expr2 ? expr1a : expr1b
3983 *
3984 * Produces instructions:
3985 * EVAL expr2 Push result of "expr"
3986 * JUMP_IF_FALSE alt jump if false
3987 * EVAL expr1a
3988 * JUMP_ALWAYS end
3989 * alt: EVAL expr1b
3990 * end:
3991 */
3992 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003993compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994{
3995 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003996 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998
Bram Moolenaar61a89812020-05-07 16:58:17 +02003999 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004000 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 return FAIL;
4002
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004003 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 if (*p == '?')
4005 {
4006 garray_T *instr = &cctx->ctx_instr;
4007 garray_T *stack = &cctx->ctx_type_stack;
4008 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004009 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004011 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 int has_const_expr = FALSE;
4014 int const_value = FALSE;
4015 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004017 if (next != NULL)
4018 {
4019 *arg = next_line_from_context(cctx, TRUE);
4020 p = skipwhite(*arg);
4021 }
4022
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004023 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4024 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004025 semsg(_(e_white_space_required_before_and_after), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004026 return FAIL;
4027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028
Bram Moolenaara5565e42020-05-09 15:44:01 +02004029 if (ppconst->pp_used == ppconst_used + 1)
4030 {
4031 // the condition is a constant, we know whether the ? or the :
4032 // expression is to be evaluated.
4033 has_const_expr = TRUE;
4034 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4035 clear_tv(&ppconst->pp_tv[ppconst_used]);
4036 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004037 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4038 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 }
4040 else
4041 {
4042 generate_ppconst(cctx, ppconst);
4043 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045
4046 // evaluate the second expression; any type is accepted
4047 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004048 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004049 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004051 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053 if (!has_const_expr)
4054 {
4055 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 // remember the type and drop it
4058 --stack->ga_len;
4059 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 end_idx = instr->ga_len;
4062 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4063
4064 // jump here from JUMP_IF_FALSE
4065 isn = ((isn_T *)instr->ga_data) + alt_idx;
4066 isn->isn_arg.jump.jump_where = instr->ga_len;
4067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068
4069 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004070 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 if (*p != ':')
4072 {
4073 emsg(_(e_missing_colon));
4074 return FAIL;
4075 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004076 if (next != NULL)
4077 {
4078 *arg = next_line_from_context(cctx, TRUE);
4079 p = skipwhite(*arg);
4080 }
4081
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004082 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4083 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004084 semsg(_(e_white_space_required_before_and_after), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004085 return FAIL;
4086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
4088 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004090 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4091 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004093 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004094 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
Bram Moolenaara5565e42020-05-09 15:44:01 +02004098 if (!has_const_expr)
4099 {
4100 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 // If the types differ, the result has a more generic type.
4103 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4104 common_type(type1, type2, &type2, cctx->ctx_type_list);
4105
4106 // jump here from JUMP_ALWAYS
4107 isn = ((isn_T *)instr->ga_data) + end_idx;
4108 isn->isn_arg.jump.jump_where = instr->ga_len;
4109 }
4110
4111 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 }
4113 return OK;
4114}
4115
4116/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 * Toplevel expression.
4118 */
4119 static int
4120compile_expr0(char_u **arg, cctx_T *cctx)
4121{
4122 ppconst_T ppconst;
4123
4124 CLEAR_FIELD(ppconst);
4125 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4126 {
4127 clear_ppconst(&ppconst);
4128 return FAIL;
4129 }
4130 if (generate_ppconst(cctx, &ppconst) == FAIL)
4131 return FAIL;
4132 return OK;
4133}
4134
4135/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 * compile "return [expr]"
4137 */
4138 static char_u *
4139compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4140{
4141 char_u *p = arg;
4142 garray_T *stack = &cctx->ctx_type_stack;
4143 type_T *stack_type;
4144
4145 if (*p != NUL && *p != '|' && *p != '\n')
4146 {
4147 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 return NULL;
4150
4151 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4152 if (set_return_type)
4153 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004154 else
4155 {
4156 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4157 && stack_type->tt_type != VAR_VOID
4158 && stack_type->tt_type != VAR_UNKNOWN)
4159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004160 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004161 return NULL;
4162 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004163 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4164 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 }
4168 else
4169 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004170 // "set_return_type" cannot be TRUE, only used for a lambda which
4171 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004172 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4173 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004175 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 return NULL;
4177 }
4178
4179 // No argument, return zero.
4180 generate_PUSHNR(cctx, 0);
4181 }
4182
4183 if (generate_instr(cctx, ISN_RETURN) == NULL)
4184 return NULL;
4185
4186 // "return val | endif" is possible
4187 return skipwhite(p);
4188}
4189
4190/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004191 * Get a line from the compilation context, compatible with exarg_T getline().
4192 * Return a pointer to the line in allocated memory.
4193 * Return NULL for end-of-file or some error.
4194 */
4195 static char_u *
4196exarg_getline(
4197 int c UNUSED,
4198 void *cookie,
4199 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004200 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004201{
4202 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004203 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004204
Bram Moolenaar66250c92020-08-20 15:02:42 +02004205 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004206 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004207 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4208 return NULL;
4209 ++cctx->ctx_lnum;
4210 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4211 // Comment lines result in NULL pointers, skip them.
4212 if (p != NULL)
4213 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004214 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004215}
4216
4217/*
4218 * Compile a nested :def command.
4219 */
4220 static char_u *
4221compile_nested_function(exarg_T *eap, cctx_T *cctx)
4222{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004223 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004224 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004225 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004226 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004227 lvar_T *lvar;
4228 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004229 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004230
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004231 // Only g:Func() can use a namespace.
4232 if (name_start[1] == ':' && !is_global)
4233 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004234 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004235 return NULL;
4236 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004237 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4238 return NULL;
4239
Bram Moolenaar04b12692020-05-04 23:24:44 +02004240 eap->arg = name_end;
4241 eap->getline = exarg_getline;
4242 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004243 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004244 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004245 lambda_name = get_lambda_name();
4246 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004247
Bram Moolenaar822ba242020-05-24 23:00:18 +02004248 if (ufunc == NULL)
4249 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004250 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004251 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004252 return NULL;
4253
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004254 if (is_global)
4255 {
4256 char_u *func_name = vim_strnsave(name_start + 2,
4257 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004258
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004259 if (func_name == NULL)
4260 r = FAIL;
4261 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004262 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004263 }
4264 else
4265 {
4266 // Define a local variable for the function reference.
4267 lvar = reserve_local(cctx, name_start, name_end - name_start,
4268 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004269 if (lvar == NULL)
4270 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004271 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004272 return NULL;
4273 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4274 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004275
Bram Moolenaar61a89812020-05-07 16:58:17 +02004276 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004277 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004278}
4279
4280/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 * Return the length of an assignment operator, or zero if there isn't one.
4282 */
4283 int
4284assignment_len(char_u *p, int *heredoc)
4285{
4286 if (*p == '=')
4287 {
4288 if (p[1] == '<' && p[2] == '<')
4289 {
4290 *heredoc = TRUE;
4291 return 3;
4292 }
4293 return 1;
4294 }
4295 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4296 return 2;
4297 if (STRNCMP(p, "..=", 3) == 0)
4298 return 3;
4299 return 0;
4300}
4301
4302// words that cannot be used as a variable
4303static char *reserved[] = {
4304 "true",
4305 "false",
4306 NULL
4307};
4308
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004309typedef enum {
4310 dest_local,
4311 dest_option,
4312 dest_env,
4313 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004314 dest_buffer,
4315 dest_window,
4316 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004317 dest_vimvar,
4318 dest_script,
4319 dest_reg,
4320} assign_dest_T;
4321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004323 * Generate the load instruction for "name".
4324 */
4325 static void
4326generate_loadvar(
4327 cctx_T *cctx,
4328 assign_dest_T dest,
4329 char_u *name,
4330 lvar_T *lvar,
4331 type_T *type)
4332{
4333 switch (dest)
4334 {
4335 case dest_option:
4336 // TODO: check the option exists
4337 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4338 break;
4339 case dest_global:
4340 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4341 break;
4342 case dest_buffer:
4343 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4344 break;
4345 case dest_window:
4346 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4347 break;
4348 case dest_tab:
4349 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4350 break;
4351 case dest_script:
4352 compile_load_scriptvar(cctx,
4353 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4354 break;
4355 case dest_env:
4356 // Include $ in the name here
4357 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4358 break;
4359 case dest_reg:
4360 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4361 break;
4362 case dest_vimvar:
4363 generate_LOADV(cctx, name + 2, TRUE);
4364 break;
4365 case dest_local:
4366 if (lvar->lv_from_outer)
4367 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4368 NULL, type);
4369 else
4370 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4371 break;
4372 }
4373}
4374
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004375 void
4376vim9_declare_error(char_u *name)
4377{
4378 char *scope = "";
4379
4380 switch (*name)
4381 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004382 case 'g': scope = _("global"); break;
4383 case 'b': scope = _("buffer"); break;
4384 case 'w': scope = _("window"); break;
4385 case 't': scope = _("tab"); break;
4386 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004387 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004388 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004389 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004390 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004391 case '@': semsg(_(e_cannot_declare_a_register), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004392 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004393 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004394 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004395 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004396}
4397
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004398/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004399 * Compile declaration and assignment:
4400 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004402 * Return NULL for an error.
4403 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 */
4405 static char_u *
4406compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4407{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004408 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004410 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 char_u *ret = NULL;
4412 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004413 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004414 int scriptvar_sid = 0;
4415 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004418 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 int oplen = 0;
4421 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004422 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004423 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004424 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004428 // Skip over the "var" or "[var, var]" to get to any "=".
4429 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4430 if (p == NULL)
4431 return *arg == '[' ? arg : NULL;
4432
4433 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004435 // TODO: should we allow this, and figure out type inference from list
4436 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004437 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 return NULL;
4439 }
4440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 sp = p;
4442 p = skipwhite(p);
4443 op = p;
4444 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004445
4446 if (var_count > 0 && oplen == 0)
4447 // can be something like "[1, 2]->func()"
4448 return arg;
4449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4451 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004452 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004453 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 if (heredoc)
4457 {
4458 list_T *l;
4459 listitem_T *li;
4460
4461 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004462 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004464 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
4466 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004467 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 {
4469 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4470 li->li_tv.vval.v_string = NULL;
4471 }
4472 generate_NEWLIST(cctx, l->lv_len);
4473 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004474 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 list_free(l);
4476 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004477 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004479 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004481 // for "[var, var] = expr" evaluate the expression here, loop over the
4482 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004483
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004484 p = skipwhite(op + oplen);
4485 if (compile_expr0(&p, cctx) == FAIL)
4486 return NULL;
4487 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004489 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004491 type_T *stacktype;
4492
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004493 stacktype = stack->ga_len == 0 ? &t_void
4494 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004495 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004497 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004498 goto theend;
4499 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004500 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004501 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004502 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004503 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4504 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004505 }
4506 }
4507
4508 /*
4509 * Loop over variables in "[var, var] = expr".
4510 * For "var = expr" and "let var: type" this is done only once.
4511 */
4512 if (var_count > 0)
4513 var_start = skipwhite(arg + 1); // skip over the "["
4514 else
4515 var_start = arg;
4516 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4517 {
4518 char_u *var_end = skip_var_one(var_start, FALSE);
4519 size_t varlen;
4520 int new_local = FALSE;
4521 int opt_type;
4522 int opt_flags = 0;
4523 assign_dest_T dest = dest_local;
4524 int vimvaridx = -1;
4525 lvar_T *lvar = NULL;
4526 lvar_T arg_lvar;
4527 int has_type = FALSE;
4528 int has_index = FALSE;
4529 int instr_count = -1;
4530
Bram Moolenaar65821722020-08-02 18:58:54 +02004531 if (*var_start == '@')
4532 p = var_start + 2;
4533 else
4534 {
4535 p = (*var_start == '&' || *var_start == '$')
4536 ? var_start + 1 : var_start;
4537 p = to_name_end(p, TRUE);
4538 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539
4540 // "a: type" is declaring variable "a" with a type, not "a:".
4541 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4542 --var_end;
4543 if (is_decl && p == var_start + 2 && p[-1] == ':')
4544 --p;
4545
4546 varlen = p - var_start;
4547 vim_free(name);
4548 name = vim_strnsave(var_start, varlen);
4549 if (name == NULL)
4550 return NULL;
4551 if (!heredoc)
4552 type = &t_any;
4553
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004554 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004555 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004556 int declare_error = FALSE;
4557
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 if (*var_start == '&')
4559 {
4560 int cc;
4561 long numval;
4562
4563 dest = dest_option;
4564 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004566 emsg(_(e_const_option));
4567 goto theend;
4568 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004569 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004570 p = var_start;
4571 p = find_option_end(&p, &opt_flags);
4572 if (p == NULL)
4573 {
4574 // cannot happen?
4575 emsg(_(e_letunexp));
4576 goto theend;
4577 }
4578 cc = *p;
4579 *p = NUL;
4580 opt_type = get_option_value(var_start + 1, &numval,
4581 NULL, opt_flags);
4582 *p = cc;
4583 if (opt_type == -3)
4584 {
4585 semsg(_(e_unknown_option), var_start);
4586 goto theend;
4587 }
4588 if (opt_type == -2 || opt_type == 0)
4589 type = &t_string;
4590 else
4591 type = &t_number; // both number and boolean option
4592 }
4593 else if (*var_start == '$')
4594 {
4595 dest = dest_env;
4596 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004597 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598 }
4599 else if (*var_start == '@')
4600 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004601 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004602 {
4603 emsg_invreg(var_start[1]);
4604 goto theend;
4605 }
4606 dest = dest_reg;
4607 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004608 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004609 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004610 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004611 {
4612 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004613 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004614 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004615 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004616 {
4617 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004618 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004619 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004620 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 {
4622 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004623 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004624 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004625 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004626 {
4627 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004628 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004630 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004631 {
4632 typval_T *vtv;
4633 int di_flags;
4634
4635 vimvaridx = find_vim_var(name + 2, &di_flags);
4636 if (vimvaridx < 0)
4637 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004638 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 goto theend;
4640 }
4641 // We use the current value of "sandbox" here, is that OK?
4642 if (var_check_ro(di_flags, name, FALSE))
4643 goto theend;
4644 dest = dest_vimvar;
4645 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004646 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004647 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004648 }
4649 else
4650 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004651 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004652
4653 for (idx = 0; reserved[idx] != NULL; ++idx)
4654 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004655 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004656 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004657 goto theend;
4658 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659
4660 lvar = lookup_local(var_start, varlen, cctx);
4661 if (lvar == NULL)
4662 {
4663 CLEAR_FIELD(arg_lvar);
4664 if (lookup_arg(var_start, varlen,
4665 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4666 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004667 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004668 if (is_decl)
4669 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004670 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004671 goto theend;
4672 }
4673 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004674 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004675 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 if (lvar != NULL)
4677 {
4678 if (is_decl)
4679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004680 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004681 goto theend;
4682 }
4683 else if (lvar->lv_const)
4684 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004685 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 goto theend;
4687 }
4688 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004689 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004690 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004691 int script_namespace = varlen > 1
4692 && STRNCMP(var_start, "s:", 2) == 0;
4693 int script_var = (script_namespace
4694 ? lookup_script(var_start + 2, varlen - 2)
4695 : lookup_script(var_start, varlen)) == OK;
4696 imported_T *import =
4697 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004698
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004699 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004701 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4702
4703 if (is_decl)
4704 {
4705 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004706 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004707 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004708 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004709 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004710 name);
4711 goto theend;
4712 }
4713 else if (cctx->ctx_ufunc->uf_script_ctx_version
4714 == SCRIPT_VERSION_VIM9
4715 && script_namespace
4716 && !script_var && import == NULL)
4717 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004718 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004719 goto theend;
4720 }
4721
4722 dest = dest_script;
4723
4724 // existing script-local variables should have a type
4725 scriptvar_sid = current_sctx.sc_sid;
4726 if (import != NULL)
4727 scriptvar_sid = import->imp_sid;
4728 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4729 rawname, TRUE);
4730 if (scriptvar_idx >= 0)
4731 {
4732 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4733 svar_T *sv =
4734 ((svar_T *)si->sn_var_vals.ga_data)
4735 + scriptvar_idx;
4736 type = sv->sv_type;
4737 }
4738 }
4739 else if (name[1] == ':' && name[2] != NUL)
4740 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004741 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004742 goto theend;
4743 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004744 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004745 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004746 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004747 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004748 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004749 else if (check_defined(var_start, varlen, cctx) == FAIL)
4750 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004751 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004752 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004753
4754 if (declare_error)
4755 {
4756 vim9_declare_error(name);
4757 goto theend;
4758 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759 }
4760
4761 // handle "a:name" as a name, not index "name" on "a"
4762 if (varlen > 1 || var_start[varlen] != ':')
4763 p = var_end;
4764
4765 if (dest != dest_option)
4766 {
4767 if (is_decl && *p == ':')
4768 {
4769 // parse optional type: "let var: type = expr"
4770 if (!VIM_ISWHITE(p[1]))
4771 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004772 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773 goto theend;
4774 }
4775 p = skipwhite(p + 1);
4776 type = parse_type(&p, cctx->ctx_type_list);
4777 has_type = TRUE;
4778 }
4779 else if (lvar != NULL)
4780 type = lvar->lv_type;
4781 }
4782
4783 if (oplen == 3 && !heredoc && dest != dest_global
4784 && type->tt_type != VAR_STRING
4785 && type->tt_type != VAR_ANY)
4786 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004787 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 goto theend;
4789 }
4790
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004791 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 {
4793 if (oplen > 1 && !heredoc)
4794 {
4795 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004796 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004797 goto theend;
4798 }
4799
4800 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004801 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4802 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004803 goto theend;
4804 lvar = reserve_local(cctx, var_start, varlen,
4805 cmdidx == CMD_const, type);
4806 if (lvar == NULL)
4807 goto theend;
4808 new_local = TRUE;
4809 }
4810
4811 member_type = type;
4812 if (var_end > var_start + varlen)
4813 {
4814 // Something follows after the variable: "var[idx]".
4815 if (is_decl)
4816 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004817 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004818 goto theend;
4819 }
4820
4821 if (var_start[varlen] == '[')
4822 {
4823 has_index = TRUE;
4824 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004825 member_type = &t_any;
4826 else
4827 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 }
4829 else
4830 {
4831 semsg("Not supported yet: %s", var_start);
4832 goto theend;
4833 }
4834 }
4835 else if (lvar == &arg_lvar)
4836 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004837 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 goto theend;
4839 }
4840
4841 if (!heredoc)
4842 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004843 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004845 if (oplen > 0 && var_count == 0)
4846 {
4847 // skip over the "=" and the expression
4848 p = skipwhite(op + oplen);
4849 compile_expr0(&p, cctx);
4850 }
4851 }
4852 else if (oplen > 0)
4853 {
4854 type_T *stacktype;
4855
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 // For "var = expr" evaluate the expression.
4857 if (var_count == 0)
4858 {
4859 int r;
4860
4861 // for "+=", "*=", "..=" etc. first load the current value
4862 if (*op != '=')
4863 {
4864 generate_loadvar(cctx, dest, name, lvar, type);
4865
4866 if (has_index)
4867 {
4868 // TODO: get member from list or dict
4869 emsg("Index with operation not supported yet");
4870 goto theend;
4871 }
4872 }
4873
4874 // Compile the expression. Temporarily hide the new local
4875 // variable here, it is not available to this expression.
4876 if (new_local)
4877 --cctx->ctx_locals.ga_len;
4878 instr_count = instr->ga_len;
4879 p = skipwhite(op + oplen);
4880 r = compile_expr0(&p, cctx);
4881 if (new_local)
4882 ++cctx->ctx_locals.ga_len;
4883 if (r == FAIL)
4884 goto theend;
4885 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004886 else if (semicolon && var_idx == var_count - 1)
4887 {
4888 // For "[var; var] = expr" get the rest of the list
4889 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4890 goto theend;
4891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004892 else
4893 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004894 // For "[var, var] = expr" get the "var_idx" item from the
4895 // list.
4896 if (generate_GETITEM(cctx, var_idx) == FAIL)
4897 return FAIL;
4898 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004899
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004900 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004901 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004902 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004903 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004904 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004905 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004906 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004908 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004909 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004910 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004911 else if ((stacktype->tt_type == VAR_FUNC
4912 || stacktype->tt_type == VAR_PARTIAL)
4913 && var_wrong_func_name(name, TRUE))
4914 {
4915 goto theend;
4916 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004917 else
4918 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004919 // An empty list or dict has a &t_void member,
4920 // for a variable that implies &t_any.
4921 if (stacktype == &t_list_empty)
4922 lvar->lv_type = &t_list_any;
4923 else if (stacktype == &t_dict_empty)
4924 lvar->lv_type = &t_dict_any;
4925 else
4926 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004928 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004929 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004930 {
4931 type_T *use_type = lvar->lv_type;
4932
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004933 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004934 if (has_index)
4935 {
4936 use_type = use_type->tt_member;
4937 if (use_type == NULL)
4938 use_type = &t_void;
4939 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004940 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004941 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004942 goto theend;
4943 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004944 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004945 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004946 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004947 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004951 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 goto theend;
4953 }
4954 else if (!has_type || dest == dest_option)
4955 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004956 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 goto theend;
4958 }
4959 else
4960 {
4961 // variables are always initialized
4962 if (ga_grow(instr, 1) == FAIL)
4963 goto theend;
4964 switch (member_type->tt_type)
4965 {
4966 case VAR_BOOL:
4967 generate_PUSHBOOL(cctx, VVAL_FALSE);
4968 break;
4969 case VAR_FLOAT:
4970#ifdef FEAT_FLOAT
4971 generate_PUSHF(cctx, 0.0);
4972#endif
4973 break;
4974 case VAR_STRING:
4975 generate_PUSHS(cctx, NULL);
4976 break;
4977 case VAR_BLOB:
4978 generate_PUSHBLOB(cctx, NULL);
4979 break;
4980 case VAR_FUNC:
4981 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4982 break;
4983 case VAR_LIST:
4984 generate_NEWLIST(cctx, 0);
4985 break;
4986 case VAR_DICT:
4987 generate_NEWDICT(cctx, 0);
4988 break;
4989 case VAR_JOB:
4990 generate_PUSHJOB(cctx, NULL);
4991 break;
4992 case VAR_CHANNEL:
4993 generate_PUSHCHANNEL(cctx, NULL);
4994 break;
4995 case VAR_NUMBER:
4996 case VAR_UNKNOWN:
4997 case VAR_ANY:
4998 case VAR_PARTIAL:
4999 case VAR_VOID:
5000 case VAR_SPECIAL: // cannot happen
5001 generate_PUSHNR(cctx, 0);
5002 break;
5003 }
5004 }
5005 if (var_count == 0)
5006 end = p;
5007 }
5008
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005009 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005010 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005011 break;
5012
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 if (oplen > 0 && *op != '=')
5014 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005015 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 type_T *stacktype;
5017
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 if (*op == '.')
5019 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005020 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005021 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005023 if (
5024#ifdef FEAT_FLOAT
5025 // If variable is float operation with number is OK.
5026 !(expected == &t_float && stacktype == &t_number) &&
5027#endif
5028 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005029 goto theend;
5030
5031 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005032 {
5033 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5034 goto theend;
5035 }
5036 else if (*op == '+')
5037 {
5038 if (generate_add_instr(cctx,
5039 operator_type(member_type, stacktype),
5040 member_type, stacktype) == FAIL)
5041 goto theend;
5042 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005043 else if (generate_two_op(cctx, op) == FAIL)
5044 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005048 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 int r;
5050
5051 // Compile the "idx" in "var[idx]".
5052 if (new_local)
5053 --cctx->ctx_locals.ga_len;
5054 p = skipwhite(var_start + varlen + 1);
5055 r = compile_expr0(&p, cctx);
5056 if (new_local)
5057 ++cctx->ctx_locals.ga_len;
5058 if (r == FAIL)
5059 goto theend;
5060 if (*skipwhite(p) != ']')
5061 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005062 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 emsg(_(e_missbrac));
5064 goto theend;
5065 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005066 if (type == &t_any)
5067 {
5068 type_T *idx_type = ((type_T **)stack->ga_data)[
5069 stack->ga_len - 1];
5070 // Index on variable of unknown type: guess the type from the
5071 // index type: number is dict, otherwise dict.
5072 // TODO: should do the assignment at runtime
5073 if (idx_type->tt_type == VAR_NUMBER)
5074 type = &t_list_any;
5075 else
5076 type = &t_dict_any;
5077 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 if (type->tt_type == VAR_DICT
5079 && may_generate_2STRING(-1, cctx) == FAIL)
5080 goto theend;
5081 if (type->tt_type == VAR_LIST
5082 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005083 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 {
5085 emsg(_(e_number_exp));
5086 goto theend;
5087 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005088
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 // Load the dict or list. On the stack we then have:
5090 // - value
5091 // - index
5092 // - variable
5093 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005094
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005095 if (type->tt_type == VAR_LIST)
5096 {
5097 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5098 return FAIL;
5099 }
5100 else if (type->tt_type == VAR_DICT)
5101 {
5102 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5103 return FAIL;
5104 }
5105 else
5106 {
5107 emsg(_(e_listreq));
5108 goto theend;
5109 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005110 }
5111 else
5112 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 switch (dest)
5114 {
5115 case dest_option:
5116 generate_STOREOPT(cctx, name + 1, opt_flags);
5117 break;
5118 case dest_global:
5119 // include g: with the name, easier to execute that way
5120 generate_STORE(cctx, ISN_STOREG, 0, name);
5121 break;
5122 case dest_buffer:
5123 // include b: with the name, easier to execute that way
5124 generate_STORE(cctx, ISN_STOREB, 0, name);
5125 break;
5126 case dest_window:
5127 // include w: with the name, easier to execute that way
5128 generate_STORE(cctx, ISN_STOREW, 0, name);
5129 break;
5130 case dest_tab:
5131 // include t: with the name, easier to execute that way
5132 generate_STORE(cctx, ISN_STORET, 0, name);
5133 break;
5134 case dest_env:
5135 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5136 break;
5137 case dest_reg:
5138 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5139 break;
5140 case dest_vimvar:
5141 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5142 break;
5143 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005144 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005145 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 {
5147 char_u *name_s = name;
5148
5149 // Include s: in the name for store_var()
5150 if (name[1] != ':')
5151 {
5152 int len = (int)STRLEN(name) + 3;
5153
5154 name_s = alloc(len);
5155 if (name_s == NULL)
5156 name_s = name;
5157 else
5158 vim_snprintf((char *)name_s, len,
5159 "s:%s", name);
5160 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005161 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5162 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005163 if (name_s != name)
5164 vim_free(name_s);
5165 }
5166 else
5167 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005168 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005169 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 break;
5171 case dest_local:
5172 if (lvar != NULL)
5173 {
5174 isn_T *isn = ((isn_T *)instr->ga_data)
5175 + instr->ga_len - 1;
5176
5177 // optimization: turn "var = 123" from ISN_PUSHNR +
5178 // ISN_STORE into ISN_STORENR
5179 if (!lvar->lv_from_outer
5180 && instr->ga_len == instr_count + 1
5181 && isn->isn_type == ISN_PUSHNR)
5182 {
5183 varnumber_T val = isn->isn_arg.number;
5184
5185 isn->isn_type = ISN_STORENR;
5186 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5187 isn->isn_arg.storenr.stnr_val = val;
5188 if (stack->ga_len > 0)
5189 --stack->ga_len;
5190 }
5191 else if (lvar->lv_from_outer)
5192 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005193 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 else
5195 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5196 }
5197 break;
5198 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005199 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200
5201 if (var_idx + 1 < var_count)
5202 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204
5205 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005206 if (var_count > 0 && !semicolon)
5207 {
5208 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5209 goto theend;
5210 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211
Bram Moolenaarb2097502020-07-19 17:17:02 +02005212 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213
5214theend:
5215 vim_free(name);
5216 return ret;
5217}
5218
5219/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005220 * Check if "name" can be "unlet".
5221 */
5222 int
5223check_vim9_unlet(char_u *name)
5224{
5225 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5226 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005227 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005228 return FAIL;
5229 }
5230 return OK;
5231}
5232
5233/*
5234 * Callback passed to ex_unletlock().
5235 */
5236 static int
5237compile_unlet(
5238 lval_T *lvp,
5239 char_u *name_end,
5240 exarg_T *eap,
5241 int deep UNUSED,
5242 void *coookie)
5243{
5244 cctx_T *cctx = coookie;
5245
5246 if (lvp->ll_tv == NULL)
5247 {
5248 char_u *p = lvp->ll_name;
5249 int cc = *name_end;
5250 int ret = OK;
5251
5252 // Normal name. Only supports g:, w:, t: and b: namespaces.
5253 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005254 if (*p == '$')
5255 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5256 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005257 ret = FAIL;
5258 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005259 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005260
5261 *name_end = cc;
5262 return ret;
5263 }
5264
5265 // TODO: unlet {list}[idx]
5266 // TODO: unlet {dict}[key]
5267 emsg("Sorry, :unlet not fully implemented yet");
5268 return FAIL;
5269}
5270
5271/*
5272 * compile "unlet var", "lock var" and "unlock var"
5273 * "arg" points to "var".
5274 */
5275 static char_u *
5276compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5277{
5278 char_u *p = arg;
5279
5280 if (eap->cmdidx != CMD_unlet)
5281 {
5282 emsg("Sorry, :lock and unlock not implemented yet");
5283 return NULL;
5284 }
5285
5286 if (*p == '!')
5287 {
5288 p = skipwhite(p + 1);
5289 eap->forceit = TRUE;
5290 }
5291
5292 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5293 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5294}
5295
5296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 * Compile an :import command.
5298 */
5299 static char_u *
5300compile_import(char_u *arg, cctx_T *cctx)
5301{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005302 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303}
5304
5305/*
5306 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5307 */
5308 static int
5309compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5310{
5311 garray_T *instr = &cctx->ctx_instr;
5312 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5313
5314 if (endlabel == NULL)
5315 return FAIL;
5316 endlabel->el_next = *el;
5317 *el = endlabel;
5318 endlabel->el_end_label = instr->ga_len;
5319
5320 generate_JUMP(cctx, when, 0);
5321 return OK;
5322}
5323
5324 static void
5325compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5326{
5327 garray_T *instr = &cctx->ctx_instr;
5328
5329 while (*el != NULL)
5330 {
5331 endlabel_T *cur = (*el);
5332 isn_T *isn;
5333
5334 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5335 isn->isn_arg.jump.jump_where = instr->ga_len;
5336 *el = cur->el_next;
5337 vim_free(cur);
5338 }
5339}
5340
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005341 static void
5342compile_free_jump_to_end(endlabel_T **el)
5343{
5344 while (*el != NULL)
5345 {
5346 endlabel_T *cur = (*el);
5347
5348 *el = cur->el_next;
5349 vim_free(cur);
5350 }
5351}
5352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353/*
5354 * Create a new scope and set up the generic items.
5355 */
5356 static scope_T *
5357new_scope(cctx_T *cctx, scopetype_T type)
5358{
5359 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5360
5361 if (scope == NULL)
5362 return NULL;
5363 scope->se_outer = cctx->ctx_scope;
5364 cctx->ctx_scope = scope;
5365 scope->se_type = type;
5366 scope->se_local_count = cctx->ctx_locals.ga_len;
5367 return scope;
5368}
5369
5370/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005371 * Free the current scope and go back to the outer scope.
5372 */
5373 static void
5374drop_scope(cctx_T *cctx)
5375{
5376 scope_T *scope = cctx->ctx_scope;
5377
5378 if (scope == NULL)
5379 {
5380 iemsg("calling drop_scope() without a scope");
5381 return;
5382 }
5383 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005384 switch (scope->se_type)
5385 {
5386 case IF_SCOPE:
5387 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5388 case FOR_SCOPE:
5389 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5390 case WHILE_SCOPE:
5391 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5392 case TRY_SCOPE:
5393 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5394 case NO_SCOPE:
5395 case BLOCK_SCOPE:
5396 break;
5397 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005398 vim_free(scope);
5399}
5400
5401/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 * compile "if expr"
5403 *
5404 * "if expr" Produces instructions:
5405 * EVAL expr Push result of "expr"
5406 * JUMP_IF_FALSE end
5407 * ... body ...
5408 * end:
5409 *
5410 * "if expr | else" Produces instructions:
5411 * EVAL expr Push result of "expr"
5412 * JUMP_IF_FALSE else
5413 * ... body ...
5414 * JUMP_ALWAYS end
5415 * else:
5416 * ... body ...
5417 * end:
5418 *
5419 * "if expr1 | elseif expr2 | else" Produces instructions:
5420 * EVAL expr Push result of "expr"
5421 * JUMP_IF_FALSE elseif
5422 * ... body ...
5423 * JUMP_ALWAYS end
5424 * elseif:
5425 * EVAL expr Push result of "expr"
5426 * JUMP_IF_FALSE else
5427 * ... body ...
5428 * JUMP_ALWAYS end
5429 * else:
5430 * ... body ...
5431 * end:
5432 */
5433 static char_u *
5434compile_if(char_u *arg, cctx_T *cctx)
5435{
5436 char_u *p = arg;
5437 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005438 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005440 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005441 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442
Bram Moolenaara5565e42020-05-09 15:44:01 +02005443 CLEAR_FIELD(ppconst);
5444 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005445 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005446 clear_ppconst(&ppconst);
5447 return NULL;
5448 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005449 if (cctx->ctx_skip == SKIP_YES)
5450 clear_ppconst(&ppconst);
5451 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005452 {
5453 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005454 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005455 clear_ppconst(&ppconst);
5456 }
5457 else
5458 {
5459 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005460 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005461 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005462 return NULL;
5463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464
5465 scope = new_scope(cctx, IF_SCOPE);
5466 if (scope == NULL)
5467 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005468 scope->se_skip_save = skip_save;
5469 // "is_had_return" will be reset if any block does not end in :return
5470 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005472 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005473 {
5474 // "where" is set when ":elseif", "else" or ":endif" is found
5475 scope->se_u.se_if.is_if_label = instr->ga_len;
5476 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5477 }
5478 else
5479 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480
5481 return p;
5482}
5483
5484 static char_u *
5485compile_elseif(char_u *arg, cctx_T *cctx)
5486{
5487 char_u *p = arg;
5488 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005489 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 isn_T *isn;
5491 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005492 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493
5494 if (scope == NULL || scope->se_type != IF_SCOPE)
5495 {
5496 emsg(_(e_elseif_without_if));
5497 return NULL;
5498 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005499 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005500 if (!cctx->ctx_had_return)
5501 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005503 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005504 {
5505 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005507 return NULL;
5508 // previous "if" or "elseif" jumps here
5509 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5510 isn->isn_arg.jump.jump_where = instr->ga_len;
5511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005513 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005514 CLEAR_FIELD(ppconst);
5515 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005516 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005517 clear_ppconst(&ppconst);
5518 return NULL;
5519 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005520 if (scope->se_skip_save == SKIP_YES)
5521 clear_ppconst(&ppconst);
5522 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005523 {
5524 // The expression results in a constant.
5525 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005526 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005527 clear_ppconst(&ppconst);
5528 scope->se_u.se_if.is_if_label = -1;
5529 }
5530 else
5531 {
5532 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005533 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005534 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005535 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005537 // "where" is set when ":elseif", "else" or ":endif" is found
5538 scope->se_u.se_if.is_if_label = instr->ga_len;
5539 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
5542 return p;
5543}
5544
5545 static char_u *
5546compile_else(char_u *arg, cctx_T *cctx)
5547{
5548 char_u *p = arg;
5549 garray_T *instr = &cctx->ctx_instr;
5550 isn_T *isn;
5551 scope_T *scope = cctx->ctx_scope;
5552
5553 if (scope == NULL || scope->se_type != IF_SCOPE)
5554 {
5555 emsg(_(e_else_without_if));
5556 return NULL;
5557 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005558 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005559 if (!cctx->ctx_had_return)
5560 scope->se_u.se_if.is_had_return = FALSE;
5561 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562
Bram Moolenaarefd88552020-06-18 20:50:10 +02005563 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005564 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005565 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005566 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005567 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005568 if (!cctx->ctx_had_return
5569 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5570 JUMP_ALWAYS, cctx) == FAIL)
5571 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005572 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005573
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005574 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005575 {
5576 if (scope->se_u.se_if.is_if_label >= 0)
5577 {
5578 // previous "if" or "elseif" jumps here
5579 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5580 isn->isn_arg.jump.jump_where = instr->ga_len;
5581 scope->se_u.se_if.is_if_label = -1;
5582 }
5583 }
5584
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005585 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005586 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588
5589 return p;
5590}
5591
5592 static char_u *
5593compile_endif(char_u *arg, cctx_T *cctx)
5594{
5595 scope_T *scope = cctx->ctx_scope;
5596 ifscope_T *ifscope;
5597 garray_T *instr = &cctx->ctx_instr;
5598 isn_T *isn;
5599
5600 if (scope == NULL || scope->se_type != IF_SCOPE)
5601 {
5602 emsg(_(e_endif_without_if));
5603 return NULL;
5604 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005605 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005606 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005607 if (!cctx->ctx_had_return)
5608 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005610 if (scope->se_u.se_if.is_if_label >= 0)
5611 {
5612 // previous "if" or "elseif" jumps here
5613 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5614 isn->isn_arg.jump.jump_where = instr->ga_len;
5615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 // Fill in the "end" label in jumps at the end of the blocks.
5617 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005618 cctx->ctx_skip = scope->se_skip_save;
5619
5620 // If all the blocks end in :return and there is an :else then the
5621 // had_return flag is set.
5622 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005624 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 return arg;
5626}
5627
5628/*
5629 * compile "for var in expr"
5630 *
5631 * Produces instructions:
5632 * PUSHNR -1
5633 * STORE loop-idx Set index to -1
5634 * EVAL expr Push result of "expr"
5635 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5636 * - if beyond end, jump to "end"
5637 * - otherwise get item from list and push it
5638 * STORE var Store item in "var"
5639 * ... body ...
5640 * JUMP top Jump back to repeat
5641 * end: DROP Drop the result of "expr"
5642 *
5643 */
5644 static char_u *
5645compile_for(char_u *arg, cctx_T *cctx)
5646{
5647 char_u *p;
5648 size_t varlen;
5649 garray_T *instr = &cctx->ctx_instr;
5650 garray_T *stack = &cctx->ctx_type_stack;
5651 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005652 lvar_T *loop_lvar; // loop iteration variable
5653 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654 type_T *vartype;
5655
5656 // TODO: list of variables: "for [key, value] in dict"
5657 // parse "var"
5658 for (p = arg; eval_isnamec1(*p); ++p)
5659 ;
5660 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005661 var_lvar = lookup_local(arg, varlen, cctx);
5662 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005664 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665 return NULL;
5666 }
5667
5668 // consume "in"
5669 p = skipwhite(p);
5670 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5671 {
5672 emsg(_(e_missing_in));
5673 return NULL;
5674 }
5675 p = skipwhite(p + 2);
5676
5677
5678 scope = new_scope(cctx, FOR_SCOPE);
5679 if (scope == NULL)
5680 return NULL;
5681
5682 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005683 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5684 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005685 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005686 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005687 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690
5691 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005692 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5693 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005694 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005695 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005696 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005700 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
5702 // compile "expr", it remains on the stack until "endfor"
5703 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005704 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005705 {
5706 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005710 // Now that we know the type of "var", check that it is a list, now or at
5711 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005713 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005715 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 return NULL;
5717 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005718 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005719 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720
5721 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005722 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005724 generate_FOR(cctx, loop_lvar->lv_idx);
5725 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
5727 return arg;
5728}
5729
5730/*
5731 * compile "endfor"
5732 */
5733 static char_u *
5734compile_endfor(char_u *arg, cctx_T *cctx)
5735{
5736 garray_T *instr = &cctx->ctx_instr;
5737 scope_T *scope = cctx->ctx_scope;
5738 forscope_T *forscope;
5739 isn_T *isn;
5740
5741 if (scope == NULL || scope->se_type != FOR_SCOPE)
5742 {
5743 emsg(_(e_for));
5744 return NULL;
5745 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005746 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005748 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749
5750 // At end of ":for" scope jump back to the FOR instruction.
5751 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5752
5753 // Fill in the "end" label in the FOR statement so it can jump here
5754 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5755 isn->isn_arg.forloop.for_end = instr->ga_len;
5756
5757 // Fill in the "end" label any BREAK statements
5758 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5759
5760 // Below the ":for" scope drop the "expr" list from the stack.
5761 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5762 return NULL;
5763
5764 vim_free(scope);
5765
5766 return arg;
5767}
5768
5769/*
5770 * compile "while expr"
5771 *
5772 * Produces instructions:
5773 * top: EVAL expr Push result of "expr"
5774 * JUMP_IF_FALSE end jump if false
5775 * ... body ...
5776 * JUMP top Jump back to repeat
5777 * end:
5778 *
5779 */
5780 static char_u *
5781compile_while(char_u *arg, cctx_T *cctx)
5782{
5783 char_u *p = arg;
5784 garray_T *instr = &cctx->ctx_instr;
5785 scope_T *scope;
5786
5787 scope = new_scope(cctx, WHILE_SCOPE);
5788 if (scope == NULL)
5789 return NULL;
5790
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005791 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792
5793 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005794 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005795 return NULL;
5796
5797 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005798 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799 JUMP_IF_FALSE, cctx) == FAIL)
5800 return FAIL;
5801
5802 return p;
5803}
5804
5805/*
5806 * compile "endwhile"
5807 */
5808 static char_u *
5809compile_endwhile(char_u *arg, cctx_T *cctx)
5810{
5811 scope_T *scope = cctx->ctx_scope;
5812
5813 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5814 {
5815 emsg(_(e_while));
5816 return NULL;
5817 }
5818 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005819 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820
5821 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005822 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823
5824 // Fill in the "end" label in the WHILE statement so it can jump here.
5825 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005826 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827
5828 vim_free(scope);
5829
5830 return arg;
5831}
5832
5833/*
5834 * compile "continue"
5835 */
5836 static char_u *
5837compile_continue(char_u *arg, cctx_T *cctx)
5838{
5839 scope_T *scope = cctx->ctx_scope;
5840
5841 for (;;)
5842 {
5843 if (scope == NULL)
5844 {
5845 emsg(_(e_continue));
5846 return NULL;
5847 }
5848 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5849 break;
5850 scope = scope->se_outer;
5851 }
5852
5853 // Jump back to the FOR or WHILE instruction.
5854 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005855 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5856 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 return arg;
5858}
5859
5860/*
5861 * compile "break"
5862 */
5863 static char_u *
5864compile_break(char_u *arg, cctx_T *cctx)
5865{
5866 scope_T *scope = cctx->ctx_scope;
5867 endlabel_T **el;
5868
5869 for (;;)
5870 {
5871 if (scope == NULL)
5872 {
5873 emsg(_(e_break));
5874 return NULL;
5875 }
5876 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5877 break;
5878 scope = scope->se_outer;
5879 }
5880
5881 // Jump to the end of the FOR or WHILE loop.
5882 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005883 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005885 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5887 return FAIL;
5888
5889 return arg;
5890}
5891
5892/*
5893 * compile "{" start of block
5894 */
5895 static char_u *
5896compile_block(char_u *arg, cctx_T *cctx)
5897{
5898 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5899 return NULL;
5900 return skipwhite(arg + 1);
5901}
5902
5903/*
5904 * compile end of block: drop one scope
5905 */
5906 static void
5907compile_endblock(cctx_T *cctx)
5908{
5909 scope_T *scope = cctx->ctx_scope;
5910
5911 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005912 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 vim_free(scope);
5914}
5915
5916/*
5917 * compile "try"
5918 * Creates a new scope for the try-endtry, pointing to the first catch and
5919 * finally.
5920 * Creates another scope for the "try" block itself.
5921 * TRY instruction sets up exception handling at runtime.
5922 *
5923 * "try"
5924 * TRY -> catch1, -> finally push trystack entry
5925 * ... try block
5926 * "throw {exception}"
5927 * EVAL {exception}
5928 * THROW create exception
5929 * ... try block
5930 * " catch {expr}"
5931 * JUMP -> finally
5932 * catch1: PUSH exeception
5933 * EVAL {expr}
5934 * MATCH
5935 * JUMP nomatch -> catch2
5936 * CATCH remove exception
5937 * ... catch block
5938 * " catch"
5939 * JUMP -> finally
5940 * catch2: CATCH remove exception
5941 * ... catch block
5942 * " finally"
5943 * finally:
5944 * ... finally block
5945 * " endtry"
5946 * ENDTRY pop trystack entry, may rethrow
5947 */
5948 static char_u *
5949compile_try(char_u *arg, cctx_T *cctx)
5950{
5951 garray_T *instr = &cctx->ctx_instr;
5952 scope_T *try_scope;
5953 scope_T *scope;
5954
5955 // scope that holds the jumps that go to catch/finally/endtry
5956 try_scope = new_scope(cctx, TRY_SCOPE);
5957 if (try_scope == NULL)
5958 return NULL;
5959
5960 // "catch" is set when the first ":catch" is found.
5961 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005962 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 if (generate_instr(cctx, ISN_TRY) == NULL)
5964 return NULL;
5965
5966 // scope for the try block itself
5967 scope = new_scope(cctx, BLOCK_SCOPE);
5968 if (scope == NULL)
5969 return NULL;
5970
5971 return arg;
5972}
5973
5974/*
5975 * compile "catch {expr}"
5976 */
5977 static char_u *
5978compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5979{
5980 scope_T *scope = cctx->ctx_scope;
5981 garray_T *instr = &cctx->ctx_instr;
5982 char_u *p;
5983 isn_T *isn;
5984
5985 // end block scope from :try or :catch
5986 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5987 compile_endblock(cctx);
5988 scope = cctx->ctx_scope;
5989
5990 // Error if not in a :try scope
5991 if (scope == NULL || scope->se_type != TRY_SCOPE)
5992 {
5993 emsg(_(e_catch));
5994 return NULL;
5995 }
5996
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005997 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005999 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 return NULL;
6001 }
6002
6003 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 JUMP_ALWAYS, cctx) == FAIL)
6006 return NULL;
6007
6008 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006009 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010 if (isn->isn_arg.try.try_catch == 0)
6011 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006012 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 {
6014 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006015 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 isn->isn_arg.jump.jump_where = instr->ga_len;
6017 }
6018
6019 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006020 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 scope->se_u.se_try.ts_caught_all = TRUE;
6023 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 }
6025 else
6026 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006027 char_u *end;
6028 char_u *pat;
6029 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006030 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006031 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 // Push v:exception, push {expr} and MATCH
6034 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6035
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006036 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006037 if (*end != *p)
6038 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006039 semsg(_(e_separator_mismatch), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006040 vim_free(tofree);
6041 return FAIL;
6042 }
6043 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006044 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006045 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006046 len = (int)(end - tofree);
6047 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006048 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006049 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006050 if (pat == NULL)
6051 return FAIL;
6052 if (generate_PUSHS(cctx, pat) == FAIL)
6053 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6056 return NULL;
6057
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006058 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6060 return NULL;
6061 }
6062
6063 if (generate_instr(cctx, ISN_CATCH) == NULL)
6064 return NULL;
6065
6066 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6067 return NULL;
6068 return p;
6069}
6070
6071 static char_u *
6072compile_finally(char_u *arg, cctx_T *cctx)
6073{
6074 scope_T *scope = cctx->ctx_scope;
6075 garray_T *instr = &cctx->ctx_instr;
6076 isn_T *isn;
6077
6078 // end block scope from :try or :catch
6079 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6080 compile_endblock(cctx);
6081 scope = cctx->ctx_scope;
6082
6083 // Error if not in a :try scope
6084 if (scope == NULL || scope->se_type != TRY_SCOPE)
6085 {
6086 emsg(_(e_finally));
6087 return NULL;
6088 }
6089
6090 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006091 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 if (isn->isn_arg.try.try_finally != 0)
6093 {
6094 emsg(_(e_finally_dup));
6095 return NULL;
6096 }
6097
6098 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006099 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100
Bram Moolenaar585fea72020-04-02 22:33:21 +02006101 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006102 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 {
6104 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006105 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006107 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 }
6109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 // TODO: set index in ts_finally_label jumps
6111
6112 return arg;
6113}
6114
6115 static char_u *
6116compile_endtry(char_u *arg, cctx_T *cctx)
6117{
6118 scope_T *scope = cctx->ctx_scope;
6119 garray_T *instr = &cctx->ctx_instr;
6120 isn_T *isn;
6121
6122 // end block scope from :catch or :finally
6123 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6124 compile_endblock(cctx);
6125 scope = cctx->ctx_scope;
6126
6127 // Error if not in a :try scope
6128 if (scope == NULL || scope->se_type != TRY_SCOPE)
6129 {
6130 if (scope == NULL)
6131 emsg(_(e_no_endtry));
6132 else if (scope->se_type == WHILE_SCOPE)
6133 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006134 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 emsg(_(e_endfor));
6136 else
6137 emsg(_(e_endif));
6138 return NULL;
6139 }
6140
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006141 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6143 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006144 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 return NULL;
6146 }
6147
6148 // Fill in the "end" label in jumps at the end of the blocks, if not done
6149 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006150 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
6152 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006153 if (isn->isn_arg.try.try_catch == 0)
6154 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 if (isn->isn_arg.try.try_finally == 0)
6156 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006157
6158 if (scope->se_u.se_try.ts_catch_label != 0)
6159 {
6160 // Last catch without match jumps here
6161 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6162 isn->isn_arg.jump.jump_where = instr->ga_len;
6163 }
6164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 compile_endblock(cctx);
6166
6167 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6168 return NULL;
6169 return arg;
6170}
6171
6172/*
6173 * compile "throw {expr}"
6174 */
6175 static char_u *
6176compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6177{
6178 char_u *p = skipwhite(arg);
6179
Bram Moolenaara5565e42020-05-09 15:44:01 +02006180 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 return NULL;
6182 if (may_generate_2STRING(-1, cctx) == FAIL)
6183 return NULL;
6184 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6185 return NULL;
6186
6187 return p;
6188}
6189
6190/*
6191 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006192 * compile "echomsg expr"
6193 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006194 * compile "execute expr"
6195 */
6196 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006197compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006198{
6199 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006200 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006201 int count = 0;
6202
6203 for (;;)
6204 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006205 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006206 return NULL;
6207 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006208 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006209 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006210 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006211 break;
6212 }
6213
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006214 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6215 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6216 else if (cmdidx == CMD_execute)
6217 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6218 else if (cmdidx == CMD_echomsg)
6219 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6220 else
6221 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 return p;
6223}
6224
6225/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006226 * A command that is not compiled, execute with legacy code.
6227 */
6228 static char_u *
6229compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6230{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006231 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006232 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006233 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006234
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006235 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006236 goto theend;
6237
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006238 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006239 {
6240 long argt = excmd_get_argt(eap->cmdidx);
6241 int usefilter = FALSE;
6242
6243 has_expr = argt & (EX_XFILE | EX_EXPAND);
6244
6245 // If the command can be followed by a bar, find the bar and truncate
6246 // it, so that the following command can be compiled.
6247 // The '|' is overwritten with a NUL, it is put back below.
6248 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6249 && *eap->arg == '!')
6250 // :w !filter or :r !filter or :r! filter
6251 usefilter = TRUE;
6252 if ((argt & EX_TRLBAR) && !usefilter)
6253 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006254 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006255 separate_nextcmd(eap);
6256 if (eap->nextcmd != NULL)
6257 nextcmd = eap->nextcmd;
6258 }
6259 }
6260
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006261 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6262 {
6263 // expand filename in "syntax include [@group] filename"
6264 has_expr = TRUE;
6265 eap->arg = skipwhite(eap->arg + 7);
6266 if (*eap->arg == '@')
6267 eap->arg = skiptowhite(eap->arg);
6268 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006269
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006270 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006271 {
6272 int count = 0;
6273 char_u *start = skipwhite(line);
6274
6275 // :cmd xxx`=expr1`yyy`=expr2`zzz
6276 // PUSHS ":cmd xxx"
6277 // eval expr1
6278 // PUSHS "yyy"
6279 // eval expr2
6280 // PUSHS "zzz"
6281 // EXECCONCAT 5
6282 for (;;)
6283 {
6284 if (p > start)
6285 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006286 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006287 ++count;
6288 }
6289 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006290 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006291 return NULL;
6292 may_generate_2STRING(-1, cctx);
6293 ++count;
6294 p = skipwhite(p);
6295 if (*p != '`')
6296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006297 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006298 return NULL;
6299 }
6300 start = p + 1;
6301
6302 p = (char_u *)strstr((char *)start, "`=");
6303 if (p == NULL)
6304 {
6305 if (*skipwhite(start) != NUL)
6306 {
6307 generate_PUSHS(cctx, vim_strsave(start));
6308 ++count;
6309 }
6310 break;
6311 }
6312 }
6313 generate_EXECCONCAT(cctx, count);
6314 }
6315 else
6316 generate_EXEC(cctx, line);
6317
6318theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006319 if (*nextcmd != NUL)
6320 {
6321 // the parser expects a pointer to the bar, put it back
6322 --nextcmd;
6323 *nextcmd = '|';
6324 }
6325
6326 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006327}
6328
6329/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006330 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006331 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006332 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006333 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006334add_def_function(ufunc_T *ufunc)
6335{
6336 dfunc_T *dfunc;
6337
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006338 if (def_functions.ga_len == 0)
6339 {
6340 // The first position is not used, so that a zero uf_dfunc_idx means it
6341 // wasn't set.
6342 if (ga_grow(&def_functions, 1) == FAIL)
6343 return FAIL;
6344 ++def_functions.ga_len;
6345 }
6346
Bram Moolenaar09689a02020-05-09 22:50:08 +02006347 // Add the function to "def_functions".
6348 if (ga_grow(&def_functions, 1) == FAIL)
6349 return FAIL;
6350 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6351 CLEAR_POINTER(dfunc);
6352 dfunc->df_idx = def_functions.ga_len;
6353 ufunc->uf_dfunc_idx = dfunc->df_idx;
6354 dfunc->df_ufunc = ufunc;
6355 ++def_functions.ga_len;
6356 return OK;
6357}
6358
6359/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 * After ex_function() has collected all the function lines: parse and compile
6361 * the lines into instructions.
6362 * Adds the function to "def_functions".
6363 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6364 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006365 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006366 * This can be used recursively through compile_lambda(), which may reallocate
6367 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006368 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006370 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006371compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 char_u *line = NULL;
6374 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 cctx_T cctx;
6377 garray_T *instr;
6378 int called_emsg_before = called_emsg;
6379 int ret = FAIL;
6380 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006381 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006382 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006383 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006385 // When using a function that was compiled before: Free old instructions.
6386 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006387 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006389 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6390 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006391 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006393 else
6394 {
6395 if (add_def_function(ufunc) == FAIL)
6396 return FAIL;
6397 new_def_function = TRUE;
6398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399
Bram Moolenaar985116a2020-07-12 17:31:09 +02006400 ufunc->uf_def_status = UF_COMPILING;
6401
Bram Moolenaara80faa82020-04-12 19:37:17 +02006402 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 cctx.ctx_ufunc = ufunc;
6404 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006405 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6407 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6408 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6409 cctx.ctx_type_list = &ufunc->uf_type_list;
6410 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6411 instr = &cctx.ctx_instr;
6412
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006413 // Set the context to the function, it may be compiled when called from
6414 // another script. Set the script version to the most modern one.
6415 // The line number will be set in next_line_from_context().
6416 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6418
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006419 // Make sure error messages are OK.
6420 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6421 if (do_estack_push)
6422 estack_push_ufunc(ufunc, 1);
6423
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006424 if (ufunc->uf_def_args.ga_len > 0)
6425 {
6426 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006427 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006428 int i;
6429 char_u *arg;
6430 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006431 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006432
6433 // Produce instructions for the default values of optional arguments.
6434 // Store the instruction index in uf_def_arg_idx[] so that we know
6435 // where to start when the function is called, depending on the number
6436 // of arguments.
6437 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6438 if (ufunc->uf_def_arg_idx == NULL)
6439 goto erret;
6440 for (i = 0; i < count; ++i)
6441 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006442 garray_T *stack = &cctx.ctx_type_stack;
6443 type_T *val_type;
6444 int arg_idx = first_def_arg + i;
6445
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006446 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6447 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006448 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006449 goto erret;
6450
6451 // If no type specified use the type of the default value.
6452 // Otherwise check that the default value type matches the
6453 // specified type.
6454 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6455 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006456 {
6457 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006458 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006459 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006460 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006461 == FAIL)
6462 {
6463 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6464 arg_idx + 1);
6465 goto erret;
6466 }
6467
6468 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006469 goto erret;
6470 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006471 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006472
6473 if (did_set_arg_type)
6474 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006475 }
6476
6477 /*
6478 * Loop over all the lines of the function and generate instructions.
6479 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480 for (;;)
6481 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006482 exarg_T ea;
6483 cmdmod_T save_cmdmod;
6484 int starts_with_colon = FALSE;
6485 char_u *cmd;
6486 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006487
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006488 // Bail out on the first error to avoid a flood of errors and report
6489 // the right line number when inside try/catch.
6490 if (emsg_before != called_emsg)
6491 goto erret;
6492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 if (line != NULL && *line == '|')
6494 // the line continues after a '|'
6495 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006496 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006497 && !(*line == '#' && (line == cctx.ctx_line_start
6498 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006500 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 goto erret;
6502 }
6503 else
6504 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006505 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006506 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006507 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006510 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511
Bram Moolenaara80faa82020-04-12 19:37:17 +02006512 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 ea.cmdlinep = &line;
6514 ea.cmd = skipwhite(line);
6515
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006516 // Some things can be recognized by the first character.
6517 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006519 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006520 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006521 if (ea.cmd[1] != '{')
6522 {
6523 line = (char_u *)"";
6524 continue;
6525 }
6526 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006528 case '}':
6529 {
6530 // "}" ends a block scope
6531 scopetype_T stype = cctx.ctx_scope == NULL
6532 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006534 if (stype == BLOCK_SCOPE)
6535 {
6536 compile_endblock(&cctx);
6537 line = ea.cmd;
6538 }
6539 else
6540 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006541 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006542 goto erret;
6543 }
6544 if (line != NULL)
6545 line = skipwhite(ea.cmd + 1);
6546 continue;
6547 }
6548
6549 case '{':
6550 // "{" starts a block scope
6551 // "{'a': 1}->func() is something else
6552 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6553 {
6554 line = compile_block(ea.cmd, &cctx);
6555 continue;
6556 }
6557 break;
6558
6559 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006560 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006561 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 }
6563
6564 /*
6565 * COMMAND MODIFIERS
6566 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006567 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6569 {
6570 if (errormsg != NULL)
6571 goto erret;
6572 // empty line or comment
6573 line = (char_u *)"";
6574 continue;
6575 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006576 // TODO: use modifiers in the command
6577 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006578 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579
6580 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006581 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006583 {
6584 if (*ea.cmd == '(')
6585 // not for "call()"
6586 ea.cmd = p;
6587 else
6588 ea.cmd = skipwhite(ea.cmd);
6589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006591 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006593 char_u *pskip;
6594
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006595 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006596 // find what follows.
6597 // Skip over "var.member", "var[idx]" and the like.
6598 // Also "&opt = val", "$ENV = val" and "@r = val".
6599 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006600 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006601 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006602 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006604 char_u *var_end;
6605 int oplen;
6606 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607
Bram Moolenaar65821722020-08-02 18:58:54 +02006608 if (ea.cmd[0] == '@')
6609 var_end = ea.cmd + 2;
6610 else
6611 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006612 FNE_CHECK_START | FNE_INCL_BR);
6613 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006614 if (oplen > 0)
6615 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006616 size_t len = p - ea.cmd;
6617
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006618 // Recognize an assignment if we recognize the variable
6619 // name:
6620 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006621 // "local = expr" where "local" is a local var.
6622 // "script = expr" where "script" is a script-local var.
6623 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006624 // "&opt = expr"
6625 // "$ENV = expr"
6626 // "@r = expr"
6627 if (*ea.cmd == '&'
6628 || *ea.cmd == '$'
6629 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006630 || ((len) > 2 && ea.cmd[1] == ':')
6631 || lookup_local(ea.cmd, len, &cctx) != NULL
6632 || lookup_arg(ea.cmd, len, NULL, NULL,
6633 NULL, &cctx) == OK
6634 || lookup_script(ea.cmd, len) == OK
6635 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006636 {
6637 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006639 goto erret;
6640 continue;
6641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 }
6643 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006644
6645 if (*ea.cmd == '[')
6646 {
6647 // [var, var] = expr
6648 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6649 if (line == NULL)
6650 goto erret;
6651 if (line != ea.cmd)
6652 continue;
6653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 }
6655
6656 /*
6657 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006658 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006660 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006661 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006662 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006663 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006664 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006665 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006666 if (!starts_with_colon)
6667 {
6668 emsg(_(e_colon_required_before_a_range));
6669 goto erret;
6670 }
6671 if (ends_excmd2(line, ea.cmd))
6672 {
6673 // A range without a command: jump to the line.
6674 // TODO: compile to a more efficient command, possibly
6675 // calling parse_cmd_address().
6676 ea.cmdidx = CMD_SIZE;
6677 line = compile_exec(line, &ea, &cctx);
6678 goto nextline;
6679 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006680 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006681 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006682 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006683 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006684 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
6686 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6687 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006688 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006689 {
6690 line += STRLEN(line);
6691 continue;
6692 }
6693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006695 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006697 // CMD_let cannot happen, compile_assignment() above is used
6698 iemsg("Command from find_ex_command() not handled");
6699 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 }
6702
6703 p = skipwhite(p);
6704
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006705 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006706 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006707 && ea.cmdidx != CMD_elseif
6708 && ea.cmdidx != CMD_else
6709 && ea.cmdidx != CMD_endif)
6710 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006711 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006712 continue;
6713 }
6714
Bram Moolenaarefd88552020-06-18 20:50:10 +02006715 if (ea.cmdidx != CMD_elseif
6716 && ea.cmdidx != CMD_else
6717 && ea.cmdidx != CMD_endif
6718 && ea.cmdidx != CMD_endfor
6719 && ea.cmdidx != CMD_endwhile
6720 && ea.cmdidx != CMD_catch
6721 && ea.cmdidx != CMD_finally
6722 && ea.cmdidx != CMD_endtry)
6723 {
6724 if (cctx.ctx_had_return)
6725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006726 emsg(_(e_unreachable_code_after_return));
Bram Moolenaarefd88552020-06-18 20:50:10 +02006727 goto erret;
6728 }
6729 }
6730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 switch (ea.cmdidx)
6732 {
6733 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006734 ea.arg = p;
6735 line = compile_nested_function(&ea, &cctx);
6736 break;
6737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006739 // TODO: should we allow this, e.g. to declare a global
6740 // function?
6741 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 goto erret;
6743
6744 case CMD_return:
6745 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006746 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 break;
6748
6749 case CMD_let:
6750 case CMD_const:
6751 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 if (line == p)
6753 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 break;
6755
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006756 case CMD_unlet:
6757 case CMD_unlockvar:
6758 case CMD_lockvar:
6759 line = compile_unletlock(p, &ea, &cctx);
6760 break;
6761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 case CMD_import:
6763 line = compile_import(p, &cctx);
6764 break;
6765
6766 case CMD_if:
6767 line = compile_if(p, &cctx);
6768 break;
6769 case CMD_elseif:
6770 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006771 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 break;
6773 case CMD_else:
6774 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006775 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 break;
6777 case CMD_endif:
6778 line = compile_endif(p, &cctx);
6779 break;
6780
6781 case CMD_while:
6782 line = compile_while(p, &cctx);
6783 break;
6784 case CMD_endwhile:
6785 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006786 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 break;
6788
6789 case CMD_for:
6790 line = compile_for(p, &cctx);
6791 break;
6792 case CMD_endfor:
6793 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006794 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 break;
6796 case CMD_continue:
6797 line = compile_continue(p, &cctx);
6798 break;
6799 case CMD_break:
6800 line = compile_break(p, &cctx);
6801 break;
6802
6803 case CMD_try:
6804 line = compile_try(p, &cctx);
6805 break;
6806 case CMD_catch:
6807 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006808 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 break;
6810 case CMD_finally:
6811 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006812 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813 break;
6814 case CMD_endtry:
6815 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006816 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817 break;
6818 case CMD_throw:
6819 line = compile_throw(p, &cctx);
6820 break;
6821
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006822 case CMD_eval:
6823 if (compile_expr0(&p, &cctx) == FAIL)
6824 goto erret;
6825
6826 // drop the return value
6827 generate_instr_drop(&cctx, ISN_DROP, 1);
6828
6829 line = skipwhite(p);
6830 break;
6831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006834 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006835 case CMD_echomsg:
6836 case CMD_echoerr:
6837 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006838 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006840 // TODO: other commands with an expression argument
6841
Bram Moolenaarae616492020-07-28 20:07:27 +02006842 case CMD_append:
6843 case CMD_change:
6844 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006845 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006846 case CMD_xit:
6847 not_in_vim9(&ea);
6848 goto erret;
6849
Bram Moolenaar002262f2020-07-08 17:47:57 +02006850 case CMD_SIZE:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006851 semsg(_(e_invalid_command_str), ea.cmd);
Bram Moolenaar002262f2020-07-08 17:47:57 +02006852 goto erret;
6853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006855 // Not recognized, execute with do_cmdline_cmd().
6856 ea.arg = p;
6857 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858 break;
6859 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006860nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 if (line == NULL)
6862 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006863 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864
6865 if (cctx.ctx_type_stack.ga_len < 0)
6866 {
6867 iemsg("Type stack underflow");
6868 goto erret;
6869 }
6870 }
6871
6872 if (cctx.ctx_scope != NULL)
6873 {
6874 if (cctx.ctx_scope->se_type == IF_SCOPE)
6875 emsg(_(e_endif));
6876 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6877 emsg(_(e_endwhile));
6878 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6879 emsg(_(e_endfor));
6880 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006881 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 goto erret;
6883 }
6884
Bram Moolenaarefd88552020-06-18 20:50:10 +02006885 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 {
6887 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006889 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 goto erret;
6891 }
6892
6893 // Return zero if there is no return at the end.
6894 generate_PUSHNR(&cctx, 0);
6895 generate_instr(&cctx, ISN_RETURN);
6896 }
6897
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006898 {
6899 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6900 + ufunc->uf_dfunc_idx;
6901 dfunc->df_deleted = FALSE;
6902 dfunc->df_instr = instr->ga_data;
6903 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006904 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006905 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006906 if (cctx.ctx_outer_used)
6907 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006908 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910
6911 ret = OK;
6912
6913erret:
6914 if (ret == FAIL)
6915 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006916 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006917 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6918 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006919
6920 for (idx = 0; idx < instr->ga_len; ++idx)
6921 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006923
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006924 // If using the last entry in the table and it was added above, we
6925 // might as well remove it.
6926 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006927 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006928 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006929 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006930 ufunc->uf_dfunc_idx = 0;
6931 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006932 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006933
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006934 while (cctx.ctx_scope != NULL)
6935 drop_scope(&cctx);
6936
Bram Moolenaar20431c92020-03-20 18:39:46 +01006937 // Don't execute this function body.
6938 ga_clear_strings(&ufunc->uf_lines);
6939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 if (errormsg != NULL)
6941 emsg(errormsg);
6942 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006943 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 }
6945
6946 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006947 if (do_estack_push)
6948 estack_pop();
6949
Bram Moolenaar20431c92020-03-20 18:39:46 +01006950 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006951 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006953 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954}
6955
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006956 void
6957set_function_type(ufunc_T *ufunc)
6958{
6959 int varargs = ufunc->uf_va_name != NULL;
6960 int argcount = ufunc->uf_args.ga_len;
6961
6962 // Create a type for the function, with the return type and any
6963 // argument types.
6964 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6965 // The type is included in "tt_args".
6966 if (argcount > 0 || varargs)
6967 {
6968 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6969 argcount, &ufunc->uf_type_list);
6970 // Add argument types to the function type.
6971 if (func_type_add_arg_types(ufunc->uf_func_type,
6972 argcount + varargs,
6973 &ufunc->uf_type_list) == FAIL)
6974 return;
6975 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6976 ufunc->uf_func_type->tt_min_argcount =
6977 argcount - ufunc->uf_def_args.ga_len;
6978 if (ufunc->uf_arg_types == NULL)
6979 {
6980 int i;
6981
6982 // lambda does not have argument types.
6983 for (i = 0; i < argcount; ++i)
6984 ufunc->uf_func_type->tt_args[i] = &t_any;
6985 }
6986 else
6987 mch_memmove(ufunc->uf_func_type->tt_args,
6988 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6989 if (varargs)
6990 {
6991 ufunc->uf_func_type->tt_args[argcount] =
6992 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6993 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6994 }
6995 }
6996 else
6997 // No arguments, can use a predefined type.
6998 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6999 argcount, &ufunc->uf_type_list);
7000}
7001
7002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003/*
7004 * Delete an instruction, free what it contains.
7005 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007006 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007delete_instr(isn_T *isn)
7008{
7009 switch (isn->isn_type)
7010 {
7011 case ISN_EXEC:
7012 case ISN_LOADENV:
7013 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007014 case ISN_LOADB:
7015 case ISN_LOADW:
7016 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007018 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 case ISN_PUSHEXC:
7020 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007021 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007023 case ISN_STOREB:
7024 case ISN_STOREW:
7025 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007026 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 vim_free(isn->isn_arg.string);
7028 break;
7029
7030 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007031 case ISN_STORES:
7032 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 break;
7034
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007035 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007036 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007037 vim_free(isn->isn_arg.unlet.ul_name);
7038 break;
7039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 case ISN_STOREOPT:
7041 vim_free(isn->isn_arg.storeopt.so_name);
7042 break;
7043
7044 case ISN_PUSHBLOB: // push blob isn_arg.blob
7045 blob_unref(isn->isn_arg.blob);
7046 break;
7047
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007048 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007049#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007050 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007051#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007052 break;
7053
7054 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007056 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007057#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007058 break;
7059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 case ISN_UCALL:
7061 vim_free(isn->isn_arg.ufunc.cuf_name);
7062 break;
7063
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007064 case ISN_FUNCREF:
7065 {
7066 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7067 + isn->isn_arg.funcref.fr_func;
7068 func_ptr_unref(dfunc->df_ufunc);
7069 }
7070 break;
7071
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007072 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007073 {
7074 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7075 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7076
7077 if (ufunc != NULL)
7078 {
7079 // Clear uf_dfunc_idx so that the function is deleted.
7080 clear_def_function(ufunc);
7081 ufunc->uf_dfunc_idx = 0;
7082 func_ptr_unref(ufunc);
7083 }
7084
7085 vim_free(lambda);
7086 vim_free(isn->isn_arg.newfunc.nf_global);
7087 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007088 break;
7089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 case ISN_2BOOL:
7091 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007092 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 case ISN_ADDBLOB:
7094 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007095 case ISN_ANYINDEX:
7096 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 case ISN_BCALL:
7098 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007099 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 case ISN_CHECKNR:
7101 case ISN_CHECKTYPE:
7102 case ISN_COMPAREANY:
7103 case ISN_COMPAREBLOB:
7104 case ISN_COMPAREBOOL:
7105 case ISN_COMPAREDICT:
7106 case ISN_COMPAREFLOAT:
7107 case ISN_COMPAREFUNC:
7108 case ISN_COMPARELIST:
7109 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 case ISN_COMPARESPECIAL:
7111 case ISN_COMPARESTRING:
7112 case ISN_CONCAT:
7113 case ISN_DCALL:
7114 case ISN_DROP:
7115 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007116 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007117 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007119 case ISN_EXECCONCAT:
7120 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007122 case ISN_GETITEM:
7123 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007124 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007125 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007127 case ISN_LOADBDICT:
7128 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007129 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007131 case ISN_LOADSCRIPT:
7132 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007134 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007135 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 case ISN_NEGATENR:
7137 case ISN_NEWDICT:
7138 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007140 case ISN_OPFLOAT:
7141 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007143 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007144 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 case ISN_PUSHF:
7146 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 case ISN_PUSHSPEC:
7148 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007149 case ISN_SHUFFLE:
7150 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007152 case ISN_STOREDICT:
7153 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007154 case ISN_STORENR:
7155 case ISN_STOREOUTER:
7156 case ISN_STOREREG:
7157 case ISN_STORESCRIPT:
7158 case ISN_STOREV:
7159 case ISN_STRINDEX:
7160 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 case ISN_THROW:
7162 case ISN_TRY:
7163 // nothing allocated
7164 break;
7165 }
7166}
7167
7168/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007169 * Free all instructions for "dfunc".
7170 */
7171 static void
7172delete_def_function_contents(dfunc_T *dfunc)
7173{
7174 int idx;
7175
7176 ga_clear(&dfunc->df_def_args_isn);
7177
7178 if (dfunc->df_instr != NULL)
7179 {
7180 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7181 delete_instr(dfunc->df_instr + idx);
7182 VIM_CLEAR(dfunc->df_instr);
7183 }
7184
7185 dfunc->df_deleted = TRUE;
7186}
7187
7188/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007189 * When a user function is deleted, clear the contents of any associated def
7190 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 */
7192 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007193clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007195 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 {
7197 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7198 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199
Bram Moolenaar20431c92020-03-20 18:39:46 +01007200 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007201 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 }
7203}
7204
7205#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007206/*
7207 * Free all functions defined with ":def".
7208 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209 void
7210free_def_functions(void)
7211{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007212 int idx;
7213
7214 for (idx = 0; idx < def_functions.ga_len; ++idx)
7215 {
7216 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7217
7218 delete_def_function_contents(dfunc);
7219 }
7220
7221 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222}
7223#endif
7224
7225
7226#endif // FEAT_EVAL