blob: 4f1b62c7b3d5cc342123d0e1d9769ebebd3d62c5 [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 Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264 * Lookup a script-local variable in the current script, possibly defined in a
265 * block that contains the function "cctx->ctx_ufunc".
266 * "cctx" is NULL at the script level.
267 * if "len" is <= 0 "name" must be NUL terminated.
268 * Return NULL when not found.
269 */
270 static sallvar_T *
271find_script_var(char_u *name, size_t len, cctx_T *cctx)
272{
273 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
274 hashitem_T *hi;
275 int cc;
276 sallvar_T *sav;
277 ufunc_T *ufunc;
278
279 // Find the list of all script variables with the right name.
280 if (len > 0)
281 {
282 cc = name[len];
283 name[len] = NUL;
284 }
285 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
286 if (len > 0)
287 name[len] = cc;
288 if (HASHITEM_EMPTY(hi))
289 return NULL;
290
291 sav = HI2SAV(hi);
292 if (sav->sav_block_id == 0 || cctx == NULL)
293 // variable defined in the script scope or not in a function.
294 return sav;
295
296 // Go over the variables with this name and find one that was visible
297 // from the function.
298 ufunc = cctx->ctx_ufunc;
299 while (sav != NULL)
300 {
301 int idx;
302
303 // Go over the blocks that this function was defined in. If the
304 // variable block ID matches it was visible to the function.
305 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
306 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
307 return sav;
308 sav = sav->sav_next;
309 }
310
311 return NULL;
312}
313
314/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200315 * Returnd TRUE if the script context is Vim9 script.
316 */
317 static int
318script_is_vim9()
319{
320 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
321}
322
323/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200324 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200325 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
326 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 * Returns OK or FAIL.
329 */
330 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200335 if (current_sctx.sc_sid <= 0)
336 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 is_vim9_script = script_is_vim9();
338 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200339 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340 if (is_vim9_script)
341 {
342 // Check script variables that were visible where the function was
343 // defined.
344 if (find_script_var(name, len, cctx) != NULL)
345 return OK;
346 }
347 else
348 {
349 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
350 dictitem_T *di;
351 int cc;
352
353 // Check script variables that are currently visible
354 cc = name[len];
355 name[len] = NUL;
356 di = find_var_in_ht(ht, 0, name, TRUE);
357 name[len] = cc;
358 if (di != NULL)
359 return OK;
360 }
361
362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363}
364
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365/*
366 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200367 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200368 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 * Return FAIL and give an error if it defined.
370 */
371 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 int c = p[len];
375 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200376
377 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200378 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200380 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100384 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200385 // A local or script-local function can shadow a global function.
386 if (ufunc == NULL || !func_is_global(ufunc)
387 || (p[0] == 'g' && p[1] == ':'))
388 {
389 p[len] = c;
390 semsg(_(e_name_already_defined_str), p);
391 return FAIL;
392 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200394 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100395 return OK;
396}
397
Bram Moolenaar65b95452020-07-19 14:03:09 +0200398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200455 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200463 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 */
465 static int
466may_generate_2STRING(int offset, cctx_T *cctx)
467{
468 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 switch ((*type)->tt_type)
474 {
475 // nothing to be done
476 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 // conversion possible
479 case VAR_SPECIAL:
480 case VAR_BOOL:
481 case VAR_NUMBER:
482 case VAR_FLOAT:
483 break;
484
485 // conversion possible (with runtime check)
486 case VAR_ANY:
487 case VAR_UNKNOWN:
488 isntype = ISN_2STRING_ANY;
489 break;
490
491 // conversion not possible
492 case VAR_VOID:
493 case VAR_BLOB:
494 case VAR_FUNC:
495 case VAR_PARTIAL:
496 case VAR_LIST:
497 case VAR_DICT:
498 case VAR_JOB:
499 case VAR_CHANNEL:
500 to_string_error((*type)->tt_type);
501 return FAIL;
502 }
503
504 *type = &t_string;
505 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507 isn->isn_arg.number = offset;
508
509 return OK;
510}
511
512 static int
513check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
514{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200517 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 {
519 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return FAIL;
524 }
525 return OK;
526}
527
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200528 static int
529generate_add_instr(
530 cctx_T *cctx,
531 vartype_T vartype,
532 type_T *type1,
533 type_T *type2)
534{
535 isn_T *isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR
537 : vartype == VAR_LIST ? ISN_ADDLIST
538 : vartype == VAR_BLOB ? ISN_ADDBLOB
539#ifdef FEAT_FLOAT
540 : vartype == VAR_FLOAT ? ISN_OPFLOAT
541#endif
542 : ISN_OPANY, 1);
543
544 if (vartype != VAR_LIST && vartype != VAR_BLOB
545 && type1->tt_type != VAR_ANY
546 && type2->tt_type != VAR_ANY
547 && check_number_or_float(
548 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
549 return FAIL;
550
551 if (isn != NULL)
552 isn->isn_arg.op.op_type = EXPR_ADD;
553 return isn == NULL ? FAIL : OK;
554}
555
556/*
557 * Get the type to use for an instruction for an operation on "type1" and
558 * "type2". If they are matching use a type-specific instruction. Otherwise
559 * fall back to runtime type checking.
560 */
561 static vartype_T
562operator_type(type_T *type1, type_T *type2)
563{
564 if (type1->tt_type == type2->tt_type
565 && (type1->tt_type == VAR_NUMBER
566 || type1->tt_type == VAR_LIST
567#ifdef FEAT_FLOAT
568 || type1->tt_type == VAR_FLOAT
569#endif
570 || type1->tt_type == VAR_BLOB))
571 return type1->tt_type;
572 return VAR_ANY;
573}
574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575/*
576 * Generate an instruction with two arguments. The instruction depends on the
577 * type of the arguments.
578 */
579 static int
580generate_two_op(cctx_T *cctx, char_u *op)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583 type_T *type1;
584 type_T *type2;
585 vartype_T vartype;
586 isn_T *isn;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_OK_IF_SKIP(cctx);
589
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200590 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
592 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 switch (*op)
596 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200597 case '+':
598 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 break;
601
602 case '-':
603 case '*':
604 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
605 op) == FAIL)
606 return FAIL;
607 if (vartype == VAR_NUMBER)
608 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
609#ifdef FEAT_FLOAT
610 else if (vartype == VAR_FLOAT)
611 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
612#endif
613 else
614 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
615 if (isn != NULL)
616 isn->isn_arg.op.op_type = *op == '*'
617 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
618 break;
619
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200622 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 && type2->tt_type != VAR_NUMBER))
624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200625 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 }
628 isn = generate_instr_drop(cctx,
629 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = EXPR_REM;
632 break;
633 }
634
635 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 type_T *type = &t_any;
639
640#ifdef FEAT_FLOAT
641 // float+number and number+float results in float
642 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
643 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
644 type = &t_float;
645#endif
646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
647 }
648
649 return OK;
650}
651
652/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200653 * Get the instruction to use for comparing "type1" with "type2"
654 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200656 static isntype_T
657get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658{
659 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 if (type1 == VAR_UNKNOWN)
662 type1 = VAR_ANY;
663 if (type2 == VAR_UNKNOWN)
664 type2 = VAR_ANY;
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (type1 == type2)
667 {
668 switch (type1)
669 {
670 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
671 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
672 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
673 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
674 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
675 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
676 case VAR_LIST: isntype = ISN_COMPARELIST; break;
677 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
678 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 default: isntype = ISN_COMPAREANY; break;
680 }
681 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
684 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
685 isntype = ISN_COMPAREANY;
686
687 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
688 && (isntype == ISN_COMPAREBOOL
689 || isntype == ISN_COMPARESPECIAL
690 || isntype == ISN_COMPARENR
691 || isntype == ISN_COMPAREFLOAT))
692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200693 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200695 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 }
697 if (isntype == ISN_DROP
698 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
699 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
700 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
701 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
702 && exptype != EXPR_IS && exptype != EXPR_ISNOT
703 && (type1 == VAR_BLOB || type2 == VAR_BLOB
704 || type1 == VAR_LIST || type2 == VAR_LIST))))
705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200706 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return isntype;
711}
712
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200713 int
714check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
715{
716 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
717 return FAIL;
718 return OK;
719}
720
Bram Moolenaara5565e42020-05-09 15:44:01 +0200721/*
722 * Generate an ISN_COMPARE* instruction with a boolean result.
723 */
724 static int
725generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
726{
727 isntype_T isntype;
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730 vartype_T type1;
731 vartype_T type2;
732
733 RETURN_OK_IF_SKIP(cctx);
734
735 // Get the known type of the two items on the stack. If they are matching
736 // use a type-specific instruction. Otherwise fall back to runtime type
737 // checking.
738 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
739 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
740 isntype = get_compare_isn(exptype, type1, type2);
741 if (isntype == ISN_DROP)
742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if ((isn = generate_instr(cctx, isntype)) == NULL)
745 return FAIL;
746 isn->isn_arg.op.op_type = exptype;
747 isn->isn_arg.op.op_ic = ic;
748
749 // takes two arguments, puts one bool back
750 if (stack->ga_len >= 2)
751 {
752 --stack->ga_len;
753 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
754 }
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_2BOOL instruction.
761 */
762 static int
763generate_2BOOL(cctx_T *cctx, int invert)
764{
765 isn_T *isn;
766 garray_T *stack = &cctx->ctx_type_stack;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
770 return FAIL;
771 isn->isn_arg.number = invert;
772
773 // type becomes bool
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775
776 return OK;
777}
778
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200779/*
780 * Generate an ISN_COND2BOOL instruction.
781 */
782 static int
783generate_COND2BOOL(cctx_T *cctx)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
788 RETURN_OK_IF_SKIP(cctx);
789 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
790 return FAIL;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200799generate_TYPECHECK(
800 cctx_T *cctx,
801 type_T *expected,
802 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
Bram Moolenaar080457c2020-03-03 21:53:32 +0100807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
809 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200810 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 isn->isn_arg.type.ct_off = offset;
812
Bram Moolenaar5e654232020-09-16 15:22:00 +0200813 // type becomes expected
814 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 return OK;
817}
818
819/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200820 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821 * used. Return FALSE if the types will never match.
822 */
823 static int
824use_typecheck(type_T *actual, type_T *expected)
825{
826 if (actual->tt_type == VAR_ANY
827 || actual->tt_type == VAR_UNKNOWN
828 || (actual->tt_type == VAR_FUNC
829 && (expected->tt_type == VAR_FUNC
830 || expected->tt_type == VAR_PARTIAL)
831 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
832 return TRUE;
833 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
834 && actual->tt_type == expected->tt_type)
835 // This takes care of a nested list or dict.
836 return use_typecheck(actual->tt_member, expected->tt_member);
837 return FALSE;
838}
839
840/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200843 * - "actual" is a type that can be "expected" type: add a runtime check; or
844 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200845 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
846 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 */
848 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200849need_type(
850 type_T *actual,
851 type_T *expected,
852 int offset,
853 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200854 int silent,
855 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200857 if (expected == &t_bool && actual != &t_bool
858 && (actual->tt_flags & TTFLAG_BOOL_OK))
859 {
860 // Using "0", "1" or the result of an expression with "&&" or "||" as a
861 // boolean is OK but requires a conversion.
862 generate_2BOOL(cctx, FALSE);
863 return OK;
864 }
865
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200866 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200868
869 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 // If it's a constant a runtime check makes no sense.
871 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 generate_TYPECHECK(cctx, expected, offset);
874 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200875 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876
877 if (!silent)
878 type_mismatch(expected, actual);
879 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880}
881
882/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 * Generate an ISN_PUSHNR instruction.
884 */
885 static int
886generate_PUSHNR(cctx_T *cctx, varnumber_T number)
887{
888 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200889 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar080457c2020-03-03 21:53:32 +0100891 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
893 return FAIL;
894 isn->isn_arg.number = number;
895
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200896 if (number == 0 || number == 1)
897 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200898 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200899
900 // A 0 or 1 number can also be used as a bool.
901 if (type != NULL)
902 {
903 type->tt_type = VAR_NUMBER;
904 type->tt_flags = TTFLAG_BOOL_OK;
905 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
906 }
907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 return OK;
909}
910
911/*
912 * Generate an ISN_PUSHBOOL instruction.
913 */
914 static int
915generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
921 return FAIL;
922 isn->isn_arg.number = number;
923
924 return OK;
925}
926
927/*
928 * Generate an ISN_PUSHSPEC instruction.
929 */
930 static int
931generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
937 return FAIL;
938 isn->isn_arg.number = number;
939
940 return OK;
941}
942
943#ifdef FEAT_FLOAT
944/*
945 * Generate an ISN_PUSHF instruction.
946 */
947 static int
948generate_PUSHF(cctx_T *cctx, float_T fnumber)
949{
950 isn_T *isn;
951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
954 return FAIL;
955 isn->isn_arg.fnumber = fnumber;
956
957 return OK;
958}
959#endif
960
961/*
962 * Generate an ISN_PUSHS instruction.
963 * Consumes "str".
964 */
965 static int
966generate_PUSHS(cctx_T *cctx, char_u *str)
967{
968 isn_T *isn;
969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
972 return FAIL;
973 isn->isn_arg.string = str;
974
975 return OK;
976}
977
978/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100979 * Generate an ISN_PUSHCHANNEL instruction.
980 * Consumes "channel".
981 */
982 static int
983generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
984{
985 isn_T *isn;
986
Bram Moolenaar080457c2020-03-03 21:53:32 +0100987 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100988 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
989 return FAIL;
990 isn->isn_arg.channel = channel;
991
992 return OK;
993}
994
995/*
996 * Generate an ISN_PUSHJOB instruction.
997 * Consumes "job".
998 */
999 static int
1000generate_PUSHJOB(cctx_T *cctx, job_T *job)
1001{
1002 isn_T *isn;
1003
Bram Moolenaar080457c2020-03-03 21:53:32 +01001004 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001005 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001006 return FAIL;
1007 isn->isn_arg.job = job;
1008
1009 return OK;
1010}
1011
1012/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 * Generate an ISN_PUSHBLOB instruction.
1014 * Consumes "blob".
1015 */
1016 static int
1017generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.blob = blob;
1025
1026 return OK;
1027}
1028
1029/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 * Generate an ISN_PUSHFUNC instruction with name "name".
1031 * Consumes "name".
1032 */
1033 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001034generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001039 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001041 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 * Generate an ISN_GETITEM instruction with "index".
1048 */
1049 static int
1050generate_GETITEM(cctx_T *cctx, int index)
1051{
1052 isn_T *isn;
1053 garray_T *stack = &cctx->ctx_type_stack;
1054 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1055 type_T *item_type = &t_any;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001061 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001062 emsg(_(e_listreq));
1063 return FAIL;
1064 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001065 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001066 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.number = index;
1069
1070 // add the item type to the type stack
1071 if (ga_grow(stack, 1) == FAIL)
1072 return FAIL;
1073 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1074 ++stack->ga_len;
1075 return OK;
1076}
1077
1078/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001079 * Generate an ISN_SLICE instruction with "count".
1080 */
1081 static int
1082generate_SLICE(cctx_T *cctx, int count)
1083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
1087 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = count;
1090 return OK;
1091}
1092
1093/*
1094 * Generate an ISN_CHECKLEN instruction with "min_len".
1095 */
1096 static int
1097generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1098{
1099 isn_T *isn;
1100
1101 RETURN_OK_IF_SKIP(cctx);
1102
1103 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.checklen.cl_min_len = min_len;
1106 isn->isn_arg.checklen.cl_more_OK = more_OK;
1107
1108 return OK;
1109}
1110
1111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 * Generate an ISN_STORE instruction.
1113 */
1114 static int
1115generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1116{
1117 isn_T *isn;
1118
Bram Moolenaar080457c2020-03-03 21:53:32 +01001119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1121 return FAIL;
1122 if (name != NULL)
1123 isn->isn_arg.string = vim_strsave(name);
1124 else
1125 isn->isn_arg.number = idx;
1126
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1132 */
1133 static int
1134generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1140 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001141 isn->isn_arg.storenr.stnr_idx = idx;
1142 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143
1144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_STOREOPT instruction
1149 */
1150 static int
1151generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001156 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 return FAIL;
1158 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1159 isn->isn_arg.storeopt.so_flags = opt_flags;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_LOAD or similar instruction.
1166 */
1167 static int
1168generate_LOAD(
1169 cctx_T *cctx,
1170 isntype_T isn_type,
1171 int idx,
1172 char_u *name,
1173 type_T *type)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1179 return FAIL;
1180 if (name != NULL)
1181 isn->isn_arg.string = vim_strsave(name);
1182 else
1183 isn->isn_arg.number = idx;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001189 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001190 */
1191 static int
1192generate_LOADV(
1193 cctx_T *cctx,
1194 char_u *name,
1195 int error)
1196{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001197 int di_flags;
1198 int vidx = find_vim_var(name, &di_flags);
1199 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001202 if (vidx < 0)
1203 {
1204 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001205 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 return FAIL;
1207 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001208 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001210 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211}
1212
1213/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001214 * Generate an ISN_UNLET instruction.
1215 */
1216 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001217generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001218{
1219 isn_T *isn;
1220
1221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001222 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001223 return FAIL;
1224 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1225 isn->isn_arg.unlet.ul_forceit = forceit;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001231 * Generate an ISN_LOCKCONST instruction.
1232 */
1233 static int
1234generate_LOCKCONST(cctx_T *cctx)
1235{
1236 isn_T *isn;
1237
1238 RETURN_OK_IF_SKIP(cctx);
1239 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1240 return FAIL;
1241 return OK;
1242}
1243
1244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 * Generate an ISN_LOADS instruction.
1246 */
1247 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001252 int sid,
1253 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254{
1255 isn_T *isn;
1256
Bram Moolenaar080457c2020-03-03 21:53:32 +01001257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 if (isn_type == ISN_LOADS)
1259 isn = generate_instr_type(cctx, isn_type, type);
1260 else
1261 isn = generate_instr_drop(cctx, isn_type, 1);
1262 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1265 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267 return OK;
1268}
1269
1270/*
1271 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1272 */
1273 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 cctx_T *cctx,
1276 isntype_T isn_type,
1277 int sid,
1278 int idx,
1279 type_T *type)
1280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if (isn_type == ISN_LOADSCRIPT)
1285 isn = generate_instr_type(cctx, isn_type, type);
1286 else
1287 isn = generate_instr_drop(cctx, isn_type, 1);
1288 if (isn == NULL)
1289 return FAIL;
1290 isn->isn_arg.script.script_sid = sid;
1291 isn->isn_arg.script.script_idx = idx;
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_NEWLIST instruction.
1297 */
1298 static int
1299generate_NEWLIST(cctx_T *cctx, int count)
1300{
1301 isn_T *isn;
1302 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 type_T *type;
1304 type_T *member;
1305
Bram Moolenaar080457c2020-03-03 21:53:32 +01001306 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.number = count;
1310
Bram Moolenaar127542b2020-08-09 17:22:04 +02001311 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001312 if (count == 0)
1313 member = &t_void;
1314 else
1315 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001316 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1317 cctx->ctx_type_list);
1318 type = get_list_type(member, cctx->ctx_type_list);
1319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 // drop the value types
1321 stack->ga_len -= count;
1322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 // add the list type to the type stack
1324 if (ga_grow(stack, 1) == FAIL)
1325 return FAIL;
1326 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1327 ++stack->ga_len;
1328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_NEWDICT instruction.
1334 */
1335 static int
1336generate_NEWDICT(cctx_T *cctx, int count)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 type_T *type;
1341 type_T *member;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.number = count;
1347
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001348 if (count == 0)
1349 member = &t_void;
1350 else
1351 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001352 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1353 cctx->ctx_type_list);
1354 type = get_dict_type(member, cctx->ctx_type_list);
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 // drop the key and value types
1357 stack->ga_len -= 2 * count;
1358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 // add the dict type to the type stack
1360 if (ga_grow(stack, 1) == FAIL)
1361 return FAIL;
1362 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1363 ++stack->ga_len;
1364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_FUNCREF instruction.
1370 */
1371 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001372generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
1376
Bram Moolenaar080457c2020-03-03 21:53:32 +01001377 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1379 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001380 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001381 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
1383 if (ga_grow(stack, 1) == FAIL)
1384 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001385 ((type_T **)stack->ga_data)[stack->ga_len] =
1386 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 ++stack->ga_len;
1388
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001393 * Generate an ISN_NEWFUNC instruction.
1394 */
1395 static int
1396generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1397{
1398 isn_T *isn;
1399 char_u *name;
1400
1401 RETURN_OK_IF_SKIP(cctx);
1402 name = vim_strsave(lambda_name);
1403 if (name == NULL)
1404 return FAIL;
1405 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1406 return FAIL;
1407 isn->isn_arg.newfunc.nf_lambda = name;
1408 isn->isn_arg.newfunc.nf_global = func_name;
1409
1410 return OK;
1411}
1412
1413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 * Generate an ISN_JUMP instruction.
1415 */
1416 static int
1417generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1418{
1419 isn_T *isn;
1420 garray_T *stack = &cctx->ctx_type_stack;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1424 return FAIL;
1425 isn->isn_arg.jump.jump_when = when;
1426 isn->isn_arg.jump.jump_where = where;
1427
1428 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1429 --stack->ga_len;
1430
1431 return OK;
1432}
1433
1434 static int
1435generate_FOR(cctx_T *cctx, int loop_idx)
1436{
1437 isn_T *isn;
1438 garray_T *stack = &cctx->ctx_type_stack;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.forloop.for_idx = loop_idx;
1444
1445 if (ga_grow(stack, 1) == FAIL)
1446 return FAIL;
1447 // type doesn't matter, will be stored next
1448 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1449 ++stack->ga_len;
1450
1451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001456 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Return FAIL if the number of arguments is wrong.
1458 */
1459 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001460generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461{
1462 isn_T *isn;
1463 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001464 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001465 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
Bram Moolenaar080457c2020-03-03 21:53:32 +01001467 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001468 argoff = check_internal_func(func_idx, argcount);
1469 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 return FAIL;
1471
Bram Moolenaar389df252020-07-09 21:20:47 +02001472 if (method_call && argoff > 1)
1473 {
1474 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.shuffle.shfl_item = argcount;
1477 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1478 }
1479
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001480 if (argcount > 0)
1481 {
1482 // Check the types of the arguments.
1483 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1484 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001486 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.bfunc.cbf_idx = func_idx;
1491 isn->isn_arg.bfunc.cbf_argcount = argcount;
1492
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 // Drop the argument types and push the return type.
1494 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 if (ga_grow(stack, 1) == FAIL)
1496 return FAIL;
1497 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001498 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001499 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
1501 return OK;
1502}
1503
1504/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001505 * Generate an ISN_LISTAPPEND instruction. Works like add().
1506 * Argument count is already checked.
1507 */
1508 static int
1509generate_LISTAPPEND(cctx_T *cctx)
1510{
1511 garray_T *stack = &cctx->ctx_type_stack;
1512 type_T *list_type;
1513 type_T *item_type;
1514 type_T *expected;
1515
1516 // Caller already checked that list_type is a list.
1517 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1518 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1519 expected = list_type->tt_member;
1520 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1521 return FAIL;
1522
1523 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1524 return FAIL;
1525
1526 --stack->ga_len; // drop the argument
1527 return OK;
1528}
1529
1530/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001531 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1532 * Argument count is already checked.
1533 */
1534 static int
1535generate_BLOBAPPEND(cctx_T *cctx)
1536{
1537 garray_T *stack = &cctx->ctx_type_stack;
1538 type_T *item_type;
1539
1540 // Caller already checked that blob_type is a blob.
1541 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1542 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 * Generate an ISN_DCALL or ISN_UCALL instruction.
1554 * Return FAIL if the number of arguments is wrong.
1555 */
1556 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001557generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558{
1559 isn_T *isn;
1560 garray_T *stack = &cctx->ctx_type_stack;
1561 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001562 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
Bram Moolenaar080457c2020-03-03 21:53:32 +01001564 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 if (argcount > regular_args && !has_varargs(ufunc))
1566 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001567 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 return FAIL;
1569 }
1570 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1571 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001572 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 return FAIL;
1574 }
1575
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001576 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001577 {
1578 int i;
1579
1580 for (i = 0; i < argcount; ++i)
1581 {
1582 type_T *expected;
1583 type_T *actual;
1584
1585 if (i < regular_args)
1586 {
1587 if (ufunc->uf_arg_types == NULL)
1588 continue;
1589 expected = ufunc->uf_arg_types[i];
1590 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001591 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1592 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001593 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001594 else
1595 expected = ufunc->uf_va_type->tt_member;
1596 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001597 if (need_type(actual, expected, -argcount + i, cctx,
1598 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 arg_type_mismatch(expected, actual, i + 1);
1601 return FAIL;
1602 }
1603 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001605 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1606 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001607 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001608 }
1609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001611 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001612 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001614 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
1616 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1617 isn->isn_arg.dfunc.cdf_argcount = argcount;
1618 }
1619 else
1620 {
1621 // A user function may be deleted and redefined later, can't use the
1622 // ufunc pointer, need to look it up again at runtime.
1623 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1624 isn->isn_arg.ufunc.cuf_argcount = argcount;
1625 }
1626
1627 stack->ga_len -= argcount; // drop the arguments
1628 if (ga_grow(stack, 1) == FAIL)
1629 return FAIL;
1630 // add return value
1631 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1632 ++stack->ga_len;
1633
1634 return OK;
1635}
1636
1637/*
1638 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1639 */
1640 static int
1641generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1648 return FAIL;
1649 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1650 isn->isn_arg.ufunc.cuf_argcount = argcount;
1651
1652 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001653 if (ga_grow(stack, 1) == FAIL)
1654 return FAIL;
1655 // add return value
1656 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1657 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658
1659 return OK;
1660}
1661
1662/*
1663 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001664 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 */
1666 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001667generate_PCALL(
1668 cctx_T *cctx,
1669 int argcount,
1670 char_u *name,
1671 type_T *type,
1672 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673{
1674 isn_T *isn;
1675 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001676 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
Bram Moolenaar080457c2020-03-03 21:53:32 +01001678 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001679
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001680 if (type->tt_type == VAR_ANY)
1681 ret_type = &t_any;
1682 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001683 {
1684 if (type->tt_argcount != -1)
1685 {
1686 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1687
1688 if (argcount < type->tt_min_argcount - varargs)
1689 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001690 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001691 return FAIL;
1692 }
1693 if (!varargs && argcount > type->tt_argcount)
1694 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001695 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001696 return FAIL;
1697 }
1698 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001699 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001700 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001701 else
1702 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001703 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001704 return FAIL;
1705 }
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.pfunc.cpf_top = at_top;
1710 isn->isn_arg.pfunc.cpf_argcount = argcount;
1711
1712 stack->ga_len -= argcount; // drop the arguments
1713
1714 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001715 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001717 // If partial is above the arguments it must be cleared and replaced with
1718 // the return value.
1719 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1720 return FAIL;
1721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 return OK;
1723}
1724
1725/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001726 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 */
1728 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730{
1731 isn_T *isn;
1732 garray_T *stack = &cctx->ctx_type_stack;
1733 type_T *type;
1734
Bram Moolenaar080457c2020-03-03 21:53:32 +01001735 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001736 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001738 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001740 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001742 if (type->tt_type != VAR_DICT && type != &t_any)
1743 {
1744 emsg(_(e_dictreq));
1745 return FAIL;
1746 }
1747 // change dict type to dict member type
1748 if (type->tt_type == VAR_DICT)
1749 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
1751 return OK;
1752}
1753
1754/*
1755 * Generate an ISN_ECHO instruction.
1756 */
1757 static int
1758generate_ECHO(cctx_T *cctx, int with_white, int count)
1759{
1760 isn_T *isn;
1761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.echo.echo_with_white = with_white;
1766 isn->isn_arg.echo.echo_count = count;
1767
1768 return OK;
1769}
1770
Bram Moolenaarad39c092020-02-26 18:23:43 +01001771/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 */
1774 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001775generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001776{
1777 isn_T *isn;
1778
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001779 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001780 return FAIL;
1781 isn->isn_arg.number = count;
1782
1783 return OK;
1784}
1785
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001786/*
1787 * Generate an ISN_PUT instruction.
1788 */
1789 static int
1790generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1791{
1792 isn_T *isn;
1793
1794 RETURN_OK_IF_SKIP(cctx);
1795 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.put.put_regname = regname;
1798 isn->isn_arg.put.put_lnum = lnum;
1799 return OK;
1800}
1801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 static int
1803generate_EXEC(cctx_T *cctx, char_u *line)
1804{
1805 isn_T *isn;
1806
Bram Moolenaar080457c2020-03-03 21:53:32 +01001807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1809 return FAIL;
1810 isn->isn_arg.string = vim_strsave(line);
1811 return OK;
1812}
1813
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001814 static int
1815generate_EXECCONCAT(cctx_T *cctx, int count)
1816{
1817 isn_T *isn;
1818
1819 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1820 return FAIL;
1821 isn->isn_arg.number = count;
1822 return OK;
1823}
1824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001826 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001827 */
1828 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001829generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001830{
1831 isn_T *isn;
1832
Bram Moolenaar02194d22020-10-24 23:08:38 +02001833 if (cmod->cmod_flags != 0
1834 || cmod->cmod_split != 0
1835 || cmod->cmod_verbose != 0
1836 || cmod->cmod_tab != 0
1837 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001838 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001839 cctx->ctx_has_cmdmod = TRUE;
1840
1841 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001842 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001843 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1844 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1845 return FAIL;
1846 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1847 // filter progam now belongs to the instruction
1848 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001849 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001850
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001851 return OK;
1852}
1853
1854 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001855generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001856{
1857 isn_T *isn;
1858
Bram Moolenaar02194d22020-10-24 23:08:38 +02001859 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001860 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001861 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001862 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001863 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001864
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001865 return OK;
1866}
1867
1868/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001870 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001872 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001873reserve_local(
1874 cctx_T *cctx,
1875 char_u *name,
1876 size_t len,
1877 int isConst,
1878 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 lvar_T *lvar;
1881
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001882 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001884 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001885 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 }
1887
1888 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001889 return NULL;
1890 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001891 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001893 // Every local variable uses the next entry on the stack. We could re-use
1894 // the last ones when leaving a scope, but then variables used in a closure
1895 // might get overwritten. To keep things simple do not re-use stack
1896 // entries. This is less efficient, but memory is cheap these days.
1897 lvar->lv_idx = cctx->ctx_locals_count++;
1898
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001899 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 lvar->lv_const = isConst;
1901 lvar->lv_type = type;
1902
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001903 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904}
1905
1906/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001907 * Remove local variables above "new_top".
1908 */
1909 static void
1910unwind_locals(cctx_T *cctx, int new_top)
1911{
1912 if (cctx->ctx_locals.ga_len > new_top)
1913 {
1914 int idx;
1915 lvar_T *lvar;
1916
1917 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1918 {
1919 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1920 vim_free(lvar->lv_name);
1921 }
1922 }
1923 cctx->ctx_locals.ga_len = new_top;
1924}
1925
1926/*
1927 * Free all local variables.
1928 */
1929 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001930free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001931{
1932 unwind_locals(cctx, 0);
1933 ga_clear(&cctx->ctx_locals);
1934}
1935
1936/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 * Find "name" in script-local items of script "sid".
1938 * Returns the index in "sn_var_vals" if found.
1939 * If found but not in "sn_var_vals" returns -1.
1940 * If not found returns -2.
1941 */
1942 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001943get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944{
1945 hashtab_T *ht;
1946 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001947 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001948 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 int idx;
1950
Bram Moolenaare3d46852020-08-29 13:39:17 +02001951 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001953 if (sid == current_sctx.sc_sid)
1954 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001955 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001956
1957 if (sav == NULL)
1958 return -2;
1959 idx = sav->sav_var_vals_idx;
1960 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1961 if (check_writable && sv->sv_const)
1962 semsg(_(e_readonlyvar), name);
1963 return idx;
1964 }
1965
1966 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 ht = &SCRIPT_VARS(sid);
1968 di = find_var_in_ht(ht, 0, name, TRUE);
1969 if (di == NULL)
1970 return -2;
1971
1972 // Now find the svar_T index in sn_var_vals.
1973 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1974 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001975 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if (sv->sv_tv == &di->di_tv)
1977 {
1978 if (check_writable && sv->sv_const)
1979 semsg(_(e_readonlyvar), name);
1980 return idx;
1981 }
1982 }
1983 return -1;
1984}
1985
1986/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001987 * Find "name" in imported items of the current script or in "cctx" if not
1988 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 */
1990 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001991find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 int idx;
1994
Bram Moolenaare3d46852020-08-29 13:39:17 +02001995 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001996 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 if (cctx != NULL)
1998 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1999 {
2000 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2001 + idx;
2002
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002003 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2004 : STRLEN(import->imp_name) == len
2005 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 return import;
2007 }
2008
Bram Moolenaarefa94442020-08-08 22:16:00 +02002009 return find_imported_in_script(name, len, current_sctx.sc_sid);
2010}
2011
2012 imported_T *
2013find_imported_in_script(char_u *name, size_t len, int sid)
2014{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002015 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002016 int idx;
2017
Bram Moolenaare3d46852020-08-29 13:39:17 +02002018 if (!SCRIPT_ID_VALID(sid))
2019 return NULL;
2020 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2022 {
2023 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2024
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002025 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2026 : STRLEN(import->imp_name) == len
2027 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 return import;
2029 }
2030 return NULL;
2031}
2032
2033/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002034 * Free all imported variables.
2035 */
2036 static void
2037free_imported(cctx_T *cctx)
2038{
2039 int idx;
2040
2041 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2042 {
2043 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2044
2045 vim_free(import->imp_name);
2046 }
2047 ga_clear(&cctx->ctx_imports);
2048}
2049
2050/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002051 * Return TRUE if "p" points at a "#" but not at "#{".
2052 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002053 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002054vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002055{
2056 return p[0] == '#' && p[1] != '{';
2057}
2058
2059/*
2060 * Return a pointer to the next line that isn't empty or only contains a
2061 * comment. Skips over white space.
2062 * Returns NULL if there is none.
2063 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002064 char_u *
2065peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002066{
2067 int lnum = cctx->ctx_lnum;
2068
2069 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2070 {
2071 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002072 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002073
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002074 // ignore NULLs inserted for continuation lines
2075 if (line != NULL)
2076 {
2077 p = skipwhite(line);
2078 if (*p != NUL && !vim9_comment_start(p))
2079 return p;
2080 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002081 }
2082 return NULL;
2083}
2084
2085/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002086 * Called when checking for a following operator at "arg". When the rest of
2087 * the line is empty or only a comment, peek the next line. If there is a next
2088 * line return a pointer to it and set "nextp".
2089 * Otherwise skip over white space.
2090 */
2091 static char_u *
2092may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2093{
2094 char_u *p = skipwhite(arg);
2095
2096 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002097 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002098 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002099 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002100 if (*nextp != NULL)
2101 return *nextp;
2102 }
2103 return p;
2104}
2105
2106/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002107 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002108 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002109 * Returns NULL when at the end.
2110 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002111 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002112next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002113{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002114 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002115
2116 do
2117 {
2118 ++cctx->ctx_lnum;
2119 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002120 {
2121 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002122 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002123 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002124 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002125 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002126 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002127 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002128 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002129 return line;
2130}
2131
2132/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002133 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002134 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002135 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2136 */
2137 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002138may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002139{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002140 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002141 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002142 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002143
2144 if (next == NULL)
2145 return FAIL;
2146 *arg = skipwhite(next);
2147 }
2148 return OK;
2149}
2150
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002151/*
2152 * Idem, and give an error when failed.
2153 */
2154 static int
2155may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2156{
2157 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2158 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002159 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002160 return FAIL;
2161 }
2162 return OK;
2163}
2164
2165
Bram Moolenaara5565e42020-05-09 15:44:01 +02002166// Structure passed between the compile_expr* functions to keep track of
2167// constants that have been parsed but for which no code was produced yet. If
2168// possible expressions on these constants are applied at compile time. If
2169// that is not possible, the code to push the constants needs to be generated
2170// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002171// Using 50 should be more than enough of 5 levels of ().
2172#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002173typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002174 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002175 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002176 int pp_is_const; // all generated code was constants, used for a
2177 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002178} ppconst_T;
2179
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002180static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002181static int compile_expr0(char_u **arg, cctx_T *cctx);
2182static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2183
Bram Moolenaara5565e42020-05-09 15:44:01 +02002184/*
2185 * Generate a PUSH instruction for "tv".
2186 * "tv" will be consumed or cleared.
2187 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2188 */
2189 static int
2190generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2191{
2192 if (tv != NULL)
2193 {
2194 switch (tv->v_type)
2195 {
2196 case VAR_UNKNOWN:
2197 break;
2198 case VAR_BOOL:
2199 generate_PUSHBOOL(cctx, tv->vval.v_number);
2200 break;
2201 case VAR_SPECIAL:
2202 generate_PUSHSPEC(cctx, tv->vval.v_number);
2203 break;
2204 case VAR_NUMBER:
2205 generate_PUSHNR(cctx, tv->vval.v_number);
2206 break;
2207#ifdef FEAT_FLOAT
2208 case VAR_FLOAT:
2209 generate_PUSHF(cctx, tv->vval.v_float);
2210 break;
2211#endif
2212 case VAR_BLOB:
2213 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2214 tv->vval.v_blob = NULL;
2215 break;
2216 case VAR_STRING:
2217 generate_PUSHS(cctx, tv->vval.v_string);
2218 tv->vval.v_string = NULL;
2219 break;
2220 default:
2221 iemsg("constant type not supported");
2222 clear_tv(tv);
2223 return FAIL;
2224 }
2225 tv->v_type = VAR_UNKNOWN;
2226 }
2227 return OK;
2228}
2229
2230/*
2231 * Generate code for any ppconst entries.
2232 */
2233 static int
2234generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2235{
2236 int i;
2237 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002238 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002239
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002240 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002241 for (i = 0; i < ppconst->pp_used; ++i)
2242 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2243 ret = FAIL;
2244 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002245 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002246 return ret;
2247}
2248
2249/*
2250 * Clear ppconst constants. Used when failing.
2251 */
2252 static void
2253clear_ppconst(ppconst_T *ppconst)
2254{
2255 int i;
2256
2257 for (i = 0; i < ppconst->pp_used; ++i)
2258 clear_tv(&ppconst->pp_tv[i]);
2259 ppconst->pp_used = 0;
2260}
2261
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002262/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002263 * Generate an instruction to load script-local variable "name", without the
2264 * leading "s:".
2265 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 */
2267 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002268compile_load_scriptvar(
2269 cctx_T *cctx,
2270 char_u *name, // variable NUL terminated
2271 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002272 char_u **end, // end of variable
2273 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002275 scriptitem_T *si;
2276 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 imported_T *import;
2278
Bram Moolenaare3d46852020-08-29 13:39:17 +02002279 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2280 return FAIL;
2281 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002282 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002283 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002285 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002286 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2287 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 }
2289 if (idx >= 0)
2290 {
2291 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2292
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002293 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 current_sctx.sc_sid, idx, sv->sv_type);
2295 return OK;
2296 }
2297
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002298 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 if (import != NULL)
2300 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002301 if (import->imp_all)
2302 {
2303 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002304 char_u *exp_name;
2305 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002306 ufunc_T *ufunc;
2307 type_T *type;
2308
2309 // Used "import * as Name", need to lookup the member.
2310 if (*p != '.')
2311 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002312 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002313 return FAIL;
2314 }
2315 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002316 if (VIM_ISWHITE(*p))
2317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002318 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002319 return FAIL;
2320 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002321
Bram Moolenaar1c991142020-07-04 13:15:31 +02002322 // isolate one name
2323 exp_name = p;
2324 while (eval_isnamec(*p))
2325 ++p;
2326 cc = *p;
2327 *p = NUL;
2328
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002329 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002330 *p = cc;
2331 p = skipwhite(p);
2332
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002333 // TODO: what if it is a function?
2334 if (idx < 0)
2335 return FAIL;
2336 *end = p;
2337
2338 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2339 import->imp_sid,
2340 idx,
2341 type);
2342 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002343 else if (import->imp_funcname != NULL)
2344 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002345 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002346 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2347 import->imp_sid,
2348 import->imp_var_vals_idx,
2349 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 return OK;
2351 }
2352
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002353 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002354 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 return FAIL;
2356}
2357
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002358 static int
2359generate_funcref(cctx_T *cctx, char_u *name)
2360{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002361 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002362
2363 if (ufunc == NULL)
2364 return FAIL;
2365
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002366 // Need to compile any default values to get the argument types.
2367 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2368 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2369 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002370 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002371}
2372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373/*
2374 * Compile a variable name into a load instruction.
2375 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002376 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 * When "error" is FALSE do not give an error when not found.
2378 */
2379 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002380compile_load(
2381 char_u **arg,
2382 char_u *end_arg,
2383 cctx_T *cctx,
2384 int is_expr,
2385 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386{
2387 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002388 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002389 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002391 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
2393 if (*(*arg + 1) == ':')
2394 {
2395 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002396 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002397 {
2398 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002400 switch (**arg)
2401 {
2402 case 'g': isn_type = ISN_LOADGDICT; break;
2403 case 'w': isn_type = ISN_LOADWDICT; break;
2404 case 't': isn_type = ISN_LOADTDICT; break;
2405 case 'b': isn_type = ISN_LOADBDICT; break;
2406 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002407 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002408 goto theend;
2409 }
2410 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2411 goto theend;
2412 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 else
2415 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002416 isntype_T isn_type = ISN_DROP;
2417
2418 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2419 if (name == NULL)
2420 return FAIL;
2421
2422 switch (**arg)
2423 {
2424 case 'v': res = generate_LOADV(cctx, name, error);
2425 break;
2426 case 's': res = compile_load_scriptvar(cctx, name,
2427 NULL, NULL, error);
2428 break;
2429 case 'g': isn_type = ISN_LOADG; break;
2430 case 'w': isn_type = ISN_LOADW; break;
2431 case 't': isn_type = ISN_LOADT; break;
2432 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002433 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002434 goto theend;
2435 }
2436 if (isn_type != ISN_DROP)
2437 {
2438 // Global, Buffer-local, Window-local and Tabpage-local
2439 // variables can be defined later, thus we don't check if it
2440 // exists, give error at runtime.
2441 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 }
2444 }
2445 else
2446 {
2447 size_t len = end - *arg;
2448 int idx;
2449 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002450 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451
2452 name = vim_strnsave(*arg, end - *arg);
2453 if (name == NULL)
2454 return FAIL;
2455
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002456 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002458 if (!gen_load_outer)
2459 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 }
2461 else
2462 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002463 lvar_T *lvar = lookup_local(*arg, len, cctx);
2464
2465 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002467 type = lvar->lv_type;
2468 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002469 if (lvar->lv_from_outer)
2470 gen_load_outer = TRUE;
2471 else
2472 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 }
2474 else
2475 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002476 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002477 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002478 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002479 || find_imported(name, 0, cctx) != NULL)
2480 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002481
Bram Moolenaar0f769812020-09-12 18:32:34 +02002482 // When evaluating an expression and the name starts with an
2483 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002484 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002485 if (res == FAIL && is_expr
2486 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002487 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 }
2489 }
2490 if (gen_load)
2491 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002492 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002493 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002494 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002495 cctx->ctx_outer_used = TRUE;
2496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498
2499 *arg = end;
2500
2501theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002502 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002503 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 vim_free(name);
2505 return res;
2506}
2507
2508/*
2509 * Compile the argument expressions.
2510 * "arg" points to just after the "(" and is advanced to after the ")"
2511 */
2512 static int
2513compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2514{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002515 char_u *p = *arg;
2516 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002517 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518
Bram Moolenaare6085c52020-04-12 20:19:16 +02002519 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002521 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2522 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002523 if (*p == ')')
2524 {
2525 *arg = p + 1;
2526 return OK;
2527 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002528 if (must_end)
2529 {
2530 semsg(_(e_missing_comma_before_argument_str), p);
2531 return FAIL;
2532 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002533
Bram Moolenaara5565e42020-05-09 15:44:01 +02002534 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 return FAIL;
2536 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002537
2538 if (*p != ',' && *skipwhite(p) == ',')
2539 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002540 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002541 p = skipwhite(p);
2542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002544 {
2545 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002546 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002547 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002548 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002549 else
2550 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002551 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002552 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002554failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002555 emsg(_(e_missing_close));
2556 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557}
2558
2559/*
2560 * Compile a function call: name(arg1, arg2)
2561 * "arg" points to "name", "arg + varlen" to the "(".
2562 * "argcount_init" is 1 for "value->method()"
2563 * Instructions:
2564 * EVAL arg1
2565 * EVAL arg2
2566 * BCALL / DCALL / UCALL
2567 */
2568 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002569compile_call(
2570 char_u **arg,
2571 size_t varlen,
2572 cctx_T *cctx,
2573 ppconst_T *ppconst,
2574 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575{
2576 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002577 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 int argcount = argcount_init;
2579 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002580 char_u fname_buf[FLEN_FIXED + 1];
2581 char_u *tofree = NULL;
2582 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002584 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002585 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587 // we can evaluate "has('name')" at compile time
2588 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2589 {
2590 char_u *s = skipwhite(*arg + varlen + 1);
2591 typval_T argvars[2];
2592
2593 argvars[0].v_type = VAR_UNKNOWN;
2594 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002595 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002596 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002597 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002598 s = skipwhite(s);
2599 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2600 {
2601 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2602
2603 *arg = s + 1;
2604 argvars[1].v_type = VAR_UNKNOWN;
2605 tv->v_type = VAR_NUMBER;
2606 tv->vval.v_number = 0;
2607 f_has(argvars, tv);
2608 clear_tv(&argvars[0]);
2609 ++ppconst->pp_used;
2610 return OK;
2611 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002612 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613 }
2614
2615 if (generate_ppconst(cctx, ppconst) == FAIL)
2616 return FAIL;
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 if (varlen >= sizeof(namebuf))
2619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002620 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 return FAIL;
2622 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002623 vim_strncpy(namebuf, *arg, varlen);
2624 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 *arg = skipwhite(*arg + varlen + 1);
2627 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002628 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629
Bram Moolenaara1773442020-08-12 15:21:22 +02002630 is_autoload = vim_strchr(name, '#') != NULL;
2631 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 {
2633 int idx;
2634
2635 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002636 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002638 {
2639 if (STRCMP(name, "add") == 0 && argcount == 2)
2640 {
2641 garray_T *stack = &cctx->ctx_type_stack;
2642 type_T *type = ((type_T **)stack->ga_data)[
2643 stack->ga_len - 2];
2644
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002645 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002646 if (type->tt_type == VAR_LIST)
2647 {
2648 // inline "add(list, item)" so that the type can be checked
2649 res = generate_LISTAPPEND(cctx);
2650 idx = -1;
2651 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002652 else if (type->tt_type == VAR_BLOB)
2653 {
2654 // inline "add(blob, nr)" so that the type can be checked
2655 res = generate_BLOBAPPEND(cctx);
2656 idx = -1;
2657 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002658 }
2659
2660 if (idx >= 0)
2661 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2662 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002663 else
2664 semsg(_(e_unknownfunc), namebuf);
2665 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 }
2667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002669 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002670 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002671 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002672 {
2673 res = generate_CALL(cctx, ufunc, argcount);
2674 goto theend;
2675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
2677 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002678 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002679 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002681 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002682 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002683 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002684 garray_T *stack = &cctx->ctx_type_stack;
2685 type_T *type;
2686
2687 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2688 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002689 goto theend;
2690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691
Bram Moolenaar0f769812020-09-12 18:32:34 +02002692 // If we can find a global function by name generate the right call.
2693 if (ufunc != NULL)
2694 {
2695 res = generate_CALL(cctx, ufunc, argcount);
2696 goto theend;
2697 }
2698
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002699 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002700 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002701 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002702 res = generate_UCALL(cctx, name, argcount);
2703 else
2704 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002705
2706theend:
2707 vim_free(tofree);
2708 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709}
2710
2711// like NAMESPACE_CHAR but with 'a' and 'l'.
2712#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2713
2714/*
2715 * Find the end of a variable or function name. Unlike find_name_end() this
2716 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002717 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 * Return a pointer to just after the name. Equal to "arg" if there is no
2719 * valid name.
2720 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002721 static char_u *
2722to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723{
2724 char_u *p;
2725
2726 // Quick check for valid starting character.
2727 if (!eval_isnamec1(*arg))
2728 return arg;
2729
2730 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2731 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2732 // and can be used in slice "[n:]".
2733 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002734 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2736 break;
2737 return p;
2738}
2739
2740/*
2741 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002742 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 */
2744 char_u *
2745to_name_const_end(char_u *arg)
2746{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002747 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 typval_T rettv;
2749
2750 if (p == arg && *arg == '[')
2751 {
2752
2753 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002754 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 p = arg;
2756 }
2757 else if (p == arg && *arg == '#' && arg[1] == '{')
2758 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002759 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002761 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 p = arg;
2763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764
2765 return p;
2766}
2767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768/*
2769 * parse a list: [expr, expr]
2770 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002771 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 */
2773 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002774compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775{
2776 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002777 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002779 int is_const;
2780 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002782 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002784 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002785 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002786 semsg(_(e_list_end), *arg);
2787 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002788 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002789 if (*p == ',')
2790 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002791 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002792 return FAIL;
2793 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002794 if (*p == ']')
2795 {
2796 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002797 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002798 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002799 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002800 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002801 if (!is_const)
2802 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 ++count;
2804 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002805 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002807 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2808 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002809 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002810 return FAIL;
2811 }
2812 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002813 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 p = skipwhite(p);
2815 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002816 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002818 ppconst->pp_is_const = is_all_const;
2819 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820}
2821
2822/*
2823 * parse a lambda: {arg, arg -> expr}
2824 * "*arg" points to the '{'.
2825 */
2826 static int
2827compile_lambda(char_u **arg, cctx_T *cctx)
2828{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 typval_T rettv;
2830 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002831 evalarg_T evalarg;
2832
2833 CLEAR_FIELD(evalarg);
2834 evalarg.eval_flags = EVAL_EVALUATE;
2835 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836
2837 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002838 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002839 {
2840 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002842 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002845 ++ufunc->uf_refcount;
2846 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002847 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 // The function will have one line: "return {expr}".
2850 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002851 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002853 clear_evalarg(&evalarg, NULL);
2854
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002855 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002856 {
2857 // The return type will now be known.
2858 set_function_type(ufunc);
2859
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002860 // The function reference count will be 1. When the ISN_FUNCREF
2861 // instruction is deleted the reference count is decremented and the
2862 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002863 return generate_FUNCREF(cctx, ufunc);
2864 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002865
2866 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 return FAIL;
2868}
2869
2870/*
2871 * Compile a lamda call: expr->{lambda}(args)
2872 * "arg" points to the "{".
2873 */
2874 static int
2875compile_lambda_call(char_u **arg, cctx_T *cctx)
2876{
2877 ufunc_T *ufunc;
2878 typval_T rettv;
2879 int argcount = 1;
2880 int ret = FAIL;
2881
2882 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002883 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 return FAIL;
2885
2886 if (**arg != '(')
2887 {
2888 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002889 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 else
2891 semsg(_(e_missing_paren), "lambda");
2892 clear_tv(&rettv);
2893 return FAIL;
2894 }
2895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 ufunc = rettv.vval.v_partial->pt_func;
2897 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002898 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002899 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002900
Bram Moolenaara05e5242020-09-19 18:19:19 +02002901 // The function will have one line: "return {expr}". Compile it into
2902 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002903 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904
2905 // compile the arguments
2906 *arg = skipwhite(*arg + 1);
2907 if (compile_arguments(arg, cctx, &argcount) == OK)
2908 // call the compiled function
2909 ret = generate_CALL(cctx, ufunc, argcount);
2910
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002911 if (ret == FAIL)
2912 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 return ret;
2914}
2915
2916/*
2917 * parse a dict: {'key': val} or #{key: val}
2918 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002919 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 */
2921 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002922compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923{
2924 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002925 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 int count = 0;
2927 dict_T *d = dict_alloc();
2928 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002929 char_u *whitep = *arg;
2930 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002931 int is_const;
2932 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933
2934 if (d == NULL)
2935 return FAIL;
2936 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002937 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 {
2939 char_u *key = NULL;
2940
Bram Moolenaar23c55272020-06-21 16:58:13 +02002941 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002942 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002943 *arg = NULL;
2944 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002945 }
2946
2947 if (**arg == '}')
2948 break;
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 if (literal)
2951 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002952 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953
Bram Moolenaar2c330432020-04-13 14:41:35 +02002954 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002956 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 return FAIL;
2958 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002959 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 if (generate_PUSHS(cctx, key) == FAIL)
2961 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002962 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 }
2964 else
2965 {
2966 isn_T *isn;
2967
Bram Moolenaara5565e42020-05-09 15:44:01 +02002968 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2971 if (isn->isn_type == ISN_PUSHS)
2972 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002973 else
2974 {
2975 type_T *keytype = ((type_T **)stack->ga_data)
2976 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002977 if (need_type(keytype, &t_string, -1, cctx,
2978 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002979 return FAIL;
2980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 }
2982
2983 // Check for duplicate keys, if using string keys.
2984 if (key != NULL)
2985 {
2986 item = dict_find(d, key, -1);
2987 if (item != NULL)
2988 {
2989 semsg(_(e_duplicate_key), key);
2990 goto failret;
2991 }
2992 item = dictitem_alloc(key);
2993 if (item != NULL)
2994 {
2995 item->di_tv.v_type = VAR_UNKNOWN;
2996 item->di_tv.v_lock = 0;
2997 if (dict_add(d, item) == FAIL)
2998 dictitem_free(item);
2999 }
3000 }
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (**arg != ':')
3003 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003004 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003005 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003006 else
3007 semsg(_(e_missing_dict_colon), *arg);
3008 return FAIL;
3009 }
3010 whitep = *arg + 1;
3011 if (!IS_WHITE_OR_NUL(*whitep))
3012 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003013 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 return FAIL;
3015 }
3016
3017 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003018 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003019 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003020 *arg = NULL;
3021 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003022 }
3023
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003024 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003026 if (!is_const)
3027 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 ++count;
3029
Bram Moolenaar2c330432020-04-13 14:41:35 +02003030 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003031 *arg = skipwhite(*arg);
3032 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003033 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003034 *arg = NULL;
3035 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if (**arg == '}')
3038 break;
3039 if (**arg != ',')
3040 {
3041 semsg(_(e_missing_dict_comma), *arg);
3042 goto failret;
3043 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003044 if (IS_WHITE_OR_NUL(*whitep))
3045 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003046 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003047 return FAIL;
3048 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003049 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003050 if (!IS_WHITE_OR_NUL(*whitep))
3051 {
3052 semsg(_(e_white_space_required_after_str), ",");
3053 return FAIL;
3054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 *arg = skipwhite(*arg + 1);
3056 }
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 *arg = *arg + 1;
3059
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003061 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003062 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 *arg += STRLEN(*arg);
3064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003066 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 return generate_NEWDICT(cctx, count);
3068
3069failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003070 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003071 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003072 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003073 *arg = (char_u *)"";
3074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 dict_unref(d);
3076 return FAIL;
3077}
3078
3079/*
3080 * Compile "&option".
3081 */
3082 static int
3083compile_get_option(char_u **arg, cctx_T *cctx)
3084{
3085 typval_T rettv;
3086 char_u *start = *arg;
3087 int ret;
3088
3089 // parse the option and get the current value to get the type.
3090 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003091 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 if (ret == OK)
3093 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003094 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 char_u *name = vim_strnsave(start, *arg - start);
3096 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3097
3098 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3099 vim_free(name);
3100 }
3101 clear_tv(&rettv);
3102
3103 return ret;
3104}
3105
3106/*
3107 * Compile "$VAR".
3108 */
3109 static int
3110compile_get_env(char_u **arg, cctx_T *cctx)
3111{
3112 char_u *start = *arg;
3113 int len;
3114 int ret;
3115 char_u *name;
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 ++*arg;
3118 len = get_env_len(arg);
3119 if (len == 0)
3120 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003121 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 return FAIL;
3123 }
3124
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003125 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 name = vim_strnsave(start, len + 1);
3127 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3128 vim_free(name);
3129 return ret;
3130}
3131
3132/*
3133 * Compile "@r".
3134 */
3135 static int
3136compile_get_register(char_u **arg, cctx_T *cctx)
3137{
3138 int ret;
3139
3140 ++*arg;
3141 if (**arg == NUL)
3142 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003143 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 return FAIL;
3145 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003146 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 {
3148 emsg_invreg(**arg);
3149 return FAIL;
3150 }
3151 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3152 ++*arg;
3153 return ret;
3154}
3155
3156/*
3157 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003158 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 */
3160 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003161apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003163 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164
3165 // this works from end to start
3166 while (p > start)
3167 {
3168 --p;
3169 if (*p == '-' || *p == '+')
3170 {
3171 // only '-' has an effect, for '+' we only check the type
3172#ifdef FEAT_FLOAT
3173 if (rettv->v_type == VAR_FLOAT)
3174 {
3175 if (*p == '-')
3176 rettv->vval.v_float = -rettv->vval.v_float;
3177 }
3178 else
3179#endif
3180 {
3181 varnumber_T val;
3182 int error = FALSE;
3183
3184 // tv_get_number_chk() accepts a string, but we don't want that
3185 // here
3186 if (check_not_string(rettv) == FAIL)
3187 return FAIL;
3188 val = tv_get_number_chk(rettv, &error);
3189 clear_tv(rettv);
3190 if (error)
3191 return FAIL;
3192 if (*p == '-')
3193 val = -val;
3194 rettv->v_type = VAR_NUMBER;
3195 rettv->vval.v_number = val;
3196 }
3197 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003198 else if (numeric_only)
3199 {
3200 ++p;
3201 break;
3202 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003203 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 {
3205 int v = tv2bool(rettv);
3206
3207 // '!' is permissive in the type.
3208 clear_tv(rettv);
3209 rettv->v_type = VAR_BOOL;
3210 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3211 }
3212 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003213 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return OK;
3215}
3216
3217/*
3218 * Recognize v: variables that are constants and set "rettv".
3219 */
3220 static void
3221get_vim_constant(char_u **arg, typval_T *rettv)
3222{
3223 if (STRNCMP(*arg, "v:true", 6) == 0)
3224 {
3225 rettv->v_type = VAR_BOOL;
3226 rettv->vval.v_number = VVAL_TRUE;
3227 *arg += 6;
3228 }
3229 else if (STRNCMP(*arg, "v:false", 7) == 0)
3230 {
3231 rettv->v_type = VAR_BOOL;
3232 rettv->vval.v_number = VVAL_FALSE;
3233 *arg += 7;
3234 }
3235 else if (STRNCMP(*arg, "v:null", 6) == 0)
3236 {
3237 rettv->v_type = VAR_SPECIAL;
3238 rettv->vval.v_number = VVAL_NULL;
3239 *arg += 6;
3240 }
3241 else if (STRNCMP(*arg, "v:none", 6) == 0)
3242 {
3243 rettv->v_type = VAR_SPECIAL;
3244 rettv->vval.v_number = VVAL_NONE;
3245 *arg += 6;
3246 }
3247}
3248
Bram Moolenaar696ba232020-07-29 21:20:41 +02003249 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003250get_compare_type(char_u *p, int *len, int *type_is)
3251{
3252 exptype_T type = EXPR_UNKNOWN;
3253 int i;
3254
3255 switch (p[0])
3256 {
3257 case '=': if (p[1] == '=')
3258 type = EXPR_EQUAL;
3259 else if (p[1] == '~')
3260 type = EXPR_MATCH;
3261 break;
3262 case '!': if (p[1] == '=')
3263 type = EXPR_NEQUAL;
3264 else if (p[1] == '~')
3265 type = EXPR_NOMATCH;
3266 break;
3267 case '>': if (p[1] != '=')
3268 {
3269 type = EXPR_GREATER;
3270 *len = 1;
3271 }
3272 else
3273 type = EXPR_GEQUAL;
3274 break;
3275 case '<': if (p[1] != '=')
3276 {
3277 type = EXPR_SMALLER;
3278 *len = 1;
3279 }
3280 else
3281 type = EXPR_SEQUAL;
3282 break;
3283 case 'i': if (p[1] == 's')
3284 {
3285 // "is" and "isnot"; but not a prefix of a name
3286 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3287 *len = 5;
3288 i = p[*len];
3289 if (!isalnum(i) && i != '_')
3290 {
3291 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3292 *type_is = TRUE;
3293 }
3294 }
3295 break;
3296 }
3297 return type;
3298}
3299
3300/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003302 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 */
3304 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003305compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003307 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308
3309 // this works from end to start
3310 while (p > start)
3311 {
3312 --p;
3313 if (*p == '-' || *p == '+')
3314 {
3315 int negate = *p == '-';
3316 isn_T *isn;
3317
3318 // TODO: check type
3319 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3320 {
3321 --p;
3322 if (*p == '-')
3323 negate = !negate;
3324 }
3325 // only '-' has an effect, for '+' we only check the type
3326 if (negate)
3327 isn = generate_instr(cctx, ISN_NEGATENR);
3328 else
3329 isn = generate_instr(cctx, ISN_CHECKNR);
3330 if (isn == NULL)
3331 return FAIL;
3332 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003333 else if (numeric_only)
3334 {
3335 ++p;
3336 break;
3337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 else
3339 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003340 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003342 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003344 if (p[-1] == '!')
3345 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
3348 if (generate_2BOOL(cctx, invert) == FAIL)
3349 return FAIL;
3350 }
3351 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003352 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 return OK;
3354}
3355
3356/*
3357 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003358 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 */
3360 static int
3361compile_subscript(
3362 char_u **arg,
3363 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003364 char_u *start_leader,
3365 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003366 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003368 char_u *name_start = *end_leader;
3369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 for (;;)
3371 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003372 char_u *p = skipwhite(*arg);
3373
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003374 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003375 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003376 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003377
3378 // If a following line starts with "->{" or "->X" advance to that
3379 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003380 // Also if a following line starts with ".x".
3381 if (next != NULL &&
3382 ((next[0] == '-' && next[1] == '>'
3383 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003384 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003385 {
3386 next = next_line_from_context(cctx, TRUE);
3387 if (next == NULL)
3388 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003389 *arg = next;
3390 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003391 }
3392 }
3393
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003394 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3395 // not a function call.
3396 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003398 garray_T *stack = &cctx->ctx_type_stack;
3399 type_T *type;
3400 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003402 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003403 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003404 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003407 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3408
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003409 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3411 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003412 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 return FAIL;
3414 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003415 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003417 char_u *pstart = p;
3418
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003419 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003420 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003421 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 // something->method()
3424 // Apply the '!', '-' and '+' first:
3425 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003426 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003429 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003430 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003431 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (**arg == '{')
3433 {
3434 // lambda call: list->{lambda}
3435 if (compile_lambda_call(arg, cctx) == FAIL)
3436 return FAIL;
3437 }
3438 else
3439 {
3440 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003441 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003442 if (!eval_isnamec1(*p))
3443 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003444 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003445 return FAIL;
3446 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003447 if (ASCII_ISALPHA(*p) && p[1] == ':')
3448 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003449 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 ;
3451 if (*p != '(')
3452 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003453 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 return FAIL;
3455 }
3456 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003457 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 return FAIL;
3459 }
3460 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003461 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003463 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003464 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003465 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003466 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003467 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003468
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003469 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003470 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003471 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003472 // TODO: blob index
3473 // TODO: more arguments
3474 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003475 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003476 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003477 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003478
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003479 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003480 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003481 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003482 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003483 if (**arg == ':')
3484 // missing first index is equal to zero
3485 generate_PUSHNR(cctx, 0);
3486 else
3487 {
3488 if (compile_expr0(arg, cctx) == FAIL)
3489 return FAIL;
3490 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3491 return FAIL;
3492 *arg = skipwhite(*arg);
3493 }
3494 if (**arg == ':')
3495 {
3496 *arg = skipwhite(*arg + 1);
3497 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3498 return FAIL;
3499 if (**arg == ']')
3500 // missing second index is equal to end of string
3501 generate_PUSHNR(cctx, -1);
3502 else
3503 {
3504 if (compile_expr0(arg, cctx) == FAIL)
3505 return FAIL;
3506 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3507 return FAIL;
3508 *arg = skipwhite(*arg);
3509 }
3510 is_slice = TRUE;
3511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
3513 if (**arg != ']')
3514 {
3515 emsg(_(e_missbrac));
3516 return FAIL;
3517 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003518 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003520 // We can index a list and a dict. If we don't know the type
3521 // we can use the index value type.
3522 // TODO: If we don't know use an instruction to figure it out at
3523 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003524 typep = ((type_T **)stack->ga_data) + stack->ga_len
3525 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003526 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003527 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3528 // If the index is a string, the variable must be a Dict.
3529 if (*typep == &t_any && valtype == &t_string)
3530 vtype = VAR_DICT;
3531 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003532 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003533 if (need_type(valtype, &t_number, -1, cctx,
3534 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003535 return FAIL;
3536 if (is_slice)
3537 {
3538 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003539 if (need_type(valtype, &t_number, -2, cctx,
3540 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003541 return FAIL;
3542 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003543 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003544
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003545 if (vtype == VAR_DICT)
3546 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003547 if (is_slice)
3548 {
3549 emsg(_(e_cannot_slice_dictionary));
3550 return FAIL;
3551 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003552 if ((*typep)->tt_type == VAR_DICT)
3553 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003554 else
3555 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003556 if (need_type(*typep, &t_dict_any, -2, cctx,
3557 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003558 return FAIL;
3559 *typep = &t_any;
3560 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003561 if (may_generate_2STRING(-1, cctx) == FAIL)
3562 return FAIL;
3563 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3564 return FAIL;
3565 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003566 else if (vtype == VAR_STRING)
3567 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003568 *typep = &t_string;
3569 if ((is_slice
3570 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3571 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003572 return FAIL;
3573 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003574 else if (vtype == VAR_BLOB)
3575 {
3576 emsg("Sorry, blob index and slice not implemented yet");
3577 return FAIL;
3578 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003579 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003580 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003581 if (is_slice)
3582 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003583 if (generate_instr_drop(cctx,
3584 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3585 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003586 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003587 }
Bram Moolenaared591872020-08-15 22:14:53 +02003588 else
3589 {
3590 if ((*typep)->tt_type == VAR_LIST)
3591 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003592 if (generate_instr_drop(cctx,
3593 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3594 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003595 return FAIL;
3596 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003597 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003598 else
3599 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003600 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003601 return FAIL;
3602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003604 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003606 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003607 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003608 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003609 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003610
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003611 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003612 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003613 {
3614 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003615 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003616 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003617 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003618 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 while (eval_isnamec(*p))
3620 MB_PTR_ADV(p);
3621 if (p == *arg)
3622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003623 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 return FAIL;
3625 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003626 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 return FAIL;
3628 *arg = p;
3629 }
3630 else
3631 break;
3632 }
3633
3634 // TODO - see handle_subscript():
3635 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3636 // Don't do this when "Func" is already a partial that was bound
3637 // explicitly (pt_auto is FALSE).
3638
3639 return OK;
3640}
3641
3642/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003643 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3644 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003646 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003648 *
3649 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 */
3651
3652/*
3653 * number number constant
3654 * 0zFFFFFFFF Blob constant
3655 * "string" string constant
3656 * 'string' literal string constant
3657 * &option-name option value
3658 * @r register contents
3659 * identifier variable value
3660 * function() function call
3661 * $VAR environment variable
3662 * (expression) nested expression
3663 * [expr, expr] List
3664 * {key: val, key: val} Dictionary
3665 * #{key: val, key: val} Dictionary with literal keys
3666 *
3667 * Also handle:
3668 * ! in front logical NOT
3669 * - in front unary minus
3670 * + in front unary plus (ignored)
3671 * trailing (arg) funcref/partial call
3672 * trailing [] subscript in String or List
3673 * trailing .name entry in Dictionary
3674 * trailing ->name() method call
3675 */
3676 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003677compile_expr7(
3678 char_u **arg,
3679 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003680 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 char_u *start_leader, *end_leader;
3683 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003684 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003685 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003687 ppconst->pp_is_const = FALSE;
3688
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 /*
3690 * Skip '!', '-' and '+' characters. They are handled later.
3691 */
3692 start_leader = *arg;
3693 while (**arg == '!' || **arg == '-' || **arg == '+')
3694 *arg = skipwhite(*arg + 1);
3695 end_leader = *arg;
3696
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003697 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 switch (**arg)
3699 {
3700 /*
3701 * Number constant.
3702 */
3703 case '0': // also for blob starting with 0z
3704 case '1':
3705 case '2':
3706 case '3':
3707 case '4':
3708 case '5':
3709 case '6':
3710 case '7':
3711 case '8':
3712 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003713 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003715 // Apply "-" and "+" just before the number now, right to
3716 // left. Matters especially when "->" follows. Stops at
3717 // '!'.
3718 if (apply_leader(rettv, TRUE,
3719 start_leader, &end_leader) == FAIL)
3720 {
3721 clear_tv(rettv);
3722 return FAIL;
3723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 break;
3725
3726 /*
3727 * String constant: "string".
3728 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003729 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 return FAIL;
3731 break;
3732
3733 /*
3734 * Literal string constant: 'str''ing'.
3735 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003736 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 return FAIL;
3738 break;
3739
3740 /*
3741 * Constant Vim variable.
3742 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003743 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 ret = NOTDONE;
3745 break;
3746
3747 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003748 * "true" constant
3749 */
3750 case 't': if (STRNCMP(*arg, "true", 4) == 0
3751 && !eval_isnamec((*arg)[4]))
3752 {
3753 *arg += 4;
3754 rettv->v_type = VAR_BOOL;
3755 rettv->vval.v_number = VVAL_TRUE;
3756 }
3757 else
3758 ret = NOTDONE;
3759 break;
3760
3761 /*
3762 * "false" constant
3763 */
3764 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3765 && !eval_isnamec((*arg)[5]))
3766 {
3767 *arg += 5;
3768 rettv->v_type = VAR_BOOL;
3769 rettv->vval.v_number = VVAL_FALSE;
3770 }
3771 else
3772 ret = NOTDONE;
3773 break;
3774
3775 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 * List: [expr, expr]
3777 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003778 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 break;
3780
3781 /*
3782 * Dictionary: #{key: val, key: val}
3783 */
3784 case '#': if ((*arg)[1] == '{')
3785 {
3786 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003787 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
3789 else
3790 ret = NOTDONE;
3791 break;
3792
3793 /*
3794 * Lambda: {arg, arg -> expr}
3795 * Dictionary: {'key': val, 'key': val}
3796 */
3797 case '{': {
3798 char_u *start = skipwhite(*arg + 1);
3799
3800 // Find out what comes after the arguments.
3801 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003802 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 if (ret != FAIL && *start == '>')
3804 ret = compile_lambda(arg, cctx);
3805 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003806 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 }
3808 break;
3809
3810 /*
3811 * Option value: &name
3812 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003813 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3814 return FAIL;
3815 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 break;
3817
3818 /*
3819 * Environment variable: $VAR.
3820 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003821 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3822 return FAIL;
3823 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 break;
3825
3826 /*
3827 * Register contents: @r.
3828 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003829 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3830 return FAIL;
3831 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 break;
3833 /*
3834 * nested expression: (expression).
3835 */
3836 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003837
3838 // recursive!
3839 if (ppconst->pp_used <= PPSIZE - 10)
3840 {
3841 ret = compile_expr1(arg, cctx, ppconst);
3842 }
3843 else
3844 {
3845 // Not enough space in ppconst, flush constants.
3846 if (generate_ppconst(cctx, ppconst) == FAIL)
3847 return FAIL;
3848 ret = compile_expr0(arg, cctx);
3849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 *arg = skipwhite(*arg);
3851 if (**arg == ')')
3852 ++*arg;
3853 else if (ret == OK)
3854 {
3855 emsg(_(e_missing_close));
3856 ret = FAIL;
3857 }
3858 break;
3859
3860 default: ret = NOTDONE;
3861 break;
3862 }
3863 if (ret == FAIL)
3864 return FAIL;
3865
Bram Moolenaar1c747212020-05-09 18:28:34 +02003866 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003868 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003869 clear_tv(rettv);
3870 else
3871 // A constant expression can possibly be handled compile time,
3872 // return the value instead of generating code.
3873 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
3875 else if (ret == NOTDONE)
3876 {
3877 char_u *p;
3878 int r;
3879
3880 if (!eval_isnamec1(**arg))
3881 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003882 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 return FAIL;
3884 }
3885
3886 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003887 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 {
3890 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003893 {
3894 if (generate_ppconst(cctx, ppconst) == FAIL)
3895 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003896 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 if (r == FAIL)
3899 return FAIL;
3900 }
3901
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003902 // Handle following "[]", ".member", etc.
3903 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003904 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003905 ppconst) == FAIL)
3906 return FAIL;
3907 if (ppconst->pp_used > 0)
3908 {
3909 // apply the '!', '-' and '+' before the constant
3910 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003912 return FAIL;
3913 return OK;
3914 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003915 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003917 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918}
3919
3920/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003921 * Give the "white on both sides" error, taking the operator from "p[len]".
3922 */
3923 void
3924error_white_both(char_u *op, int len)
3925{
3926 char_u buf[10];
3927
3928 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003929 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003930}
3931
3932/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003933 * <type>expr7: runtime type check / conversion
3934 */
3935 static int
3936compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3937{
3938 type_T *want_type = NULL;
3939
3940 // Recognize <type>
3941 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3942 {
3943 int called_emsg_before = called_emsg;
3944
3945 ++*arg;
3946 want_type = parse_type(arg, cctx->ctx_type_list);
3947 if (called_emsg != called_emsg_before)
3948 return FAIL;
3949
3950 if (**arg != '>')
3951 {
3952 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003953 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003954 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003955 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003956 return FAIL;
3957 }
3958 ++*arg;
3959 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3960 return FAIL;
3961 }
3962
3963 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3964 return FAIL;
3965
3966 if (want_type != NULL)
3967 {
3968 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003969 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003970
Bram Moolenaard1103582020-08-14 22:44:25 +02003971 generate_ppconst(cctx, ppconst);
3972 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003973 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003974 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003975 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003976 return FAIL;
3977 }
3978 }
3979
3980 return OK;
3981}
3982
3983/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 * * number multiplication
3985 * / number division
3986 * % number modulo
3987 */
3988 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990{
3991 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003992 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003995 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003996 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 return FAIL;
3998
3999 /*
4000 * Repeat computing, until no "*", "/" or "%" is following.
4001 */
4002 for (;;)
4003 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004004 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 if (*op != '*' && *op != '/' && *op != '%')
4006 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004007 if (next != NULL)
4008 {
4009 *arg = next_line_from_context(cctx, TRUE);
4010 op = skipwhite(*arg);
4011 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004012
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004013 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004015 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004016 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 }
4018 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004019 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004020 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004022 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004023 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004025
4026 if (ppconst->pp_used == ppconst_used + 2
4027 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4028 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004029 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004030 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4031 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004032 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004034 // both are numbers: compute the result
4035 switch (*op)
4036 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004038 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004039 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004040 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004041 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042 break;
4043 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004044 tv1->vval.v_number = res;
4045 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004046 }
4047 else
4048 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004049 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004050 generate_two_op(cctx, op);
4051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
4053
4054 return OK;
4055}
4056
4057/*
4058 * + number addition
4059 * - number subtraction
4060 * .. string concatenation
4061 */
4062 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
4065 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004066 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004068 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069
4070 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 return FAIL;
4073
4074 /*
4075 * Repeat computing, until no "+", "-" or ".." is following.
4076 */
4077 for (;;)
4078 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004079 op = may_peek_next_line(cctx, *arg, &next);
4080 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 break;
4082 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004083 if (next != NULL)
4084 {
4085 *arg = next_line_from_context(cctx, TRUE);
4086 op = skipwhite(*arg);
4087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004089 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004091 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 }
4094
4095 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004096 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004097 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
4102
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004105 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4106 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4107 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4108 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4111 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004113 // concat/subtract/add constant numbers
4114 if (*op == '+')
4115 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4116 else if (*op == '-')
4117 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4118 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004119 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004120 // concatenate constant strings
4121 char_u *s1 = tv1->vval.v_string;
4122 char_u *s2 = tv2->vval.v_string;
4123 size_t len1 = STRLEN(s1);
4124
4125 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4126 if (tv1->vval.v_string == NULL)
4127 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 return FAIL;
4130 }
4131 mch_memmove(tv1->vval.v_string, s1, len1);
4132 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004133 vim_free(s1);
4134 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004135 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004139 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004141 if (*op == '.')
4142 {
4143 if (may_generate_2STRING(-2, cctx) == FAIL
4144 || may_generate_2STRING(-1, cctx) == FAIL)
4145 return FAIL;
4146 generate_instr_drop(cctx, ISN_CONCAT, 1);
4147 }
4148 else
4149 generate_two_op(cctx, op);
4150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 }
4152
4153 return OK;
4154}
4155
4156/*
4157 * expr5a == expr5b
4158 * expr5a =~ expr5b
4159 * expr5a != expr5b
4160 * expr5a !~ expr5b
4161 * expr5a > expr5b
4162 * expr5a >= expr5b
4163 * expr5a < expr5b
4164 * expr5a <= expr5b
4165 * expr5a is expr5b
4166 * expr5a isnot expr5b
4167 *
4168 * Produces instructions:
4169 * EVAL expr5a Push result of "expr5a"
4170 * EVAL expr5b Push result of "expr5b"
4171 * COMPARE one of the compare instructions
4172 */
4173 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175{
4176 exptype_T type = EXPR_UNKNOWN;
4177 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004178 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
4183 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 return FAIL;
4186
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004187 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004188 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
4190 /*
4191 * If there is a comparative operator, use it.
4192 */
4193 if (type != EXPR_UNKNOWN)
4194 {
4195 int ic = FALSE; // Default: do not ignore case
4196
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004197 if (next != NULL)
4198 {
4199 *arg = next_line_from_context(cctx, TRUE);
4200 p = skipwhite(*arg);
4201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 if (type_is && (p[len] == '?' || p[len] == '#'))
4203 {
4204 semsg(_(e_invexpr2), *arg);
4205 return FAIL;
4206 }
4207 // extra question mark appended: ignore case
4208 if (p[len] == '?')
4209 {
4210 ic = TRUE;
4211 ++len;
4212 }
4213 // extra '#' appended: match case (ignored)
4214 else if (p[len] == '#')
4215 ++len;
4216 // nothing appended: match case
4217
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004218 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004220 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223
4224 // get the second variable
4225 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004226 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 return FAIL;
4228
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 return FAIL;
4231
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 if (ppconst->pp_used == ppconst_used + 2)
4233 {
4234 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4235 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4236 int ret;
4237
4238 // Both sides are a constant, compute the result now.
4239 // First check for a valid combination of types, this is more
4240 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004241 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004242 ret = FAIL;
4243 else
4244 {
4245 ret = typval_compare(tv1, tv2, type, ic);
4246 tv1->v_type = VAR_BOOL;
4247 tv1->vval.v_number = tv1->vval.v_number
4248 ? VVAL_TRUE : VVAL_FALSE;
4249 clear_tv(tv2);
4250 --ppconst->pp_used;
4251 }
4252 return ret;
4253 }
4254
4255 generate_ppconst(cctx, ppconst);
4256 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 }
4258
4259 return OK;
4260}
4261
Bram Moolenaar7f141552020-05-09 17:35:53 +02004262static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264/*
4265 * Compile || or &&.
4266 */
4267 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268compile_and_or(
4269 char_u **arg,
4270 cctx_T *cctx,
4271 char *op,
4272 ppconst_T *ppconst,
4273 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004275 char_u *next;
4276 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 int opchar = *op;
4278
4279 if (p[0] == opchar && p[1] == opchar)
4280 {
4281 garray_T *instr = &cctx->ctx_instr;
4282 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004283 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004284 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
4286 /*
4287 * Repeat until there is no following "||" or "&&"
4288 */
4289 ga_init2(&end_ga, sizeof(int), 10);
4290 while (p[0] == opchar && p[1] == opchar)
4291 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004292 if (next != NULL)
4293 {
4294 *arg = next_line_from_context(cctx, TRUE);
4295 p = skipwhite(*arg);
4296 }
4297
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4299 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004300 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004301 return FAIL;
4302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004304 // TODO: use ppconst if the value is a constant and check
4305 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306 generate_ppconst(cctx, ppconst);
4307
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004308 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4309 all_bool_values = FALSE;
4310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 if (ga_grow(&end_ga, 1) == FAIL)
4312 {
4313 ga_clear(&end_ga);
4314 return FAIL;
4315 }
4316 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4317 ++end_ga.ga_len;
4318 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004319 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320
4321 // eval the next expression
4322 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004323 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004324 return FAIL;
4325
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4327 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 {
4329 ga_clear(&end_ga);
4330 return FAIL;
4331 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004332
4333 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004335 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336
4337 // Fill in the end label in all jumps.
4338 while (end_ga.ga_len > 0)
4339 {
4340 isn_T *isn;
4341
4342 --end_ga.ga_len;
4343 isn = ((isn_T *)instr->ga_data)
4344 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4345 isn->isn_arg.jump.jump_where = instr->ga_len;
4346 }
4347 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004348
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004349 // The resulting type is converted to bool if needed.
4350 if (!all_bool_values)
4351 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * expr4a && expr4a && expr4a logical AND
4359 *
4360 * Produces instructions:
4361 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004362 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004364 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004366 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 * end:
4368 */
4369 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 int ppconst_used = ppconst->pp_used;
4373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 return FAIL;
4377
4378 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380}
4381
4382/*
4383 * expr3a || expr3b || expr3c logical OR
4384 *
4385 * Produces instructions:
4386 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004387 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004389 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004391 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 * end:
4393 */
4394 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 int ppconst_used = ppconst->pp_used;
4398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 return FAIL;
4402
4403 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405}
4406
4407/*
4408 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004410 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 * JUMP_IF_FALSE alt jump if false
4412 * EVAL expr1a
4413 * JUMP_ALWAYS end
4414 * alt: EVAL expr1b
4415 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004416 *
4417 * Toplevel expression: expr2 ?? expr1
4418 * Produces instructions:
4419 * EVAL expr2 Push result of "expr2"
4420 * JUMP_AND_KEEP_IF_TRUE end jump if true
4421 * EVAL expr1
4422 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 */
4424 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426{
4427 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004428 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004429 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430
Bram Moolenaar3988f642020-08-27 22:43:03 +02004431 // Ignore all kinds of errors when not producing code.
4432 if (cctx->ctx_skip == SKIP_YES)
4433 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004434 evalarg_T evalarg;
4435
4436 CLEAR_FIELD(evalarg);
4437 evalarg.eval_cctx = cctx;
4438 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004439 return OK;
4440 }
4441
Bram Moolenaar61a89812020-05-07 16:58:17 +02004442 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 return FAIL;
4445
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004446 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 if (*p == '?')
4448 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004449 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 garray_T *instr = &cctx->ctx_instr;
4451 garray_T *stack = &cctx->ctx_type_stack;
4452 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004453 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004455 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 int has_const_expr = FALSE;
4457 int const_value = FALSE;
4458 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004460 if (next != NULL)
4461 {
4462 *arg = next_line_from_context(cctx, TRUE);
4463 p = skipwhite(*arg);
4464 }
4465
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004466 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004467 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004468 semsg(_(e_white_space_required_before_and_after_str),
4469 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004470 return FAIL;
4471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 if (ppconst->pp_used == ppconst_used + 1)
4474 {
4475 // the condition is a constant, we know whether the ? or the :
4476 // expression is to be evaluated.
4477 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004478 if (op_falsy)
4479 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4480 else
4481 {
4482 int error = FALSE;
4483
4484 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4485 &error);
4486 if (error)
4487 return FAIL;
4488 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004489 cctx->ctx_skip = save_skip == SKIP_YES ||
4490 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4491
4492 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4493 // "left ?? right" and "left" is truthy: produce "left"
4494 generate_ppconst(cctx, ppconst);
4495 else
4496 {
4497 clear_tv(&ppconst->pp_tv[ppconst_used]);
4498 --ppconst->pp_used;
4499 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 }
4501 else
4502 {
4503 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004504 if (op_falsy)
4505 end_idx = instr->ga_len;
4506 generate_JUMP(cctx, op_falsy
4507 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4508 if (op_falsy)
4509 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511
4512 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004513 *arg = skipwhite(p + 1 + op_falsy);
4514 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004515 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004517 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 if (!has_const_expr)
4520 {
4521 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004523 if (!op_falsy)
4524 {
4525 // remember the type and drop it
4526 --stack->ga_len;
4527 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004529 end_idx = instr->ga_len;
4530 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004532 // jump here from JUMP_IF_FALSE
4533 isn = ((isn_T *)instr->ga_data) + alt_idx;
4534 isn->isn_arg.jump.jump_where = instr->ga_len;
4535 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004538 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004540 // Check for the ":".
4541 p = may_peek_next_line(cctx, *arg, &next);
4542 if (*p != ':')
4543 {
4544 emsg(_(e_missing_colon));
4545 return FAIL;
4546 }
4547 if (next != NULL)
4548 {
4549 *arg = next_line_from_context(cctx, TRUE);
4550 p = skipwhite(*arg);
4551 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004552
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004553 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4554 {
4555 semsg(_(e_white_space_required_before_and_after_str), ":");
4556 return FAIL;
4557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004559 // evaluate the third expression
4560 if (has_const_expr)
4561 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004562 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004563 *arg = skipwhite(p + 1);
4564 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4565 return FAIL;
4566 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4567 return FAIL;
4568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 if (!has_const_expr)
4571 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004572 type_T **typep;
4573
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004577 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4578 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004580 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 isn = ((isn_T *)instr->ga_data) + end_idx;
4582 isn->isn_arg.jump.jump_where = instr->ga_len;
4583 }
4584
4585 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 }
4587 return OK;
4588}
4589
4590/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004591 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004592 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4593 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 */
4595 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004596compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597{
4598 ppconst_T ppconst;
4599
4600 CLEAR_FIELD(ppconst);
4601 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4602 {
4603 clear_ppconst(&ppconst);
4604 return FAIL;
4605 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004606 if (is_const != NULL)
4607 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004608 if (generate_ppconst(cctx, &ppconst) == FAIL)
4609 return FAIL;
4610 return OK;
4611}
4612
4613/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004614 * Toplevel expression.
4615 */
4616 static int
4617compile_expr0(char_u **arg, cctx_T *cctx)
4618{
4619 return compile_expr0_ext(arg, cctx, NULL);
4620}
4621
4622/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 * compile "return [expr]"
4624 */
4625 static char_u *
4626compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4627{
4628 char_u *p = arg;
4629 garray_T *stack = &cctx->ctx_type_stack;
4630 type_T *stack_type;
4631
4632 if (*p != NUL && *p != '|' && *p != '\n')
4633 {
4634 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 return NULL;
4637
4638 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4639 if (set_return_type)
4640 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004641 else
4642 {
4643 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4644 && stack_type->tt_type != VAR_VOID
4645 && stack_type->tt_type != VAR_UNKNOWN)
4646 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004647 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004648 return NULL;
4649 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004650 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004651 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004652 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 }
4655 else
4656 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004657 // "set_return_type" cannot be TRUE, only used for a lambda which
4658 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004659 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4660 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004662 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 return NULL;
4664 }
4665
4666 // No argument, return zero.
4667 generate_PUSHNR(cctx, 0);
4668 }
4669
4670 if (generate_instr(cctx, ISN_RETURN) == NULL)
4671 return NULL;
4672
4673 // "return val | endif" is possible
4674 return skipwhite(p);
4675}
4676
4677/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004678 * Get a line from the compilation context, compatible with exarg_T getline().
4679 * Return a pointer to the line in allocated memory.
4680 * Return NULL for end-of-file or some error.
4681 */
4682 static char_u *
4683exarg_getline(
4684 int c UNUSED,
4685 void *cookie,
4686 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004687 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688{
4689 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004690 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691
Bram Moolenaar66250c92020-08-20 15:02:42 +02004692 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004693 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004694 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004695 return NULL;
4696 ++cctx->ctx_lnum;
4697 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4698 // Comment lines result in NULL pointers, skip them.
4699 if (p != NULL)
4700 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004701 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702}
4703
4704/*
4705 * Compile a nested :def command.
4706 */
4707 static char_u *
4708compile_nested_function(exarg_T *eap, cctx_T *cctx)
4709{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004710 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004711 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004712 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004713 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004714 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004715 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004716
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004717 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004718 {
4719 emsg(_(e_cannot_use_bang_with_nested_def));
4720 return NULL;
4721 }
4722
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004723 // Only g:Func() can use a namespace.
4724 if (name_start[1] == ':' && !is_global)
4725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004726 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004727 return NULL;
4728 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004729 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4730 return NULL;
4731
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732 eap->arg = name_end;
4733 eap->getline = exarg_getline;
4734 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004735 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004736 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004737 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004738 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004739
Bram Moolenaar822ba242020-05-24 23:00:18 +02004740 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004741 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004742 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004743 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004744 {
4745 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004746 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004747 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004748
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004749 if (is_global)
4750 {
4751 char_u *func_name = vim_strnsave(name_start + 2,
4752 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004753
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004754 if (func_name == NULL)
4755 r = FAIL;
4756 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004757 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004758 }
4759 else
4760 {
4761 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004762 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004763 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004764 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004765
Bram Moolenaareef21022020-08-01 22:16:43 +02004766 if (lvar == NULL)
4767 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004768 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004769 return NULL;
4770 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004771
4772 // copy over the block scope IDs
4773 if (block_depth > 0)
4774 {
4775 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4776 if (ufunc->uf_block_ids != NULL)
4777 {
4778 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4779 sizeof(int) * block_depth);
4780 ufunc->uf_block_depth = block_depth;
4781 }
4782 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004783 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004784
Bram Moolenaar61a89812020-05-07 16:58:17 +02004785 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004786 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004787}
4788
4789/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 * Return the length of an assignment operator, or zero if there isn't one.
4791 */
4792 int
4793assignment_len(char_u *p, int *heredoc)
4794{
4795 if (*p == '=')
4796 {
4797 if (p[1] == '<' && p[2] == '<')
4798 {
4799 *heredoc = TRUE;
4800 return 3;
4801 }
4802 return 1;
4803 }
4804 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4805 return 2;
4806 if (STRNCMP(p, "..=", 3) == 0)
4807 return 3;
4808 return 0;
4809}
4810
4811// words that cannot be used as a variable
4812static char *reserved[] = {
4813 "true",
4814 "false",
4815 NULL
4816};
4817
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004818typedef enum {
4819 dest_local,
4820 dest_option,
4821 dest_env,
4822 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004823 dest_buffer,
4824 dest_window,
4825 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004826 dest_vimvar,
4827 dest_script,
4828 dest_reg,
4829} assign_dest_T;
4830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004832 * Generate the load instruction for "name".
4833 */
4834 static void
4835generate_loadvar(
4836 cctx_T *cctx,
4837 assign_dest_T dest,
4838 char_u *name,
4839 lvar_T *lvar,
4840 type_T *type)
4841{
4842 switch (dest)
4843 {
4844 case dest_option:
4845 // TODO: check the option exists
4846 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4847 break;
4848 case dest_global:
4849 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4850 break;
4851 case dest_buffer:
4852 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4853 break;
4854 case dest_window:
4855 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4856 break;
4857 case dest_tab:
4858 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4859 break;
4860 case dest_script:
4861 compile_load_scriptvar(cctx,
4862 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4863 break;
4864 case dest_env:
4865 // Include $ in the name here
4866 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4867 break;
4868 case dest_reg:
4869 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4870 break;
4871 case dest_vimvar:
4872 generate_LOADV(cctx, name + 2, TRUE);
4873 break;
4874 case dest_local:
4875 if (lvar->lv_from_outer)
4876 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4877 NULL, type);
4878 else
4879 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4880 break;
4881 }
4882}
4883
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004884 void
4885vim9_declare_error(char_u *name)
4886{
4887 char *scope = "";
4888
4889 switch (*name)
4890 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004891 case 'g': scope = _("global"); break;
4892 case 'b': scope = _("buffer"); break;
4893 case 'w': scope = _("window"); break;
4894 case 't': scope = _("tab"); break;
4895 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004896 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004897 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004898 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004899 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004900 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004901 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004902 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004903 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004904 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004905}
4906
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004907/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004908 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004909 * "let name"
4910 * "var name = expr"
4911 * "final name = expr"
4912 * "const name = expr"
4913 * "name = expr"
4914 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 * Return NULL for an error.
4916 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 */
4918 static char_u *
4919compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4920{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 char_u *ret = NULL;
4925 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004927 int scriptvar_sid = 0;
4928 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004931 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 int oplen = 0;
4934 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004935 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004936 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004939 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4940 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 // Skip over the "var" or "[var, var]" to get to any "=".
4943 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4944 if (p == NULL)
4945 return *arg == '[' ? arg : NULL;
4946
4947 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004949 // TODO: should we allow this, and figure out type inference from list
4950 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004951 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 return NULL;
4953 }
4954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 sp = p;
4956 p = skipwhite(p);
4957 op = p;
4958 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959
4960 if (var_count > 0 && oplen == 0)
4961 // can be something like "[1, 2]->func()"
4962 return arg;
4963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4965 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004966 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004968 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 if (heredoc)
4971 {
4972 list_T *l;
4973 listitem_T *li;
4974
4975 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004976 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004978 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004979 if (l == NULL)
4980 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981
Bram Moolenaar078269b2020-09-21 20:35:55 +02004982 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004984 // Push each line and the create the list.
4985 FOR_ALL_LIST_ITEMS(l, li)
4986 {
4987 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4988 li->li_tv.vval.v_string = NULL;
4989 }
4990 generate_NEWLIST(cctx, l->lv_len);
4991 type = &t_list_string;
4992 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 list_free(l);
4995 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 // for "[var, var] = expr" evaluate the expression here, loop over the
5001 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005002
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 p = skipwhite(op + oplen);
5004 if (compile_expr0(&p, cctx) == FAIL)
5005 return NULL;
5006 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005008 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005010 type_T *stacktype;
5011
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005012 stacktype = stack->ga_len == 0 ? &t_void
5013 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005014 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005016 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 goto theend;
5018 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005019 if (need_type(stacktype, &t_list_any, -1, cctx,
5020 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005022 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005023 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5024 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 }
5026 }
5027
5028 /*
5029 * Loop over variables in "[var, var] = expr".
5030 * For "var = expr" and "let var: type" this is done only once.
5031 */
5032 if (var_count > 0)
5033 var_start = skipwhite(arg + 1); // skip over the "["
5034 else
5035 var_start = arg;
5036 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5037 {
5038 char_u *var_end = skip_var_one(var_start, FALSE);
5039 size_t varlen;
5040 int new_local = FALSE;
5041 int opt_type;
5042 int opt_flags = 0;
5043 assign_dest_T dest = dest_local;
5044 int vimvaridx = -1;
5045 lvar_T *lvar = NULL;
5046 lvar_T arg_lvar;
5047 int has_type = FALSE;
5048 int has_index = FALSE;
5049 int instr_count = -1;
5050
Bram Moolenaar65821722020-08-02 18:58:54 +02005051 if (*var_start == '@')
5052 p = var_start + 2;
5053 else
5054 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005055 // skip over the leading "&", "&l:", "&g:" and "$"
5056 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005057 p = to_name_end(p, TRUE);
5058 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059
5060 // "a: type" is declaring variable "a" with a type, not "a:".
5061 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5062 --var_end;
5063 if (is_decl && p == var_start + 2 && p[-1] == ':')
5064 --p;
5065
5066 varlen = p - var_start;
5067 vim_free(name);
5068 name = vim_strnsave(var_start, varlen);
5069 if (name == NULL)
5070 return NULL;
5071 if (!heredoc)
5072 type = &t_any;
5073
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005074 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005076 int declare_error = FALSE;
5077
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 if (*var_start == '&')
5079 {
5080 int cc;
5081 long numval;
5082
5083 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005084 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 emsg(_(e_const_option));
5087 goto theend;
5088 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005089 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005090 p = var_start;
5091 p = find_option_end(&p, &opt_flags);
5092 if (p == NULL)
5093 {
5094 // cannot happen?
5095 emsg(_(e_letunexp));
5096 goto theend;
5097 }
5098 cc = *p;
5099 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005100 opt_type = get_option_value(skip_option_env_lead(var_start),
5101 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 *p = cc;
5103 if (opt_type == -3)
5104 {
5105 semsg(_(e_unknown_option), var_start);
5106 goto theend;
5107 }
5108 if (opt_type == -2 || opt_type == 0)
5109 type = &t_string;
5110 else
5111 type = &t_number; // both number and boolean option
5112 }
5113 else if (*var_start == '$')
5114 {
5115 dest = dest_env;
5116 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005117 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 }
5119 else if (*var_start == '@')
5120 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005121 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 {
5123 emsg_invreg(var_start[1]);
5124 goto theend;
5125 }
5126 dest = dest_reg;
5127 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005128 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005130 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 {
5132 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005133 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005135 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 {
5137 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005138 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005140 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 {
5142 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005143 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005145 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 {
5147 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005148 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005150 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 {
5152 typval_T *vtv;
5153 int di_flags;
5154
5155 vimvaridx = find_vim_var(name + 2, &di_flags);
5156 if (vimvaridx < 0)
5157 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005158 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 goto theend;
5160 }
5161 // We use the current value of "sandbox" here, is that OK?
5162 if (var_check_ro(di_flags, name, FALSE))
5163 goto theend;
5164 dest = dest_vimvar;
5165 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005166 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005167 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005168 }
5169 else
5170 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005171 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172
5173 for (idx = 0; reserved[idx] != NULL; ++idx)
5174 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005175 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005176 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005177 goto theend;
5178 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179
5180 lvar = lookup_local(var_start, varlen, cctx);
5181 if (lvar == NULL)
5182 {
5183 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005184 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5186 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005187 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 if (is_decl)
5189 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005190 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 goto theend;
5192 }
5193 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005194 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005195 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005196 if (lvar != NULL)
5197 {
5198 if (is_decl)
5199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005200 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 goto theend;
5202 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005204 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005206 int script_namespace = varlen > 1
5207 && STRNCMP(var_start, "s:", 2) == 0;
5208 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005209 ? script_var_exists(var_start + 2, varlen - 2,
5210 FALSE, cctx)
5211 : script_var_exists(var_start, varlen,
5212 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005213 imported_T *import =
5214 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005215
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005216 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005218 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5219
5220 if (is_decl)
5221 {
5222 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005223 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005224 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005225 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005226 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005227 name);
5228 goto theend;
5229 }
5230 else if (cctx->ctx_ufunc->uf_script_ctx_version
5231 == SCRIPT_VERSION_VIM9
5232 && script_namespace
5233 && !script_var && import == NULL)
5234 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005235 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005236 goto theend;
5237 }
5238
5239 dest = dest_script;
5240
5241 // existing script-local variables should have a type
5242 scriptvar_sid = current_sctx.sc_sid;
5243 if (import != NULL)
5244 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005245 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005246 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005247 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005248 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005249 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005250 {
5251 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5252 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005253 ((svar_T *)si->sn_var_vals.ga_data)
5254 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005255 type = sv->sv_type;
5256 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005257 }
5258 }
5259 else if (name[1] == ':' && name[2] != NUL)
5260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 goto theend;
5263 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005264 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005265 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005266 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005267 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005268 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005269 else if (check_defined(var_start, varlen, cctx) == FAIL)
5270 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005273
5274 if (declare_error)
5275 {
5276 vim9_declare_error(name);
5277 goto theend;
5278 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005279 }
5280
5281 // handle "a:name" as a name, not index "name" on "a"
5282 if (varlen > 1 || var_start[varlen] != ':')
5283 p = var_end;
5284
5285 if (dest != dest_option)
5286 {
5287 if (is_decl && *p == ':')
5288 {
5289 // parse optional type: "let var: type = expr"
5290 if (!VIM_ISWHITE(p[1]))
5291 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005292 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 goto theend;
5294 }
5295 p = skipwhite(p + 1);
5296 type = parse_type(&p, cctx->ctx_type_list);
5297 has_type = TRUE;
5298 }
5299 else if (lvar != NULL)
5300 type = lvar->lv_type;
5301 }
5302
5303 if (oplen == 3 && !heredoc && dest != dest_global
5304 && type->tt_type != VAR_STRING
5305 && type->tt_type != VAR_ANY)
5306 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005307 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 goto theend;
5309 }
5310
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005311 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 {
5313 if (oplen > 1 && !heredoc)
5314 {
5315 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005316 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 goto theend;
5318 }
5319
5320 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005321 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5322 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 goto theend;
5324 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005325 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 if (lvar == NULL)
5327 goto theend;
5328 new_local = TRUE;
5329 }
5330
5331 member_type = type;
5332 if (var_end > var_start + varlen)
5333 {
5334 // Something follows after the variable: "var[idx]".
5335 if (is_decl)
5336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005337 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 goto theend;
5339 }
5340
5341 if (var_start[varlen] == '[')
5342 {
5343 has_index = TRUE;
5344 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005345 member_type = &t_any;
5346 else
5347 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 }
5349 else
5350 {
5351 semsg("Not supported yet: %s", var_start);
5352 goto theend;
5353 }
5354 }
5355 else if (lvar == &arg_lvar)
5356 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005357 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 goto theend;
5359 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005360 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5361 {
5362 semsg(_(e_cannot_assign_to_constant), name);
5363 goto theend;
5364 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365
5366 if (!heredoc)
5367 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005368 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005370 if (oplen > 0 && var_count == 0)
5371 {
5372 // skip over the "=" and the expression
5373 p = skipwhite(op + oplen);
5374 compile_expr0(&p, cctx);
5375 }
5376 }
5377 else if (oplen > 0)
5378 {
5379 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005380 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005381
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 // For "var = expr" evaluate the expression.
5383 if (var_count == 0)
5384 {
5385 int r;
5386
5387 // for "+=", "*=", "..=" etc. first load the current value
5388 if (*op != '=')
5389 {
5390 generate_loadvar(cctx, dest, name, lvar, type);
5391
5392 if (has_index)
5393 {
5394 // TODO: get member from list or dict
5395 emsg("Index with operation not supported yet");
5396 goto theend;
5397 }
5398 }
5399
5400 // Compile the expression. Temporarily hide the new local
5401 // variable here, it is not available to this expression.
5402 if (new_local)
5403 --cctx->ctx_locals.ga_len;
5404 instr_count = instr->ga_len;
5405 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005406 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 if (new_local)
5408 ++cctx->ctx_locals.ga_len;
5409 if (r == FAIL)
5410 goto theend;
5411 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005412 else if (semicolon && var_idx == var_count - 1)
5413 {
5414 // For "[var; var] = expr" get the rest of the list
5415 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5416 goto theend;
5417 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005418 else
5419 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 // For "[var, var] = expr" get the "var_idx" item from the
5421 // list.
5422 if (generate_GETITEM(cctx, var_idx) == FAIL)
5423 return FAIL;
5424 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005426 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005427 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005428 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005430 if ((stacktype->tt_type == VAR_FUNC
5431 || stacktype->tt_type == VAR_PARTIAL)
5432 && var_wrong_func_name(name, TRUE))
5433 goto theend;
5434
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005435 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005436 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005437 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005439 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005440 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 }
5442 else
5443 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005444 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005445 // for a variable that implies &t_any.
5446 if (stacktype == &t_list_empty)
5447 lvar->lv_type = &t_list_any;
5448 else if (stacktype == &t_dict_empty)
5449 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005450 else if (stacktype == &t_unknown)
5451 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005452 else
5453 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005455 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005456 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005457 {
5458 type_T *use_type = lvar->lv_type;
5459
Bram Moolenaar71abe482020-09-14 22:28:30 +02005460 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005461 if (has_index)
5462 {
5463 use_type = use_type->tt_member;
5464 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005465 // could be indexing "any"
5466 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005467 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005468 if (need_type(stacktype, use_type, -1, cctx,
5469 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005470 goto theend;
5471 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005472 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005473 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005474 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005475 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005477 else if (cmdidx == CMD_final)
5478 {
5479 emsg(_(e_final_requires_a_value));
5480 goto theend;
5481 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005484 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 goto theend;
5486 }
5487 else if (!has_type || dest == dest_option)
5488 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005489 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005490 goto theend;
5491 }
5492 else
5493 {
5494 // variables are always initialized
5495 if (ga_grow(instr, 1) == FAIL)
5496 goto theend;
5497 switch (member_type->tt_type)
5498 {
5499 case VAR_BOOL:
5500 generate_PUSHBOOL(cctx, VVAL_FALSE);
5501 break;
5502 case VAR_FLOAT:
5503#ifdef FEAT_FLOAT
5504 generate_PUSHF(cctx, 0.0);
5505#endif
5506 break;
5507 case VAR_STRING:
5508 generate_PUSHS(cctx, NULL);
5509 break;
5510 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005511 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005512 break;
5513 case VAR_FUNC:
5514 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5515 break;
5516 case VAR_LIST:
5517 generate_NEWLIST(cctx, 0);
5518 break;
5519 case VAR_DICT:
5520 generate_NEWDICT(cctx, 0);
5521 break;
5522 case VAR_JOB:
5523 generate_PUSHJOB(cctx, NULL);
5524 break;
5525 case VAR_CHANNEL:
5526 generate_PUSHCHANNEL(cctx, NULL);
5527 break;
5528 case VAR_NUMBER:
5529 case VAR_UNKNOWN:
5530 case VAR_ANY:
5531 case VAR_PARTIAL:
5532 case VAR_VOID:
5533 case VAR_SPECIAL: // cannot happen
5534 generate_PUSHNR(cctx, 0);
5535 break;
5536 }
5537 }
5538 if (var_count == 0)
5539 end = p;
5540 }
5541
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005542 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005543 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005544 break;
5545
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005546 if (oplen > 0 && *op != '=')
5547 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005548 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 type_T *stacktype;
5550
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 if (*op == '.')
5552 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005553 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005554 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005556 if (
5557#ifdef FEAT_FLOAT
5558 // If variable is float operation with number is OK.
5559 !(expected == &t_float && stacktype == &t_number) &&
5560#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005561 need_type(stacktype, expected, -1, cctx,
5562 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 goto theend;
5564
5565 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005566 {
5567 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5568 goto theend;
5569 }
5570 else if (*op == '+')
5571 {
5572 if (generate_add_instr(cctx,
5573 operator_type(member_type, stacktype),
5574 member_type, stacktype) == FAIL)
5575 goto theend;
5576 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005577 else if (generate_two_op(cctx, op) == FAIL)
5578 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005582 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005583 int r;
5584
5585 // Compile the "idx" in "var[idx]".
5586 if (new_local)
5587 --cctx->ctx_locals.ga_len;
5588 p = skipwhite(var_start + varlen + 1);
5589 r = compile_expr0(&p, cctx);
5590 if (new_local)
5591 ++cctx->ctx_locals.ga_len;
5592 if (r == FAIL)
5593 goto theend;
5594 if (*skipwhite(p) != ']')
5595 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005596 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 emsg(_(e_missbrac));
5598 goto theend;
5599 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005600 if (type == &t_any)
5601 {
5602 type_T *idx_type = ((type_T **)stack->ga_data)[
5603 stack->ga_len - 1];
5604 // Index on variable of unknown type: guess the type from the
5605 // index type: number is dict, otherwise dict.
5606 // TODO: should do the assignment at runtime
5607 if (idx_type->tt_type == VAR_NUMBER)
5608 type = &t_list_any;
5609 else
5610 type = &t_dict_any;
5611 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 if (type->tt_type == VAR_DICT
5613 && may_generate_2STRING(-1, cctx) == FAIL)
5614 goto theend;
5615 if (type->tt_type == VAR_LIST
5616 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005617 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 {
5619 emsg(_(e_number_exp));
5620 goto theend;
5621 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005622
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 // Load the dict or list. On the stack we then have:
5624 // - value
5625 // - index
5626 // - variable
5627 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005628
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005629 if (type->tt_type == VAR_LIST)
5630 {
5631 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5632 return FAIL;
5633 }
5634 else if (type->tt_type == VAR_DICT)
5635 {
5636 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5637 return FAIL;
5638 }
5639 else
5640 {
5641 emsg(_(e_listreq));
5642 goto theend;
5643 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644 }
5645 else
5646 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005647 if (is_decl && cmdidx == CMD_const
5648 && (dest == dest_script || dest == dest_local))
5649 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005650 generate_LOCKCONST(cctx);
5651
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005652 switch (dest)
5653 {
5654 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005655 generate_STOREOPT(cctx, skip_option_env_lead(name),
5656 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005657 break;
5658 case dest_global:
5659 // include g: with the name, easier to execute that way
5660 generate_STORE(cctx, ISN_STOREG, 0, name);
5661 break;
5662 case dest_buffer:
5663 // include b: with the name, easier to execute that way
5664 generate_STORE(cctx, ISN_STOREB, 0, name);
5665 break;
5666 case dest_window:
5667 // include w: with the name, easier to execute that way
5668 generate_STORE(cctx, ISN_STOREW, 0, name);
5669 break;
5670 case dest_tab:
5671 // include t: with the name, easier to execute that way
5672 generate_STORE(cctx, ISN_STORET, 0, name);
5673 break;
5674 case dest_env:
5675 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5676 break;
5677 case dest_reg:
5678 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5679 break;
5680 case dest_vimvar:
5681 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5682 break;
5683 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005684 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005685 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005686 {
5687 char_u *name_s = name;
5688
5689 // Include s: in the name for store_var()
5690 if (name[1] != ':')
5691 {
5692 int len = (int)STRLEN(name) + 3;
5693
5694 name_s = alloc(len);
5695 if (name_s == NULL)
5696 name_s = name;
5697 else
5698 vim_snprintf((char *)name_s, len,
5699 "s:%s", name);
5700 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005701 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5702 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005703 if (name_s != name)
5704 vim_free(name_s);
5705 }
5706 else
5707 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005708 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005709 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005710 break;
5711 case dest_local:
5712 if (lvar != NULL)
5713 {
5714 isn_T *isn = ((isn_T *)instr->ga_data)
5715 + instr->ga_len - 1;
5716
5717 // optimization: turn "var = 123" from ISN_PUSHNR +
5718 // ISN_STORE into ISN_STORENR
5719 if (!lvar->lv_from_outer
5720 && instr->ga_len == instr_count + 1
5721 && isn->isn_type == ISN_PUSHNR)
5722 {
5723 varnumber_T val = isn->isn_arg.number;
5724
5725 isn->isn_type = ISN_STORENR;
5726 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5727 isn->isn_arg.storenr.stnr_val = val;
5728 if (stack->ga_len > 0)
5729 --stack->ga_len;
5730 }
5731 else if (lvar->lv_from_outer)
5732 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005734 else
5735 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5736 }
5737 break;
5738 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005739 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005740
5741 if (var_idx + 1 < var_count)
5742 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005744
5745 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005746 if (var_count > 0 && !semicolon)
5747 {
5748 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5749 goto theend;
5750 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005751
Bram Moolenaarb2097502020-07-19 17:17:02 +02005752 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753
5754theend:
5755 vim_free(name);
5756 return ret;
5757}
5758
5759/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005760 * Check if "name" can be "unlet".
5761 */
5762 int
5763check_vim9_unlet(char_u *name)
5764{
5765 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5766 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005767 // "unlet s:var" is allowed in legacy script.
5768 if (*name == 's' && !script_is_vim9())
5769 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005770 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005771 return FAIL;
5772 }
5773 return OK;
5774}
5775
5776/*
5777 * Callback passed to ex_unletlock().
5778 */
5779 static int
5780compile_unlet(
5781 lval_T *lvp,
5782 char_u *name_end,
5783 exarg_T *eap,
5784 int deep UNUSED,
5785 void *coookie)
5786{
5787 cctx_T *cctx = coookie;
5788
5789 if (lvp->ll_tv == NULL)
5790 {
5791 char_u *p = lvp->ll_name;
5792 int cc = *name_end;
5793 int ret = OK;
5794
5795 // Normal name. Only supports g:, w:, t: and b: namespaces.
5796 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005797 if (*p == '$')
5798 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5799 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005800 ret = FAIL;
5801 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005802 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005803
5804 *name_end = cc;
5805 return ret;
5806 }
5807
5808 // TODO: unlet {list}[idx]
5809 // TODO: unlet {dict}[key]
5810 emsg("Sorry, :unlet not fully implemented yet");
5811 return FAIL;
5812}
5813
5814/*
5815 * compile "unlet var", "lock var" and "unlock var"
5816 * "arg" points to "var".
5817 */
5818 static char_u *
5819compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5820{
5821 char_u *p = arg;
5822
5823 if (eap->cmdidx != CMD_unlet)
5824 {
5825 emsg("Sorry, :lock and unlock not implemented yet");
5826 return NULL;
5827 }
5828
5829 if (*p == '!')
5830 {
5831 p = skipwhite(p + 1);
5832 eap->forceit = TRUE;
5833 }
5834
5835 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5836 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5837}
5838
5839/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005840 * Compile an :import command.
5841 */
5842 static char_u *
5843compile_import(char_u *arg, cctx_T *cctx)
5844{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005845 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846}
5847
5848/*
5849 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5850 */
5851 static int
5852compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5853{
5854 garray_T *instr = &cctx->ctx_instr;
5855 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5856
5857 if (endlabel == NULL)
5858 return FAIL;
5859 endlabel->el_next = *el;
5860 *el = endlabel;
5861 endlabel->el_end_label = instr->ga_len;
5862
5863 generate_JUMP(cctx, when, 0);
5864 return OK;
5865}
5866
5867 static void
5868compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5869{
5870 garray_T *instr = &cctx->ctx_instr;
5871
5872 while (*el != NULL)
5873 {
5874 endlabel_T *cur = (*el);
5875 isn_T *isn;
5876
5877 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5878 isn->isn_arg.jump.jump_where = instr->ga_len;
5879 *el = cur->el_next;
5880 vim_free(cur);
5881 }
5882}
5883
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005884 static void
5885compile_free_jump_to_end(endlabel_T **el)
5886{
5887 while (*el != NULL)
5888 {
5889 endlabel_T *cur = (*el);
5890
5891 *el = cur->el_next;
5892 vim_free(cur);
5893 }
5894}
5895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896/*
5897 * Create a new scope and set up the generic items.
5898 */
5899 static scope_T *
5900new_scope(cctx_T *cctx, scopetype_T type)
5901{
5902 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5903
5904 if (scope == NULL)
5905 return NULL;
5906 scope->se_outer = cctx->ctx_scope;
5907 cctx->ctx_scope = scope;
5908 scope->se_type = type;
5909 scope->se_local_count = cctx->ctx_locals.ga_len;
5910 return scope;
5911}
5912
5913/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005914 * Free the current scope and go back to the outer scope.
5915 */
5916 static void
5917drop_scope(cctx_T *cctx)
5918{
5919 scope_T *scope = cctx->ctx_scope;
5920
5921 if (scope == NULL)
5922 {
5923 iemsg("calling drop_scope() without a scope");
5924 return;
5925 }
5926 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005927 switch (scope->se_type)
5928 {
5929 case IF_SCOPE:
5930 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5931 case FOR_SCOPE:
5932 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5933 case WHILE_SCOPE:
5934 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5935 case TRY_SCOPE:
5936 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5937 case NO_SCOPE:
5938 case BLOCK_SCOPE:
5939 break;
5940 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005941 vim_free(scope);
5942}
5943
5944/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005945 * Check that the top of the type stack has a type that can be used as a
5946 * condition. Give an error and return FAIL if not.
5947 */
5948 static int
5949bool_on_stack(cctx_T *cctx)
5950{
5951 garray_T *stack = &cctx->ctx_type_stack;
5952 type_T *type;
5953
5954 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5955 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005956 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005957 return FAIL;
5958 return OK;
5959}
5960
5961/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 * compile "if expr"
5963 *
5964 * "if expr" Produces instructions:
5965 * EVAL expr Push result of "expr"
5966 * JUMP_IF_FALSE end
5967 * ... body ...
5968 * end:
5969 *
5970 * "if expr | else" Produces instructions:
5971 * EVAL expr Push result of "expr"
5972 * JUMP_IF_FALSE else
5973 * ... body ...
5974 * JUMP_ALWAYS end
5975 * else:
5976 * ... body ...
5977 * end:
5978 *
5979 * "if expr1 | elseif expr2 | else" Produces instructions:
5980 * EVAL expr Push result of "expr"
5981 * JUMP_IF_FALSE elseif
5982 * ... body ...
5983 * JUMP_ALWAYS end
5984 * elseif:
5985 * EVAL expr Push result of "expr"
5986 * JUMP_IF_FALSE else
5987 * ... body ...
5988 * JUMP_ALWAYS end
5989 * else:
5990 * ... body ...
5991 * end:
5992 */
5993 static char_u *
5994compile_if(char_u *arg, cctx_T *cctx)
5995{
5996 char_u *p = arg;
5997 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005998 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006000 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006001 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
Bram Moolenaara5565e42020-05-09 15:44:01 +02006003 CLEAR_FIELD(ppconst);
6004 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006005 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006006 clear_ppconst(&ppconst);
6007 return NULL;
6008 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006009 if (cctx->ctx_skip == SKIP_YES)
6010 clear_ppconst(&ppconst);
6011 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006012 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006013 int error = FALSE;
6014 int v;
6015
Bram Moolenaara5565e42020-05-09 15:44:01 +02006016 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006017 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006018 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006019 if (error)
6020 return NULL;
6021 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006022 }
6023 else
6024 {
6025 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006026 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006027 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006028 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006029 if (bool_on_stack(cctx) == FAIL)
6030 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032
6033 scope = new_scope(cctx, IF_SCOPE);
6034 if (scope == NULL)
6035 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006036 scope->se_skip_save = skip_save;
6037 // "is_had_return" will be reset if any block does not end in :return
6038 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006040 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006041 {
6042 // "where" is set when ":elseif", "else" or ":endif" is found
6043 scope->se_u.se_if.is_if_label = instr->ga_len;
6044 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6045 }
6046 else
6047 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048
6049 return p;
6050}
6051
6052 static char_u *
6053compile_elseif(char_u *arg, cctx_T *cctx)
6054{
6055 char_u *p = arg;
6056 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006057 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 isn_T *isn;
6059 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006060 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006061 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062
6063 if (scope == NULL || scope->se_type != IF_SCOPE)
6064 {
6065 emsg(_(e_elseif_without_if));
6066 return NULL;
6067 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006068 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006069 if (!cctx->ctx_had_return)
6070 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006072 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006073 {
6074 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006076 return NULL;
6077 // previous "if" or "elseif" jumps here
6078 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6079 isn->isn_arg.jump.jump_where = instr->ga_len;
6080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006082 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006083 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006084 if (cctx->ctx_skip == SKIP_YES)
6085 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006086 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006087 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006088 clear_ppconst(&ppconst);
6089 return NULL;
6090 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006091 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006092 if (scope->se_skip_save == SKIP_YES)
6093 clear_ppconst(&ppconst);
6094 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006095 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006096 int error = FALSE;
6097 int v;
6098
Bram Moolenaar7f141552020-05-09 17:35:53 +02006099 // The expression results in a constant.
6100 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006101 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6102 if (error)
6103 return NULL;
6104 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006105 clear_ppconst(&ppconst);
6106 scope->se_u.se_if.is_if_label = -1;
6107 }
6108 else
6109 {
6110 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006111 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006112 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006113 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006114 if (bool_on_stack(cctx) == FAIL)
6115 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006117 // "where" is set when ":elseif", "else" or ":endif" is found
6118 scope->se_u.se_if.is_if_label = instr->ga_len;
6119 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
6122 return p;
6123}
6124
6125 static char_u *
6126compile_else(char_u *arg, cctx_T *cctx)
6127{
6128 char_u *p = arg;
6129 garray_T *instr = &cctx->ctx_instr;
6130 isn_T *isn;
6131 scope_T *scope = cctx->ctx_scope;
6132
6133 if (scope == NULL || scope->se_type != IF_SCOPE)
6134 {
6135 emsg(_(e_else_without_if));
6136 return NULL;
6137 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006138 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006139 if (!cctx->ctx_had_return)
6140 scope->se_u.se_if.is_had_return = FALSE;
6141 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142
Bram Moolenaarefd88552020-06-18 20:50:10 +02006143 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006144 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006145 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006146 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006147 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006148 if (!cctx->ctx_had_return
6149 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6150 JUMP_ALWAYS, cctx) == FAIL)
6151 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006152 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006153
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006154 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006155 {
6156 if (scope->se_u.se_if.is_if_label >= 0)
6157 {
6158 // previous "if" or "elseif" jumps here
6159 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6160 isn->isn_arg.jump.jump_where = instr->ga_len;
6161 scope->se_u.se_if.is_if_label = -1;
6162 }
6163 }
6164
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006165 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006166 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
6169 return p;
6170}
6171
6172 static char_u *
6173compile_endif(char_u *arg, cctx_T *cctx)
6174{
6175 scope_T *scope = cctx->ctx_scope;
6176 ifscope_T *ifscope;
6177 garray_T *instr = &cctx->ctx_instr;
6178 isn_T *isn;
6179
6180 if (scope == NULL || scope->se_type != IF_SCOPE)
6181 {
6182 emsg(_(e_endif_without_if));
6183 return NULL;
6184 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006185 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006186 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006187 if (!cctx->ctx_had_return)
6188 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006190 if (scope->se_u.se_if.is_if_label >= 0)
6191 {
6192 // previous "if" or "elseif" jumps here
6193 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6194 isn->isn_arg.jump.jump_where = instr->ga_len;
6195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 // Fill in the "end" label in jumps at the end of the blocks.
6197 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006198 cctx->ctx_skip = scope->se_skip_save;
6199
6200 // If all the blocks end in :return and there is an :else then the
6201 // had_return flag is set.
6202 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006204 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 return arg;
6206}
6207
6208/*
6209 * compile "for var in expr"
6210 *
6211 * Produces instructions:
6212 * PUSHNR -1
6213 * STORE loop-idx Set index to -1
6214 * EVAL expr Push result of "expr"
6215 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6216 * - if beyond end, jump to "end"
6217 * - otherwise get item from list and push it
6218 * STORE var Store item in "var"
6219 * ... body ...
6220 * JUMP top Jump back to repeat
6221 * end: DROP Drop the result of "expr"
6222 *
6223 */
6224 static char_u *
6225compile_for(char_u *arg, cctx_T *cctx)
6226{
6227 char_u *p;
6228 size_t varlen;
6229 garray_T *instr = &cctx->ctx_instr;
6230 garray_T *stack = &cctx->ctx_type_stack;
6231 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006232 lvar_T *loop_lvar; // loop iteration variable
6233 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 type_T *vartype;
6235
6236 // TODO: list of variables: "for [key, value] in dict"
6237 // parse "var"
6238 for (p = arg; eval_isnamec1(*p); ++p)
6239 ;
6240 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006241 var_lvar = lookup_local(arg, varlen, cctx);
6242 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006244 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245 return NULL;
6246 }
6247
6248 // consume "in"
6249 p = skipwhite(p);
6250 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6251 {
6252 emsg(_(e_missing_in));
6253 return NULL;
6254 }
6255 p = skipwhite(p + 2);
6256
6257
6258 scope = new_scope(cctx, FOR_SCOPE);
6259 if (scope == NULL)
6260 return NULL;
6261
6262 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006263 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6264 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006265 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006266 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006267 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270
6271 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006272 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6273 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006274 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006275 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006276 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006280 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281
6282 // compile "expr", it remains on the stack until "endfor"
6283 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006284 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006285 {
6286 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006290 // Now that we know the type of "var", check that it is a list, now or at
6291 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006293 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006295 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 return NULL;
6297 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006298 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006299 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300
6301 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006302 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006304 generate_FOR(cctx, loop_lvar->lv_idx);
6305 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
6307 return arg;
6308}
6309
6310/*
6311 * compile "endfor"
6312 */
6313 static char_u *
6314compile_endfor(char_u *arg, cctx_T *cctx)
6315{
6316 garray_T *instr = &cctx->ctx_instr;
6317 scope_T *scope = cctx->ctx_scope;
6318 forscope_T *forscope;
6319 isn_T *isn;
6320
6321 if (scope == NULL || scope->se_type != FOR_SCOPE)
6322 {
6323 emsg(_(e_for));
6324 return NULL;
6325 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006326 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006328 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 // At end of ":for" scope jump back to the FOR instruction.
6331 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6332
6333 // Fill in the "end" label in the FOR statement so it can jump here
6334 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6335 isn->isn_arg.forloop.for_end = instr->ga_len;
6336
6337 // Fill in the "end" label any BREAK statements
6338 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6339
6340 // Below the ":for" scope drop the "expr" list from the stack.
6341 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6342 return NULL;
6343
6344 vim_free(scope);
6345
6346 return arg;
6347}
6348
6349/*
6350 * compile "while expr"
6351 *
6352 * Produces instructions:
6353 * top: EVAL expr Push result of "expr"
6354 * JUMP_IF_FALSE end jump if false
6355 * ... body ...
6356 * JUMP top Jump back to repeat
6357 * end:
6358 *
6359 */
6360 static char_u *
6361compile_while(char_u *arg, cctx_T *cctx)
6362{
6363 char_u *p = arg;
6364 garray_T *instr = &cctx->ctx_instr;
6365 scope_T *scope;
6366
6367 scope = new_scope(cctx, WHILE_SCOPE);
6368 if (scope == NULL)
6369 return NULL;
6370
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006371 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372
6373 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006374 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 return NULL;
6376
Bram Moolenaar13106602020-10-04 16:06:05 +02006377 if (bool_on_stack(cctx) == FAIL)
6378 return FAIL;
6379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006381 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 JUMP_IF_FALSE, cctx) == FAIL)
6383 return FAIL;
6384
6385 return p;
6386}
6387
6388/*
6389 * compile "endwhile"
6390 */
6391 static char_u *
6392compile_endwhile(char_u *arg, cctx_T *cctx)
6393{
6394 scope_T *scope = cctx->ctx_scope;
6395
6396 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6397 {
6398 emsg(_(e_while));
6399 return NULL;
6400 }
6401 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006402 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
6404 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006405 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
6407 // Fill in the "end" label in the WHILE statement so it can jump here.
6408 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006409 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410
6411 vim_free(scope);
6412
6413 return arg;
6414}
6415
6416/*
6417 * compile "continue"
6418 */
6419 static char_u *
6420compile_continue(char_u *arg, cctx_T *cctx)
6421{
6422 scope_T *scope = cctx->ctx_scope;
6423
6424 for (;;)
6425 {
6426 if (scope == NULL)
6427 {
6428 emsg(_(e_continue));
6429 return NULL;
6430 }
6431 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6432 break;
6433 scope = scope->se_outer;
6434 }
6435
6436 // Jump back to the FOR or WHILE instruction.
6437 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006438 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6439 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 return arg;
6441}
6442
6443/*
6444 * compile "break"
6445 */
6446 static char_u *
6447compile_break(char_u *arg, cctx_T *cctx)
6448{
6449 scope_T *scope = cctx->ctx_scope;
6450 endlabel_T **el;
6451
6452 for (;;)
6453 {
6454 if (scope == NULL)
6455 {
6456 emsg(_(e_break));
6457 return NULL;
6458 }
6459 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6460 break;
6461 scope = scope->se_outer;
6462 }
6463
6464 // Jump to the end of the FOR or WHILE loop.
6465 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006466 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006468 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6470 return FAIL;
6471
6472 return arg;
6473}
6474
6475/*
6476 * compile "{" start of block
6477 */
6478 static char_u *
6479compile_block(char_u *arg, cctx_T *cctx)
6480{
6481 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6482 return NULL;
6483 return skipwhite(arg + 1);
6484}
6485
6486/*
6487 * compile end of block: drop one scope
6488 */
6489 static void
6490compile_endblock(cctx_T *cctx)
6491{
6492 scope_T *scope = cctx->ctx_scope;
6493
6494 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006495 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 vim_free(scope);
6497}
6498
6499/*
6500 * compile "try"
6501 * Creates a new scope for the try-endtry, pointing to the first catch and
6502 * finally.
6503 * Creates another scope for the "try" block itself.
6504 * TRY instruction sets up exception handling at runtime.
6505 *
6506 * "try"
6507 * TRY -> catch1, -> finally push trystack entry
6508 * ... try block
6509 * "throw {exception}"
6510 * EVAL {exception}
6511 * THROW create exception
6512 * ... try block
6513 * " catch {expr}"
6514 * JUMP -> finally
6515 * catch1: PUSH exeception
6516 * EVAL {expr}
6517 * MATCH
6518 * JUMP nomatch -> catch2
6519 * CATCH remove exception
6520 * ... catch block
6521 * " catch"
6522 * JUMP -> finally
6523 * catch2: CATCH remove exception
6524 * ... catch block
6525 * " finally"
6526 * finally:
6527 * ... finally block
6528 * " endtry"
6529 * ENDTRY pop trystack entry, may rethrow
6530 */
6531 static char_u *
6532compile_try(char_u *arg, cctx_T *cctx)
6533{
6534 garray_T *instr = &cctx->ctx_instr;
6535 scope_T *try_scope;
6536 scope_T *scope;
6537
6538 // scope that holds the jumps that go to catch/finally/endtry
6539 try_scope = new_scope(cctx, TRY_SCOPE);
6540 if (try_scope == NULL)
6541 return NULL;
6542
6543 // "catch" is set when the first ":catch" is found.
6544 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006545 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 if (generate_instr(cctx, ISN_TRY) == NULL)
6547 return NULL;
6548
6549 // scope for the try block itself
6550 scope = new_scope(cctx, BLOCK_SCOPE);
6551 if (scope == NULL)
6552 return NULL;
6553
6554 return arg;
6555}
6556
6557/*
6558 * compile "catch {expr}"
6559 */
6560 static char_u *
6561compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6562{
6563 scope_T *scope = cctx->ctx_scope;
6564 garray_T *instr = &cctx->ctx_instr;
6565 char_u *p;
6566 isn_T *isn;
6567
6568 // end block scope from :try or :catch
6569 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6570 compile_endblock(cctx);
6571 scope = cctx->ctx_scope;
6572
6573 // Error if not in a :try scope
6574 if (scope == NULL || scope->se_type != TRY_SCOPE)
6575 {
6576 emsg(_(e_catch));
6577 return NULL;
6578 }
6579
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006580 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006582 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 return NULL;
6584 }
6585
6586 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006587 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 JUMP_ALWAYS, cctx) == FAIL)
6589 return NULL;
6590
6591 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006592 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 if (isn->isn_arg.try.try_catch == 0)
6594 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006595 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 {
6597 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006598 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 isn->isn_arg.jump.jump_where = instr->ga_len;
6600 }
6601
6602 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006603 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006605 scope->se_u.se_try.ts_caught_all = TRUE;
6606 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 }
6608 else
6609 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006610 char_u *end;
6611 char_u *pat;
6612 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006613 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006614 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 // Push v:exception, push {expr} and MATCH
6617 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6618
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006619 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006620 if (*end != *p)
6621 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006622 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006623 vim_free(tofree);
6624 return FAIL;
6625 }
6626 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006627 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006628 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006629 len = (int)(end - tofree);
6630 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006631 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006632 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006633 if (pat == NULL)
6634 return FAIL;
6635 if (generate_PUSHS(cctx, pat) == FAIL)
6636 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6639 return NULL;
6640
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006641 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6643 return NULL;
6644 }
6645
6646 if (generate_instr(cctx, ISN_CATCH) == NULL)
6647 return NULL;
6648
6649 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6650 return NULL;
6651 return p;
6652}
6653
6654 static char_u *
6655compile_finally(char_u *arg, cctx_T *cctx)
6656{
6657 scope_T *scope = cctx->ctx_scope;
6658 garray_T *instr = &cctx->ctx_instr;
6659 isn_T *isn;
6660
6661 // end block scope from :try or :catch
6662 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6663 compile_endblock(cctx);
6664 scope = cctx->ctx_scope;
6665
6666 // Error if not in a :try scope
6667 if (scope == NULL || scope->se_type != TRY_SCOPE)
6668 {
6669 emsg(_(e_finally));
6670 return NULL;
6671 }
6672
6673 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006674 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 if (isn->isn_arg.try.try_finally != 0)
6676 {
6677 emsg(_(e_finally_dup));
6678 return NULL;
6679 }
6680
6681 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006682 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683
Bram Moolenaar585fea72020-04-02 22:33:21 +02006684 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006685 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 {
6687 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006688 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006690 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 }
6692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 // TODO: set index in ts_finally_label jumps
6694
6695 return arg;
6696}
6697
6698 static char_u *
6699compile_endtry(char_u *arg, cctx_T *cctx)
6700{
6701 scope_T *scope = cctx->ctx_scope;
6702 garray_T *instr = &cctx->ctx_instr;
6703 isn_T *isn;
6704
6705 // end block scope from :catch or :finally
6706 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6707 compile_endblock(cctx);
6708 scope = cctx->ctx_scope;
6709
6710 // Error if not in a :try scope
6711 if (scope == NULL || scope->se_type != TRY_SCOPE)
6712 {
6713 if (scope == NULL)
6714 emsg(_(e_no_endtry));
6715 else if (scope->se_type == WHILE_SCOPE)
6716 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006717 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 emsg(_(e_endfor));
6719 else
6720 emsg(_(e_endif));
6721 return NULL;
6722 }
6723
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006724 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6726 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006727 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 return NULL;
6729 }
6730
6731 // Fill in the "end" label in jumps at the end of the blocks, if not done
6732 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006733 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734
6735 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006736 if (isn->isn_arg.try.try_catch == 0)
6737 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 if (isn->isn_arg.try.try_finally == 0)
6739 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006740
6741 if (scope->se_u.se_try.ts_catch_label != 0)
6742 {
6743 // Last catch without match jumps here
6744 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6745 isn->isn_arg.jump.jump_where = instr->ga_len;
6746 }
6747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 compile_endblock(cctx);
6749
6750 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6751 return NULL;
6752 return arg;
6753}
6754
6755/*
6756 * compile "throw {expr}"
6757 */
6758 static char_u *
6759compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6760{
6761 char_u *p = skipwhite(arg);
6762
Bram Moolenaara5565e42020-05-09 15:44:01 +02006763 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 return NULL;
6765 if (may_generate_2STRING(-1, cctx) == FAIL)
6766 return NULL;
6767 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6768 return NULL;
6769
6770 return p;
6771}
6772
6773/*
6774 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006775 * compile "echomsg expr"
6776 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006777 * compile "execute expr"
6778 */
6779 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006780compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006781{
6782 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006783 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006784 int count = 0;
6785
6786 for (;;)
6787 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006788 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006789 return NULL;
6790 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006791 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006792 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006793 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006794 break;
6795 }
6796
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006797 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6798 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6799 else if (cmdidx == CMD_execute)
6800 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6801 else if (cmdidx == CMD_echomsg)
6802 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6803 else
6804 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 return p;
6806}
6807
6808/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006809 * :put r
6810 * :put ={expr}
6811 */
6812 static char_u *
6813compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6814{
6815 char_u *line = arg;
6816 linenr_T lnum;
6817 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006818 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006819
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006820 eap->regname = *line;
6821
6822 if (eap->regname == '=')
6823 {
6824 char_u *p = line + 1;
6825
6826 if (compile_expr0(&p, cctx) == FAIL)
6827 return NULL;
6828 line = p;
6829 }
6830 else if (eap->regname != NUL)
6831 ++line;
6832
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006833 // "errormsg" will not be set because the range is ADDR_LINES.
6834 // TODO: if the range contains something like "$" or "." need to evaluate
6835 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006836 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6837 return NULL;
6838 if (eap->addr_count == 0)
6839 lnum = -1;
6840 else
6841 lnum = eap->line2;
6842 if (above)
6843 --lnum;
6844
6845 generate_PUT(cctx, eap->regname, lnum);
6846 return line;
6847}
6848
6849/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006850 * A command that is not compiled, execute with legacy code.
6851 */
6852 static char_u *
6853compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6854{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006855 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006856 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006857 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006858
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006859 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006860 goto theend;
6861
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006862 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006863 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006864 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006865 int usefilter = FALSE;
6866
6867 has_expr = argt & (EX_XFILE | EX_EXPAND);
6868
6869 // If the command can be followed by a bar, find the bar and truncate
6870 // it, so that the following command can be compiled.
6871 // The '|' is overwritten with a NUL, it is put back below.
6872 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6873 && *eap->arg == '!')
6874 // :w !filter or :r !filter or :r! filter
6875 usefilter = TRUE;
6876 if ((argt & EX_TRLBAR) && !usefilter)
6877 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006878 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006879 separate_nextcmd(eap);
6880 if (eap->nextcmd != NULL)
6881 nextcmd = eap->nextcmd;
6882 }
6883 }
6884
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006885 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6886 {
6887 // expand filename in "syntax include [@group] filename"
6888 has_expr = TRUE;
6889 eap->arg = skipwhite(eap->arg + 7);
6890 if (*eap->arg == '@')
6891 eap->arg = skiptowhite(eap->arg);
6892 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006893
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006894 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006895 {
6896 int count = 0;
6897 char_u *start = skipwhite(line);
6898
6899 // :cmd xxx`=expr1`yyy`=expr2`zzz
6900 // PUSHS ":cmd xxx"
6901 // eval expr1
6902 // PUSHS "yyy"
6903 // eval expr2
6904 // PUSHS "zzz"
6905 // EXECCONCAT 5
6906 for (;;)
6907 {
6908 if (p > start)
6909 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006910 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006911 ++count;
6912 }
6913 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006914 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006915 return NULL;
6916 may_generate_2STRING(-1, cctx);
6917 ++count;
6918 p = skipwhite(p);
6919 if (*p != '`')
6920 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006921 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006922 return NULL;
6923 }
6924 start = p + 1;
6925
6926 p = (char_u *)strstr((char *)start, "`=");
6927 if (p == NULL)
6928 {
6929 if (*skipwhite(start) != NUL)
6930 {
6931 generate_PUSHS(cctx, vim_strsave(start));
6932 ++count;
6933 }
6934 break;
6935 }
6936 }
6937 generate_EXECCONCAT(cctx, count);
6938 }
6939 else
6940 generate_EXEC(cctx, line);
6941
6942theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006943 if (*nextcmd != NUL)
6944 {
6945 // the parser expects a pointer to the bar, put it back
6946 --nextcmd;
6947 *nextcmd = '|';
6948 }
6949
6950 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006951}
6952
6953/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006954 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006955 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006956 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006957 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006958add_def_function(ufunc_T *ufunc)
6959{
6960 dfunc_T *dfunc;
6961
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006962 if (def_functions.ga_len == 0)
6963 {
6964 // The first position is not used, so that a zero uf_dfunc_idx means it
6965 // wasn't set.
6966 if (ga_grow(&def_functions, 1) == FAIL)
6967 return FAIL;
6968 ++def_functions.ga_len;
6969 }
6970
Bram Moolenaar09689a02020-05-09 22:50:08 +02006971 // Add the function to "def_functions".
6972 if (ga_grow(&def_functions, 1) == FAIL)
6973 return FAIL;
6974 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6975 CLEAR_POINTER(dfunc);
6976 dfunc->df_idx = def_functions.ga_len;
6977 ufunc->uf_dfunc_idx = dfunc->df_idx;
6978 dfunc->df_ufunc = ufunc;
6979 ++def_functions.ga_len;
6980 return OK;
6981}
6982
6983/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 * After ex_function() has collected all the function lines: parse and compile
6985 * the lines into instructions.
6986 * Adds the function to "def_functions".
6987 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6988 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006989 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006990 * This can be used recursively through compile_lambda(), which may reallocate
6991 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006992 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006994 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006995compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 char_u *line = NULL;
6998 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 cctx_T cctx;
7001 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007002 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 int ret = FAIL;
7004 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007005 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007006 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007007 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007009 // When using a function that was compiled before: Free old instructions.
7010 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007011 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007013 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7014 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007015 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007017 else
7018 {
7019 if (add_def_function(ufunc) == FAIL)
7020 return FAIL;
7021 new_def_function = TRUE;
7022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023
Bram Moolenaar985116a2020-07-12 17:31:09 +02007024 ufunc->uf_def_status = UF_COMPILING;
7025
Bram Moolenaara80faa82020-04-12 19:37:17 +02007026 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 cctx.ctx_ufunc = ufunc;
7028 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007029 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7031 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7032 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7033 cctx.ctx_type_list = &ufunc->uf_type_list;
7034 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7035 instr = &cctx.ctx_instr;
7036
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007037 // Set the context to the function, it may be compiled when called from
7038 // another script. Set the script version to the most modern one.
7039 // The line number will be set in next_line_from_context().
7040 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7042
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007043 // Make sure error messages are OK.
7044 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7045 if (do_estack_push)
7046 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007047 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007048
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007049 if (ufunc->uf_def_args.ga_len > 0)
7050 {
7051 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007052 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007053 int i;
7054 char_u *arg;
7055 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007056 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007057
7058 // Produce instructions for the default values of optional arguments.
7059 // Store the instruction index in uf_def_arg_idx[] so that we know
7060 // where to start when the function is called, depending on the number
7061 // of arguments.
7062 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7063 if (ufunc->uf_def_arg_idx == NULL)
7064 goto erret;
7065 for (i = 0; i < count; ++i)
7066 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007067 garray_T *stack = &cctx.ctx_type_stack;
7068 type_T *val_type;
7069 int arg_idx = first_def_arg + i;
7070
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007071 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7072 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007073 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007074 goto erret;
7075
7076 // If no type specified use the type of the default value.
7077 // Otherwise check that the default value type matches the
7078 // specified type.
7079 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7080 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007081 {
7082 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007083 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007084 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007085 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7086 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007087 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007088
7089 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007090 goto erret;
7091 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007092 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007093
7094 if (did_set_arg_type)
7095 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007096 }
7097
7098 /*
7099 * Loop over all the lines of the function and generate instructions.
7100 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 for (;;)
7102 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007103 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007104 int starts_with_colon = FALSE;
7105 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007106 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007107
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007108 // Bail out on the first error to avoid a flood of errors and report
7109 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007110 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007111 goto erret;
7112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 if (line != NULL && *line == '|')
7114 // the line continues after a '|'
7115 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007116 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007117 && !(*line == '#' && (line == cctx.ctx_line_start
7118 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007119 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007120 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 goto erret;
7122 }
7123 else
7124 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007125 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007126 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007127 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 }
7130
Bram Moolenaara80faa82020-04-12 19:37:17 +02007131 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 ea.cmdlinep = &line;
7133 ea.cmd = skipwhite(line);
7134
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007135 // Some things can be recognized by the first character.
7136 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007138 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007139 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007140 if (ea.cmd[1] != '{')
7141 {
7142 line = (char_u *)"";
7143 continue;
7144 }
7145 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007147 case '}':
7148 {
7149 // "}" ends a block scope
7150 scopetype_T stype = cctx.ctx_scope == NULL
7151 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007153 if (stype == BLOCK_SCOPE)
7154 {
7155 compile_endblock(&cctx);
7156 line = ea.cmd;
7157 }
7158 else
7159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007160 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007161 goto erret;
7162 }
7163 if (line != NULL)
7164 line = skipwhite(ea.cmd + 1);
7165 continue;
7166 }
7167
7168 case '{':
7169 // "{" starts a block scope
7170 // "{'a': 1}->func() is something else
7171 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7172 {
7173 line = compile_block(ea.cmd, &cctx);
7174 continue;
7175 }
7176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 }
7178
7179 /*
7180 * COMMAND MODIFIERS
7181 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007182 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007183 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7184 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 {
7186 if (errormsg != NULL)
7187 goto erret;
7188 // empty line or comment
7189 line = (char_u *)"";
7190 continue;
7191 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007192 generate_cmdmods(&cctx, &local_cmdmod);
7193 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007195 // Check if there was a colon after the last command modifier or before
7196 // the current position.
7197 for (p = ea.cmd; p >= line; --p)
7198 {
7199 if (*p == ':')
7200 starts_with_colon = TRUE;
7201 if (p < ea.cmd && !VIM_ISWHITE(*p))
7202 break;
7203 }
7204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007206 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007208 {
7209 if (*ea.cmd == '(')
7210 // not for "call()"
7211 ea.cmd = p;
7212 else
7213 ea.cmd = skipwhite(ea.cmd);
7214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007216 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007218 char_u *pskip;
7219
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007220 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007221 // find what follows.
7222 // Skip over "var.member", "var[idx]" and the like.
7223 // Also "&opt = val", "$ENV = val" and "@r = val".
7224 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007225 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007226 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007227 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007229 char_u *var_end;
7230 int oplen;
7231 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
Bram Moolenaar65821722020-08-02 18:58:54 +02007233 if (ea.cmd[0] == '@')
7234 var_end = ea.cmd + 2;
7235 else
7236 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007237 FNE_CHECK_START | FNE_INCL_BR);
7238 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007239 if (oplen > 0)
7240 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007241 size_t len = p - ea.cmd;
7242
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007243 // Recognize an assignment if we recognize the variable
7244 // name:
7245 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007246 // "local = expr" where "local" is a local var.
7247 // "script = expr" where "script" is a script-local var.
7248 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007249 // "&opt = expr"
7250 // "$ENV = expr"
7251 // "@r = expr"
7252 if (*ea.cmd == '&'
7253 || *ea.cmd == '$'
7254 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007255 || ((len) > 2 && ea.cmd[1] == ':')
7256 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007257 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007258 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007259 || script_var_exists(ea.cmd, len,
7260 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007261 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007262 {
7263 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007264 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007265 goto erret;
7266 continue;
7267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 }
7269 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007270
7271 if (*ea.cmd == '[')
7272 {
7273 // [var, var] = expr
7274 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7275 if (line == NULL)
7276 goto erret;
7277 if (line != ea.cmd)
7278 continue;
7279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 }
7281
7282 /*
7283 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007284 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007286 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007287 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007288 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007289 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007290 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007291 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007292 if (!starts_with_colon)
7293 {
7294 emsg(_(e_colon_required_before_a_range));
7295 goto erret;
7296 }
7297 if (ends_excmd2(line, ea.cmd))
7298 {
7299 // A range without a command: jump to the line.
7300 // TODO: compile to a more efficient command, possibly
7301 // calling parse_cmd_address().
7302 ea.cmdidx = CMD_SIZE;
7303 line = compile_exec(line, &ea, &cctx);
7304 goto nextline;
7305 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007306 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007307 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007308 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007309 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007310 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
7312 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7313 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007314 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007315 {
7316 line += STRLEN(line);
7317 continue;
7318 }
7319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007321 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007323 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007324 iemsg("Command from find_ex_command() not handled");
7325 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 }
7328
Bram Moolenaar3988f642020-08-27 22:43:03 +02007329 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007330 && ea.cmdidx != CMD_elseif
7331 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007332 && ea.cmdidx != CMD_endif
7333 && ea.cmdidx != CMD_endfor
7334 && ea.cmdidx != CMD_endwhile
7335 && ea.cmdidx != CMD_catch
7336 && ea.cmdidx != CMD_finally
7337 && ea.cmdidx != CMD_endtry)
7338 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007339 emsg(_(e_unreachable_code_after_return));
7340 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007341 }
7342
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007343 p = skipwhite(p);
7344 if (ea.cmdidx != CMD_SIZE
7345 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7346 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007347 if (ea.cmdidx >= 0)
7348 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007349 if ((ea.argt & EX_BANG) && *p == '!')
7350 {
7351 ea.forceit = TRUE;
7352 p = skipwhite(p + 1);
7353 }
7354 }
7355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 switch (ea.cmdidx)
7357 {
7358 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007359 ea.arg = p;
7360 line = compile_nested_function(&ea, &cctx);
7361 break;
7362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007364 // TODO: should we allow this, e.g. to declare a global
7365 // function?
7366 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 goto erret;
7368
7369 case CMD_return:
7370 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 break;
7373
7374 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007375 emsg(_(e_cannot_use_let_in_vim9_script));
7376 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007377 case CMD_var:
7378 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case CMD_const:
7380 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007381 if (line == p)
7382 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 break;
7384
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007385 case CMD_unlet:
7386 case CMD_unlockvar:
7387 case CMD_lockvar:
7388 line = compile_unletlock(p, &ea, &cctx);
7389 break;
7390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 case CMD_import:
7392 line = compile_import(p, &cctx);
7393 break;
7394
7395 case CMD_if:
7396 line = compile_if(p, &cctx);
7397 break;
7398 case CMD_elseif:
7399 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 break;
7402 case CMD_else:
7403 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007404 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 break;
7406 case CMD_endif:
7407 line = compile_endif(p, &cctx);
7408 break;
7409
7410 case CMD_while:
7411 line = compile_while(p, &cctx);
7412 break;
7413 case CMD_endwhile:
7414 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007415 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 break;
7417
7418 case CMD_for:
7419 line = compile_for(p, &cctx);
7420 break;
7421 case CMD_endfor:
7422 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007423 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 break;
7425 case CMD_continue:
7426 line = compile_continue(p, &cctx);
7427 break;
7428 case CMD_break:
7429 line = compile_break(p, &cctx);
7430 break;
7431
7432 case CMD_try:
7433 line = compile_try(p, &cctx);
7434 break;
7435 case CMD_catch:
7436 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007437 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 break;
7439 case CMD_finally:
7440 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007441 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 break;
7443 case CMD_endtry:
7444 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007445 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 break;
7447 case CMD_throw:
7448 line = compile_throw(p, &cctx);
7449 break;
7450
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007451 case CMD_eval:
7452 if (compile_expr0(&p, &cctx) == FAIL)
7453 goto erret;
7454
Bram Moolenaar3988f642020-08-27 22:43:03 +02007455 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007456 generate_instr_drop(&cctx, ISN_DROP, 1);
7457
7458 line = skipwhite(p);
7459 break;
7460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007463 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007464 case CMD_echomsg:
7465 case CMD_echoerr:
7466 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007467 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007469 case CMD_put:
7470 ea.cmd = cmd;
7471 line = compile_put(p, &ea, &cctx);
7472 break;
7473
Bram Moolenaar3988f642020-08-27 22:43:03 +02007474 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007475
Bram Moolenaarae616492020-07-28 20:07:27 +02007476 case CMD_append:
7477 case CMD_change:
7478 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007479 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007480 case CMD_xit:
7481 not_in_vim9(&ea);
7482 goto erret;
7483
Bram Moolenaar002262f2020-07-08 17:47:57 +02007484 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007485 if (cctx.ctx_skip != SKIP_YES)
7486 {
7487 semsg(_(e_invalid_command_str), ea.cmd);
7488 goto erret;
7489 }
7490 // We don't check for a next command here.
7491 line = (char_u *)"";
7492 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007495 if (cctx.ctx_skip == SKIP_YES)
7496 {
7497 // We don't check for a next command here.
7498 line = (char_u *)"";
7499 }
7500 else
7501 {
7502 // Not recognized, execute with do_cmdline_cmd().
7503 ea.arg = p;
7504 line = compile_exec(line, &ea, &cctx);
7505 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 break;
7507 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007508nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 if (line == NULL)
7510 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007511 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007513 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007514 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 if (cctx.ctx_type_stack.ga_len < 0)
7517 {
7518 iemsg("Type stack underflow");
7519 goto erret;
7520 }
7521 }
7522
7523 if (cctx.ctx_scope != NULL)
7524 {
7525 if (cctx.ctx_scope->se_type == IF_SCOPE)
7526 emsg(_(e_endif));
7527 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7528 emsg(_(e_endwhile));
7529 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7530 emsg(_(e_endfor));
7531 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007532 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 goto erret;
7534 }
7535
Bram Moolenaarefd88552020-06-18 20:50:10 +02007536 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 {
7538 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7539 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007540 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 goto erret;
7542 }
7543
7544 // Return zero if there is no return at the end.
7545 generate_PUSHNR(&cctx, 0);
7546 generate_instr(&cctx, ISN_RETURN);
7547 }
7548
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007549 {
7550 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7551 + ufunc->uf_dfunc_idx;
7552 dfunc->df_deleted = FALSE;
7553 dfunc->df_instr = instr->ga_data;
7554 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007555 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007556 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007557 if (cctx.ctx_outer_used)
7558 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007559 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561
7562 ret = OK;
7563
7564erret:
7565 if (ret == FAIL)
7566 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007567 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7569 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007570
7571 for (idx = 0; idx < instr->ga_len; ++idx)
7572 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007574
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007575 // If using the last entry in the table and it was added above, we
7576 // might as well remove it.
7577 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007578 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007579 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007580 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007581 ufunc->uf_dfunc_idx = 0;
7582 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007583 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007584
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007585 while (cctx.ctx_scope != NULL)
7586 drop_scope(&cctx);
7587
Bram Moolenaar20431c92020-03-20 18:39:46 +01007588 // Don't execute this function body.
7589 ga_clear_strings(&ufunc->uf_lines);
7590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 if (errormsg != NULL)
7592 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007593 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007594 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 }
7596
7597 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007598 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007599 if (do_estack_push)
7600 estack_pop();
7601
Bram Moolenaar20431c92020-03-20 18:39:46 +01007602 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007603 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007605 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606}
7607
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007608 void
7609set_function_type(ufunc_T *ufunc)
7610{
7611 int varargs = ufunc->uf_va_name != NULL;
7612 int argcount = ufunc->uf_args.ga_len;
7613
7614 // Create a type for the function, with the return type and any
7615 // argument types.
7616 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7617 // The type is included in "tt_args".
7618 if (argcount > 0 || varargs)
7619 {
7620 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7621 argcount, &ufunc->uf_type_list);
7622 // Add argument types to the function type.
7623 if (func_type_add_arg_types(ufunc->uf_func_type,
7624 argcount + varargs,
7625 &ufunc->uf_type_list) == FAIL)
7626 return;
7627 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7628 ufunc->uf_func_type->tt_min_argcount =
7629 argcount - ufunc->uf_def_args.ga_len;
7630 if (ufunc->uf_arg_types == NULL)
7631 {
7632 int i;
7633
7634 // lambda does not have argument types.
7635 for (i = 0; i < argcount; ++i)
7636 ufunc->uf_func_type->tt_args[i] = &t_any;
7637 }
7638 else
7639 mch_memmove(ufunc->uf_func_type->tt_args,
7640 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7641 if (varargs)
7642 {
7643 ufunc->uf_func_type->tt_args[argcount] =
7644 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7645 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7646 }
7647 }
7648 else
7649 // No arguments, can use a predefined type.
7650 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7651 argcount, &ufunc->uf_type_list);
7652}
7653
7654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655/*
7656 * Delete an instruction, free what it contains.
7657 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007658 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659delete_instr(isn_T *isn)
7660{
7661 switch (isn->isn_type)
7662 {
7663 case ISN_EXEC:
7664 case ISN_LOADENV:
7665 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007666 case ISN_LOADB:
7667 case ISN_LOADW:
7668 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007669 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007670 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 case ISN_PUSHEXC:
7672 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007673 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007675 case ISN_STOREB:
7676 case ISN_STOREW:
7677 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007678 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 vim_free(isn->isn_arg.string);
7680 break;
7681
7682 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007683 case ISN_STORES:
7684 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 break;
7686
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007687 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007688 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007689 vim_free(isn->isn_arg.unlet.ul_name);
7690 break;
7691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 case ISN_STOREOPT:
7693 vim_free(isn->isn_arg.storeopt.so_name);
7694 break;
7695
7696 case ISN_PUSHBLOB: // push blob isn_arg.blob
7697 blob_unref(isn->isn_arg.blob);
7698 break;
7699
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007700 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007701#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007702 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007703#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007704 break;
7705
7706 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007708 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007709#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007710 break;
7711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 case ISN_UCALL:
7713 vim_free(isn->isn_arg.ufunc.cuf_name);
7714 break;
7715
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007716 case ISN_FUNCREF:
7717 {
7718 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7719 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007720
7721 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7722 func_ptr_unref(dfunc->df_ufunc);
7723 }
7724 break;
7725
7726 case ISN_DCALL:
7727 {
7728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7729 + isn->isn_arg.dfunc.cdf_idx;
7730
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007731 if (dfunc->df_ufunc != NULL
7732 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007733 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007734 }
7735 break;
7736
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007737 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007738 {
7739 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7740 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7741
7742 if (ufunc != NULL)
7743 {
7744 // Clear uf_dfunc_idx so that the function is deleted.
7745 clear_def_function(ufunc);
7746 ufunc->uf_dfunc_idx = 0;
7747 func_ptr_unref(ufunc);
7748 }
7749
7750 vim_free(lambda);
7751 vim_free(isn->isn_arg.newfunc.nf_global);
7752 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007753 break;
7754
Bram Moolenaar5e654232020-09-16 15:22:00 +02007755 case ISN_CHECKTYPE:
7756 free_type(isn->isn_arg.type.ct_type);
7757 break;
7758
Bram Moolenaar02194d22020-10-24 23:08:38 +02007759 case ISN_CMDMOD:
7760 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7761 ->cmod_filter_regmatch.regprog);
7762 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7763 break;
7764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 case ISN_2BOOL:
7766 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007767 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 case ISN_ADDBLOB:
7769 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007770 case ISN_ANYINDEX:
7771 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007773 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007775 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007777 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 case ISN_COMPAREANY:
7779 case ISN_COMPAREBLOB:
7780 case ISN_COMPAREBOOL:
7781 case ISN_COMPAREDICT:
7782 case ISN_COMPAREFLOAT:
7783 case ISN_COMPAREFUNC:
7784 case ISN_COMPARELIST:
7785 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786 case ISN_COMPARESPECIAL:
7787 case ISN_COMPARESTRING:
7788 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007789 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 case ISN_DROP:
7791 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007792 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007793 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007795 case ISN_EXECCONCAT:
7796 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007798 case ISN_GETITEM:
7799 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007800 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007801 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007802 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007804 case ISN_LOADBDICT:
7805 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007806 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007808 case ISN_LOADSCRIPT:
7809 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007811 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007812 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007813 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 case ISN_NEGATENR:
7815 case ISN_NEWDICT:
7816 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007818 case ISN_OPFLOAT:
7819 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007821 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007822 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 case ISN_PUSHF:
7824 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007826 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007828 case ISN_SHUFFLE:
7829 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007831 case ISN_STOREDICT:
7832 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007833 case ISN_STORENR:
7834 case ISN_STOREOUTER:
7835 case ISN_STOREREG:
7836 case ISN_STORESCRIPT:
7837 case ISN_STOREV:
7838 case ISN_STRINDEX:
7839 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 case ISN_THROW:
7841 case ISN_TRY:
7842 // nothing allocated
7843 break;
7844 }
7845}
7846
7847/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007848 * Free all instructions for "dfunc".
7849 */
7850 static void
7851delete_def_function_contents(dfunc_T *dfunc)
7852{
7853 int idx;
7854
7855 ga_clear(&dfunc->df_def_args_isn);
7856
7857 if (dfunc->df_instr != NULL)
7858 {
7859 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7860 delete_instr(dfunc->df_instr + idx);
7861 VIM_CLEAR(dfunc->df_instr);
7862 }
7863
7864 dfunc->df_deleted = TRUE;
7865}
7866
7867/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007868 * When a user function is deleted, clear the contents of any associated def
7869 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 */
7871 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007872clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007874 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 {
7876 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7877 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878
Bram Moolenaar20431c92020-03-20 18:39:46 +01007879 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007880 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 }
7882}
7883
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007884/*
7885 * Used when a user function is about to be deleted: remove the pointer to it.
7886 * The entry in def_functions is then unused.
7887 */
7888 void
7889unlink_def_function(ufunc_T *ufunc)
7890{
7891 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7892
7893 dfunc->df_ufunc = NULL;
7894}
7895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007897/*
7898 * Free all functions defined with ":def".
7899 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 void
7901free_def_functions(void)
7902{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007903 int idx;
7904
7905 for (idx = 0; idx < def_functions.ga_len; ++idx)
7906 {
7907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7908
7909 delete_def_function_contents(dfunc);
7910 }
7911
7912 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913}
7914#endif
7915
7916
7917#endif // FEAT_EVAL