blob: 7ae47b393deb189cffcae64108e4bbc3f6ed1aff [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 Moolenaarfbbcd002020-10-15 12:46:44 +0200198arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 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.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200250 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200251 == 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 Moolenaarfbbcd002020-10-15 12:46:44 +0200262 * Lookup a script-local variable in the current script, possibly defined in a
263 * block that contains the function "cctx->ctx_ufunc".
264 * "cctx" is NULL at the script level.
265 * if "len" is <= 0 "name" must be NUL terminated.
266 * Return NULL when not found.
267 */
268 static sallvar_T *
269find_script_var(char_u *name, size_t len, cctx_T *cctx)
270{
271 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
272 hashitem_T *hi;
273 int cc;
274 sallvar_T *sav;
275 ufunc_T *ufunc;
276
277 // Find the list of all script variables with the right name.
278 if (len > 0)
279 {
280 cc = name[len];
281 name[len] = NUL;
282 }
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (len > 0)
285 name[len] = cc;
286 if (HASHITEM_EMPTY(hi))
287 return NULL;
288
289 sav = HI2SAV(hi);
290 if (sav->sav_block_id == 0 || cctx == NULL)
291 // variable defined in the script scope or not in a function.
292 return sav;
293
294 // Go over the variables with this name and find one that was visible
295 // from the function.
296 ufunc = cctx->ctx_ufunc;
297 while (sav != NULL)
298 {
299 int idx;
300
301 // Go over the blocks that this function was defined in. If the
302 // variable block ID matches it was visible to the function.
303 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
304 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
305 return sav;
306 sav = sav->sav_next;
307 }
308
309 return NULL;
310}
311
312/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200313 * Returnd TRUE if the script context is Vim9 script.
314 */
315 static int
316script_is_vim9()
317{
318 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
319}
320
321/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200322 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200323 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
324 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200325 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 * Returns OK or FAIL.
327 */
328 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200333 if (current_sctx.sc_sid <= 0)
334 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 is_vim9_script = script_is_vim9();
336 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200337 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200338 if (is_vim9_script)
339 {
340 // Check script variables that were visible where the function was
341 // defined.
342 if (find_script_var(name, len, cctx) != NULL)
343 return OK;
344 }
345 else
346 {
347 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
348 dictitem_T *di;
349 int cc;
350
351 // Check script variables that are currently visible
352 cc = name[len];
353 name[len] = NUL;
354 di = find_var_in_ht(ht, 0, name, TRUE);
355 name[len] = cc;
356 if (di != NULL)
357 return OK;
358 }
359
360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361}
362
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100363/*
364 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200365 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200366 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100367 * Return FAIL and give an error if it defined.
368 */
369 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200370check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200372 int c = p[len];
373 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200374
375 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100377 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200378 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200380 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200381 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100382 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 // A local or script-local function can shadow a global function.
384 if (ufunc == NULL || !func_is_global(ufunc)
385 || (p[0] == 'g' && p[1] == ':'))
386 {
387 p[len] = c;
388 semsg(_(e_name_already_defined_str), p);
389 return FAIL;
390 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100391 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200392 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 return OK;
394}
395
Bram Moolenaar65b95452020-07-19 14:03:09 +0200396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397/////////////////////////////////////////////////////////////////////
398// Following generate_ functions expect the caller to call ga_grow().
399
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200400#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
401#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403/*
404 * Generate an instruction without arguments.
405 * Returns a pointer to the new instruction, NULL if failed.
406 */
407 static isn_T *
408generate_instr(cctx_T *cctx, isntype_T isn_type)
409{
410 garray_T *instr = &cctx->ctx_instr;
411 isn_T *isn;
412
Bram Moolenaar080457c2020-03-03 21:53:32 +0100413 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (ga_grow(instr, 1) == FAIL)
415 return NULL;
416 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
417 isn->isn_type = isn_type;
418 isn->isn_lnum = cctx->ctx_lnum + 1;
419 ++instr->ga_len;
420
421 return isn;
422}
423
424/*
425 * Generate an instruction without arguments.
426 * "drop" will be removed from the stack.
427 * Returns a pointer to the new instruction, NULL if failed.
428 */
429 static isn_T *
430generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
431{
432 garray_T *stack = &cctx->ctx_type_stack;
433
Bram Moolenaar080457c2020-03-03 21:53:32 +0100434 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 stack->ga_len -= drop;
436 return generate_instr(cctx, isn_type);
437}
438
439/*
440 * Generate instruction "isn_type" and put "type" on the type stack.
441 */
442 static isn_T *
443generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
444{
445 isn_T *isn;
446 garray_T *stack = &cctx->ctx_type_stack;
447
448 if ((isn = generate_instr(cctx, isn_type)) == NULL)
449 return NULL;
450
451 if (ga_grow(stack, 1) == FAIL)
452 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200453 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 ++stack->ga_len;
455
456 return isn;
457}
458
459/*
460 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200461 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 */
463 static int
464may_generate_2STRING(int offset, cctx_T *cctx)
465{
466 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200467 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 garray_T *stack = &cctx->ctx_type_stack;
469 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
470
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200471 switch ((*type)->tt_type)
472 {
473 // nothing to be done
474 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200476 // conversion possible
477 case VAR_SPECIAL:
478 case VAR_BOOL:
479 case VAR_NUMBER:
480 case VAR_FLOAT:
481 break;
482
483 // conversion possible (with runtime check)
484 case VAR_ANY:
485 case VAR_UNKNOWN:
486 isntype = ISN_2STRING_ANY;
487 break;
488
489 // conversion not possible
490 case VAR_VOID:
491 case VAR_BLOB:
492 case VAR_FUNC:
493 case VAR_PARTIAL:
494 case VAR_LIST:
495 case VAR_DICT:
496 case VAR_JOB:
497 case VAR_CHANNEL:
498 to_string_error((*type)->tt_type);
499 return FAIL;
500 }
501
502 *type = &t_string;
503 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return FAIL;
505 isn->isn_arg.number = offset;
506
507 return OK;
508}
509
510 static int
511check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
512{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200513 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 {
517 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200518 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 return FAIL;
522 }
523 return OK;
524}
525
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200526 static int
527generate_add_instr(
528 cctx_T *cctx,
529 vartype_T vartype,
530 type_T *type1,
531 type_T *type2)
532{
533 isn_T *isn = generate_instr_drop(cctx,
534 vartype == VAR_NUMBER ? ISN_OPNR
535 : vartype == VAR_LIST ? ISN_ADDLIST
536 : vartype == VAR_BLOB ? ISN_ADDBLOB
537#ifdef FEAT_FLOAT
538 : vartype == VAR_FLOAT ? ISN_OPFLOAT
539#endif
540 : ISN_OPANY, 1);
541
542 if (vartype != VAR_LIST && vartype != VAR_BLOB
543 && type1->tt_type != VAR_ANY
544 && type2->tt_type != VAR_ANY
545 && check_number_or_float(
546 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
547 return FAIL;
548
549 if (isn != NULL)
550 isn->isn_arg.op.op_type = EXPR_ADD;
551 return isn == NULL ? FAIL : OK;
552}
553
554/*
555 * Get the type to use for an instruction for an operation on "type1" and
556 * "type2". If they are matching use a type-specific instruction. Otherwise
557 * fall back to runtime type checking.
558 */
559 static vartype_T
560operator_type(type_T *type1, type_T *type2)
561{
562 if (type1->tt_type == type2->tt_type
563 && (type1->tt_type == VAR_NUMBER
564 || type1->tt_type == VAR_LIST
565#ifdef FEAT_FLOAT
566 || type1->tt_type == VAR_FLOAT
567#endif
568 || type1->tt_type == VAR_BLOB))
569 return type1->tt_type;
570 return VAR_ANY;
571}
572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573/*
574 * Generate an instruction with two arguments. The instruction depends on the
575 * type of the arguments.
576 */
577 static int
578generate_two_op(cctx_T *cctx, char_u *op)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581 type_T *type1;
582 type_T *type2;
583 vartype_T vartype;
584 isn_T *isn;
585
Bram Moolenaar080457c2020-03-03 21:53:32 +0100586 RETURN_OK_IF_SKIP(cctx);
587
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200588 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
590 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200591 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
593 switch (*op)
594 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200595 case '+':
596 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 break;
599
600 case '-':
601 case '*':
602 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
603 op) == FAIL)
604 return FAIL;
605 if (vartype == VAR_NUMBER)
606 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
607#ifdef FEAT_FLOAT
608 else if (vartype == VAR_FLOAT)
609 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
610#endif
611 else
612 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = *op == '*'
615 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
616 break;
617
Bram Moolenaar4c683752020-04-05 21:38:23 +0200618 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type2->tt_type != VAR_NUMBER))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 isn = generate_instr_drop(cctx,
627 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
628 if (isn != NULL)
629 isn->isn_arg.op.op_type = EXPR_REM;
630 break;
631 }
632
633 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 {
636 type_T *type = &t_any;
637
638#ifdef FEAT_FLOAT
639 // float+number and number+float results in float
640 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
641 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
642 type = &t_float;
643#endif
644 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
645 }
646
647 return OK;
648}
649
650/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651 * Get the instruction to use for comparing "type1" with "type2"
652 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200654 static isntype_T
655get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656{
657 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
Bram Moolenaar4c683752020-04-05 21:38:23 +0200659 if (type1 == VAR_UNKNOWN)
660 type1 = VAR_ANY;
661 if (type2 == VAR_UNKNOWN)
662 type2 = VAR_ANY;
663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 if (type1 == type2)
665 {
666 switch (type1)
667 {
668 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
669 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
670 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
671 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
672 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
673 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
674 case VAR_LIST: isntype = ISN_COMPARELIST; break;
675 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
676 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 default: isntype = ISN_COMPAREANY; break;
678 }
679 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
682 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
683 isntype = ISN_COMPAREANY;
684
685 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
686 && (isntype == ISN_COMPAREBOOL
687 || isntype == ISN_COMPARESPECIAL
688 || isntype == ISN_COMPARENR
689 || isntype == ISN_COMPAREFLOAT))
690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200691 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200693 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 }
695 if (isntype == ISN_DROP
696 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
697 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
698 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
699 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
700 && exptype != EXPR_IS && exptype != EXPR_ISNOT
701 && (type1 == VAR_BLOB || type2 == VAR_BLOB
702 || type1 == VAR_LIST || type2 == VAR_LIST))))
703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200704 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200706 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return isntype;
709}
710
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200711 int
712check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
713{
714 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
715 return FAIL;
716 return OK;
717}
718
Bram Moolenaara5565e42020-05-09 15:44:01 +0200719/*
720 * Generate an ISN_COMPARE* instruction with a boolean result.
721 */
722 static int
723generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
724{
725 isntype_T isntype;
726 isn_T *isn;
727 garray_T *stack = &cctx->ctx_type_stack;
728 vartype_T type1;
729 vartype_T type2;
730
731 RETURN_OK_IF_SKIP(cctx);
732
733 // Get the known type of the two items on the stack. If they are matching
734 // use a type-specific instruction. Otherwise fall back to runtime type
735 // checking.
736 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
737 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
738 isntype = get_compare_isn(exptype, type1, type2);
739 if (isntype == ISN_DROP)
740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 if ((isn = generate_instr(cctx, isntype)) == NULL)
743 return FAIL;
744 isn->isn_arg.op.op_type = exptype;
745 isn->isn_arg.op.op_ic = ic;
746
747 // takes two arguments, puts one bool back
748 if (stack->ga_len >= 2)
749 {
750 --stack->ga_len;
751 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
752 }
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_2BOOL instruction.
759 */
760 static int
761generate_2BOOL(cctx_T *cctx, int invert)
762{
763 isn_T *isn;
764 garray_T *stack = &cctx->ctx_type_stack;
765
Bram Moolenaar080457c2020-03-03 21:53:32 +0100766 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
768 return FAIL;
769 isn->isn_arg.number = invert;
770
771 // type becomes bool
772 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
773
774 return OK;
775}
776
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200777/*
778 * Generate an ISN_COND2BOOL instruction.
779 */
780 static int
781generate_COND2BOOL(cctx_T *cctx)
782{
783 isn_T *isn;
784 garray_T *stack = &cctx->ctx_type_stack;
785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
788 return FAIL;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200797generate_TYPECHECK(
798 cctx_T *cctx,
799 type_T *expected,
800 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
Bram Moolenaar080457c2020-03-03 21:53:32 +0100805 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
807 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200808 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 isn->isn_arg.type.ct_off = offset;
810
Bram Moolenaar5e654232020-09-16 15:22:00 +0200811 // type becomes expected
812 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 return OK;
815}
816
817/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200818 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
819 * used. Return FALSE if the types will never match.
820 */
821 static int
822use_typecheck(type_T *actual, type_T *expected)
823{
824 if (actual->tt_type == VAR_ANY
825 || actual->tt_type == VAR_UNKNOWN
826 || (actual->tt_type == VAR_FUNC
827 && (expected->tt_type == VAR_FUNC
828 || expected->tt_type == VAR_PARTIAL)
829 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
830 return TRUE;
831 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
832 && actual->tt_type == expected->tt_type)
833 // This takes care of a nested list or dict.
834 return use_typecheck(actual->tt_member, expected->tt_member);
835 return FALSE;
836}
837
838/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200839 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200840 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * - "actual" is a type that can be "expected" type: add a runtime check; or
842 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200843 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
844 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200845 */
846 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200847need_type(
848 type_T *actual,
849 type_T *expected,
850 int offset,
851 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200852 int silent,
853 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200854{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200855 if (expected == &t_bool && actual != &t_bool
856 && (actual->tt_flags & TTFLAG_BOOL_OK))
857 {
858 // Using "0", "1" or the result of an expression with "&&" or "||" as a
859 // boolean is OK but requires a conversion.
860 generate_2BOOL(cctx, FALSE);
861 return OK;
862 }
863
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200864 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200865 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200866
867 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200868 // If it's a constant a runtime check makes no sense.
869 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 generate_TYPECHECK(cctx, expected, offset);
872 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200873 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200874
875 if (!silent)
876 type_mismatch(expected, actual);
877 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878}
879
880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 * Generate an ISN_PUSHNR instruction.
882 */
883 static int
884generate_PUSHNR(cctx_T *cctx, varnumber_T number)
885{
886 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar080457c2020-03-03 21:53:32 +0100889 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
891 return FAIL;
892 isn->isn_arg.number = number;
893
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200894 if (number == 0 || number == 1)
895 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200896 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200897
898 // A 0 or 1 number can also be used as a bool.
899 if (type != NULL)
900 {
901 type->tt_type = VAR_NUMBER;
902 type->tt_flags = TTFLAG_BOOL_OK;
903 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
904 }
905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 return OK;
907}
908
909/*
910 * Generate an ISN_PUSHBOOL instruction.
911 */
912 static int
913generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
914{
915 isn_T *isn;
916
Bram Moolenaar080457c2020-03-03 21:53:32 +0100917 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = number;
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_PUSHSPEC instruction.
927 */
928 static int
929generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
935 return FAIL;
936 isn->isn_arg.number = number;
937
938 return OK;
939}
940
941#ifdef FEAT_FLOAT
942/*
943 * Generate an ISN_PUSHF instruction.
944 */
945 static int
946generate_PUSHF(cctx_T *cctx, float_T fnumber)
947{
948 isn_T *isn;
949
Bram Moolenaar080457c2020-03-03 21:53:32 +0100950 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
952 return FAIL;
953 isn->isn_arg.fnumber = fnumber;
954
955 return OK;
956}
957#endif
958
959/*
960 * Generate an ISN_PUSHS instruction.
961 * Consumes "str".
962 */
963 static int
964generate_PUSHS(cctx_T *cctx, char_u *str)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
970 return FAIL;
971 isn->isn_arg.string = str;
972
973 return OK;
974}
975
976/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100977 * Generate an ISN_PUSHCHANNEL instruction.
978 * Consumes "channel".
979 */
980 static int
981generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
982{
983 isn_T *isn;
984
Bram Moolenaar080457c2020-03-03 21:53:32 +0100985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100986 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
987 return FAIL;
988 isn->isn_arg.channel = channel;
989
990 return OK;
991}
992
993/*
994 * Generate an ISN_PUSHJOB instruction.
995 * Consumes "job".
996 */
997 static int
998generate_PUSHJOB(cctx_T *cctx, job_T *job)
999{
1000 isn_T *isn;
1001
Bram Moolenaar080457c2020-03-03 21:53:32 +01001002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001003 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001004 return FAIL;
1005 isn->isn_arg.job = job;
1006
1007 return OK;
1008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Generate an ISN_PUSHBLOB instruction.
1012 * Consumes "blob".
1013 */
1014 static int
1015generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.blob = blob;
1023
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 * Generate an ISN_PUSHFUNC instruction with name "name".
1029 * Consumes "name".
1030 */
1031 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001032generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001037 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001038 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001039 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 * Generate an ISN_GETITEM instruction with "index".
1046 */
1047 static int
1048generate_GETITEM(cctx_T *cctx, int index)
1049{
1050 isn_T *isn;
1051 garray_T *stack = &cctx->ctx_type_stack;
1052 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1053 type_T *item_type = &t_any;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001057 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 emsg(_(e_listreq));
1061 return FAIL;
1062 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001063 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = index;
1067
1068 // add the item type to the type stack
1069 if (ga_grow(stack, 1) == FAIL)
1070 return FAIL;
1071 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1072 ++stack->ga_len;
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001077 * Generate an ISN_SLICE instruction with "count".
1078 */
1079 static int
1080generate_SLICE(cctx_T *cctx, int count)
1081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
1085 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.number = count;
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_CHECKLEN instruction with "min_len".
1093 */
1094 static int
1095generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1096{
1097 isn_T *isn;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.checklen.cl_min_len = min_len;
1104 isn->isn_arg.checklen.cl_more_OK = more_OK;
1105
1106 return OK;
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Generate an ISN_STORE instruction.
1111 */
1112 static int
1113generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1114{
1115 isn_T *isn;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1119 return FAIL;
1120 if (name != NULL)
1121 isn->isn_arg.string = vim_strsave(name);
1122 else
1123 isn->isn_arg.number = idx;
1124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1130 */
1131 static int
1132generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1133{
1134 isn_T *isn;
1135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1138 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001139 isn->isn_arg.storenr.stnr_idx = idx;
1140 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_STOREOPT instruction
1147 */
1148 static int
1149generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1150{
1151 isn_T *isn;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001154 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1157 isn->isn_arg.storeopt.so_flags = opt_flags;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_LOAD or similar instruction.
1164 */
1165 static int
1166generate_LOAD(
1167 cctx_T *cctx,
1168 isntype_T isn_type,
1169 int idx,
1170 char_u *name,
1171 type_T *type)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1177 return FAIL;
1178 if (name != NULL)
1179 isn->isn_arg.string = vim_strsave(name);
1180 else
1181 isn->isn_arg.number = idx;
1182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 */
1189 static int
1190generate_LOADV(
1191 cctx_T *cctx,
1192 char_u *name,
1193 int error)
1194{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001195 int di_flags;
1196 int vidx = find_vim_var(name, &di_flags);
1197 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 if (vidx < 0)
1201 {
1202 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001203 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001204 return FAIL;
1205 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001206 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001207
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001208 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209}
1210
1211/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001212 * Generate an ISN_UNLET instruction.
1213 */
1214 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001215generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001216{
1217 isn_T *isn;
1218
1219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001220 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001221 return FAIL;
1222 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1223 isn->isn_arg.unlet.ul_forceit = forceit;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001229 * Generate an ISN_LOCKCONST instruction.
1230 */
1231 static int
1232generate_LOCKCONST(cctx_T *cctx)
1233{
1234 isn_T *isn;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1238 return FAIL;
1239 return OK;
1240}
1241
1242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 * Generate an ISN_LOADS instruction.
1244 */
1245 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 int sid,
1251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252{
1253 isn_T *isn;
1254
Bram Moolenaar080457c2020-03-03 21:53:32 +01001255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 if (isn_type == ISN_LOADS)
1257 isn = generate_instr_type(cctx, isn_type, type);
1258 else
1259 isn = generate_instr_drop(cctx, isn_type, 1);
1260 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1263 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1270 */
1271 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 cctx_T *cctx,
1274 isntype_T isn_type,
1275 int sid,
1276 int idx,
1277 type_T *type)
1278{
1279 isn_T *isn;
1280
Bram Moolenaar080457c2020-03-03 21:53:32 +01001281 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if (isn_type == ISN_LOADSCRIPT)
1283 isn = generate_instr_type(cctx, isn_type, type);
1284 else
1285 isn = generate_instr_drop(cctx, isn_type, 1);
1286 if (isn == NULL)
1287 return FAIL;
1288 isn->isn_arg.script.script_sid = sid;
1289 isn->isn_arg.script.script_idx = idx;
1290 return OK;
1291}
1292
1293/*
1294 * Generate an ISN_NEWLIST instruction.
1295 */
1296 static int
1297generate_NEWLIST(cctx_T *cctx, int count)
1298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 type_T *type;
1302 type_T *member;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.number = count;
1308
Bram Moolenaar127542b2020-08-09 17:22:04 +02001309 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001310 if (count == 0)
1311 member = &t_void;
1312 else
1313 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001314 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1315 cctx->ctx_type_list);
1316 type = get_list_type(member, cctx->ctx_type_list);
1317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 // drop the value types
1319 stack->ga_len -= count;
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 // add the list type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_NEWDICT instruction.
1332 */
1333 static int
1334generate_NEWDICT(cctx_T *cctx, int count)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 type_T *type;
1339 type_T *member;
1340
Bram Moolenaar080457c2020-03-03 21:53:32 +01001341 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.number = count;
1345
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001346 if (count == 0)
1347 member = &t_void;
1348 else
1349 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001350 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1351 cctx->ctx_type_list);
1352 type = get_dict_type(member, cctx->ctx_type_list);
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // drop the key and value types
1355 stack->ga_len -= 2 * count;
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // add the dict type to the type stack
1358 if (ga_grow(stack, 1) == FAIL)
1359 return FAIL;
1360 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1361 ++stack->ga_len;
1362
1363 return OK;
1364}
1365
1366/*
1367 * Generate an ISN_FUNCREF instruction.
1368 */
1369 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
1372 isn_T *isn;
1373 garray_T *stack = &cctx->ctx_type_stack;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1377 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001378 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001379 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 ((type_T **)stack->ga_data)[stack->ga_len] =
1384 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001391 * Generate an ISN_NEWFUNC instruction.
1392 */
1393 static int
1394generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1395{
1396 isn_T *isn;
1397 char_u *name;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 name = vim_strsave(lambda_name);
1401 if (name == NULL)
1402 return FAIL;
1403 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.newfunc.nf_lambda = name;
1406 isn->isn_arg.newfunc.nf_global = func_name;
1407
1408 return OK;
1409}
1410
1411/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 * Generate an ISN_JUMP instruction.
1413 */
1414 static int
1415generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1416{
1417 isn_T *isn;
1418 garray_T *stack = &cctx->ctx_type_stack;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.jump.jump_when = when;
1424 isn->isn_arg.jump.jump_where = where;
1425
1426 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1427 --stack->ga_len;
1428
1429 return OK;
1430}
1431
1432 static int
1433generate_FOR(cctx_T *cctx, int loop_idx)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
1437
Bram Moolenaar080457c2020-03-03 21:53:32 +01001438 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1440 return FAIL;
1441 isn->isn_arg.forloop.for_idx = loop_idx;
1442
1443 if (ga_grow(stack, 1) == FAIL)
1444 return FAIL;
1445 // type doesn't matter, will be stored next
1446 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1447 ++stack->ga_len;
1448
1449 return OK;
1450}
1451
1452/*
1453 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001454 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 * Return FAIL if the number of arguments is wrong.
1456 */
1457 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001458generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 int argoff;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 type_T **argtypes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001466 argoff = check_internal_func(func_idx, argcount);
1467 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 return FAIL;
1469
Bram Moolenaar389df252020-07-09 21:20:47 +02001470 if (method_call && argoff > 1)
1471 {
1472 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1473 return FAIL;
1474 isn->isn_arg.shuffle.shfl_item = argcount;
1475 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1476 }
1477
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001478 if (argcount > 0)
1479 {
1480 // Check the types of the arguments.
1481 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1482 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001484 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1487 return FAIL;
1488 isn->isn_arg.bfunc.cbf_idx = func_idx;
1489 isn->isn_arg.bfunc.cbf_argcount = argcount;
1490
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 // Drop the argument types and push the return type.
1492 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
1495 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001496 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498
1499 return OK;
1500}
1501
1502/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001503 * Generate an ISN_LISTAPPEND instruction. Works like add().
1504 * Argument count is already checked.
1505 */
1506 static int
1507generate_LISTAPPEND(cctx_T *cctx)
1508{
1509 garray_T *stack = &cctx->ctx_type_stack;
1510 type_T *list_type;
1511 type_T *item_type;
1512 type_T *expected;
1513
1514 // Caller already checked that list_type is a list.
1515 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1516 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1517 expected = list_type->tt_member;
1518 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1519 return FAIL;
1520
1521 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1522 return FAIL;
1523
1524 --stack->ga_len; // drop the argument
1525 return OK;
1526}
1527
1528/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001529 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1530 * Argument count is already checked.
1531 */
1532 static int
1533generate_BLOBAPPEND(cctx_T *cctx)
1534{
1535 garray_T *stack = &cctx->ctx_type_stack;
1536 type_T *item_type;
1537
1538 // Caller already checked that blob_type is a blob.
1539 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1540 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1541 return FAIL;
1542
1543 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1544 return FAIL;
1545
1546 --stack->ga_len; // drop the argument
1547 return OK;
1548}
1549
1550/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 * Generate an ISN_DCALL or ISN_UCALL instruction.
1552 * Return FAIL if the number of arguments is wrong.
1553 */
1554 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001555generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556{
1557 isn_T *isn;
1558 garray_T *stack = &cctx->ctx_type_stack;
1559 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001560 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (argcount > regular_args && !has_varargs(ufunc))
1564 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001565 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 return FAIL;
1567 }
1568 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1569 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001570 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 return FAIL;
1572 }
1573
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001574 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001575 {
1576 int i;
1577
1578 for (i = 0; i < argcount; ++i)
1579 {
1580 type_T *expected;
1581 type_T *actual;
1582
1583 if (i < regular_args)
1584 {
1585 if (ufunc->uf_arg_types == NULL)
1586 continue;
1587 expected = ufunc->uf_arg_types[i];
1588 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001589 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1590 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001591 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001592 else
1593 expected = ufunc->uf_va_type->tt_member;
1594 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001595 if (need_type(actual, expected, -argcount + i, cctx,
1596 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001597 {
1598 arg_type_mismatch(expected, actual, i + 1);
1599 return FAIL;
1600 }
1601 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001602 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001603 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1604 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001605 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001606 }
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001609 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001610 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001612 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 {
1614 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1615 isn->isn_arg.dfunc.cdf_argcount = argcount;
1616 }
1617 else
1618 {
1619 // A user function may be deleted and redefined later, can't use the
1620 // ufunc pointer, need to look it up again at runtime.
1621 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1622 isn->isn_arg.ufunc.cuf_argcount = argcount;
1623 }
1624
1625 stack->ga_len -= argcount; // drop the arguments
1626 if (ga_grow(stack, 1) == FAIL)
1627 return FAIL;
1628 // add return value
1629 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1630 ++stack->ga_len;
1631
1632 return OK;
1633}
1634
1635/*
1636 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1637 */
1638 static int
1639generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1640{
1641 isn_T *isn;
1642 garray_T *stack = &cctx->ctx_type_stack;
1643
Bram Moolenaar080457c2020-03-03 21:53:32 +01001644 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1646 return FAIL;
1647 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1648 isn->isn_arg.ufunc.cuf_argcount = argcount;
1649
1650 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001651 if (ga_grow(stack, 1) == FAIL)
1652 return FAIL;
1653 // add return value
1654 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1655 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656
1657 return OK;
1658}
1659
1660/*
1661 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001662 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 */
1664 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001665generate_PCALL(
1666 cctx_T *cctx,
1667 int argcount,
1668 char_u *name,
1669 type_T *type,
1670 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671{
1672 isn_T *isn;
1673 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001674 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675
Bram Moolenaar080457c2020-03-03 21:53:32 +01001676 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001677
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001678 if (type->tt_type == VAR_ANY)
1679 ret_type = &t_any;
1680 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001681 {
1682 if (type->tt_argcount != -1)
1683 {
1684 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1685
1686 if (argcount < type->tt_min_argcount - varargs)
1687 {
1688 semsg(_(e_toofewarg), "[reference]");
1689 return FAIL;
1690 }
1691 if (!varargs && argcount > type->tt_argcount)
1692 {
1693 semsg(_(e_toomanyarg), "[reference]");
1694 return FAIL;
1695 }
1696 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001697 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001698 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001699 else
1700 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001701 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001702 return FAIL;
1703 }
1704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.pfunc.cpf_top = at_top;
1708 isn->isn_arg.pfunc.cpf_argcount = argcount;
1709
1710 stack->ga_len -= argcount; // drop the arguments
1711
1712 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001713 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001715 // If partial is above the arguments it must be cleared and replaced with
1716 // the return value.
1717 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1718 return FAIL;
1719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 return OK;
1721}
1722
1723/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001724 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 */
1726 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001727generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728{
1729 isn_T *isn;
1730 garray_T *stack = &cctx->ctx_type_stack;
1731 type_T *type;
1732
Bram Moolenaar080457c2020-03-03 21:53:32 +01001733 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001734 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001736 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001738 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001740 if (type->tt_type != VAR_DICT && type != &t_any)
1741 {
1742 emsg(_(e_dictreq));
1743 return FAIL;
1744 }
1745 // change dict type to dict member type
1746 if (type->tt_type == VAR_DICT)
1747 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
1749 return OK;
1750}
1751
1752/*
1753 * Generate an ISN_ECHO instruction.
1754 */
1755 static int
1756generate_ECHO(cctx_T *cctx, int with_white, int count)
1757{
1758 isn_T *isn;
1759
Bram Moolenaar080457c2020-03-03 21:53:32 +01001760 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1762 return FAIL;
1763 isn->isn_arg.echo.echo_with_white = with_white;
1764 isn->isn_arg.echo.echo_count = count;
1765
1766 return OK;
1767}
1768
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001770 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001771 */
1772 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001773generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001774{
1775 isn_T *isn;
1776
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001777 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001778 return FAIL;
1779 isn->isn_arg.number = count;
1780
1781 return OK;
1782}
1783
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001784/*
1785 * Generate an ISN_PUT instruction.
1786 */
1787 static int
1788generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1789{
1790 isn_T *isn;
1791
1792 RETURN_OK_IF_SKIP(cctx);
1793 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1794 return FAIL;
1795 isn->isn_arg.put.put_regname = regname;
1796 isn->isn_arg.put.put_lnum = lnum;
1797 return OK;
1798}
1799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 static int
1801generate_EXEC(cctx_T *cctx, char_u *line)
1802{
1803 isn_T *isn;
1804
Bram Moolenaar080457c2020-03-03 21:53:32 +01001805 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1807 return FAIL;
1808 isn->isn_arg.string = vim_strsave(line);
1809 return OK;
1810}
1811
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001812 static int
1813generate_EXECCONCAT(cctx_T *cctx, int count)
1814{
1815 isn_T *isn;
1816
1817 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.number = count;
1820 return OK;
1821}
1822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823/*
1824 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001825 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001827 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001828reserve_local(
1829 cctx_T *cctx,
1830 char_u *name,
1831 size_t len,
1832 int isConst,
1833 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 lvar_T *lvar;
1836
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001837 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001839 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001840 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842
1843 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001844 return NULL;
1845 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001846 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001848 // Every local variable uses the next entry on the stack. We could re-use
1849 // the last ones when leaving a scope, but then variables used in a closure
1850 // might get overwritten. To keep things simple do not re-use stack
1851 // entries. This is less efficient, but memory is cheap these days.
1852 lvar->lv_idx = cctx->ctx_locals_count++;
1853
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001854 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 lvar->lv_const = isConst;
1856 lvar->lv_type = type;
1857
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001858 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859}
1860
1861/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001862 * Remove local variables above "new_top".
1863 */
1864 static void
1865unwind_locals(cctx_T *cctx, int new_top)
1866{
1867 if (cctx->ctx_locals.ga_len > new_top)
1868 {
1869 int idx;
1870 lvar_T *lvar;
1871
1872 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1873 {
1874 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1875 vim_free(lvar->lv_name);
1876 }
1877 }
1878 cctx->ctx_locals.ga_len = new_top;
1879}
1880
1881/*
1882 * Free all local variables.
1883 */
1884 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001885free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001886{
1887 unwind_locals(cctx, 0);
1888 ga_clear(&cctx->ctx_locals);
1889}
1890
1891/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 * Find "name" in script-local items of script "sid".
1893 * Returns the index in "sn_var_vals" if found.
1894 * If found but not in "sn_var_vals" returns -1.
1895 * If not found returns -2.
1896 */
1897 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001898get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899{
1900 hashtab_T *ht;
1901 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001902 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001903 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 int idx;
1905
Bram Moolenaare3d46852020-08-29 13:39:17 +02001906 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001908 if (sid == current_sctx.sc_sid)
1909 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001910 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001911
1912 if (sav == NULL)
1913 return -2;
1914 idx = sav->sav_var_vals_idx;
1915 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1916 if (check_writable && sv->sv_const)
1917 semsg(_(e_readonlyvar), name);
1918 return idx;
1919 }
1920
1921 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 ht = &SCRIPT_VARS(sid);
1923 di = find_var_in_ht(ht, 0, name, TRUE);
1924 if (di == NULL)
1925 return -2;
1926
1927 // Now find the svar_T index in sn_var_vals.
1928 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1929 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001930 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 if (sv->sv_tv == &di->di_tv)
1932 {
1933 if (check_writable && sv->sv_const)
1934 semsg(_(e_readonlyvar), name);
1935 return idx;
1936 }
1937 }
1938 return -1;
1939}
1940
1941/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001942 * Find "name" in imported items of the current script or in "cctx" if not
1943 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 */
1945 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001946find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 int idx;
1949
Bram Moolenaare3d46852020-08-29 13:39:17 +02001950 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001951 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 if (cctx != NULL)
1953 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1954 {
1955 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1956 + idx;
1957
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001958 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1959 : STRLEN(import->imp_name) == len
1960 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 return import;
1962 }
1963
Bram Moolenaarefa94442020-08-08 22:16:00 +02001964 return find_imported_in_script(name, len, current_sctx.sc_sid);
1965}
1966
1967 imported_T *
1968find_imported_in_script(char_u *name, size_t len, int sid)
1969{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001970 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001971 int idx;
1972
Bram Moolenaare3d46852020-08-29 13:39:17 +02001973 if (!SCRIPT_ID_VALID(sid))
1974 return NULL;
1975 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1977 {
1978 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1979
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001980 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1981 : STRLEN(import->imp_name) == len
1982 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 return import;
1984 }
1985 return NULL;
1986}
1987
1988/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001989 * Free all imported variables.
1990 */
1991 static void
1992free_imported(cctx_T *cctx)
1993{
1994 int idx;
1995
1996 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1997 {
1998 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1999
2000 vim_free(import->imp_name);
2001 }
2002 ga_clear(&cctx->ctx_imports);
2003}
2004
2005/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002006 * Return TRUE if "p" points at a "#" but not at "#{".
2007 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002008 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002009vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002010{
2011 return p[0] == '#' && p[1] != '{';
2012}
2013
2014/*
2015 * Return a pointer to the next line that isn't empty or only contains a
2016 * comment. Skips over white space.
2017 * Returns NULL if there is none.
2018 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002019 char_u *
2020peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002021{
2022 int lnum = cctx->ctx_lnum;
2023
2024 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2025 {
2026 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002027 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002028
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002029 // ignore NULLs inserted for continuation lines
2030 if (line != NULL)
2031 {
2032 p = skipwhite(line);
2033 if (*p != NUL && !vim9_comment_start(p))
2034 return p;
2035 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002036 }
2037 return NULL;
2038}
2039
2040/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002041 * Called when checking for a following operator at "arg". When the rest of
2042 * the line is empty or only a comment, peek the next line. If there is a next
2043 * line return a pointer to it and set "nextp".
2044 * Otherwise skip over white space.
2045 */
2046 static char_u *
2047may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2048{
2049 char_u *p = skipwhite(arg);
2050
2051 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002052 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002053 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002054 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002055 if (*nextp != NULL)
2056 return *nextp;
2057 }
2058 return p;
2059}
2060
2061/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002062 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002063 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002064 * Returns NULL when at the end.
2065 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002066 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002067next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002068{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002069 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002070
2071 do
2072 {
2073 ++cctx->ctx_lnum;
2074 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002075 {
2076 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002077 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002078 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002079 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002080 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002081 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002082 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002083 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002084 return line;
2085}
2086
2087/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002088 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002089 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002090 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2091 */
2092 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002093may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002094{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002095 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002096 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002097 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002098
2099 if (next == NULL)
2100 return FAIL;
2101 *arg = skipwhite(next);
2102 }
2103 return OK;
2104}
2105
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002106/*
2107 * Idem, and give an error when failed.
2108 */
2109 static int
2110may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2111{
2112 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2113 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002114 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002115 return FAIL;
2116 }
2117 return OK;
2118}
2119
2120
Bram Moolenaara5565e42020-05-09 15:44:01 +02002121// Structure passed between the compile_expr* functions to keep track of
2122// constants that have been parsed but for which no code was produced yet. If
2123// possible expressions on these constants are applied at compile time. If
2124// that is not possible, the code to push the constants needs to be generated
2125// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002126// Using 50 should be more than enough of 5 levels of ().
2127#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002128typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002129 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002130 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002131 int pp_is_const; // all generated code was constants, used for a
2132 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002133} ppconst_T;
2134
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002135static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002136static int compile_expr0(char_u **arg, cctx_T *cctx);
2137static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2138
Bram Moolenaara5565e42020-05-09 15:44:01 +02002139/*
2140 * Generate a PUSH instruction for "tv".
2141 * "tv" will be consumed or cleared.
2142 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2143 */
2144 static int
2145generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2146{
2147 if (tv != NULL)
2148 {
2149 switch (tv->v_type)
2150 {
2151 case VAR_UNKNOWN:
2152 break;
2153 case VAR_BOOL:
2154 generate_PUSHBOOL(cctx, tv->vval.v_number);
2155 break;
2156 case VAR_SPECIAL:
2157 generate_PUSHSPEC(cctx, tv->vval.v_number);
2158 break;
2159 case VAR_NUMBER:
2160 generate_PUSHNR(cctx, tv->vval.v_number);
2161 break;
2162#ifdef FEAT_FLOAT
2163 case VAR_FLOAT:
2164 generate_PUSHF(cctx, tv->vval.v_float);
2165 break;
2166#endif
2167 case VAR_BLOB:
2168 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2169 tv->vval.v_blob = NULL;
2170 break;
2171 case VAR_STRING:
2172 generate_PUSHS(cctx, tv->vval.v_string);
2173 tv->vval.v_string = NULL;
2174 break;
2175 default:
2176 iemsg("constant type not supported");
2177 clear_tv(tv);
2178 return FAIL;
2179 }
2180 tv->v_type = VAR_UNKNOWN;
2181 }
2182 return OK;
2183}
2184
2185/*
2186 * Generate code for any ppconst entries.
2187 */
2188 static int
2189generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2190{
2191 int i;
2192 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002193 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002194
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002195 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002196 for (i = 0; i < ppconst->pp_used; ++i)
2197 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2198 ret = FAIL;
2199 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002200 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002201 return ret;
2202}
2203
2204/*
2205 * Clear ppconst constants. Used when failing.
2206 */
2207 static void
2208clear_ppconst(ppconst_T *ppconst)
2209{
2210 int i;
2211
2212 for (i = 0; i < ppconst->pp_used; ++i)
2213 clear_tv(&ppconst->pp_tv[i]);
2214 ppconst->pp_used = 0;
2215}
2216
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002217/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002218 * Generate an instruction to load script-local variable "name", without the
2219 * leading "s:".
2220 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 */
2222 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002223compile_load_scriptvar(
2224 cctx_T *cctx,
2225 char_u *name, // variable NUL terminated
2226 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002227 char_u **end, // end of variable
2228 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002230 scriptitem_T *si;
2231 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 imported_T *import;
2233
Bram Moolenaare3d46852020-08-29 13:39:17 +02002234 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2235 return FAIL;
2236 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002237 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002238 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002240 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002241 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2242 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 }
2244 if (idx >= 0)
2245 {
2246 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2247
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002248 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 current_sctx.sc_sid, idx, sv->sv_type);
2250 return OK;
2251 }
2252
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002253 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 if (import != NULL)
2255 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002256 if (import->imp_all)
2257 {
2258 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002259 char_u *exp_name;
2260 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002261 ufunc_T *ufunc;
2262 type_T *type;
2263
2264 // Used "import * as Name", need to lookup the member.
2265 if (*p != '.')
2266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002267 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002268 return FAIL;
2269 }
2270 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002271 if (VIM_ISWHITE(*p))
2272 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002273 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002274 return FAIL;
2275 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002276
Bram Moolenaar1c991142020-07-04 13:15:31 +02002277 // isolate one name
2278 exp_name = p;
2279 while (eval_isnamec(*p))
2280 ++p;
2281 cc = *p;
2282 *p = NUL;
2283
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002284 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002285 *p = cc;
2286 p = skipwhite(p);
2287
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002288 // TODO: what if it is a function?
2289 if (idx < 0)
2290 return FAIL;
2291 *end = p;
2292
2293 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2294 import->imp_sid,
2295 idx,
2296 type);
2297 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002298 else if (import->imp_funcname != NULL)
2299 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002300 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002301 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2302 import->imp_sid,
2303 import->imp_var_vals_idx,
2304 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return OK;
2306 }
2307
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002308 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002309 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 return FAIL;
2311}
2312
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002313 static int
2314generate_funcref(cctx_T *cctx, char_u *name)
2315{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002316 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002317
2318 if (ufunc == NULL)
2319 return FAIL;
2320
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002321 // Need to compile any default values to get the argument types.
2322 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2323 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2324 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002325 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002326}
2327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328/*
2329 * Compile a variable name into a load instruction.
2330 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002331 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 * When "error" is FALSE do not give an error when not found.
2333 */
2334 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002335compile_load(
2336 char_u **arg,
2337 char_u *end_arg,
2338 cctx_T *cctx,
2339 int is_expr,
2340 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341{
2342 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002343 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002344 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002346 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
2348 if (*(*arg + 1) == ':')
2349 {
2350 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002351 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002352 {
2353 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002355 switch (**arg)
2356 {
2357 case 'g': isn_type = ISN_LOADGDICT; break;
2358 case 'w': isn_type = ISN_LOADWDICT; break;
2359 case 't': isn_type = ISN_LOADTDICT; break;
2360 case 'b': isn_type = ISN_LOADBDICT; break;
2361 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002362 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002363 goto theend;
2364 }
2365 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2366 goto theend;
2367 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 else
2370 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002371 isntype_T isn_type = ISN_DROP;
2372
2373 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2374 if (name == NULL)
2375 return FAIL;
2376
2377 switch (**arg)
2378 {
2379 case 'v': res = generate_LOADV(cctx, name, error);
2380 break;
2381 case 's': res = compile_load_scriptvar(cctx, name,
2382 NULL, NULL, error);
2383 break;
2384 case 'g': isn_type = ISN_LOADG; break;
2385 case 'w': isn_type = ISN_LOADW; break;
2386 case 't': isn_type = ISN_LOADT; break;
2387 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002388 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002389 goto theend;
2390 }
2391 if (isn_type != ISN_DROP)
2392 {
2393 // Global, Buffer-local, Window-local and Tabpage-local
2394 // variables can be defined later, thus we don't check if it
2395 // exists, give error at runtime.
2396 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 }
2399 }
2400 else
2401 {
2402 size_t len = end - *arg;
2403 int idx;
2404 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002405 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406
2407 name = vim_strnsave(*arg, end - *arg);
2408 if (name == NULL)
2409 return FAIL;
2410
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002411 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002413 if (!gen_load_outer)
2414 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 }
2416 else
2417 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002418 lvar_T *lvar = lookup_local(*arg, len, cctx);
2419
2420 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002422 type = lvar->lv_type;
2423 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002424 if (lvar->lv_from_outer)
2425 gen_load_outer = TRUE;
2426 else
2427 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429 else
2430 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002432 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002433 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002434 || find_imported(name, 0, cctx) != NULL)
2435 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002436
Bram Moolenaar0f769812020-09-12 18:32:34 +02002437 // When evaluating an expression and the name starts with an
2438 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002439 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002440 if (res == FAIL && is_expr
2441 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002442 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 }
2444 }
2445 if (gen_load)
2446 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002447 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002448 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002449 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002450 cctx->ctx_outer_used = TRUE;
2451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 }
2453
2454 *arg = end;
2455
2456theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002457 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002458 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 vim_free(name);
2460 return res;
2461}
2462
2463/*
2464 * Compile the argument expressions.
2465 * "arg" points to just after the "(" and is advanced to after the ")"
2466 */
2467 static int
2468compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2469{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002470 char_u *p = *arg;
2471 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002472 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002476 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2477 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002478 if (*p == ')')
2479 {
2480 *arg = p + 1;
2481 return OK;
2482 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002483 if (must_end)
2484 {
2485 semsg(_(e_missing_comma_before_argument_str), p);
2486 return FAIL;
2487 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002488
Bram Moolenaara5565e42020-05-09 15:44:01 +02002489 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 return FAIL;
2491 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002492
2493 if (*p != ',' && *skipwhite(p) == ',')
2494 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002495 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002496 p = skipwhite(p);
2497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002499 {
2500 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002501 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002502 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002503 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002504 else
2505 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002506 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002507 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002509failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002510 emsg(_(e_missing_close));
2511 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512}
2513
2514/*
2515 * Compile a function call: name(arg1, arg2)
2516 * "arg" points to "name", "arg + varlen" to the "(".
2517 * "argcount_init" is 1 for "value->method()"
2518 * Instructions:
2519 * EVAL arg1
2520 * EVAL arg2
2521 * BCALL / DCALL / UCALL
2522 */
2523 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002524compile_call(
2525 char_u **arg,
2526 size_t varlen,
2527 cctx_T *cctx,
2528 ppconst_T *ppconst,
2529 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
2531 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002532 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 int argcount = argcount_init;
2534 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002535 char_u fname_buf[FLEN_FIXED + 1];
2536 char_u *tofree = NULL;
2537 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002539 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002540 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541
Bram Moolenaara5565e42020-05-09 15:44:01 +02002542 // we can evaluate "has('name')" at compile time
2543 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2544 {
2545 char_u *s = skipwhite(*arg + varlen + 1);
2546 typval_T argvars[2];
2547
2548 argvars[0].v_type = VAR_UNKNOWN;
2549 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002550 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002551 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002552 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002553 s = skipwhite(s);
2554 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2555 {
2556 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2557
2558 *arg = s + 1;
2559 argvars[1].v_type = VAR_UNKNOWN;
2560 tv->v_type = VAR_NUMBER;
2561 tv->vval.v_number = 0;
2562 f_has(argvars, tv);
2563 clear_tv(&argvars[0]);
2564 ++ppconst->pp_used;
2565 return OK;
2566 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002567 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568 }
2569
2570 if (generate_ppconst(cctx, ppconst) == FAIL)
2571 return FAIL;
2572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 if (varlen >= sizeof(namebuf))
2574 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002575 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 return FAIL;
2577 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002578 vim_strncpy(namebuf, *arg, varlen);
2579 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
2581 *arg = skipwhite(*arg + varlen + 1);
2582 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
Bram Moolenaara1773442020-08-12 15:21:22 +02002585 is_autoload = vim_strchr(name, '#') != NULL;
2586 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 {
2588 int idx;
2589
2590 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002591 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002593 {
2594 if (STRCMP(name, "add") == 0 && argcount == 2)
2595 {
2596 garray_T *stack = &cctx->ctx_type_stack;
2597 type_T *type = ((type_T **)stack->ga_data)[
2598 stack->ga_len - 2];
2599
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002600 if (type->tt_type == VAR_LIST)
2601 {
2602 // inline "add(list, item)" so that the type can be checked
2603 res = generate_LISTAPPEND(cctx);
2604 idx = -1;
2605 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002606 else if (type->tt_type == VAR_BLOB)
2607 {
2608 // inline "add(blob, nr)" so that the type can be checked
2609 res = generate_BLOBAPPEND(cctx);
2610 idx = -1;
2611 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002612 }
2613
2614 if (idx >= 0)
2615 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2616 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002617 else
2618 semsg(_(e_unknownfunc), namebuf);
2619 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 }
2621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002623 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002624 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002625 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002626 {
2627 res = generate_CALL(cctx, ufunc, argcount);
2628 goto theend;
2629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630
2631 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002632 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002633 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002635 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002636 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002637 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002638 garray_T *stack = &cctx->ctx_type_stack;
2639 type_T *type;
2640
2641 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2642 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002643 goto theend;
2644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645
Bram Moolenaar0f769812020-09-12 18:32:34 +02002646 // If we can find a global function by name generate the right call.
2647 if (ufunc != NULL)
2648 {
2649 res = generate_CALL(cctx, ufunc, argcount);
2650 goto theend;
2651 }
2652
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002653 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002654 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002655 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002656 res = generate_UCALL(cctx, name, argcount);
2657 else
2658 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002659
2660theend:
2661 vim_free(tofree);
2662 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663}
2664
2665// like NAMESPACE_CHAR but with 'a' and 'l'.
2666#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2667
2668/*
2669 * Find the end of a variable or function name. Unlike find_name_end() this
2670 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002671 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 * Return a pointer to just after the name. Equal to "arg" if there is no
2673 * valid name.
2674 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002675 static char_u *
2676to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677{
2678 char_u *p;
2679
2680 // Quick check for valid starting character.
2681 if (!eval_isnamec1(*arg))
2682 return arg;
2683
2684 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2685 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2686 // and can be used in slice "[n:]".
2687 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002688 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2690 break;
2691 return p;
2692}
2693
2694/*
2695 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002696 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 */
2698 char_u *
2699to_name_const_end(char_u *arg)
2700{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002701 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 typval_T rettv;
2703
2704 if (p == arg && *arg == '[')
2705 {
2706
2707 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002708 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 p = arg;
2710 }
2711 else if (p == arg && *arg == '#' && arg[1] == '{')
2712 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002713 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002715 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 p = arg;
2717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718
2719 return p;
2720}
2721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722/*
2723 * parse a list: [expr, expr]
2724 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002725 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 */
2727 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002728compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729{
2730 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002731 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002733 int is_const;
2734 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002736 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002738 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002739 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002740 semsg(_(e_list_end), *arg);
2741 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002742 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002743 if (*p == ',')
2744 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002746 return FAIL;
2747 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002748 if (*p == ']')
2749 {
2750 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002751 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002752 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002753 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002754 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002755 if (!is_const)
2756 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 ++count;
2758 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002759 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002761 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2762 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002763 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002764 return FAIL;
2765 }
2766 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002767 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 p = skipwhite(p);
2769 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002770 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002772 ppconst->pp_is_const = is_all_const;
2773 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774}
2775
2776/*
2777 * parse a lambda: {arg, arg -> expr}
2778 * "*arg" points to the '{'.
2779 */
2780 static int
2781compile_lambda(char_u **arg, cctx_T *cctx)
2782{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 typval_T rettv;
2784 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002785 evalarg_T evalarg;
2786
2787 CLEAR_FIELD(evalarg);
2788 evalarg.eval_flags = EVAL_EVALUATE;
2789 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790
2791 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002792 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002793 {
2794 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002796 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002799 ++ufunc->uf_refcount;
2800 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002801 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802
2803 // The function will have one line: "return {expr}".
2804 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002805 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002807 clear_evalarg(&evalarg, NULL);
2808
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002809 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002810 {
2811 // The return type will now be known.
2812 set_function_type(ufunc);
2813
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002814 // The function reference count will be 1. When the ISN_FUNCREF
2815 // instruction is deleted the reference count is decremented and the
2816 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002817 return generate_FUNCREF(cctx, ufunc);
2818 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002819
2820 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 return FAIL;
2822}
2823
2824/*
2825 * Compile a lamda call: expr->{lambda}(args)
2826 * "arg" points to the "{".
2827 */
2828 static int
2829compile_lambda_call(char_u **arg, cctx_T *cctx)
2830{
2831 ufunc_T *ufunc;
2832 typval_T rettv;
2833 int argcount = 1;
2834 int ret = FAIL;
2835
2836 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002837 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 return FAIL;
2839
2840 if (**arg != '(')
2841 {
2842 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002843 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 else
2845 semsg(_(e_missing_paren), "lambda");
2846 clear_tv(&rettv);
2847 return FAIL;
2848 }
2849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 ufunc = rettv.vval.v_partial->pt_func;
2851 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002852 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002853 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002854
Bram Moolenaara05e5242020-09-19 18:19:19 +02002855 // The function will have one line: "return {expr}". Compile it into
2856 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002857 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858
2859 // compile the arguments
2860 *arg = skipwhite(*arg + 1);
2861 if (compile_arguments(arg, cctx, &argcount) == OK)
2862 // call the compiled function
2863 ret = generate_CALL(cctx, ufunc, argcount);
2864
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002865 if (ret == FAIL)
2866 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 return ret;
2868}
2869
2870/*
2871 * parse a dict: {'key': val} or #{key: val}
2872 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002873 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 */
2875 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002876compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877{
2878 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002879 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 int count = 0;
2881 dict_T *d = dict_alloc();
2882 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002883 char_u *whitep = *arg;
2884 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002885 int is_const;
2886 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887
2888 if (d == NULL)
2889 return FAIL;
2890 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002891 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 {
2893 char_u *key = NULL;
2894
Bram Moolenaar23c55272020-06-21 16:58:13 +02002895 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002896 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002897 *arg = NULL;
2898 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002899 }
2900
2901 if (**arg == '}')
2902 break;
2903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 if (literal)
2905 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002906 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907
Bram Moolenaar2c330432020-04-13 14:41:35 +02002908 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002910 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 return FAIL;
2912 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002913 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 if (generate_PUSHS(cctx, key) == FAIL)
2915 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002916 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918 else
2919 {
2920 isn_T *isn;
2921
Bram Moolenaara5565e42020-05-09 15:44:01 +02002922 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2925 if (isn->isn_type == ISN_PUSHS)
2926 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002927 else
2928 {
2929 type_T *keytype = ((type_T **)stack->ga_data)
2930 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002931 if (need_type(keytype, &t_string, -1, cctx,
2932 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002933 return FAIL;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
2936
2937 // Check for duplicate keys, if using string keys.
2938 if (key != NULL)
2939 {
2940 item = dict_find(d, key, -1);
2941 if (item != NULL)
2942 {
2943 semsg(_(e_duplicate_key), key);
2944 goto failret;
2945 }
2946 item = dictitem_alloc(key);
2947 if (item != NULL)
2948 {
2949 item->di_tv.v_type = VAR_UNKNOWN;
2950 item->di_tv.v_lock = 0;
2951 if (dict_add(d, item) == FAIL)
2952 dictitem_free(item);
2953 }
2954 }
2955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 if (**arg != ':')
2957 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002958 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002959 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002960 else
2961 semsg(_(e_missing_dict_colon), *arg);
2962 return FAIL;
2963 }
2964 whitep = *arg + 1;
2965 if (!IS_WHITE_OR_NUL(*whitep))
2966 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002967 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
2969 }
2970
2971 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002972 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002973 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002974 *arg = NULL;
2975 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002976 }
2977
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002978 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002980 if (!is_const)
2981 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 ++count;
2983
Bram Moolenaar2c330432020-04-13 14:41:35 +02002984 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002985 *arg = skipwhite(*arg);
2986 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002987 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002988 *arg = NULL;
2989 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if (**arg == '}')
2992 break;
2993 if (**arg != ',')
2994 {
2995 semsg(_(e_missing_dict_comma), *arg);
2996 goto failret;
2997 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002998 if (IS_WHITE_OR_NUL(*whitep))
2999 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003000 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003001 return FAIL;
3002 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003003 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003004 if (!IS_WHITE_OR_NUL(*whitep))
3005 {
3006 semsg(_(e_white_space_required_after_str), ",");
3007 return FAIL;
3008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 *arg = skipwhite(*arg + 1);
3010 }
3011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 *arg = *arg + 1;
3013
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003015 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003016 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003017 *arg += STRLEN(*arg);
3018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003020 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 return generate_NEWDICT(cctx, count);
3022
3023failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003024 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003025 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003026 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003027 *arg = (char_u *)"";
3028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 dict_unref(d);
3030 return FAIL;
3031}
3032
3033/*
3034 * Compile "&option".
3035 */
3036 static int
3037compile_get_option(char_u **arg, cctx_T *cctx)
3038{
3039 typval_T rettv;
3040 char_u *start = *arg;
3041 int ret;
3042
3043 // parse the option and get the current value to get the type.
3044 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003045 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 if (ret == OK)
3047 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003048 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 char_u *name = vim_strnsave(start, *arg - start);
3050 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3051
3052 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3053 vim_free(name);
3054 }
3055 clear_tv(&rettv);
3056
3057 return ret;
3058}
3059
3060/*
3061 * Compile "$VAR".
3062 */
3063 static int
3064compile_get_env(char_u **arg, cctx_T *cctx)
3065{
3066 char_u *start = *arg;
3067 int len;
3068 int ret;
3069 char_u *name;
3070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 ++*arg;
3072 len = get_env_len(arg);
3073 if (len == 0)
3074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003075 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 return FAIL;
3077 }
3078
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003079 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 name = vim_strnsave(start, len + 1);
3081 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3082 vim_free(name);
3083 return ret;
3084}
3085
3086/*
3087 * Compile "@r".
3088 */
3089 static int
3090compile_get_register(char_u **arg, cctx_T *cctx)
3091{
3092 int ret;
3093
3094 ++*arg;
3095 if (**arg == NUL)
3096 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003097 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 return FAIL;
3099 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003100 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 {
3102 emsg_invreg(**arg);
3103 return FAIL;
3104 }
3105 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3106 ++*arg;
3107 return ret;
3108}
3109
3110/*
3111 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003112 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 */
3114 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003115apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003117 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
3119 // this works from end to start
3120 while (p > start)
3121 {
3122 --p;
3123 if (*p == '-' || *p == '+')
3124 {
3125 // only '-' has an effect, for '+' we only check the type
3126#ifdef FEAT_FLOAT
3127 if (rettv->v_type == VAR_FLOAT)
3128 {
3129 if (*p == '-')
3130 rettv->vval.v_float = -rettv->vval.v_float;
3131 }
3132 else
3133#endif
3134 {
3135 varnumber_T val;
3136 int error = FALSE;
3137
3138 // tv_get_number_chk() accepts a string, but we don't want that
3139 // here
3140 if (check_not_string(rettv) == FAIL)
3141 return FAIL;
3142 val = tv_get_number_chk(rettv, &error);
3143 clear_tv(rettv);
3144 if (error)
3145 return FAIL;
3146 if (*p == '-')
3147 val = -val;
3148 rettv->v_type = VAR_NUMBER;
3149 rettv->vval.v_number = val;
3150 }
3151 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003152 else if (numeric_only)
3153 {
3154 ++p;
3155 break;
3156 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003157 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 {
3159 int v = tv2bool(rettv);
3160
3161 // '!' is permissive in the type.
3162 clear_tv(rettv);
3163 rettv->v_type = VAR_BOOL;
3164 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3165 }
3166 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003167 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 return OK;
3169}
3170
3171/*
3172 * Recognize v: variables that are constants and set "rettv".
3173 */
3174 static void
3175get_vim_constant(char_u **arg, typval_T *rettv)
3176{
3177 if (STRNCMP(*arg, "v:true", 6) == 0)
3178 {
3179 rettv->v_type = VAR_BOOL;
3180 rettv->vval.v_number = VVAL_TRUE;
3181 *arg += 6;
3182 }
3183 else if (STRNCMP(*arg, "v:false", 7) == 0)
3184 {
3185 rettv->v_type = VAR_BOOL;
3186 rettv->vval.v_number = VVAL_FALSE;
3187 *arg += 7;
3188 }
3189 else if (STRNCMP(*arg, "v:null", 6) == 0)
3190 {
3191 rettv->v_type = VAR_SPECIAL;
3192 rettv->vval.v_number = VVAL_NULL;
3193 *arg += 6;
3194 }
3195 else if (STRNCMP(*arg, "v:none", 6) == 0)
3196 {
3197 rettv->v_type = VAR_SPECIAL;
3198 rettv->vval.v_number = VVAL_NONE;
3199 *arg += 6;
3200 }
3201}
3202
Bram Moolenaar696ba232020-07-29 21:20:41 +02003203 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003204get_compare_type(char_u *p, int *len, int *type_is)
3205{
3206 exptype_T type = EXPR_UNKNOWN;
3207 int i;
3208
3209 switch (p[0])
3210 {
3211 case '=': if (p[1] == '=')
3212 type = EXPR_EQUAL;
3213 else if (p[1] == '~')
3214 type = EXPR_MATCH;
3215 break;
3216 case '!': if (p[1] == '=')
3217 type = EXPR_NEQUAL;
3218 else if (p[1] == '~')
3219 type = EXPR_NOMATCH;
3220 break;
3221 case '>': if (p[1] != '=')
3222 {
3223 type = EXPR_GREATER;
3224 *len = 1;
3225 }
3226 else
3227 type = EXPR_GEQUAL;
3228 break;
3229 case '<': if (p[1] != '=')
3230 {
3231 type = EXPR_SMALLER;
3232 *len = 1;
3233 }
3234 else
3235 type = EXPR_SEQUAL;
3236 break;
3237 case 'i': if (p[1] == 's')
3238 {
3239 // "is" and "isnot"; but not a prefix of a name
3240 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3241 *len = 5;
3242 i = p[*len];
3243 if (!isalnum(i) && i != '_')
3244 {
3245 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3246 *type_is = TRUE;
3247 }
3248 }
3249 break;
3250 }
3251 return type;
3252}
3253
3254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003256 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 */
3258 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003259compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003261 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
3263 // this works from end to start
3264 while (p > start)
3265 {
3266 --p;
3267 if (*p == '-' || *p == '+')
3268 {
3269 int negate = *p == '-';
3270 isn_T *isn;
3271
3272 // TODO: check type
3273 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3274 {
3275 --p;
3276 if (*p == '-')
3277 negate = !negate;
3278 }
3279 // only '-' has an effect, for '+' we only check the type
3280 if (negate)
3281 isn = generate_instr(cctx, ISN_NEGATENR);
3282 else
3283 isn = generate_instr(cctx, ISN_CHECKNR);
3284 if (isn == NULL)
3285 return FAIL;
3286 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003287 else if (numeric_only)
3288 {
3289 ++p;
3290 break;
3291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 else
3293 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003294 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003296 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003298 if (p[-1] == '!')
3299 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 }
3302 if (generate_2BOOL(cctx, invert) == FAIL)
3303 return FAIL;
3304 }
3305 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003306 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 return OK;
3308}
3309
3310/*
3311 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003312 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 */
3314 static int
3315compile_subscript(
3316 char_u **arg,
3317 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003318 char_u *start_leader,
3319 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003320 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003322 char_u *name_start = *end_leader;
3323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 for (;;)
3325 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003326 char_u *p = skipwhite(*arg);
3327
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003328 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003329 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003330 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003331
3332 // If a following line starts with "->{" or "->X" advance to that
3333 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003334 // Also if a following line starts with ".x".
3335 if (next != NULL &&
3336 ((next[0] == '-' && next[1] == '>'
3337 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003338 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003339 {
3340 next = next_line_from_context(cctx, TRUE);
3341 if (next == NULL)
3342 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003343 *arg = next;
3344 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003345 }
3346 }
3347
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003348 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3349 // not a function call.
3350 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003352 garray_T *stack = &cctx->ctx_type_stack;
3353 type_T *type;
3354 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003356 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003357 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003358 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003361 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3362
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003363 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3365 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003366 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 return FAIL;
3368 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003369 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003371 char_u *pstart = p;
3372
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003373 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003374 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003375 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 // something->method()
3378 // Apply the '!', '-' and '+' first:
3379 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003380 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003383 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003384 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003385 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 if (**arg == '{')
3387 {
3388 // lambda call: list->{lambda}
3389 if (compile_lambda_call(arg, cctx) == FAIL)
3390 return FAIL;
3391 }
3392 else
3393 {
3394 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003395 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003396 if (!eval_isnamec1(*p))
3397 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003398 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003399 return FAIL;
3400 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003401 if (ASCII_ISALPHA(*p) && p[1] == ':')
3402 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003403 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 ;
3405 if (*p != '(')
3406 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003407 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
3409 }
3410 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003411 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 return FAIL;
3413 }
3414 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003415 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003417 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003418 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003419 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003420 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003421 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003422
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003423 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003424 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003425 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003426 // TODO: blob index
3427 // TODO: more arguments
3428 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003429 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003430 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003431 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003432
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003433 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003434 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003435 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003436 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003437 if (**arg == ':')
3438 // missing first index is equal to zero
3439 generate_PUSHNR(cctx, 0);
3440 else
3441 {
3442 if (compile_expr0(arg, cctx) == FAIL)
3443 return FAIL;
3444 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3445 return FAIL;
3446 *arg = skipwhite(*arg);
3447 }
3448 if (**arg == ':')
3449 {
3450 *arg = skipwhite(*arg + 1);
3451 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3452 return FAIL;
3453 if (**arg == ']')
3454 // missing second index is equal to end of string
3455 generate_PUSHNR(cctx, -1);
3456 else
3457 {
3458 if (compile_expr0(arg, cctx) == FAIL)
3459 return FAIL;
3460 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3461 return FAIL;
3462 *arg = skipwhite(*arg);
3463 }
3464 is_slice = TRUE;
3465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466
3467 if (**arg != ']')
3468 {
3469 emsg(_(e_missbrac));
3470 return FAIL;
3471 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003472 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003474 // We can index a list and a dict. If we don't know the type
3475 // we can use the index value type.
3476 // TODO: If we don't know use an instruction to figure it out at
3477 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003478 typep = ((type_T **)stack->ga_data) + stack->ga_len
3479 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003480 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003481 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3482 // If the index is a string, the variable must be a Dict.
3483 if (*typep == &t_any && valtype == &t_string)
3484 vtype = VAR_DICT;
3485 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003486 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003487 if (need_type(valtype, &t_number, -1, cctx,
3488 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003489 return FAIL;
3490 if (is_slice)
3491 {
3492 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003493 if (need_type(valtype, &t_number, -2, cctx,
3494 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003495 return FAIL;
3496 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003497 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003498
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003499 if (vtype == VAR_DICT)
3500 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003501 if (is_slice)
3502 {
3503 emsg(_(e_cannot_slice_dictionary));
3504 return FAIL;
3505 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003506 if ((*typep)->tt_type == VAR_DICT)
3507 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003508 else
3509 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003510 if (need_type(*typep, &t_dict_any, -2, cctx,
3511 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003512 return FAIL;
3513 *typep = &t_any;
3514 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003515 if (may_generate_2STRING(-1, cctx) == FAIL)
3516 return FAIL;
3517 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3518 return FAIL;
3519 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003520 else if (vtype == VAR_STRING)
3521 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003522 *typep = &t_string;
3523 if ((is_slice
3524 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3525 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003526 return FAIL;
3527 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003528 else if (vtype == VAR_BLOB)
3529 {
3530 emsg("Sorry, blob index and slice not implemented yet");
3531 return FAIL;
3532 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003533 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003534 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003535 if (is_slice)
3536 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003537 if (generate_instr_drop(cctx,
3538 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3539 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003540 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003541 }
Bram Moolenaared591872020-08-15 22:14:53 +02003542 else
3543 {
3544 if ((*typep)->tt_type == VAR_LIST)
3545 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003546 if (generate_instr_drop(cctx,
3547 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3548 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003549 return FAIL;
3550 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003551 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003552 else
3553 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003554 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003555 return FAIL;
3556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003558 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003560 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003561 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003562 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003563 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003564
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003565 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003566 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003567 {
3568 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003569 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003570 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003571 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003572 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 while (eval_isnamec(*p))
3574 MB_PTR_ADV(p);
3575 if (p == *arg)
3576 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003577 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 return FAIL;
3579 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003580 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 return FAIL;
3582 *arg = p;
3583 }
3584 else
3585 break;
3586 }
3587
3588 // TODO - see handle_subscript():
3589 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3590 // Don't do this when "Func" is already a partial that was bound
3591 // explicitly (pt_auto is FALSE).
3592
3593 return OK;
3594}
3595
3596/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003597 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3598 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003600 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003601 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003602 *
3603 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 */
3605
3606/*
3607 * number number constant
3608 * 0zFFFFFFFF Blob constant
3609 * "string" string constant
3610 * 'string' literal string constant
3611 * &option-name option value
3612 * @r register contents
3613 * identifier variable value
3614 * function() function call
3615 * $VAR environment variable
3616 * (expression) nested expression
3617 * [expr, expr] List
3618 * {key: val, key: val} Dictionary
3619 * #{key: val, key: val} Dictionary with literal keys
3620 *
3621 * Also handle:
3622 * ! in front logical NOT
3623 * - in front unary minus
3624 * + in front unary plus (ignored)
3625 * trailing (arg) funcref/partial call
3626 * trailing [] subscript in String or List
3627 * trailing .name entry in Dictionary
3628 * trailing ->name() method call
3629 */
3630 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003631compile_expr7(
3632 char_u **arg,
3633 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003634 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 char_u *start_leader, *end_leader;
3637 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003638 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003639 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003641 ppconst->pp_is_const = FALSE;
3642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 /*
3644 * Skip '!', '-' and '+' characters. They are handled later.
3645 */
3646 start_leader = *arg;
3647 while (**arg == '!' || **arg == '-' || **arg == '+')
3648 *arg = skipwhite(*arg + 1);
3649 end_leader = *arg;
3650
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003651 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 switch (**arg)
3653 {
3654 /*
3655 * Number constant.
3656 */
3657 case '0': // also for blob starting with 0z
3658 case '1':
3659 case '2':
3660 case '3':
3661 case '4':
3662 case '5':
3663 case '6':
3664 case '7':
3665 case '8':
3666 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003667 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003669 // Apply "-" and "+" just before the number now, right to
3670 // left. Matters especially when "->" follows. Stops at
3671 // '!'.
3672 if (apply_leader(rettv, TRUE,
3673 start_leader, &end_leader) == FAIL)
3674 {
3675 clear_tv(rettv);
3676 return FAIL;
3677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 break;
3679
3680 /*
3681 * String constant: "string".
3682 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003683 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 return FAIL;
3685 break;
3686
3687 /*
3688 * Literal string constant: 'str''ing'.
3689 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003690 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 return FAIL;
3692 break;
3693
3694 /*
3695 * Constant Vim variable.
3696 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003697 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 ret = NOTDONE;
3699 break;
3700
3701 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003702 * "true" constant
3703 */
3704 case 't': if (STRNCMP(*arg, "true", 4) == 0
3705 && !eval_isnamec((*arg)[4]))
3706 {
3707 *arg += 4;
3708 rettv->v_type = VAR_BOOL;
3709 rettv->vval.v_number = VVAL_TRUE;
3710 }
3711 else
3712 ret = NOTDONE;
3713 break;
3714
3715 /*
3716 * "false" constant
3717 */
3718 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3719 && !eval_isnamec((*arg)[5]))
3720 {
3721 *arg += 5;
3722 rettv->v_type = VAR_BOOL;
3723 rettv->vval.v_number = VVAL_FALSE;
3724 }
3725 else
3726 ret = NOTDONE;
3727 break;
3728
3729 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 * List: [expr, expr]
3731 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003732 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 break;
3734
3735 /*
3736 * Dictionary: #{key: val, key: val}
3737 */
3738 case '#': if ((*arg)[1] == '{')
3739 {
3740 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003741 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 }
3743 else
3744 ret = NOTDONE;
3745 break;
3746
3747 /*
3748 * Lambda: {arg, arg -> expr}
3749 * Dictionary: {'key': val, 'key': val}
3750 */
3751 case '{': {
3752 char_u *start = skipwhite(*arg + 1);
3753
3754 // Find out what comes after the arguments.
3755 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003756 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (ret != FAIL && *start == '>')
3758 ret = compile_lambda(arg, cctx);
3759 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003760 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 }
3762 break;
3763
3764 /*
3765 * Option value: &name
3766 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003767 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3768 return FAIL;
3769 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 break;
3771
3772 /*
3773 * Environment variable: $VAR.
3774 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003775 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3776 return FAIL;
3777 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 break;
3779
3780 /*
3781 * Register contents: @r.
3782 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003783 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3784 return FAIL;
3785 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 break;
3787 /*
3788 * nested expression: (expression).
3789 */
3790 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003791
3792 // recursive!
3793 if (ppconst->pp_used <= PPSIZE - 10)
3794 {
3795 ret = compile_expr1(arg, cctx, ppconst);
3796 }
3797 else
3798 {
3799 // Not enough space in ppconst, flush constants.
3800 if (generate_ppconst(cctx, ppconst) == FAIL)
3801 return FAIL;
3802 ret = compile_expr0(arg, cctx);
3803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 *arg = skipwhite(*arg);
3805 if (**arg == ')')
3806 ++*arg;
3807 else if (ret == OK)
3808 {
3809 emsg(_(e_missing_close));
3810 ret = FAIL;
3811 }
3812 break;
3813
3814 default: ret = NOTDONE;
3815 break;
3816 }
3817 if (ret == FAIL)
3818 return FAIL;
3819
Bram Moolenaar1c747212020-05-09 18:28:34 +02003820 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003822 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003823 clear_tv(rettv);
3824 else
3825 // A constant expression can possibly be handled compile time,
3826 // return the value instead of generating code.
3827 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 }
3829 else if (ret == NOTDONE)
3830 {
3831 char_u *p;
3832 int r;
3833
3834 if (!eval_isnamec1(**arg))
3835 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003836 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 return FAIL;
3838 }
3839
3840 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003841 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003843 {
3844 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003847 {
3848 if (generate_ppconst(cctx, ppconst) == FAIL)
3849 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003850 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 if (r == FAIL)
3853 return FAIL;
3854 }
3855
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003856 // Handle following "[]", ".member", etc.
3857 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003858 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003859 ppconst) == FAIL)
3860 return FAIL;
3861 if (ppconst->pp_used > 0)
3862 {
3863 // apply the '!', '-' and '+' before the constant
3864 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003865 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003866 return FAIL;
3867 return OK;
3868 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003869 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003871 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872}
3873
3874/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003875 * Give the "white on both sides" error, taking the operator from "p[len]".
3876 */
3877 void
3878error_white_both(char_u *op, int len)
3879{
3880 char_u buf[10];
3881
3882 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003883 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003884}
3885
3886/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003887 * <type>expr7: runtime type check / conversion
3888 */
3889 static int
3890compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3891{
3892 type_T *want_type = NULL;
3893
3894 // Recognize <type>
3895 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3896 {
3897 int called_emsg_before = called_emsg;
3898
3899 ++*arg;
3900 want_type = parse_type(arg, cctx->ctx_type_list);
3901 if (called_emsg != called_emsg_before)
3902 return FAIL;
3903
3904 if (**arg != '>')
3905 {
3906 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003907 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003908 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003909 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003910 return FAIL;
3911 }
3912 ++*arg;
3913 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3914 return FAIL;
3915 }
3916
3917 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3918 return FAIL;
3919
3920 if (want_type != NULL)
3921 {
3922 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003923 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003924
Bram Moolenaard1103582020-08-14 22:44:25 +02003925 generate_ppconst(cctx, ppconst);
3926 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003927 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003928 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003929 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003930 return FAIL;
3931 }
3932 }
3933
3934 return OK;
3935}
3936
3937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 * * number multiplication
3939 * / number division
3940 * % number modulo
3941 */
3942 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003943compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944{
3945 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003946 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003947 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003950 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 return FAIL;
3952
3953 /*
3954 * Repeat computing, until no "*", "/" or "%" is following.
3955 */
3956 for (;;)
3957 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003958 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 if (*op != '*' && *op != '/' && *op != '%')
3960 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003961 if (next != NULL)
3962 {
3963 *arg = next_line_from_context(cctx, TRUE);
3964 op = skipwhite(*arg);
3965 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003967 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003969 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 }
3972 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003973 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003974 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003976 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003977 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003979
3980 if (ppconst->pp_used == ppconst_used + 2
3981 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3982 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003983 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003984 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3985 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003986 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003988 // both are numbers: compute the result
3989 switch (*op)
3990 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003991 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003992 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003995 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003996 break;
3997 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003998 tv1->vval.v_number = res;
3999 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004000 }
4001 else
4002 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004003 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004004 generate_two_op(cctx, op);
4005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 }
4007
4008 return OK;
4009}
4010
4011/*
4012 * + number addition
4013 * - number subtraction
4014 * .. string concatenation
4015 */
4016 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018{
4019 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004020 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
4024 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004025 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027
4028 /*
4029 * Repeat computing, until no "+", "-" or ".." is following.
4030 */
4031 for (;;)
4032 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004033 op = may_peek_next_line(cctx, *arg, &next);
4034 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 break;
4036 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004037 if (next != NULL)
4038 {
4039 *arg = next_line_from_context(cctx, TRUE);
4040 op = skipwhite(*arg);
4041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004043 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004045 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004046 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 }
4048
4049 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004050 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004051 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004053 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 return FAIL;
4056
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004058 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4060 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4061 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4062 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4065 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004066
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 // concat/subtract/add constant numbers
4068 if (*op == '+')
4069 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4070 else if (*op == '-')
4071 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4072 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004073 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004074 // concatenate constant strings
4075 char_u *s1 = tv1->vval.v_string;
4076 char_u *s2 = tv2->vval.v_string;
4077 size_t len1 = STRLEN(s1);
4078
4079 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4080 if (tv1->vval.v_string == NULL)
4081 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004083 return FAIL;
4084 }
4085 mch_memmove(tv1->vval.v_string, s1, len1);
4086 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087 vim_free(s1);
4088 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004089 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 }
4092 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004093 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004094 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095 if (*op == '.')
4096 {
4097 if (may_generate_2STRING(-2, cctx) == FAIL
4098 || may_generate_2STRING(-1, cctx) == FAIL)
4099 return FAIL;
4100 generate_instr_drop(cctx, ISN_CONCAT, 1);
4101 }
4102 else
4103 generate_two_op(cctx, op);
4104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 }
4106
4107 return OK;
4108}
4109
4110/*
4111 * expr5a == expr5b
4112 * expr5a =~ expr5b
4113 * expr5a != expr5b
4114 * expr5a !~ expr5b
4115 * expr5a > expr5b
4116 * expr5a >= expr5b
4117 * expr5a < expr5b
4118 * expr5a <= expr5b
4119 * expr5a is expr5b
4120 * expr5a isnot expr5b
4121 *
4122 * Produces instructions:
4123 * EVAL expr5a Push result of "expr5a"
4124 * EVAL expr5b Push result of "expr5b"
4125 * COMPARE one of the compare instructions
4126 */
4127 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129{
4130 exptype_T type = EXPR_UNKNOWN;
4131 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004132 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
4137 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 return FAIL;
4140
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004141 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004142 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143
4144 /*
4145 * If there is a comparative operator, use it.
4146 */
4147 if (type != EXPR_UNKNOWN)
4148 {
4149 int ic = FALSE; // Default: do not ignore case
4150
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004151 if (next != NULL)
4152 {
4153 *arg = next_line_from_context(cctx, TRUE);
4154 p = skipwhite(*arg);
4155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 if (type_is && (p[len] == '?' || p[len] == '#'))
4157 {
4158 semsg(_(e_invexpr2), *arg);
4159 return FAIL;
4160 }
4161 // extra question mark appended: ignore case
4162 if (p[len] == '?')
4163 {
4164 ic = TRUE;
4165 ++len;
4166 }
4167 // extra '#' appended: match case (ignored)
4168 else if (p[len] == '#')
4169 ++len;
4170 // nothing appended: match case
4171
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004172 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004174 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004175 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 }
4177
4178 // get the second variable
4179 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004180 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004181 return FAIL;
4182
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 return FAIL;
4185
Bram Moolenaara5565e42020-05-09 15:44:01 +02004186 if (ppconst->pp_used == ppconst_used + 2)
4187 {
4188 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4189 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4190 int ret;
4191
4192 // Both sides are a constant, compute the result now.
4193 // First check for a valid combination of types, this is more
4194 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004195 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004196 ret = FAIL;
4197 else
4198 {
4199 ret = typval_compare(tv1, tv2, type, ic);
4200 tv1->v_type = VAR_BOOL;
4201 tv1->vval.v_number = tv1->vval.v_number
4202 ? VVAL_TRUE : VVAL_FALSE;
4203 clear_tv(tv2);
4204 --ppconst->pp_used;
4205 }
4206 return ret;
4207 }
4208
4209 generate_ppconst(cctx, ppconst);
4210 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 }
4212
4213 return OK;
4214}
4215
Bram Moolenaar7f141552020-05-09 17:35:53 +02004216static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218/*
4219 * Compile || or &&.
4220 */
4221 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004222compile_and_or(
4223 char_u **arg,
4224 cctx_T *cctx,
4225 char *op,
4226 ppconst_T *ppconst,
4227 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004229 char_u *next;
4230 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 int opchar = *op;
4232
4233 if (p[0] == opchar && p[1] == opchar)
4234 {
4235 garray_T *instr = &cctx->ctx_instr;
4236 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004237 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004238 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239
4240 /*
4241 * Repeat until there is no following "||" or "&&"
4242 */
4243 ga_init2(&end_ga, sizeof(int), 10);
4244 while (p[0] == opchar && p[1] == opchar)
4245 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004246 if (next != NULL)
4247 {
4248 *arg = next_line_from_context(cctx, TRUE);
4249 p = skipwhite(*arg);
4250 }
4251
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4253 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004254 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004255 return FAIL;
4256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004258 // TODO: use ppconst if the value is a constant and check
4259 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260 generate_ppconst(cctx, ppconst);
4261
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004262 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4263 all_bool_values = FALSE;
4264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 if (ga_grow(&end_ga, 1) == FAIL)
4266 {
4267 ga_clear(&end_ga);
4268 return FAIL;
4269 }
4270 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4271 ++end_ga.ga_len;
4272 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004273 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
4275 // eval the next expression
4276 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004277 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004278 return FAIL;
4279
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4281 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 {
4283 ga_clear(&end_ga);
4284 return FAIL;
4285 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004286
4287 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004289 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290
4291 // Fill in the end label in all jumps.
4292 while (end_ga.ga_len > 0)
4293 {
4294 isn_T *isn;
4295
4296 --end_ga.ga_len;
4297 isn = ((isn_T *)instr->ga_data)
4298 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4299 isn->isn_arg.jump.jump_where = instr->ga_len;
4300 }
4301 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004302
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004303 // The resulting type is converted to bool if needed.
4304 if (!all_bool_values)
4305 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * expr4a && expr4a && expr4a logical AND
4313 *
4314 * Produces instructions:
4315 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004316 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004318 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004320 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 * end:
4322 */
4323 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004324compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 int ppconst_used = ppconst->pp_used;
4327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 return FAIL;
4331
4332 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004333 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334}
4335
4336/*
4337 * expr3a || expr3b || expr3c logical OR
4338 *
4339 * Produces instructions:
4340 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004341 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004343 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004345 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 * end:
4347 */
4348 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 int ppconst_used = ppconst->pp_used;
4352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004354 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 return FAIL;
4356
4357 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359}
4360
4361/*
4362 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004364 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 * JUMP_IF_FALSE alt jump if false
4366 * EVAL expr1a
4367 * JUMP_ALWAYS end
4368 * alt: EVAL expr1b
4369 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004370 *
4371 * Toplevel expression: expr2 ?? expr1
4372 * Produces instructions:
4373 * EVAL expr2 Push result of "expr2"
4374 * JUMP_AND_KEEP_IF_TRUE end jump if true
4375 * EVAL expr1
4376 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 */
4378 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380{
4381 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004383 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
Bram Moolenaar3988f642020-08-27 22:43:03 +02004385 // Ignore all kinds of errors when not producing code.
4386 if (cctx->ctx_skip == SKIP_YES)
4387 {
4388 skip_expr(arg);
4389 return OK;
4390 }
4391
Bram Moolenaar61a89812020-05-07 16:58:17 +02004392 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 return FAIL;
4395
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004396 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 if (*p == '?')
4398 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004399 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 garray_T *instr = &cctx->ctx_instr;
4401 garray_T *stack = &cctx->ctx_type_stack;
4402 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004403 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004405 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004406 int has_const_expr = FALSE;
4407 int const_value = FALSE;
4408 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004410 if (next != NULL)
4411 {
4412 *arg = next_line_from_context(cctx, TRUE);
4413 p = skipwhite(*arg);
4414 }
4415
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004416 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004417 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004418 semsg(_(e_white_space_required_before_and_after_str),
4419 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 return FAIL;
4421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 if (ppconst->pp_used == ppconst_used + 1)
4424 {
4425 // the condition is a constant, we know whether the ? or the :
4426 // expression is to be evaluated.
4427 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004428 if (op_falsy)
4429 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4430 else
4431 {
4432 int error = FALSE;
4433
4434 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4435 &error);
4436 if (error)
4437 return FAIL;
4438 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004439 cctx->ctx_skip = save_skip == SKIP_YES ||
4440 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4441
4442 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4443 // "left ?? right" and "left" is truthy: produce "left"
4444 generate_ppconst(cctx, ppconst);
4445 else
4446 {
4447 clear_tv(&ppconst->pp_tv[ppconst_used]);
4448 --ppconst->pp_used;
4449 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 }
4451 else
4452 {
4453 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004454 if (op_falsy)
4455 end_idx = instr->ga_len;
4456 generate_JUMP(cctx, op_falsy
4457 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4458 if (op_falsy)
4459 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
4462 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004463 *arg = skipwhite(p + 1 + op_falsy);
4464 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004465 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004467 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaara5565e42020-05-09 15:44:01 +02004469 if (!has_const_expr)
4470 {
4471 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004473 if (!op_falsy)
4474 {
4475 // remember the type and drop it
4476 --stack->ga_len;
4477 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004479 end_idx = instr->ga_len;
4480 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004482 // jump here from JUMP_IF_FALSE
4483 isn = ((isn_T *)instr->ga_data) + alt_idx;
4484 isn->isn_arg.jump.jump_where = instr->ga_len;
4485 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004488 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004490 // Check for the ":".
4491 p = may_peek_next_line(cctx, *arg, &next);
4492 if (*p != ':')
4493 {
4494 emsg(_(e_missing_colon));
4495 return FAIL;
4496 }
4497 if (next != NULL)
4498 {
4499 *arg = next_line_from_context(cctx, TRUE);
4500 p = skipwhite(*arg);
4501 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004502
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004503 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4504 {
4505 semsg(_(e_white_space_required_before_and_after_str), ":");
4506 return FAIL;
4507 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004509 // evaluate the third expression
4510 if (has_const_expr)
4511 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004512 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004513 *arg = skipwhite(p + 1);
4514 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4515 return FAIL;
4516 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4517 return FAIL;
4518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519
Bram Moolenaara5565e42020-05-09 15:44:01 +02004520 if (!has_const_expr)
4521 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004522 type_T **typep;
4523
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004527 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4528 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004530 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531 isn = ((isn_T *)instr->ga_data) + end_idx;
4532 isn->isn_arg.jump.jump_where = instr->ga_len;
4533 }
4534
4535 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 }
4537 return OK;
4538}
4539
4540/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004542 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4543 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 */
4545 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004546compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547{
4548 ppconst_T ppconst;
4549
4550 CLEAR_FIELD(ppconst);
4551 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4552 {
4553 clear_ppconst(&ppconst);
4554 return FAIL;
4555 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004556 if (is_const != NULL)
4557 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 if (generate_ppconst(cctx, &ppconst) == FAIL)
4559 return FAIL;
4560 return OK;
4561}
4562
4563/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004564 * Toplevel expression.
4565 */
4566 static int
4567compile_expr0(char_u **arg, cctx_T *cctx)
4568{
4569 return compile_expr0_ext(arg, cctx, NULL);
4570}
4571
4572/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 * compile "return [expr]"
4574 */
4575 static char_u *
4576compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4577{
4578 char_u *p = arg;
4579 garray_T *stack = &cctx->ctx_type_stack;
4580 type_T *stack_type;
4581
4582 if (*p != NUL && *p != '|' && *p != '\n')
4583 {
4584 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004585 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 return NULL;
4587
4588 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4589 if (set_return_type)
4590 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004591 else
4592 {
4593 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4594 && stack_type->tt_type != VAR_VOID
4595 && stack_type->tt_type != VAR_UNKNOWN)
4596 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004597 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004598 return NULL;
4599 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004600 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004601 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004602 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 }
4605 else
4606 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004607 // "set_return_type" cannot be TRUE, only used for a lambda which
4608 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004609 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4610 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004612 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 return NULL;
4614 }
4615
4616 // No argument, return zero.
4617 generate_PUSHNR(cctx, 0);
4618 }
4619
4620 if (generate_instr(cctx, ISN_RETURN) == NULL)
4621 return NULL;
4622
4623 // "return val | endif" is possible
4624 return skipwhite(p);
4625}
4626
4627/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004628 * Get a line from the compilation context, compatible with exarg_T getline().
4629 * Return a pointer to the line in allocated memory.
4630 * Return NULL for end-of-file or some error.
4631 */
4632 static char_u *
4633exarg_getline(
4634 int c UNUSED,
4635 void *cookie,
4636 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004637 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004638{
4639 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004640 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641
Bram Moolenaar66250c92020-08-20 15:02:42 +02004642 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004643 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004644 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004645 return NULL;
4646 ++cctx->ctx_lnum;
4647 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4648 // Comment lines result in NULL pointers, skip them.
4649 if (p != NULL)
4650 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004651 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004652}
4653
4654/*
4655 * Compile a nested :def command.
4656 */
4657 static char_u *
4658compile_nested_function(exarg_T *eap, cctx_T *cctx)
4659{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004660 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004661 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004662 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004663 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004664 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004665 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004666
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004667 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004668 {
4669 emsg(_(e_cannot_use_bang_with_nested_def));
4670 return NULL;
4671 }
4672
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004673 // Only g:Func() can use a namespace.
4674 if (name_start[1] == ':' && !is_global)
4675 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004676 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004677 return NULL;
4678 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004679 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4680 return NULL;
4681
Bram Moolenaar04b12692020-05-04 23:24:44 +02004682 eap->arg = name_end;
4683 eap->getline = exarg_getline;
4684 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004685 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004686 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004687 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004688 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004689
Bram Moolenaar822ba242020-05-24 23:00:18 +02004690 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004691 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004692 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004693 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004694 {
4695 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004696 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004697 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004698
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004699 if (is_global)
4700 {
4701 char_u *func_name = vim_strnsave(name_start + 2,
4702 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004703
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004704 if (func_name == NULL)
4705 r = FAIL;
4706 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004707 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004708 }
4709 else
4710 {
4711 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004712 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004713 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004714 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004715
Bram Moolenaareef21022020-08-01 22:16:43 +02004716 if (lvar == NULL)
4717 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004718 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004719 return NULL;
4720 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004721
4722 // copy over the block scope IDs
4723 if (block_depth > 0)
4724 {
4725 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4726 if (ufunc->uf_block_ids != NULL)
4727 {
4728 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4729 sizeof(int) * block_depth);
4730 ufunc->uf_block_depth = block_depth;
4731 }
4732 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004733 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004734
Bram Moolenaar61a89812020-05-07 16:58:17 +02004735 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004736 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004737}
4738
4739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 * Return the length of an assignment operator, or zero if there isn't one.
4741 */
4742 int
4743assignment_len(char_u *p, int *heredoc)
4744{
4745 if (*p == '=')
4746 {
4747 if (p[1] == '<' && p[2] == '<')
4748 {
4749 *heredoc = TRUE;
4750 return 3;
4751 }
4752 return 1;
4753 }
4754 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4755 return 2;
4756 if (STRNCMP(p, "..=", 3) == 0)
4757 return 3;
4758 return 0;
4759}
4760
4761// words that cannot be used as a variable
4762static char *reserved[] = {
4763 "true",
4764 "false",
4765 NULL
4766};
4767
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004768typedef enum {
4769 dest_local,
4770 dest_option,
4771 dest_env,
4772 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004773 dest_buffer,
4774 dest_window,
4775 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004776 dest_vimvar,
4777 dest_script,
4778 dest_reg,
4779} assign_dest_T;
4780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004782 * Generate the load instruction for "name".
4783 */
4784 static void
4785generate_loadvar(
4786 cctx_T *cctx,
4787 assign_dest_T dest,
4788 char_u *name,
4789 lvar_T *lvar,
4790 type_T *type)
4791{
4792 switch (dest)
4793 {
4794 case dest_option:
4795 // TODO: check the option exists
4796 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4797 break;
4798 case dest_global:
4799 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4800 break;
4801 case dest_buffer:
4802 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4803 break;
4804 case dest_window:
4805 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4806 break;
4807 case dest_tab:
4808 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4809 break;
4810 case dest_script:
4811 compile_load_scriptvar(cctx,
4812 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4813 break;
4814 case dest_env:
4815 // Include $ in the name here
4816 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4817 break;
4818 case dest_reg:
4819 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4820 break;
4821 case dest_vimvar:
4822 generate_LOADV(cctx, name + 2, TRUE);
4823 break;
4824 case dest_local:
4825 if (lvar->lv_from_outer)
4826 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4827 NULL, type);
4828 else
4829 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4830 break;
4831 }
4832}
4833
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004834 void
4835vim9_declare_error(char_u *name)
4836{
4837 char *scope = "";
4838
4839 switch (*name)
4840 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004841 case 'g': scope = _("global"); break;
4842 case 'b': scope = _("buffer"); break;
4843 case 'w': scope = _("window"); break;
4844 case 't': scope = _("tab"); break;
4845 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004846 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004847 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004848 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004849 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004850 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004851 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004852 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004853 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004854 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004855}
4856
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004857/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004859 * "let name"
4860 * "var name = expr"
4861 * "final name = expr"
4862 * "const name = expr"
4863 * "name = expr"
4864 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 * Return NULL for an error.
4866 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 */
4868 static char_u *
4869compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4870{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004873 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 char_u *ret = NULL;
4875 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004877 int scriptvar_sid = 0;
4878 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004881 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 int oplen = 0;
4884 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004885 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004886 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004887 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004889 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4890 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004892 // Skip over the "var" or "[var, var]" to get to any "=".
4893 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4894 if (p == NULL)
4895 return *arg == '[' ? arg : NULL;
4896
4897 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004899 // TODO: should we allow this, and figure out type inference from list
4900 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004901 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 return NULL;
4903 }
4904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 sp = p;
4906 p = skipwhite(p);
4907 op = p;
4908 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909
4910 if (var_count > 0 && oplen == 0)
4911 // can be something like "[1, 2]->func()"
4912 return arg;
4913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4915 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004916 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004917 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004918 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 if (heredoc)
4921 {
4922 list_T *l;
4923 listitem_T *li;
4924
4925 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004926 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004928 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004929 if (l == NULL)
4930 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931
Bram Moolenaar078269b2020-09-21 20:35:55 +02004932 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004934 // Push each line and the create the list.
4935 FOR_ALL_LIST_ITEMS(l, li)
4936 {
4937 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4938 li->li_tv.vval.v_string = NULL;
4939 }
4940 generate_NEWLIST(cctx, l->lv_len);
4941 type = &t_list_string;
4942 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 list_free(l);
4945 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 // for "[var, var] = expr" evaluate the expression here, loop over the
4951 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004952
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004953 p = skipwhite(op + oplen);
4954 if (compile_expr0(&p, cctx) == FAIL)
4955 return NULL;
4956 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004958 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004960 type_T *stacktype;
4961
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004962 stacktype = stack->ga_len == 0 ? &t_void
4963 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004966 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 goto theend;
4968 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004969 if (need_type(stacktype, &t_list_any, -1, cctx,
4970 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004972 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004973 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4974 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975 }
4976 }
4977
4978 /*
4979 * Loop over variables in "[var, var] = expr".
4980 * For "var = expr" and "let var: type" this is done only once.
4981 */
4982 if (var_count > 0)
4983 var_start = skipwhite(arg + 1); // skip over the "["
4984 else
4985 var_start = arg;
4986 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4987 {
4988 char_u *var_end = skip_var_one(var_start, FALSE);
4989 size_t varlen;
4990 int new_local = FALSE;
4991 int opt_type;
4992 int opt_flags = 0;
4993 assign_dest_T dest = dest_local;
4994 int vimvaridx = -1;
4995 lvar_T *lvar = NULL;
4996 lvar_T arg_lvar;
4997 int has_type = FALSE;
4998 int has_index = FALSE;
4999 int instr_count = -1;
5000
Bram Moolenaar65821722020-08-02 18:58:54 +02005001 if (*var_start == '@')
5002 p = var_start + 2;
5003 else
5004 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005005 // skip over the leading "&", "&l:", "&g:" and "$"
5006 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005007 p = to_name_end(p, TRUE);
5008 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005009
5010 // "a: type" is declaring variable "a" with a type, not "a:".
5011 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5012 --var_end;
5013 if (is_decl && p == var_start + 2 && p[-1] == ':')
5014 --p;
5015
5016 varlen = p - var_start;
5017 vim_free(name);
5018 name = vim_strnsave(var_start, varlen);
5019 if (name == NULL)
5020 return NULL;
5021 if (!heredoc)
5022 type = &t_any;
5023
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005024 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005026 int declare_error = FALSE;
5027
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 if (*var_start == '&')
5029 {
5030 int cc;
5031 long numval;
5032
5033 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005034 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036 emsg(_(e_const_option));
5037 goto theend;
5038 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005039 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 p = var_start;
5041 p = find_option_end(&p, &opt_flags);
5042 if (p == NULL)
5043 {
5044 // cannot happen?
5045 emsg(_(e_letunexp));
5046 goto theend;
5047 }
5048 cc = *p;
5049 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005050 opt_type = get_option_value(skip_option_env_lead(var_start),
5051 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 *p = cc;
5053 if (opt_type == -3)
5054 {
5055 semsg(_(e_unknown_option), var_start);
5056 goto theend;
5057 }
5058 if (opt_type == -2 || opt_type == 0)
5059 type = &t_string;
5060 else
5061 type = &t_number; // both number and boolean option
5062 }
5063 else if (*var_start == '$')
5064 {
5065 dest = dest_env;
5066 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005067 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005068 }
5069 else if (*var_start == '@')
5070 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005071 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 {
5073 emsg_invreg(var_start[1]);
5074 goto theend;
5075 }
5076 dest = dest_reg;
5077 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005078 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005080 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 {
5082 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005083 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005085 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 {
5087 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005088 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005090 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 {
5092 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005093 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005095 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 {
5097 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005098 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005100 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 {
5102 typval_T *vtv;
5103 int di_flags;
5104
5105 vimvaridx = find_vim_var(name + 2, &di_flags);
5106 if (vimvaridx < 0)
5107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005108 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 goto theend;
5110 }
5111 // We use the current value of "sandbox" here, is that OK?
5112 if (var_check_ro(di_flags, name, FALSE))
5113 goto theend;
5114 dest = dest_vimvar;
5115 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005116 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005117 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 }
5119 else
5120 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005121 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122
5123 for (idx = 0; reserved[idx] != NULL; ++idx)
5124 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005126 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005127 goto theend;
5128 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129
5130 lvar = lookup_local(var_start, varlen, cctx);
5131 if (lvar == NULL)
5132 {
5133 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005134 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5136 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005137 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 if (is_decl)
5139 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005140 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 goto theend;
5142 }
5143 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005144 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005145 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 if (lvar != NULL)
5147 {
5148 if (is_decl)
5149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005150 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 goto theend;
5152 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005154 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005156 int script_namespace = varlen > 1
5157 && STRNCMP(var_start, "s:", 2) == 0;
5158 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005159 ? script_var_exists(var_start + 2, varlen - 2,
5160 FALSE, cctx)
5161 : script_var_exists(var_start, varlen,
5162 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005163 imported_T *import =
5164 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005165
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005166 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005168 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5169
5170 if (is_decl)
5171 {
5172 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005173 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005174 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005175 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005176 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005177 name);
5178 goto theend;
5179 }
5180 else if (cctx->ctx_ufunc->uf_script_ctx_version
5181 == SCRIPT_VERSION_VIM9
5182 && script_namespace
5183 && !script_var && import == NULL)
5184 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005185 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005186 goto theend;
5187 }
5188
5189 dest = dest_script;
5190
5191 // existing script-local variables should have a type
5192 scriptvar_sid = current_sctx.sc_sid;
5193 if (import != NULL)
5194 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005195 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005196 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005197 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005198 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005199 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005200 {
5201 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5202 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005203 ((svar_T *)si->sn_var_vals.ga_data)
5204 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005205 type = sv->sv_type;
5206 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005207 }
5208 }
5209 else if (name[1] == ':' && name[2] != NUL)
5210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005211 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 goto theend;
5213 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005214 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005216 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005217 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005218 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005219 else if (check_defined(var_start, varlen, cctx) == FAIL)
5220 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005223
5224 if (declare_error)
5225 {
5226 vim9_declare_error(name);
5227 goto theend;
5228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 }
5230
5231 // handle "a:name" as a name, not index "name" on "a"
5232 if (varlen > 1 || var_start[varlen] != ':')
5233 p = var_end;
5234
5235 if (dest != dest_option)
5236 {
5237 if (is_decl && *p == ':')
5238 {
5239 // parse optional type: "let var: type = expr"
5240 if (!VIM_ISWHITE(p[1]))
5241 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005242 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 goto theend;
5244 }
5245 p = skipwhite(p + 1);
5246 type = parse_type(&p, cctx->ctx_type_list);
5247 has_type = TRUE;
5248 }
5249 else if (lvar != NULL)
5250 type = lvar->lv_type;
5251 }
5252
5253 if (oplen == 3 && !heredoc && dest != dest_global
5254 && type->tt_type != VAR_STRING
5255 && type->tt_type != VAR_ANY)
5256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005257 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 goto theend;
5259 }
5260
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005261 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 {
5263 if (oplen > 1 && !heredoc)
5264 {
5265 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005266 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 goto theend;
5268 }
5269
5270 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005271 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5272 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273 goto theend;
5274 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005275 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005276 if (lvar == NULL)
5277 goto theend;
5278 new_local = TRUE;
5279 }
5280
5281 member_type = type;
5282 if (var_end > var_start + varlen)
5283 {
5284 // Something follows after the variable: "var[idx]".
5285 if (is_decl)
5286 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005287 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 goto theend;
5289 }
5290
5291 if (var_start[varlen] == '[')
5292 {
5293 has_index = TRUE;
5294 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005295 member_type = &t_any;
5296 else
5297 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 }
5299 else
5300 {
5301 semsg("Not supported yet: %s", var_start);
5302 goto theend;
5303 }
5304 }
5305 else if (lvar == &arg_lvar)
5306 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005307 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 goto theend;
5309 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005310 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5311 {
5312 semsg(_(e_cannot_assign_to_constant), name);
5313 goto theend;
5314 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005315
5316 if (!heredoc)
5317 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005318 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005320 if (oplen > 0 && var_count == 0)
5321 {
5322 // skip over the "=" and the expression
5323 p = skipwhite(op + oplen);
5324 compile_expr0(&p, cctx);
5325 }
5326 }
5327 else if (oplen > 0)
5328 {
5329 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005330 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005331
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005332 // For "var = expr" evaluate the expression.
5333 if (var_count == 0)
5334 {
5335 int r;
5336
5337 // for "+=", "*=", "..=" etc. first load the current value
5338 if (*op != '=')
5339 {
5340 generate_loadvar(cctx, dest, name, lvar, type);
5341
5342 if (has_index)
5343 {
5344 // TODO: get member from list or dict
5345 emsg("Index with operation not supported yet");
5346 goto theend;
5347 }
5348 }
5349
5350 // Compile the expression. Temporarily hide the new local
5351 // variable here, it is not available to this expression.
5352 if (new_local)
5353 --cctx->ctx_locals.ga_len;
5354 instr_count = instr->ga_len;
5355 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005356 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005357 if (new_local)
5358 ++cctx->ctx_locals.ga_len;
5359 if (r == FAIL)
5360 goto theend;
5361 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005362 else if (semicolon && var_idx == var_count - 1)
5363 {
5364 // For "[var; var] = expr" get the rest of the list
5365 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5366 goto theend;
5367 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005368 else
5369 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 // For "[var, var] = expr" get the "var_idx" item from the
5371 // list.
5372 if (generate_GETITEM(cctx, var_idx) == FAIL)
5373 return FAIL;
5374 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005375
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005376 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005377 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005378 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005380 if ((stacktype->tt_type == VAR_FUNC
5381 || stacktype->tt_type == VAR_PARTIAL)
5382 && var_wrong_func_name(name, TRUE))
5383 goto theend;
5384
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005385 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005386 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005387 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005388 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005389 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005390 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 }
5392 else
5393 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005394 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005395 // for a variable that implies &t_any.
5396 if (stacktype == &t_list_empty)
5397 lvar->lv_type = &t_list_any;
5398 else if (stacktype == &t_dict_empty)
5399 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005400 else if (stacktype == &t_unknown)
5401 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005402 else
5403 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005405 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005406 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005407 {
5408 type_T *use_type = lvar->lv_type;
5409
Bram Moolenaar71abe482020-09-14 22:28:30 +02005410 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005411 if (has_index)
5412 {
5413 use_type = use_type->tt_member;
5414 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005415 // could be indexing "any"
5416 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005417 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005418 if (need_type(stacktype, use_type, -1, cctx,
5419 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005420 goto theend;
5421 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005422 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005423 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005424 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005425 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005427 else if (cmdidx == CMD_final)
5428 {
5429 emsg(_(e_final_requires_a_value));
5430 goto theend;
5431 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005434 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 goto theend;
5436 }
5437 else if (!has_type || dest == dest_option)
5438 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005439 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440 goto theend;
5441 }
5442 else
5443 {
5444 // variables are always initialized
5445 if (ga_grow(instr, 1) == FAIL)
5446 goto theend;
5447 switch (member_type->tt_type)
5448 {
5449 case VAR_BOOL:
5450 generate_PUSHBOOL(cctx, VVAL_FALSE);
5451 break;
5452 case VAR_FLOAT:
5453#ifdef FEAT_FLOAT
5454 generate_PUSHF(cctx, 0.0);
5455#endif
5456 break;
5457 case VAR_STRING:
5458 generate_PUSHS(cctx, NULL);
5459 break;
5460 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005461 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005462 break;
5463 case VAR_FUNC:
5464 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5465 break;
5466 case VAR_LIST:
5467 generate_NEWLIST(cctx, 0);
5468 break;
5469 case VAR_DICT:
5470 generate_NEWDICT(cctx, 0);
5471 break;
5472 case VAR_JOB:
5473 generate_PUSHJOB(cctx, NULL);
5474 break;
5475 case VAR_CHANNEL:
5476 generate_PUSHCHANNEL(cctx, NULL);
5477 break;
5478 case VAR_NUMBER:
5479 case VAR_UNKNOWN:
5480 case VAR_ANY:
5481 case VAR_PARTIAL:
5482 case VAR_VOID:
5483 case VAR_SPECIAL: // cannot happen
5484 generate_PUSHNR(cctx, 0);
5485 break;
5486 }
5487 }
5488 if (var_count == 0)
5489 end = p;
5490 }
5491
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005493 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005494 break;
5495
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496 if (oplen > 0 && *op != '=')
5497 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005498 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 type_T *stacktype;
5500
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 if (*op == '.')
5502 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005503 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005504 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005506 if (
5507#ifdef FEAT_FLOAT
5508 // If variable is float operation with number is OK.
5509 !(expected == &t_float && stacktype == &t_number) &&
5510#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005511 need_type(stacktype, expected, -1, cctx,
5512 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513 goto theend;
5514
5515 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005516 {
5517 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5518 goto theend;
5519 }
5520 else if (*op == '+')
5521 {
5522 if (generate_add_instr(cctx,
5523 operator_type(member_type, stacktype),
5524 member_type, stacktype) == FAIL)
5525 goto theend;
5526 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005527 else if (generate_two_op(cctx, op) == FAIL)
5528 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005530
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005531 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005532 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005533 int r;
5534
5535 // Compile the "idx" in "var[idx]".
5536 if (new_local)
5537 --cctx->ctx_locals.ga_len;
5538 p = skipwhite(var_start + varlen + 1);
5539 r = compile_expr0(&p, cctx);
5540 if (new_local)
5541 ++cctx->ctx_locals.ga_len;
5542 if (r == FAIL)
5543 goto theend;
5544 if (*skipwhite(p) != ']')
5545 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005546 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005547 emsg(_(e_missbrac));
5548 goto theend;
5549 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005550 if (type == &t_any)
5551 {
5552 type_T *idx_type = ((type_T **)stack->ga_data)[
5553 stack->ga_len - 1];
5554 // Index on variable of unknown type: guess the type from the
5555 // index type: number is dict, otherwise dict.
5556 // TODO: should do the assignment at runtime
5557 if (idx_type->tt_type == VAR_NUMBER)
5558 type = &t_list_any;
5559 else
5560 type = &t_dict_any;
5561 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562 if (type->tt_type == VAR_DICT
5563 && may_generate_2STRING(-1, cctx) == FAIL)
5564 goto theend;
5565 if (type->tt_type == VAR_LIST
5566 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005567 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005568 {
5569 emsg(_(e_number_exp));
5570 goto theend;
5571 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573 // Load the dict or list. On the stack we then have:
5574 // - value
5575 // - index
5576 // - variable
5577 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005578
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579 if (type->tt_type == VAR_LIST)
5580 {
5581 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5582 return FAIL;
5583 }
5584 else if (type->tt_type == VAR_DICT)
5585 {
5586 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5587 return FAIL;
5588 }
5589 else
5590 {
5591 emsg(_(e_listreq));
5592 goto theend;
5593 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005594 }
5595 else
5596 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005597 if (is_decl && cmdidx == CMD_const
5598 && (dest == dest_script || dest == dest_local))
5599 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005600 generate_LOCKCONST(cctx);
5601
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 switch (dest)
5603 {
5604 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005605 generate_STOREOPT(cctx, skip_option_env_lead(name),
5606 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005607 break;
5608 case dest_global:
5609 // include g: with the name, easier to execute that way
5610 generate_STORE(cctx, ISN_STOREG, 0, name);
5611 break;
5612 case dest_buffer:
5613 // include b: with the name, easier to execute that way
5614 generate_STORE(cctx, ISN_STOREB, 0, name);
5615 break;
5616 case dest_window:
5617 // include w: with the name, easier to execute that way
5618 generate_STORE(cctx, ISN_STOREW, 0, name);
5619 break;
5620 case dest_tab:
5621 // include t: with the name, easier to execute that way
5622 generate_STORE(cctx, ISN_STORET, 0, name);
5623 break;
5624 case dest_env:
5625 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5626 break;
5627 case dest_reg:
5628 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5629 break;
5630 case dest_vimvar:
5631 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5632 break;
5633 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005634 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005635 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636 {
5637 char_u *name_s = name;
5638
5639 // Include s: in the name for store_var()
5640 if (name[1] != ':')
5641 {
5642 int len = (int)STRLEN(name) + 3;
5643
5644 name_s = alloc(len);
5645 if (name_s == NULL)
5646 name_s = name;
5647 else
5648 vim_snprintf((char *)name_s, len,
5649 "s:%s", name);
5650 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005651 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5652 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653 if (name_s != name)
5654 vim_free(name_s);
5655 }
5656 else
5657 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005658 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005659 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005660 break;
5661 case dest_local:
5662 if (lvar != NULL)
5663 {
5664 isn_T *isn = ((isn_T *)instr->ga_data)
5665 + instr->ga_len - 1;
5666
5667 // optimization: turn "var = 123" from ISN_PUSHNR +
5668 // ISN_STORE into ISN_STORENR
5669 if (!lvar->lv_from_outer
5670 && instr->ga_len == instr_count + 1
5671 && isn->isn_type == ISN_PUSHNR)
5672 {
5673 varnumber_T val = isn->isn_arg.number;
5674
5675 isn->isn_type = ISN_STORENR;
5676 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5677 isn->isn_arg.storenr.stnr_val = val;
5678 if (stack->ga_len > 0)
5679 --stack->ga_len;
5680 }
5681 else if (lvar->lv_from_outer)
5682 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005683 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684 else
5685 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5686 }
5687 break;
5688 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005689 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005690
5691 if (var_idx + 1 < var_count)
5692 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005694
5695 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005696 if (var_count > 0 && !semicolon)
5697 {
5698 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5699 goto theend;
5700 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005701
Bram Moolenaarb2097502020-07-19 17:17:02 +02005702 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703
5704theend:
5705 vim_free(name);
5706 return ret;
5707}
5708
5709/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005710 * Check if "name" can be "unlet".
5711 */
5712 int
5713check_vim9_unlet(char_u *name)
5714{
5715 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5716 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005717 // "unlet s:var" is allowed in legacy script.
5718 if (*name == 's' && !script_is_vim9())
5719 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005720 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005721 return FAIL;
5722 }
5723 return OK;
5724}
5725
5726/*
5727 * Callback passed to ex_unletlock().
5728 */
5729 static int
5730compile_unlet(
5731 lval_T *lvp,
5732 char_u *name_end,
5733 exarg_T *eap,
5734 int deep UNUSED,
5735 void *coookie)
5736{
5737 cctx_T *cctx = coookie;
5738
5739 if (lvp->ll_tv == NULL)
5740 {
5741 char_u *p = lvp->ll_name;
5742 int cc = *name_end;
5743 int ret = OK;
5744
5745 // Normal name. Only supports g:, w:, t: and b: namespaces.
5746 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005747 if (*p == '$')
5748 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5749 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005750 ret = FAIL;
5751 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005752 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005753
5754 *name_end = cc;
5755 return ret;
5756 }
5757
5758 // TODO: unlet {list}[idx]
5759 // TODO: unlet {dict}[key]
5760 emsg("Sorry, :unlet not fully implemented yet");
5761 return FAIL;
5762}
5763
5764/*
5765 * compile "unlet var", "lock var" and "unlock var"
5766 * "arg" points to "var".
5767 */
5768 static char_u *
5769compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5770{
5771 char_u *p = arg;
5772
5773 if (eap->cmdidx != CMD_unlet)
5774 {
5775 emsg("Sorry, :lock and unlock not implemented yet");
5776 return NULL;
5777 }
5778
5779 if (*p == '!')
5780 {
5781 p = skipwhite(p + 1);
5782 eap->forceit = TRUE;
5783 }
5784
5785 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5786 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5787}
5788
5789/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 * Compile an :import command.
5791 */
5792 static char_u *
5793compile_import(char_u *arg, cctx_T *cctx)
5794{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005795 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796}
5797
5798/*
5799 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5800 */
5801 static int
5802compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5803{
5804 garray_T *instr = &cctx->ctx_instr;
5805 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5806
5807 if (endlabel == NULL)
5808 return FAIL;
5809 endlabel->el_next = *el;
5810 *el = endlabel;
5811 endlabel->el_end_label = instr->ga_len;
5812
5813 generate_JUMP(cctx, when, 0);
5814 return OK;
5815}
5816
5817 static void
5818compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5819{
5820 garray_T *instr = &cctx->ctx_instr;
5821
5822 while (*el != NULL)
5823 {
5824 endlabel_T *cur = (*el);
5825 isn_T *isn;
5826
5827 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5828 isn->isn_arg.jump.jump_where = instr->ga_len;
5829 *el = cur->el_next;
5830 vim_free(cur);
5831 }
5832}
5833
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005834 static void
5835compile_free_jump_to_end(endlabel_T **el)
5836{
5837 while (*el != NULL)
5838 {
5839 endlabel_T *cur = (*el);
5840
5841 *el = cur->el_next;
5842 vim_free(cur);
5843 }
5844}
5845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846/*
5847 * Create a new scope and set up the generic items.
5848 */
5849 static scope_T *
5850new_scope(cctx_T *cctx, scopetype_T type)
5851{
5852 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5853
5854 if (scope == NULL)
5855 return NULL;
5856 scope->se_outer = cctx->ctx_scope;
5857 cctx->ctx_scope = scope;
5858 scope->se_type = type;
5859 scope->se_local_count = cctx->ctx_locals.ga_len;
5860 return scope;
5861}
5862
5863/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005864 * Free the current scope and go back to the outer scope.
5865 */
5866 static void
5867drop_scope(cctx_T *cctx)
5868{
5869 scope_T *scope = cctx->ctx_scope;
5870
5871 if (scope == NULL)
5872 {
5873 iemsg("calling drop_scope() without a scope");
5874 return;
5875 }
5876 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005877 switch (scope->se_type)
5878 {
5879 case IF_SCOPE:
5880 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5881 case FOR_SCOPE:
5882 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5883 case WHILE_SCOPE:
5884 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5885 case TRY_SCOPE:
5886 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5887 case NO_SCOPE:
5888 case BLOCK_SCOPE:
5889 break;
5890 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005891 vim_free(scope);
5892}
5893
5894/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005895 * Check that the top of the type stack has a type that can be used as a
5896 * condition. Give an error and return FAIL if not.
5897 */
5898 static int
5899bool_on_stack(cctx_T *cctx)
5900{
5901 garray_T *stack = &cctx->ctx_type_stack;
5902 type_T *type;
5903
5904 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5905 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005906 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005907 return FAIL;
5908 return OK;
5909}
5910
5911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 * compile "if expr"
5913 *
5914 * "if expr" Produces instructions:
5915 * EVAL expr Push result of "expr"
5916 * JUMP_IF_FALSE end
5917 * ... body ...
5918 * end:
5919 *
5920 * "if expr | else" Produces instructions:
5921 * EVAL expr Push result of "expr"
5922 * JUMP_IF_FALSE else
5923 * ... body ...
5924 * JUMP_ALWAYS end
5925 * else:
5926 * ... body ...
5927 * end:
5928 *
5929 * "if expr1 | elseif expr2 | else" Produces instructions:
5930 * EVAL expr Push result of "expr"
5931 * JUMP_IF_FALSE elseif
5932 * ... body ...
5933 * JUMP_ALWAYS end
5934 * elseif:
5935 * EVAL expr Push result of "expr"
5936 * JUMP_IF_FALSE else
5937 * ... body ...
5938 * JUMP_ALWAYS end
5939 * else:
5940 * ... body ...
5941 * end:
5942 */
5943 static char_u *
5944compile_if(char_u *arg, cctx_T *cctx)
5945{
5946 char_u *p = arg;
5947 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005948 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005950 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005951 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaara5565e42020-05-09 15:44:01 +02005953 CLEAR_FIELD(ppconst);
5954 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005955 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005956 clear_ppconst(&ppconst);
5957 return NULL;
5958 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005959 if (cctx->ctx_skip == SKIP_YES)
5960 clear_ppconst(&ppconst);
5961 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005962 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005963 int error = FALSE;
5964 int v;
5965
Bram Moolenaara5565e42020-05-09 15:44:01 +02005966 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005967 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005968 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005969 if (error)
5970 return NULL;
5971 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005972 }
5973 else
5974 {
5975 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005976 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005977 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005978 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005979 if (bool_on_stack(cctx) == FAIL)
5980 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982
5983 scope = new_scope(cctx, IF_SCOPE);
5984 if (scope == NULL)
5985 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005986 scope->se_skip_save = skip_save;
5987 // "is_had_return" will be reset if any block does not end in :return
5988 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005990 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005991 {
5992 // "where" is set when ":elseif", "else" or ":endif" is found
5993 scope->se_u.se_if.is_if_label = instr->ga_len;
5994 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5995 }
5996 else
5997 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
5999 return p;
6000}
6001
6002 static char_u *
6003compile_elseif(char_u *arg, cctx_T *cctx)
6004{
6005 char_u *p = arg;
6006 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006007 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 isn_T *isn;
6009 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006010 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006011 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
6013 if (scope == NULL || scope->se_type != IF_SCOPE)
6014 {
6015 emsg(_(e_elseif_without_if));
6016 return NULL;
6017 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006018 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006019 if (!cctx->ctx_had_return)
6020 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006022 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006023 {
6024 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006026 return NULL;
6027 // previous "if" or "elseif" jumps here
6028 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6029 isn->isn_arg.jump.jump_where = instr->ga_len;
6030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006032 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006033 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006034 if (cctx->ctx_skip == SKIP_YES)
6035 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006036 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006037 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006038 clear_ppconst(&ppconst);
6039 return NULL;
6040 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006041 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006042 if (scope->se_skip_save == SKIP_YES)
6043 clear_ppconst(&ppconst);
6044 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006045 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006046 int error = FALSE;
6047 int v;
6048
Bram Moolenaar7f141552020-05-09 17:35:53 +02006049 // The expression results in a constant.
6050 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006051 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6052 if (error)
6053 return NULL;
6054 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006055 clear_ppconst(&ppconst);
6056 scope->se_u.se_if.is_if_label = -1;
6057 }
6058 else
6059 {
6060 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006061 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006062 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006063 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006064 if (bool_on_stack(cctx) == FAIL)
6065 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006067 // "where" is set when ":elseif", "else" or ":endif" is found
6068 scope->se_u.se_if.is_if_label = instr->ga_len;
6069 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
6072 return p;
6073}
6074
6075 static char_u *
6076compile_else(char_u *arg, cctx_T *cctx)
6077{
6078 char_u *p = arg;
6079 garray_T *instr = &cctx->ctx_instr;
6080 isn_T *isn;
6081 scope_T *scope = cctx->ctx_scope;
6082
6083 if (scope == NULL || scope->se_type != IF_SCOPE)
6084 {
6085 emsg(_(e_else_without_if));
6086 return NULL;
6087 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006088 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006089 if (!cctx->ctx_had_return)
6090 scope->se_u.se_if.is_had_return = FALSE;
6091 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092
Bram Moolenaarefd88552020-06-18 20:50:10 +02006093 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006094 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006095 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006096 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006097 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006098 if (!cctx->ctx_had_return
6099 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6100 JUMP_ALWAYS, cctx) == FAIL)
6101 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006102 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006103
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006104 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006105 {
6106 if (scope->se_u.se_if.is_if_label >= 0)
6107 {
6108 // previous "if" or "elseif" jumps here
6109 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6110 isn->isn_arg.jump.jump_where = instr->ga_len;
6111 scope->se_u.se_if.is_if_label = -1;
6112 }
6113 }
6114
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006115 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006116 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 return p;
6120}
6121
6122 static char_u *
6123compile_endif(char_u *arg, cctx_T *cctx)
6124{
6125 scope_T *scope = cctx->ctx_scope;
6126 ifscope_T *ifscope;
6127 garray_T *instr = &cctx->ctx_instr;
6128 isn_T *isn;
6129
6130 if (scope == NULL || scope->se_type != IF_SCOPE)
6131 {
6132 emsg(_(e_endif_without_if));
6133 return NULL;
6134 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006135 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006136 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006137 if (!cctx->ctx_had_return)
6138 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006140 if (scope->se_u.se_if.is_if_label >= 0)
6141 {
6142 // previous "if" or "elseif" jumps here
6143 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6144 isn->isn_arg.jump.jump_where = instr->ga_len;
6145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 // Fill in the "end" label in jumps at the end of the blocks.
6147 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006148 cctx->ctx_skip = scope->se_skip_save;
6149
6150 // If all the blocks end in :return and there is an :else then the
6151 // had_return flag is set.
6152 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006154 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 return arg;
6156}
6157
6158/*
6159 * compile "for var in expr"
6160 *
6161 * Produces instructions:
6162 * PUSHNR -1
6163 * STORE loop-idx Set index to -1
6164 * EVAL expr Push result of "expr"
6165 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6166 * - if beyond end, jump to "end"
6167 * - otherwise get item from list and push it
6168 * STORE var Store item in "var"
6169 * ... body ...
6170 * JUMP top Jump back to repeat
6171 * end: DROP Drop the result of "expr"
6172 *
6173 */
6174 static char_u *
6175compile_for(char_u *arg, cctx_T *cctx)
6176{
6177 char_u *p;
6178 size_t varlen;
6179 garray_T *instr = &cctx->ctx_instr;
6180 garray_T *stack = &cctx->ctx_type_stack;
6181 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006182 lvar_T *loop_lvar; // loop iteration variable
6183 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 type_T *vartype;
6185
6186 // TODO: list of variables: "for [key, value] in dict"
6187 // parse "var"
6188 for (p = arg; eval_isnamec1(*p); ++p)
6189 ;
6190 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006191 var_lvar = lookup_local(arg, varlen, cctx);
6192 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006194 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 return NULL;
6196 }
6197
6198 // consume "in"
6199 p = skipwhite(p);
6200 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6201 {
6202 emsg(_(e_missing_in));
6203 return NULL;
6204 }
6205 p = skipwhite(p + 2);
6206
6207
6208 scope = new_scope(cctx, FOR_SCOPE);
6209 if (scope == NULL)
6210 return NULL;
6211
6212 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006213 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6214 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006215 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006216 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006217 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220
6221 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006222 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6223 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006224 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006225 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006226 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006230 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231
6232 // compile "expr", it remains on the stack until "endfor"
6233 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006234 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006235 {
6236 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006240 // Now that we know the type of "var", check that it is a list, now or at
6241 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006243 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006245 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 return NULL;
6247 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006248 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006249 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250
6251 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006252 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006254 generate_FOR(cctx, loop_lvar->lv_idx);
6255 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256
6257 return arg;
6258}
6259
6260/*
6261 * compile "endfor"
6262 */
6263 static char_u *
6264compile_endfor(char_u *arg, cctx_T *cctx)
6265{
6266 garray_T *instr = &cctx->ctx_instr;
6267 scope_T *scope = cctx->ctx_scope;
6268 forscope_T *forscope;
6269 isn_T *isn;
6270
6271 if (scope == NULL || scope->se_type != FOR_SCOPE)
6272 {
6273 emsg(_(e_for));
6274 return NULL;
6275 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006276 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006278 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
6280 // At end of ":for" scope jump back to the FOR instruction.
6281 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6282
6283 // Fill in the "end" label in the FOR statement so it can jump here
6284 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6285 isn->isn_arg.forloop.for_end = instr->ga_len;
6286
6287 // Fill in the "end" label any BREAK statements
6288 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6289
6290 // Below the ":for" scope drop the "expr" list from the stack.
6291 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6292 return NULL;
6293
6294 vim_free(scope);
6295
6296 return arg;
6297}
6298
6299/*
6300 * compile "while expr"
6301 *
6302 * Produces instructions:
6303 * top: EVAL expr Push result of "expr"
6304 * JUMP_IF_FALSE end jump if false
6305 * ... body ...
6306 * JUMP top Jump back to repeat
6307 * end:
6308 *
6309 */
6310 static char_u *
6311compile_while(char_u *arg, cctx_T *cctx)
6312{
6313 char_u *p = arg;
6314 garray_T *instr = &cctx->ctx_instr;
6315 scope_T *scope;
6316
6317 scope = new_scope(cctx, WHILE_SCOPE);
6318 if (scope == NULL)
6319 return NULL;
6320
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322
6323 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006324 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 return NULL;
6326
Bram Moolenaar13106602020-10-04 16:06:05 +02006327 if (bool_on_stack(cctx) == FAIL)
6328 return FAIL;
6329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006331 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 JUMP_IF_FALSE, cctx) == FAIL)
6333 return FAIL;
6334
6335 return p;
6336}
6337
6338/*
6339 * compile "endwhile"
6340 */
6341 static char_u *
6342compile_endwhile(char_u *arg, cctx_T *cctx)
6343{
6344 scope_T *scope = cctx->ctx_scope;
6345
6346 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6347 {
6348 emsg(_(e_while));
6349 return NULL;
6350 }
6351 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006352 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353
6354 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006355 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
6357 // Fill in the "end" label in the WHILE statement so it can jump here.
6358 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006359 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
6361 vim_free(scope);
6362
6363 return arg;
6364}
6365
6366/*
6367 * compile "continue"
6368 */
6369 static char_u *
6370compile_continue(char_u *arg, cctx_T *cctx)
6371{
6372 scope_T *scope = cctx->ctx_scope;
6373
6374 for (;;)
6375 {
6376 if (scope == NULL)
6377 {
6378 emsg(_(e_continue));
6379 return NULL;
6380 }
6381 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6382 break;
6383 scope = scope->se_outer;
6384 }
6385
6386 // Jump back to the FOR or WHILE instruction.
6387 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006388 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6389 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 return arg;
6391}
6392
6393/*
6394 * compile "break"
6395 */
6396 static char_u *
6397compile_break(char_u *arg, cctx_T *cctx)
6398{
6399 scope_T *scope = cctx->ctx_scope;
6400 endlabel_T **el;
6401
6402 for (;;)
6403 {
6404 if (scope == NULL)
6405 {
6406 emsg(_(e_break));
6407 return NULL;
6408 }
6409 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6410 break;
6411 scope = scope->se_outer;
6412 }
6413
6414 // Jump to the end of the FOR or WHILE loop.
6415 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006416 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006418 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6420 return FAIL;
6421
6422 return arg;
6423}
6424
6425/*
6426 * compile "{" start of block
6427 */
6428 static char_u *
6429compile_block(char_u *arg, cctx_T *cctx)
6430{
6431 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6432 return NULL;
6433 return skipwhite(arg + 1);
6434}
6435
6436/*
6437 * compile end of block: drop one scope
6438 */
6439 static void
6440compile_endblock(cctx_T *cctx)
6441{
6442 scope_T *scope = cctx->ctx_scope;
6443
6444 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006445 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 vim_free(scope);
6447}
6448
6449/*
6450 * compile "try"
6451 * Creates a new scope for the try-endtry, pointing to the first catch and
6452 * finally.
6453 * Creates another scope for the "try" block itself.
6454 * TRY instruction sets up exception handling at runtime.
6455 *
6456 * "try"
6457 * TRY -> catch1, -> finally push trystack entry
6458 * ... try block
6459 * "throw {exception}"
6460 * EVAL {exception}
6461 * THROW create exception
6462 * ... try block
6463 * " catch {expr}"
6464 * JUMP -> finally
6465 * catch1: PUSH exeception
6466 * EVAL {expr}
6467 * MATCH
6468 * JUMP nomatch -> catch2
6469 * CATCH remove exception
6470 * ... catch block
6471 * " catch"
6472 * JUMP -> finally
6473 * catch2: CATCH remove exception
6474 * ... catch block
6475 * " finally"
6476 * finally:
6477 * ... finally block
6478 * " endtry"
6479 * ENDTRY pop trystack entry, may rethrow
6480 */
6481 static char_u *
6482compile_try(char_u *arg, cctx_T *cctx)
6483{
6484 garray_T *instr = &cctx->ctx_instr;
6485 scope_T *try_scope;
6486 scope_T *scope;
6487
6488 // scope that holds the jumps that go to catch/finally/endtry
6489 try_scope = new_scope(cctx, TRY_SCOPE);
6490 if (try_scope == NULL)
6491 return NULL;
6492
6493 // "catch" is set when the first ":catch" is found.
6494 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006495 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 if (generate_instr(cctx, ISN_TRY) == NULL)
6497 return NULL;
6498
6499 // scope for the try block itself
6500 scope = new_scope(cctx, BLOCK_SCOPE);
6501 if (scope == NULL)
6502 return NULL;
6503
6504 return arg;
6505}
6506
6507/*
6508 * compile "catch {expr}"
6509 */
6510 static char_u *
6511compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6512{
6513 scope_T *scope = cctx->ctx_scope;
6514 garray_T *instr = &cctx->ctx_instr;
6515 char_u *p;
6516 isn_T *isn;
6517
6518 // end block scope from :try or :catch
6519 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6520 compile_endblock(cctx);
6521 scope = cctx->ctx_scope;
6522
6523 // Error if not in a :try scope
6524 if (scope == NULL || scope->se_type != TRY_SCOPE)
6525 {
6526 emsg(_(e_catch));
6527 return NULL;
6528 }
6529
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006530 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006532 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 return NULL;
6534 }
6535
6536 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006537 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 JUMP_ALWAYS, cctx) == FAIL)
6539 return NULL;
6540
6541 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006542 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 if (isn->isn_arg.try.try_catch == 0)
6544 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006545 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 {
6547 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006548 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 isn->isn_arg.jump.jump_where = instr->ga_len;
6550 }
6551
6552 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006553 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 scope->se_u.se_try.ts_caught_all = TRUE;
6556 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 }
6558 else
6559 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006560 char_u *end;
6561 char_u *pat;
6562 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006563 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006564 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 // Push v:exception, push {expr} and MATCH
6567 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6568
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006569 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006570 if (*end != *p)
6571 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006572 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006573 vim_free(tofree);
6574 return FAIL;
6575 }
6576 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006577 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006578 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006579 len = (int)(end - tofree);
6580 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006581 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006582 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006583 if (pat == NULL)
6584 return FAIL;
6585 if (generate_PUSHS(cctx, pat) == FAIL)
6586 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6589 return NULL;
6590
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006591 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6593 return NULL;
6594 }
6595
6596 if (generate_instr(cctx, ISN_CATCH) == NULL)
6597 return NULL;
6598
6599 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6600 return NULL;
6601 return p;
6602}
6603
6604 static char_u *
6605compile_finally(char_u *arg, cctx_T *cctx)
6606{
6607 scope_T *scope = cctx->ctx_scope;
6608 garray_T *instr = &cctx->ctx_instr;
6609 isn_T *isn;
6610
6611 // end block scope from :try or :catch
6612 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6613 compile_endblock(cctx);
6614 scope = cctx->ctx_scope;
6615
6616 // Error if not in a :try scope
6617 if (scope == NULL || scope->se_type != TRY_SCOPE)
6618 {
6619 emsg(_(e_finally));
6620 return NULL;
6621 }
6622
6623 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006624 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 if (isn->isn_arg.try.try_finally != 0)
6626 {
6627 emsg(_(e_finally_dup));
6628 return NULL;
6629 }
6630
6631 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006632 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633
Bram Moolenaar585fea72020-04-02 22:33:21 +02006634 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006635 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 {
6637 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006638 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006640 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 }
6642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 // TODO: set index in ts_finally_label jumps
6644
6645 return arg;
6646}
6647
6648 static char_u *
6649compile_endtry(char_u *arg, cctx_T *cctx)
6650{
6651 scope_T *scope = cctx->ctx_scope;
6652 garray_T *instr = &cctx->ctx_instr;
6653 isn_T *isn;
6654
6655 // end block scope from :catch or :finally
6656 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6657 compile_endblock(cctx);
6658 scope = cctx->ctx_scope;
6659
6660 // Error if not in a :try scope
6661 if (scope == NULL || scope->se_type != TRY_SCOPE)
6662 {
6663 if (scope == NULL)
6664 emsg(_(e_no_endtry));
6665 else if (scope->se_type == WHILE_SCOPE)
6666 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006667 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 emsg(_(e_endfor));
6669 else
6670 emsg(_(e_endif));
6671 return NULL;
6672 }
6673
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006674 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006677 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 return NULL;
6679 }
6680
6681 // Fill in the "end" label in jumps at the end of the blocks, if not done
6682 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006683 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684
6685 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006686 if (isn->isn_arg.try.try_catch == 0)
6687 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 if (isn->isn_arg.try.try_finally == 0)
6689 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006690
6691 if (scope->se_u.se_try.ts_catch_label != 0)
6692 {
6693 // Last catch without match jumps here
6694 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6695 isn->isn_arg.jump.jump_where = instr->ga_len;
6696 }
6697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 compile_endblock(cctx);
6699
6700 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6701 return NULL;
6702 return arg;
6703}
6704
6705/*
6706 * compile "throw {expr}"
6707 */
6708 static char_u *
6709compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6710{
6711 char_u *p = skipwhite(arg);
6712
Bram Moolenaara5565e42020-05-09 15:44:01 +02006713 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 return NULL;
6715 if (may_generate_2STRING(-1, cctx) == FAIL)
6716 return NULL;
6717 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6718 return NULL;
6719
6720 return p;
6721}
6722
6723/*
6724 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006725 * compile "echomsg expr"
6726 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006727 * compile "execute expr"
6728 */
6729 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006730compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006731{
6732 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006733 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006734 int count = 0;
6735
6736 for (;;)
6737 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006738 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006739 return NULL;
6740 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006741 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006742 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006743 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006744 break;
6745 }
6746
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006747 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6748 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6749 else if (cmdidx == CMD_execute)
6750 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6751 else if (cmdidx == CMD_echomsg)
6752 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6753 else
6754 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 return p;
6756}
6757
6758/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006759 * :put r
6760 * :put ={expr}
6761 */
6762 static char_u *
6763compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6764{
6765 char_u *line = arg;
6766 linenr_T lnum;
6767 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006768 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006769
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006770 eap->regname = *line;
6771
6772 if (eap->regname == '=')
6773 {
6774 char_u *p = line + 1;
6775
6776 if (compile_expr0(&p, cctx) == FAIL)
6777 return NULL;
6778 line = p;
6779 }
6780 else if (eap->regname != NUL)
6781 ++line;
6782
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006783 // "errormsg" will not be set because the range is ADDR_LINES.
6784 // TODO: if the range contains something like "$" or "." need to evaluate
6785 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006786 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6787 return NULL;
6788 if (eap->addr_count == 0)
6789 lnum = -1;
6790 else
6791 lnum = eap->line2;
6792 if (above)
6793 --lnum;
6794
6795 generate_PUT(cctx, eap->regname, lnum);
6796 return line;
6797}
6798
6799/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006800 * A command that is not compiled, execute with legacy code.
6801 */
6802 static char_u *
6803compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6804{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006805 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006806 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006807 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006808
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006809 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006810 goto theend;
6811
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006812 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006813 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006814 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006815 int usefilter = FALSE;
6816
6817 has_expr = argt & (EX_XFILE | EX_EXPAND);
6818
6819 // If the command can be followed by a bar, find the bar and truncate
6820 // it, so that the following command can be compiled.
6821 // The '|' is overwritten with a NUL, it is put back below.
6822 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6823 && *eap->arg == '!')
6824 // :w !filter or :r !filter or :r! filter
6825 usefilter = TRUE;
6826 if ((argt & EX_TRLBAR) && !usefilter)
6827 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006828 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006829 separate_nextcmd(eap);
6830 if (eap->nextcmd != NULL)
6831 nextcmd = eap->nextcmd;
6832 }
6833 }
6834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006835 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6836 {
6837 // expand filename in "syntax include [@group] filename"
6838 has_expr = TRUE;
6839 eap->arg = skipwhite(eap->arg + 7);
6840 if (*eap->arg == '@')
6841 eap->arg = skiptowhite(eap->arg);
6842 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006843
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006844 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006845 {
6846 int count = 0;
6847 char_u *start = skipwhite(line);
6848
6849 // :cmd xxx`=expr1`yyy`=expr2`zzz
6850 // PUSHS ":cmd xxx"
6851 // eval expr1
6852 // PUSHS "yyy"
6853 // eval expr2
6854 // PUSHS "zzz"
6855 // EXECCONCAT 5
6856 for (;;)
6857 {
6858 if (p > start)
6859 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006860 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006861 ++count;
6862 }
6863 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006864 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006865 return NULL;
6866 may_generate_2STRING(-1, cctx);
6867 ++count;
6868 p = skipwhite(p);
6869 if (*p != '`')
6870 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006871 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006872 return NULL;
6873 }
6874 start = p + 1;
6875
6876 p = (char_u *)strstr((char *)start, "`=");
6877 if (p == NULL)
6878 {
6879 if (*skipwhite(start) != NUL)
6880 {
6881 generate_PUSHS(cctx, vim_strsave(start));
6882 ++count;
6883 }
6884 break;
6885 }
6886 }
6887 generate_EXECCONCAT(cctx, count);
6888 }
6889 else
6890 generate_EXEC(cctx, line);
6891
6892theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006893 if (*nextcmd != NUL)
6894 {
6895 // the parser expects a pointer to the bar, put it back
6896 --nextcmd;
6897 *nextcmd = '|';
6898 }
6899
6900 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006901}
6902
6903/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006904 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006905 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006906 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006907 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006908add_def_function(ufunc_T *ufunc)
6909{
6910 dfunc_T *dfunc;
6911
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006912 if (def_functions.ga_len == 0)
6913 {
6914 // The first position is not used, so that a zero uf_dfunc_idx means it
6915 // wasn't set.
6916 if (ga_grow(&def_functions, 1) == FAIL)
6917 return FAIL;
6918 ++def_functions.ga_len;
6919 }
6920
Bram Moolenaar09689a02020-05-09 22:50:08 +02006921 // Add the function to "def_functions".
6922 if (ga_grow(&def_functions, 1) == FAIL)
6923 return FAIL;
6924 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6925 CLEAR_POINTER(dfunc);
6926 dfunc->df_idx = def_functions.ga_len;
6927 ufunc->uf_dfunc_idx = dfunc->df_idx;
6928 dfunc->df_ufunc = ufunc;
6929 ++def_functions.ga_len;
6930 return OK;
6931}
6932
6933/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 * After ex_function() has collected all the function lines: parse and compile
6935 * the lines into instructions.
6936 * Adds the function to "def_functions".
6937 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6938 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006939 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006940 * This can be used recursively through compile_lambda(), which may reallocate
6941 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006942 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006944 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006945compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947 char_u *line = NULL;
6948 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 cctx_T cctx;
6951 garray_T *instr;
6952 int called_emsg_before = called_emsg;
6953 int ret = FAIL;
6954 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006955 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006956 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006957 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006958 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006960 // When using a function that was compiled before: Free old instructions.
6961 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006962 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006964 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6965 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006966 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006968 else
6969 {
6970 if (add_def_function(ufunc) == FAIL)
6971 return FAIL;
6972 new_def_function = TRUE;
6973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974
Bram Moolenaar985116a2020-07-12 17:31:09 +02006975 ufunc->uf_def_status = UF_COMPILING;
6976
Bram Moolenaara80faa82020-04-12 19:37:17 +02006977 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 cctx.ctx_ufunc = ufunc;
6979 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006980 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6982 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6983 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6984 cctx.ctx_type_list = &ufunc->uf_type_list;
6985 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6986 instr = &cctx.ctx_instr;
6987
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006988 // Set the context to the function, it may be compiled when called from
6989 // another script. Set the script version to the most modern one.
6990 // The line number will be set in next_line_from_context().
6991 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6993
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006994 // Make sure error messages are OK.
6995 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6996 if (do_estack_push)
6997 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006998 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006999
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007000 if (ufunc->uf_def_args.ga_len > 0)
7001 {
7002 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007003 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007004 int i;
7005 char_u *arg;
7006 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007007 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007008
7009 // Produce instructions for the default values of optional arguments.
7010 // Store the instruction index in uf_def_arg_idx[] so that we know
7011 // where to start when the function is called, depending on the number
7012 // of arguments.
7013 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7014 if (ufunc->uf_def_arg_idx == NULL)
7015 goto erret;
7016 for (i = 0; i < count; ++i)
7017 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007018 garray_T *stack = &cctx.ctx_type_stack;
7019 type_T *val_type;
7020 int arg_idx = first_def_arg + i;
7021
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007022 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7023 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007024 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007025 goto erret;
7026
7027 // If no type specified use the type of the default value.
7028 // Otherwise check that the default value type matches the
7029 // specified type.
7030 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7031 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007032 {
7033 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007034 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007035 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007036 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7037 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007038 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007039
7040 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007041 goto erret;
7042 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007043 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007044
7045 if (did_set_arg_type)
7046 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007047 }
7048
7049 /*
7050 * Loop over all the lines of the function and generate instructions.
7051 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 for (;;)
7053 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007054 exarg_T ea;
7055 cmdmod_T save_cmdmod;
7056 int starts_with_colon = FALSE;
7057 char_u *cmd;
7058 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007059
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007060 // Bail out on the first error to avoid a flood of errors and report
7061 // the right line number when inside try/catch.
7062 if (emsg_before != called_emsg)
7063 goto erret;
7064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 if (line != NULL && *line == '|')
7066 // the line continues after a '|'
7067 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007068 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007069 && !(*line == '#' && (line == cctx.ctx_line_start
7070 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007072 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 goto erret;
7074 }
7075 else
7076 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007077 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007078 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007079 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007082 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083
Bram Moolenaara80faa82020-04-12 19:37:17 +02007084 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 ea.cmdlinep = &line;
7086 ea.cmd = skipwhite(line);
7087
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007088 // Some things can be recognized by the first character.
7089 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007091 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007092 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007093 if (ea.cmd[1] != '{')
7094 {
7095 line = (char_u *)"";
7096 continue;
7097 }
7098 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007100 case '}':
7101 {
7102 // "}" ends a block scope
7103 scopetype_T stype = cctx.ctx_scope == NULL
7104 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007106 if (stype == BLOCK_SCOPE)
7107 {
7108 compile_endblock(&cctx);
7109 line = ea.cmd;
7110 }
7111 else
7112 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007113 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007114 goto erret;
7115 }
7116 if (line != NULL)
7117 line = skipwhite(ea.cmd + 1);
7118 continue;
7119 }
7120
7121 case '{':
7122 // "{" starts a block scope
7123 // "{'a': 1}->func() is something else
7124 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7125 {
7126 line = compile_block(ea.cmd, &cctx);
7127 continue;
7128 }
7129 break;
7130
7131 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007132 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134 }
7135
7136 /*
7137 * COMMAND MODIFIERS
7138 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007139 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7141 {
7142 if (errormsg != NULL)
7143 goto erret;
7144 // empty line or comment
7145 line = (char_u *)"";
7146 continue;
7147 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007148 // TODO: use modifiers in the command
7149 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007150 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151
7152 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007153 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007155 {
7156 if (*ea.cmd == '(')
7157 // not for "call()"
7158 ea.cmd = p;
7159 else
7160 ea.cmd = skipwhite(ea.cmd);
7161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007163 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007165 char_u *pskip;
7166
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007167 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007168 // find what follows.
7169 // Skip over "var.member", "var[idx]" and the like.
7170 // Also "&opt = val", "$ENV = val" and "@r = val".
7171 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007172 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007173 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007174 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007176 char_u *var_end;
7177 int oplen;
7178 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179
Bram Moolenaar65821722020-08-02 18:58:54 +02007180 if (ea.cmd[0] == '@')
7181 var_end = ea.cmd + 2;
7182 else
7183 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007184 FNE_CHECK_START | FNE_INCL_BR);
7185 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007186 if (oplen > 0)
7187 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007188 size_t len = p - ea.cmd;
7189
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007190 // Recognize an assignment if we recognize the variable
7191 // name:
7192 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007193 // "local = expr" where "local" is a local var.
7194 // "script = expr" where "script" is a script-local var.
7195 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007196 // "&opt = expr"
7197 // "$ENV = expr"
7198 // "@r = expr"
7199 if (*ea.cmd == '&'
7200 || *ea.cmd == '$'
7201 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007202 || ((len) > 2 && ea.cmd[1] == ':')
7203 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007204 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007205 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007206 || script_var_exists(ea.cmd, len,
7207 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007208 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007209 {
7210 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007211 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007212 goto erret;
7213 continue;
7214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 }
7216 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007217
7218 if (*ea.cmd == '[')
7219 {
7220 // [var, var] = expr
7221 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7222 if (line == NULL)
7223 goto erret;
7224 if (line != ea.cmd)
7225 continue;
7226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 }
7228
7229 /*
7230 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007231 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007233 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007234 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007235 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007236 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007237 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007238 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007239 if (!starts_with_colon)
7240 {
7241 emsg(_(e_colon_required_before_a_range));
7242 goto erret;
7243 }
7244 if (ends_excmd2(line, ea.cmd))
7245 {
7246 // A range without a command: jump to the line.
7247 // TODO: compile to a more efficient command, possibly
7248 // calling parse_cmd_address().
7249 ea.cmdidx = CMD_SIZE;
7250 line = compile_exec(line, &ea, &cctx);
7251 goto nextline;
7252 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007253 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007254 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007255 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007256 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007257 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258
7259 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7260 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007261 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007262 {
7263 line += STRLEN(line);
7264 continue;
7265 }
7266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007268 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007270 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007271 iemsg("Command from find_ex_command() not handled");
7272 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 }
7275
Bram Moolenaar3988f642020-08-27 22:43:03 +02007276 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007277 && ea.cmdidx != CMD_elseif
7278 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007279 && ea.cmdidx != CMD_endif
7280 && ea.cmdidx != CMD_endfor
7281 && ea.cmdidx != CMD_endwhile
7282 && ea.cmdidx != CMD_catch
7283 && ea.cmdidx != CMD_finally
7284 && ea.cmdidx != CMD_endtry)
7285 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007286 emsg(_(e_unreachable_code_after_return));
7287 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007288 }
7289
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007290 p = skipwhite(p);
7291 if (ea.cmdidx != CMD_SIZE
7292 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7293 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007294 if (ea.cmdidx >= 0)
7295 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007296 if ((ea.argt & EX_BANG) && *p == '!')
7297 {
7298 ea.forceit = TRUE;
7299 p = skipwhite(p + 1);
7300 }
7301 }
7302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 switch (ea.cmdidx)
7304 {
7305 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007306 ea.arg = p;
7307 line = compile_nested_function(&ea, &cctx);
7308 break;
7309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007311 // TODO: should we allow this, e.g. to declare a global
7312 // function?
7313 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 goto erret;
7315
7316 case CMD_return:
7317 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007318 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 break;
7320
7321 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007322 emsg(_(e_cannot_use_let_in_vim9_script));
7323 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007324 case CMD_var:
7325 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 case CMD_const:
7327 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007328 if (line == p)
7329 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 break;
7331
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007332 case CMD_unlet:
7333 case CMD_unlockvar:
7334 case CMD_lockvar:
7335 line = compile_unletlock(p, &ea, &cctx);
7336 break;
7337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 case CMD_import:
7339 line = compile_import(p, &cctx);
7340 break;
7341
7342 case CMD_if:
7343 line = compile_if(p, &cctx);
7344 break;
7345 case CMD_elseif:
7346 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007347 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 break;
7349 case CMD_else:
7350 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007351 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 break;
7353 case CMD_endif:
7354 line = compile_endif(p, &cctx);
7355 break;
7356
7357 case CMD_while:
7358 line = compile_while(p, &cctx);
7359 break;
7360 case CMD_endwhile:
7361 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007362 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 break;
7364
7365 case CMD_for:
7366 line = compile_for(p, &cctx);
7367 break;
7368 case CMD_endfor:
7369 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007370 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 break;
7372 case CMD_continue:
7373 line = compile_continue(p, &cctx);
7374 break;
7375 case CMD_break:
7376 line = compile_break(p, &cctx);
7377 break;
7378
7379 case CMD_try:
7380 line = compile_try(p, &cctx);
7381 break;
7382 case CMD_catch:
7383 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007384 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 break;
7386 case CMD_finally:
7387 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007388 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 break;
7390 case CMD_endtry:
7391 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007392 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 break;
7394 case CMD_throw:
7395 line = compile_throw(p, &cctx);
7396 break;
7397
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007398 case CMD_eval:
7399 if (compile_expr0(&p, &cctx) == FAIL)
7400 goto erret;
7401
Bram Moolenaar3988f642020-08-27 22:43:03 +02007402 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007403 generate_instr_drop(&cctx, ISN_DROP, 1);
7404
7405 line = skipwhite(p);
7406 break;
7407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007410 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007411 case CMD_echomsg:
7412 case CMD_echoerr:
7413 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007414 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007416 case CMD_put:
7417 ea.cmd = cmd;
7418 line = compile_put(p, &ea, &cctx);
7419 break;
7420
Bram Moolenaar3988f642020-08-27 22:43:03 +02007421 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007422
Bram Moolenaarae616492020-07-28 20:07:27 +02007423 case CMD_append:
7424 case CMD_change:
7425 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007426 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007427 case CMD_xit:
7428 not_in_vim9(&ea);
7429 goto erret;
7430
Bram Moolenaar002262f2020-07-08 17:47:57 +02007431 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007432 if (cctx.ctx_skip != SKIP_YES)
7433 {
7434 semsg(_(e_invalid_command_str), ea.cmd);
7435 goto erret;
7436 }
7437 // We don't check for a next command here.
7438 line = (char_u *)"";
7439 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007442 if (cctx.ctx_skip == SKIP_YES)
7443 {
7444 // We don't check for a next command here.
7445 line = (char_u *)"";
7446 }
7447 else
7448 {
7449 // Not recognized, execute with do_cmdline_cmd().
7450 ea.arg = p;
7451 line = compile_exec(line, &ea, &cctx);
7452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 break;
7454 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007455nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 if (line == NULL)
7457 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007458 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459
7460 if (cctx.ctx_type_stack.ga_len < 0)
7461 {
7462 iemsg("Type stack underflow");
7463 goto erret;
7464 }
7465 }
7466
7467 if (cctx.ctx_scope != NULL)
7468 {
7469 if (cctx.ctx_scope->se_type == IF_SCOPE)
7470 emsg(_(e_endif));
7471 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7472 emsg(_(e_endwhile));
7473 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7474 emsg(_(e_endfor));
7475 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007476 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 goto erret;
7478 }
7479
Bram Moolenaarefd88552020-06-18 20:50:10 +02007480 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 {
7482 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7483 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007484 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 goto erret;
7486 }
7487
7488 // Return zero if there is no return at the end.
7489 generate_PUSHNR(&cctx, 0);
7490 generate_instr(&cctx, ISN_RETURN);
7491 }
7492
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007493 {
7494 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7495 + ufunc->uf_dfunc_idx;
7496 dfunc->df_deleted = FALSE;
7497 dfunc->df_instr = instr->ga_data;
7498 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007499 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007500 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007501 if (cctx.ctx_outer_used)
7502 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007503 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505
7506 ret = OK;
7507
7508erret:
7509 if (ret == FAIL)
7510 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007511 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007512 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7513 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007514
7515 for (idx = 0; idx < instr->ga_len; ++idx)
7516 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007518
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007519 // If using the last entry in the table and it was added above, we
7520 // might as well remove it.
7521 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007522 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007523 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007524 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007525 ufunc->uf_dfunc_idx = 0;
7526 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007527 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007528
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007529 while (cctx.ctx_scope != NULL)
7530 drop_scope(&cctx);
7531
Bram Moolenaar20431c92020-03-20 18:39:46 +01007532 // Don't execute this function body.
7533 ga_clear_strings(&ufunc->uf_lines);
7534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535 if (errormsg != NULL)
7536 emsg(errormsg);
7537 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007538 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 }
7540
7541 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007542 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007543 if (do_estack_push)
7544 estack_pop();
7545
Bram Moolenaar20431c92020-03-20 18:39:46 +01007546 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007547 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007549 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550}
7551
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007552 void
7553set_function_type(ufunc_T *ufunc)
7554{
7555 int varargs = ufunc->uf_va_name != NULL;
7556 int argcount = ufunc->uf_args.ga_len;
7557
7558 // Create a type for the function, with the return type and any
7559 // argument types.
7560 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7561 // The type is included in "tt_args".
7562 if (argcount > 0 || varargs)
7563 {
7564 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7565 argcount, &ufunc->uf_type_list);
7566 // Add argument types to the function type.
7567 if (func_type_add_arg_types(ufunc->uf_func_type,
7568 argcount + varargs,
7569 &ufunc->uf_type_list) == FAIL)
7570 return;
7571 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7572 ufunc->uf_func_type->tt_min_argcount =
7573 argcount - ufunc->uf_def_args.ga_len;
7574 if (ufunc->uf_arg_types == NULL)
7575 {
7576 int i;
7577
7578 // lambda does not have argument types.
7579 for (i = 0; i < argcount; ++i)
7580 ufunc->uf_func_type->tt_args[i] = &t_any;
7581 }
7582 else
7583 mch_memmove(ufunc->uf_func_type->tt_args,
7584 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7585 if (varargs)
7586 {
7587 ufunc->uf_func_type->tt_args[argcount] =
7588 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7589 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7590 }
7591 }
7592 else
7593 // No arguments, can use a predefined type.
7594 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7595 argcount, &ufunc->uf_type_list);
7596}
7597
7598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599/*
7600 * Delete an instruction, free what it contains.
7601 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007602 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603delete_instr(isn_T *isn)
7604{
7605 switch (isn->isn_type)
7606 {
7607 case ISN_EXEC:
7608 case ISN_LOADENV:
7609 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007610 case ISN_LOADB:
7611 case ISN_LOADW:
7612 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007614 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 case ISN_PUSHEXC:
7616 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007617 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007619 case ISN_STOREB:
7620 case ISN_STOREW:
7621 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007622 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 vim_free(isn->isn_arg.string);
7624 break;
7625
7626 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007627 case ISN_STORES:
7628 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 break;
7630
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007631 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007632 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007633 vim_free(isn->isn_arg.unlet.ul_name);
7634 break;
7635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 case ISN_STOREOPT:
7637 vim_free(isn->isn_arg.storeopt.so_name);
7638 break;
7639
7640 case ISN_PUSHBLOB: // push blob isn_arg.blob
7641 blob_unref(isn->isn_arg.blob);
7642 break;
7643
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007644 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007646 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007647#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007648 break;
7649
7650 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007651#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007652 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007653#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007654 break;
7655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656 case ISN_UCALL:
7657 vim_free(isn->isn_arg.ufunc.cuf_name);
7658 break;
7659
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007660 case ISN_FUNCREF:
7661 {
7662 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7663 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007664
7665 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7666 func_ptr_unref(dfunc->df_ufunc);
7667 }
7668 break;
7669
7670 case ISN_DCALL:
7671 {
7672 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7673 + isn->isn_arg.dfunc.cdf_idx;
7674
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007675 if (dfunc->df_ufunc != NULL
7676 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007677 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007678 }
7679 break;
7680
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007681 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007682 {
7683 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7684 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7685
7686 if (ufunc != NULL)
7687 {
7688 // Clear uf_dfunc_idx so that the function is deleted.
7689 clear_def_function(ufunc);
7690 ufunc->uf_dfunc_idx = 0;
7691 func_ptr_unref(ufunc);
7692 }
7693
7694 vim_free(lambda);
7695 vim_free(isn->isn_arg.newfunc.nf_global);
7696 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007697 break;
7698
Bram Moolenaar5e654232020-09-16 15:22:00 +02007699 case ISN_CHECKTYPE:
7700 free_type(isn->isn_arg.type.ct_type);
7701 break;
7702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703 case ISN_2BOOL:
7704 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007705 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 case ISN_ADDBLOB:
7707 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007708 case ISN_ANYINDEX:
7709 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007711 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007713 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715 case ISN_COMPAREANY:
7716 case ISN_COMPAREBLOB:
7717 case ISN_COMPAREBOOL:
7718 case ISN_COMPAREDICT:
7719 case ISN_COMPAREFLOAT:
7720 case ISN_COMPAREFUNC:
7721 case ISN_COMPARELIST:
7722 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723 case ISN_COMPARESPECIAL:
7724 case ISN_COMPARESTRING:
7725 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007726 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 case ISN_DROP:
7728 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007729 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007730 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007732 case ISN_EXECCONCAT:
7733 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007735 case ISN_GETITEM:
7736 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007737 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007738 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007739 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007741 case ISN_LOADBDICT:
7742 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007743 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007745 case ISN_LOADSCRIPT:
7746 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007748 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007749 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007750 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case ISN_NEGATENR:
7752 case ISN_NEWDICT:
7753 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007755 case ISN_OPFLOAT:
7756 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007758 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007759 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 case ISN_PUSHF:
7761 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007763 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007765 case ISN_SHUFFLE:
7766 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007768 case ISN_STOREDICT:
7769 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007770 case ISN_STORENR:
7771 case ISN_STOREOUTER:
7772 case ISN_STOREREG:
7773 case ISN_STORESCRIPT:
7774 case ISN_STOREV:
7775 case ISN_STRINDEX:
7776 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 case ISN_THROW:
7778 case ISN_TRY:
7779 // nothing allocated
7780 break;
7781 }
7782}
7783
7784/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007785 * Free all instructions for "dfunc".
7786 */
7787 static void
7788delete_def_function_contents(dfunc_T *dfunc)
7789{
7790 int idx;
7791
7792 ga_clear(&dfunc->df_def_args_isn);
7793
7794 if (dfunc->df_instr != NULL)
7795 {
7796 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7797 delete_instr(dfunc->df_instr + idx);
7798 VIM_CLEAR(dfunc->df_instr);
7799 }
7800
7801 dfunc->df_deleted = TRUE;
7802}
7803
7804/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007805 * When a user function is deleted, clear the contents of any associated def
7806 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 */
7808 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007809clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007811 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 {
7813 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7814 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815
Bram Moolenaar20431c92020-03-20 18:39:46 +01007816 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007817 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 }
7819}
7820
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007821/*
7822 * Used when a user function is about to be deleted: remove the pointer to it.
7823 * The entry in def_functions is then unused.
7824 */
7825 void
7826unlink_def_function(ufunc_T *ufunc)
7827{
7828 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7829
7830 dfunc->df_ufunc = NULL;
7831}
7832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007834/*
7835 * Free all functions defined with ":def".
7836 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 void
7838free_def_functions(void)
7839{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007840 int idx;
7841
7842 for (idx = 0; idx < def_functions.ga_len; ++idx)
7843 {
7844 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7845
7846 delete_def_function_contents(dfunc);
7847 }
7848
7849 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850}
7851#endif
7852
7853
7854#endif // FEAT_EVAL