blob: 45c35b2e4b29659ab5646bd68b5c817956dfb549 [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 Moolenaara1224cb2020-10-22 12:31:49 +02001463 type_T **argtypes = NULL;
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 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004388 evalarg_T evalarg;
4389
4390 CLEAR_FIELD(evalarg);
4391 evalarg.eval_cctx = cctx;
4392 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004393 return OK;
4394 }
4395
Bram Moolenaar61a89812020-05-07 16:58:17 +02004396 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
4399
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004400 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 if (*p == '?')
4402 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004403 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 garray_T *instr = &cctx->ctx_instr;
4405 garray_T *stack = &cctx->ctx_type_stack;
4406 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004407 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004409 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 int has_const_expr = FALSE;
4411 int const_value = FALSE;
4412 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004414 if (next != NULL)
4415 {
4416 *arg = next_line_from_context(cctx, TRUE);
4417 p = skipwhite(*arg);
4418 }
4419
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004420 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004421 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004422 semsg(_(e_white_space_required_before_and_after_str),
4423 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004424 return FAIL;
4425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426
Bram Moolenaara5565e42020-05-09 15:44:01 +02004427 if (ppconst->pp_used == ppconst_used + 1)
4428 {
4429 // the condition is a constant, we know whether the ? or the :
4430 // expression is to be evaluated.
4431 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004432 if (op_falsy)
4433 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4434 else
4435 {
4436 int error = FALSE;
4437
4438 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4439 &error);
4440 if (error)
4441 return FAIL;
4442 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004443 cctx->ctx_skip = save_skip == SKIP_YES ||
4444 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4445
4446 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4447 // "left ?? right" and "left" is truthy: produce "left"
4448 generate_ppconst(cctx, ppconst);
4449 else
4450 {
4451 clear_tv(&ppconst->pp_tv[ppconst_used]);
4452 --ppconst->pp_used;
4453 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 }
4455 else
4456 {
4457 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004458 if (op_falsy)
4459 end_idx = instr->ga_len;
4460 generate_JUMP(cctx, op_falsy
4461 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4462 if (op_falsy)
4463 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
4466 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004467 *arg = skipwhite(p + 1 + op_falsy);
4468 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004469 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004471 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 if (!has_const_expr)
4474 {
4475 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004477 if (!op_falsy)
4478 {
4479 // remember the type and drop it
4480 --stack->ga_len;
4481 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004483 end_idx = instr->ga_len;
4484 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004485
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004486 // jump here from JUMP_IF_FALSE
4487 isn = ((isn_T *)instr->ga_data) + alt_idx;
4488 isn->isn_arg.jump.jump_where = instr->ga_len;
4489 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004492 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004494 // Check for the ":".
4495 p = may_peek_next_line(cctx, *arg, &next);
4496 if (*p != ':')
4497 {
4498 emsg(_(e_missing_colon));
4499 return FAIL;
4500 }
4501 if (next != NULL)
4502 {
4503 *arg = next_line_from_context(cctx, TRUE);
4504 p = skipwhite(*arg);
4505 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004506
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004507 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4508 {
4509 semsg(_(e_white_space_required_before_and_after_str), ":");
4510 return FAIL;
4511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004513 // evaluate the third expression
4514 if (has_const_expr)
4515 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004516 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004517 *arg = skipwhite(p + 1);
4518 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4519 return FAIL;
4520 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4521 return FAIL;
4522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524 if (!has_const_expr)
4525 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004526 type_T **typep;
4527
Bram Moolenaara5565e42020-05-09 15:44:01 +02004528 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004531 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4532 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004534 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 isn = ((isn_T *)instr->ga_data) + end_idx;
4536 isn->isn_arg.jump.jump_where = instr->ga_len;
4537 }
4538
4539 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 }
4541 return OK;
4542}
4543
4544/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004546 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4547 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548 */
4549 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004550compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551{
4552 ppconst_T ppconst;
4553
4554 CLEAR_FIELD(ppconst);
4555 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4556 {
4557 clear_ppconst(&ppconst);
4558 return FAIL;
4559 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004560 if (is_const != NULL)
4561 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 if (generate_ppconst(cctx, &ppconst) == FAIL)
4563 return FAIL;
4564 return OK;
4565}
4566
4567/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004568 * Toplevel expression.
4569 */
4570 static int
4571compile_expr0(char_u **arg, cctx_T *cctx)
4572{
4573 return compile_expr0_ext(arg, cctx, NULL);
4574}
4575
4576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 * compile "return [expr]"
4578 */
4579 static char_u *
4580compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4581{
4582 char_u *p = arg;
4583 garray_T *stack = &cctx->ctx_type_stack;
4584 type_T *stack_type;
4585
4586 if (*p != NUL && *p != '|' && *p != '\n')
4587 {
4588 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 return NULL;
4591
4592 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4593 if (set_return_type)
4594 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004595 else
4596 {
4597 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4598 && stack_type->tt_type != VAR_VOID
4599 && stack_type->tt_type != VAR_UNKNOWN)
4600 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004601 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004602 return NULL;
4603 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004604 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004605 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004606 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
4609 else
4610 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004611 // "set_return_type" cannot be TRUE, only used for a lambda which
4612 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004613 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4614 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004616 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 return NULL;
4618 }
4619
4620 // No argument, return zero.
4621 generate_PUSHNR(cctx, 0);
4622 }
4623
4624 if (generate_instr(cctx, ISN_RETURN) == NULL)
4625 return NULL;
4626
4627 // "return val | endif" is possible
4628 return skipwhite(p);
4629}
4630
4631/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004632 * Get a line from the compilation context, compatible with exarg_T getline().
4633 * Return a pointer to the line in allocated memory.
4634 * Return NULL for end-of-file or some error.
4635 */
4636 static char_u *
4637exarg_getline(
4638 int c UNUSED,
4639 void *cookie,
4640 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004641 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004642{
4643 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004644 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004645
Bram Moolenaar66250c92020-08-20 15:02:42 +02004646 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004647 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004648 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004649 return NULL;
4650 ++cctx->ctx_lnum;
4651 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4652 // Comment lines result in NULL pointers, skip them.
4653 if (p != NULL)
4654 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004655 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656}
4657
4658/*
4659 * Compile a nested :def command.
4660 */
4661 static char_u *
4662compile_nested_function(exarg_T *eap, cctx_T *cctx)
4663{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004664 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004665 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004666 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004667 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004668 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004669 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004670
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004671 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004672 {
4673 emsg(_(e_cannot_use_bang_with_nested_def));
4674 return NULL;
4675 }
4676
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004677 // Only g:Func() can use a namespace.
4678 if (name_start[1] == ':' && !is_global)
4679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004680 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004681 return NULL;
4682 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004683 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4684 return NULL;
4685
Bram Moolenaar04b12692020-05-04 23:24:44 +02004686 eap->arg = name_end;
4687 eap->getline = exarg_getline;
4688 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004689 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004691 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004692 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004693
Bram Moolenaar822ba242020-05-24 23:00:18 +02004694 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004695 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004696 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004697 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004698 {
4699 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004700 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004701 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004703 if (is_global)
4704 {
4705 char_u *func_name = vim_strnsave(name_start + 2,
4706 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004707
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004708 if (func_name == NULL)
4709 r = FAIL;
4710 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004711 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004712 }
4713 else
4714 {
4715 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004716 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004717 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004718 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004719
Bram Moolenaareef21022020-08-01 22:16:43 +02004720 if (lvar == NULL)
4721 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004722 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004723 return NULL;
4724 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004725
4726 // copy over the block scope IDs
4727 if (block_depth > 0)
4728 {
4729 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4730 if (ufunc->uf_block_ids != NULL)
4731 {
4732 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4733 sizeof(int) * block_depth);
4734 ufunc->uf_block_depth = block_depth;
4735 }
4736 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004737 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004738
Bram Moolenaar61a89812020-05-07 16:58:17 +02004739 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004740 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004741}
4742
4743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 * Return the length of an assignment operator, or zero if there isn't one.
4745 */
4746 int
4747assignment_len(char_u *p, int *heredoc)
4748{
4749 if (*p == '=')
4750 {
4751 if (p[1] == '<' && p[2] == '<')
4752 {
4753 *heredoc = TRUE;
4754 return 3;
4755 }
4756 return 1;
4757 }
4758 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4759 return 2;
4760 if (STRNCMP(p, "..=", 3) == 0)
4761 return 3;
4762 return 0;
4763}
4764
4765// words that cannot be used as a variable
4766static char *reserved[] = {
4767 "true",
4768 "false",
4769 NULL
4770};
4771
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004772typedef enum {
4773 dest_local,
4774 dest_option,
4775 dest_env,
4776 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004777 dest_buffer,
4778 dest_window,
4779 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004780 dest_vimvar,
4781 dest_script,
4782 dest_reg,
4783} assign_dest_T;
4784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004786 * Generate the load instruction for "name".
4787 */
4788 static void
4789generate_loadvar(
4790 cctx_T *cctx,
4791 assign_dest_T dest,
4792 char_u *name,
4793 lvar_T *lvar,
4794 type_T *type)
4795{
4796 switch (dest)
4797 {
4798 case dest_option:
4799 // TODO: check the option exists
4800 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4801 break;
4802 case dest_global:
4803 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4804 break;
4805 case dest_buffer:
4806 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4807 break;
4808 case dest_window:
4809 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4810 break;
4811 case dest_tab:
4812 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4813 break;
4814 case dest_script:
4815 compile_load_scriptvar(cctx,
4816 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4817 break;
4818 case dest_env:
4819 // Include $ in the name here
4820 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4821 break;
4822 case dest_reg:
4823 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4824 break;
4825 case dest_vimvar:
4826 generate_LOADV(cctx, name + 2, TRUE);
4827 break;
4828 case dest_local:
4829 if (lvar->lv_from_outer)
4830 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4831 NULL, type);
4832 else
4833 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4834 break;
4835 }
4836}
4837
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004838 void
4839vim9_declare_error(char_u *name)
4840{
4841 char *scope = "";
4842
4843 switch (*name)
4844 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004845 case 'g': scope = _("global"); break;
4846 case 'b': scope = _("buffer"); break;
4847 case 'w': scope = _("window"); break;
4848 case 't': scope = _("tab"); break;
4849 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004850 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004851 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004852 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004853 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004854 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004855 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004856 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004857 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004858 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004859}
4860
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004861/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004863 * "let name"
4864 * "var name = expr"
4865 * "final name = expr"
4866 * "const name = expr"
4867 * "name = expr"
4868 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 * Return NULL for an error.
4870 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 */
4872 static char_u *
4873compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4874{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004875 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004877 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 char_u *ret = NULL;
4879 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004880 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004881 int scriptvar_sid = 0;
4882 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004885 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 int oplen = 0;
4888 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004889 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004890 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004893 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4894 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 // Skip over the "var" or "[var, var]" to get to any "=".
4897 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4898 if (p == NULL)
4899 return *arg == '[' ? arg : NULL;
4900
4901 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004903 // TODO: should we allow this, and figure out type inference from list
4904 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004905 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 return NULL;
4907 }
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 sp = p;
4910 p = skipwhite(p);
4911 op = p;
4912 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004913
4914 if (var_count > 0 && oplen == 0)
4915 // can be something like "[1, 2]->func()"
4916 return arg;
4917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4919 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004920 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004922 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 if (heredoc)
4925 {
4926 list_T *l;
4927 listitem_T *li;
4928
4929 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004930 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004932 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004933 if (l == NULL)
4934 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935
Bram Moolenaar078269b2020-09-21 20:35:55 +02004936 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004938 // Push each line and the create the list.
4939 FOR_ALL_LIST_ITEMS(l, li)
4940 {
4941 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4942 li->li_tv.vval.v_string = NULL;
4943 }
4944 generate_NEWLIST(cctx, l->lv_len);
4945 type = &t_list_string;
4946 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 list_free(l);
4949 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004954 // for "[var, var] = expr" evaluate the expression here, loop over the
4955 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004956
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 p = skipwhite(op + oplen);
4958 if (compile_expr0(&p, cctx) == FAIL)
4959 return NULL;
4960 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004962 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004964 type_T *stacktype;
4965
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004966 stacktype = stack->ga_len == 0 ? &t_void
4967 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004970 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 goto theend;
4972 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004973 if (need_type(stacktype, &t_list_any, -1, cctx,
4974 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004976 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004977 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4978 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 }
4980 }
4981
4982 /*
4983 * Loop over variables in "[var, var] = expr".
4984 * For "var = expr" and "let var: type" this is done only once.
4985 */
4986 if (var_count > 0)
4987 var_start = skipwhite(arg + 1); // skip over the "["
4988 else
4989 var_start = arg;
4990 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4991 {
4992 char_u *var_end = skip_var_one(var_start, FALSE);
4993 size_t varlen;
4994 int new_local = FALSE;
4995 int opt_type;
4996 int opt_flags = 0;
4997 assign_dest_T dest = dest_local;
4998 int vimvaridx = -1;
4999 lvar_T *lvar = NULL;
5000 lvar_T arg_lvar;
5001 int has_type = FALSE;
5002 int has_index = FALSE;
5003 int instr_count = -1;
5004
Bram Moolenaar65821722020-08-02 18:58:54 +02005005 if (*var_start == '@')
5006 p = var_start + 2;
5007 else
5008 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005009 // skip over the leading "&", "&l:", "&g:" and "$"
5010 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005011 p = to_name_end(p, TRUE);
5012 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013
5014 // "a: type" is declaring variable "a" with a type, not "a:".
5015 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5016 --var_end;
5017 if (is_decl && p == var_start + 2 && p[-1] == ':')
5018 --p;
5019
5020 varlen = p - var_start;
5021 vim_free(name);
5022 name = vim_strnsave(var_start, varlen);
5023 if (name == NULL)
5024 return NULL;
5025 if (!heredoc)
5026 type = &t_any;
5027
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005028 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005029 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005030 int declare_error = FALSE;
5031
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005032 if (*var_start == '&')
5033 {
5034 int cc;
5035 long numval;
5036
5037 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005038 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 emsg(_(e_const_option));
5041 goto theend;
5042 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005043 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 p = var_start;
5045 p = find_option_end(&p, &opt_flags);
5046 if (p == NULL)
5047 {
5048 // cannot happen?
5049 emsg(_(e_letunexp));
5050 goto theend;
5051 }
5052 cc = *p;
5053 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005054 opt_type = get_option_value(skip_option_env_lead(var_start),
5055 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 *p = cc;
5057 if (opt_type == -3)
5058 {
5059 semsg(_(e_unknown_option), var_start);
5060 goto theend;
5061 }
5062 if (opt_type == -2 || opt_type == 0)
5063 type = &t_string;
5064 else
5065 type = &t_number; // both number and boolean option
5066 }
5067 else if (*var_start == '$')
5068 {
5069 dest = dest_env;
5070 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005071 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 }
5073 else if (*var_start == '@')
5074 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005075 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 {
5077 emsg_invreg(var_start[1]);
5078 goto theend;
5079 }
5080 dest = dest_reg;
5081 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005082 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005084 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 {
5086 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005087 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005089 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005090 {
5091 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005092 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005094 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005095 {
5096 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005097 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005099 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005100 {
5101 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005102 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005104 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005105 {
5106 typval_T *vtv;
5107 int di_flags;
5108
5109 vimvaridx = find_vim_var(name + 2, &di_flags);
5110 if (vimvaridx < 0)
5111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005112 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 goto theend;
5114 }
5115 // We use the current value of "sandbox" here, is that OK?
5116 if (var_check_ro(di_flags, name, FALSE))
5117 goto theend;
5118 dest = dest_vimvar;
5119 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005120 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005121 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 }
5123 else
5124 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005125 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126
5127 for (idx = 0; reserved[idx] != NULL; ++idx)
5128 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005129 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005130 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005131 goto theend;
5132 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133
5134 lvar = lookup_local(var_start, varlen, cctx);
5135 if (lvar == NULL)
5136 {
5137 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005138 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5140 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005141 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005142 if (is_decl)
5143 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005144 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 goto theend;
5146 }
5147 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005148 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005149 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 if (lvar != NULL)
5151 {
5152 if (is_decl)
5153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005154 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 goto theend;
5156 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005158 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005160 int script_namespace = varlen > 1
5161 && STRNCMP(var_start, "s:", 2) == 0;
5162 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005163 ? script_var_exists(var_start + 2, varlen - 2,
5164 FALSE, cctx)
5165 : script_var_exists(var_start, varlen,
5166 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005167 imported_T *import =
5168 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005169
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005170 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005172 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5173
5174 if (is_decl)
5175 {
5176 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005177 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005178 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005179 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005180 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005181 name);
5182 goto theend;
5183 }
5184 else if (cctx->ctx_ufunc->uf_script_ctx_version
5185 == SCRIPT_VERSION_VIM9
5186 && script_namespace
5187 && !script_var && import == NULL)
5188 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005189 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005190 goto theend;
5191 }
5192
5193 dest = dest_script;
5194
5195 // existing script-local variables should have a type
5196 scriptvar_sid = current_sctx.sc_sid;
5197 if (import != NULL)
5198 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005199 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005200 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005201 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005202 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005203 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005204 {
5205 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5206 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005207 ((svar_T *)si->sn_var_vals.ga_data)
5208 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005209 type = sv->sv_type;
5210 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005211 }
5212 }
5213 else if (name[1] == ':' && name[2] != NUL)
5214 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005215 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 goto theend;
5217 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005218 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005219 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005220 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005221 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005222 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005223 else if (check_defined(var_start, varlen, cctx) == FAIL)
5224 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005227
5228 if (declare_error)
5229 {
5230 vim9_declare_error(name);
5231 goto theend;
5232 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 }
5234
5235 // handle "a:name" as a name, not index "name" on "a"
5236 if (varlen > 1 || var_start[varlen] != ':')
5237 p = var_end;
5238
5239 if (dest != dest_option)
5240 {
5241 if (is_decl && *p == ':')
5242 {
5243 // parse optional type: "let var: type = expr"
5244 if (!VIM_ISWHITE(p[1]))
5245 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005246 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005247 goto theend;
5248 }
5249 p = skipwhite(p + 1);
5250 type = parse_type(&p, cctx->ctx_type_list);
5251 has_type = TRUE;
5252 }
5253 else if (lvar != NULL)
5254 type = lvar->lv_type;
5255 }
5256
5257 if (oplen == 3 && !heredoc && dest != dest_global
5258 && type->tt_type != VAR_STRING
5259 && type->tt_type != VAR_ANY)
5260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 goto theend;
5263 }
5264
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005265 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 {
5267 if (oplen > 1 && !heredoc)
5268 {
5269 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005270 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 goto theend;
5272 }
5273
5274 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005275 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5276 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 goto theend;
5278 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005279 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280 if (lvar == NULL)
5281 goto theend;
5282 new_local = TRUE;
5283 }
5284
5285 member_type = type;
5286 if (var_end > var_start + varlen)
5287 {
5288 // Something follows after the variable: "var[idx]".
5289 if (is_decl)
5290 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005291 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 goto theend;
5293 }
5294
5295 if (var_start[varlen] == '[')
5296 {
5297 has_index = TRUE;
5298 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005299 member_type = &t_any;
5300 else
5301 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 }
5303 else
5304 {
5305 semsg("Not supported yet: %s", var_start);
5306 goto theend;
5307 }
5308 }
5309 else if (lvar == &arg_lvar)
5310 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005311 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 goto theend;
5313 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005314 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5315 {
5316 semsg(_(e_cannot_assign_to_constant), name);
5317 goto theend;
5318 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319
5320 if (!heredoc)
5321 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005322 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005324 if (oplen > 0 && var_count == 0)
5325 {
5326 // skip over the "=" and the expression
5327 p = skipwhite(op + oplen);
5328 compile_expr0(&p, cctx);
5329 }
5330 }
5331 else if (oplen > 0)
5332 {
5333 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005334 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005335
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005336 // For "var = expr" evaluate the expression.
5337 if (var_count == 0)
5338 {
5339 int r;
5340
5341 // for "+=", "*=", "..=" etc. first load the current value
5342 if (*op != '=')
5343 {
5344 generate_loadvar(cctx, dest, name, lvar, type);
5345
5346 if (has_index)
5347 {
5348 // TODO: get member from list or dict
5349 emsg("Index with operation not supported yet");
5350 goto theend;
5351 }
5352 }
5353
5354 // Compile the expression. Temporarily hide the new local
5355 // variable here, it is not available to this expression.
5356 if (new_local)
5357 --cctx->ctx_locals.ga_len;
5358 instr_count = instr->ga_len;
5359 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005360 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 if (new_local)
5362 ++cctx->ctx_locals.ga_len;
5363 if (r == FAIL)
5364 goto theend;
5365 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005366 else if (semicolon && var_idx == var_count - 1)
5367 {
5368 // For "[var; var] = expr" get the rest of the list
5369 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5370 goto theend;
5371 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005372 else
5373 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 // For "[var, var] = expr" get the "var_idx" item from the
5375 // list.
5376 if (generate_GETITEM(cctx, var_idx) == FAIL)
5377 return FAIL;
5378 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005379
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005380 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005381 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005382 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005383 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005384 if ((stacktype->tt_type == VAR_FUNC
5385 || stacktype->tt_type == VAR_PARTIAL)
5386 && var_wrong_func_name(name, TRUE))
5387 goto theend;
5388
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005389 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005390 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005391 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005393 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005394 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 }
5396 else
5397 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005398 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005399 // for a variable that implies &t_any.
5400 if (stacktype == &t_list_empty)
5401 lvar->lv_type = &t_list_any;
5402 else if (stacktype == &t_dict_empty)
5403 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005404 else if (stacktype == &t_unknown)
5405 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005406 else
5407 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005409 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005410 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005411 {
5412 type_T *use_type = lvar->lv_type;
5413
Bram Moolenaar71abe482020-09-14 22:28:30 +02005414 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005415 if (has_index)
5416 {
5417 use_type = use_type->tt_member;
5418 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005419 // could be indexing "any"
5420 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005421 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005422 if (need_type(stacktype, use_type, -1, cctx,
5423 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005424 goto theend;
5425 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005427 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005428 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005429 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005431 else if (cmdidx == CMD_final)
5432 {
5433 emsg(_(e_final_requires_a_value));
5434 goto theend;
5435 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005438 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005439 goto theend;
5440 }
5441 else if (!has_type || dest == dest_option)
5442 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005443 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 goto theend;
5445 }
5446 else
5447 {
5448 // variables are always initialized
5449 if (ga_grow(instr, 1) == FAIL)
5450 goto theend;
5451 switch (member_type->tt_type)
5452 {
5453 case VAR_BOOL:
5454 generate_PUSHBOOL(cctx, VVAL_FALSE);
5455 break;
5456 case VAR_FLOAT:
5457#ifdef FEAT_FLOAT
5458 generate_PUSHF(cctx, 0.0);
5459#endif
5460 break;
5461 case VAR_STRING:
5462 generate_PUSHS(cctx, NULL);
5463 break;
5464 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005465 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 break;
5467 case VAR_FUNC:
5468 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5469 break;
5470 case VAR_LIST:
5471 generate_NEWLIST(cctx, 0);
5472 break;
5473 case VAR_DICT:
5474 generate_NEWDICT(cctx, 0);
5475 break;
5476 case VAR_JOB:
5477 generate_PUSHJOB(cctx, NULL);
5478 break;
5479 case VAR_CHANNEL:
5480 generate_PUSHCHANNEL(cctx, NULL);
5481 break;
5482 case VAR_NUMBER:
5483 case VAR_UNKNOWN:
5484 case VAR_ANY:
5485 case VAR_PARTIAL:
5486 case VAR_VOID:
5487 case VAR_SPECIAL: // cannot happen
5488 generate_PUSHNR(cctx, 0);
5489 break;
5490 }
5491 }
5492 if (var_count == 0)
5493 end = p;
5494 }
5495
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005496 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005497 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 break;
5499
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005500 if (oplen > 0 && *op != '=')
5501 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005502 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005503 type_T *stacktype;
5504
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 if (*op == '.')
5506 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005507 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005508 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005509 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005510 if (
5511#ifdef FEAT_FLOAT
5512 // If variable is float operation with number is OK.
5513 !(expected == &t_float && stacktype == &t_number) &&
5514#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005515 need_type(stacktype, expected, -1, cctx,
5516 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 goto theend;
5518
5519 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005520 {
5521 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5522 goto theend;
5523 }
5524 else if (*op == '+')
5525 {
5526 if (generate_add_instr(cctx,
5527 operator_type(member_type, stacktype),
5528 member_type, stacktype) == FAIL)
5529 goto theend;
5530 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005531 else if (generate_two_op(cctx, op) == FAIL)
5532 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005536 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 int r;
5538
5539 // Compile the "idx" in "var[idx]".
5540 if (new_local)
5541 --cctx->ctx_locals.ga_len;
5542 p = skipwhite(var_start + varlen + 1);
5543 r = compile_expr0(&p, cctx);
5544 if (new_local)
5545 ++cctx->ctx_locals.ga_len;
5546 if (r == FAIL)
5547 goto theend;
5548 if (*skipwhite(p) != ']')
5549 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005550 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 emsg(_(e_missbrac));
5552 goto theend;
5553 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005554 if (type == &t_any)
5555 {
5556 type_T *idx_type = ((type_T **)stack->ga_data)[
5557 stack->ga_len - 1];
5558 // Index on variable of unknown type: guess the type from the
5559 // index type: number is dict, otherwise dict.
5560 // TODO: should do the assignment at runtime
5561 if (idx_type->tt_type == VAR_NUMBER)
5562 type = &t_list_any;
5563 else
5564 type = &t_dict_any;
5565 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566 if (type->tt_type == VAR_DICT
5567 && may_generate_2STRING(-1, cctx) == FAIL)
5568 goto theend;
5569 if (type->tt_type == VAR_LIST
5570 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005571 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 {
5573 emsg(_(e_number_exp));
5574 goto theend;
5575 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005576
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005577 // Load the dict or list. On the stack we then have:
5578 // - value
5579 // - index
5580 // - variable
5581 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005582
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005583 if (type->tt_type == VAR_LIST)
5584 {
5585 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5586 return FAIL;
5587 }
5588 else if (type->tt_type == VAR_DICT)
5589 {
5590 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5591 return FAIL;
5592 }
5593 else
5594 {
5595 emsg(_(e_listreq));
5596 goto theend;
5597 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005598 }
5599 else
5600 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005601 if (is_decl && cmdidx == CMD_const
5602 && (dest == dest_script || dest == dest_local))
5603 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005604 generate_LOCKCONST(cctx);
5605
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 switch (dest)
5607 {
5608 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005609 generate_STOREOPT(cctx, skip_option_env_lead(name),
5610 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 break;
5612 case dest_global:
5613 // include g: with the name, easier to execute that way
5614 generate_STORE(cctx, ISN_STOREG, 0, name);
5615 break;
5616 case dest_buffer:
5617 // include b: with the name, easier to execute that way
5618 generate_STORE(cctx, ISN_STOREB, 0, name);
5619 break;
5620 case dest_window:
5621 // include w: with the name, easier to execute that way
5622 generate_STORE(cctx, ISN_STOREW, 0, name);
5623 break;
5624 case dest_tab:
5625 // include t: with the name, easier to execute that way
5626 generate_STORE(cctx, ISN_STORET, 0, name);
5627 break;
5628 case dest_env:
5629 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5630 break;
5631 case dest_reg:
5632 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5633 break;
5634 case dest_vimvar:
5635 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5636 break;
5637 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005638 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005639 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 {
5641 char_u *name_s = name;
5642
5643 // Include s: in the name for store_var()
5644 if (name[1] != ':')
5645 {
5646 int len = (int)STRLEN(name) + 3;
5647
5648 name_s = alloc(len);
5649 if (name_s == NULL)
5650 name_s = name;
5651 else
5652 vim_snprintf((char *)name_s, len,
5653 "s:%s", name);
5654 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005655 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5656 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005657 if (name_s != name)
5658 vim_free(name_s);
5659 }
5660 else
5661 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005662 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005663 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005664 break;
5665 case dest_local:
5666 if (lvar != NULL)
5667 {
5668 isn_T *isn = ((isn_T *)instr->ga_data)
5669 + instr->ga_len - 1;
5670
5671 // optimization: turn "var = 123" from ISN_PUSHNR +
5672 // ISN_STORE into ISN_STORENR
5673 if (!lvar->lv_from_outer
5674 && instr->ga_len == instr_count + 1
5675 && isn->isn_type == ISN_PUSHNR)
5676 {
5677 varnumber_T val = isn->isn_arg.number;
5678
5679 isn->isn_type = ISN_STORENR;
5680 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5681 isn->isn_arg.storenr.stnr_val = val;
5682 if (stack->ga_len > 0)
5683 --stack->ga_len;
5684 }
5685 else if (lvar->lv_from_outer)
5686 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005687 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688 else
5689 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5690 }
5691 break;
5692 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005693 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005694
5695 if (var_idx + 1 < var_count)
5696 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005698
5699 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005700 if (var_count > 0 && !semicolon)
5701 {
5702 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5703 goto theend;
5704 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005705
Bram Moolenaarb2097502020-07-19 17:17:02 +02005706 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707
5708theend:
5709 vim_free(name);
5710 return ret;
5711}
5712
5713/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005714 * Check if "name" can be "unlet".
5715 */
5716 int
5717check_vim9_unlet(char_u *name)
5718{
5719 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5720 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005721 // "unlet s:var" is allowed in legacy script.
5722 if (*name == 's' && !script_is_vim9())
5723 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005724 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005725 return FAIL;
5726 }
5727 return OK;
5728}
5729
5730/*
5731 * Callback passed to ex_unletlock().
5732 */
5733 static int
5734compile_unlet(
5735 lval_T *lvp,
5736 char_u *name_end,
5737 exarg_T *eap,
5738 int deep UNUSED,
5739 void *coookie)
5740{
5741 cctx_T *cctx = coookie;
5742
5743 if (lvp->ll_tv == NULL)
5744 {
5745 char_u *p = lvp->ll_name;
5746 int cc = *name_end;
5747 int ret = OK;
5748
5749 // Normal name. Only supports g:, w:, t: and b: namespaces.
5750 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005751 if (*p == '$')
5752 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5753 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005754 ret = FAIL;
5755 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005756 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005757
5758 *name_end = cc;
5759 return ret;
5760 }
5761
5762 // TODO: unlet {list}[idx]
5763 // TODO: unlet {dict}[key]
5764 emsg("Sorry, :unlet not fully implemented yet");
5765 return FAIL;
5766}
5767
5768/*
5769 * compile "unlet var", "lock var" and "unlock var"
5770 * "arg" points to "var".
5771 */
5772 static char_u *
5773compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5774{
5775 char_u *p = arg;
5776
5777 if (eap->cmdidx != CMD_unlet)
5778 {
5779 emsg("Sorry, :lock and unlock not implemented yet");
5780 return NULL;
5781 }
5782
5783 if (*p == '!')
5784 {
5785 p = skipwhite(p + 1);
5786 eap->forceit = TRUE;
5787 }
5788
5789 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5790 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5791}
5792
5793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 * Compile an :import command.
5795 */
5796 static char_u *
5797compile_import(char_u *arg, cctx_T *cctx)
5798{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005799 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800}
5801
5802/*
5803 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5804 */
5805 static int
5806compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5807{
5808 garray_T *instr = &cctx->ctx_instr;
5809 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5810
5811 if (endlabel == NULL)
5812 return FAIL;
5813 endlabel->el_next = *el;
5814 *el = endlabel;
5815 endlabel->el_end_label = instr->ga_len;
5816
5817 generate_JUMP(cctx, when, 0);
5818 return OK;
5819}
5820
5821 static void
5822compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5823{
5824 garray_T *instr = &cctx->ctx_instr;
5825
5826 while (*el != NULL)
5827 {
5828 endlabel_T *cur = (*el);
5829 isn_T *isn;
5830
5831 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5832 isn->isn_arg.jump.jump_where = instr->ga_len;
5833 *el = cur->el_next;
5834 vim_free(cur);
5835 }
5836}
5837
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005838 static void
5839compile_free_jump_to_end(endlabel_T **el)
5840{
5841 while (*el != NULL)
5842 {
5843 endlabel_T *cur = (*el);
5844
5845 *el = cur->el_next;
5846 vim_free(cur);
5847 }
5848}
5849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850/*
5851 * Create a new scope and set up the generic items.
5852 */
5853 static scope_T *
5854new_scope(cctx_T *cctx, scopetype_T type)
5855{
5856 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5857
5858 if (scope == NULL)
5859 return NULL;
5860 scope->se_outer = cctx->ctx_scope;
5861 cctx->ctx_scope = scope;
5862 scope->se_type = type;
5863 scope->se_local_count = cctx->ctx_locals.ga_len;
5864 return scope;
5865}
5866
5867/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005868 * Free the current scope and go back to the outer scope.
5869 */
5870 static void
5871drop_scope(cctx_T *cctx)
5872{
5873 scope_T *scope = cctx->ctx_scope;
5874
5875 if (scope == NULL)
5876 {
5877 iemsg("calling drop_scope() without a scope");
5878 return;
5879 }
5880 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005881 switch (scope->se_type)
5882 {
5883 case IF_SCOPE:
5884 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5885 case FOR_SCOPE:
5886 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5887 case WHILE_SCOPE:
5888 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5889 case TRY_SCOPE:
5890 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5891 case NO_SCOPE:
5892 case BLOCK_SCOPE:
5893 break;
5894 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005895 vim_free(scope);
5896}
5897
5898/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005899 * Check that the top of the type stack has a type that can be used as a
5900 * condition. Give an error and return FAIL if not.
5901 */
5902 static int
5903bool_on_stack(cctx_T *cctx)
5904{
5905 garray_T *stack = &cctx->ctx_type_stack;
5906 type_T *type;
5907
5908 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5909 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005910 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005911 return FAIL;
5912 return OK;
5913}
5914
5915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 * compile "if expr"
5917 *
5918 * "if expr" Produces instructions:
5919 * EVAL expr Push result of "expr"
5920 * JUMP_IF_FALSE end
5921 * ... body ...
5922 * end:
5923 *
5924 * "if expr | else" Produces instructions:
5925 * EVAL expr Push result of "expr"
5926 * JUMP_IF_FALSE else
5927 * ... body ...
5928 * JUMP_ALWAYS end
5929 * else:
5930 * ... body ...
5931 * end:
5932 *
5933 * "if expr1 | elseif expr2 | else" Produces instructions:
5934 * EVAL expr Push result of "expr"
5935 * JUMP_IF_FALSE elseif
5936 * ... body ...
5937 * JUMP_ALWAYS end
5938 * elseif:
5939 * EVAL expr Push result of "expr"
5940 * JUMP_IF_FALSE else
5941 * ... body ...
5942 * JUMP_ALWAYS end
5943 * else:
5944 * ... body ...
5945 * end:
5946 */
5947 static char_u *
5948compile_if(char_u *arg, cctx_T *cctx)
5949{
5950 char_u *p = arg;
5951 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005952 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005955 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
Bram Moolenaara5565e42020-05-09 15:44:01 +02005957 CLEAR_FIELD(ppconst);
5958 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005960 clear_ppconst(&ppconst);
5961 return NULL;
5962 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 if (cctx->ctx_skip == SKIP_YES)
5964 clear_ppconst(&ppconst);
5965 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005966 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005967 int error = FALSE;
5968 int v;
5969
Bram Moolenaara5565e42020-05-09 15:44:01 +02005970 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005971 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005972 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005973 if (error)
5974 return NULL;
5975 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005976 }
5977 else
5978 {
5979 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005980 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005981 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005983 if (bool_on_stack(cctx) == FAIL)
5984 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986
5987 scope = new_scope(cctx, IF_SCOPE);
5988 if (scope == NULL)
5989 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005990 scope->se_skip_save = skip_save;
5991 // "is_had_return" will be reset if any block does not end in :return
5992 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005994 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005995 {
5996 // "where" is set when ":elseif", "else" or ":endif" is found
5997 scope->se_u.se_if.is_if_label = instr->ga_len;
5998 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5999 }
6000 else
6001 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
6003 return p;
6004}
6005
6006 static char_u *
6007compile_elseif(char_u *arg, cctx_T *cctx)
6008{
6009 char_u *p = arg;
6010 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006011 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 isn_T *isn;
6013 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006014 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006015 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016
6017 if (scope == NULL || scope->se_type != IF_SCOPE)
6018 {
6019 emsg(_(e_elseif_without_if));
6020 return NULL;
6021 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006022 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006023 if (!cctx->ctx_had_return)
6024 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006026 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 {
6028 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 return NULL;
6031 // previous "if" or "elseif" jumps here
6032 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6033 isn->isn_arg.jump.jump_where = instr->ga_len;
6034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006036 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006037 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006038 if (cctx->ctx_skip == SKIP_YES)
6039 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006040 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006041 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006042 clear_ppconst(&ppconst);
6043 return NULL;
6044 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006045 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006046 if (scope->se_skip_save == SKIP_YES)
6047 clear_ppconst(&ppconst);
6048 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006049 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006050 int error = FALSE;
6051 int v;
6052
Bram Moolenaar7f141552020-05-09 17:35:53 +02006053 // The expression results in a constant.
6054 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006055 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6056 if (error)
6057 return NULL;
6058 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006059 clear_ppconst(&ppconst);
6060 scope->se_u.se_if.is_if_label = -1;
6061 }
6062 else
6063 {
6064 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006065 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006066 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006067 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006068 if (bool_on_stack(cctx) == FAIL)
6069 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006071 // "where" is set when ":elseif", "else" or ":endif" is found
6072 scope->se_u.se_if.is_if_label = instr->ga_len;
6073 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075
6076 return p;
6077}
6078
6079 static char_u *
6080compile_else(char_u *arg, cctx_T *cctx)
6081{
6082 char_u *p = arg;
6083 garray_T *instr = &cctx->ctx_instr;
6084 isn_T *isn;
6085 scope_T *scope = cctx->ctx_scope;
6086
6087 if (scope == NULL || scope->se_type != IF_SCOPE)
6088 {
6089 emsg(_(e_else_without_if));
6090 return NULL;
6091 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006092 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006093 if (!cctx->ctx_had_return)
6094 scope->se_u.se_if.is_had_return = FALSE;
6095 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
Bram Moolenaarefd88552020-06-18 20:50:10 +02006097 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006098 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006099 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006100 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006101 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006102 if (!cctx->ctx_had_return
6103 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6104 JUMP_ALWAYS, cctx) == FAIL)
6105 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006106 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006108 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006109 {
6110 if (scope->se_u.se_if.is_if_label >= 0)
6111 {
6112 // previous "if" or "elseif" jumps here
6113 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6114 isn->isn_arg.jump.jump_where = instr->ga_len;
6115 scope->se_u.se_if.is_if_label = -1;
6116 }
6117 }
6118
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006119 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006120 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122
6123 return p;
6124}
6125
6126 static char_u *
6127compile_endif(char_u *arg, cctx_T *cctx)
6128{
6129 scope_T *scope = cctx->ctx_scope;
6130 ifscope_T *ifscope;
6131 garray_T *instr = &cctx->ctx_instr;
6132 isn_T *isn;
6133
6134 if (scope == NULL || scope->se_type != IF_SCOPE)
6135 {
6136 emsg(_(e_endif_without_if));
6137 return NULL;
6138 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006139 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006140 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006141 if (!cctx->ctx_had_return)
6142 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006144 if (scope->se_u.se_if.is_if_label >= 0)
6145 {
6146 // previous "if" or "elseif" jumps here
6147 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6148 isn->isn_arg.jump.jump_where = instr->ga_len;
6149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 // Fill in the "end" label in jumps at the end of the blocks.
6151 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006152 cctx->ctx_skip = scope->se_skip_save;
6153
6154 // If all the blocks end in :return and there is an :else then the
6155 // had_return flag is set.
6156 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006158 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 return arg;
6160}
6161
6162/*
6163 * compile "for var in expr"
6164 *
6165 * Produces instructions:
6166 * PUSHNR -1
6167 * STORE loop-idx Set index to -1
6168 * EVAL expr Push result of "expr"
6169 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6170 * - if beyond end, jump to "end"
6171 * - otherwise get item from list and push it
6172 * STORE var Store item in "var"
6173 * ... body ...
6174 * JUMP top Jump back to repeat
6175 * end: DROP Drop the result of "expr"
6176 *
6177 */
6178 static char_u *
6179compile_for(char_u *arg, cctx_T *cctx)
6180{
6181 char_u *p;
6182 size_t varlen;
6183 garray_T *instr = &cctx->ctx_instr;
6184 garray_T *stack = &cctx->ctx_type_stack;
6185 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006186 lvar_T *loop_lvar; // loop iteration variable
6187 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 type_T *vartype;
6189
6190 // TODO: list of variables: "for [key, value] in dict"
6191 // parse "var"
6192 for (p = arg; eval_isnamec1(*p); ++p)
6193 ;
6194 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006195 var_lvar = lookup_local(arg, varlen, cctx);
6196 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006198 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 return NULL;
6200 }
6201
6202 // consume "in"
6203 p = skipwhite(p);
6204 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6205 {
6206 emsg(_(e_missing_in));
6207 return NULL;
6208 }
6209 p = skipwhite(p + 2);
6210
6211
6212 scope = new_scope(cctx, FOR_SCOPE);
6213 if (scope == NULL)
6214 return NULL;
6215
6216 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006217 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6218 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006219 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006220 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006221 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224
6225 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006226 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6227 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006228 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006229 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006230 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006234 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235
6236 // compile "expr", it remains on the stack until "endfor"
6237 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006238 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006239 {
6240 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006244 // Now that we know the type of "var", check that it is a list, now or at
6245 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006247 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006249 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 return NULL;
6251 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006252 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006253 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
6255 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006256 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006258 generate_FOR(cctx, loop_lvar->lv_idx);
6259 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
6261 return arg;
6262}
6263
6264/*
6265 * compile "endfor"
6266 */
6267 static char_u *
6268compile_endfor(char_u *arg, cctx_T *cctx)
6269{
6270 garray_T *instr = &cctx->ctx_instr;
6271 scope_T *scope = cctx->ctx_scope;
6272 forscope_T *forscope;
6273 isn_T *isn;
6274
6275 if (scope == NULL || scope->se_type != FOR_SCOPE)
6276 {
6277 emsg(_(e_for));
6278 return NULL;
6279 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006280 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006282 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283
6284 // At end of ":for" scope jump back to the FOR instruction.
6285 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6286
6287 // Fill in the "end" label in the FOR statement so it can jump here
6288 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6289 isn->isn_arg.forloop.for_end = instr->ga_len;
6290
6291 // Fill in the "end" label any BREAK statements
6292 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6293
6294 // Below the ":for" scope drop the "expr" list from the stack.
6295 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6296 return NULL;
6297
6298 vim_free(scope);
6299
6300 return arg;
6301}
6302
6303/*
6304 * compile "while expr"
6305 *
6306 * Produces instructions:
6307 * top: EVAL expr Push result of "expr"
6308 * JUMP_IF_FALSE end jump if false
6309 * ... body ...
6310 * JUMP top Jump back to repeat
6311 * end:
6312 *
6313 */
6314 static char_u *
6315compile_while(char_u *arg, cctx_T *cctx)
6316{
6317 char_u *p = arg;
6318 garray_T *instr = &cctx->ctx_instr;
6319 scope_T *scope;
6320
6321 scope = new_scope(cctx, WHILE_SCOPE);
6322 if (scope == NULL)
6323 return NULL;
6324
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006325 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
6327 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006328 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 return NULL;
6330
Bram Moolenaar13106602020-10-04 16:06:05 +02006331 if (bool_on_stack(cctx) == FAIL)
6332 return FAIL;
6333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006335 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 JUMP_IF_FALSE, cctx) == FAIL)
6337 return FAIL;
6338
6339 return p;
6340}
6341
6342/*
6343 * compile "endwhile"
6344 */
6345 static char_u *
6346compile_endwhile(char_u *arg, cctx_T *cctx)
6347{
6348 scope_T *scope = cctx->ctx_scope;
6349
6350 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6351 {
6352 emsg(_(e_while));
6353 return NULL;
6354 }
6355 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006356 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
6358 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006359 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
6361 // Fill in the "end" label in the WHILE statement so it can jump here.
6362 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006363 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364
6365 vim_free(scope);
6366
6367 return arg;
6368}
6369
6370/*
6371 * compile "continue"
6372 */
6373 static char_u *
6374compile_continue(char_u *arg, cctx_T *cctx)
6375{
6376 scope_T *scope = cctx->ctx_scope;
6377
6378 for (;;)
6379 {
6380 if (scope == NULL)
6381 {
6382 emsg(_(e_continue));
6383 return NULL;
6384 }
6385 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6386 break;
6387 scope = scope->se_outer;
6388 }
6389
6390 // Jump back to the FOR or WHILE instruction.
6391 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006392 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6393 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 return arg;
6395}
6396
6397/*
6398 * compile "break"
6399 */
6400 static char_u *
6401compile_break(char_u *arg, cctx_T *cctx)
6402{
6403 scope_T *scope = cctx->ctx_scope;
6404 endlabel_T **el;
6405
6406 for (;;)
6407 {
6408 if (scope == NULL)
6409 {
6410 emsg(_(e_break));
6411 return NULL;
6412 }
6413 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6414 break;
6415 scope = scope->se_outer;
6416 }
6417
6418 // Jump to the end of the FOR or WHILE loop.
6419 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006420 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006422 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6424 return FAIL;
6425
6426 return arg;
6427}
6428
6429/*
6430 * compile "{" start of block
6431 */
6432 static char_u *
6433compile_block(char_u *arg, cctx_T *cctx)
6434{
6435 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6436 return NULL;
6437 return skipwhite(arg + 1);
6438}
6439
6440/*
6441 * compile end of block: drop one scope
6442 */
6443 static void
6444compile_endblock(cctx_T *cctx)
6445{
6446 scope_T *scope = cctx->ctx_scope;
6447
6448 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006449 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 vim_free(scope);
6451}
6452
6453/*
6454 * compile "try"
6455 * Creates a new scope for the try-endtry, pointing to the first catch and
6456 * finally.
6457 * Creates another scope for the "try" block itself.
6458 * TRY instruction sets up exception handling at runtime.
6459 *
6460 * "try"
6461 * TRY -> catch1, -> finally push trystack entry
6462 * ... try block
6463 * "throw {exception}"
6464 * EVAL {exception}
6465 * THROW create exception
6466 * ... try block
6467 * " catch {expr}"
6468 * JUMP -> finally
6469 * catch1: PUSH exeception
6470 * EVAL {expr}
6471 * MATCH
6472 * JUMP nomatch -> catch2
6473 * CATCH remove exception
6474 * ... catch block
6475 * " catch"
6476 * JUMP -> finally
6477 * catch2: CATCH remove exception
6478 * ... catch block
6479 * " finally"
6480 * finally:
6481 * ... finally block
6482 * " endtry"
6483 * ENDTRY pop trystack entry, may rethrow
6484 */
6485 static char_u *
6486compile_try(char_u *arg, cctx_T *cctx)
6487{
6488 garray_T *instr = &cctx->ctx_instr;
6489 scope_T *try_scope;
6490 scope_T *scope;
6491
6492 // scope that holds the jumps that go to catch/finally/endtry
6493 try_scope = new_scope(cctx, TRY_SCOPE);
6494 if (try_scope == NULL)
6495 return NULL;
6496
6497 // "catch" is set when the first ":catch" is found.
6498 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006499 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500 if (generate_instr(cctx, ISN_TRY) == NULL)
6501 return NULL;
6502
6503 // scope for the try block itself
6504 scope = new_scope(cctx, BLOCK_SCOPE);
6505 if (scope == NULL)
6506 return NULL;
6507
6508 return arg;
6509}
6510
6511/*
6512 * compile "catch {expr}"
6513 */
6514 static char_u *
6515compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6516{
6517 scope_T *scope = cctx->ctx_scope;
6518 garray_T *instr = &cctx->ctx_instr;
6519 char_u *p;
6520 isn_T *isn;
6521
6522 // end block scope from :try or :catch
6523 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6524 compile_endblock(cctx);
6525 scope = cctx->ctx_scope;
6526
6527 // Error if not in a :try scope
6528 if (scope == NULL || scope->se_type != TRY_SCOPE)
6529 {
6530 emsg(_(e_catch));
6531 return NULL;
6532 }
6533
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006534 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006536 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 return NULL;
6538 }
6539
6540 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006541 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 JUMP_ALWAYS, cctx) == FAIL)
6543 return NULL;
6544
6545 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006546 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 if (isn->isn_arg.try.try_catch == 0)
6548 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006549 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 {
6551 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006552 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 isn->isn_arg.jump.jump_where = instr->ga_len;
6554 }
6555
6556 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006557 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006559 scope->se_u.se_try.ts_caught_all = TRUE;
6560 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 }
6562 else
6563 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006564 char_u *end;
6565 char_u *pat;
6566 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006567 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006568 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 // Push v:exception, push {expr} and MATCH
6571 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6572
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006573 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006574 if (*end != *p)
6575 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006576 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006577 vim_free(tofree);
6578 return FAIL;
6579 }
6580 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006581 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006582 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006583 len = (int)(end - tofree);
6584 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006585 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006586 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006587 if (pat == NULL)
6588 return FAIL;
6589 if (generate_PUSHS(cctx, pat) == FAIL)
6590 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6593 return NULL;
6594
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006595 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6597 return NULL;
6598 }
6599
6600 if (generate_instr(cctx, ISN_CATCH) == NULL)
6601 return NULL;
6602
6603 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6604 return NULL;
6605 return p;
6606}
6607
6608 static char_u *
6609compile_finally(char_u *arg, cctx_T *cctx)
6610{
6611 scope_T *scope = cctx->ctx_scope;
6612 garray_T *instr = &cctx->ctx_instr;
6613 isn_T *isn;
6614
6615 // end block scope from :try or :catch
6616 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6617 compile_endblock(cctx);
6618 scope = cctx->ctx_scope;
6619
6620 // Error if not in a :try scope
6621 if (scope == NULL || scope->se_type != TRY_SCOPE)
6622 {
6623 emsg(_(e_finally));
6624 return NULL;
6625 }
6626
6627 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 if (isn->isn_arg.try.try_finally != 0)
6630 {
6631 emsg(_(e_finally_dup));
6632 return NULL;
6633 }
6634
6635 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006636 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637
Bram Moolenaar585fea72020-04-02 22:33:21 +02006638 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006639 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 {
6641 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006642 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006644 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 }
6646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 // TODO: set index in ts_finally_label jumps
6648
6649 return arg;
6650}
6651
6652 static char_u *
6653compile_endtry(char_u *arg, cctx_T *cctx)
6654{
6655 scope_T *scope = cctx->ctx_scope;
6656 garray_T *instr = &cctx->ctx_instr;
6657 isn_T *isn;
6658
6659 // end block scope from :catch or :finally
6660 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6661 compile_endblock(cctx);
6662 scope = cctx->ctx_scope;
6663
6664 // Error if not in a :try scope
6665 if (scope == NULL || scope->se_type != TRY_SCOPE)
6666 {
6667 if (scope == NULL)
6668 emsg(_(e_no_endtry));
6669 else if (scope->se_type == WHILE_SCOPE)
6670 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006671 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 emsg(_(e_endfor));
6673 else
6674 emsg(_(e_endif));
6675 return NULL;
6676 }
6677
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006678 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6680 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006681 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 return NULL;
6683 }
6684
6685 // Fill in the "end" label in jumps at the end of the blocks, if not done
6686 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006687 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688
6689 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006690 if (isn->isn_arg.try.try_catch == 0)
6691 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 if (isn->isn_arg.try.try_finally == 0)
6693 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006694
6695 if (scope->se_u.se_try.ts_catch_label != 0)
6696 {
6697 // Last catch without match jumps here
6698 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6699 isn->isn_arg.jump.jump_where = instr->ga_len;
6700 }
6701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 compile_endblock(cctx);
6703
6704 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6705 return NULL;
6706 return arg;
6707}
6708
6709/*
6710 * compile "throw {expr}"
6711 */
6712 static char_u *
6713compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6714{
6715 char_u *p = skipwhite(arg);
6716
Bram Moolenaara5565e42020-05-09 15:44:01 +02006717 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 return NULL;
6719 if (may_generate_2STRING(-1, cctx) == FAIL)
6720 return NULL;
6721 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6722 return NULL;
6723
6724 return p;
6725}
6726
6727/*
6728 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006729 * compile "echomsg expr"
6730 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006731 * compile "execute expr"
6732 */
6733 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006734compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006735{
6736 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006737 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006738 int count = 0;
6739
6740 for (;;)
6741 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006742 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006743 return NULL;
6744 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006745 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006746 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006747 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006748 break;
6749 }
6750
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006751 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6752 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6753 else if (cmdidx == CMD_execute)
6754 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6755 else if (cmdidx == CMD_echomsg)
6756 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6757 else
6758 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 return p;
6760}
6761
6762/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006763 * :put r
6764 * :put ={expr}
6765 */
6766 static char_u *
6767compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6768{
6769 char_u *line = arg;
6770 linenr_T lnum;
6771 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006772 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006773
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006774 eap->regname = *line;
6775
6776 if (eap->regname == '=')
6777 {
6778 char_u *p = line + 1;
6779
6780 if (compile_expr0(&p, cctx) == FAIL)
6781 return NULL;
6782 line = p;
6783 }
6784 else if (eap->regname != NUL)
6785 ++line;
6786
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006787 // "errormsg" will not be set because the range is ADDR_LINES.
6788 // TODO: if the range contains something like "$" or "." need to evaluate
6789 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006790 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6791 return NULL;
6792 if (eap->addr_count == 0)
6793 lnum = -1;
6794 else
6795 lnum = eap->line2;
6796 if (above)
6797 --lnum;
6798
6799 generate_PUT(cctx, eap->regname, lnum);
6800 return line;
6801}
6802
6803/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006804 * A command that is not compiled, execute with legacy code.
6805 */
6806 static char_u *
6807compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6808{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006809 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006810 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006811 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006812
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006813 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006814 goto theend;
6815
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006816 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006817 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006818 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006819 int usefilter = FALSE;
6820
6821 has_expr = argt & (EX_XFILE | EX_EXPAND);
6822
6823 // If the command can be followed by a bar, find the bar and truncate
6824 // it, so that the following command can be compiled.
6825 // The '|' is overwritten with a NUL, it is put back below.
6826 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6827 && *eap->arg == '!')
6828 // :w !filter or :r !filter or :r! filter
6829 usefilter = TRUE;
6830 if ((argt & EX_TRLBAR) && !usefilter)
6831 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006832 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006833 separate_nextcmd(eap);
6834 if (eap->nextcmd != NULL)
6835 nextcmd = eap->nextcmd;
6836 }
6837 }
6838
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006839 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6840 {
6841 // expand filename in "syntax include [@group] filename"
6842 has_expr = TRUE;
6843 eap->arg = skipwhite(eap->arg + 7);
6844 if (*eap->arg == '@')
6845 eap->arg = skiptowhite(eap->arg);
6846 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006847
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006848 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006849 {
6850 int count = 0;
6851 char_u *start = skipwhite(line);
6852
6853 // :cmd xxx`=expr1`yyy`=expr2`zzz
6854 // PUSHS ":cmd xxx"
6855 // eval expr1
6856 // PUSHS "yyy"
6857 // eval expr2
6858 // PUSHS "zzz"
6859 // EXECCONCAT 5
6860 for (;;)
6861 {
6862 if (p > start)
6863 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006864 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006865 ++count;
6866 }
6867 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006868 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006869 return NULL;
6870 may_generate_2STRING(-1, cctx);
6871 ++count;
6872 p = skipwhite(p);
6873 if (*p != '`')
6874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006875 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006876 return NULL;
6877 }
6878 start = p + 1;
6879
6880 p = (char_u *)strstr((char *)start, "`=");
6881 if (p == NULL)
6882 {
6883 if (*skipwhite(start) != NUL)
6884 {
6885 generate_PUSHS(cctx, vim_strsave(start));
6886 ++count;
6887 }
6888 break;
6889 }
6890 }
6891 generate_EXECCONCAT(cctx, count);
6892 }
6893 else
6894 generate_EXEC(cctx, line);
6895
6896theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006897 if (*nextcmd != NUL)
6898 {
6899 // the parser expects a pointer to the bar, put it back
6900 --nextcmd;
6901 *nextcmd = '|';
6902 }
6903
6904 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006905}
6906
6907/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006908 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006909 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006910 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006911 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006912add_def_function(ufunc_T *ufunc)
6913{
6914 dfunc_T *dfunc;
6915
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006916 if (def_functions.ga_len == 0)
6917 {
6918 // The first position is not used, so that a zero uf_dfunc_idx means it
6919 // wasn't set.
6920 if (ga_grow(&def_functions, 1) == FAIL)
6921 return FAIL;
6922 ++def_functions.ga_len;
6923 }
6924
Bram Moolenaar09689a02020-05-09 22:50:08 +02006925 // Add the function to "def_functions".
6926 if (ga_grow(&def_functions, 1) == FAIL)
6927 return FAIL;
6928 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6929 CLEAR_POINTER(dfunc);
6930 dfunc->df_idx = def_functions.ga_len;
6931 ufunc->uf_dfunc_idx = dfunc->df_idx;
6932 dfunc->df_ufunc = ufunc;
6933 ++def_functions.ga_len;
6934 return OK;
6935}
6936
6937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 * After ex_function() has collected all the function lines: parse and compile
6939 * the lines into instructions.
6940 * Adds the function to "def_functions".
6941 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6942 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006943 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006944 * This can be used recursively through compile_lambda(), which may reallocate
6945 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006946 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006948 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006949compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 char_u *line = NULL;
6952 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 cctx_T cctx;
6955 garray_T *instr;
6956 int called_emsg_before = called_emsg;
6957 int ret = FAIL;
6958 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006959 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006960 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006961 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006962 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006964 // When using a function that was compiled before: Free old instructions.
6965 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006966 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006968 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6969 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006970 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006972 else
6973 {
6974 if (add_def_function(ufunc) == FAIL)
6975 return FAIL;
6976 new_def_function = TRUE;
6977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978
Bram Moolenaar985116a2020-07-12 17:31:09 +02006979 ufunc->uf_def_status = UF_COMPILING;
6980
Bram Moolenaara80faa82020-04-12 19:37:17 +02006981 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 cctx.ctx_ufunc = ufunc;
6983 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006984 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6986 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6987 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6988 cctx.ctx_type_list = &ufunc->uf_type_list;
6989 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6990 instr = &cctx.ctx_instr;
6991
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006992 // Set the context to the function, it may be compiled when called from
6993 // another script. Set the script version to the most modern one.
6994 // The line number will be set in next_line_from_context().
6995 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6997
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006998 // Make sure error messages are OK.
6999 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7000 if (do_estack_push)
7001 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007002 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007003
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007004 if (ufunc->uf_def_args.ga_len > 0)
7005 {
7006 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007007 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007008 int i;
7009 char_u *arg;
7010 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007011 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007012
7013 // Produce instructions for the default values of optional arguments.
7014 // Store the instruction index in uf_def_arg_idx[] so that we know
7015 // where to start when the function is called, depending on the number
7016 // of arguments.
7017 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7018 if (ufunc->uf_def_arg_idx == NULL)
7019 goto erret;
7020 for (i = 0; i < count; ++i)
7021 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007022 garray_T *stack = &cctx.ctx_type_stack;
7023 type_T *val_type;
7024 int arg_idx = first_def_arg + i;
7025
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007026 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7027 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007028 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007029 goto erret;
7030
7031 // If no type specified use the type of the default value.
7032 // Otherwise check that the default value type matches the
7033 // specified type.
7034 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7035 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007036 {
7037 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007038 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007039 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007040 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7041 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007042 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007043
7044 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007045 goto erret;
7046 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007047 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007048
7049 if (did_set_arg_type)
7050 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007051 }
7052
7053 /*
7054 * Loop over all the lines of the function and generate instructions.
7055 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 for (;;)
7057 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007058 exarg_T ea;
7059 cmdmod_T save_cmdmod;
7060 int starts_with_colon = FALSE;
7061 char_u *cmd;
7062 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007063
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007064 // Bail out on the first error to avoid a flood of errors and report
7065 // the right line number when inside try/catch.
7066 if (emsg_before != called_emsg)
7067 goto erret;
7068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 if (line != NULL && *line == '|')
7070 // the line continues after a '|'
7071 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007072 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007073 && !(*line == '#' && (line == cctx.ctx_line_start
7074 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007076 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 goto erret;
7078 }
7079 else
7080 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007081 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007082 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007083 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007086 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087
Bram Moolenaara80faa82020-04-12 19:37:17 +02007088 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 ea.cmdlinep = &line;
7090 ea.cmd = skipwhite(line);
7091
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007092 // Some things can be recognized by the first character.
7093 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007095 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007096 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007097 if (ea.cmd[1] != '{')
7098 {
7099 line = (char_u *)"";
7100 continue;
7101 }
7102 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007104 case '}':
7105 {
7106 // "}" ends a block scope
7107 scopetype_T stype = cctx.ctx_scope == NULL
7108 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007110 if (stype == BLOCK_SCOPE)
7111 {
7112 compile_endblock(&cctx);
7113 line = ea.cmd;
7114 }
7115 else
7116 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007117 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007118 goto erret;
7119 }
7120 if (line != NULL)
7121 line = skipwhite(ea.cmd + 1);
7122 continue;
7123 }
7124
7125 case '{':
7126 // "{" starts a block scope
7127 // "{'a': 1}->func() is something else
7128 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7129 {
7130 line = compile_block(ea.cmd, &cctx);
7131 continue;
7132 }
7133 break;
7134
7135 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007136 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007137 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 }
7139
7140 /*
7141 * COMMAND MODIFIERS
7142 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007143 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7145 {
7146 if (errormsg != NULL)
7147 goto erret;
7148 // empty line or comment
7149 line = (char_u *)"";
7150 continue;
7151 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007152 // TODO: use modifiers in the command
7153 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007154 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155
7156 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007157 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007159 {
7160 if (*ea.cmd == '(')
7161 // not for "call()"
7162 ea.cmd = p;
7163 else
7164 ea.cmd = skipwhite(ea.cmd);
7165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007167 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007169 char_u *pskip;
7170
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007171 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007172 // find what follows.
7173 // Skip over "var.member", "var[idx]" and the like.
7174 // Also "&opt = val", "$ENV = val" and "@r = val".
7175 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007176 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007177 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007178 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 char_u *var_end;
7181 int oplen;
7182 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183
Bram Moolenaar65821722020-08-02 18:58:54 +02007184 if (ea.cmd[0] == '@')
7185 var_end = ea.cmd + 2;
7186 else
7187 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007188 FNE_CHECK_START | FNE_INCL_BR);
7189 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007190 if (oplen > 0)
7191 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007192 size_t len = p - ea.cmd;
7193
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007194 // Recognize an assignment if we recognize the variable
7195 // name:
7196 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007197 // "local = expr" where "local" is a local var.
7198 // "script = expr" where "script" is a script-local var.
7199 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007200 // "&opt = expr"
7201 // "$ENV = expr"
7202 // "@r = expr"
7203 if (*ea.cmd == '&'
7204 || *ea.cmd == '$'
7205 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007206 || ((len) > 2 && ea.cmd[1] == ':')
7207 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007208 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007209 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007210 || script_var_exists(ea.cmd, len,
7211 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007212 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007213 {
7214 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007215 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007216 goto erret;
7217 continue;
7218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 }
7220 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007221
7222 if (*ea.cmd == '[')
7223 {
7224 // [var, var] = expr
7225 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7226 if (line == NULL)
7227 goto erret;
7228 if (line != ea.cmd)
7229 continue;
7230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 }
7232
7233 /*
7234 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007235 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007237 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007238 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007239 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007240 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007241 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007242 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007243 if (!starts_with_colon)
7244 {
7245 emsg(_(e_colon_required_before_a_range));
7246 goto erret;
7247 }
7248 if (ends_excmd2(line, ea.cmd))
7249 {
7250 // A range without a command: jump to the line.
7251 // TODO: compile to a more efficient command, possibly
7252 // calling parse_cmd_address().
7253 ea.cmdidx = CMD_SIZE;
7254 line = compile_exec(line, &ea, &cctx);
7255 goto nextline;
7256 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007257 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007258 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007259 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007260 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007261 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262
7263 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7264 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007265 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007266 {
7267 line += STRLEN(line);
7268 continue;
7269 }
7270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007272 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007274 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007275 iemsg("Command from find_ex_command() not handled");
7276 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 }
7279
Bram Moolenaar3988f642020-08-27 22:43:03 +02007280 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007281 && ea.cmdidx != CMD_elseif
7282 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007283 && ea.cmdidx != CMD_endif
7284 && ea.cmdidx != CMD_endfor
7285 && ea.cmdidx != CMD_endwhile
7286 && ea.cmdidx != CMD_catch
7287 && ea.cmdidx != CMD_finally
7288 && ea.cmdidx != CMD_endtry)
7289 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007290 emsg(_(e_unreachable_code_after_return));
7291 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007292 }
7293
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007294 p = skipwhite(p);
7295 if (ea.cmdidx != CMD_SIZE
7296 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7297 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007298 if (ea.cmdidx >= 0)
7299 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007300 if ((ea.argt & EX_BANG) && *p == '!')
7301 {
7302 ea.forceit = TRUE;
7303 p = skipwhite(p + 1);
7304 }
7305 }
7306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 switch (ea.cmdidx)
7308 {
7309 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007310 ea.arg = p;
7311 line = compile_nested_function(&ea, &cctx);
7312 break;
7313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007315 // TODO: should we allow this, e.g. to declare a global
7316 // function?
7317 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 goto erret;
7319
7320 case CMD_return:
7321 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007322 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 break;
7324
7325 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007326 emsg(_(e_cannot_use_let_in_vim9_script));
7327 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007328 case CMD_var:
7329 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 case CMD_const:
7331 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007332 if (line == p)
7333 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 break;
7335
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007336 case CMD_unlet:
7337 case CMD_unlockvar:
7338 case CMD_lockvar:
7339 line = compile_unletlock(p, &ea, &cctx);
7340 break;
7341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 case CMD_import:
7343 line = compile_import(p, &cctx);
7344 break;
7345
7346 case CMD_if:
7347 line = compile_if(p, &cctx);
7348 break;
7349 case CMD_elseif:
7350 line = compile_elseif(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_else:
7354 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007355 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 break;
7357 case CMD_endif:
7358 line = compile_endif(p, &cctx);
7359 break;
7360
7361 case CMD_while:
7362 line = compile_while(p, &cctx);
7363 break;
7364 case CMD_endwhile:
7365 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007366 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 break;
7368
7369 case CMD_for:
7370 line = compile_for(p, &cctx);
7371 break;
7372 case CMD_endfor:
7373 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007374 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 break;
7376 case CMD_continue:
7377 line = compile_continue(p, &cctx);
7378 break;
7379 case CMD_break:
7380 line = compile_break(p, &cctx);
7381 break;
7382
7383 case CMD_try:
7384 line = compile_try(p, &cctx);
7385 break;
7386 case CMD_catch:
7387 line = compile_catch(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_finally:
7391 line = compile_finally(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_endtry:
7395 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007396 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 break;
7398 case CMD_throw:
7399 line = compile_throw(p, &cctx);
7400 break;
7401
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007402 case CMD_eval:
7403 if (compile_expr0(&p, &cctx) == FAIL)
7404 goto erret;
7405
Bram Moolenaar3988f642020-08-27 22:43:03 +02007406 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007407 generate_instr_drop(&cctx, ISN_DROP, 1);
7408
7409 line = skipwhite(p);
7410 break;
7411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007414 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007415 case CMD_echomsg:
7416 case CMD_echoerr:
7417 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007418 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007420 case CMD_put:
7421 ea.cmd = cmd;
7422 line = compile_put(p, &ea, &cctx);
7423 break;
7424
Bram Moolenaar3988f642020-08-27 22:43:03 +02007425 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007426
Bram Moolenaarae616492020-07-28 20:07:27 +02007427 case CMD_append:
7428 case CMD_change:
7429 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007430 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007431 case CMD_xit:
7432 not_in_vim9(&ea);
7433 goto erret;
7434
Bram Moolenaar002262f2020-07-08 17:47:57 +02007435 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007436 if (cctx.ctx_skip != SKIP_YES)
7437 {
7438 semsg(_(e_invalid_command_str), ea.cmd);
7439 goto erret;
7440 }
7441 // We don't check for a next command here.
7442 line = (char_u *)"";
7443 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007446 if (cctx.ctx_skip == SKIP_YES)
7447 {
7448 // We don't check for a next command here.
7449 line = (char_u *)"";
7450 }
7451 else
7452 {
7453 // Not recognized, execute with do_cmdline_cmd().
7454 ea.arg = p;
7455 line = compile_exec(line, &ea, &cctx);
7456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 break;
7458 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007459nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 if (line == NULL)
7461 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007462 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463
7464 if (cctx.ctx_type_stack.ga_len < 0)
7465 {
7466 iemsg("Type stack underflow");
7467 goto erret;
7468 }
7469 }
7470
7471 if (cctx.ctx_scope != NULL)
7472 {
7473 if (cctx.ctx_scope->se_type == IF_SCOPE)
7474 emsg(_(e_endif));
7475 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7476 emsg(_(e_endwhile));
7477 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7478 emsg(_(e_endfor));
7479 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007480 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 goto erret;
7482 }
7483
Bram Moolenaarefd88552020-06-18 20:50:10 +02007484 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 {
7486 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7487 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007488 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 goto erret;
7490 }
7491
7492 // Return zero if there is no return at the end.
7493 generate_PUSHNR(&cctx, 0);
7494 generate_instr(&cctx, ISN_RETURN);
7495 }
7496
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007497 {
7498 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7499 + ufunc->uf_dfunc_idx;
7500 dfunc->df_deleted = FALSE;
7501 dfunc->df_instr = instr->ga_data;
7502 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007503 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007504 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007505 if (cctx.ctx_outer_used)
7506 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007507 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509
7510 ret = OK;
7511
7512erret:
7513 if (ret == FAIL)
7514 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007515 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7517 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007518
7519 for (idx = 0; idx < instr->ga_len; ++idx)
7520 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007522
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007523 // If using the last entry in the table and it was added above, we
7524 // might as well remove it.
7525 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007526 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007527 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007528 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007529 ufunc->uf_dfunc_idx = 0;
7530 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007531 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007532
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007533 while (cctx.ctx_scope != NULL)
7534 drop_scope(&cctx);
7535
Bram Moolenaar20431c92020-03-20 18:39:46 +01007536 // Don't execute this function body.
7537 ga_clear_strings(&ufunc->uf_lines);
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 if (errormsg != NULL)
7540 emsg(errormsg);
7541 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007542 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 }
7544
7545 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007546 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007547 if (do_estack_push)
7548 estack_pop();
7549
Bram Moolenaar20431c92020-03-20 18:39:46 +01007550 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007551 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007553 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554}
7555
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007556 void
7557set_function_type(ufunc_T *ufunc)
7558{
7559 int varargs = ufunc->uf_va_name != NULL;
7560 int argcount = ufunc->uf_args.ga_len;
7561
7562 // Create a type for the function, with the return type and any
7563 // argument types.
7564 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7565 // The type is included in "tt_args".
7566 if (argcount > 0 || varargs)
7567 {
7568 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7569 argcount, &ufunc->uf_type_list);
7570 // Add argument types to the function type.
7571 if (func_type_add_arg_types(ufunc->uf_func_type,
7572 argcount + varargs,
7573 &ufunc->uf_type_list) == FAIL)
7574 return;
7575 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7576 ufunc->uf_func_type->tt_min_argcount =
7577 argcount - ufunc->uf_def_args.ga_len;
7578 if (ufunc->uf_arg_types == NULL)
7579 {
7580 int i;
7581
7582 // lambda does not have argument types.
7583 for (i = 0; i < argcount; ++i)
7584 ufunc->uf_func_type->tt_args[i] = &t_any;
7585 }
7586 else
7587 mch_memmove(ufunc->uf_func_type->tt_args,
7588 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7589 if (varargs)
7590 {
7591 ufunc->uf_func_type->tt_args[argcount] =
7592 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7593 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7594 }
7595 }
7596 else
7597 // No arguments, can use a predefined type.
7598 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7599 argcount, &ufunc->uf_type_list);
7600}
7601
7602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603/*
7604 * Delete an instruction, free what it contains.
7605 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007606 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607delete_instr(isn_T *isn)
7608{
7609 switch (isn->isn_type)
7610 {
7611 case ISN_EXEC:
7612 case ISN_LOADENV:
7613 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007614 case ISN_LOADB:
7615 case ISN_LOADW:
7616 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007618 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 case ISN_PUSHEXC:
7620 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007621 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007623 case ISN_STOREB:
7624 case ISN_STOREW:
7625 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007626 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 vim_free(isn->isn_arg.string);
7628 break;
7629
7630 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007631 case ISN_STORES:
7632 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 break;
7634
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007635 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007636 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007637 vim_free(isn->isn_arg.unlet.ul_name);
7638 break;
7639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 case ISN_STOREOPT:
7641 vim_free(isn->isn_arg.storeopt.so_name);
7642 break;
7643
7644 case ISN_PUSHBLOB: // push blob isn_arg.blob
7645 blob_unref(isn->isn_arg.blob);
7646 break;
7647
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007648 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007650 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007651#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007652 break;
7653
7654 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007655#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007656 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007657#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007658 break;
7659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 case ISN_UCALL:
7661 vim_free(isn->isn_arg.ufunc.cuf_name);
7662 break;
7663
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007664 case ISN_FUNCREF:
7665 {
7666 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7667 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007668
7669 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7670 func_ptr_unref(dfunc->df_ufunc);
7671 }
7672 break;
7673
7674 case ISN_DCALL:
7675 {
7676 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7677 + isn->isn_arg.dfunc.cdf_idx;
7678
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007679 if (dfunc->df_ufunc != NULL
7680 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007681 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007682 }
7683 break;
7684
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007685 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007686 {
7687 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7688 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7689
7690 if (ufunc != NULL)
7691 {
7692 // Clear uf_dfunc_idx so that the function is deleted.
7693 clear_def_function(ufunc);
7694 ufunc->uf_dfunc_idx = 0;
7695 func_ptr_unref(ufunc);
7696 }
7697
7698 vim_free(lambda);
7699 vim_free(isn->isn_arg.newfunc.nf_global);
7700 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007701 break;
7702
Bram Moolenaar5e654232020-09-16 15:22:00 +02007703 case ISN_CHECKTYPE:
7704 free_type(isn->isn_arg.type.ct_type);
7705 break;
7706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 case ISN_2BOOL:
7708 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007709 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 case ISN_ADDBLOB:
7711 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007712 case ISN_ANYINDEX:
7713 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007715 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007717 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 case ISN_COMPAREANY:
7720 case ISN_COMPAREBLOB:
7721 case ISN_COMPAREBOOL:
7722 case ISN_COMPAREDICT:
7723 case ISN_COMPAREFLOAT:
7724 case ISN_COMPAREFUNC:
7725 case ISN_COMPARELIST:
7726 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 case ISN_COMPARESPECIAL:
7728 case ISN_COMPARESTRING:
7729 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007730 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 case ISN_DROP:
7732 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007733 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007734 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007736 case ISN_EXECCONCAT:
7737 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007739 case ISN_GETITEM:
7740 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007741 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007742 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007743 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007745 case ISN_LOADBDICT:
7746 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007747 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007749 case ISN_LOADSCRIPT:
7750 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007752 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007753 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007754 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 case ISN_NEGATENR:
7756 case ISN_NEWDICT:
7757 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007759 case ISN_OPFLOAT:
7760 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007762 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007763 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 case ISN_PUSHF:
7765 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007767 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007769 case ISN_SHUFFLE:
7770 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007772 case ISN_STOREDICT:
7773 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007774 case ISN_STORENR:
7775 case ISN_STOREOUTER:
7776 case ISN_STOREREG:
7777 case ISN_STORESCRIPT:
7778 case ISN_STOREV:
7779 case ISN_STRINDEX:
7780 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 case ISN_THROW:
7782 case ISN_TRY:
7783 // nothing allocated
7784 break;
7785 }
7786}
7787
7788/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007789 * Free all instructions for "dfunc".
7790 */
7791 static void
7792delete_def_function_contents(dfunc_T *dfunc)
7793{
7794 int idx;
7795
7796 ga_clear(&dfunc->df_def_args_isn);
7797
7798 if (dfunc->df_instr != NULL)
7799 {
7800 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7801 delete_instr(dfunc->df_instr + idx);
7802 VIM_CLEAR(dfunc->df_instr);
7803 }
7804
7805 dfunc->df_deleted = TRUE;
7806}
7807
7808/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007809 * When a user function is deleted, clear the contents of any associated def
7810 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 */
7812 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007813clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007815 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 {
7817 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7818 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819
Bram Moolenaar20431c92020-03-20 18:39:46 +01007820 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007821 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 }
7823}
7824
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007825/*
7826 * Used when a user function is about to be deleted: remove the pointer to it.
7827 * The entry in def_functions is then unused.
7828 */
7829 void
7830unlink_def_function(ufunc_T *ufunc)
7831{
7832 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7833
7834 dfunc->df_ufunc = NULL;
7835}
7836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007838/*
7839 * Free all functions defined with ":def".
7840 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 void
7842free_def_functions(void)
7843{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007844 int idx;
7845
7846 for (idx = 0; idx < def_functions.ga_len; ++idx)
7847 {
7848 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7849
7850 delete_def_function_contents(dfunc);
7851 }
7852
7853 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854}
7855#endif
7856
7857
7858#endif // FEAT_EVAL