blob: a6bdcc3f3df56c78a3c1988221045c6823540cbe [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 Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
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/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200280 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 dictitem_T *di;
282
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200283 if (current_sctx.sc_sid <= 0)
284 return FAIL;
285 ht = &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar84367732020-08-23 15:21:55 +0200286 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 cc = name[len];
289 name[len] = NUL;
290 di = find_var_in_ht(ht, 0, name, TRUE);
291 name[len] = cc;
292 return di == NULL ? FAIL: OK;
293}
294
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100295/*
296 * Check if "p[len]" is already defined, either in script "import_sid" or in
297 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200298 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299 * Return FAIL and give an error if it defined.
300 */
301 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200302check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200304 int c = p[len];
305 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200306
307 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200308 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200310 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200311 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200312 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200313 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100314 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200315 // A local or script-local function can shadow a global function.
316 if (ufunc == NULL || !func_is_global(ufunc)
317 || (p[0] == 'g' && p[1] == ':'))
318 {
319 p[len] = c;
320 semsg(_(e_name_already_defined_str), p);
321 return FAIL;
322 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100323 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200324 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100325 return OK;
326}
327
Bram Moolenaar65b95452020-07-19 14:03:09 +0200328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329/////////////////////////////////////////////////////////////////////
330// Following generate_ functions expect the caller to call ga_grow().
331
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200332#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
333#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335/*
336 * Generate an instruction without arguments.
337 * Returns a pointer to the new instruction, NULL if failed.
338 */
339 static isn_T *
340generate_instr(cctx_T *cctx, isntype_T isn_type)
341{
342 garray_T *instr = &cctx->ctx_instr;
343 isn_T *isn;
344
Bram Moolenaar080457c2020-03-03 21:53:32 +0100345 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (ga_grow(instr, 1) == FAIL)
347 return NULL;
348 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
349 isn->isn_type = isn_type;
350 isn->isn_lnum = cctx->ctx_lnum + 1;
351 ++instr->ga_len;
352
353 return isn;
354}
355
356/*
357 * Generate an instruction without arguments.
358 * "drop" will be removed from the stack.
359 * Returns a pointer to the new instruction, NULL if failed.
360 */
361 static isn_T *
362generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
363{
364 garray_T *stack = &cctx->ctx_type_stack;
365
Bram Moolenaar080457c2020-03-03 21:53:32 +0100366 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 stack->ga_len -= drop;
368 return generate_instr(cctx, isn_type);
369}
370
371/*
372 * Generate instruction "isn_type" and put "type" on the type stack.
373 */
374 static isn_T *
375generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
376{
377 isn_T *isn;
378 garray_T *stack = &cctx->ctx_type_stack;
379
380 if ((isn = generate_instr(cctx, isn_type)) == NULL)
381 return NULL;
382
383 if (ga_grow(stack, 1) == FAIL)
384 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200385 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 ++stack->ga_len;
387
388 return isn;
389}
390
391/*
392 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 */
395 static int
396may_generate_2STRING(int offset, cctx_T *cctx)
397{
398 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200399 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 garray_T *stack = &cctx->ctx_type_stack;
401 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
402
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200403 switch ((*type)->tt_type)
404 {
405 // nothing to be done
406 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200408 // conversion possible
409 case VAR_SPECIAL:
410 case VAR_BOOL:
411 case VAR_NUMBER:
412 case VAR_FLOAT:
413 break;
414
415 // conversion possible (with runtime check)
416 case VAR_ANY:
417 case VAR_UNKNOWN:
418 isntype = ISN_2STRING_ANY;
419 break;
420
421 // conversion not possible
422 case VAR_VOID:
423 case VAR_BLOB:
424 case VAR_FUNC:
425 case VAR_PARTIAL:
426 case VAR_LIST:
427 case VAR_DICT:
428 case VAR_JOB:
429 case VAR_CHANNEL:
430 to_string_error((*type)->tt_type);
431 return FAIL;
432 }
433
434 *type = &t_string;
435 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 return FAIL;
437 isn->isn_arg.number = offset;
438
439 return OK;
440}
441
442 static int
443check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
444{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200445 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200447 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 {
449 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200450 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200452 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455 return OK;
456}
457
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200458 static int
459generate_add_instr(
460 cctx_T *cctx,
461 vartype_T vartype,
462 type_T *type1,
463 type_T *type2)
464{
465 isn_T *isn = generate_instr_drop(cctx,
466 vartype == VAR_NUMBER ? ISN_OPNR
467 : vartype == VAR_LIST ? ISN_ADDLIST
468 : vartype == VAR_BLOB ? ISN_ADDBLOB
469#ifdef FEAT_FLOAT
470 : vartype == VAR_FLOAT ? ISN_OPFLOAT
471#endif
472 : ISN_OPANY, 1);
473
474 if (vartype != VAR_LIST && vartype != VAR_BLOB
475 && type1->tt_type != VAR_ANY
476 && type2->tt_type != VAR_ANY
477 && check_number_or_float(
478 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
479 return FAIL;
480
481 if (isn != NULL)
482 isn->isn_arg.op.op_type = EXPR_ADD;
483 return isn == NULL ? FAIL : OK;
484}
485
486/*
487 * Get the type to use for an instruction for an operation on "type1" and
488 * "type2". If they are matching use a type-specific instruction. Otherwise
489 * fall back to runtime type checking.
490 */
491 static vartype_T
492operator_type(type_T *type1, type_T *type2)
493{
494 if (type1->tt_type == type2->tt_type
495 && (type1->tt_type == VAR_NUMBER
496 || type1->tt_type == VAR_LIST
497#ifdef FEAT_FLOAT
498 || type1->tt_type == VAR_FLOAT
499#endif
500 || type1->tt_type == VAR_BLOB))
501 return type1->tt_type;
502 return VAR_ANY;
503}
504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505/*
506 * Generate an instruction with two arguments. The instruction depends on the
507 * type of the arguments.
508 */
509 static int
510generate_two_op(cctx_T *cctx, char_u *op)
511{
512 garray_T *stack = &cctx->ctx_type_stack;
513 type_T *type1;
514 type_T *type2;
515 vartype_T vartype;
516 isn_T *isn;
517
Bram Moolenaar080457c2020-03-03 21:53:32 +0100518 RETURN_OK_IF_SKIP(cctx);
519
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
522 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200523 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524
525 switch (*op)
526 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200527 case '+':
528 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 break;
531
532 case '-':
533 case '*':
534 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
535 op) == FAIL)
536 return FAIL;
537 if (vartype == VAR_NUMBER)
538 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
539#ifdef FEAT_FLOAT
540 else if (vartype == VAR_FLOAT)
541 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
542#endif
543 else
544 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
545 if (isn != NULL)
546 isn->isn_arg.op.op_type = *op == '*'
547 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
548 break;
549
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200552 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 && type2->tt_type != VAR_NUMBER))
554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200555 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 return FAIL;
557 }
558 isn = generate_instr_drop(cctx,
559 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_REM;
562 break;
563 }
564
565 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200566 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 {
568 type_T *type = &t_any;
569
570#ifdef FEAT_FLOAT
571 // float+number and number+float results in float
572 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
573 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
574 type = &t_float;
575#endif
576 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
577 }
578
579 return OK;
580}
581
582/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 * Get the instruction to use for comparing "type1" with "type2"
584 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200586 static isntype_T
587get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588{
589 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590
Bram Moolenaar4c683752020-04-05 21:38:23 +0200591 if (type1 == VAR_UNKNOWN)
592 type1 = VAR_ANY;
593 if (type2 == VAR_UNKNOWN)
594 type2 = VAR_ANY;
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 if (type1 == type2)
597 {
598 switch (type1)
599 {
600 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
601 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
602 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
603 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
604 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
605 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
606 case VAR_LIST: isntype = ISN_COMPARELIST; break;
607 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
608 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609 default: isntype = ISN_COMPAREANY; break;
610 }
611 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200612 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
614 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
615 isntype = ISN_COMPAREANY;
616
617 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
618 && (isntype == ISN_COMPAREBOOL
619 || isntype == ISN_COMPARESPECIAL
620 || isntype == ISN_COMPARENR
621 || isntype == ISN_COMPAREFLOAT))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200625 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 }
627 if (isntype == ISN_DROP
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
630 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
631 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
632 && exptype != EXPR_IS && exptype != EXPR_ISNOT
633 && (type1 == VAR_BLOB || type2 == VAR_BLOB
634 || type1 == VAR_LIST || type2 == VAR_LIST))))
635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200636 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200638 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200640 return isntype;
641}
642
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200643 int
644check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
645{
646 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
647 return FAIL;
648 return OK;
649}
650
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651/*
652 * Generate an ISN_COMPARE* instruction with a boolean result.
653 */
654 static int
655generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
656{
657 isntype_T isntype;
658 isn_T *isn;
659 garray_T *stack = &cctx->ctx_type_stack;
660 vartype_T type1;
661 vartype_T type2;
662
663 RETURN_OK_IF_SKIP(cctx);
664
665 // Get the known type of the two items on the stack. If they are matching
666 // use a type-specific instruction. Otherwise fall back to runtime type
667 // checking.
668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
670 isntype = get_compare_isn(exptype, type1, type2);
671 if (isntype == ISN_DROP)
672 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 if ((isn = generate_instr(cctx, isntype)) == NULL)
675 return FAIL;
676 isn->isn_arg.op.op_type = exptype;
677 isn->isn_arg.op.op_ic = ic;
678
679 // takes two arguments, puts one bool back
680 if (stack->ga_len >= 2)
681 {
682 --stack->ga_len;
683 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
684 }
685
686 return OK;
687}
688
689/*
690 * Generate an ISN_2BOOL instruction.
691 */
692 static int
693generate_2BOOL(cctx_T *cctx, int invert)
694{
695 isn_T *isn;
696 garray_T *stack = &cctx->ctx_type_stack;
697
Bram Moolenaar080457c2020-03-03 21:53:32 +0100698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
700 return FAIL;
701 isn->isn_arg.number = invert;
702
703 // type becomes bool
704 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
705
706 return OK;
707}
708
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200709/*
710 * Generate an ISN_COND2BOOL instruction.
711 */
712 static int
713generate_COND2BOOL(cctx_T *cctx)
714{
715 isn_T *isn;
716 garray_T *stack = &cctx->ctx_type_stack;
717
718 RETURN_OK_IF_SKIP(cctx);
719 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
720 return FAIL;
721
722 // type becomes bool
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
724
725 return OK;
726}
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200729generate_TYPECHECK(
730 cctx_T *cctx,
731 type_T *expected,
732 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733{
734 isn_T *isn;
735 garray_T *stack = &cctx->ctx_type_stack;
736
Bram Moolenaar080457c2020-03-03 21:53:32 +0100737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
739 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200740 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 isn->isn_arg.type.ct_off = offset;
742
Bram Moolenaar5e654232020-09-16 15:22:00 +0200743 // type becomes expected
744 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 return OK;
747}
748
749/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200750 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200751 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200752 * - "actual" is a type that can be "expected" type: add a runtime check; or
753 * - return FAIL.
754 */
755 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200756need_type(
757 type_T *actual,
758 type_T *expected,
759 int offset,
760 cctx_T *cctx,
761 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200762{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200763 if (expected == &t_bool && actual != &t_bool
764 && (actual->tt_flags & TTFLAG_BOOL_OK))
765 {
766 // Using "0", "1" or the result of an expression with "&&" or "||" as a
767 // boolean is OK but requires a conversion.
768 generate_2BOOL(cctx, FALSE);
769 return OK;
770 }
771
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200772 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200773 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200774
775 // If the actual type can be the expected type add a runtime check.
776 // TODO: if it's a constant a runtime check makes no sense.
777 if (actual->tt_type == VAR_ANY
778 || actual->tt_type == VAR_UNKNOWN
779 || (actual->tt_type == VAR_FUNC
780 && (expected->tt_type == VAR_FUNC
781 || expected->tt_type == VAR_PARTIAL)
782 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
783 || (actual->tt_type == VAR_LIST
784 && expected->tt_type == VAR_LIST
785 && actual->tt_member == &t_any)
786 || (actual->tt_type == VAR_DICT
787 && expected->tt_type == VAR_DICT
788 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200789 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200790 generate_TYPECHECK(cctx, expected, offset);
791 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200792 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200793
794 if (!silent)
795 type_mismatch(expected, actual);
796 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200797}
798
799/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 * Generate an ISN_PUSHNR instruction.
801 */
802 static int
803generate_PUSHNR(cctx_T *cctx, varnumber_T number)
804{
805 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200806 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807
Bram Moolenaar080457c2020-03-03 21:53:32 +0100808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
810 return FAIL;
811 isn->isn_arg.number = number;
812
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200813 if (number == 0 || number == 1)
814 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200815 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200816
817 // A 0 or 1 number can also be used as a bool.
818 if (type != NULL)
819 {
820 type->tt_type = VAR_NUMBER;
821 type->tt_flags = TTFLAG_BOOL_OK;
822 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
823 }
824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 return OK;
826}
827
828/*
829 * Generate an ISN_PUSHBOOL instruction.
830 */
831 static int
832generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
833{
834 isn_T *isn;
835
Bram Moolenaar080457c2020-03-03 21:53:32 +0100836 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
838 return FAIL;
839 isn->isn_arg.number = number;
840
841 return OK;
842}
843
844/*
845 * Generate an ISN_PUSHSPEC instruction.
846 */
847 static int
848generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
849{
850 isn_T *isn;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = number;
856
857 return OK;
858}
859
860#ifdef FEAT_FLOAT
861/*
862 * Generate an ISN_PUSHF instruction.
863 */
864 static int
865generate_PUSHF(cctx_T *cctx, float_T fnumber)
866{
867 isn_T *isn;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
871 return FAIL;
872 isn->isn_arg.fnumber = fnumber;
873
874 return OK;
875}
876#endif
877
878/*
879 * Generate an ISN_PUSHS instruction.
880 * Consumes "str".
881 */
882 static int
883generate_PUSHS(cctx_T *cctx, char_u *str)
884{
885 isn_T *isn;
886
Bram Moolenaar080457c2020-03-03 21:53:32 +0100887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
889 return FAIL;
890 isn->isn_arg.string = str;
891
892 return OK;
893}
894
895/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100896 * Generate an ISN_PUSHCHANNEL instruction.
897 * Consumes "channel".
898 */
899 static int
900generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
901{
902 isn_T *isn;
903
Bram Moolenaar080457c2020-03-03 21:53:32 +0100904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100905 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
906 return FAIL;
907 isn->isn_arg.channel = channel;
908
909 return OK;
910}
911
912/*
913 * Generate an ISN_PUSHJOB instruction.
914 * Consumes "job".
915 */
916 static int
917generate_PUSHJOB(cctx_T *cctx, job_T *job)
918{
919 isn_T *isn;
920
Bram Moolenaar080457c2020-03-03 21:53:32 +0100921 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100922 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100923 return FAIL;
924 isn->isn_arg.job = job;
925
926 return OK;
927}
928
929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 * Generate an ISN_PUSHBLOB instruction.
931 * Consumes "blob".
932 */
933 static int
934generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
935{
936 isn_T *isn;
937
Bram Moolenaar080457c2020-03-03 21:53:32 +0100938 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
940 return FAIL;
941 isn->isn_arg.blob = blob;
942
943 return OK;
944}
945
946/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100947 * Generate an ISN_PUSHFUNC instruction with name "name".
948 * Consumes "name".
949 */
950 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200951generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200956 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100957 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200958 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100959
960 return OK;
961}
962
963/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200964 * Generate an ISN_GETITEM instruction with "index".
965 */
966 static int
967generate_GETITEM(cctx_T *cctx, int index)
968{
969 isn_T *isn;
970 garray_T *stack = &cctx->ctx_type_stack;
971 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
972 type_T *item_type = &t_any;
973
974 RETURN_OK_IF_SKIP(cctx);
975
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200976 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200977 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200978 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 emsg(_(e_listreq));
980 return FAIL;
981 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200982 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200983 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
984 return FAIL;
985 isn->isn_arg.number = index;
986
987 // add the item type to the type stack
988 if (ga_grow(stack, 1) == FAIL)
989 return FAIL;
990 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
991 ++stack->ga_len;
992 return OK;
993}
994
995/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200996 * Generate an ISN_SLICE instruction with "count".
997 */
998 static int
999generate_SLICE(cctx_T *cctx, int count)
1000{
1001 isn_T *isn;
1002
1003 RETURN_OK_IF_SKIP(cctx);
1004 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.number = count;
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_CHECKLEN instruction with "min_len".
1012 */
1013 static int
1014generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1015{
1016 isn_T *isn;
1017
1018 RETURN_OK_IF_SKIP(cctx);
1019
1020 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.checklen.cl_min_len = min_len;
1023 isn->isn_arg.checklen.cl_more_OK = more_OK;
1024
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Generate an ISN_STORE instruction.
1030 */
1031 static int
1032generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1038 return FAIL;
1039 if (name != NULL)
1040 isn->isn_arg.string = vim_strsave(name);
1041 else
1042 isn->isn_arg.number = idx;
1043
1044 return OK;
1045}
1046
1047/*
1048 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1049 */
1050 static int
1051generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1052{
1053 isn_T *isn;
1054
Bram Moolenaar080457c2020-03-03 21:53:32 +01001055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1057 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001058 isn->isn_arg.storenr.stnr_idx = idx;
1059 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
1061 return OK;
1062}
1063
1064/*
1065 * Generate an ISN_STOREOPT instruction
1066 */
1067 static int
1068generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001073 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 return FAIL;
1075 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1076 isn->isn_arg.storeopt.so_flags = opt_flags;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOAD or similar instruction.
1083 */
1084 static int
1085generate_LOAD(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int idx,
1089 char_u *name,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1096 return FAIL;
1097 if (name != NULL)
1098 isn->isn_arg.string = vim_strsave(name);
1099 else
1100 isn->isn_arg.number = idx;
1101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001106 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001107 */
1108 static int
1109generate_LOADV(
1110 cctx_T *cctx,
1111 char_u *name,
1112 int error)
1113{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001114 int di_flags;
1115 int vidx = find_vim_var(name, &di_flags);
1116 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001119 if (vidx < 0)
1120 {
1121 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001122 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 return FAIL;
1124 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001125 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001127 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001128}
1129
1130/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001131 * Generate an ISN_UNLET instruction.
1132 */
1133 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001134generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001139 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001140 return FAIL;
1141 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1142 isn->isn_arg.unlet.ul_forceit = forceit;
1143
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001148 * Generate an ISN_LOCKCONST instruction.
1149 */
1150 static int
1151generate_LOCKCONST(cctx_T *cctx)
1152{
1153 isn_T *isn;
1154
1155 RETURN_OK_IF_SKIP(cctx);
1156 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1157 return FAIL;
1158 return OK;
1159}
1160
1161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 * Generate an ISN_LOADS instruction.
1163 */
1164 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001165generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001167 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169 int sid,
1170 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001175 if (isn_type == ISN_LOADS)
1176 isn = generate_instr_type(cctx, isn_type, type);
1177 else
1178 isn = generate_instr_drop(cctx, isn_type, 1);
1179 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001181 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1182 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
1184 return OK;
1185}
1186
1187/*
1188 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1189 */
1190 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 cctx_T *cctx,
1193 isntype_T isn_type,
1194 int sid,
1195 int idx,
1196 type_T *type)
1197{
1198 isn_T *isn;
1199
Bram Moolenaar080457c2020-03-03 21:53:32 +01001200 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 if (isn_type == ISN_LOADSCRIPT)
1202 isn = generate_instr_type(cctx, isn_type, type);
1203 else
1204 isn = generate_instr_drop(cctx, isn_type, 1);
1205 if (isn == NULL)
1206 return FAIL;
1207 isn->isn_arg.script.script_sid = sid;
1208 isn->isn_arg.script.script_idx = idx;
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_NEWLIST instruction.
1214 */
1215 static int
1216generate_NEWLIST(cctx_T *cctx, int count)
1217{
1218 isn_T *isn;
1219 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 type_T *type;
1221 type_T *member;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.number = count;
1227
Bram Moolenaar127542b2020-08-09 17:22:04 +02001228 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001229 if (count == 0)
1230 member = &t_void;
1231 else
1232 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001233 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1234 cctx->ctx_type_list);
1235 type = get_list_type(member, cctx->ctx_type_list);
1236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 // drop the value types
1238 stack->ga_len -= count;
1239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 // add the list type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_NEWDICT instruction.
1251 */
1252 static int
1253generate_NEWDICT(cctx_T *cctx, int count)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 type_T *type;
1258 type_T *member;
1259
Bram Moolenaar080457c2020-03-03 21:53:32 +01001260 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001265 if (count == 0)
1266 member = &t_void;
1267 else
1268 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001269 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1270 cctx->ctx_type_list);
1271 type = get_dict_type(member, cctx->ctx_type_list);
1272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 // drop the key and value types
1274 stack->ga_len -= 2 * count;
1275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 // add the dict type to the type stack
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_FUNCREF instruction.
1287 */
1288 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001289generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1296 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001297 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001298 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001302 ((type_T **)stack->ga_data)[stack->ga_len] =
1303 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001310 * Generate an ISN_NEWFUNC instruction.
1311 */
1312 static int
1313generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1314{
1315 isn_T *isn;
1316 char_u *name;
1317
1318 RETURN_OK_IF_SKIP(cctx);
1319 name = vim_strsave(lambda_name);
1320 if (name == NULL)
1321 return FAIL;
1322 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.newfunc.nf_lambda = name;
1325 isn->isn_arg.newfunc.nf_global = func_name;
1326
1327 return OK;
1328}
1329
1330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 * Generate an ISN_JUMP instruction.
1332 */
1333 static int
1334generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1341 return FAIL;
1342 isn->isn_arg.jump.jump_when = when;
1343 isn->isn_arg.jump.jump_where = where;
1344
1345 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1346 --stack->ga_len;
1347
1348 return OK;
1349}
1350
1351 static int
1352generate_FOR(cctx_T *cctx, int loop_idx)
1353{
1354 isn_T *isn;
1355 garray_T *stack = &cctx->ctx_type_stack;
1356
Bram Moolenaar080457c2020-03-03 21:53:32 +01001357 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1359 return FAIL;
1360 isn->isn_arg.forloop.for_idx = loop_idx;
1361
1362 if (ga_grow(stack, 1) == FAIL)
1363 return FAIL;
1364 // type doesn't matter, will be stored next
1365 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1366 ++stack->ga_len;
1367
1368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001373 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 * Return FAIL if the number of arguments is wrong.
1375 */
1376 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001377generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378{
1379 isn_T *isn;
1380 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001381 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001382 type_T *argtypes[MAX_FUNC_ARGS];
1383 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
Bram Moolenaar080457c2020-03-03 21:53:32 +01001385 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001386 argoff = check_internal_func(func_idx, argcount);
1387 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 return FAIL;
1389
Bram Moolenaar389df252020-07-09 21:20:47 +02001390 if (method_call && argoff > 1)
1391 {
1392 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1393 return FAIL;
1394 isn->isn_arg.shuffle.shfl_item = argcount;
1395 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1396 }
1397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1399 return FAIL;
1400 isn->isn_arg.bfunc.cbf_idx = func_idx;
1401 isn->isn_arg.bfunc.cbf_argcount = argcount;
1402
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001403 for (i = 0; i < argcount; ++i)
1404 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 stack->ga_len -= argcount; // drop the arguments
1407 if (ga_grow(stack, 1) == FAIL)
1408 return FAIL;
1409 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001410 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 ++stack->ga_len; // add return value
1412
1413 return OK;
1414}
1415
1416/*
1417 * Generate an ISN_DCALL or ISN_UCALL instruction.
1418 * Return FAIL if the number of arguments is wrong.
1419 */
1420 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001421generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422{
1423 isn_T *isn;
1424 garray_T *stack = &cctx->ctx_type_stack;
1425 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001426 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if (argcount > regular_args && !has_varargs(ufunc))
1430 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001431 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 return FAIL;
1433 }
1434 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1435 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001436 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 return FAIL;
1438 }
1439
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001440 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001441 {
1442 int i;
1443
1444 for (i = 0; i < argcount; ++i)
1445 {
1446 type_T *expected;
1447 type_T *actual;
1448
1449 if (i < regular_args)
1450 {
1451 if (ufunc->uf_arg_types == NULL)
1452 continue;
1453 expected = ufunc->uf_arg_types[i];
1454 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001455 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1456 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001457 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001458 else
1459 expected = ufunc->uf_va_type->tt_member;
1460 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001461 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001462 {
1463 arg_type_mismatch(expected, actual, i + 1);
1464 return FAIL;
1465 }
1466 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001467 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001468 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1469 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001470 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001471 }
1472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001474 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001475 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001477 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
1479 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1480 isn->isn_arg.dfunc.cdf_argcount = argcount;
1481 }
1482 else
1483 {
1484 // A user function may be deleted and redefined later, can't use the
1485 // ufunc pointer, need to look it up again at runtime.
1486 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1487 isn->isn_arg.ufunc.cuf_argcount = argcount;
1488 }
1489
1490 stack->ga_len -= argcount; // drop the arguments
1491 if (ga_grow(stack, 1) == FAIL)
1492 return FAIL;
1493 // add return value
1494 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1495 ++stack->ga_len;
1496
1497 return OK;
1498}
1499
1500/*
1501 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1502 */
1503 static int
1504generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1505{
1506 isn_T *isn;
1507 garray_T *stack = &cctx->ctx_type_stack;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1513 isn->isn_arg.ufunc.cuf_argcount = argcount;
1514
1515 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
1518 // add return value
1519 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1520 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521
1522 return OK;
1523}
1524
1525/*
1526 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001527 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 */
1529 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001530generate_PCALL(
1531 cctx_T *cctx,
1532 int argcount,
1533 char_u *name,
1534 type_T *type,
1535 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001539 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001542
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001543 if (type->tt_type == VAR_ANY)
1544 ret_type = &t_any;
1545 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001546 {
1547 if (type->tt_argcount != -1)
1548 {
1549 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1550
1551 if (argcount < type->tt_min_argcount - varargs)
1552 {
1553 semsg(_(e_toofewarg), "[reference]");
1554 return FAIL;
1555 }
1556 if (!varargs && argcount > type->tt_argcount)
1557 {
1558 semsg(_(e_toomanyarg), "[reference]");
1559 return FAIL;
1560 }
1561 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001562 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001563 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001564 else
1565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001566 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001567 return FAIL;
1568 }
1569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.pfunc.cpf_top = at_top;
1573 isn->isn_arg.pfunc.cpf_argcount = argcount;
1574
1575 stack->ga_len -= argcount; // drop the arguments
1576
1577 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001578 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001580 // If partial is above the arguments it must be cleared and replaced with
1581 // the return value.
1582 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1583 return FAIL;
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 return OK;
1586}
1587
1588/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001589 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
1591 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001592generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593{
1594 isn_T *isn;
1595 garray_T *stack = &cctx->ctx_type_stack;
1596 type_T *type;
1597
Bram Moolenaar080457c2020-03-03 21:53:32 +01001598 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001599 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001601 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001603 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001605 if (type->tt_type != VAR_DICT && type != &t_any)
1606 {
1607 emsg(_(e_dictreq));
1608 return FAIL;
1609 }
1610 // change dict type to dict member type
1611 if (type->tt_type == VAR_DICT)
1612 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613
1614 return OK;
1615}
1616
1617/*
1618 * Generate an ISN_ECHO instruction.
1619 */
1620 static int
1621generate_ECHO(cctx_T *cctx, int with_white, int count)
1622{
1623 isn_T *isn;
1624
Bram Moolenaar080457c2020-03-03 21:53:32 +01001625 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.echo.echo_with_white = with_white;
1629 isn->isn_arg.echo.echo_count = count;
1630
1631 return OK;
1632}
1633
Bram Moolenaarad39c092020-02-26 18:23:43 +01001634/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636 */
1637 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001638generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001639{
1640 isn_T *isn;
1641
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001642 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001643 return FAIL;
1644 isn->isn_arg.number = count;
1645
1646 return OK;
1647}
1648
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001649/*
1650 * Generate an ISN_PUT instruction.
1651 */
1652 static int
1653generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1654{
1655 isn_T *isn;
1656
1657 RETURN_OK_IF_SKIP(cctx);
1658 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1659 return FAIL;
1660 isn->isn_arg.put.put_regname = regname;
1661 isn->isn_arg.put.put_lnum = lnum;
1662 return OK;
1663}
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 static int
1666generate_EXEC(cctx_T *cctx, char_u *line)
1667{
1668 isn_T *isn;
1669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.string = vim_strsave(line);
1674 return OK;
1675}
1676
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001677 static int
1678generate_EXECCONCAT(cctx_T *cctx, int count)
1679{
1680 isn_T *isn;
1681
1682 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1683 return FAIL;
1684 isn->isn_arg.number = count;
1685 return OK;
1686}
1687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688/*
1689 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001690 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001692 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1694{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 lvar_T *lvar;
1696
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001697 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001699 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001700 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 }
1702
1703 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001704 return NULL;
1705 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001707 // Every local variable uses the next entry on the stack. We could re-use
1708 // the last ones when leaving a scope, but then variables used in a closure
1709 // might get overwritten. To keep things simple do not re-use stack
1710 // entries. This is less efficient, but memory is cheap these days.
1711 lvar->lv_idx = cctx->ctx_locals_count++;
1712
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001713 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 lvar->lv_const = isConst;
1715 lvar->lv_type = type;
1716
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001717 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718}
1719
1720/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001721 * Remove local variables above "new_top".
1722 */
1723 static void
1724unwind_locals(cctx_T *cctx, int new_top)
1725{
1726 if (cctx->ctx_locals.ga_len > new_top)
1727 {
1728 int idx;
1729 lvar_T *lvar;
1730
1731 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1732 {
1733 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1734 vim_free(lvar->lv_name);
1735 }
1736 }
1737 cctx->ctx_locals.ga_len = new_top;
1738}
1739
1740/*
1741 * Free all local variables.
1742 */
1743 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001744free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001745{
1746 unwind_locals(cctx, 0);
1747 ga_clear(&cctx->ctx_locals);
1748}
1749
1750/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 * Find "name" in script-local items of script "sid".
1752 * Returns the index in "sn_var_vals" if found.
1753 * If found but not in "sn_var_vals" returns -1.
1754 * If not found returns -2.
1755 */
1756 int
1757get_script_item_idx(int sid, char_u *name, int check_writable)
1758{
1759 hashtab_T *ht;
1760 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001761 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 int idx;
1763
1764 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001765 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 return -1;
1767 ht = &SCRIPT_VARS(sid);
1768 di = find_var_in_ht(ht, 0, name, TRUE);
1769 if (di == NULL)
1770 return -2;
1771
1772 // Now find the svar_T index in sn_var_vals.
1773 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1774 {
1775 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1776
1777 if (sv->sv_tv == &di->di_tv)
1778 {
1779 if (check_writable && sv->sv_const)
1780 semsg(_(e_readonlyvar), name);
1781 return idx;
1782 }
1783 }
1784 return -1;
1785}
1786
1787/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001788 * Find "name" in imported items of the current script or in "cctx" if not
1789 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 */
1791 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001792find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 int idx;
1795
Bram Moolenaare3d46852020-08-29 13:39:17 +02001796 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001797 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 if (cctx != NULL)
1799 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1800 {
1801 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1802 + idx;
1803
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001804 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1805 : STRLEN(import->imp_name) == len
1806 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 return import;
1808 }
1809
Bram Moolenaarefa94442020-08-08 22:16:00 +02001810 return find_imported_in_script(name, len, current_sctx.sc_sid);
1811}
1812
1813 imported_T *
1814find_imported_in_script(char_u *name, size_t len, int sid)
1815{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001816 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001817 int idx;
1818
Bram Moolenaare3d46852020-08-29 13:39:17 +02001819 if (!SCRIPT_ID_VALID(sid))
1820 return NULL;
1821 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1823 {
1824 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1825
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001826 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1827 : STRLEN(import->imp_name) == len
1828 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return import;
1830 }
1831 return NULL;
1832}
1833
1834/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001835 * Free all imported variables.
1836 */
1837 static void
1838free_imported(cctx_T *cctx)
1839{
1840 int idx;
1841
1842 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1843 {
1844 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1845
1846 vim_free(import->imp_name);
1847 }
1848 ga_clear(&cctx->ctx_imports);
1849}
1850
1851/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852 * Return TRUE if "p" points at a "#" but not at "#{".
1853 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001854 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001855vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001856{
1857 return p[0] == '#' && p[1] != '{';
1858}
1859
1860/*
1861 * Return a pointer to the next line that isn't empty or only contains a
1862 * comment. Skips over white space.
1863 * Returns NULL if there is none.
1864 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001865 char_u *
1866peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001867{
1868 int lnum = cctx->ctx_lnum;
1869
1870 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1871 {
1872 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001873 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001874
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001875 // ignore NULLs inserted for continuation lines
1876 if (line != NULL)
1877 {
1878 p = skipwhite(line);
1879 if (*p != NUL && !vim9_comment_start(p))
1880 return p;
1881 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001882 }
1883 return NULL;
1884}
1885
1886/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001887 * Called when checking for a following operator at "arg". When the rest of
1888 * the line is empty or only a comment, peek the next line. If there is a next
1889 * line return a pointer to it and set "nextp".
1890 * Otherwise skip over white space.
1891 */
1892 static char_u *
1893may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1894{
1895 char_u *p = skipwhite(arg);
1896
1897 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001898 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001899 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001900 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001901 if (*nextp != NULL)
1902 return *nextp;
1903 }
1904 return p;
1905}
1906
1907/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001908 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001909 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001910 * Returns NULL when at the end.
1911 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001912 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001913next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001914{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001915 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001916
1917 do
1918 {
1919 ++cctx->ctx_lnum;
1920 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001921 {
1922 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001923 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001924 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001925 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001926 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001927 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001928 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001929 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001930 return line;
1931}
1932
1933/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001934 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001935 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001936 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1937 */
1938 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001939may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001940{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001941 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001942 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001943 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001944
1945 if (next == NULL)
1946 return FAIL;
1947 *arg = skipwhite(next);
1948 }
1949 return OK;
1950}
1951
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001952/*
1953 * Idem, and give an error when failed.
1954 */
1955 static int
1956may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1957{
1958 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001960 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001961 return FAIL;
1962 }
1963 return OK;
1964}
1965
1966
Bram Moolenaara5565e42020-05-09 15:44:01 +02001967// Structure passed between the compile_expr* functions to keep track of
1968// constants that have been parsed but for which no code was produced yet. If
1969// possible expressions on these constants are applied at compile time. If
1970// that is not possible, the code to push the constants needs to be generated
1971// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001972// Using 50 should be more than enough of 5 levels of ().
1973#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001974typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001975 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001976 int pp_used; // active entries in pp_tv[]
1977} ppconst_T;
1978
Bram Moolenaar1c747212020-05-09 18:28:34 +02001979static int compile_expr0(char_u **arg, cctx_T *cctx);
1980static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1981
Bram Moolenaara5565e42020-05-09 15:44:01 +02001982/*
1983 * Generate a PUSH instruction for "tv".
1984 * "tv" will be consumed or cleared.
1985 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1986 */
1987 static int
1988generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1989{
1990 if (tv != NULL)
1991 {
1992 switch (tv->v_type)
1993 {
1994 case VAR_UNKNOWN:
1995 break;
1996 case VAR_BOOL:
1997 generate_PUSHBOOL(cctx, tv->vval.v_number);
1998 break;
1999 case VAR_SPECIAL:
2000 generate_PUSHSPEC(cctx, tv->vval.v_number);
2001 break;
2002 case VAR_NUMBER:
2003 generate_PUSHNR(cctx, tv->vval.v_number);
2004 break;
2005#ifdef FEAT_FLOAT
2006 case VAR_FLOAT:
2007 generate_PUSHF(cctx, tv->vval.v_float);
2008 break;
2009#endif
2010 case VAR_BLOB:
2011 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2012 tv->vval.v_blob = NULL;
2013 break;
2014 case VAR_STRING:
2015 generate_PUSHS(cctx, tv->vval.v_string);
2016 tv->vval.v_string = NULL;
2017 break;
2018 default:
2019 iemsg("constant type not supported");
2020 clear_tv(tv);
2021 return FAIL;
2022 }
2023 tv->v_type = VAR_UNKNOWN;
2024 }
2025 return OK;
2026}
2027
2028/*
2029 * Generate code for any ppconst entries.
2030 */
2031 static int
2032generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2033{
2034 int i;
2035 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002036 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002037
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002038 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002039 for (i = 0; i < ppconst->pp_used; ++i)
2040 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2041 ret = FAIL;
2042 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002043 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002044 return ret;
2045}
2046
2047/*
2048 * Clear ppconst constants. Used when failing.
2049 */
2050 static void
2051clear_ppconst(ppconst_T *ppconst)
2052{
2053 int i;
2054
2055 for (i = 0; i < ppconst->pp_used; ++i)
2056 clear_tv(&ppconst->pp_tv[i]);
2057 ppconst->pp_used = 0;
2058}
2059
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002060/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002061 * Generate an instruction to load script-local variable "name", without the
2062 * leading "s:".
2063 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 */
2065 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002066compile_load_scriptvar(
2067 cctx_T *cctx,
2068 char_u *name, // variable NUL terminated
2069 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002070 char_u **end, // end of variable
2071 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002073 scriptitem_T *si;
2074 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 imported_T *import;
2076
Bram Moolenaare3d46852020-08-29 13:39:17 +02002077 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2078 return FAIL;
2079 si = SCRIPT_ITEM(current_sctx.sc_sid);
2080 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002081 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002083 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002084 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2085 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 if (idx >= 0)
2088 {
2089 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2090
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002091 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 current_sctx.sc_sid, idx, sv->sv_type);
2093 return OK;
2094 }
2095
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002096 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 if (import != NULL)
2098 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002099 if (import->imp_all)
2100 {
2101 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002102 char_u *exp_name;
2103 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002104 ufunc_T *ufunc;
2105 type_T *type;
2106
2107 // Used "import * as Name", need to lookup the member.
2108 if (*p != '.')
2109 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002110 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002111 return FAIL;
2112 }
2113 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002114 if (VIM_ISWHITE(*p))
2115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002117 return FAIL;
2118 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002119
Bram Moolenaar1c991142020-07-04 13:15:31 +02002120 // isolate one name
2121 exp_name = p;
2122 while (eval_isnamec(*p))
2123 ++p;
2124 cc = *p;
2125 *p = NUL;
2126
2127 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2128 *p = cc;
2129 p = skipwhite(p);
2130
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002131 // TODO: what if it is a function?
2132 if (idx < 0)
2133 return FAIL;
2134 *end = p;
2135
2136 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2137 import->imp_sid,
2138 idx,
2139 type);
2140 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002141 else if (import->imp_funcname != NULL)
2142 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002143 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002144 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2145 import->imp_sid,
2146 import->imp_var_vals_idx,
2147 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 return OK;
2149 }
2150
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002151 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002152 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 return FAIL;
2154}
2155
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002156 static int
2157generate_funcref(cctx_T *cctx, char_u *name)
2158{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002159 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002160
2161 if (ufunc == NULL)
2162 return FAIL;
2163
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002164 // Need to compile any default values to get the argument types.
2165 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2166 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2167 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002168 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002169}
2170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171/*
2172 * Compile a variable name into a load instruction.
2173 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002174 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 * When "error" is FALSE do not give an error when not found.
2176 */
2177 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002178compile_load(
2179 char_u **arg,
2180 char_u *end_arg,
2181 cctx_T *cctx,
2182 int is_expr,
2183 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184{
2185 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002186 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002187 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002189 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190
2191 if (*(*arg + 1) == ':')
2192 {
2193 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002194 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002195 {
2196 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002198 switch (**arg)
2199 {
2200 case 'g': isn_type = ISN_LOADGDICT; break;
2201 case 'w': isn_type = ISN_LOADWDICT; break;
2202 case 't': isn_type = ISN_LOADTDICT; break;
2203 case 'b': isn_type = ISN_LOADBDICT; break;
2204 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002205 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002206 goto theend;
2207 }
2208 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2209 goto theend;
2210 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 else
2213 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002214 isntype_T isn_type = ISN_DROP;
2215
2216 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2217 if (name == NULL)
2218 return FAIL;
2219
2220 switch (**arg)
2221 {
2222 case 'v': res = generate_LOADV(cctx, name, error);
2223 break;
2224 case 's': res = compile_load_scriptvar(cctx, name,
2225 NULL, NULL, error);
2226 break;
2227 case 'g': isn_type = ISN_LOADG; break;
2228 case 'w': isn_type = ISN_LOADW; break;
2229 case 't': isn_type = ISN_LOADT; break;
2230 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002231 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002232 goto theend;
2233 }
2234 if (isn_type != ISN_DROP)
2235 {
2236 // Global, Buffer-local, Window-local and Tabpage-local
2237 // variables can be defined later, thus we don't check if it
2238 // exists, give error at runtime.
2239 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 }
2243 else
2244 {
2245 size_t len = end - *arg;
2246 int idx;
2247 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002248 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249
2250 name = vim_strnsave(*arg, end - *arg);
2251 if (name == NULL)
2252 return FAIL;
2253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002254 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002256 if (!gen_load_outer)
2257 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 }
2259 else
2260 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002261 lvar_T *lvar = lookup_local(*arg, len, cctx);
2262
2263 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002265 type = lvar->lv_type;
2266 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002267 if (lvar->lv_from_outer)
2268 gen_load_outer = TRUE;
2269 else
2270 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 }
2272 else
2273 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002274 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002275 // already exists in a Vim9 script or when it's imported.
2276 if (lookup_script(*arg, len, TRUE) == OK
2277 || find_imported(name, 0, cctx) != NULL)
2278 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002279
Bram Moolenaar0f769812020-09-12 18:32:34 +02002280 // When evaluating an expression and the name starts with an
2281 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002282 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002283 if (res == FAIL && is_expr
2284 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002285 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 }
2287 }
2288 if (gen_load)
2289 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002290 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002291 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002292 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002293 cctx->ctx_outer_used = TRUE;
2294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 }
2296
2297 *arg = end;
2298
2299theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002300 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002301 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 vim_free(name);
2303 return res;
2304}
2305
2306/*
2307 * Compile the argument expressions.
2308 * "arg" points to just after the "(" and is advanced to after the ")"
2309 */
2310 static int
2311compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2312{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002313 char_u *p = *arg;
2314 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002315 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316
Bram Moolenaare6085c52020-04-12 20:19:16 +02002317 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002319 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2320 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002321 if (*p == ')')
2322 {
2323 *arg = p + 1;
2324 return OK;
2325 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002326 if (must_end)
2327 {
2328 semsg(_(e_missing_comma_before_argument_str), p);
2329 return FAIL;
2330 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002331
Bram Moolenaara5565e42020-05-09 15:44:01 +02002332 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 return FAIL;
2334 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002335
2336 if (*p != ',' && *skipwhite(p) == ',')
2337 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002338 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002339 p = skipwhite(p);
2340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002342 {
2343 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002344 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002345 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002346 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002347 else
2348 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002349 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002350 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002352failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002353 emsg(_(e_missing_close));
2354 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355}
2356
2357/*
2358 * Compile a function call: name(arg1, arg2)
2359 * "arg" points to "name", "arg + varlen" to the "(".
2360 * "argcount_init" is 1 for "value->method()"
2361 * Instructions:
2362 * EVAL arg1
2363 * EVAL arg2
2364 * BCALL / DCALL / UCALL
2365 */
2366 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002367compile_call(
2368 char_u **arg,
2369 size_t varlen,
2370 cctx_T *cctx,
2371 ppconst_T *ppconst,
2372 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373{
2374 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002375 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 int argcount = argcount_init;
2377 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002378 char_u fname_buf[FLEN_FIXED + 1];
2379 char_u *tofree = NULL;
2380 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002382 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002383 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
Bram Moolenaara5565e42020-05-09 15:44:01 +02002385 // we can evaluate "has('name')" at compile time
2386 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2387 {
2388 char_u *s = skipwhite(*arg + varlen + 1);
2389 typval_T argvars[2];
2390
2391 argvars[0].v_type = VAR_UNKNOWN;
2392 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002393 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002394 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002395 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002396 s = skipwhite(s);
2397 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2398 {
2399 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2400
2401 *arg = s + 1;
2402 argvars[1].v_type = VAR_UNKNOWN;
2403 tv->v_type = VAR_NUMBER;
2404 tv->vval.v_number = 0;
2405 f_has(argvars, tv);
2406 clear_tv(&argvars[0]);
2407 ++ppconst->pp_used;
2408 return OK;
2409 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002410 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002411 }
2412
2413 if (generate_ppconst(cctx, ppconst) == FAIL)
2414 return FAIL;
2415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (varlen >= sizeof(namebuf))
2417 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002418 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 return FAIL;
2420 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002421 vim_strncpy(namebuf, *arg, varlen);
2422 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 *arg = skipwhite(*arg + varlen + 1);
2425 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002426 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427
Bram Moolenaara1773442020-08-12 15:21:22 +02002428 is_autoload = vim_strchr(name, '#') != NULL;
2429 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 {
2431 int idx;
2432
2433 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002434 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002436 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002437 else
2438 semsg(_(e_unknownfunc), namebuf);
2439 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 }
2441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002443 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002444 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002445 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002446 {
2447 res = generate_CALL(cctx, ufunc, argcount);
2448 goto theend;
2449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
2451 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002452 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002453 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002455 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002456 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002457 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002458 garray_T *stack = &cctx->ctx_type_stack;
2459 type_T *type;
2460
2461 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2462 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002463 goto theend;
2464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465
Bram Moolenaar0f769812020-09-12 18:32:34 +02002466 // If we can find a global function by name generate the right call.
2467 if (ufunc != NULL)
2468 {
2469 res = generate_CALL(cctx, ufunc, argcount);
2470 goto theend;
2471 }
2472
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002473 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002474 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002475 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002476 res = generate_UCALL(cctx, name, argcount);
2477 else
2478 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002479
2480theend:
2481 vim_free(tofree);
2482 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483}
2484
2485// like NAMESPACE_CHAR but with 'a' and 'l'.
2486#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2487
2488/*
2489 * Find the end of a variable or function name. Unlike find_name_end() this
2490 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002491 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 * Return a pointer to just after the name. Equal to "arg" if there is no
2493 * valid name.
2494 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002495 static char_u *
2496to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497{
2498 char_u *p;
2499
2500 // Quick check for valid starting character.
2501 if (!eval_isnamec1(*arg))
2502 return arg;
2503
2504 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2505 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2506 // and can be used in slice "[n:]".
2507 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002508 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2510 break;
2511 return p;
2512}
2513
2514/*
2515 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002516 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 */
2518 char_u *
2519to_name_const_end(char_u *arg)
2520{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002521 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 typval_T rettv;
2523
2524 if (p == arg && *arg == '[')
2525 {
2526
2527 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002528 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 p = arg;
2530 }
2531 else if (p == arg && *arg == '#' && arg[1] == '{')
2532 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002533 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002535 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 p = arg;
2537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
2539 return p;
2540}
2541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542/*
2543 * parse a list: [expr, expr]
2544 * "*arg" points to the '['.
2545 */
2546 static int
2547compile_list(char_u **arg, cctx_T *cctx)
2548{
2549 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002550 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 int count = 0;
2552
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002553 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002556 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002557 semsg(_(e_list_end), *arg);
2558 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002559 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002560 if (*p == ',')
2561 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002562 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002563 return FAIL;
2564 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002565 if (*p == ']')
2566 {
2567 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002568 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002569 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002570 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002571 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 ++count;
2573 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002574 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002576 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2577 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002578 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002579 return FAIL;
2580 }
2581 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002582 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 p = skipwhite(p);
2584 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002585 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
2587 generate_NEWLIST(cctx, count);
2588 return OK;
2589}
2590
2591/*
2592 * parse a lambda: {arg, arg -> expr}
2593 * "*arg" points to the '{'.
2594 */
2595 static int
2596compile_lambda(char_u **arg, cctx_T *cctx)
2597{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 typval_T rettv;
2599 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002600 evalarg_T evalarg;
2601
2602 CLEAR_FIELD(evalarg);
2603 evalarg.eval_flags = EVAL_EVALUATE;
2604 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
2606 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002607 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002611 ++ufunc->uf_refcount;
2612 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614
2615 // The function will have one line: "return {expr}".
2616 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002617 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002619 clear_evalarg(&evalarg, NULL);
2620
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002621 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002622 {
2623 // The return type will now be known.
2624 set_function_type(ufunc);
2625
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002626 // The function reference count will be 1. When the ISN_FUNCREF
2627 // instruction is deleted the reference count is decremented and the
2628 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002629 return generate_FUNCREF(cctx, ufunc);
2630 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002631
2632 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 return FAIL;
2634}
2635
2636/*
2637 * Compile a lamda call: expr->{lambda}(args)
2638 * "arg" points to the "{".
2639 */
2640 static int
2641compile_lambda_call(char_u **arg, cctx_T *cctx)
2642{
2643 ufunc_T *ufunc;
2644 typval_T rettv;
2645 int argcount = 1;
2646 int ret = FAIL;
2647
2648 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002649 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 return FAIL;
2651
2652 if (**arg != '(')
2653 {
2654 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002655 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 else
2657 semsg(_(e_missing_paren), "lambda");
2658 clear_tv(&rettv);
2659 return FAIL;
2660 }
2661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 ufunc = rettv.vval.v_partial->pt_func;
2663 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002664 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002665 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002666
Bram Moolenaara05e5242020-09-19 18:19:19 +02002667 // The function will have one line: "return {expr}". Compile it into
2668 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002669 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670
2671 // compile the arguments
2672 *arg = skipwhite(*arg + 1);
2673 if (compile_arguments(arg, cctx, &argcount) == OK)
2674 // call the compiled function
2675 ret = generate_CALL(cctx, ufunc, argcount);
2676
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002677 if (ret == FAIL)
2678 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 return ret;
2680}
2681
2682/*
2683 * parse a dict: {'key': val} or #{key: val}
2684 * "*arg" points to the '{'.
2685 */
2686 static int
2687compile_dict(char_u **arg, cctx_T *cctx, int literal)
2688{
2689 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002690 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 int count = 0;
2692 dict_T *d = dict_alloc();
2693 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002694 char_u *whitep = *arg;
2695 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696
2697 if (d == NULL)
2698 return FAIL;
2699 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002700 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 {
2702 char_u *key = NULL;
2703
Bram Moolenaar23c55272020-06-21 16:58:13 +02002704 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002706 *arg = NULL;
2707 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002708 }
2709
2710 if (**arg == '}')
2711 break;
2712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 if (literal)
2714 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002715 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
Bram Moolenaar2c330432020-04-13 14:41:35 +02002717 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002719 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 return FAIL;
2721 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002722 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 if (generate_PUSHS(cctx, key) == FAIL)
2724 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002725 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 }
2727 else
2728 {
2729 isn_T *isn;
2730
Bram Moolenaara5565e42020-05-09 15:44:01 +02002731 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2734 if (isn->isn_type == ISN_PUSHS)
2735 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002736 else
2737 {
2738 type_T *keytype = ((type_T **)stack->ga_data)
2739 [stack->ga_len - 1];
2740 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2741 return FAIL;
2742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 }
2744
2745 // Check for duplicate keys, if using string keys.
2746 if (key != NULL)
2747 {
2748 item = dict_find(d, key, -1);
2749 if (item != NULL)
2750 {
2751 semsg(_(e_duplicate_key), key);
2752 goto failret;
2753 }
2754 item = dictitem_alloc(key);
2755 if (item != NULL)
2756 {
2757 item->di_tv.v_type = VAR_UNKNOWN;
2758 item->di_tv.v_lock = 0;
2759 if (dict_add(d, item) == FAIL)
2760 dictitem_free(item);
2761 }
2762 }
2763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 if (**arg != ':')
2765 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002766 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002767 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002768 else
2769 semsg(_(e_missing_dict_colon), *arg);
2770 return FAIL;
2771 }
2772 whitep = *arg + 1;
2773 if (!IS_WHITE_OR_NUL(*whitep))
2774 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002775 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 return FAIL;
2777 }
2778
2779 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002780 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002781 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002782 *arg = NULL;
2783 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002784 }
2785
Bram Moolenaara5565e42020-05-09 15:44:01 +02002786 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 return FAIL;
2788 ++count;
2789
Bram Moolenaar2c330432020-04-13 14:41:35 +02002790 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002791 *arg = skipwhite(*arg);
2792 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002793 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002794 *arg = NULL;
2795 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 if (**arg == '}')
2798 break;
2799 if (**arg != ',')
2800 {
2801 semsg(_(e_missing_dict_comma), *arg);
2802 goto failret;
2803 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002804 if (IS_WHITE_OR_NUL(*whitep))
2805 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002806 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002807 return FAIL;
2808 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002809 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 *arg = skipwhite(*arg + 1);
2811 }
2812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 *arg = *arg + 1;
2814
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002815 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002816 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002817 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002818 *arg += STRLEN(*arg);
2819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 dict_unref(d);
2821 return generate_NEWDICT(cctx, count);
2822
2823failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002824 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002825 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002826 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002827 *arg = (char_u *)"";
2828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 dict_unref(d);
2830 return FAIL;
2831}
2832
2833/*
2834 * Compile "&option".
2835 */
2836 static int
2837compile_get_option(char_u **arg, cctx_T *cctx)
2838{
2839 typval_T rettv;
2840 char_u *start = *arg;
2841 int ret;
2842
2843 // parse the option and get the current value to get the type.
2844 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002845 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 if (ret == OK)
2847 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002848 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 char_u *name = vim_strnsave(start, *arg - start);
2850 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2851
2852 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2853 vim_free(name);
2854 }
2855 clear_tv(&rettv);
2856
2857 return ret;
2858}
2859
2860/*
2861 * Compile "$VAR".
2862 */
2863 static int
2864compile_get_env(char_u **arg, cctx_T *cctx)
2865{
2866 char_u *start = *arg;
2867 int len;
2868 int ret;
2869 char_u *name;
2870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 ++*arg;
2872 len = get_env_len(arg);
2873 if (len == 0)
2874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002875 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 return FAIL;
2877 }
2878
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002879 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 name = vim_strnsave(start, len + 1);
2881 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2882 vim_free(name);
2883 return ret;
2884}
2885
2886/*
2887 * Compile "@r".
2888 */
2889 static int
2890compile_get_register(char_u **arg, cctx_T *cctx)
2891{
2892 int ret;
2893
2894 ++*arg;
2895 if (**arg == NUL)
2896 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002897 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 return FAIL;
2899 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002900 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 {
2902 emsg_invreg(**arg);
2903 return FAIL;
2904 }
2905 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2906 ++*arg;
2907 return ret;
2908}
2909
2910/*
2911 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002912 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 */
2914 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002915apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002917 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918
2919 // this works from end to start
2920 while (p > start)
2921 {
2922 --p;
2923 if (*p == '-' || *p == '+')
2924 {
2925 // only '-' has an effect, for '+' we only check the type
2926#ifdef FEAT_FLOAT
2927 if (rettv->v_type == VAR_FLOAT)
2928 {
2929 if (*p == '-')
2930 rettv->vval.v_float = -rettv->vval.v_float;
2931 }
2932 else
2933#endif
2934 {
2935 varnumber_T val;
2936 int error = FALSE;
2937
2938 // tv_get_number_chk() accepts a string, but we don't want that
2939 // here
2940 if (check_not_string(rettv) == FAIL)
2941 return FAIL;
2942 val = tv_get_number_chk(rettv, &error);
2943 clear_tv(rettv);
2944 if (error)
2945 return FAIL;
2946 if (*p == '-')
2947 val = -val;
2948 rettv->v_type = VAR_NUMBER;
2949 rettv->vval.v_number = val;
2950 }
2951 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002952 else if (numeric_only)
2953 {
2954 ++p;
2955 break;
2956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 else
2958 {
2959 int v = tv2bool(rettv);
2960
2961 // '!' is permissive in the type.
2962 clear_tv(rettv);
2963 rettv->v_type = VAR_BOOL;
2964 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2965 }
2966 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002967 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return OK;
2969}
2970
2971/*
2972 * Recognize v: variables that are constants and set "rettv".
2973 */
2974 static void
2975get_vim_constant(char_u **arg, typval_T *rettv)
2976{
2977 if (STRNCMP(*arg, "v:true", 6) == 0)
2978 {
2979 rettv->v_type = VAR_BOOL;
2980 rettv->vval.v_number = VVAL_TRUE;
2981 *arg += 6;
2982 }
2983 else if (STRNCMP(*arg, "v:false", 7) == 0)
2984 {
2985 rettv->v_type = VAR_BOOL;
2986 rettv->vval.v_number = VVAL_FALSE;
2987 *arg += 7;
2988 }
2989 else if (STRNCMP(*arg, "v:null", 6) == 0)
2990 {
2991 rettv->v_type = VAR_SPECIAL;
2992 rettv->vval.v_number = VVAL_NULL;
2993 *arg += 6;
2994 }
2995 else if (STRNCMP(*arg, "v:none", 6) == 0)
2996 {
2997 rettv->v_type = VAR_SPECIAL;
2998 rettv->vval.v_number = VVAL_NONE;
2999 *arg += 6;
3000 }
3001}
3002
Bram Moolenaar696ba232020-07-29 21:20:41 +02003003 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003004get_compare_type(char_u *p, int *len, int *type_is)
3005{
3006 exptype_T type = EXPR_UNKNOWN;
3007 int i;
3008
3009 switch (p[0])
3010 {
3011 case '=': if (p[1] == '=')
3012 type = EXPR_EQUAL;
3013 else if (p[1] == '~')
3014 type = EXPR_MATCH;
3015 break;
3016 case '!': if (p[1] == '=')
3017 type = EXPR_NEQUAL;
3018 else if (p[1] == '~')
3019 type = EXPR_NOMATCH;
3020 break;
3021 case '>': if (p[1] != '=')
3022 {
3023 type = EXPR_GREATER;
3024 *len = 1;
3025 }
3026 else
3027 type = EXPR_GEQUAL;
3028 break;
3029 case '<': if (p[1] != '=')
3030 {
3031 type = EXPR_SMALLER;
3032 *len = 1;
3033 }
3034 else
3035 type = EXPR_SEQUAL;
3036 break;
3037 case 'i': if (p[1] == 's')
3038 {
3039 // "is" and "isnot"; but not a prefix of a name
3040 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3041 *len = 5;
3042 i = p[*len];
3043 if (!isalnum(i) && i != '_')
3044 {
3045 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3046 *type_is = TRUE;
3047 }
3048 }
3049 break;
3050 }
3051 return type;
3052}
3053
3054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003056 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 */
3058 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003059compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003061 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062
3063 // this works from end to start
3064 while (p > start)
3065 {
3066 --p;
3067 if (*p == '-' || *p == '+')
3068 {
3069 int negate = *p == '-';
3070 isn_T *isn;
3071
3072 // TODO: check type
3073 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3074 {
3075 --p;
3076 if (*p == '-')
3077 negate = !negate;
3078 }
3079 // only '-' has an effect, for '+' we only check the type
3080 if (negate)
3081 isn = generate_instr(cctx, ISN_NEGATENR);
3082 else
3083 isn = generate_instr(cctx, ISN_CHECKNR);
3084 if (isn == NULL)
3085 return FAIL;
3086 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003087 else if (numeric_only)
3088 {
3089 ++p;
3090 break;
3091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 else
3093 {
3094 int invert = TRUE;
3095
3096 while (p > start && p[-1] == '!')
3097 {
3098 --p;
3099 invert = !invert;
3100 }
3101 if (generate_2BOOL(cctx, invert) == FAIL)
3102 return FAIL;
3103 }
3104 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003105 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 return OK;
3107}
3108
3109/*
3110 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003111 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 */
3113 static int
3114compile_subscript(
3115 char_u **arg,
3116 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003117 char_u *start_leader,
3118 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003119 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003121 char_u *name_start = *end_leader;
3122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 for (;;)
3124 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003125 char_u *p = skipwhite(*arg);
3126
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003127 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003128 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003129 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003130
3131 // If a following line starts with "->{" or "->X" advance to that
3132 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003133 // Also if a following line starts with ".x".
3134 if (next != NULL &&
3135 ((next[0] == '-' && next[1] == '>'
3136 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003137 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003138 {
3139 next = next_line_from_context(cctx, TRUE);
3140 if (next == NULL)
3141 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003142 *arg = next;
3143 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003144 }
3145 }
3146
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003147 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3148 // not a function call.
3149 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003151 garray_T *stack = &cctx->ctx_type_stack;
3152 type_T *type;
3153 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003155 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003156 return FAIL;
3157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003159 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3160
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003161 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3163 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003164 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 return FAIL;
3166 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003167 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003169 char_u *pstart = p;
3170
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003171 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003172 return FAIL;
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 // something->method()
3175 // Apply the '!', '-' and '+' first:
3176 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003177 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003180 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003181 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003182 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 if (**arg == '{')
3184 {
3185 // lambda call: list->{lambda}
3186 if (compile_lambda_call(arg, cctx) == FAIL)
3187 return FAIL;
3188 }
3189 else
3190 {
3191 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003192 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003193 if (!eval_isnamec1(*p))
3194 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003195 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003196 return FAIL;
3197 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003198 if (ASCII_ISALPHA(*p) && p[1] == ':')
3199 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003200 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 ;
3202 if (*p != '(')
3203 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003204 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return FAIL;
3206 }
3207 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003208 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 return FAIL;
3210 }
3211 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003212 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003214 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003215 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003216 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003217 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003218 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003219
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003220 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003221 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003222 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003223 // TODO: blob index
3224 // TODO: more arguments
3225 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003226 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003227 return FAIL;
3228
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003229 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003230 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003231 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003232 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003233 if (**arg == ':')
3234 // missing first index is equal to zero
3235 generate_PUSHNR(cctx, 0);
3236 else
3237 {
3238 if (compile_expr0(arg, cctx) == FAIL)
3239 return FAIL;
3240 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3241 return FAIL;
3242 *arg = skipwhite(*arg);
3243 }
3244 if (**arg == ':')
3245 {
3246 *arg = skipwhite(*arg + 1);
3247 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3248 return FAIL;
3249 if (**arg == ']')
3250 // missing second index is equal to end of string
3251 generate_PUSHNR(cctx, -1);
3252 else
3253 {
3254 if (compile_expr0(arg, cctx) == FAIL)
3255 return FAIL;
3256 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3257 return FAIL;
3258 *arg = skipwhite(*arg);
3259 }
3260 is_slice = TRUE;
3261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
3263 if (**arg != ']')
3264 {
3265 emsg(_(e_missbrac));
3266 return FAIL;
3267 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003268 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003270 // We can index a list and a dict. If we don't know the type
3271 // we can use the index value type.
3272 // TODO: If we don't know use an instruction to figure it out at
3273 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003274 typep = ((type_T **)stack->ga_data) + stack->ga_len
3275 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003276 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003277 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3278 // If the index is a string, the variable must be a Dict.
3279 if (*typep == &t_any && valtype == &t_string)
3280 vtype = VAR_DICT;
3281 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003282 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003283 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3284 return FAIL;
3285 if (is_slice)
3286 {
3287 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3288 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3289 return FAIL;
3290 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003291 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003292
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003293 if (vtype == VAR_DICT)
3294 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003295 if (is_slice)
3296 {
3297 emsg(_(e_cannot_slice_dictionary));
3298 return FAIL;
3299 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003300 if ((*typep)->tt_type == VAR_DICT)
3301 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003302 else
3303 {
3304 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3305 return FAIL;
3306 *typep = &t_any;
3307 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003308 if (may_generate_2STRING(-1, cctx) == FAIL)
3309 return FAIL;
3310 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3311 return FAIL;
3312 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003313 else if (vtype == VAR_STRING)
3314 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003315 *typep = &t_string;
3316 if ((is_slice
3317 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3318 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003319 return FAIL;
3320 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003321 else if (vtype == VAR_BLOB)
3322 {
3323 emsg("Sorry, blob index and slice not implemented yet");
3324 return FAIL;
3325 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003326 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003327 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003328 if (is_slice)
3329 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003330 if (generate_instr_drop(cctx,
3331 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3332 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003333 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003334 }
Bram Moolenaared591872020-08-15 22:14:53 +02003335 else
3336 {
3337 if ((*typep)->tt_type == VAR_LIST)
3338 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003339 if (generate_instr_drop(cctx,
3340 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3341 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003342 return FAIL;
3343 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003344 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003345 else
3346 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003347 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003348 return FAIL;
3349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003351 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003353 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003354 return FAIL;
3355
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003356 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003357 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003358 {
3359 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003360 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003363 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003364 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 while (eval_isnamec(*p))
3366 MB_PTR_ADV(p);
3367 if (p == *arg)
3368 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003369 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 return FAIL;
3371 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003372 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 return FAIL;
3374 *arg = p;
3375 }
3376 else
3377 break;
3378 }
3379
3380 // TODO - see handle_subscript():
3381 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3382 // Don't do this when "Func" is already a partial that was bound
3383 // explicitly (pt_auto is FALSE).
3384
3385 return OK;
3386}
3387
3388/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003389 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3390 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003392 * If the value is a constant "ppconst->pp_ret" will be set.
3393 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003394 *
3395 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 */
3397
3398/*
3399 * number number constant
3400 * 0zFFFFFFFF Blob constant
3401 * "string" string constant
3402 * 'string' literal string constant
3403 * &option-name option value
3404 * @r register contents
3405 * identifier variable value
3406 * function() function call
3407 * $VAR environment variable
3408 * (expression) nested expression
3409 * [expr, expr] List
3410 * {key: val, key: val} Dictionary
3411 * #{key: val, key: val} Dictionary with literal keys
3412 *
3413 * Also handle:
3414 * ! in front logical NOT
3415 * - in front unary minus
3416 * + in front unary plus (ignored)
3417 * trailing (arg) funcref/partial call
3418 * trailing [] subscript in String or List
3419 * trailing .name entry in Dictionary
3420 * trailing ->name() method call
3421 */
3422 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003423compile_expr7(
3424 char_u **arg,
3425 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003426 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 char_u *start_leader, *end_leader;
3429 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003430 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003431 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432
3433 /*
3434 * Skip '!', '-' and '+' characters. They are handled later.
3435 */
3436 start_leader = *arg;
3437 while (**arg == '!' || **arg == '-' || **arg == '+')
3438 *arg = skipwhite(*arg + 1);
3439 end_leader = *arg;
3440
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003441 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 switch (**arg)
3443 {
3444 /*
3445 * Number constant.
3446 */
3447 case '0': // also for blob starting with 0z
3448 case '1':
3449 case '2':
3450 case '3':
3451 case '4':
3452 case '5':
3453 case '6':
3454 case '7':
3455 case '8':
3456 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003457 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003459 // Apply "-" and "+" just before the number now, right to
3460 // left. Matters especially when "->" follows. Stops at
3461 // '!'.
3462 if (apply_leader(rettv, TRUE,
3463 start_leader, &end_leader) == FAIL)
3464 {
3465 clear_tv(rettv);
3466 return FAIL;
3467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 break;
3469
3470 /*
3471 * String constant: "string".
3472 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003473 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 return FAIL;
3475 break;
3476
3477 /*
3478 * Literal string constant: 'str''ing'.
3479 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return FAIL;
3482 break;
3483
3484 /*
3485 * Constant Vim variable.
3486 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003487 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 ret = NOTDONE;
3489 break;
3490
3491 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003492 * "true" constant
3493 */
3494 case 't': if (STRNCMP(*arg, "true", 4) == 0
3495 && !eval_isnamec((*arg)[4]))
3496 {
3497 *arg += 4;
3498 rettv->v_type = VAR_BOOL;
3499 rettv->vval.v_number = VVAL_TRUE;
3500 }
3501 else
3502 ret = NOTDONE;
3503 break;
3504
3505 /*
3506 * "false" constant
3507 */
3508 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3509 && !eval_isnamec((*arg)[5]))
3510 {
3511 *arg += 5;
3512 rettv->v_type = VAR_BOOL;
3513 rettv->vval.v_number = VVAL_FALSE;
3514 }
3515 else
3516 ret = NOTDONE;
3517 break;
3518
3519 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 * List: [expr, expr]
3521 */
3522 case '[': ret = compile_list(arg, cctx);
3523 break;
3524
3525 /*
3526 * Dictionary: #{key: val, key: val}
3527 */
3528 case '#': if ((*arg)[1] == '{')
3529 {
3530 ++*arg;
3531 ret = compile_dict(arg, cctx, TRUE);
3532 }
3533 else
3534 ret = NOTDONE;
3535 break;
3536
3537 /*
3538 * Lambda: {arg, arg -> expr}
3539 * Dictionary: {'key': val, 'key': val}
3540 */
3541 case '{': {
3542 char_u *start = skipwhite(*arg + 1);
3543
3544 // Find out what comes after the arguments.
3545 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003546 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (ret != FAIL && *start == '>')
3548 ret = compile_lambda(arg, cctx);
3549 else
3550 ret = compile_dict(arg, cctx, FALSE);
3551 }
3552 break;
3553
3554 /*
3555 * Option value: &name
3556 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003557 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3558 return FAIL;
3559 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 break;
3561
3562 /*
3563 * Environment variable: $VAR.
3564 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003565 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3566 return FAIL;
3567 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 break;
3569
3570 /*
3571 * Register contents: @r.
3572 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003573 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3574 return FAIL;
3575 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 break;
3577 /*
3578 * nested expression: (expression).
3579 */
3580 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003581
3582 // recursive!
3583 if (ppconst->pp_used <= PPSIZE - 10)
3584 {
3585 ret = compile_expr1(arg, cctx, ppconst);
3586 }
3587 else
3588 {
3589 // Not enough space in ppconst, flush constants.
3590 if (generate_ppconst(cctx, ppconst) == FAIL)
3591 return FAIL;
3592 ret = compile_expr0(arg, cctx);
3593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 *arg = skipwhite(*arg);
3595 if (**arg == ')')
3596 ++*arg;
3597 else if (ret == OK)
3598 {
3599 emsg(_(e_missing_close));
3600 ret = FAIL;
3601 }
3602 break;
3603
3604 default: ret = NOTDONE;
3605 break;
3606 }
3607 if (ret == FAIL)
3608 return FAIL;
3609
Bram Moolenaar1c747212020-05-09 18:28:34 +02003610 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003612 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003613 clear_tv(rettv);
3614 else
3615 // A constant expression can possibly be handled compile time,
3616 // return the value instead of generating code.
3617 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 }
3619 else if (ret == NOTDONE)
3620 {
3621 char_u *p;
3622 int r;
3623
3624 if (!eval_isnamec1(**arg))
3625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003626 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 return FAIL;
3628 }
3629
3630 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003631 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003633 {
3634 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003637 {
3638 if (generate_ppconst(cctx, ppconst) == FAIL)
3639 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003640 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 if (r == FAIL)
3643 return FAIL;
3644 }
3645
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003646 // Handle following "[]", ".member", etc.
3647 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003648 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003649 ppconst) == FAIL)
3650 return FAIL;
3651 if (ppconst->pp_used > 0)
3652 {
3653 // apply the '!', '-' and '+' before the constant
3654 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003655 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003656 return FAIL;
3657 return OK;
3658 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003659 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003661 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662}
3663
3664/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003665 * Give the "white on both sides" error, taking the operator from "p[len]".
3666 */
3667 void
3668error_white_both(char_u *op, int len)
3669{
3670 char_u buf[10];
3671
3672 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003673 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003674}
3675
3676/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003677 * <type>expr7: runtime type check / conversion
3678 */
3679 static int
3680compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3681{
3682 type_T *want_type = NULL;
3683
3684 // Recognize <type>
3685 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3686 {
3687 int called_emsg_before = called_emsg;
3688
3689 ++*arg;
3690 want_type = parse_type(arg, cctx->ctx_type_list);
3691 if (called_emsg != called_emsg_before)
3692 return FAIL;
3693
3694 if (**arg != '>')
3695 {
3696 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003697 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003698 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003699 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003700 return FAIL;
3701 }
3702 ++*arg;
3703 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3704 return FAIL;
3705 }
3706
3707 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3708 return FAIL;
3709
3710 if (want_type != NULL)
3711 {
3712 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003713 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003714
Bram Moolenaard1103582020-08-14 22:44:25 +02003715 generate_ppconst(cctx, ppconst);
3716 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003717 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003718 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003719 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3720 return FAIL;
3721 }
3722 }
3723
3724 return OK;
3725}
3726
3727/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 * * number multiplication
3729 * / number division
3730 * % number modulo
3731 */
3732 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003733compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734{
3735 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003736 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003737 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003739 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003740 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 return FAIL;
3742
3743 /*
3744 * Repeat computing, until no "*", "/" or "%" is following.
3745 */
3746 for (;;)
3747 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003748 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 if (*op != '*' && *op != '/' && *op != '%')
3750 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003751 if (next != NULL)
3752 {
3753 *arg = next_line_from_context(cctx, TRUE);
3754 op = skipwhite(*arg);
3755 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003756
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003757 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003759 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003760 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 }
3762 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003763 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003764 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003766 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003767 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003769
3770 if (ppconst->pp_used == ppconst_used + 2
3771 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3772 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003773 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003774 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3775 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003776 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003778 // both are numbers: compute the result
3779 switch (*op)
3780 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003781 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003782 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003783 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003784 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003785 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003786 break;
3787 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003788 tv1->vval.v_number = res;
3789 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 }
3791 else
3792 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003793 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 generate_two_op(cctx, op);
3795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
3797
3798 return OK;
3799}
3800
3801/*
3802 * + number addition
3803 * - number subtraction
3804 * .. string concatenation
3805 */
3806 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003807compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808{
3809 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003810 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003812 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
3814 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003815 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 return FAIL;
3817
3818 /*
3819 * Repeat computing, until no "+", "-" or ".." is following.
3820 */
3821 for (;;)
3822 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003823 op = may_peek_next_line(cctx, *arg, &next);
3824 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 break;
3826 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003827 if (next != NULL)
3828 {
3829 *arg = next_line_from_context(cctx, TRUE);
3830 op = skipwhite(*arg);
3831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003833 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003835 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003836 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
3838
3839 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003840 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003841 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003843 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 return FAIL;
3846
Bram Moolenaara5565e42020-05-09 15:44:01 +02003847 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003848 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003849 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3850 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3851 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3852 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3855 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003856
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003857 // concat/subtract/add constant numbers
3858 if (*op == '+')
3859 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3860 else if (*op == '-')
3861 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3862 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003863 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003864 // concatenate constant strings
3865 char_u *s1 = tv1->vval.v_string;
3866 char_u *s2 = tv2->vval.v_string;
3867 size_t len1 = STRLEN(s1);
3868
3869 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3870 if (tv1->vval.v_string == NULL)
3871 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003872 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003873 return FAIL;
3874 }
3875 mch_memmove(tv1->vval.v_string, s1, len1);
3876 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003877 vim_free(s1);
3878 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003879 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003880 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 }
3882 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003883 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003884 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003885 if (*op == '.')
3886 {
3887 if (may_generate_2STRING(-2, cctx) == FAIL
3888 || may_generate_2STRING(-1, cctx) == FAIL)
3889 return FAIL;
3890 generate_instr_drop(cctx, ISN_CONCAT, 1);
3891 }
3892 else
3893 generate_two_op(cctx, op);
3894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 }
3896
3897 return OK;
3898}
3899
3900/*
3901 * expr5a == expr5b
3902 * expr5a =~ expr5b
3903 * expr5a != expr5b
3904 * expr5a !~ expr5b
3905 * expr5a > expr5b
3906 * expr5a >= expr5b
3907 * expr5a < expr5b
3908 * expr5a <= expr5b
3909 * expr5a is expr5b
3910 * expr5a isnot expr5b
3911 *
3912 * Produces instructions:
3913 * EVAL expr5a Push result of "expr5a"
3914 * EVAL expr5b Push result of "expr5b"
3915 * COMPARE one of the compare instructions
3916 */
3917 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919{
3920 exptype_T type = EXPR_UNKNOWN;
3921 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003922 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926
3927 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003928 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 return FAIL;
3930
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003931 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003932 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
3934 /*
3935 * If there is a comparative operator, use it.
3936 */
3937 if (type != EXPR_UNKNOWN)
3938 {
3939 int ic = FALSE; // Default: do not ignore case
3940
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003941 if (next != NULL)
3942 {
3943 *arg = next_line_from_context(cctx, TRUE);
3944 p = skipwhite(*arg);
3945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 if (type_is && (p[len] == '?' || p[len] == '#'))
3947 {
3948 semsg(_(e_invexpr2), *arg);
3949 return FAIL;
3950 }
3951 // extra question mark appended: ignore case
3952 if (p[len] == '?')
3953 {
3954 ic = TRUE;
3955 ++len;
3956 }
3957 // extra '#' appended: match case (ignored)
3958 else if (p[len] == '#')
3959 ++len;
3960 // nothing appended: match case
3961
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003962 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003964 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003965 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
3967
3968 // get the second variable
3969 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003970 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003971 return FAIL;
3972
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 return FAIL;
3975
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976 if (ppconst->pp_used == ppconst_used + 2)
3977 {
3978 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3979 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3980 int ret;
3981
3982 // Both sides are a constant, compute the result now.
3983 // First check for a valid combination of types, this is more
3984 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003985 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003986 ret = FAIL;
3987 else
3988 {
3989 ret = typval_compare(tv1, tv2, type, ic);
3990 tv1->v_type = VAR_BOOL;
3991 tv1->vval.v_number = tv1->vval.v_number
3992 ? VVAL_TRUE : VVAL_FALSE;
3993 clear_tv(tv2);
3994 --ppconst->pp_used;
3995 }
3996 return ret;
3997 }
3998
3999 generate_ppconst(cctx, ppconst);
4000 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 }
4002
4003 return OK;
4004}
4005
Bram Moolenaar7f141552020-05-09 17:35:53 +02004006static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008/*
4009 * Compile || or &&.
4010 */
4011 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012compile_and_or(
4013 char_u **arg,
4014 cctx_T *cctx,
4015 char *op,
4016 ppconst_T *ppconst,
4017 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004019 char_u *next;
4020 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 int opchar = *op;
4022
4023 if (p[0] == opchar && p[1] == opchar)
4024 {
4025 garray_T *instr = &cctx->ctx_instr;
4026 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004027 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004028 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029
4030 /*
4031 * Repeat until there is no following "||" or "&&"
4032 */
4033 ga_init2(&end_ga, sizeof(int), 10);
4034 while (p[0] == opchar && p[1] == opchar)
4035 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004036 if (next != NULL)
4037 {
4038 *arg = next_line_from_context(cctx, TRUE);
4039 p = skipwhite(*arg);
4040 }
4041
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4043 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004044 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004045 return FAIL;
4046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004048 // TODO: use ppconst if the value is a constant and check
4049 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 generate_ppconst(cctx, ppconst);
4051
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004052 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4053 all_bool_values = FALSE;
4054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 if (ga_grow(&end_ga, 1) == FAIL)
4056 {
4057 ga_clear(&end_ga);
4058 return FAIL;
4059 }
4060 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4061 ++end_ga.ga_len;
4062 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004063 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
4065 // eval the next expression
4066 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004067 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004068 return FAIL;
4069
Bram Moolenaara5565e42020-05-09 15:44:01 +02004070 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4071 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 {
4073 ga_clear(&end_ga);
4074 return FAIL;
4075 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004076
4077 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004079 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080
4081 // Fill in the end label in all jumps.
4082 while (end_ga.ga_len > 0)
4083 {
4084 isn_T *isn;
4085
4086 --end_ga.ga_len;
4087 isn = ((isn_T *)instr->ga_data)
4088 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4089 isn->isn_arg.jump.jump_where = instr->ga_len;
4090 }
4091 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004092
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004093 // The resulting type is converted to bool if needed.
4094 if (!all_bool_values)
4095 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 }
4097
4098 return OK;
4099}
4100
4101/*
4102 * expr4a && expr4a && expr4a logical AND
4103 *
4104 * Produces instructions:
4105 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004106 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004108 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004110 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 * end:
4112 */
4113 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004114compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 int ppconst_used = ppconst->pp_used;
4117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004119 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 return FAIL;
4121
4122 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124}
4125
4126/*
4127 * expr3a || expr3b || expr3c logical OR
4128 *
4129 * Produces instructions:
4130 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004131 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004133 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004135 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 * end:
4137 */
4138 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 int ppconst_used = ppconst->pp_used;
4142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004144 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 return FAIL;
4146
4147 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149}
4150
4151/*
4152 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004154 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 * JUMP_IF_FALSE alt jump if false
4156 * EVAL expr1a
4157 * JUMP_ALWAYS end
4158 * alt: EVAL expr1b
4159 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004160 *
4161 * Toplevel expression: expr2 ?? expr1
4162 * Produces instructions:
4163 * EVAL expr2 Push result of "expr2"
4164 * JUMP_AND_KEEP_IF_TRUE end jump if true
4165 * EVAL expr1
4166 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 */
4168 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170{
4171 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004173 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
Bram Moolenaar3988f642020-08-27 22:43:03 +02004175 // Ignore all kinds of errors when not producing code.
4176 if (cctx->ctx_skip == SKIP_YES)
4177 {
4178 skip_expr(arg);
4179 return OK;
4180 }
4181
Bram Moolenaar61a89812020-05-07 16:58:17 +02004182 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 return FAIL;
4185
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004186 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 if (*p == '?')
4188 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004189 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 garray_T *instr = &cctx->ctx_instr;
4191 garray_T *stack = &cctx->ctx_type_stack;
4192 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004193 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004195 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004196 int has_const_expr = FALSE;
4197 int const_value = FALSE;
4198 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004200 if (next != NULL)
4201 {
4202 *arg = next_line_from_context(cctx, TRUE);
4203 p = skipwhite(*arg);
4204 }
4205
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004206 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004207 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004208 semsg(_(e_white_space_required_before_and_after_str),
4209 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004210 return FAIL;
4211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaara5565e42020-05-09 15:44:01 +02004213 if (ppconst->pp_used == ppconst_used + 1)
4214 {
4215 // the condition is a constant, we know whether the ? or the :
4216 // expression is to be evaluated.
4217 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004218 if (op_falsy)
4219 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4220 else
4221 {
4222 int error = FALSE;
4223
4224 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4225 &error);
4226 if (error)
4227 return FAIL;
4228 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004229 cctx->ctx_skip = save_skip == SKIP_YES ||
4230 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4231
4232 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4233 // "left ?? right" and "left" is truthy: produce "left"
4234 generate_ppconst(cctx, ppconst);
4235 else
4236 {
4237 clear_tv(&ppconst->pp_tv[ppconst_used]);
4238 --ppconst->pp_used;
4239 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 }
4241 else
4242 {
4243 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004244 if (op_falsy)
4245 end_idx = instr->ga_len;
4246 generate_JUMP(cctx, op_falsy
4247 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4248 if (op_falsy)
4249 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
4252 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004253 *arg = skipwhite(p + 1 + op_falsy);
4254 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004255 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 if (!has_const_expr)
4260 {
4261 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004263 if (!op_falsy)
4264 {
4265 // remember the type and drop it
4266 --stack->ga_len;
4267 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004269 end_idx = instr->ga_len;
4270 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004272 // jump here from JUMP_IF_FALSE
4273 isn = ((isn_T *)instr->ga_data) + alt_idx;
4274 isn->isn_arg.jump.jump_where = instr->ga_len;
4275 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004278 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004280 // Check for the ":".
4281 p = may_peek_next_line(cctx, *arg, &next);
4282 if (*p != ':')
4283 {
4284 emsg(_(e_missing_colon));
4285 return FAIL;
4286 }
4287 if (next != NULL)
4288 {
4289 *arg = next_line_from_context(cctx, TRUE);
4290 p = skipwhite(*arg);
4291 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004292
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004293 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4294 {
4295 semsg(_(e_white_space_required_before_and_after_str), ":");
4296 return FAIL;
4297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004299 // evaluate the third expression
4300 if (has_const_expr)
4301 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004302 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004303 *arg = skipwhite(p + 1);
4304 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4305 return FAIL;
4306 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4307 return FAIL;
4308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
Bram Moolenaara5565e42020-05-09 15:44:01 +02004310 if (!has_const_expr)
4311 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004312 type_T **typep;
4313
Bram Moolenaara5565e42020-05-09 15:44:01 +02004314 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004317 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4318 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004320 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 isn = ((isn_T *)instr->ga_data) + end_idx;
4322 isn->isn_arg.jump.jump_where = instr->ga_len;
4323 }
4324
4325 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 }
4327 return OK;
4328}
4329
4330/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 * Toplevel expression.
4332 */
4333 static int
4334compile_expr0(char_u **arg, cctx_T *cctx)
4335{
4336 ppconst_T ppconst;
4337
4338 CLEAR_FIELD(ppconst);
4339 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4340 {
4341 clear_ppconst(&ppconst);
4342 return FAIL;
4343 }
4344 if (generate_ppconst(cctx, &ppconst) == FAIL)
4345 return FAIL;
4346 return OK;
4347}
4348
4349/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 * compile "return [expr]"
4351 */
4352 static char_u *
4353compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4354{
4355 char_u *p = arg;
4356 garray_T *stack = &cctx->ctx_type_stack;
4357 type_T *stack_type;
4358
4359 if (*p != NUL && *p != '|' && *p != '\n')
4360 {
4361 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 return NULL;
4364
4365 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4366 if (set_return_type)
4367 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004368 else
4369 {
4370 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4371 && stack_type->tt_type != VAR_VOID
4372 && stack_type->tt_type != VAR_UNKNOWN)
4373 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004374 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004375 return NULL;
4376 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004377 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4378 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004379 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
4382 else
4383 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004384 // "set_return_type" cannot be TRUE, only used for a lambda which
4385 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004386 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4387 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004389 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 return NULL;
4391 }
4392
4393 // No argument, return zero.
4394 generate_PUSHNR(cctx, 0);
4395 }
4396
4397 if (generate_instr(cctx, ISN_RETURN) == NULL)
4398 return NULL;
4399
4400 // "return val | endif" is possible
4401 return skipwhite(p);
4402}
4403
4404/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004405 * Get a line from the compilation context, compatible with exarg_T getline().
4406 * Return a pointer to the line in allocated memory.
4407 * Return NULL for end-of-file or some error.
4408 */
4409 static char_u *
4410exarg_getline(
4411 int c UNUSED,
4412 void *cookie,
4413 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004414 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004415{
4416 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004417 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004418
Bram Moolenaar66250c92020-08-20 15:02:42 +02004419 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004420 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004421 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004422 return NULL;
4423 ++cctx->ctx_lnum;
4424 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4425 // Comment lines result in NULL pointers, skip them.
4426 if (p != NULL)
4427 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004428 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004429}
4430
4431/*
4432 * Compile a nested :def command.
4433 */
4434 static char_u *
4435compile_nested_function(exarg_T *eap, cctx_T *cctx)
4436{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004437 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004438 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004439 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004440 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004441 lvar_T *lvar;
4442 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004443 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004444
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004445 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004446 {
4447 emsg(_(e_cannot_use_bang_with_nested_def));
4448 return NULL;
4449 }
4450
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004451 // Only g:Func() can use a namespace.
4452 if (name_start[1] == ':' && !is_global)
4453 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004454 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004455 return NULL;
4456 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004457 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4458 return NULL;
4459
Bram Moolenaar04b12692020-05-04 23:24:44 +02004460 eap->arg = name_end;
4461 eap->getline = exarg_getline;
4462 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004463 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004464 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004465 lambda_name = get_lambda_name();
4466 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004467
Bram Moolenaar822ba242020-05-24 23:00:18 +02004468 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004469 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004470 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004471 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004472 {
4473 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004474 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004475 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004476
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004477 if (is_global)
4478 {
4479 char_u *func_name = vim_strnsave(name_start + 2,
4480 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004481
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004482 if (func_name == NULL)
4483 r = FAIL;
4484 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004485 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004486 }
4487 else
4488 {
4489 // Define a local variable for the function reference.
4490 lvar = reserve_local(cctx, name_start, name_end - name_start,
4491 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004492 if (lvar == NULL)
4493 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004494 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004495 return NULL;
4496 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4497 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004498
Bram Moolenaar61a89812020-05-07 16:58:17 +02004499 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004500 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004501}
4502
4503/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 * Return the length of an assignment operator, or zero if there isn't one.
4505 */
4506 int
4507assignment_len(char_u *p, int *heredoc)
4508{
4509 if (*p == '=')
4510 {
4511 if (p[1] == '<' && p[2] == '<')
4512 {
4513 *heredoc = TRUE;
4514 return 3;
4515 }
4516 return 1;
4517 }
4518 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4519 return 2;
4520 if (STRNCMP(p, "..=", 3) == 0)
4521 return 3;
4522 return 0;
4523}
4524
4525// words that cannot be used as a variable
4526static char *reserved[] = {
4527 "true",
4528 "false",
4529 NULL
4530};
4531
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004532typedef enum {
4533 dest_local,
4534 dest_option,
4535 dest_env,
4536 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004537 dest_buffer,
4538 dest_window,
4539 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004540 dest_vimvar,
4541 dest_script,
4542 dest_reg,
4543} assign_dest_T;
4544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004546 * Generate the load instruction for "name".
4547 */
4548 static void
4549generate_loadvar(
4550 cctx_T *cctx,
4551 assign_dest_T dest,
4552 char_u *name,
4553 lvar_T *lvar,
4554 type_T *type)
4555{
4556 switch (dest)
4557 {
4558 case dest_option:
4559 // TODO: check the option exists
4560 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4561 break;
4562 case dest_global:
4563 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4564 break;
4565 case dest_buffer:
4566 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4567 break;
4568 case dest_window:
4569 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4570 break;
4571 case dest_tab:
4572 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4573 break;
4574 case dest_script:
4575 compile_load_scriptvar(cctx,
4576 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4577 break;
4578 case dest_env:
4579 // Include $ in the name here
4580 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4581 break;
4582 case dest_reg:
4583 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4584 break;
4585 case dest_vimvar:
4586 generate_LOADV(cctx, name + 2, TRUE);
4587 break;
4588 case dest_local:
4589 if (lvar->lv_from_outer)
4590 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4591 NULL, type);
4592 else
4593 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4594 break;
4595 }
4596}
4597
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004598 void
4599vim9_declare_error(char_u *name)
4600{
4601 char *scope = "";
4602
4603 switch (*name)
4604 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004605 case 'g': scope = _("global"); break;
4606 case 'b': scope = _("buffer"); break;
4607 case 'w': scope = _("window"); break;
4608 case 't': scope = _("tab"); break;
4609 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004610 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004611 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004612 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004613 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004614 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004615 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004616 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004617 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004618 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004619}
4620
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004621/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004622 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004623 * "let name"
4624 * "var name = expr"
4625 * "final name = expr"
4626 * "const name = expr"
4627 * "name = expr"
4628 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 * Return NULL for an error.
4630 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 */
4632 static char_u *
4633compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4634{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004635 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004637 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 char_u *ret = NULL;
4639 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004640 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004641 int scriptvar_sid = 0;
4642 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004645 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 int oplen = 0;
4648 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004649 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004650 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004651 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004653 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4654 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 // Skip over the "var" or "[var, var]" to get to any "=".
4657 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4658 if (p == NULL)
4659 return *arg == '[' ? arg : NULL;
4660
4661 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004663 // TODO: should we allow this, and figure out type inference from list
4664 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004665 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 return NULL;
4667 }
4668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 sp = p;
4670 p = skipwhite(p);
4671 op = p;
4672 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673
4674 if (var_count > 0 && oplen == 0)
4675 // can be something like "[1, 2]->func()"
4676 return arg;
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4679 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004680 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004681 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004682 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 if (heredoc)
4685 {
4686 list_T *l;
4687 listitem_T *li;
4688
4689 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004692 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004693 if (l == NULL)
4694 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaar078269b2020-09-21 20:35:55 +02004696 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004698 // Push each line and the create the list.
4699 FOR_ALL_LIST_ITEMS(l, li)
4700 {
4701 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4702 li->li_tv.vval.v_string = NULL;
4703 }
4704 generate_NEWLIST(cctx, l->lv_len);
4705 type = &t_list_string;
4706 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 list_free(l);
4709 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004710 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004712 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 // for "[var, var] = expr" evaluate the expression here, loop over the
4715 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004716
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 p = skipwhite(op + oplen);
4718 if (compile_expr0(&p, cctx) == FAIL)
4719 return NULL;
4720 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004722 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004724 type_T *stacktype;
4725
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004726 stacktype = stack->ga_len == 0 ? &t_void
4727 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004728 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004730 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 goto theend;
4732 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004733 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004734 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004735 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004736 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4737 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004738 }
4739 }
4740
4741 /*
4742 * Loop over variables in "[var, var] = expr".
4743 * For "var = expr" and "let var: type" this is done only once.
4744 */
4745 if (var_count > 0)
4746 var_start = skipwhite(arg + 1); // skip over the "["
4747 else
4748 var_start = arg;
4749 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4750 {
4751 char_u *var_end = skip_var_one(var_start, FALSE);
4752 size_t varlen;
4753 int new_local = FALSE;
4754 int opt_type;
4755 int opt_flags = 0;
4756 assign_dest_T dest = dest_local;
4757 int vimvaridx = -1;
4758 lvar_T *lvar = NULL;
4759 lvar_T arg_lvar;
4760 int has_type = FALSE;
4761 int has_index = FALSE;
4762 int instr_count = -1;
4763
Bram Moolenaar65821722020-08-02 18:58:54 +02004764 if (*var_start == '@')
4765 p = var_start + 2;
4766 else
4767 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004768 // skip over the leading "&", "&l:", "&g:" and "$"
4769 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004770 p = to_name_end(p, TRUE);
4771 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004772
4773 // "a: type" is declaring variable "a" with a type, not "a:".
4774 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4775 --var_end;
4776 if (is_decl && p == var_start + 2 && p[-1] == ':')
4777 --p;
4778
4779 varlen = p - var_start;
4780 vim_free(name);
4781 name = vim_strnsave(var_start, varlen);
4782 if (name == NULL)
4783 return NULL;
4784 if (!heredoc)
4785 type = &t_any;
4786
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004787 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004789 int declare_error = FALSE;
4790
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 if (*var_start == '&')
4792 {
4793 int cc;
4794 long numval;
4795
4796 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004797 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 emsg(_(e_const_option));
4800 goto theend;
4801 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004802 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004803 p = var_start;
4804 p = find_option_end(&p, &opt_flags);
4805 if (p == NULL)
4806 {
4807 // cannot happen?
4808 emsg(_(e_letunexp));
4809 goto theend;
4810 }
4811 cc = *p;
4812 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004813 opt_type = get_option_value(skip_option_env_lead(var_start),
4814 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 *p = cc;
4816 if (opt_type == -3)
4817 {
4818 semsg(_(e_unknown_option), var_start);
4819 goto theend;
4820 }
4821 if (opt_type == -2 || opt_type == 0)
4822 type = &t_string;
4823 else
4824 type = &t_number; // both number and boolean option
4825 }
4826 else if (*var_start == '$')
4827 {
4828 dest = dest_env;
4829 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004830 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 }
4832 else if (*var_start == '@')
4833 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004834 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004835 {
4836 emsg_invreg(var_start[1]);
4837 goto theend;
4838 }
4839 dest = dest_reg;
4840 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004841 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004842 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004843 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 {
4845 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004846 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004847 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004848 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004849 {
4850 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004851 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004852 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004853 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 {
4855 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004856 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004857 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004858 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 {
4860 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004861 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004863 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004864 {
4865 typval_T *vtv;
4866 int di_flags;
4867
4868 vimvaridx = find_vim_var(name + 2, &di_flags);
4869 if (vimvaridx < 0)
4870 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004871 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872 goto theend;
4873 }
4874 // We use the current value of "sandbox" here, is that OK?
4875 if (var_check_ro(di_flags, name, FALSE))
4876 goto theend;
4877 dest = dest_vimvar;
4878 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004879 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004880 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004881 }
4882 else
4883 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004884 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885
4886 for (idx = 0; reserved[idx] != NULL; ++idx)
4887 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004889 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004890 goto theend;
4891 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004892
4893 lvar = lookup_local(var_start, varlen, cctx);
4894 if (lvar == NULL)
4895 {
4896 CLEAR_FIELD(arg_lvar);
4897 if (lookup_arg(var_start, varlen,
4898 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4899 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004900 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 if (is_decl)
4902 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004903 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 goto theend;
4905 }
4906 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004907 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004908 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 if (lvar != NULL)
4910 {
4911 if (is_decl)
4912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004913 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 goto theend;
4915 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004916 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004917 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004918 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004919 int script_namespace = varlen > 1
4920 && STRNCMP(var_start, "s:", 2) == 0;
4921 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004922 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4923 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004924 imported_T *import =
4925 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004926
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004927 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004929 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4930
4931 if (is_decl)
4932 {
4933 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004934 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004935 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004936 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004937 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004938 name);
4939 goto theend;
4940 }
4941 else if (cctx->ctx_ufunc->uf_script_ctx_version
4942 == SCRIPT_VERSION_VIM9
4943 && script_namespace
4944 && !script_var && import == NULL)
4945 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004946 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004947 goto theend;
4948 }
4949
4950 dest = dest_script;
4951
4952 // existing script-local variables should have a type
4953 scriptvar_sid = current_sctx.sc_sid;
4954 if (import != NULL)
4955 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004956 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004957 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004958 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4959 rawname, TRUE);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02004960 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02004961 {
4962 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4963 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004964 ((svar_T *)si->sn_var_vals.ga_data)
4965 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004966 type = sv->sv_type;
4967 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004968 }
4969 }
4970 else if (name[1] == ':' && name[2] != NUL)
4971 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004972 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 goto theend;
4974 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004975 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004977 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004978 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004979 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004980 else if (check_defined(var_start, varlen, cctx) == FAIL)
4981 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004983 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004984
4985 if (declare_error)
4986 {
4987 vim9_declare_error(name);
4988 goto theend;
4989 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 }
4991
4992 // handle "a:name" as a name, not index "name" on "a"
4993 if (varlen > 1 || var_start[varlen] != ':')
4994 p = var_end;
4995
4996 if (dest != dest_option)
4997 {
4998 if (is_decl && *p == ':')
4999 {
5000 // parse optional type: "let var: type = expr"
5001 if (!VIM_ISWHITE(p[1]))
5002 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005003 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 goto theend;
5005 }
5006 p = skipwhite(p + 1);
5007 type = parse_type(&p, cctx->ctx_type_list);
5008 has_type = TRUE;
5009 }
5010 else if (lvar != NULL)
5011 type = lvar->lv_type;
5012 }
5013
5014 if (oplen == 3 && !heredoc && dest != dest_global
5015 && type->tt_type != VAR_STRING
5016 && type->tt_type != VAR_ANY)
5017 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005018 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 goto theend;
5020 }
5021
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005022 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 {
5024 if (oplen > 1 && !heredoc)
5025 {
5026 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005027 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 goto theend;
5029 }
5030
5031 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005032 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5033 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 goto theend;
5035 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005036 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 if (lvar == NULL)
5038 goto theend;
5039 new_local = TRUE;
5040 }
5041
5042 member_type = type;
5043 if (var_end > var_start + varlen)
5044 {
5045 // Something follows after the variable: "var[idx]".
5046 if (is_decl)
5047 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005048 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 goto theend;
5050 }
5051
5052 if (var_start[varlen] == '[')
5053 {
5054 has_index = TRUE;
5055 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005056 member_type = &t_any;
5057 else
5058 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 }
5060 else
5061 {
5062 semsg("Not supported yet: %s", var_start);
5063 goto theend;
5064 }
5065 }
5066 else if (lvar == &arg_lvar)
5067 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005068 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005069 goto theend;
5070 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005071 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5072 {
5073 semsg(_(e_cannot_assign_to_constant), name);
5074 goto theend;
5075 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076
5077 if (!heredoc)
5078 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005079 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005081 if (oplen > 0 && var_count == 0)
5082 {
5083 // skip over the "=" and the expression
5084 p = skipwhite(op + oplen);
5085 compile_expr0(&p, cctx);
5086 }
5087 }
5088 else if (oplen > 0)
5089 {
5090 type_T *stacktype;
5091
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 // For "var = expr" evaluate the expression.
5093 if (var_count == 0)
5094 {
5095 int r;
5096
5097 // for "+=", "*=", "..=" etc. first load the current value
5098 if (*op != '=')
5099 {
5100 generate_loadvar(cctx, dest, name, lvar, type);
5101
5102 if (has_index)
5103 {
5104 // TODO: get member from list or dict
5105 emsg("Index with operation not supported yet");
5106 goto theend;
5107 }
5108 }
5109
5110 // Compile the expression. Temporarily hide the new local
5111 // variable here, it is not available to this expression.
5112 if (new_local)
5113 --cctx->ctx_locals.ga_len;
5114 instr_count = instr->ga_len;
5115 p = skipwhite(op + oplen);
5116 r = compile_expr0(&p, cctx);
5117 if (new_local)
5118 ++cctx->ctx_locals.ga_len;
5119 if (r == FAIL)
5120 goto theend;
5121 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005122 else if (semicolon && var_idx == var_count - 1)
5123 {
5124 // For "[var; var] = expr" get the rest of the list
5125 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5126 goto theend;
5127 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005128 else
5129 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 // For "[var, var] = expr" get the "var_idx" item from the
5131 // list.
5132 if (generate_GETITEM(cctx, var_idx) == FAIL)
5133 return FAIL;
5134 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005135
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005136 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005137 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005138 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005140 if ((stacktype->tt_type == VAR_FUNC
5141 || stacktype->tt_type == VAR_PARTIAL)
5142 && var_wrong_func_name(name, TRUE))
5143 goto theend;
5144
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005145 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005146 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005147 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005149 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005150 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 }
5152 else
5153 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005154 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005155 // for a variable that implies &t_any.
5156 if (stacktype == &t_list_empty)
5157 lvar->lv_type = &t_list_any;
5158 else if (stacktype == &t_dict_empty)
5159 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005160 else if (stacktype == &t_unknown)
5161 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005162 else
5163 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005165 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005166 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005167 {
5168 type_T *use_type = lvar->lv_type;
5169
Bram Moolenaar71abe482020-09-14 22:28:30 +02005170 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005171 if (has_index)
5172 {
5173 use_type = use_type->tt_member;
5174 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005175 // could be indexing "any"
5176 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005177 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005178 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005179 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005180 goto theend;
5181 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005182 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005183 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005184 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005187 else if (cmdidx == CMD_final)
5188 {
5189 emsg(_(e_final_requires_a_value));
5190 goto theend;
5191 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005194 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 goto theend;
5196 }
5197 else if (!has_type || dest == dest_option)
5198 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005199 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 goto theend;
5201 }
5202 else
5203 {
5204 // variables are always initialized
5205 if (ga_grow(instr, 1) == FAIL)
5206 goto theend;
5207 switch (member_type->tt_type)
5208 {
5209 case VAR_BOOL:
5210 generate_PUSHBOOL(cctx, VVAL_FALSE);
5211 break;
5212 case VAR_FLOAT:
5213#ifdef FEAT_FLOAT
5214 generate_PUSHF(cctx, 0.0);
5215#endif
5216 break;
5217 case VAR_STRING:
5218 generate_PUSHS(cctx, NULL);
5219 break;
5220 case VAR_BLOB:
5221 generate_PUSHBLOB(cctx, NULL);
5222 break;
5223 case VAR_FUNC:
5224 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5225 break;
5226 case VAR_LIST:
5227 generate_NEWLIST(cctx, 0);
5228 break;
5229 case VAR_DICT:
5230 generate_NEWDICT(cctx, 0);
5231 break;
5232 case VAR_JOB:
5233 generate_PUSHJOB(cctx, NULL);
5234 break;
5235 case VAR_CHANNEL:
5236 generate_PUSHCHANNEL(cctx, NULL);
5237 break;
5238 case VAR_NUMBER:
5239 case VAR_UNKNOWN:
5240 case VAR_ANY:
5241 case VAR_PARTIAL:
5242 case VAR_VOID:
5243 case VAR_SPECIAL: // cannot happen
5244 generate_PUSHNR(cctx, 0);
5245 break;
5246 }
5247 }
5248 if (var_count == 0)
5249 end = p;
5250 }
5251
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005252 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005253 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005254 break;
5255
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 if (oplen > 0 && *op != '=')
5257 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005258 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 type_T *stacktype;
5260
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 if (*op == '.')
5262 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005263 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005264 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005266 if (
5267#ifdef FEAT_FLOAT
5268 // If variable is float operation with number is OK.
5269 !(expected == &t_float && stacktype == &t_number) &&
5270#endif
5271 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 goto theend;
5273
5274 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005275 {
5276 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5277 goto theend;
5278 }
5279 else if (*op == '+')
5280 {
5281 if (generate_add_instr(cctx,
5282 operator_type(member_type, stacktype),
5283 member_type, stacktype) == FAIL)
5284 goto theend;
5285 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005286 else if (generate_two_op(cctx, op) == FAIL)
5287 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005291 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 int r;
5293
5294 // Compile the "idx" in "var[idx]".
5295 if (new_local)
5296 --cctx->ctx_locals.ga_len;
5297 p = skipwhite(var_start + varlen + 1);
5298 r = compile_expr0(&p, cctx);
5299 if (new_local)
5300 ++cctx->ctx_locals.ga_len;
5301 if (r == FAIL)
5302 goto theend;
5303 if (*skipwhite(p) != ']')
5304 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005305 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 emsg(_(e_missbrac));
5307 goto theend;
5308 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005309 if (type == &t_any)
5310 {
5311 type_T *idx_type = ((type_T **)stack->ga_data)[
5312 stack->ga_len - 1];
5313 // Index on variable of unknown type: guess the type from the
5314 // index type: number is dict, otherwise dict.
5315 // TODO: should do the assignment at runtime
5316 if (idx_type->tt_type == VAR_NUMBER)
5317 type = &t_list_any;
5318 else
5319 type = &t_dict_any;
5320 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 if (type->tt_type == VAR_DICT
5322 && may_generate_2STRING(-1, cctx) == FAIL)
5323 goto theend;
5324 if (type->tt_type == VAR_LIST
5325 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005326 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 {
5328 emsg(_(e_number_exp));
5329 goto theend;
5330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005332 // Load the dict or list. On the stack we then have:
5333 // - value
5334 // - index
5335 // - variable
5336 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005337
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 if (type->tt_type == VAR_LIST)
5339 {
5340 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5341 return FAIL;
5342 }
5343 else if (type->tt_type == VAR_DICT)
5344 {
5345 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5346 return FAIL;
5347 }
5348 else
5349 {
5350 emsg(_(e_listreq));
5351 goto theend;
5352 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005353 }
5354 else
5355 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005356 if (is_decl && cmdidx == CMD_const
5357 && (dest == dest_script || dest == dest_local))
5358 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005359 generate_LOCKCONST(cctx);
5360
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 switch (dest)
5362 {
5363 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005364 generate_STOREOPT(cctx, skip_option_env_lead(name),
5365 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005366 break;
5367 case dest_global:
5368 // include g: with the name, easier to execute that way
5369 generate_STORE(cctx, ISN_STOREG, 0, name);
5370 break;
5371 case dest_buffer:
5372 // include b: with the name, easier to execute that way
5373 generate_STORE(cctx, ISN_STOREB, 0, name);
5374 break;
5375 case dest_window:
5376 // include w: with the name, easier to execute that way
5377 generate_STORE(cctx, ISN_STOREW, 0, name);
5378 break;
5379 case dest_tab:
5380 // include t: with the name, easier to execute that way
5381 generate_STORE(cctx, ISN_STORET, 0, name);
5382 break;
5383 case dest_env:
5384 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5385 break;
5386 case dest_reg:
5387 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5388 break;
5389 case dest_vimvar:
5390 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5391 break;
5392 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005393 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005394 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 {
5396 char_u *name_s = name;
5397
5398 // Include s: in the name for store_var()
5399 if (name[1] != ':')
5400 {
5401 int len = (int)STRLEN(name) + 3;
5402
5403 name_s = alloc(len);
5404 if (name_s == NULL)
5405 name_s = name;
5406 else
5407 vim_snprintf((char *)name_s, len,
5408 "s:%s", name);
5409 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005410 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5411 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412 if (name_s != name)
5413 vim_free(name_s);
5414 }
5415 else
5416 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005417 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005418 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 break;
5420 case dest_local:
5421 if (lvar != NULL)
5422 {
5423 isn_T *isn = ((isn_T *)instr->ga_data)
5424 + instr->ga_len - 1;
5425
5426 // optimization: turn "var = 123" from ISN_PUSHNR +
5427 // ISN_STORE into ISN_STORENR
5428 if (!lvar->lv_from_outer
5429 && instr->ga_len == instr_count + 1
5430 && isn->isn_type == ISN_PUSHNR)
5431 {
5432 varnumber_T val = isn->isn_arg.number;
5433
5434 isn->isn_type = ISN_STORENR;
5435 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5436 isn->isn_arg.storenr.stnr_val = val;
5437 if (stack->ga_len > 0)
5438 --stack->ga_len;
5439 }
5440 else if (lvar->lv_from_outer)
5441 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005442 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005443 else
5444 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5445 }
5446 break;
5447 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005448 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449
5450 if (var_idx + 1 < var_count)
5451 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005453
5454 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005455 if (var_count > 0 && !semicolon)
5456 {
5457 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5458 goto theend;
5459 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460
Bram Moolenaarb2097502020-07-19 17:17:02 +02005461 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462
5463theend:
5464 vim_free(name);
5465 return ret;
5466}
5467
5468/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005469 * Check if "name" can be "unlet".
5470 */
5471 int
5472check_vim9_unlet(char_u *name)
5473{
5474 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5475 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005476 // "unlet s:var" is allowed in legacy script.
5477 if (*name == 's' && !script_is_vim9())
5478 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005479 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005480 return FAIL;
5481 }
5482 return OK;
5483}
5484
5485/*
5486 * Callback passed to ex_unletlock().
5487 */
5488 static int
5489compile_unlet(
5490 lval_T *lvp,
5491 char_u *name_end,
5492 exarg_T *eap,
5493 int deep UNUSED,
5494 void *coookie)
5495{
5496 cctx_T *cctx = coookie;
5497
5498 if (lvp->ll_tv == NULL)
5499 {
5500 char_u *p = lvp->ll_name;
5501 int cc = *name_end;
5502 int ret = OK;
5503
5504 // Normal name. Only supports g:, w:, t: and b: namespaces.
5505 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005506 if (*p == '$')
5507 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5508 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005509 ret = FAIL;
5510 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005511 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005512
5513 *name_end = cc;
5514 return ret;
5515 }
5516
5517 // TODO: unlet {list}[idx]
5518 // TODO: unlet {dict}[key]
5519 emsg("Sorry, :unlet not fully implemented yet");
5520 return FAIL;
5521}
5522
5523/*
5524 * compile "unlet var", "lock var" and "unlock var"
5525 * "arg" points to "var".
5526 */
5527 static char_u *
5528compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5529{
5530 char_u *p = arg;
5531
5532 if (eap->cmdidx != CMD_unlet)
5533 {
5534 emsg("Sorry, :lock and unlock not implemented yet");
5535 return NULL;
5536 }
5537
5538 if (*p == '!')
5539 {
5540 p = skipwhite(p + 1);
5541 eap->forceit = TRUE;
5542 }
5543
5544 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5545 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5546}
5547
5548/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549 * Compile an :import command.
5550 */
5551 static char_u *
5552compile_import(char_u *arg, cctx_T *cctx)
5553{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005554 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555}
5556
5557/*
5558 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5559 */
5560 static int
5561compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5562{
5563 garray_T *instr = &cctx->ctx_instr;
5564 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5565
5566 if (endlabel == NULL)
5567 return FAIL;
5568 endlabel->el_next = *el;
5569 *el = endlabel;
5570 endlabel->el_end_label = instr->ga_len;
5571
5572 generate_JUMP(cctx, when, 0);
5573 return OK;
5574}
5575
5576 static void
5577compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5578{
5579 garray_T *instr = &cctx->ctx_instr;
5580
5581 while (*el != NULL)
5582 {
5583 endlabel_T *cur = (*el);
5584 isn_T *isn;
5585
5586 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5587 isn->isn_arg.jump.jump_where = instr->ga_len;
5588 *el = cur->el_next;
5589 vim_free(cur);
5590 }
5591}
5592
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005593 static void
5594compile_free_jump_to_end(endlabel_T **el)
5595{
5596 while (*el != NULL)
5597 {
5598 endlabel_T *cur = (*el);
5599
5600 *el = cur->el_next;
5601 vim_free(cur);
5602 }
5603}
5604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605/*
5606 * Create a new scope and set up the generic items.
5607 */
5608 static scope_T *
5609new_scope(cctx_T *cctx, scopetype_T type)
5610{
5611 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5612
5613 if (scope == NULL)
5614 return NULL;
5615 scope->se_outer = cctx->ctx_scope;
5616 cctx->ctx_scope = scope;
5617 scope->se_type = type;
5618 scope->se_local_count = cctx->ctx_locals.ga_len;
5619 return scope;
5620}
5621
5622/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005623 * Free the current scope and go back to the outer scope.
5624 */
5625 static void
5626drop_scope(cctx_T *cctx)
5627{
5628 scope_T *scope = cctx->ctx_scope;
5629
5630 if (scope == NULL)
5631 {
5632 iemsg("calling drop_scope() without a scope");
5633 return;
5634 }
5635 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005636 switch (scope->se_type)
5637 {
5638 case IF_SCOPE:
5639 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5640 case FOR_SCOPE:
5641 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5642 case WHILE_SCOPE:
5643 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5644 case TRY_SCOPE:
5645 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5646 case NO_SCOPE:
5647 case BLOCK_SCOPE:
5648 break;
5649 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005650 vim_free(scope);
5651}
5652
5653/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005654 * Check that the top of the type stack has a type that can be used as a
5655 * condition. Give an error and return FAIL if not.
5656 */
5657 static int
5658bool_on_stack(cctx_T *cctx)
5659{
5660 garray_T *stack = &cctx->ctx_type_stack;
5661 type_T *type;
5662
5663 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5664 if (type != &t_bool && type != &t_number && type != &t_any
5665 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5666 return FAIL;
5667 return OK;
5668}
5669
5670/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671 * compile "if expr"
5672 *
5673 * "if expr" Produces instructions:
5674 * EVAL expr Push result of "expr"
5675 * JUMP_IF_FALSE end
5676 * ... body ...
5677 * end:
5678 *
5679 * "if expr | else" Produces instructions:
5680 * EVAL expr Push result of "expr"
5681 * JUMP_IF_FALSE else
5682 * ... body ...
5683 * JUMP_ALWAYS end
5684 * else:
5685 * ... body ...
5686 * end:
5687 *
5688 * "if expr1 | elseif expr2 | else" Produces instructions:
5689 * EVAL expr Push result of "expr"
5690 * JUMP_IF_FALSE elseif
5691 * ... body ...
5692 * JUMP_ALWAYS end
5693 * elseif:
5694 * EVAL expr Push result of "expr"
5695 * JUMP_IF_FALSE else
5696 * ... body ...
5697 * JUMP_ALWAYS end
5698 * else:
5699 * ... body ...
5700 * end:
5701 */
5702 static char_u *
5703compile_if(char_u *arg, cctx_T *cctx)
5704{
5705 char_u *p = arg;
5706 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005707 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005709 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005710 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711
Bram Moolenaara5565e42020-05-09 15:44:01 +02005712 CLEAR_FIELD(ppconst);
5713 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005714 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005715 clear_ppconst(&ppconst);
5716 return NULL;
5717 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005718 if (cctx->ctx_skip == SKIP_YES)
5719 clear_ppconst(&ppconst);
5720 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005721 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005722 int error = FALSE;
5723 int v;
5724
Bram Moolenaara5565e42020-05-09 15:44:01 +02005725 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005726 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005727 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005728 if (error)
5729 return NULL;
5730 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005731 }
5732 else
5733 {
5734 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005735 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005736 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005737 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005738 if (bool_on_stack(cctx) == FAIL)
5739 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
5742 scope = new_scope(cctx, IF_SCOPE);
5743 if (scope == NULL)
5744 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005745 scope->se_skip_save = skip_save;
5746 // "is_had_return" will be reset if any block does not end in :return
5747 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005749 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005750 {
5751 // "where" is set when ":elseif", "else" or ":endif" is found
5752 scope->se_u.se_if.is_if_label = instr->ga_len;
5753 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5754 }
5755 else
5756 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757
5758 return p;
5759}
5760
5761 static char_u *
5762compile_elseif(char_u *arg, cctx_T *cctx)
5763{
5764 char_u *p = arg;
5765 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005766 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767 isn_T *isn;
5768 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005769 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005770 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771
5772 if (scope == NULL || scope->se_type != IF_SCOPE)
5773 {
5774 emsg(_(e_elseif_without_if));
5775 return NULL;
5776 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005777 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005778 if (!cctx->ctx_had_return)
5779 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005781 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005782 {
5783 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005785 return NULL;
5786 // previous "if" or "elseif" jumps here
5787 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5788 isn->isn_arg.jump.jump_where = instr->ga_len;
5789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005791 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005792 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005793 if (cctx->ctx_skip == SKIP_YES)
5794 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005795 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005796 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005797 clear_ppconst(&ppconst);
5798 return NULL;
5799 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005800 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005801 if (scope->se_skip_save == SKIP_YES)
5802 clear_ppconst(&ppconst);
5803 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005804 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005805 int error = FALSE;
5806 int v;
5807
Bram Moolenaar7f141552020-05-09 17:35:53 +02005808 // The expression results in a constant.
5809 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005810 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5811 if (error)
5812 return NULL;
5813 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005814 clear_ppconst(&ppconst);
5815 scope->se_u.se_if.is_if_label = -1;
5816 }
5817 else
5818 {
5819 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005820 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005821 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005822 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005823 if (bool_on_stack(cctx) == FAIL)
5824 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005826 // "where" is set when ":elseif", "else" or ":endif" is found
5827 scope->se_u.se_if.is_if_label = instr->ga_len;
5828 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830
5831 return p;
5832}
5833
5834 static char_u *
5835compile_else(char_u *arg, cctx_T *cctx)
5836{
5837 char_u *p = arg;
5838 garray_T *instr = &cctx->ctx_instr;
5839 isn_T *isn;
5840 scope_T *scope = cctx->ctx_scope;
5841
5842 if (scope == NULL || scope->se_type != IF_SCOPE)
5843 {
5844 emsg(_(e_else_without_if));
5845 return NULL;
5846 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005847 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005848 if (!cctx->ctx_had_return)
5849 scope->se_u.se_if.is_had_return = FALSE;
5850 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851
Bram Moolenaarefd88552020-06-18 20:50:10 +02005852 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005853 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005854 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005855 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005856 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005857 if (!cctx->ctx_had_return
5858 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5859 JUMP_ALWAYS, cctx) == FAIL)
5860 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005861 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005862
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005863 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005864 {
5865 if (scope->se_u.se_if.is_if_label >= 0)
5866 {
5867 // previous "if" or "elseif" jumps here
5868 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5869 isn->isn_arg.jump.jump_where = instr->ga_len;
5870 scope->se_u.se_if.is_if_label = -1;
5871 }
5872 }
5873
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005874 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005875 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877
5878 return p;
5879}
5880
5881 static char_u *
5882compile_endif(char_u *arg, cctx_T *cctx)
5883{
5884 scope_T *scope = cctx->ctx_scope;
5885 ifscope_T *ifscope;
5886 garray_T *instr = &cctx->ctx_instr;
5887 isn_T *isn;
5888
5889 if (scope == NULL || scope->se_type != IF_SCOPE)
5890 {
5891 emsg(_(e_endif_without_if));
5892 return NULL;
5893 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005894 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005895 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005896 if (!cctx->ctx_had_return)
5897 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005899 if (scope->se_u.se_if.is_if_label >= 0)
5900 {
5901 // previous "if" or "elseif" jumps here
5902 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5903 isn->isn_arg.jump.jump_where = instr->ga_len;
5904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 // Fill in the "end" label in jumps at the end of the blocks.
5906 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005907 cctx->ctx_skip = scope->se_skip_save;
5908
5909 // If all the blocks end in :return and there is an :else then the
5910 // had_return flag is set.
5911 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005913 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 return arg;
5915}
5916
5917/*
5918 * compile "for var in expr"
5919 *
5920 * Produces instructions:
5921 * PUSHNR -1
5922 * STORE loop-idx Set index to -1
5923 * EVAL expr Push result of "expr"
5924 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5925 * - if beyond end, jump to "end"
5926 * - otherwise get item from list and push it
5927 * STORE var Store item in "var"
5928 * ... body ...
5929 * JUMP top Jump back to repeat
5930 * end: DROP Drop the result of "expr"
5931 *
5932 */
5933 static char_u *
5934compile_for(char_u *arg, cctx_T *cctx)
5935{
5936 char_u *p;
5937 size_t varlen;
5938 garray_T *instr = &cctx->ctx_instr;
5939 garray_T *stack = &cctx->ctx_type_stack;
5940 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005941 lvar_T *loop_lvar; // loop iteration variable
5942 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 type_T *vartype;
5944
5945 // TODO: list of variables: "for [key, value] in dict"
5946 // parse "var"
5947 for (p = arg; eval_isnamec1(*p); ++p)
5948 ;
5949 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005950 var_lvar = lookup_local(arg, varlen, cctx);
5951 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005953 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 return NULL;
5955 }
5956
5957 // consume "in"
5958 p = skipwhite(p);
5959 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5960 {
5961 emsg(_(e_missing_in));
5962 return NULL;
5963 }
5964 p = skipwhite(p + 2);
5965
5966
5967 scope = new_scope(cctx, FOR_SCOPE);
5968 if (scope == NULL)
5969 return NULL;
5970
5971 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005972 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5973 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005974 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005975 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005976 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005981 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5982 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005983 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005984 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005985 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005989 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 // compile "expr", it remains on the stack until "endfor"
5992 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005993 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005994 {
5995 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005999 // Now that we know the type of "var", check that it is a list, now or at
6000 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006002 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006004 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 return NULL;
6006 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006007 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006008 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009
6010 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006011 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006013 generate_FOR(cctx, loop_lvar->lv_idx);
6014 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015
6016 return arg;
6017}
6018
6019/*
6020 * compile "endfor"
6021 */
6022 static char_u *
6023compile_endfor(char_u *arg, cctx_T *cctx)
6024{
6025 garray_T *instr = &cctx->ctx_instr;
6026 scope_T *scope = cctx->ctx_scope;
6027 forscope_T *forscope;
6028 isn_T *isn;
6029
6030 if (scope == NULL || scope->se_type != FOR_SCOPE)
6031 {
6032 emsg(_(e_for));
6033 return NULL;
6034 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006035 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006037 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038
6039 // At end of ":for" scope jump back to the FOR instruction.
6040 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6041
6042 // Fill in the "end" label in the FOR statement so it can jump here
6043 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6044 isn->isn_arg.forloop.for_end = instr->ga_len;
6045
6046 // Fill in the "end" label any BREAK statements
6047 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6048
6049 // Below the ":for" scope drop the "expr" list from the stack.
6050 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6051 return NULL;
6052
6053 vim_free(scope);
6054
6055 return arg;
6056}
6057
6058/*
6059 * compile "while expr"
6060 *
6061 * Produces instructions:
6062 * top: EVAL expr Push result of "expr"
6063 * JUMP_IF_FALSE end jump if false
6064 * ... body ...
6065 * JUMP top Jump back to repeat
6066 * end:
6067 *
6068 */
6069 static char_u *
6070compile_while(char_u *arg, cctx_T *cctx)
6071{
6072 char_u *p = arg;
6073 garray_T *instr = &cctx->ctx_instr;
6074 scope_T *scope;
6075
6076 scope = new_scope(cctx, WHILE_SCOPE);
6077 if (scope == NULL)
6078 return NULL;
6079
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006080 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
6082 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006083 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 return NULL;
6085
Bram Moolenaar13106602020-10-04 16:06:05 +02006086 if (bool_on_stack(cctx) == FAIL)
6087 return FAIL;
6088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006090 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 JUMP_IF_FALSE, cctx) == FAIL)
6092 return FAIL;
6093
6094 return p;
6095}
6096
6097/*
6098 * compile "endwhile"
6099 */
6100 static char_u *
6101compile_endwhile(char_u *arg, cctx_T *cctx)
6102{
6103 scope_T *scope = cctx->ctx_scope;
6104
6105 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6106 {
6107 emsg(_(e_while));
6108 return NULL;
6109 }
6110 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006111 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112
6113 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006114 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
6116 // Fill in the "end" label in the WHILE statement so it can jump here.
6117 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006118 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
6120 vim_free(scope);
6121
6122 return arg;
6123}
6124
6125/*
6126 * compile "continue"
6127 */
6128 static char_u *
6129compile_continue(char_u *arg, cctx_T *cctx)
6130{
6131 scope_T *scope = cctx->ctx_scope;
6132
6133 for (;;)
6134 {
6135 if (scope == NULL)
6136 {
6137 emsg(_(e_continue));
6138 return NULL;
6139 }
6140 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6141 break;
6142 scope = scope->se_outer;
6143 }
6144
6145 // Jump back to the FOR or WHILE instruction.
6146 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006147 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6148 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 return arg;
6150}
6151
6152/*
6153 * compile "break"
6154 */
6155 static char_u *
6156compile_break(char_u *arg, cctx_T *cctx)
6157{
6158 scope_T *scope = cctx->ctx_scope;
6159 endlabel_T **el;
6160
6161 for (;;)
6162 {
6163 if (scope == NULL)
6164 {
6165 emsg(_(e_break));
6166 return NULL;
6167 }
6168 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6169 break;
6170 scope = scope->se_outer;
6171 }
6172
6173 // Jump to the end of the FOR or WHILE loop.
6174 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006175 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006177 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6179 return FAIL;
6180
6181 return arg;
6182}
6183
6184/*
6185 * compile "{" start of block
6186 */
6187 static char_u *
6188compile_block(char_u *arg, cctx_T *cctx)
6189{
6190 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6191 return NULL;
6192 return skipwhite(arg + 1);
6193}
6194
6195/*
6196 * compile end of block: drop one scope
6197 */
6198 static void
6199compile_endblock(cctx_T *cctx)
6200{
6201 scope_T *scope = cctx->ctx_scope;
6202
6203 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006204 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 vim_free(scope);
6206}
6207
6208/*
6209 * compile "try"
6210 * Creates a new scope for the try-endtry, pointing to the first catch and
6211 * finally.
6212 * Creates another scope for the "try" block itself.
6213 * TRY instruction sets up exception handling at runtime.
6214 *
6215 * "try"
6216 * TRY -> catch1, -> finally push trystack entry
6217 * ... try block
6218 * "throw {exception}"
6219 * EVAL {exception}
6220 * THROW create exception
6221 * ... try block
6222 * " catch {expr}"
6223 * JUMP -> finally
6224 * catch1: PUSH exeception
6225 * EVAL {expr}
6226 * MATCH
6227 * JUMP nomatch -> catch2
6228 * CATCH remove exception
6229 * ... catch block
6230 * " catch"
6231 * JUMP -> finally
6232 * catch2: CATCH remove exception
6233 * ... catch block
6234 * " finally"
6235 * finally:
6236 * ... finally block
6237 * " endtry"
6238 * ENDTRY pop trystack entry, may rethrow
6239 */
6240 static char_u *
6241compile_try(char_u *arg, cctx_T *cctx)
6242{
6243 garray_T *instr = &cctx->ctx_instr;
6244 scope_T *try_scope;
6245 scope_T *scope;
6246
6247 // scope that holds the jumps that go to catch/finally/endtry
6248 try_scope = new_scope(cctx, TRY_SCOPE);
6249 if (try_scope == NULL)
6250 return NULL;
6251
6252 // "catch" is set when the first ":catch" is found.
6253 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006254 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 if (generate_instr(cctx, ISN_TRY) == NULL)
6256 return NULL;
6257
6258 // scope for the try block itself
6259 scope = new_scope(cctx, BLOCK_SCOPE);
6260 if (scope == NULL)
6261 return NULL;
6262
6263 return arg;
6264}
6265
6266/*
6267 * compile "catch {expr}"
6268 */
6269 static char_u *
6270compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6271{
6272 scope_T *scope = cctx->ctx_scope;
6273 garray_T *instr = &cctx->ctx_instr;
6274 char_u *p;
6275 isn_T *isn;
6276
6277 // end block scope from :try or :catch
6278 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6279 compile_endblock(cctx);
6280 scope = cctx->ctx_scope;
6281
6282 // Error if not in a :try scope
6283 if (scope == NULL || scope->se_type != TRY_SCOPE)
6284 {
6285 emsg(_(e_catch));
6286 return NULL;
6287 }
6288
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006289 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006291 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 return NULL;
6293 }
6294
6295 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006296 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 JUMP_ALWAYS, cctx) == FAIL)
6298 return NULL;
6299
6300 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006301 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 if (isn->isn_arg.try.try_catch == 0)
6303 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006304 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 {
6306 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 isn->isn_arg.jump.jump_where = instr->ga_len;
6309 }
6310
6311 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006312 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 scope->se_u.se_try.ts_caught_all = TRUE;
6315 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 }
6317 else
6318 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006319 char_u *end;
6320 char_u *pat;
6321 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006322 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006323 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 // Push v:exception, push {expr} and MATCH
6326 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6327
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006328 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006329 if (*end != *p)
6330 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006331 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006332 vim_free(tofree);
6333 return FAIL;
6334 }
6335 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006336 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006337 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006338 len = (int)(end - tofree);
6339 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006340 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006341 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006342 if (pat == NULL)
6343 return FAIL;
6344 if (generate_PUSHS(cctx, pat) == FAIL)
6345 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6348 return NULL;
6349
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006350 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6352 return NULL;
6353 }
6354
6355 if (generate_instr(cctx, ISN_CATCH) == NULL)
6356 return NULL;
6357
6358 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6359 return NULL;
6360 return p;
6361}
6362
6363 static char_u *
6364compile_finally(char_u *arg, cctx_T *cctx)
6365{
6366 scope_T *scope = cctx->ctx_scope;
6367 garray_T *instr = &cctx->ctx_instr;
6368 isn_T *isn;
6369
6370 // end block scope from :try or :catch
6371 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6372 compile_endblock(cctx);
6373 scope = cctx->ctx_scope;
6374
6375 // Error if not in a :try scope
6376 if (scope == NULL || scope->se_type != TRY_SCOPE)
6377 {
6378 emsg(_(e_finally));
6379 return NULL;
6380 }
6381
6382 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006383 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 if (isn->isn_arg.try.try_finally != 0)
6385 {
6386 emsg(_(e_finally_dup));
6387 return NULL;
6388 }
6389
6390 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006391 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392
Bram Moolenaar585fea72020-04-02 22:33:21 +02006393 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 {
6396 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006397 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006399 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 }
6401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 // TODO: set index in ts_finally_label jumps
6403
6404 return arg;
6405}
6406
6407 static char_u *
6408compile_endtry(char_u *arg, cctx_T *cctx)
6409{
6410 scope_T *scope = cctx->ctx_scope;
6411 garray_T *instr = &cctx->ctx_instr;
6412 isn_T *isn;
6413
6414 // end block scope from :catch or :finally
6415 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6416 compile_endblock(cctx);
6417 scope = cctx->ctx_scope;
6418
6419 // Error if not in a :try scope
6420 if (scope == NULL || scope->se_type != TRY_SCOPE)
6421 {
6422 if (scope == NULL)
6423 emsg(_(e_no_endtry));
6424 else if (scope->se_type == WHILE_SCOPE)
6425 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006426 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 emsg(_(e_endfor));
6428 else
6429 emsg(_(e_endif));
6430 return NULL;
6431 }
6432
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006433 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6435 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006436 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 return NULL;
6438 }
6439
6440 // Fill in the "end" label in jumps at the end of the blocks, if not done
6441 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006442 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443
6444 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006445 if (isn->isn_arg.try.try_catch == 0)
6446 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447 if (isn->isn_arg.try.try_finally == 0)
6448 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006449
6450 if (scope->se_u.se_try.ts_catch_label != 0)
6451 {
6452 // Last catch without match jumps here
6453 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6454 isn->isn_arg.jump.jump_where = instr->ga_len;
6455 }
6456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 compile_endblock(cctx);
6458
6459 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6460 return NULL;
6461 return arg;
6462}
6463
6464/*
6465 * compile "throw {expr}"
6466 */
6467 static char_u *
6468compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6469{
6470 char_u *p = skipwhite(arg);
6471
Bram Moolenaara5565e42020-05-09 15:44:01 +02006472 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 return NULL;
6474 if (may_generate_2STRING(-1, cctx) == FAIL)
6475 return NULL;
6476 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6477 return NULL;
6478
6479 return p;
6480}
6481
6482/*
6483 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006484 * compile "echomsg expr"
6485 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006486 * compile "execute expr"
6487 */
6488 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006489compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006490{
6491 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006492 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006493 int count = 0;
6494
6495 for (;;)
6496 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006497 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006498 return NULL;
6499 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006500 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006501 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006502 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006503 break;
6504 }
6505
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006506 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6507 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6508 else if (cmdidx == CMD_execute)
6509 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6510 else if (cmdidx == CMD_echomsg)
6511 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6512 else
6513 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 return p;
6515}
6516
6517/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006518 * :put r
6519 * :put ={expr}
6520 */
6521 static char_u *
6522compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6523{
6524 char_u *line = arg;
6525 linenr_T lnum;
6526 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006527 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006528
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006529 eap->regname = *line;
6530
6531 if (eap->regname == '=')
6532 {
6533 char_u *p = line + 1;
6534
6535 if (compile_expr0(&p, cctx) == FAIL)
6536 return NULL;
6537 line = p;
6538 }
6539 else if (eap->regname != NUL)
6540 ++line;
6541
6542 // TODO: if the range is something like "$" need to evaluate at runtime
6543 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6544 return NULL;
6545 if (eap->addr_count == 0)
6546 lnum = -1;
6547 else
6548 lnum = eap->line2;
6549 if (above)
6550 --lnum;
6551
6552 generate_PUT(cctx, eap->regname, lnum);
6553 return line;
6554}
6555
6556/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006557 * A command that is not compiled, execute with legacy code.
6558 */
6559 static char_u *
6560compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6561{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006562 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006563 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006564 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006565
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006566 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006567 goto theend;
6568
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006569 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006570 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006571 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006572 int usefilter = FALSE;
6573
6574 has_expr = argt & (EX_XFILE | EX_EXPAND);
6575
6576 // If the command can be followed by a bar, find the bar and truncate
6577 // it, so that the following command can be compiled.
6578 // The '|' is overwritten with a NUL, it is put back below.
6579 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6580 && *eap->arg == '!')
6581 // :w !filter or :r !filter or :r! filter
6582 usefilter = TRUE;
6583 if ((argt & EX_TRLBAR) && !usefilter)
6584 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006585 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006586 separate_nextcmd(eap);
6587 if (eap->nextcmd != NULL)
6588 nextcmd = eap->nextcmd;
6589 }
6590 }
6591
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006592 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6593 {
6594 // expand filename in "syntax include [@group] filename"
6595 has_expr = TRUE;
6596 eap->arg = skipwhite(eap->arg + 7);
6597 if (*eap->arg == '@')
6598 eap->arg = skiptowhite(eap->arg);
6599 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006600
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006601 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006602 {
6603 int count = 0;
6604 char_u *start = skipwhite(line);
6605
6606 // :cmd xxx`=expr1`yyy`=expr2`zzz
6607 // PUSHS ":cmd xxx"
6608 // eval expr1
6609 // PUSHS "yyy"
6610 // eval expr2
6611 // PUSHS "zzz"
6612 // EXECCONCAT 5
6613 for (;;)
6614 {
6615 if (p > start)
6616 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006617 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006618 ++count;
6619 }
6620 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006621 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006622 return NULL;
6623 may_generate_2STRING(-1, cctx);
6624 ++count;
6625 p = skipwhite(p);
6626 if (*p != '`')
6627 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006628 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006629 return NULL;
6630 }
6631 start = p + 1;
6632
6633 p = (char_u *)strstr((char *)start, "`=");
6634 if (p == NULL)
6635 {
6636 if (*skipwhite(start) != NUL)
6637 {
6638 generate_PUSHS(cctx, vim_strsave(start));
6639 ++count;
6640 }
6641 break;
6642 }
6643 }
6644 generate_EXECCONCAT(cctx, count);
6645 }
6646 else
6647 generate_EXEC(cctx, line);
6648
6649theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006650 if (*nextcmd != NUL)
6651 {
6652 // the parser expects a pointer to the bar, put it back
6653 --nextcmd;
6654 *nextcmd = '|';
6655 }
6656
6657 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006658}
6659
6660/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006661 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006662 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006663 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006664 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006665add_def_function(ufunc_T *ufunc)
6666{
6667 dfunc_T *dfunc;
6668
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006669 if (def_functions.ga_len == 0)
6670 {
6671 // The first position is not used, so that a zero uf_dfunc_idx means it
6672 // wasn't set.
6673 if (ga_grow(&def_functions, 1) == FAIL)
6674 return FAIL;
6675 ++def_functions.ga_len;
6676 }
6677
Bram Moolenaar09689a02020-05-09 22:50:08 +02006678 // Add the function to "def_functions".
6679 if (ga_grow(&def_functions, 1) == FAIL)
6680 return FAIL;
6681 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6682 CLEAR_POINTER(dfunc);
6683 dfunc->df_idx = def_functions.ga_len;
6684 ufunc->uf_dfunc_idx = dfunc->df_idx;
6685 dfunc->df_ufunc = ufunc;
6686 ++def_functions.ga_len;
6687 return OK;
6688}
6689
6690/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 * After ex_function() has collected all the function lines: parse and compile
6692 * the lines into instructions.
6693 * Adds the function to "def_functions".
6694 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6695 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006696 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006697 * This can be used recursively through compile_lambda(), which may reallocate
6698 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006699 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006701 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006702compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 char_u *line = NULL;
6705 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 cctx_T cctx;
6708 garray_T *instr;
6709 int called_emsg_before = called_emsg;
6710 int ret = FAIL;
6711 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006712 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006713 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006714 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006716 // When using a function that was compiled before: Free old instructions.
6717 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006718 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006720 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6721 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006722 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006724 else
6725 {
6726 if (add_def_function(ufunc) == FAIL)
6727 return FAIL;
6728 new_def_function = TRUE;
6729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730
Bram Moolenaar985116a2020-07-12 17:31:09 +02006731 ufunc->uf_def_status = UF_COMPILING;
6732
Bram Moolenaara80faa82020-04-12 19:37:17 +02006733 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 cctx.ctx_ufunc = ufunc;
6735 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006736 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6738 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6739 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6740 cctx.ctx_type_list = &ufunc->uf_type_list;
6741 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6742 instr = &cctx.ctx_instr;
6743
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006744 // Set the context to the function, it may be compiled when called from
6745 // another script. Set the script version to the most modern one.
6746 // The line number will be set in next_line_from_context().
6747 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6749
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006750 // Make sure error messages are OK.
6751 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6752 if (do_estack_push)
6753 estack_push_ufunc(ufunc, 1);
6754
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006755 if (ufunc->uf_def_args.ga_len > 0)
6756 {
6757 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006758 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006759 int i;
6760 char_u *arg;
6761 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006762 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006763
6764 // Produce instructions for the default values of optional arguments.
6765 // Store the instruction index in uf_def_arg_idx[] so that we know
6766 // where to start when the function is called, depending on the number
6767 // of arguments.
6768 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6769 if (ufunc->uf_def_arg_idx == NULL)
6770 goto erret;
6771 for (i = 0; i < count; ++i)
6772 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006773 garray_T *stack = &cctx.ctx_type_stack;
6774 type_T *val_type;
6775 int arg_idx = first_def_arg + i;
6776
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006777 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6778 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006779 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006780 goto erret;
6781
6782 // If no type specified use the type of the default value.
6783 // Otherwise check that the default value type matches the
6784 // specified type.
6785 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6786 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006787 {
6788 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006789 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006790 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006791 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6792 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006793 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006794
6795 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006796 goto erret;
6797 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006798 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006799
6800 if (did_set_arg_type)
6801 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006802 }
6803
6804 /*
6805 * Loop over all the lines of the function and generate instructions.
6806 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807 for (;;)
6808 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006809 exarg_T ea;
6810 cmdmod_T save_cmdmod;
6811 int starts_with_colon = FALSE;
6812 char_u *cmd;
6813 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006814
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006815 // Bail out on the first error to avoid a flood of errors and report
6816 // the right line number when inside try/catch.
6817 if (emsg_before != called_emsg)
6818 goto erret;
6819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 if (line != NULL && *line == '|')
6821 // the line continues after a '|'
6822 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006823 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006824 && !(*line == '#' && (line == cctx.ctx_line_start
6825 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006827 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 goto erret;
6829 }
6830 else
6831 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006832 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006833 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006834 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006837 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838
Bram Moolenaara80faa82020-04-12 19:37:17 +02006839 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 ea.cmdlinep = &line;
6841 ea.cmd = skipwhite(line);
6842
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006843 // Some things can be recognized by the first character.
6844 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006846 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006847 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006848 if (ea.cmd[1] != '{')
6849 {
6850 line = (char_u *)"";
6851 continue;
6852 }
6853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006855 case '}':
6856 {
6857 // "}" ends a block scope
6858 scopetype_T stype = cctx.ctx_scope == NULL
6859 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006861 if (stype == BLOCK_SCOPE)
6862 {
6863 compile_endblock(&cctx);
6864 line = ea.cmd;
6865 }
6866 else
6867 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006868 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006869 goto erret;
6870 }
6871 if (line != NULL)
6872 line = skipwhite(ea.cmd + 1);
6873 continue;
6874 }
6875
6876 case '{':
6877 // "{" starts a block scope
6878 // "{'a': 1}->func() is something else
6879 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6880 {
6881 line = compile_block(ea.cmd, &cctx);
6882 continue;
6883 }
6884 break;
6885
6886 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006887 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006888 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 }
6890
6891 /*
6892 * COMMAND MODIFIERS
6893 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006894 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6896 {
6897 if (errormsg != NULL)
6898 goto erret;
6899 // empty line or comment
6900 line = (char_u *)"";
6901 continue;
6902 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006903 // TODO: use modifiers in the command
6904 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006905 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906
6907 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006908 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006910 {
6911 if (*ea.cmd == '(')
6912 // not for "call()"
6913 ea.cmd = p;
6914 else
6915 ea.cmd = skipwhite(ea.cmd);
6916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006918 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006920 char_u *pskip;
6921
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006922 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006923 // find what follows.
6924 // Skip over "var.member", "var[idx]" and the like.
6925 // Also "&opt = val", "$ENV = val" and "@r = val".
6926 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006927 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006928 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006929 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006931 char_u *var_end;
6932 int oplen;
6933 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934
Bram Moolenaar65821722020-08-02 18:58:54 +02006935 if (ea.cmd[0] == '@')
6936 var_end = ea.cmd + 2;
6937 else
6938 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006939 FNE_CHECK_START | FNE_INCL_BR);
6940 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006941 if (oplen > 0)
6942 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006943 size_t len = p - ea.cmd;
6944
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006945 // Recognize an assignment if we recognize the variable
6946 // name:
6947 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006948 // "local = expr" where "local" is a local var.
6949 // "script = expr" where "script" is a script-local var.
6950 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006951 // "&opt = expr"
6952 // "$ENV = expr"
6953 // "@r = expr"
6954 if (*ea.cmd == '&'
6955 || *ea.cmd == '$'
6956 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006957 || ((len) > 2 && ea.cmd[1] == ':')
6958 || lookup_local(ea.cmd, len, &cctx) != NULL
6959 || lookup_arg(ea.cmd, len, NULL, NULL,
6960 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006961 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006962 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006963 {
6964 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006965 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006966 goto erret;
6967 continue;
6968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 }
6970 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971
6972 if (*ea.cmd == '[')
6973 {
6974 // [var, var] = expr
6975 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6976 if (line == NULL)
6977 goto erret;
6978 if (line != ea.cmd)
6979 continue;
6980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 }
6982
6983 /*
6984 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006985 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006987 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006988 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006989 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006990 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006991 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006992 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006993 if (!starts_with_colon)
6994 {
6995 emsg(_(e_colon_required_before_a_range));
6996 goto erret;
6997 }
6998 if (ends_excmd2(line, ea.cmd))
6999 {
7000 // A range without a command: jump to the line.
7001 // TODO: compile to a more efficient command, possibly
7002 // calling parse_cmd_address().
7003 ea.cmdidx = CMD_SIZE;
7004 line = compile_exec(line, &ea, &cctx);
7005 goto nextline;
7006 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007007 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007008 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007009 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007010 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007011 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012
7013 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7014 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007015 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007016 {
7017 line += STRLEN(line);
7018 continue;
7019 }
7020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007022 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007024 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007025 iemsg("Command from find_ex_command() not handled");
7026 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 }
7029
Bram Moolenaar3988f642020-08-27 22:43:03 +02007030 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007031 && ea.cmdidx != CMD_elseif
7032 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007033 && ea.cmdidx != CMD_endif
7034 && ea.cmdidx != CMD_endfor
7035 && ea.cmdidx != CMD_endwhile
7036 && ea.cmdidx != CMD_catch
7037 && ea.cmdidx != CMD_finally
7038 && ea.cmdidx != CMD_endtry)
7039 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007040 emsg(_(e_unreachable_code_after_return));
7041 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007042 }
7043
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007044 p = skipwhite(p);
7045 if (ea.cmdidx != CMD_SIZE
7046 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7047 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007048 if (ea.cmdidx >= 0)
7049 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007050 if ((ea.argt & EX_BANG) && *p == '!')
7051 {
7052 ea.forceit = TRUE;
7053 p = skipwhite(p + 1);
7054 }
7055 }
7056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 switch (ea.cmdidx)
7058 {
7059 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007060 ea.arg = p;
7061 line = compile_nested_function(&ea, &cctx);
7062 break;
7063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007065 // TODO: should we allow this, e.g. to declare a global
7066 // function?
7067 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 goto erret;
7069
7070 case CMD_return:
7071 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007072 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 break;
7074
7075 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007076 if (get_vim_var_nr(VV_DISALLOW_LET))
7077 {
7078 emsg(_(e_cannot_use_let_in_vim9_script));
7079 break;
7080 }
7081 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007082 case CMD_var:
7083 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 case CMD_const:
7085 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007086 if (line == p)
7087 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 break;
7089
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007090 case CMD_unlet:
7091 case CMD_unlockvar:
7092 case CMD_lockvar:
7093 line = compile_unletlock(p, &ea, &cctx);
7094 break;
7095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 case CMD_import:
7097 line = compile_import(p, &cctx);
7098 break;
7099
7100 case CMD_if:
7101 line = compile_if(p, &cctx);
7102 break;
7103 case CMD_elseif:
7104 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007105 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 break;
7107 case CMD_else:
7108 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007109 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 break;
7111 case CMD_endif:
7112 line = compile_endif(p, &cctx);
7113 break;
7114
7115 case CMD_while:
7116 line = compile_while(p, &cctx);
7117 break;
7118 case CMD_endwhile:
7119 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007120 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 break;
7122
7123 case CMD_for:
7124 line = compile_for(p, &cctx);
7125 break;
7126 case CMD_endfor:
7127 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 break;
7130 case CMD_continue:
7131 line = compile_continue(p, &cctx);
7132 break;
7133 case CMD_break:
7134 line = compile_break(p, &cctx);
7135 break;
7136
7137 case CMD_try:
7138 line = compile_try(p, &cctx);
7139 break;
7140 case CMD_catch:
7141 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007142 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 break;
7144 case CMD_finally:
7145 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007146 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 break;
7148 case CMD_endtry:
7149 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007150 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 break;
7152 case CMD_throw:
7153 line = compile_throw(p, &cctx);
7154 break;
7155
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007156 case CMD_eval:
7157 if (compile_expr0(&p, &cctx) == FAIL)
7158 goto erret;
7159
Bram Moolenaar3988f642020-08-27 22:43:03 +02007160 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007161 generate_instr_drop(&cctx, ISN_DROP, 1);
7162
7163 line = skipwhite(p);
7164 break;
7165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007168 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007169 case CMD_echomsg:
7170 case CMD_echoerr:
7171 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007172 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007174 case CMD_put:
7175 ea.cmd = cmd;
7176 line = compile_put(p, &ea, &cctx);
7177 break;
7178
Bram Moolenaar3988f642020-08-27 22:43:03 +02007179 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007180
Bram Moolenaarae616492020-07-28 20:07:27 +02007181 case CMD_append:
7182 case CMD_change:
7183 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007184 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007185 case CMD_xit:
7186 not_in_vim9(&ea);
7187 goto erret;
7188
Bram Moolenaar002262f2020-07-08 17:47:57 +02007189 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007190 if (cctx.ctx_skip != SKIP_YES)
7191 {
7192 semsg(_(e_invalid_command_str), ea.cmd);
7193 goto erret;
7194 }
7195 // We don't check for a next command here.
7196 line = (char_u *)"";
7197 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007200 if (cctx.ctx_skip == SKIP_YES)
7201 {
7202 // We don't check for a next command here.
7203 line = (char_u *)"";
7204 }
7205 else
7206 {
7207 // Not recognized, execute with do_cmdline_cmd().
7208 ea.arg = p;
7209 line = compile_exec(line, &ea, &cctx);
7210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 break;
7212 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007213nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 if (line == NULL)
7215 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007216 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217
7218 if (cctx.ctx_type_stack.ga_len < 0)
7219 {
7220 iemsg("Type stack underflow");
7221 goto erret;
7222 }
7223 }
7224
7225 if (cctx.ctx_scope != NULL)
7226 {
7227 if (cctx.ctx_scope->se_type == IF_SCOPE)
7228 emsg(_(e_endif));
7229 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7230 emsg(_(e_endwhile));
7231 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7232 emsg(_(e_endfor));
7233 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007234 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 goto erret;
7236 }
7237
Bram Moolenaarefd88552020-06-18 20:50:10 +02007238 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 {
7240 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007242 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 goto erret;
7244 }
7245
7246 // Return zero if there is no return at the end.
7247 generate_PUSHNR(&cctx, 0);
7248 generate_instr(&cctx, ISN_RETURN);
7249 }
7250
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007251 {
7252 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7253 + ufunc->uf_dfunc_idx;
7254 dfunc->df_deleted = FALSE;
7255 dfunc->df_instr = instr->ga_data;
7256 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007257 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007258 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007259 if (cctx.ctx_outer_used)
7260 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007261 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263
7264 ret = OK;
7265
7266erret:
7267 if (ret == FAIL)
7268 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007269 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007270 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7271 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007272
7273 for (idx = 0; idx < instr->ga_len; ++idx)
7274 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007276
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007277 // If using the last entry in the table and it was added above, we
7278 // might as well remove it.
7279 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007280 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007281 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007282 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007283 ufunc->uf_dfunc_idx = 0;
7284 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007285 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007286
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007287 while (cctx.ctx_scope != NULL)
7288 drop_scope(&cctx);
7289
Bram Moolenaar20431c92020-03-20 18:39:46 +01007290 // Don't execute this function body.
7291 ga_clear_strings(&ufunc->uf_lines);
7292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 if (errormsg != NULL)
7294 emsg(errormsg);
7295 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007296 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 }
7298
7299 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007300 if (do_estack_push)
7301 estack_pop();
7302
Bram Moolenaar20431c92020-03-20 18:39:46 +01007303 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007304 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007306 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307}
7308
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007309 void
7310set_function_type(ufunc_T *ufunc)
7311{
7312 int varargs = ufunc->uf_va_name != NULL;
7313 int argcount = ufunc->uf_args.ga_len;
7314
7315 // Create a type for the function, with the return type and any
7316 // argument types.
7317 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7318 // The type is included in "tt_args".
7319 if (argcount > 0 || varargs)
7320 {
7321 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7322 argcount, &ufunc->uf_type_list);
7323 // Add argument types to the function type.
7324 if (func_type_add_arg_types(ufunc->uf_func_type,
7325 argcount + varargs,
7326 &ufunc->uf_type_list) == FAIL)
7327 return;
7328 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7329 ufunc->uf_func_type->tt_min_argcount =
7330 argcount - ufunc->uf_def_args.ga_len;
7331 if (ufunc->uf_arg_types == NULL)
7332 {
7333 int i;
7334
7335 // lambda does not have argument types.
7336 for (i = 0; i < argcount; ++i)
7337 ufunc->uf_func_type->tt_args[i] = &t_any;
7338 }
7339 else
7340 mch_memmove(ufunc->uf_func_type->tt_args,
7341 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7342 if (varargs)
7343 {
7344 ufunc->uf_func_type->tt_args[argcount] =
7345 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7346 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7347 }
7348 }
7349 else
7350 // No arguments, can use a predefined type.
7351 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7352 argcount, &ufunc->uf_type_list);
7353}
7354
7355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356/*
7357 * Delete an instruction, free what it contains.
7358 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007359 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360delete_instr(isn_T *isn)
7361{
7362 switch (isn->isn_type)
7363 {
7364 case ISN_EXEC:
7365 case ISN_LOADENV:
7366 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007367 case ISN_LOADB:
7368 case ISN_LOADW:
7369 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007371 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 case ISN_PUSHEXC:
7373 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007374 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007376 case ISN_STOREB:
7377 case ISN_STOREW:
7378 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007379 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 vim_free(isn->isn_arg.string);
7381 break;
7382
7383 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007384 case ISN_STORES:
7385 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 break;
7387
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007388 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007389 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007390 vim_free(isn->isn_arg.unlet.ul_name);
7391 break;
7392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 case ISN_STOREOPT:
7394 vim_free(isn->isn_arg.storeopt.so_name);
7395 break;
7396
7397 case ISN_PUSHBLOB: // push blob isn_arg.blob
7398 blob_unref(isn->isn_arg.blob);
7399 break;
7400
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007401 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007402#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007403 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007404#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007405 break;
7406
7407 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007408#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007409 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007410#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007411 break;
7412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 case ISN_UCALL:
7414 vim_free(isn->isn_arg.ufunc.cuf_name);
7415 break;
7416
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007417 case ISN_FUNCREF:
7418 {
7419 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7420 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007421
7422 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7423 func_ptr_unref(dfunc->df_ufunc);
7424 }
7425 break;
7426
7427 case ISN_DCALL:
7428 {
7429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7430 + isn->isn_arg.dfunc.cdf_idx;
7431
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007432 if (dfunc->df_ufunc != NULL
7433 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007434 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007435 }
7436 break;
7437
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007438 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007439 {
7440 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7441 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7442
7443 if (ufunc != NULL)
7444 {
7445 // Clear uf_dfunc_idx so that the function is deleted.
7446 clear_def_function(ufunc);
7447 ufunc->uf_dfunc_idx = 0;
7448 func_ptr_unref(ufunc);
7449 }
7450
7451 vim_free(lambda);
7452 vim_free(isn->isn_arg.newfunc.nf_global);
7453 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007454 break;
7455
Bram Moolenaar5e654232020-09-16 15:22:00 +02007456 case ISN_CHECKTYPE:
7457 free_type(isn->isn_arg.type.ct_type);
7458 break;
7459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 case ISN_2BOOL:
7461 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007462 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 case ISN_ADDBLOB:
7464 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007465 case ISN_ANYINDEX:
7466 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 case ISN_BCALL:
7468 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007469 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 case ISN_COMPAREANY:
7472 case ISN_COMPAREBLOB:
7473 case ISN_COMPAREBOOL:
7474 case ISN_COMPAREDICT:
7475 case ISN_COMPAREFLOAT:
7476 case ISN_COMPAREFUNC:
7477 case ISN_COMPARELIST:
7478 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 case ISN_COMPARESPECIAL:
7480 case ISN_COMPARESTRING:
7481 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007482 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 case ISN_DROP:
7484 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007485 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007486 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007488 case ISN_EXECCONCAT:
7489 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007491 case ISN_GETITEM:
7492 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007493 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007494 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007496 case ISN_LOADBDICT:
7497 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007498 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007500 case ISN_LOADSCRIPT:
7501 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007503 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007504 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007505 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 case ISN_NEGATENR:
7507 case ISN_NEWDICT:
7508 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007510 case ISN_OPFLOAT:
7511 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007513 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007514 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_PUSHF:
7516 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007518 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007520 case ISN_SHUFFLE:
7521 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007523 case ISN_STOREDICT:
7524 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007525 case ISN_STORENR:
7526 case ISN_STOREOUTER:
7527 case ISN_STOREREG:
7528 case ISN_STORESCRIPT:
7529 case ISN_STOREV:
7530 case ISN_STRINDEX:
7531 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 case ISN_THROW:
7533 case ISN_TRY:
7534 // nothing allocated
7535 break;
7536 }
7537}
7538
7539/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007540 * Free all instructions for "dfunc".
7541 */
7542 static void
7543delete_def_function_contents(dfunc_T *dfunc)
7544{
7545 int idx;
7546
7547 ga_clear(&dfunc->df_def_args_isn);
7548
7549 if (dfunc->df_instr != NULL)
7550 {
7551 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7552 delete_instr(dfunc->df_instr + idx);
7553 VIM_CLEAR(dfunc->df_instr);
7554 }
7555
7556 dfunc->df_deleted = TRUE;
7557}
7558
7559/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007560 * When a user function is deleted, clear the contents of any associated def
7561 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 */
7563 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007564clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007566 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 {
7568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7569 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570
Bram Moolenaar20431c92020-03-20 18:39:46 +01007571 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007572 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 }
7574}
7575
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007576/*
7577 * Used when a user function is about to be deleted: remove the pointer to it.
7578 * The entry in def_functions is then unused.
7579 */
7580 void
7581unlink_def_function(ufunc_T *ufunc)
7582{
7583 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7584
7585 dfunc->df_ufunc = NULL;
7586}
7587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007589/*
7590 * Free all functions defined with ":def".
7591 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 void
7593free_def_functions(void)
7594{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007595 int idx;
7596
7597 for (idx = 0; idx < def_functions.ga_len; ++idx)
7598 {
7599 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7600
7601 delete_def_function_contents(dfunc);
7602 }
7603
7604 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605}
7606#endif
7607
7608
7609#endif // FEAT_EVAL