blob: 60b55d560230d5baa41e2ec01e0cd396be60e8ce [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 {
1690 semsg(_(e_toofewarg), "[reference]");
1691 return FAIL;
1692 }
1693 if (!varargs && argcount > type->tt_argcount)
1694 {
1695 semsg(_(e_toomanyarg), "[reference]");
1696 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 Moolenaar1dcae592020-10-19 19:02:42 +02002645 if (type->tt_type == VAR_LIST)
2646 {
2647 // inline "add(list, item)" so that the type can be checked
2648 res = generate_LISTAPPEND(cctx);
2649 idx = -1;
2650 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002651 else if (type->tt_type == VAR_BLOB)
2652 {
2653 // inline "add(blob, nr)" so that the type can be checked
2654 res = generate_BLOBAPPEND(cctx);
2655 idx = -1;
2656 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002657 }
2658
2659 if (idx >= 0)
2660 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2661 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002662 else
2663 semsg(_(e_unknownfunc), namebuf);
2664 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
2666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002668 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002669 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002670 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002671 {
2672 res = generate_CALL(cctx, ufunc, argcount);
2673 goto theend;
2674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675
2676 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002677 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002678 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002680 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002681 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002682 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002683 garray_T *stack = &cctx->ctx_type_stack;
2684 type_T *type;
2685
2686 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2687 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002688 goto theend;
2689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690
Bram Moolenaar0f769812020-09-12 18:32:34 +02002691 // If we can find a global function by name generate the right call.
2692 if (ufunc != NULL)
2693 {
2694 res = generate_CALL(cctx, ufunc, argcount);
2695 goto theend;
2696 }
2697
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002698 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002699 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002700 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002701 res = generate_UCALL(cctx, name, argcount);
2702 else
2703 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002704
2705theend:
2706 vim_free(tofree);
2707 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708}
2709
2710// like NAMESPACE_CHAR but with 'a' and 'l'.
2711#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2712
2713/*
2714 * Find the end of a variable or function name. Unlike find_name_end() this
2715 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002716 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 * Return a pointer to just after the name. Equal to "arg" if there is no
2718 * valid name.
2719 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002720 static char_u *
2721to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722{
2723 char_u *p;
2724
2725 // Quick check for valid starting character.
2726 if (!eval_isnamec1(*arg))
2727 return arg;
2728
2729 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2730 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2731 // and can be used in slice "[n:]".
2732 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002733 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2735 break;
2736 return p;
2737}
2738
2739/*
2740 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002741 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 */
2743 char_u *
2744to_name_const_end(char_u *arg)
2745{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002746 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 typval_T rettv;
2748
2749 if (p == arg && *arg == '[')
2750 {
2751
2752 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002753 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 p = arg;
2755 }
2756 else if (p == arg && *arg == '#' && arg[1] == '{')
2757 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002758 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002760 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 p = arg;
2762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763
2764 return p;
2765}
2766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767/*
2768 * parse a list: [expr, expr]
2769 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002770 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 */
2772 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002773compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774{
2775 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002776 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002778 int is_const;
2779 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002781 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002783 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002784 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002785 semsg(_(e_list_end), *arg);
2786 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002787 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002788 if (*p == ',')
2789 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002790 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002791 return FAIL;
2792 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002793 if (*p == ']')
2794 {
2795 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002797 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002798 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002799 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002800 if (!is_const)
2801 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 ++count;
2803 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002804 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002806 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2807 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002808 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002809 return FAIL;
2810 }
2811 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002812 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 p = skipwhite(p);
2814 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002815 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002817 ppconst->pp_is_const = is_all_const;
2818 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819}
2820
2821/*
2822 * parse a lambda: {arg, arg -> expr}
2823 * "*arg" points to the '{'.
2824 */
2825 static int
2826compile_lambda(char_u **arg, cctx_T *cctx)
2827{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 typval_T rettv;
2829 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002830 evalarg_T evalarg;
2831
2832 CLEAR_FIELD(evalarg);
2833 evalarg.eval_flags = EVAL_EVALUATE;
2834 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835
2836 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002837 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002838 {
2839 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002841 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002844 ++ufunc->uf_refcount;
2845 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002846 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847
2848 // The function will have one line: "return {expr}".
2849 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002850 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002852 clear_evalarg(&evalarg, NULL);
2853
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002854 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002855 {
2856 // The return type will now be known.
2857 set_function_type(ufunc);
2858
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002859 // The function reference count will be 1. When the ISN_FUNCREF
2860 // instruction is deleted the reference count is decremented and the
2861 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002862 return generate_FUNCREF(cctx, ufunc);
2863 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002864
2865 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 return FAIL;
2867}
2868
2869/*
2870 * Compile a lamda call: expr->{lambda}(args)
2871 * "arg" points to the "{".
2872 */
2873 static int
2874compile_lambda_call(char_u **arg, cctx_T *cctx)
2875{
2876 ufunc_T *ufunc;
2877 typval_T rettv;
2878 int argcount = 1;
2879 int ret = FAIL;
2880
2881 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002882 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 return FAIL;
2884
2885 if (**arg != '(')
2886 {
2887 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002888 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 else
2890 semsg(_(e_missing_paren), "lambda");
2891 clear_tv(&rettv);
2892 return FAIL;
2893 }
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 ufunc = rettv.vval.v_partial->pt_func;
2896 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002897 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002898 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002899
Bram Moolenaara05e5242020-09-19 18:19:19 +02002900 // The function will have one line: "return {expr}". Compile it into
2901 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002902 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903
2904 // compile the arguments
2905 *arg = skipwhite(*arg + 1);
2906 if (compile_arguments(arg, cctx, &argcount) == OK)
2907 // call the compiled function
2908 ret = generate_CALL(cctx, ufunc, argcount);
2909
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002910 if (ret == FAIL)
2911 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 return ret;
2913}
2914
2915/*
2916 * parse a dict: {'key': val} or #{key: val}
2917 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002918 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 */
2920 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002921compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922{
2923 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002924 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 int count = 0;
2926 dict_T *d = dict_alloc();
2927 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002928 char_u *whitep = *arg;
2929 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002930 int is_const;
2931 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
2933 if (d == NULL)
2934 return FAIL;
2935 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002936 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
2938 char_u *key = NULL;
2939
Bram Moolenaar23c55272020-06-21 16:58:13 +02002940 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002941 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002942 *arg = NULL;
2943 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002944 }
2945
2946 if (**arg == '}')
2947 break;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 if (literal)
2950 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002951 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
Bram Moolenaar2c330432020-04-13 14:41:35 +02002953 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002955 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 return FAIL;
2957 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002958 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 if (generate_PUSHS(cctx, key) == FAIL)
2960 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002961 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
2963 else
2964 {
2965 isn_T *isn;
2966
Bram Moolenaara5565e42020-05-09 15:44:01 +02002967 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2970 if (isn->isn_type == ISN_PUSHS)
2971 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002972 else
2973 {
2974 type_T *keytype = ((type_T **)stack->ga_data)
2975 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002976 if (need_type(keytype, &t_string, -1, cctx,
2977 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002978 return FAIL;
2979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 }
2981
2982 // Check for duplicate keys, if using string keys.
2983 if (key != NULL)
2984 {
2985 item = dict_find(d, key, -1);
2986 if (item != NULL)
2987 {
2988 semsg(_(e_duplicate_key), key);
2989 goto failret;
2990 }
2991 item = dictitem_alloc(key);
2992 if (item != NULL)
2993 {
2994 item->di_tv.v_type = VAR_UNKNOWN;
2995 item->di_tv.v_lock = 0;
2996 if (dict_add(d, item) == FAIL)
2997 dictitem_free(item);
2998 }
2999 }
3000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 if (**arg != ':')
3002 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003003 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003004 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003005 else
3006 semsg(_(e_missing_dict_colon), *arg);
3007 return FAIL;
3008 }
3009 whitep = *arg + 1;
3010 if (!IS_WHITE_OR_NUL(*whitep))
3011 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003012 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 return FAIL;
3014 }
3015
3016 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003017 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003018 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003019 *arg = NULL;
3020 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003021 }
3022
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003023 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003025 if (!is_const)
3026 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 ++count;
3028
Bram Moolenaar2c330432020-04-13 14:41:35 +02003029 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003030 *arg = skipwhite(*arg);
3031 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003032 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003033 *arg = NULL;
3034 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 if (**arg == '}')
3037 break;
3038 if (**arg != ',')
3039 {
3040 semsg(_(e_missing_dict_comma), *arg);
3041 goto failret;
3042 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003043 if (IS_WHITE_OR_NUL(*whitep))
3044 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003045 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003046 return FAIL;
3047 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003048 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003049 if (!IS_WHITE_OR_NUL(*whitep))
3050 {
3051 semsg(_(e_white_space_required_after_str), ",");
3052 return FAIL;
3053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 *arg = skipwhite(*arg + 1);
3055 }
3056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 *arg = *arg + 1;
3058
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003059 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003060 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003061 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003062 *arg += STRLEN(*arg);
3063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003065 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return generate_NEWDICT(cctx, count);
3067
3068failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003069 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003070 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003072 *arg = (char_u *)"";
3073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 dict_unref(d);
3075 return FAIL;
3076}
3077
3078/*
3079 * Compile "&option".
3080 */
3081 static int
3082compile_get_option(char_u **arg, cctx_T *cctx)
3083{
3084 typval_T rettv;
3085 char_u *start = *arg;
3086 int ret;
3087
3088 // parse the option and get the current value to get the type.
3089 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003090 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 if (ret == OK)
3092 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003093 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 char_u *name = vim_strnsave(start, *arg - start);
3095 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3096
3097 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3098 vim_free(name);
3099 }
3100 clear_tv(&rettv);
3101
3102 return ret;
3103}
3104
3105/*
3106 * Compile "$VAR".
3107 */
3108 static int
3109compile_get_env(char_u **arg, cctx_T *cctx)
3110{
3111 char_u *start = *arg;
3112 int len;
3113 int ret;
3114 char_u *name;
3115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 ++*arg;
3117 len = get_env_len(arg);
3118 if (len == 0)
3119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003120 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 return FAIL;
3122 }
3123
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003124 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 name = vim_strnsave(start, len + 1);
3126 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3127 vim_free(name);
3128 return ret;
3129}
3130
3131/*
3132 * Compile "@r".
3133 */
3134 static int
3135compile_get_register(char_u **arg, cctx_T *cctx)
3136{
3137 int ret;
3138
3139 ++*arg;
3140 if (**arg == NUL)
3141 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003142 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 return FAIL;
3144 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003145 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
3147 emsg_invreg(**arg);
3148 return FAIL;
3149 }
3150 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3151 ++*arg;
3152 return ret;
3153}
3154
3155/*
3156 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003157 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 */
3159 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003160apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003162 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
3164 // this works from end to start
3165 while (p > start)
3166 {
3167 --p;
3168 if (*p == '-' || *p == '+')
3169 {
3170 // only '-' has an effect, for '+' we only check the type
3171#ifdef FEAT_FLOAT
3172 if (rettv->v_type == VAR_FLOAT)
3173 {
3174 if (*p == '-')
3175 rettv->vval.v_float = -rettv->vval.v_float;
3176 }
3177 else
3178#endif
3179 {
3180 varnumber_T val;
3181 int error = FALSE;
3182
3183 // tv_get_number_chk() accepts a string, but we don't want that
3184 // here
3185 if (check_not_string(rettv) == FAIL)
3186 return FAIL;
3187 val = tv_get_number_chk(rettv, &error);
3188 clear_tv(rettv);
3189 if (error)
3190 return FAIL;
3191 if (*p == '-')
3192 val = -val;
3193 rettv->v_type = VAR_NUMBER;
3194 rettv->vval.v_number = val;
3195 }
3196 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003197 else if (numeric_only)
3198 {
3199 ++p;
3200 break;
3201 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003202 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
3204 int v = tv2bool(rettv);
3205
3206 // '!' is permissive in the type.
3207 clear_tv(rettv);
3208 rettv->v_type = VAR_BOOL;
3209 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3210 }
3211 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003212 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 return OK;
3214}
3215
3216/*
3217 * Recognize v: variables that are constants and set "rettv".
3218 */
3219 static void
3220get_vim_constant(char_u **arg, typval_T *rettv)
3221{
3222 if (STRNCMP(*arg, "v:true", 6) == 0)
3223 {
3224 rettv->v_type = VAR_BOOL;
3225 rettv->vval.v_number = VVAL_TRUE;
3226 *arg += 6;
3227 }
3228 else if (STRNCMP(*arg, "v:false", 7) == 0)
3229 {
3230 rettv->v_type = VAR_BOOL;
3231 rettv->vval.v_number = VVAL_FALSE;
3232 *arg += 7;
3233 }
3234 else if (STRNCMP(*arg, "v:null", 6) == 0)
3235 {
3236 rettv->v_type = VAR_SPECIAL;
3237 rettv->vval.v_number = VVAL_NULL;
3238 *arg += 6;
3239 }
3240 else if (STRNCMP(*arg, "v:none", 6) == 0)
3241 {
3242 rettv->v_type = VAR_SPECIAL;
3243 rettv->vval.v_number = VVAL_NONE;
3244 *arg += 6;
3245 }
3246}
3247
Bram Moolenaar696ba232020-07-29 21:20:41 +02003248 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003249get_compare_type(char_u *p, int *len, int *type_is)
3250{
3251 exptype_T type = EXPR_UNKNOWN;
3252 int i;
3253
3254 switch (p[0])
3255 {
3256 case '=': if (p[1] == '=')
3257 type = EXPR_EQUAL;
3258 else if (p[1] == '~')
3259 type = EXPR_MATCH;
3260 break;
3261 case '!': if (p[1] == '=')
3262 type = EXPR_NEQUAL;
3263 else if (p[1] == '~')
3264 type = EXPR_NOMATCH;
3265 break;
3266 case '>': if (p[1] != '=')
3267 {
3268 type = EXPR_GREATER;
3269 *len = 1;
3270 }
3271 else
3272 type = EXPR_GEQUAL;
3273 break;
3274 case '<': if (p[1] != '=')
3275 {
3276 type = EXPR_SMALLER;
3277 *len = 1;
3278 }
3279 else
3280 type = EXPR_SEQUAL;
3281 break;
3282 case 'i': if (p[1] == 's')
3283 {
3284 // "is" and "isnot"; but not a prefix of a name
3285 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3286 *len = 5;
3287 i = p[*len];
3288 if (!isalnum(i) && i != '_')
3289 {
3290 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3291 *type_is = TRUE;
3292 }
3293 }
3294 break;
3295 }
3296 return type;
3297}
3298
3299/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003301 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 */
3303 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003304compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003306 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
3308 // this works from end to start
3309 while (p > start)
3310 {
3311 --p;
3312 if (*p == '-' || *p == '+')
3313 {
3314 int negate = *p == '-';
3315 isn_T *isn;
3316
3317 // TODO: check type
3318 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3319 {
3320 --p;
3321 if (*p == '-')
3322 negate = !negate;
3323 }
3324 // only '-' has an effect, for '+' we only check the type
3325 if (negate)
3326 isn = generate_instr(cctx, ISN_NEGATENR);
3327 else
3328 isn = generate_instr(cctx, ISN_CHECKNR);
3329 if (isn == NULL)
3330 return FAIL;
3331 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003332 else if (numeric_only)
3333 {
3334 ++p;
3335 break;
3336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 else
3338 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003339 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003341 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003343 if (p[-1] == '!')
3344 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 }
3347 if (generate_2BOOL(cctx, invert) == FAIL)
3348 return FAIL;
3349 }
3350 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003351 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 return OK;
3353}
3354
3355/*
3356 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003357 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 */
3359 static int
3360compile_subscript(
3361 char_u **arg,
3362 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003363 char_u *start_leader,
3364 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003365 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003367 char_u *name_start = *end_leader;
3368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 for (;;)
3370 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003371 char_u *p = skipwhite(*arg);
3372
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003373 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003374 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003375 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003376
3377 // If a following line starts with "->{" or "->X" advance to that
3378 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003379 // Also if a following line starts with ".x".
3380 if (next != NULL &&
3381 ((next[0] == '-' && next[1] == '>'
3382 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003383 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003384 {
3385 next = next_line_from_context(cctx, TRUE);
3386 if (next == NULL)
3387 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003388 *arg = next;
3389 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003390 }
3391 }
3392
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003393 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3394 // not a function call.
3395 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003397 garray_T *stack = &cctx->ctx_type_stack;
3398 type_T *type;
3399 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003401 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003402 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003403 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003406 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3407
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003408 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3410 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003411 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 return FAIL;
3413 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003414 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003416 char_u *pstart = p;
3417
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003418 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003419 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003420 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 // something->method()
3423 // Apply the '!', '-' and '+' first:
3424 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003425 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003428 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003429 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003430 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (**arg == '{')
3432 {
3433 // lambda call: list->{lambda}
3434 if (compile_lambda_call(arg, cctx) == FAIL)
3435 return FAIL;
3436 }
3437 else
3438 {
3439 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003440 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003441 if (!eval_isnamec1(*p))
3442 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003443 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003444 return FAIL;
3445 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003446 if (ASCII_ISALPHA(*p) && p[1] == ':')
3447 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003448 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 ;
3450 if (*p != '(')
3451 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003452 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 return FAIL;
3454 }
3455 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003456 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 return FAIL;
3458 }
3459 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003460 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003462 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003463 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003464 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003465 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003466 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003467
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003468 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003469 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003470 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003471 // TODO: blob index
3472 // TODO: more arguments
3473 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003474 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003475 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003476 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003477
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003478 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003479 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003480 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003481 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003482 if (**arg == ':')
3483 // missing first index is equal to zero
3484 generate_PUSHNR(cctx, 0);
3485 else
3486 {
3487 if (compile_expr0(arg, cctx) == FAIL)
3488 return FAIL;
3489 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3490 return FAIL;
3491 *arg = skipwhite(*arg);
3492 }
3493 if (**arg == ':')
3494 {
3495 *arg = skipwhite(*arg + 1);
3496 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3497 return FAIL;
3498 if (**arg == ']')
3499 // missing second index is equal to end of string
3500 generate_PUSHNR(cctx, -1);
3501 else
3502 {
3503 if (compile_expr0(arg, cctx) == FAIL)
3504 return FAIL;
3505 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3506 return FAIL;
3507 *arg = skipwhite(*arg);
3508 }
3509 is_slice = TRUE;
3510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511
3512 if (**arg != ']')
3513 {
3514 emsg(_(e_missbrac));
3515 return FAIL;
3516 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003517 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003519 // We can index a list and a dict. If we don't know the type
3520 // we can use the index value type.
3521 // TODO: If we don't know use an instruction to figure it out at
3522 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003523 typep = ((type_T **)stack->ga_data) + stack->ga_len
3524 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003525 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003526 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3527 // If the index is a string, the variable must be a Dict.
3528 if (*typep == &t_any && valtype == &t_string)
3529 vtype = VAR_DICT;
3530 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003531 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003532 if (need_type(valtype, &t_number, -1, cctx,
3533 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003534 return FAIL;
3535 if (is_slice)
3536 {
3537 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003538 if (need_type(valtype, &t_number, -2, cctx,
3539 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003540 return FAIL;
3541 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003542 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003543
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003544 if (vtype == VAR_DICT)
3545 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003546 if (is_slice)
3547 {
3548 emsg(_(e_cannot_slice_dictionary));
3549 return FAIL;
3550 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003551 if ((*typep)->tt_type == VAR_DICT)
3552 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003553 else
3554 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003555 if (need_type(*typep, &t_dict_any, -2, cctx,
3556 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003557 return FAIL;
3558 *typep = &t_any;
3559 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003560 if (may_generate_2STRING(-1, cctx) == FAIL)
3561 return FAIL;
3562 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3563 return FAIL;
3564 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003565 else if (vtype == VAR_STRING)
3566 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003567 *typep = &t_string;
3568 if ((is_slice
3569 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3570 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003571 return FAIL;
3572 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003573 else if (vtype == VAR_BLOB)
3574 {
3575 emsg("Sorry, blob index and slice not implemented yet");
3576 return FAIL;
3577 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003578 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003579 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003580 if (is_slice)
3581 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003582 if (generate_instr_drop(cctx,
3583 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3584 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003585 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003586 }
Bram Moolenaared591872020-08-15 22:14:53 +02003587 else
3588 {
3589 if ((*typep)->tt_type == VAR_LIST)
3590 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003591 if (generate_instr_drop(cctx,
3592 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3593 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003594 return FAIL;
3595 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003596 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003597 else
3598 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003599 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003600 return FAIL;
3601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003603 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003605 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003606 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003607 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003608 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003609
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003610 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003611 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003612 {
3613 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003614 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003615 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003616 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003617 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 while (eval_isnamec(*p))
3619 MB_PTR_ADV(p);
3620 if (p == *arg)
3621 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003622 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 return FAIL;
3624 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003625 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 return FAIL;
3627 *arg = p;
3628 }
3629 else
3630 break;
3631 }
3632
3633 // TODO - see handle_subscript():
3634 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3635 // Don't do this when "Func" is already a partial that was bound
3636 // explicitly (pt_auto is FALSE).
3637
3638 return OK;
3639}
3640
3641/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3643 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003645 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003646 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003647 *
3648 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 */
3650
3651/*
3652 * number number constant
3653 * 0zFFFFFFFF Blob constant
3654 * "string" string constant
3655 * 'string' literal string constant
3656 * &option-name option value
3657 * @r register contents
3658 * identifier variable value
3659 * function() function call
3660 * $VAR environment variable
3661 * (expression) nested expression
3662 * [expr, expr] List
3663 * {key: val, key: val} Dictionary
3664 * #{key: val, key: val} Dictionary with literal keys
3665 *
3666 * Also handle:
3667 * ! in front logical NOT
3668 * - in front unary minus
3669 * + in front unary plus (ignored)
3670 * trailing (arg) funcref/partial call
3671 * trailing [] subscript in String or List
3672 * trailing .name entry in Dictionary
3673 * trailing ->name() method call
3674 */
3675 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003676compile_expr7(
3677 char_u **arg,
3678 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003679 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 char_u *start_leader, *end_leader;
3682 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003683 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003684 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003686 ppconst->pp_is_const = FALSE;
3687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 /*
3689 * Skip '!', '-' and '+' characters. They are handled later.
3690 */
3691 start_leader = *arg;
3692 while (**arg == '!' || **arg == '-' || **arg == '+')
3693 *arg = skipwhite(*arg + 1);
3694 end_leader = *arg;
3695
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003696 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 switch (**arg)
3698 {
3699 /*
3700 * Number constant.
3701 */
3702 case '0': // also for blob starting with 0z
3703 case '1':
3704 case '2':
3705 case '3':
3706 case '4':
3707 case '5':
3708 case '6':
3709 case '7':
3710 case '8':
3711 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003712 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003714 // Apply "-" and "+" just before the number now, right to
3715 // left. Matters especially when "->" follows. Stops at
3716 // '!'.
3717 if (apply_leader(rettv, TRUE,
3718 start_leader, &end_leader) == FAIL)
3719 {
3720 clear_tv(rettv);
3721 return FAIL;
3722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 break;
3724
3725 /*
3726 * String constant: "string".
3727 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003728 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 return FAIL;
3730 break;
3731
3732 /*
3733 * Literal string constant: 'str''ing'.
3734 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003735 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 return FAIL;
3737 break;
3738
3739 /*
3740 * Constant Vim variable.
3741 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003742 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 ret = NOTDONE;
3744 break;
3745
3746 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003747 * "true" constant
3748 */
3749 case 't': if (STRNCMP(*arg, "true", 4) == 0
3750 && !eval_isnamec((*arg)[4]))
3751 {
3752 *arg += 4;
3753 rettv->v_type = VAR_BOOL;
3754 rettv->vval.v_number = VVAL_TRUE;
3755 }
3756 else
3757 ret = NOTDONE;
3758 break;
3759
3760 /*
3761 * "false" constant
3762 */
3763 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3764 && !eval_isnamec((*arg)[5]))
3765 {
3766 *arg += 5;
3767 rettv->v_type = VAR_BOOL;
3768 rettv->vval.v_number = VVAL_FALSE;
3769 }
3770 else
3771 ret = NOTDONE;
3772 break;
3773
3774 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 * List: [expr, expr]
3776 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003777 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 break;
3779
3780 /*
3781 * Dictionary: #{key: val, key: val}
3782 */
3783 case '#': if ((*arg)[1] == '{')
3784 {
3785 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003786 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 }
3788 else
3789 ret = NOTDONE;
3790 break;
3791
3792 /*
3793 * Lambda: {arg, arg -> expr}
3794 * Dictionary: {'key': val, 'key': val}
3795 */
3796 case '{': {
3797 char_u *start = skipwhite(*arg + 1);
3798
3799 // Find out what comes after the arguments.
3800 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003801 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 if (ret != FAIL && *start == '>')
3803 ret = compile_lambda(arg, cctx);
3804 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003805 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 }
3807 break;
3808
3809 /*
3810 * Option value: &name
3811 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003812 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3813 return FAIL;
3814 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 break;
3816
3817 /*
3818 * Environment variable: $VAR.
3819 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003820 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3821 return FAIL;
3822 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 break;
3824
3825 /*
3826 * Register contents: @r.
3827 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003828 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3829 return FAIL;
3830 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 break;
3832 /*
3833 * nested expression: (expression).
3834 */
3835 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003836
3837 // recursive!
3838 if (ppconst->pp_used <= PPSIZE - 10)
3839 {
3840 ret = compile_expr1(arg, cctx, ppconst);
3841 }
3842 else
3843 {
3844 // Not enough space in ppconst, flush constants.
3845 if (generate_ppconst(cctx, ppconst) == FAIL)
3846 return FAIL;
3847 ret = compile_expr0(arg, cctx);
3848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 *arg = skipwhite(*arg);
3850 if (**arg == ')')
3851 ++*arg;
3852 else if (ret == OK)
3853 {
3854 emsg(_(e_missing_close));
3855 ret = FAIL;
3856 }
3857 break;
3858
3859 default: ret = NOTDONE;
3860 break;
3861 }
3862 if (ret == FAIL)
3863 return FAIL;
3864
Bram Moolenaar1c747212020-05-09 18:28:34 +02003865 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003867 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003868 clear_tv(rettv);
3869 else
3870 // A constant expression can possibly be handled compile time,
3871 // return the value instead of generating code.
3872 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874 else if (ret == NOTDONE)
3875 {
3876 char_u *p;
3877 int r;
3878
3879 if (!eval_isnamec1(**arg))
3880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003881 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 return FAIL;
3883 }
3884
3885 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003886 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003888 {
3889 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003892 {
3893 if (generate_ppconst(cctx, ppconst) == FAIL)
3894 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003895 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 if (r == FAIL)
3898 return FAIL;
3899 }
3900
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003901 // Handle following "[]", ".member", etc.
3902 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003903 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003904 ppconst) == FAIL)
3905 return FAIL;
3906 if (ppconst->pp_used > 0)
3907 {
3908 // apply the '!', '-' and '+' before the constant
3909 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003910 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003911 return FAIL;
3912 return OK;
3913 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003914 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003916 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917}
3918
3919/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003920 * Give the "white on both sides" error, taking the operator from "p[len]".
3921 */
3922 void
3923error_white_both(char_u *op, int len)
3924{
3925 char_u buf[10];
3926
3927 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003928 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003929}
3930
3931/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003932 * <type>expr7: runtime type check / conversion
3933 */
3934 static int
3935compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3936{
3937 type_T *want_type = NULL;
3938
3939 // Recognize <type>
3940 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3941 {
3942 int called_emsg_before = called_emsg;
3943
3944 ++*arg;
3945 want_type = parse_type(arg, cctx->ctx_type_list);
3946 if (called_emsg != called_emsg_before)
3947 return FAIL;
3948
3949 if (**arg != '>')
3950 {
3951 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003952 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003953 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003954 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003955 return FAIL;
3956 }
3957 ++*arg;
3958 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3959 return FAIL;
3960 }
3961
3962 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3963 return FAIL;
3964
3965 if (want_type != NULL)
3966 {
3967 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003968 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003969
Bram Moolenaard1103582020-08-14 22:44:25 +02003970 generate_ppconst(cctx, ppconst);
3971 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003972 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003973 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003974 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003975 return FAIL;
3976 }
3977 }
3978
3979 return OK;
3980}
3981
3982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 * * number multiplication
3984 * / number division
3985 * % number modulo
3986 */
3987 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989{
3990 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003991 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003992 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003995 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 return FAIL;
3997
3998 /*
3999 * Repeat computing, until no "*", "/" or "%" is following.
4000 */
4001 for (;;)
4002 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004003 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 if (*op != '*' && *op != '/' && *op != '%')
4005 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004006 if (next != NULL)
4007 {
4008 *arg = next_line_from_context(cctx, TRUE);
4009 op = skipwhite(*arg);
4010 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004011
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004012 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004014 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004018 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004019 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004021 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004022 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004024
4025 if (ppconst->pp_used == ppconst_used + 2
4026 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4027 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004028 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004029 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4030 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004031 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004033 // both are numbers: compute the result
4034 switch (*op)
4035 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004036 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004037 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004038 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004039 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004040 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004041 break;
4042 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004043 tv1->vval.v_number = res;
4044 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004045 }
4046 else
4047 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004048 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004049 generate_two_op(cctx, op);
4050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 }
4052
4053 return OK;
4054}
4055
4056/*
4057 * + number addition
4058 * - number subtraction
4059 * .. string concatenation
4060 */
4061 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063{
4064 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004065 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068
4069 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004070 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 return FAIL;
4072
4073 /*
4074 * Repeat computing, until no "+", "-" or ".." is following.
4075 */
4076 for (;;)
4077 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004078 op = may_peek_next_line(cctx, *arg, &next);
4079 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 break;
4081 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004082 if (next != NULL)
4083 {
4084 *arg = next_line_from_context(cctx, TRUE);
4085 op = skipwhite(*arg);
4086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004088 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004090 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004091 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
4093
4094 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004095 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 return FAIL;
4101
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004104 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4105 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4106 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4107 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4110 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004111
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004112 // concat/subtract/add constant numbers
4113 if (*op == '+')
4114 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4115 else if (*op == '-')
4116 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4117 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004118 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004119 // concatenate constant strings
4120 char_u *s1 = tv1->vval.v_string;
4121 char_u *s2 = tv2->vval.v_string;
4122 size_t len1 = STRLEN(s1);
4123
4124 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4125 if (tv1->vval.v_string == NULL)
4126 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004128 return FAIL;
4129 }
4130 mch_memmove(tv1->vval.v_string, s1, len1);
4131 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004132 vim_free(s1);
4133 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 }
4137 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140 if (*op == '.')
4141 {
4142 if (may_generate_2STRING(-2, cctx) == FAIL
4143 || may_generate_2STRING(-1, cctx) == FAIL)
4144 return FAIL;
4145 generate_instr_drop(cctx, ISN_CONCAT, 1);
4146 }
4147 else
4148 generate_two_op(cctx, op);
4149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
4151
4152 return OK;
4153}
4154
4155/*
4156 * expr5a == expr5b
4157 * expr5a =~ expr5b
4158 * expr5a != expr5b
4159 * expr5a !~ expr5b
4160 * expr5a > expr5b
4161 * expr5a >= expr5b
4162 * expr5a < expr5b
4163 * expr5a <= expr5b
4164 * expr5a is expr5b
4165 * expr5a isnot expr5b
4166 *
4167 * Produces instructions:
4168 * EVAL expr5a Push result of "expr5a"
4169 * EVAL expr5b Push result of "expr5b"
4170 * COMPARE one of the compare instructions
4171 */
4172 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174{
4175 exptype_T type = EXPR_UNKNOWN;
4176 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004177 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
4182 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 return FAIL;
4185
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004186 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004187 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
4189 /*
4190 * If there is a comparative operator, use it.
4191 */
4192 if (type != EXPR_UNKNOWN)
4193 {
4194 int ic = FALSE; // Default: do not ignore case
4195
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004196 if (next != NULL)
4197 {
4198 *arg = next_line_from_context(cctx, TRUE);
4199 p = skipwhite(*arg);
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 if (type_is && (p[len] == '?' || p[len] == '#'))
4202 {
4203 semsg(_(e_invexpr2), *arg);
4204 return FAIL;
4205 }
4206 // extra question mark appended: ignore case
4207 if (p[len] == '?')
4208 {
4209 ic = TRUE;
4210 ++len;
4211 }
4212 // extra '#' appended: match case (ignored)
4213 else if (p[len] == '#')
4214 ++len;
4215 // nothing appended: match case
4216
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004217 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004219 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004220 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 }
4222
4223 // get the second variable
4224 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004225 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004226 return FAIL;
4227
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 return FAIL;
4230
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 if (ppconst->pp_used == ppconst_used + 2)
4232 {
4233 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4234 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4235 int ret;
4236
4237 // Both sides are a constant, compute the result now.
4238 // First check for a valid combination of types, this is more
4239 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004240 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004241 ret = FAIL;
4242 else
4243 {
4244 ret = typval_compare(tv1, tv2, type, ic);
4245 tv1->v_type = VAR_BOOL;
4246 tv1->vval.v_number = tv1->vval.v_number
4247 ? VVAL_TRUE : VVAL_FALSE;
4248 clear_tv(tv2);
4249 --ppconst->pp_used;
4250 }
4251 return ret;
4252 }
4253
4254 generate_ppconst(cctx, ppconst);
4255 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 }
4257
4258 return OK;
4259}
4260
Bram Moolenaar7f141552020-05-09 17:35:53 +02004261static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263/*
4264 * Compile || or &&.
4265 */
4266 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267compile_and_or(
4268 char_u **arg,
4269 cctx_T *cctx,
4270 char *op,
4271 ppconst_T *ppconst,
4272 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004274 char_u *next;
4275 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 int opchar = *op;
4277
4278 if (p[0] == opchar && p[1] == opchar)
4279 {
4280 garray_T *instr = &cctx->ctx_instr;
4281 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004282 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004283 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284
4285 /*
4286 * Repeat until there is no following "||" or "&&"
4287 */
4288 ga_init2(&end_ga, sizeof(int), 10);
4289 while (p[0] == opchar && p[1] == opchar)
4290 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004291 if (next != NULL)
4292 {
4293 *arg = next_line_from_context(cctx, TRUE);
4294 p = skipwhite(*arg);
4295 }
4296
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004297 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4298 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004299 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004300 return FAIL;
4301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004303 // TODO: use ppconst if the value is a constant and check
4304 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004305 generate_ppconst(cctx, ppconst);
4306
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004307 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4308 all_bool_values = FALSE;
4309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 if (ga_grow(&end_ga, 1) == FAIL)
4311 {
4312 ga_clear(&end_ga);
4313 return FAIL;
4314 }
4315 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4316 ++end_ga.ga_len;
4317 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004318 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319
4320 // eval the next expression
4321 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004322 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 return FAIL;
4324
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4326 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
4328 ga_clear(&end_ga);
4329 return FAIL;
4330 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004331
4332 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
4336 // Fill in the end label in all jumps.
4337 while (end_ga.ga_len > 0)
4338 {
4339 isn_T *isn;
4340
4341 --end_ga.ga_len;
4342 isn = ((isn_T *)instr->ga_data)
4343 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4344 isn->isn_arg.jump.jump_where = instr->ga_len;
4345 }
4346 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004347
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004348 // The resulting type is converted to bool if needed.
4349 if (!all_bool_values)
4350 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
4352
4353 return OK;
4354}
4355
4356/*
4357 * expr4a && expr4a && expr4a logical AND
4358 *
4359 * Produces instructions:
4360 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004361 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004363 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004365 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 * end:
4367 */
4368 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 int ppconst_used = ppconst->pp_used;
4372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 return FAIL;
4376
4377 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379}
4380
4381/*
4382 * expr3a || expr3b || expr3c logical OR
4383 *
4384 * Produces instructions:
4385 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004386 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004388 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004390 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 * end:
4392 */
4393 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 int ppconst_used = ppconst->pp_used;
4397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 return FAIL;
4401
4402 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404}
4405
4406/*
4407 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004409 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 * JUMP_IF_FALSE alt jump if false
4411 * EVAL expr1a
4412 * JUMP_ALWAYS end
4413 * alt: EVAL expr1b
4414 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004415 *
4416 * Toplevel expression: expr2 ?? expr1
4417 * Produces instructions:
4418 * EVAL expr2 Push result of "expr2"
4419 * JUMP_AND_KEEP_IF_TRUE end jump if true
4420 * EVAL expr1
4421 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 */
4423 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004424compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425{
4426 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004427 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004428 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
Bram Moolenaar3988f642020-08-27 22:43:03 +02004430 // Ignore all kinds of errors when not producing code.
4431 if (cctx->ctx_skip == SKIP_YES)
4432 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004433 evalarg_T evalarg;
4434
4435 CLEAR_FIELD(evalarg);
4436 evalarg.eval_cctx = cctx;
4437 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004438 return OK;
4439 }
4440
Bram Moolenaar61a89812020-05-07 16:58:17 +02004441 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004442 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 return FAIL;
4444
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004445 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 if (*p == '?')
4447 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004448 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 garray_T *instr = &cctx->ctx_instr;
4450 garray_T *stack = &cctx->ctx_type_stack;
4451 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004452 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004454 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 int has_const_expr = FALSE;
4456 int const_value = FALSE;
4457 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004459 if (next != NULL)
4460 {
4461 *arg = next_line_from_context(cctx, TRUE);
4462 p = skipwhite(*arg);
4463 }
4464
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004465 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004466 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004467 semsg(_(e_white_space_required_before_and_after_str),
4468 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004469 return FAIL;
4470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaara5565e42020-05-09 15:44:01 +02004472 if (ppconst->pp_used == ppconst_used + 1)
4473 {
4474 // the condition is a constant, we know whether the ? or the :
4475 // expression is to be evaluated.
4476 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004477 if (op_falsy)
4478 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4479 else
4480 {
4481 int error = FALSE;
4482
4483 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4484 &error);
4485 if (error)
4486 return FAIL;
4487 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004488 cctx->ctx_skip = save_skip == SKIP_YES ||
4489 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4490
4491 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4492 // "left ?? right" and "left" is truthy: produce "left"
4493 generate_ppconst(cctx, ppconst);
4494 else
4495 {
4496 clear_tv(&ppconst->pp_tv[ppconst_used]);
4497 --ppconst->pp_used;
4498 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 }
4500 else
4501 {
4502 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004503 if (op_falsy)
4504 end_idx = instr->ga_len;
4505 generate_JUMP(cctx, op_falsy
4506 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4507 if (op_falsy)
4508 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510
4511 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004512 *arg = skipwhite(p + 1 + op_falsy);
4513 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004514 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004516 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
Bram Moolenaara5565e42020-05-09 15:44:01 +02004518 if (!has_const_expr)
4519 {
4520 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004522 if (!op_falsy)
4523 {
4524 // remember the type and drop it
4525 --stack->ga_len;
4526 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004528 end_idx = instr->ga_len;
4529 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004531 // jump here from JUMP_IF_FALSE
4532 isn = ((isn_T *)instr->ga_data) + alt_idx;
4533 isn->isn_arg.jump.jump_where = instr->ga_len;
4534 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004537 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004539 // Check for the ":".
4540 p = may_peek_next_line(cctx, *arg, &next);
4541 if (*p != ':')
4542 {
4543 emsg(_(e_missing_colon));
4544 return FAIL;
4545 }
4546 if (next != NULL)
4547 {
4548 *arg = next_line_from_context(cctx, TRUE);
4549 p = skipwhite(*arg);
4550 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004551
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004552 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4553 {
4554 semsg(_(e_white_space_required_before_and_after_str), ":");
4555 return FAIL;
4556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004558 // evaluate the third expression
4559 if (has_const_expr)
4560 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004561 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004562 *arg = skipwhite(p + 1);
4563 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4564 return FAIL;
4565 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4566 return FAIL;
4567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 if (!has_const_expr)
4570 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004571 type_T **typep;
4572
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004576 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4577 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004579 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 isn = ((isn_T *)instr->ga_data) + end_idx;
4581 isn->isn_arg.jump.jump_where = instr->ga_len;
4582 }
4583
4584 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 }
4586 return OK;
4587}
4588
4589/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004591 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4592 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593 */
4594 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004595compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004596{
4597 ppconst_T ppconst;
4598
4599 CLEAR_FIELD(ppconst);
4600 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4601 {
4602 clear_ppconst(&ppconst);
4603 return FAIL;
4604 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004605 if (is_const != NULL)
4606 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 if (generate_ppconst(cctx, &ppconst) == FAIL)
4608 return FAIL;
4609 return OK;
4610}
4611
4612/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004613 * Toplevel expression.
4614 */
4615 static int
4616compile_expr0(char_u **arg, cctx_T *cctx)
4617{
4618 return compile_expr0_ext(arg, cctx, NULL);
4619}
4620
4621/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 * compile "return [expr]"
4623 */
4624 static char_u *
4625compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4626{
4627 char_u *p = arg;
4628 garray_T *stack = &cctx->ctx_type_stack;
4629 type_T *stack_type;
4630
4631 if (*p != NUL && *p != '|' && *p != '\n')
4632 {
4633 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 return NULL;
4636
4637 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4638 if (set_return_type)
4639 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004640 else
4641 {
4642 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4643 && stack_type->tt_type != VAR_VOID
4644 && stack_type->tt_type != VAR_UNKNOWN)
4645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004646 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004647 return NULL;
4648 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004649 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004650 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004651 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654 else
4655 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004656 // "set_return_type" cannot be TRUE, only used for a lambda which
4657 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004658 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4659 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004661 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 return NULL;
4663 }
4664
4665 // No argument, return zero.
4666 generate_PUSHNR(cctx, 0);
4667 }
4668
4669 if (generate_instr(cctx, ISN_RETURN) == NULL)
4670 return NULL;
4671
4672 // "return val | endif" is possible
4673 return skipwhite(p);
4674}
4675
4676/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 * Get a line from the compilation context, compatible with exarg_T getline().
4678 * Return a pointer to the line in allocated memory.
4679 * Return NULL for end-of-file or some error.
4680 */
4681 static char_u *
4682exarg_getline(
4683 int c UNUSED,
4684 void *cookie,
4685 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004686 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687{
4688 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004689 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690
Bram Moolenaar66250c92020-08-20 15:02:42 +02004691 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004692 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004693 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004694 return NULL;
4695 ++cctx->ctx_lnum;
4696 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4697 // Comment lines result in NULL pointers, skip them.
4698 if (p != NULL)
4699 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004700 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004701}
4702
4703/*
4704 * Compile a nested :def command.
4705 */
4706 static char_u *
4707compile_nested_function(exarg_T *eap, cctx_T *cctx)
4708{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004709 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004710 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004711 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004712 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004713 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004714 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004715
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004716 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004717 {
4718 emsg(_(e_cannot_use_bang_with_nested_def));
4719 return NULL;
4720 }
4721
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004722 // Only g:Func() can use a namespace.
4723 if (name_start[1] == ':' && !is_global)
4724 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004725 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004726 return NULL;
4727 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004728 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4729 return NULL;
4730
Bram Moolenaar04b12692020-05-04 23:24:44 +02004731 eap->arg = name_end;
4732 eap->getline = exarg_getline;
4733 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004734 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004735 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004736 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004737 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004738
Bram Moolenaar822ba242020-05-24 23:00:18 +02004739 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004740 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004741 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004742 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004743 {
4744 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004745 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004746 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004747
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004748 if (is_global)
4749 {
4750 char_u *func_name = vim_strnsave(name_start + 2,
4751 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004752
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004753 if (func_name == NULL)
4754 r = FAIL;
4755 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004756 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004757 }
4758 else
4759 {
4760 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004761 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004762 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004763 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004764
Bram Moolenaareef21022020-08-01 22:16:43 +02004765 if (lvar == NULL)
4766 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004767 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004768 return NULL;
4769 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004770
4771 // copy over the block scope IDs
4772 if (block_depth > 0)
4773 {
4774 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4775 if (ufunc->uf_block_ids != NULL)
4776 {
4777 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4778 sizeof(int) * block_depth);
4779 ufunc->uf_block_depth = block_depth;
4780 }
4781 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004782 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004783
Bram Moolenaar61a89812020-05-07 16:58:17 +02004784 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004785 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004786}
4787
4788/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 * Return the length of an assignment operator, or zero if there isn't one.
4790 */
4791 int
4792assignment_len(char_u *p, int *heredoc)
4793{
4794 if (*p == '=')
4795 {
4796 if (p[1] == '<' && p[2] == '<')
4797 {
4798 *heredoc = TRUE;
4799 return 3;
4800 }
4801 return 1;
4802 }
4803 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4804 return 2;
4805 if (STRNCMP(p, "..=", 3) == 0)
4806 return 3;
4807 return 0;
4808}
4809
4810// words that cannot be used as a variable
4811static char *reserved[] = {
4812 "true",
4813 "false",
4814 NULL
4815};
4816
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004817typedef enum {
4818 dest_local,
4819 dest_option,
4820 dest_env,
4821 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004822 dest_buffer,
4823 dest_window,
4824 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004825 dest_vimvar,
4826 dest_script,
4827 dest_reg,
4828} assign_dest_T;
4829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004831 * Generate the load instruction for "name".
4832 */
4833 static void
4834generate_loadvar(
4835 cctx_T *cctx,
4836 assign_dest_T dest,
4837 char_u *name,
4838 lvar_T *lvar,
4839 type_T *type)
4840{
4841 switch (dest)
4842 {
4843 case dest_option:
4844 // TODO: check the option exists
4845 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4846 break;
4847 case dest_global:
4848 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4849 break;
4850 case dest_buffer:
4851 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4852 break;
4853 case dest_window:
4854 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4855 break;
4856 case dest_tab:
4857 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4858 break;
4859 case dest_script:
4860 compile_load_scriptvar(cctx,
4861 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4862 break;
4863 case dest_env:
4864 // Include $ in the name here
4865 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4866 break;
4867 case dest_reg:
4868 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4869 break;
4870 case dest_vimvar:
4871 generate_LOADV(cctx, name + 2, TRUE);
4872 break;
4873 case dest_local:
4874 if (lvar->lv_from_outer)
4875 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4876 NULL, type);
4877 else
4878 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4879 break;
4880 }
4881}
4882
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004883 void
4884vim9_declare_error(char_u *name)
4885{
4886 char *scope = "";
4887
4888 switch (*name)
4889 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004890 case 'g': scope = _("global"); break;
4891 case 'b': scope = _("buffer"); break;
4892 case 'w': scope = _("window"); break;
4893 case 't': scope = _("tab"); break;
4894 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004895 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004896 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004897 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004898 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004899 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004900 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004901 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004902 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004903 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004904}
4905
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004906/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004908 * "let name"
4909 * "var name = expr"
4910 * "final name = expr"
4911 * "const name = expr"
4912 * "name = expr"
4913 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 * Return NULL for an error.
4915 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 */
4917 static char_u *
4918compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4919{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004922 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 char_u *ret = NULL;
4924 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004925 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004926 int scriptvar_sid = 0;
4927 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004930 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 int oplen = 0;
4933 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004934 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004935 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004936 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004938 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4939 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 // Skip over the "var" or "[var, var]" to get to any "=".
4942 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4943 if (p == NULL)
4944 return *arg == '[' ? arg : NULL;
4945
4946 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004948 // TODO: should we allow this, and figure out type inference from list
4949 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004950 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return NULL;
4952 }
4953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 sp = p;
4955 p = skipwhite(p);
4956 op = p;
4957 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004958
4959 if (var_count > 0 && oplen == 0)
4960 // can be something like "[1, 2]->func()"
4961 return arg;
4962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4964 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004965 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004967 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 if (heredoc)
4970 {
4971 list_T *l;
4972 listitem_T *li;
4973
4974 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004975 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004977 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004978 if (l == NULL)
4979 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980
Bram Moolenaar078269b2020-09-21 20:35:55 +02004981 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004983 // Push each line and the create the list.
4984 FOR_ALL_LIST_ITEMS(l, li)
4985 {
4986 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4987 li->li_tv.vval.v_string = NULL;
4988 }
4989 generate_NEWLIST(cctx, l->lv_len);
4990 type = &t_list_string;
4991 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 list_free(l);
4994 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004999 // for "[var, var] = expr" evaluate the expression here, loop over the
5000 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005001
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 p = skipwhite(op + oplen);
5003 if (compile_expr0(&p, cctx) == FAIL)
5004 return NULL;
5005 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005007 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005009 type_T *stacktype;
5010
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005011 stacktype = stack->ga_len == 0 ? &t_void
5012 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005015 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 goto theend;
5017 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005018 if (need_type(stacktype, &t_list_any, -1, cctx,
5019 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005021 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005022 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5023 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 }
5025 }
5026
5027 /*
5028 * Loop over variables in "[var, var] = expr".
5029 * For "var = expr" and "let var: type" this is done only once.
5030 */
5031 if (var_count > 0)
5032 var_start = skipwhite(arg + 1); // skip over the "["
5033 else
5034 var_start = arg;
5035 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5036 {
5037 char_u *var_end = skip_var_one(var_start, FALSE);
5038 size_t varlen;
5039 int new_local = FALSE;
5040 int opt_type;
5041 int opt_flags = 0;
5042 assign_dest_T dest = dest_local;
5043 int vimvaridx = -1;
5044 lvar_T *lvar = NULL;
5045 lvar_T arg_lvar;
5046 int has_type = FALSE;
5047 int has_index = FALSE;
5048 int instr_count = -1;
5049
Bram Moolenaar65821722020-08-02 18:58:54 +02005050 if (*var_start == '@')
5051 p = var_start + 2;
5052 else
5053 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005054 // skip over the leading "&", "&l:", "&g:" and "$"
5055 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005056 p = to_name_end(p, TRUE);
5057 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005058
5059 // "a: type" is declaring variable "a" with a type, not "a:".
5060 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5061 --var_end;
5062 if (is_decl && p == var_start + 2 && p[-1] == ':')
5063 --p;
5064
5065 varlen = p - var_start;
5066 vim_free(name);
5067 name = vim_strnsave(var_start, varlen);
5068 if (name == NULL)
5069 return NULL;
5070 if (!heredoc)
5071 type = &t_any;
5072
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005073 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005075 int declare_error = FALSE;
5076
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 if (*var_start == '&')
5078 {
5079 int cc;
5080 long numval;
5081
5082 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005083 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 emsg(_(e_const_option));
5086 goto theend;
5087 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005088 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 p = var_start;
5090 p = find_option_end(&p, &opt_flags);
5091 if (p == NULL)
5092 {
5093 // cannot happen?
5094 emsg(_(e_letunexp));
5095 goto theend;
5096 }
5097 cc = *p;
5098 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005099 opt_type = get_option_value(skip_option_env_lead(var_start),
5100 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 *p = cc;
5102 if (opt_type == -3)
5103 {
5104 semsg(_(e_unknown_option), var_start);
5105 goto theend;
5106 }
5107 if (opt_type == -2 || opt_type == 0)
5108 type = &t_string;
5109 else
5110 type = &t_number; // both number and boolean option
5111 }
5112 else if (*var_start == '$')
5113 {
5114 dest = dest_env;
5115 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005116 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 }
5118 else if (*var_start == '@')
5119 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005120 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 {
5122 emsg_invreg(var_start[1]);
5123 goto theend;
5124 }
5125 dest = dest_reg;
5126 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005127 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005129 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 {
5131 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005132 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005134 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135 {
5136 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005137 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005139 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 {
5141 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005142 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005144 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 {
5146 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005147 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005149 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 {
5151 typval_T *vtv;
5152 int di_flags;
5153
5154 vimvaridx = find_vim_var(name + 2, &di_flags);
5155 if (vimvaridx < 0)
5156 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005157 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158 goto theend;
5159 }
5160 // We use the current value of "sandbox" here, is that OK?
5161 if (var_check_ro(di_flags, name, FALSE))
5162 goto theend;
5163 dest = dest_vimvar;
5164 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005165 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005166 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 }
5168 else
5169 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005170 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171
5172 for (idx = 0; reserved[idx] != NULL; ++idx)
5173 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005174 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005175 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005176 goto theend;
5177 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005178
5179 lvar = lookup_local(var_start, varlen, cctx);
5180 if (lvar == NULL)
5181 {
5182 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005183 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5185 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005186 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 if (is_decl)
5188 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005189 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005190 goto theend;
5191 }
5192 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005193 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005194 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 if (lvar != NULL)
5196 {
5197 if (is_decl)
5198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005199 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 goto theend;
5201 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005202 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005203 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005205 int script_namespace = varlen > 1
5206 && STRNCMP(var_start, "s:", 2) == 0;
5207 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005208 ? script_var_exists(var_start + 2, varlen - 2,
5209 FALSE, cctx)
5210 : script_var_exists(var_start, varlen,
5211 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005212 imported_T *import =
5213 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005214
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005215 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005217 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5218
5219 if (is_decl)
5220 {
5221 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005222 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005223 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005224 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005225 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005226 name);
5227 goto theend;
5228 }
5229 else if (cctx->ctx_ufunc->uf_script_ctx_version
5230 == SCRIPT_VERSION_VIM9
5231 && script_namespace
5232 && !script_var && import == NULL)
5233 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005234 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005235 goto theend;
5236 }
5237
5238 dest = dest_script;
5239
5240 // existing script-local variables should have a type
5241 scriptvar_sid = current_sctx.sc_sid;
5242 if (import != NULL)
5243 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005244 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005245 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005246 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005247 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005248 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005249 {
5250 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5251 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005252 ((svar_T *)si->sn_var_vals.ga_data)
5253 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005254 type = sv->sv_type;
5255 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005256 }
5257 }
5258 else if (name[1] == ':' && name[2] != NUL)
5259 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005260 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 goto theend;
5262 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005263 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005264 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005265 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005266 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005267 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005268 else if (check_defined(var_start, varlen, cctx) == FAIL)
5269 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005270 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005272
5273 if (declare_error)
5274 {
5275 vim9_declare_error(name);
5276 goto theend;
5277 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 }
5279
5280 // handle "a:name" as a name, not index "name" on "a"
5281 if (varlen > 1 || var_start[varlen] != ':')
5282 p = var_end;
5283
5284 if (dest != dest_option)
5285 {
5286 if (is_decl && *p == ':')
5287 {
5288 // parse optional type: "let var: type = expr"
5289 if (!VIM_ISWHITE(p[1]))
5290 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005291 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 goto theend;
5293 }
5294 p = skipwhite(p + 1);
5295 type = parse_type(&p, cctx->ctx_type_list);
5296 has_type = TRUE;
5297 }
5298 else if (lvar != NULL)
5299 type = lvar->lv_type;
5300 }
5301
5302 if (oplen == 3 && !heredoc && dest != dest_global
5303 && type->tt_type != VAR_STRING
5304 && type->tt_type != VAR_ANY)
5305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005306 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 goto theend;
5308 }
5309
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005310 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005311 {
5312 if (oplen > 1 && !heredoc)
5313 {
5314 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005315 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 goto theend;
5317 }
5318
5319 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005320 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5321 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005322 goto theend;
5323 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005324 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 if (lvar == NULL)
5326 goto theend;
5327 new_local = TRUE;
5328 }
5329
5330 member_type = type;
5331 if (var_end > var_start + varlen)
5332 {
5333 // Something follows after the variable: "var[idx]".
5334 if (is_decl)
5335 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005336 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005337 goto theend;
5338 }
5339
5340 if (var_start[varlen] == '[')
5341 {
5342 has_index = TRUE;
5343 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005344 member_type = &t_any;
5345 else
5346 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 }
5348 else
5349 {
5350 semsg("Not supported yet: %s", var_start);
5351 goto theend;
5352 }
5353 }
5354 else if (lvar == &arg_lvar)
5355 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005356 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005357 goto theend;
5358 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005359 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5360 {
5361 semsg(_(e_cannot_assign_to_constant), name);
5362 goto theend;
5363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364
5365 if (!heredoc)
5366 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005367 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005369 if (oplen > 0 && var_count == 0)
5370 {
5371 // skip over the "=" and the expression
5372 p = skipwhite(op + oplen);
5373 compile_expr0(&p, cctx);
5374 }
5375 }
5376 else if (oplen > 0)
5377 {
5378 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005379 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005380
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 // For "var = expr" evaluate the expression.
5382 if (var_count == 0)
5383 {
5384 int r;
5385
5386 // for "+=", "*=", "..=" etc. first load the current value
5387 if (*op != '=')
5388 {
5389 generate_loadvar(cctx, dest, name, lvar, type);
5390
5391 if (has_index)
5392 {
5393 // TODO: get member from list or dict
5394 emsg("Index with operation not supported yet");
5395 goto theend;
5396 }
5397 }
5398
5399 // Compile the expression. Temporarily hide the new local
5400 // variable here, it is not available to this expression.
5401 if (new_local)
5402 --cctx->ctx_locals.ga_len;
5403 instr_count = instr->ga_len;
5404 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005405 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005406 if (new_local)
5407 ++cctx->ctx_locals.ga_len;
5408 if (r == FAIL)
5409 goto theend;
5410 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005411 else if (semicolon && var_idx == var_count - 1)
5412 {
5413 // For "[var; var] = expr" get the rest of the list
5414 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5415 goto theend;
5416 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005417 else
5418 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 // For "[var, var] = expr" get the "var_idx" item from the
5420 // list.
5421 if (generate_GETITEM(cctx, var_idx) == FAIL)
5422 return FAIL;
5423 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005424
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005425 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005426 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005427 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005429 if ((stacktype->tt_type == VAR_FUNC
5430 || stacktype->tt_type == VAR_PARTIAL)
5431 && var_wrong_func_name(name, TRUE))
5432 goto theend;
5433
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005434 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005435 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005436 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005437 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005438 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005439 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440 }
5441 else
5442 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005443 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005444 // for a variable that implies &t_any.
5445 if (stacktype == &t_list_empty)
5446 lvar->lv_type = &t_list_any;
5447 else if (stacktype == &t_dict_empty)
5448 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005449 else if (stacktype == &t_unknown)
5450 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005451 else
5452 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005455 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005456 {
5457 type_T *use_type = lvar->lv_type;
5458
Bram Moolenaar71abe482020-09-14 22:28:30 +02005459 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005460 if (has_index)
5461 {
5462 use_type = use_type->tt_member;
5463 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005464 // could be indexing "any"
5465 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005466 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005467 if (need_type(stacktype, use_type, -1, cctx,
5468 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005469 goto theend;
5470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005472 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005473 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005474 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005476 else if (cmdidx == CMD_final)
5477 {
5478 emsg(_(e_final_requires_a_value));
5479 goto theend;
5480 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005483 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005484 goto theend;
5485 }
5486 else if (!has_type || dest == dest_option)
5487 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005488 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005489 goto theend;
5490 }
5491 else
5492 {
5493 // variables are always initialized
5494 if (ga_grow(instr, 1) == FAIL)
5495 goto theend;
5496 switch (member_type->tt_type)
5497 {
5498 case VAR_BOOL:
5499 generate_PUSHBOOL(cctx, VVAL_FALSE);
5500 break;
5501 case VAR_FLOAT:
5502#ifdef FEAT_FLOAT
5503 generate_PUSHF(cctx, 0.0);
5504#endif
5505 break;
5506 case VAR_STRING:
5507 generate_PUSHS(cctx, NULL);
5508 break;
5509 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005510 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 break;
5512 case VAR_FUNC:
5513 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5514 break;
5515 case VAR_LIST:
5516 generate_NEWLIST(cctx, 0);
5517 break;
5518 case VAR_DICT:
5519 generate_NEWDICT(cctx, 0);
5520 break;
5521 case VAR_JOB:
5522 generate_PUSHJOB(cctx, NULL);
5523 break;
5524 case VAR_CHANNEL:
5525 generate_PUSHCHANNEL(cctx, NULL);
5526 break;
5527 case VAR_NUMBER:
5528 case VAR_UNKNOWN:
5529 case VAR_ANY:
5530 case VAR_PARTIAL:
5531 case VAR_VOID:
5532 case VAR_SPECIAL: // cannot happen
5533 generate_PUSHNR(cctx, 0);
5534 break;
5535 }
5536 }
5537 if (var_count == 0)
5538 end = p;
5539 }
5540
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005541 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005542 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005543 break;
5544
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 if (oplen > 0 && *op != '=')
5546 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005547 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 type_T *stacktype;
5549
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005550 if (*op == '.')
5551 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005552 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005553 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005554 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005555 if (
5556#ifdef FEAT_FLOAT
5557 // If variable is float operation with number is OK.
5558 !(expected == &t_float && stacktype == &t_number) &&
5559#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005560 need_type(stacktype, expected, -1, cctx,
5561 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562 goto theend;
5563
5564 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005565 {
5566 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5567 goto theend;
5568 }
5569 else if (*op == '+')
5570 {
5571 if (generate_add_instr(cctx,
5572 operator_type(member_type, stacktype),
5573 member_type, stacktype) == FAIL)
5574 goto theend;
5575 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005576 else if (generate_two_op(cctx, op) == FAIL)
5577 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005580 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005581 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005582 int r;
5583
5584 // Compile the "idx" in "var[idx]".
5585 if (new_local)
5586 --cctx->ctx_locals.ga_len;
5587 p = skipwhite(var_start + varlen + 1);
5588 r = compile_expr0(&p, cctx);
5589 if (new_local)
5590 ++cctx->ctx_locals.ga_len;
5591 if (r == FAIL)
5592 goto theend;
5593 if (*skipwhite(p) != ']')
5594 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005595 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005596 emsg(_(e_missbrac));
5597 goto theend;
5598 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005599 if (type == &t_any)
5600 {
5601 type_T *idx_type = ((type_T **)stack->ga_data)[
5602 stack->ga_len - 1];
5603 // Index on variable of unknown type: guess the type from the
5604 // index type: number is dict, otherwise dict.
5605 // TODO: should do the assignment at runtime
5606 if (idx_type->tt_type == VAR_NUMBER)
5607 type = &t_list_any;
5608 else
5609 type = &t_dict_any;
5610 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 if (type->tt_type == VAR_DICT
5612 && may_generate_2STRING(-1, cctx) == FAIL)
5613 goto theend;
5614 if (type->tt_type == VAR_LIST
5615 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005616 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005617 {
5618 emsg(_(e_number_exp));
5619 goto theend;
5620 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005621
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 // Load the dict or list. On the stack we then have:
5623 // - value
5624 // - index
5625 // - variable
5626 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005627
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005628 if (type->tt_type == VAR_LIST)
5629 {
5630 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5631 return FAIL;
5632 }
5633 else if (type->tt_type == VAR_DICT)
5634 {
5635 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5636 return FAIL;
5637 }
5638 else
5639 {
5640 emsg(_(e_listreq));
5641 goto theend;
5642 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005643 }
5644 else
5645 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005646 if (is_decl && cmdidx == CMD_const
5647 && (dest == dest_script || dest == dest_local))
5648 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005649 generate_LOCKCONST(cctx);
5650
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005651 switch (dest)
5652 {
5653 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005654 generate_STOREOPT(cctx, skip_option_env_lead(name),
5655 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005656 break;
5657 case dest_global:
5658 // include g: with the name, easier to execute that way
5659 generate_STORE(cctx, ISN_STOREG, 0, name);
5660 break;
5661 case dest_buffer:
5662 // include b: with the name, easier to execute that way
5663 generate_STORE(cctx, ISN_STOREB, 0, name);
5664 break;
5665 case dest_window:
5666 // include w: with the name, easier to execute that way
5667 generate_STORE(cctx, ISN_STOREW, 0, name);
5668 break;
5669 case dest_tab:
5670 // include t: with the name, easier to execute that way
5671 generate_STORE(cctx, ISN_STORET, 0, name);
5672 break;
5673 case dest_env:
5674 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5675 break;
5676 case dest_reg:
5677 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5678 break;
5679 case dest_vimvar:
5680 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5681 break;
5682 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005683 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005684 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 {
5686 char_u *name_s = name;
5687
5688 // Include s: in the name for store_var()
5689 if (name[1] != ':')
5690 {
5691 int len = (int)STRLEN(name) + 3;
5692
5693 name_s = alloc(len);
5694 if (name_s == NULL)
5695 name_s = name;
5696 else
5697 vim_snprintf((char *)name_s, len,
5698 "s:%s", name);
5699 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005700 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5701 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 if (name_s != name)
5703 vim_free(name_s);
5704 }
5705 else
5706 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005707 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005708 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005709 break;
5710 case dest_local:
5711 if (lvar != NULL)
5712 {
5713 isn_T *isn = ((isn_T *)instr->ga_data)
5714 + instr->ga_len - 1;
5715
5716 // optimization: turn "var = 123" from ISN_PUSHNR +
5717 // ISN_STORE into ISN_STORENR
5718 if (!lvar->lv_from_outer
5719 && instr->ga_len == instr_count + 1
5720 && isn->isn_type == ISN_PUSHNR)
5721 {
5722 varnumber_T val = isn->isn_arg.number;
5723
5724 isn->isn_type = ISN_STORENR;
5725 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5726 isn->isn_arg.storenr.stnr_val = val;
5727 if (stack->ga_len > 0)
5728 --stack->ga_len;
5729 }
5730 else if (lvar->lv_from_outer)
5731 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005732 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005733 else
5734 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5735 }
5736 break;
5737 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005739
5740 if (var_idx + 1 < var_count)
5741 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005743
5744 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005745 if (var_count > 0 && !semicolon)
5746 {
5747 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5748 goto theend;
5749 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005750
Bram Moolenaarb2097502020-07-19 17:17:02 +02005751 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
5753theend:
5754 vim_free(name);
5755 return ret;
5756}
5757
5758/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005759 * Check if "name" can be "unlet".
5760 */
5761 int
5762check_vim9_unlet(char_u *name)
5763{
5764 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5765 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005766 // "unlet s:var" is allowed in legacy script.
5767 if (*name == 's' && !script_is_vim9())
5768 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005769 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005770 return FAIL;
5771 }
5772 return OK;
5773}
5774
5775/*
5776 * Callback passed to ex_unletlock().
5777 */
5778 static int
5779compile_unlet(
5780 lval_T *lvp,
5781 char_u *name_end,
5782 exarg_T *eap,
5783 int deep UNUSED,
5784 void *coookie)
5785{
5786 cctx_T *cctx = coookie;
5787
5788 if (lvp->ll_tv == NULL)
5789 {
5790 char_u *p = lvp->ll_name;
5791 int cc = *name_end;
5792 int ret = OK;
5793
5794 // Normal name. Only supports g:, w:, t: and b: namespaces.
5795 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005796 if (*p == '$')
5797 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5798 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005799 ret = FAIL;
5800 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005801 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005802
5803 *name_end = cc;
5804 return ret;
5805 }
5806
5807 // TODO: unlet {list}[idx]
5808 // TODO: unlet {dict}[key]
5809 emsg("Sorry, :unlet not fully implemented yet");
5810 return FAIL;
5811}
5812
5813/*
5814 * compile "unlet var", "lock var" and "unlock var"
5815 * "arg" points to "var".
5816 */
5817 static char_u *
5818compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5819{
5820 char_u *p = arg;
5821
5822 if (eap->cmdidx != CMD_unlet)
5823 {
5824 emsg("Sorry, :lock and unlock not implemented yet");
5825 return NULL;
5826 }
5827
5828 if (*p == '!')
5829 {
5830 p = skipwhite(p + 1);
5831 eap->forceit = TRUE;
5832 }
5833
5834 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5835 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5836}
5837
5838/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839 * Compile an :import command.
5840 */
5841 static char_u *
5842compile_import(char_u *arg, cctx_T *cctx)
5843{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005844 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845}
5846
5847/*
5848 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5849 */
5850 static int
5851compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5852{
5853 garray_T *instr = &cctx->ctx_instr;
5854 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5855
5856 if (endlabel == NULL)
5857 return FAIL;
5858 endlabel->el_next = *el;
5859 *el = endlabel;
5860 endlabel->el_end_label = instr->ga_len;
5861
5862 generate_JUMP(cctx, when, 0);
5863 return OK;
5864}
5865
5866 static void
5867compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5868{
5869 garray_T *instr = &cctx->ctx_instr;
5870
5871 while (*el != NULL)
5872 {
5873 endlabel_T *cur = (*el);
5874 isn_T *isn;
5875
5876 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5877 isn->isn_arg.jump.jump_where = instr->ga_len;
5878 *el = cur->el_next;
5879 vim_free(cur);
5880 }
5881}
5882
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005883 static void
5884compile_free_jump_to_end(endlabel_T **el)
5885{
5886 while (*el != NULL)
5887 {
5888 endlabel_T *cur = (*el);
5889
5890 *el = cur->el_next;
5891 vim_free(cur);
5892 }
5893}
5894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895/*
5896 * Create a new scope and set up the generic items.
5897 */
5898 static scope_T *
5899new_scope(cctx_T *cctx, scopetype_T type)
5900{
5901 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5902
5903 if (scope == NULL)
5904 return NULL;
5905 scope->se_outer = cctx->ctx_scope;
5906 cctx->ctx_scope = scope;
5907 scope->se_type = type;
5908 scope->se_local_count = cctx->ctx_locals.ga_len;
5909 return scope;
5910}
5911
5912/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005913 * Free the current scope and go back to the outer scope.
5914 */
5915 static void
5916drop_scope(cctx_T *cctx)
5917{
5918 scope_T *scope = cctx->ctx_scope;
5919
5920 if (scope == NULL)
5921 {
5922 iemsg("calling drop_scope() without a scope");
5923 return;
5924 }
5925 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005926 switch (scope->se_type)
5927 {
5928 case IF_SCOPE:
5929 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5930 case FOR_SCOPE:
5931 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5932 case WHILE_SCOPE:
5933 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5934 case TRY_SCOPE:
5935 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5936 case NO_SCOPE:
5937 case BLOCK_SCOPE:
5938 break;
5939 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005940 vim_free(scope);
5941}
5942
5943/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005944 * Check that the top of the type stack has a type that can be used as a
5945 * condition. Give an error and return FAIL if not.
5946 */
5947 static int
5948bool_on_stack(cctx_T *cctx)
5949{
5950 garray_T *stack = &cctx->ctx_type_stack;
5951 type_T *type;
5952
5953 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5954 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005955 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005956 return FAIL;
5957 return OK;
5958}
5959
5960/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 * compile "if expr"
5962 *
5963 * "if expr" Produces instructions:
5964 * EVAL expr Push result of "expr"
5965 * JUMP_IF_FALSE end
5966 * ... body ...
5967 * end:
5968 *
5969 * "if expr | else" Produces instructions:
5970 * EVAL expr Push result of "expr"
5971 * JUMP_IF_FALSE else
5972 * ... body ...
5973 * JUMP_ALWAYS end
5974 * else:
5975 * ... body ...
5976 * end:
5977 *
5978 * "if expr1 | elseif expr2 | else" Produces instructions:
5979 * EVAL expr Push result of "expr"
5980 * JUMP_IF_FALSE elseif
5981 * ... body ...
5982 * JUMP_ALWAYS end
5983 * elseif:
5984 * EVAL expr Push result of "expr"
5985 * JUMP_IF_FALSE else
5986 * ... body ...
5987 * JUMP_ALWAYS end
5988 * else:
5989 * ... body ...
5990 * end:
5991 */
5992 static char_u *
5993compile_if(char_u *arg, cctx_T *cctx)
5994{
5995 char_u *p = arg;
5996 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005997 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006000 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001
Bram Moolenaara5565e42020-05-09 15:44:01 +02006002 CLEAR_FIELD(ppconst);
6003 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006004 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006005 clear_ppconst(&ppconst);
6006 return NULL;
6007 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006008 if (cctx->ctx_skip == SKIP_YES)
6009 clear_ppconst(&ppconst);
6010 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006011 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006012 int error = FALSE;
6013 int v;
6014
Bram Moolenaara5565e42020-05-09 15:44:01 +02006015 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006016 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006017 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006018 if (error)
6019 return NULL;
6020 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006021 }
6022 else
6023 {
6024 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006025 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006026 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006028 if (bool_on_stack(cctx) == FAIL)
6029 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031
6032 scope = new_scope(cctx, IF_SCOPE);
6033 if (scope == NULL)
6034 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006035 scope->se_skip_save = skip_save;
6036 // "is_had_return" will be reset if any block does not end in :return
6037 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006039 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006040 {
6041 // "where" is set when ":elseif", "else" or ":endif" is found
6042 scope->se_u.se_if.is_if_label = instr->ga_len;
6043 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6044 }
6045 else
6046 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047
6048 return p;
6049}
6050
6051 static char_u *
6052compile_elseif(char_u *arg, cctx_T *cctx)
6053{
6054 char_u *p = arg;
6055 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006056 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 isn_T *isn;
6058 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006059 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006060 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
6062 if (scope == NULL || scope->se_type != IF_SCOPE)
6063 {
6064 emsg(_(e_elseif_without_if));
6065 return NULL;
6066 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006067 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006068 if (!cctx->ctx_had_return)
6069 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006071 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006072 {
6073 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006075 return NULL;
6076 // previous "if" or "elseif" jumps here
6077 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6078 isn->isn_arg.jump.jump_where = instr->ga_len;
6079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006081 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006082 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006083 if (cctx->ctx_skip == SKIP_YES)
6084 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006085 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006086 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006087 clear_ppconst(&ppconst);
6088 return NULL;
6089 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006090 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006091 if (scope->se_skip_save == SKIP_YES)
6092 clear_ppconst(&ppconst);
6093 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006094 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006095 int error = FALSE;
6096 int v;
6097
Bram Moolenaar7f141552020-05-09 17:35:53 +02006098 // The expression results in a constant.
6099 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006100 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6101 if (error)
6102 return NULL;
6103 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006104 clear_ppconst(&ppconst);
6105 scope->se_u.se_if.is_if_label = -1;
6106 }
6107 else
6108 {
6109 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006110 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006111 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006112 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006113 if (bool_on_stack(cctx) == FAIL)
6114 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006116 // "where" is set when ":elseif", "else" or ":endif" is found
6117 scope->se_u.se_if.is_if_label = instr->ga_len;
6118 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120
6121 return p;
6122}
6123
6124 static char_u *
6125compile_else(char_u *arg, cctx_T *cctx)
6126{
6127 char_u *p = arg;
6128 garray_T *instr = &cctx->ctx_instr;
6129 isn_T *isn;
6130 scope_T *scope = cctx->ctx_scope;
6131
6132 if (scope == NULL || scope->se_type != IF_SCOPE)
6133 {
6134 emsg(_(e_else_without_if));
6135 return NULL;
6136 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006137 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006138 if (!cctx->ctx_had_return)
6139 scope->se_u.se_if.is_had_return = FALSE;
6140 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141
Bram Moolenaarefd88552020-06-18 20:50:10 +02006142 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006143 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006144 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006145 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006146 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006147 if (!cctx->ctx_had_return
6148 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6149 JUMP_ALWAYS, cctx) == FAIL)
6150 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006151 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006152
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006153 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006154 {
6155 if (scope->se_u.se_if.is_if_label >= 0)
6156 {
6157 // previous "if" or "elseif" jumps here
6158 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6159 isn->isn_arg.jump.jump_where = instr->ga_len;
6160 scope->se_u.se_if.is_if_label = -1;
6161 }
6162 }
6163
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006164 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006165 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006167
6168 return p;
6169}
6170
6171 static char_u *
6172compile_endif(char_u *arg, cctx_T *cctx)
6173{
6174 scope_T *scope = cctx->ctx_scope;
6175 ifscope_T *ifscope;
6176 garray_T *instr = &cctx->ctx_instr;
6177 isn_T *isn;
6178
6179 if (scope == NULL || scope->se_type != IF_SCOPE)
6180 {
6181 emsg(_(e_endif_without_if));
6182 return NULL;
6183 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006184 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006185 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006186 if (!cctx->ctx_had_return)
6187 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006189 if (scope->se_u.se_if.is_if_label >= 0)
6190 {
6191 // previous "if" or "elseif" jumps here
6192 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6193 isn->isn_arg.jump.jump_where = instr->ga_len;
6194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 // Fill in the "end" label in jumps at the end of the blocks.
6196 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006197 cctx->ctx_skip = scope->se_skip_save;
6198
6199 // If all the blocks end in :return and there is an :else then the
6200 // had_return flag is set.
6201 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006203 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 return arg;
6205}
6206
6207/*
6208 * compile "for var in expr"
6209 *
6210 * Produces instructions:
6211 * PUSHNR -1
6212 * STORE loop-idx Set index to -1
6213 * EVAL expr Push result of "expr"
6214 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6215 * - if beyond end, jump to "end"
6216 * - otherwise get item from list and push it
6217 * STORE var Store item in "var"
6218 * ... body ...
6219 * JUMP top Jump back to repeat
6220 * end: DROP Drop the result of "expr"
6221 *
6222 */
6223 static char_u *
6224compile_for(char_u *arg, cctx_T *cctx)
6225{
6226 char_u *p;
6227 size_t varlen;
6228 garray_T *instr = &cctx->ctx_instr;
6229 garray_T *stack = &cctx->ctx_type_stack;
6230 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006231 lvar_T *loop_lvar; // loop iteration variable
6232 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 type_T *vartype;
6234
6235 // TODO: list of variables: "for [key, value] in dict"
6236 // parse "var"
6237 for (p = arg; eval_isnamec1(*p); ++p)
6238 ;
6239 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006240 var_lvar = lookup_local(arg, varlen, cctx);
6241 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006243 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 return NULL;
6245 }
6246
6247 // consume "in"
6248 p = skipwhite(p);
6249 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6250 {
6251 emsg(_(e_missing_in));
6252 return NULL;
6253 }
6254 p = skipwhite(p + 2);
6255
6256
6257 scope = new_scope(cctx, FOR_SCOPE);
6258 if (scope == NULL)
6259 return NULL;
6260
6261 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006262 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6263 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006264 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006265 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006266 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
6270 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006271 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6272 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006273 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006274 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006275 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006279 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280
6281 // compile "expr", it remains on the stack until "endfor"
6282 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006283 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006284 {
6285 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006289 // Now that we know the type of "var", check that it is a list, now or at
6290 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006292 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006294 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 return NULL;
6296 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006297 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006298 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299
6300 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006301 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006303 generate_FOR(cctx, loop_lvar->lv_idx);
6304 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305
6306 return arg;
6307}
6308
6309/*
6310 * compile "endfor"
6311 */
6312 static char_u *
6313compile_endfor(char_u *arg, cctx_T *cctx)
6314{
6315 garray_T *instr = &cctx->ctx_instr;
6316 scope_T *scope = cctx->ctx_scope;
6317 forscope_T *forscope;
6318 isn_T *isn;
6319
6320 if (scope == NULL || scope->se_type != FOR_SCOPE)
6321 {
6322 emsg(_(e_for));
6323 return NULL;
6324 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006325 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006327 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
6329 // At end of ":for" scope jump back to the FOR instruction.
6330 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6331
6332 // Fill in the "end" label in the FOR statement so it can jump here
6333 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6334 isn->isn_arg.forloop.for_end = instr->ga_len;
6335
6336 // Fill in the "end" label any BREAK statements
6337 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6338
6339 // Below the ":for" scope drop the "expr" list from the stack.
6340 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6341 return NULL;
6342
6343 vim_free(scope);
6344
6345 return arg;
6346}
6347
6348/*
6349 * compile "while expr"
6350 *
6351 * Produces instructions:
6352 * top: EVAL expr Push result of "expr"
6353 * JUMP_IF_FALSE end jump if false
6354 * ... body ...
6355 * JUMP top Jump back to repeat
6356 * end:
6357 *
6358 */
6359 static char_u *
6360compile_while(char_u *arg, cctx_T *cctx)
6361{
6362 char_u *p = arg;
6363 garray_T *instr = &cctx->ctx_instr;
6364 scope_T *scope;
6365
6366 scope = new_scope(cctx, WHILE_SCOPE);
6367 if (scope == NULL)
6368 return NULL;
6369
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006370 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371
6372 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006373 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374 return NULL;
6375
Bram Moolenaar13106602020-10-04 16:06:05 +02006376 if (bool_on_stack(cctx) == FAIL)
6377 return FAIL;
6378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006380 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 JUMP_IF_FALSE, cctx) == FAIL)
6382 return FAIL;
6383
6384 return p;
6385}
6386
6387/*
6388 * compile "endwhile"
6389 */
6390 static char_u *
6391compile_endwhile(char_u *arg, cctx_T *cctx)
6392{
6393 scope_T *scope = cctx->ctx_scope;
6394
6395 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6396 {
6397 emsg(_(e_while));
6398 return NULL;
6399 }
6400 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006401 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402
6403 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006404 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405
6406 // Fill in the "end" label in the WHILE statement so it can jump here.
6407 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006408 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409
6410 vim_free(scope);
6411
6412 return arg;
6413}
6414
6415/*
6416 * compile "continue"
6417 */
6418 static char_u *
6419compile_continue(char_u *arg, cctx_T *cctx)
6420{
6421 scope_T *scope = cctx->ctx_scope;
6422
6423 for (;;)
6424 {
6425 if (scope == NULL)
6426 {
6427 emsg(_(e_continue));
6428 return NULL;
6429 }
6430 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6431 break;
6432 scope = scope->se_outer;
6433 }
6434
6435 // Jump back to the FOR or WHILE instruction.
6436 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006437 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6438 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 return arg;
6440}
6441
6442/*
6443 * compile "break"
6444 */
6445 static char_u *
6446compile_break(char_u *arg, cctx_T *cctx)
6447{
6448 scope_T *scope = cctx->ctx_scope;
6449 endlabel_T **el;
6450
6451 for (;;)
6452 {
6453 if (scope == NULL)
6454 {
6455 emsg(_(e_break));
6456 return NULL;
6457 }
6458 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6459 break;
6460 scope = scope->se_outer;
6461 }
6462
6463 // Jump to the end of the FOR or WHILE loop.
6464 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006465 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006467 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6469 return FAIL;
6470
6471 return arg;
6472}
6473
6474/*
6475 * compile "{" start of block
6476 */
6477 static char_u *
6478compile_block(char_u *arg, cctx_T *cctx)
6479{
6480 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6481 return NULL;
6482 return skipwhite(arg + 1);
6483}
6484
6485/*
6486 * compile end of block: drop one scope
6487 */
6488 static void
6489compile_endblock(cctx_T *cctx)
6490{
6491 scope_T *scope = cctx->ctx_scope;
6492
6493 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006494 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 vim_free(scope);
6496}
6497
6498/*
6499 * compile "try"
6500 * Creates a new scope for the try-endtry, pointing to the first catch and
6501 * finally.
6502 * Creates another scope for the "try" block itself.
6503 * TRY instruction sets up exception handling at runtime.
6504 *
6505 * "try"
6506 * TRY -> catch1, -> finally push trystack entry
6507 * ... try block
6508 * "throw {exception}"
6509 * EVAL {exception}
6510 * THROW create exception
6511 * ... try block
6512 * " catch {expr}"
6513 * JUMP -> finally
6514 * catch1: PUSH exeception
6515 * EVAL {expr}
6516 * MATCH
6517 * JUMP nomatch -> catch2
6518 * CATCH remove exception
6519 * ... catch block
6520 * " catch"
6521 * JUMP -> finally
6522 * catch2: CATCH remove exception
6523 * ... catch block
6524 * " finally"
6525 * finally:
6526 * ... finally block
6527 * " endtry"
6528 * ENDTRY pop trystack entry, may rethrow
6529 */
6530 static char_u *
6531compile_try(char_u *arg, cctx_T *cctx)
6532{
6533 garray_T *instr = &cctx->ctx_instr;
6534 scope_T *try_scope;
6535 scope_T *scope;
6536
6537 // scope that holds the jumps that go to catch/finally/endtry
6538 try_scope = new_scope(cctx, TRY_SCOPE);
6539 if (try_scope == NULL)
6540 return NULL;
6541
6542 // "catch" is set when the first ":catch" is found.
6543 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006544 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 if (generate_instr(cctx, ISN_TRY) == NULL)
6546 return NULL;
6547
6548 // scope for the try block itself
6549 scope = new_scope(cctx, BLOCK_SCOPE);
6550 if (scope == NULL)
6551 return NULL;
6552
6553 return arg;
6554}
6555
6556/*
6557 * compile "catch {expr}"
6558 */
6559 static char_u *
6560compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6561{
6562 scope_T *scope = cctx->ctx_scope;
6563 garray_T *instr = &cctx->ctx_instr;
6564 char_u *p;
6565 isn_T *isn;
6566
6567 // end block scope from :try or :catch
6568 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6569 compile_endblock(cctx);
6570 scope = cctx->ctx_scope;
6571
6572 // Error if not in a :try scope
6573 if (scope == NULL || scope->se_type != TRY_SCOPE)
6574 {
6575 emsg(_(e_catch));
6576 return NULL;
6577 }
6578
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006579 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006581 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 return NULL;
6583 }
6584
6585 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006586 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 JUMP_ALWAYS, cctx) == FAIL)
6588 return NULL;
6589
6590 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006591 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 if (isn->isn_arg.try.try_catch == 0)
6593 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006594 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 {
6596 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 isn->isn_arg.jump.jump_where = instr->ga_len;
6599 }
6600
6601 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006602 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006604 scope->se_u.se_try.ts_caught_all = TRUE;
6605 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 }
6607 else
6608 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006609 char_u *end;
6610 char_u *pat;
6611 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006612 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006613 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 // Push v:exception, push {expr} and MATCH
6616 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6617
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006618 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006619 if (*end != *p)
6620 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006621 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006622 vim_free(tofree);
6623 return FAIL;
6624 }
6625 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006626 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006627 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006628 len = (int)(end - tofree);
6629 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006630 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006631 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006632 if (pat == NULL)
6633 return FAIL;
6634 if (generate_PUSHS(cctx, pat) == FAIL)
6635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6638 return NULL;
6639
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006640 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6642 return NULL;
6643 }
6644
6645 if (generate_instr(cctx, ISN_CATCH) == NULL)
6646 return NULL;
6647
6648 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6649 return NULL;
6650 return p;
6651}
6652
6653 static char_u *
6654compile_finally(char_u *arg, cctx_T *cctx)
6655{
6656 scope_T *scope = cctx->ctx_scope;
6657 garray_T *instr = &cctx->ctx_instr;
6658 isn_T *isn;
6659
6660 // end block scope from :try or :catch
6661 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6662 compile_endblock(cctx);
6663 scope = cctx->ctx_scope;
6664
6665 // Error if not in a :try scope
6666 if (scope == NULL || scope->se_type != TRY_SCOPE)
6667 {
6668 emsg(_(e_finally));
6669 return NULL;
6670 }
6671
6672 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006673 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 if (isn->isn_arg.try.try_finally != 0)
6675 {
6676 emsg(_(e_finally_dup));
6677 return NULL;
6678 }
6679
6680 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006681 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682
Bram Moolenaar585fea72020-04-02 22:33:21 +02006683 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006684 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 {
6686 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006687 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006689 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 }
6691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 // TODO: set index in ts_finally_label jumps
6693
6694 return arg;
6695}
6696
6697 static char_u *
6698compile_endtry(char_u *arg, cctx_T *cctx)
6699{
6700 scope_T *scope = cctx->ctx_scope;
6701 garray_T *instr = &cctx->ctx_instr;
6702 isn_T *isn;
6703
6704 // end block scope from :catch or :finally
6705 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6706 compile_endblock(cctx);
6707 scope = cctx->ctx_scope;
6708
6709 // Error if not in a :try scope
6710 if (scope == NULL || scope->se_type != TRY_SCOPE)
6711 {
6712 if (scope == NULL)
6713 emsg(_(e_no_endtry));
6714 else if (scope->se_type == WHILE_SCOPE)
6715 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006716 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 emsg(_(e_endfor));
6718 else
6719 emsg(_(e_endif));
6720 return NULL;
6721 }
6722
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006723 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006726 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 return NULL;
6728 }
6729
6730 // Fill in the "end" label in jumps at the end of the blocks, if not done
6731 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006732 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733
6734 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006735 if (isn->isn_arg.try.try_catch == 0)
6736 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 if (isn->isn_arg.try.try_finally == 0)
6738 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006739
6740 if (scope->se_u.se_try.ts_catch_label != 0)
6741 {
6742 // Last catch without match jumps here
6743 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6744 isn->isn_arg.jump.jump_where = instr->ga_len;
6745 }
6746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 compile_endblock(cctx);
6748
6749 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6750 return NULL;
6751 return arg;
6752}
6753
6754/*
6755 * compile "throw {expr}"
6756 */
6757 static char_u *
6758compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6759{
6760 char_u *p = skipwhite(arg);
6761
Bram Moolenaara5565e42020-05-09 15:44:01 +02006762 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 return NULL;
6764 if (may_generate_2STRING(-1, cctx) == FAIL)
6765 return NULL;
6766 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6767 return NULL;
6768
6769 return p;
6770}
6771
6772/*
6773 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006774 * compile "echomsg expr"
6775 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006776 * compile "execute expr"
6777 */
6778 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006779compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006780{
6781 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006782 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006783 int count = 0;
6784
6785 for (;;)
6786 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006787 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006788 return NULL;
6789 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006790 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006791 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006792 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006793 break;
6794 }
6795
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006796 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6797 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6798 else if (cmdidx == CMD_execute)
6799 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6800 else if (cmdidx == CMD_echomsg)
6801 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6802 else
6803 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 return p;
6805}
6806
6807/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006808 * :put r
6809 * :put ={expr}
6810 */
6811 static char_u *
6812compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6813{
6814 char_u *line = arg;
6815 linenr_T lnum;
6816 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006817 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006818
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006819 eap->regname = *line;
6820
6821 if (eap->regname == '=')
6822 {
6823 char_u *p = line + 1;
6824
6825 if (compile_expr0(&p, cctx) == FAIL)
6826 return NULL;
6827 line = p;
6828 }
6829 else if (eap->regname != NUL)
6830 ++line;
6831
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006832 // "errormsg" will not be set because the range is ADDR_LINES.
6833 // TODO: if the range contains something like "$" or "." need to evaluate
6834 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006835 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6836 return NULL;
6837 if (eap->addr_count == 0)
6838 lnum = -1;
6839 else
6840 lnum = eap->line2;
6841 if (above)
6842 --lnum;
6843
6844 generate_PUT(cctx, eap->regname, lnum);
6845 return line;
6846}
6847
6848/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006849 * A command that is not compiled, execute with legacy code.
6850 */
6851 static char_u *
6852compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6853{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006854 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006855 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006856 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006857
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006858 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006859 goto theend;
6860
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006861 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006862 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006863 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006864 int usefilter = FALSE;
6865
6866 has_expr = argt & (EX_XFILE | EX_EXPAND);
6867
6868 // If the command can be followed by a bar, find the bar and truncate
6869 // it, so that the following command can be compiled.
6870 // The '|' is overwritten with a NUL, it is put back below.
6871 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6872 && *eap->arg == '!')
6873 // :w !filter or :r !filter or :r! filter
6874 usefilter = TRUE;
6875 if ((argt & EX_TRLBAR) && !usefilter)
6876 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006877 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006878 separate_nextcmd(eap);
6879 if (eap->nextcmd != NULL)
6880 nextcmd = eap->nextcmd;
6881 }
6882 }
6883
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006884 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6885 {
6886 // expand filename in "syntax include [@group] filename"
6887 has_expr = TRUE;
6888 eap->arg = skipwhite(eap->arg + 7);
6889 if (*eap->arg == '@')
6890 eap->arg = skiptowhite(eap->arg);
6891 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006892
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006893 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006894 {
6895 int count = 0;
6896 char_u *start = skipwhite(line);
6897
6898 // :cmd xxx`=expr1`yyy`=expr2`zzz
6899 // PUSHS ":cmd xxx"
6900 // eval expr1
6901 // PUSHS "yyy"
6902 // eval expr2
6903 // PUSHS "zzz"
6904 // EXECCONCAT 5
6905 for (;;)
6906 {
6907 if (p > start)
6908 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006909 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006910 ++count;
6911 }
6912 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006913 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006914 return NULL;
6915 may_generate_2STRING(-1, cctx);
6916 ++count;
6917 p = skipwhite(p);
6918 if (*p != '`')
6919 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006920 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006921 return NULL;
6922 }
6923 start = p + 1;
6924
6925 p = (char_u *)strstr((char *)start, "`=");
6926 if (p == NULL)
6927 {
6928 if (*skipwhite(start) != NUL)
6929 {
6930 generate_PUSHS(cctx, vim_strsave(start));
6931 ++count;
6932 }
6933 break;
6934 }
6935 }
6936 generate_EXECCONCAT(cctx, count);
6937 }
6938 else
6939 generate_EXEC(cctx, line);
6940
6941theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006942 if (*nextcmd != NUL)
6943 {
6944 // the parser expects a pointer to the bar, put it back
6945 --nextcmd;
6946 *nextcmd = '|';
6947 }
6948
6949 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006950}
6951
6952/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006953 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006954 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006955 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006956 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006957add_def_function(ufunc_T *ufunc)
6958{
6959 dfunc_T *dfunc;
6960
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006961 if (def_functions.ga_len == 0)
6962 {
6963 // The first position is not used, so that a zero uf_dfunc_idx means it
6964 // wasn't set.
6965 if (ga_grow(&def_functions, 1) == FAIL)
6966 return FAIL;
6967 ++def_functions.ga_len;
6968 }
6969
Bram Moolenaar09689a02020-05-09 22:50:08 +02006970 // Add the function to "def_functions".
6971 if (ga_grow(&def_functions, 1) == FAIL)
6972 return FAIL;
6973 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6974 CLEAR_POINTER(dfunc);
6975 dfunc->df_idx = def_functions.ga_len;
6976 ufunc->uf_dfunc_idx = dfunc->df_idx;
6977 dfunc->df_ufunc = ufunc;
6978 ++def_functions.ga_len;
6979 return OK;
6980}
6981
6982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 * After ex_function() has collected all the function lines: parse and compile
6984 * the lines into instructions.
6985 * Adds the function to "def_functions".
6986 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6987 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006988 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006989 * This can be used recursively through compile_lambda(), which may reallocate
6990 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006991 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006993 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006994compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 char_u *line = NULL;
6997 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 cctx_T cctx;
7000 garray_T *instr;
7001 int called_emsg_before = called_emsg;
7002 int ret = FAIL;
7003 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007004 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007005 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007006 int emsg_before = called_emsg;
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.
7110 if (emsg_before != called_emsg)
7111 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 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007130 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131
Bram Moolenaara80faa82020-04-12 19:37:17 +02007132 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 ea.cmdlinep = &line;
7134 ea.cmd = skipwhite(line);
7135
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007136 // Some things can be recognized by the first character.
7137 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007139 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007140 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007141 if (ea.cmd[1] != '{')
7142 {
7143 line = (char_u *)"";
7144 continue;
7145 }
7146 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007148 case '}':
7149 {
7150 // "}" ends a block scope
7151 scopetype_T stype = cctx.ctx_scope == NULL
7152 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007154 if (stype == BLOCK_SCOPE)
7155 {
7156 compile_endblock(&cctx);
7157 line = ea.cmd;
7158 }
7159 else
7160 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007161 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007162 goto erret;
7163 }
7164 if (line != NULL)
7165 line = skipwhite(ea.cmd + 1);
7166 continue;
7167 }
7168
7169 case '{':
7170 // "{" starts a block scope
7171 // "{'a': 1}->func() is something else
7172 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7173 {
7174 line = compile_block(ea.cmd, &cctx);
7175 continue;
7176 }
7177 break;
7178
7179 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 }
7183
7184 /*
7185 * COMMAND MODIFIERS
7186 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007187 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007188 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7189 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 {
7191 if (errormsg != NULL)
7192 goto erret;
7193 // empty line or comment
7194 line = (char_u *)"";
7195 continue;
7196 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007197 generate_cmdmods(&cctx, &local_cmdmod);
7198 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199
7200 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007201 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007203 {
7204 if (*ea.cmd == '(')
7205 // not for "call()"
7206 ea.cmd = p;
7207 else
7208 ea.cmd = skipwhite(ea.cmd);
7209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007211 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007213 char_u *pskip;
7214
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007215 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007216 // find what follows.
7217 // Skip over "var.member", "var[idx]" and the like.
7218 // Also "&opt = val", "$ENV = val" and "@r = val".
7219 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007220 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007221 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007222 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007224 char_u *var_end;
7225 int oplen;
7226 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227
Bram Moolenaar65821722020-08-02 18:58:54 +02007228 if (ea.cmd[0] == '@')
7229 var_end = ea.cmd + 2;
7230 else
7231 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007232 FNE_CHECK_START | FNE_INCL_BR);
7233 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007234 if (oplen > 0)
7235 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007236 size_t len = p - ea.cmd;
7237
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007238 // Recognize an assignment if we recognize the variable
7239 // name:
7240 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007241 // "local = expr" where "local" is a local var.
7242 // "script = expr" where "script" is a script-local var.
7243 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007244 // "&opt = expr"
7245 // "$ENV = expr"
7246 // "@r = expr"
7247 if (*ea.cmd == '&'
7248 || *ea.cmd == '$'
7249 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007250 || ((len) > 2 && ea.cmd[1] == ':')
7251 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007252 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007253 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007254 || script_var_exists(ea.cmd, len,
7255 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007256 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007257 {
7258 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007259 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007260 goto erret;
7261 continue;
7262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 }
7264 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007265
7266 if (*ea.cmd == '[')
7267 {
7268 // [var, var] = expr
7269 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7270 if (line == NULL)
7271 goto erret;
7272 if (line != ea.cmd)
7273 continue;
7274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 }
7276
7277 /*
7278 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007279 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007281 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007282 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007283 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007284 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007285 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007286 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007287 if (!starts_with_colon)
7288 {
7289 emsg(_(e_colon_required_before_a_range));
7290 goto erret;
7291 }
7292 if (ends_excmd2(line, ea.cmd))
7293 {
7294 // A range without a command: jump to the line.
7295 // TODO: compile to a more efficient command, possibly
7296 // calling parse_cmd_address().
7297 ea.cmdidx = CMD_SIZE;
7298 line = compile_exec(line, &ea, &cctx);
7299 goto nextline;
7300 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007301 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007302 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007303 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007304 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007305 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306
7307 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7308 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007309 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007310 {
7311 line += STRLEN(line);
7312 continue;
7313 }
7314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007316 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007318 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007319 iemsg("Command from find_ex_command() not handled");
7320 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 }
7323
Bram Moolenaar3988f642020-08-27 22:43:03 +02007324 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007325 && ea.cmdidx != CMD_elseif
7326 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007327 && ea.cmdidx != CMD_endif
7328 && ea.cmdidx != CMD_endfor
7329 && ea.cmdidx != CMD_endwhile
7330 && ea.cmdidx != CMD_catch
7331 && ea.cmdidx != CMD_finally
7332 && ea.cmdidx != CMD_endtry)
7333 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007334 emsg(_(e_unreachable_code_after_return));
7335 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007336 }
7337
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007338 p = skipwhite(p);
7339 if (ea.cmdidx != CMD_SIZE
7340 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7341 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007342 if (ea.cmdidx >= 0)
7343 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007344 if ((ea.argt & EX_BANG) && *p == '!')
7345 {
7346 ea.forceit = TRUE;
7347 p = skipwhite(p + 1);
7348 }
7349 }
7350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 switch (ea.cmdidx)
7352 {
7353 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007354 ea.arg = p;
7355 line = compile_nested_function(&ea, &cctx);
7356 break;
7357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007359 // TODO: should we allow this, e.g. to declare a global
7360 // function?
7361 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 goto erret;
7363
7364 case CMD_return:
7365 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007366 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 break;
7368
7369 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007370 emsg(_(e_cannot_use_let_in_vim9_script));
7371 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007372 case CMD_var:
7373 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 case CMD_const:
7375 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007376 if (line == p)
7377 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 break;
7379
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007380 case CMD_unlet:
7381 case CMD_unlockvar:
7382 case CMD_lockvar:
7383 line = compile_unletlock(p, &ea, &cctx);
7384 break;
7385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 case CMD_import:
7387 line = compile_import(p, &cctx);
7388 break;
7389
7390 case CMD_if:
7391 line = compile_if(p, &cctx);
7392 break;
7393 case CMD_elseif:
7394 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007395 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 break;
7397 case CMD_else:
7398 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007399 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 break;
7401 case CMD_endif:
7402 line = compile_endif(p, &cctx);
7403 break;
7404
7405 case CMD_while:
7406 line = compile_while(p, &cctx);
7407 break;
7408 case CMD_endwhile:
7409 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007410 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 break;
7412
7413 case CMD_for:
7414 line = compile_for(p, &cctx);
7415 break;
7416 case CMD_endfor:
7417 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007418 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 break;
7420 case CMD_continue:
7421 line = compile_continue(p, &cctx);
7422 break;
7423 case CMD_break:
7424 line = compile_break(p, &cctx);
7425 break;
7426
7427 case CMD_try:
7428 line = compile_try(p, &cctx);
7429 break;
7430 case CMD_catch:
7431 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007432 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 break;
7434 case CMD_finally:
7435 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007436 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 break;
7438 case CMD_endtry:
7439 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007440 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 break;
7442 case CMD_throw:
7443 line = compile_throw(p, &cctx);
7444 break;
7445
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007446 case CMD_eval:
7447 if (compile_expr0(&p, &cctx) == FAIL)
7448 goto erret;
7449
Bram Moolenaar3988f642020-08-27 22:43:03 +02007450 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007451 generate_instr_drop(&cctx, ISN_DROP, 1);
7452
7453 line = skipwhite(p);
7454 break;
7455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007458 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007459 case CMD_echomsg:
7460 case CMD_echoerr:
7461 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007462 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007464 case CMD_put:
7465 ea.cmd = cmd;
7466 line = compile_put(p, &ea, &cctx);
7467 break;
7468
Bram Moolenaar3988f642020-08-27 22:43:03 +02007469 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007470
Bram Moolenaarae616492020-07-28 20:07:27 +02007471 case CMD_append:
7472 case CMD_change:
7473 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007474 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007475 case CMD_xit:
7476 not_in_vim9(&ea);
7477 goto erret;
7478
Bram Moolenaar002262f2020-07-08 17:47:57 +02007479 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007480 if (cctx.ctx_skip != SKIP_YES)
7481 {
7482 semsg(_(e_invalid_command_str), ea.cmd);
7483 goto erret;
7484 }
7485 // We don't check for a next command here.
7486 line = (char_u *)"";
7487 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007490 if (cctx.ctx_skip == SKIP_YES)
7491 {
7492 // We don't check for a next command here.
7493 line = (char_u *)"";
7494 }
7495 else
7496 {
7497 // Not recognized, execute with do_cmdline_cmd().
7498 ea.arg = p;
7499 line = compile_exec(line, &ea, &cctx);
7500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 break;
7502 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007503nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 if (line == NULL)
7505 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007506 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007508 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007509 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 if (cctx.ctx_type_stack.ga_len < 0)
7512 {
7513 iemsg("Type stack underflow");
7514 goto erret;
7515 }
7516 }
7517
7518 if (cctx.ctx_scope != NULL)
7519 {
7520 if (cctx.ctx_scope->se_type == IF_SCOPE)
7521 emsg(_(e_endif));
7522 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7523 emsg(_(e_endwhile));
7524 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7525 emsg(_(e_endfor));
7526 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007527 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 goto erret;
7529 }
7530
Bram Moolenaarefd88552020-06-18 20:50:10 +02007531 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 {
7533 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7534 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007535 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 goto erret;
7537 }
7538
7539 // Return zero if there is no return at the end.
7540 generate_PUSHNR(&cctx, 0);
7541 generate_instr(&cctx, ISN_RETURN);
7542 }
7543
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007544 {
7545 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7546 + ufunc->uf_dfunc_idx;
7547 dfunc->df_deleted = FALSE;
7548 dfunc->df_instr = instr->ga_data;
7549 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007550 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007551 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007552 if (cctx.ctx_outer_used)
7553 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007554 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556
7557 ret = OK;
7558
7559erret:
7560 if (ret == FAIL)
7561 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007562 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007563 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7564 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007565
7566 for (idx = 0; idx < instr->ga_len; ++idx)
7567 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007569
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007570 // If using the last entry in the table and it was added above, we
7571 // might as well remove it.
7572 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007573 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007574 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007575 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007576 ufunc->uf_dfunc_idx = 0;
7577 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007578 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007579
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007580 while (cctx.ctx_scope != NULL)
7581 drop_scope(&cctx);
7582
Bram Moolenaar20431c92020-03-20 18:39:46 +01007583 // Don't execute this function body.
7584 ga_clear_strings(&ufunc->uf_lines);
7585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 if (errormsg != NULL)
7587 emsg(errormsg);
7588 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007589 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 }
7591
7592 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007593 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007594 if (do_estack_push)
7595 estack_pop();
7596
Bram Moolenaar20431c92020-03-20 18:39:46 +01007597 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007598 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007600 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601}
7602
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007603 void
7604set_function_type(ufunc_T *ufunc)
7605{
7606 int varargs = ufunc->uf_va_name != NULL;
7607 int argcount = ufunc->uf_args.ga_len;
7608
7609 // Create a type for the function, with the return type and any
7610 // argument types.
7611 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7612 // The type is included in "tt_args".
7613 if (argcount > 0 || varargs)
7614 {
7615 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7616 argcount, &ufunc->uf_type_list);
7617 // Add argument types to the function type.
7618 if (func_type_add_arg_types(ufunc->uf_func_type,
7619 argcount + varargs,
7620 &ufunc->uf_type_list) == FAIL)
7621 return;
7622 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7623 ufunc->uf_func_type->tt_min_argcount =
7624 argcount - ufunc->uf_def_args.ga_len;
7625 if (ufunc->uf_arg_types == NULL)
7626 {
7627 int i;
7628
7629 // lambda does not have argument types.
7630 for (i = 0; i < argcount; ++i)
7631 ufunc->uf_func_type->tt_args[i] = &t_any;
7632 }
7633 else
7634 mch_memmove(ufunc->uf_func_type->tt_args,
7635 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7636 if (varargs)
7637 {
7638 ufunc->uf_func_type->tt_args[argcount] =
7639 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7640 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7641 }
7642 }
7643 else
7644 // No arguments, can use a predefined type.
7645 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7646 argcount, &ufunc->uf_type_list);
7647}
7648
7649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650/*
7651 * Delete an instruction, free what it contains.
7652 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007653 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654delete_instr(isn_T *isn)
7655{
7656 switch (isn->isn_type)
7657 {
7658 case ISN_EXEC:
7659 case ISN_LOADENV:
7660 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007661 case ISN_LOADB:
7662 case ISN_LOADW:
7663 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007665 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 case ISN_PUSHEXC:
7667 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007668 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007669 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007670 case ISN_STOREB:
7671 case ISN_STOREW:
7672 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007673 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 vim_free(isn->isn_arg.string);
7675 break;
7676
7677 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007678 case ISN_STORES:
7679 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 break;
7681
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007682 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007683 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007684 vim_free(isn->isn_arg.unlet.ul_name);
7685 break;
7686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687 case ISN_STOREOPT:
7688 vim_free(isn->isn_arg.storeopt.so_name);
7689 break;
7690
7691 case ISN_PUSHBLOB: // push blob isn_arg.blob
7692 blob_unref(isn->isn_arg.blob);
7693 break;
7694
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007695 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007697 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007698#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007699 break;
7700
7701 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007703 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007704#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007705 break;
7706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 case ISN_UCALL:
7708 vim_free(isn->isn_arg.ufunc.cuf_name);
7709 break;
7710
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007711 case ISN_FUNCREF:
7712 {
7713 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7714 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007715
7716 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7717 func_ptr_unref(dfunc->df_ufunc);
7718 }
7719 break;
7720
7721 case ISN_DCALL:
7722 {
7723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7724 + isn->isn_arg.dfunc.cdf_idx;
7725
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007726 if (dfunc->df_ufunc != NULL
7727 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007728 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007729 }
7730 break;
7731
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007732 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007733 {
7734 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7735 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7736
7737 if (ufunc != NULL)
7738 {
7739 // Clear uf_dfunc_idx so that the function is deleted.
7740 clear_def_function(ufunc);
7741 ufunc->uf_dfunc_idx = 0;
7742 func_ptr_unref(ufunc);
7743 }
7744
7745 vim_free(lambda);
7746 vim_free(isn->isn_arg.newfunc.nf_global);
7747 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007748 break;
7749
Bram Moolenaar5e654232020-09-16 15:22:00 +02007750 case ISN_CHECKTYPE:
7751 free_type(isn->isn_arg.type.ct_type);
7752 break;
7753
Bram Moolenaar02194d22020-10-24 23:08:38 +02007754 case ISN_CMDMOD:
7755 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7756 ->cmod_filter_regmatch.regprog);
7757 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7758 break;
7759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 case ISN_2BOOL:
7761 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007762 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 case ISN_ADDBLOB:
7764 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007765 case ISN_ANYINDEX:
7766 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007768 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007770 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007772 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 case ISN_COMPAREANY:
7774 case ISN_COMPAREBLOB:
7775 case ISN_COMPAREBOOL:
7776 case ISN_COMPAREDICT:
7777 case ISN_COMPAREFLOAT:
7778 case ISN_COMPAREFUNC:
7779 case ISN_COMPARELIST:
7780 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 case ISN_COMPARESPECIAL:
7782 case ISN_COMPARESTRING:
7783 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007784 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 case ISN_DROP:
7786 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007787 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007788 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007790 case ISN_EXECCONCAT:
7791 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007793 case ISN_GETITEM:
7794 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007795 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007796 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007797 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007799 case ISN_LOADBDICT:
7800 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007801 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007803 case ISN_LOADSCRIPT:
7804 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007806 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007807 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007808 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 case ISN_NEGATENR:
7810 case ISN_NEWDICT:
7811 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007813 case ISN_OPFLOAT:
7814 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007816 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007817 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 case ISN_PUSHF:
7819 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007821 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007823 case ISN_SHUFFLE:
7824 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007826 case ISN_STOREDICT:
7827 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007828 case ISN_STORENR:
7829 case ISN_STOREOUTER:
7830 case ISN_STOREREG:
7831 case ISN_STORESCRIPT:
7832 case ISN_STOREV:
7833 case ISN_STRINDEX:
7834 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 case ISN_THROW:
7836 case ISN_TRY:
7837 // nothing allocated
7838 break;
7839 }
7840}
7841
7842/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007843 * Free all instructions for "dfunc".
7844 */
7845 static void
7846delete_def_function_contents(dfunc_T *dfunc)
7847{
7848 int idx;
7849
7850 ga_clear(&dfunc->df_def_args_isn);
7851
7852 if (dfunc->df_instr != NULL)
7853 {
7854 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7855 delete_instr(dfunc->df_instr + idx);
7856 VIM_CLEAR(dfunc->df_instr);
7857 }
7858
7859 dfunc->df_deleted = TRUE;
7860}
7861
7862/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007863 * When a user function is deleted, clear the contents of any associated def
7864 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 */
7866 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007867clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007869 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 {
7871 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7872 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873
Bram Moolenaar20431c92020-03-20 18:39:46 +01007874 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007875 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 }
7877}
7878
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007879/*
7880 * Used when a user function is about to be deleted: remove the pointer to it.
7881 * The entry in def_functions is then unused.
7882 */
7883 void
7884unlink_def_function(ufunc_T *ufunc)
7885{
7886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7887
7888 dfunc->df_ufunc = NULL;
7889}
7890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007892/*
7893 * Free all functions defined with ":def".
7894 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 void
7896free_def_functions(void)
7897{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007898 int idx;
7899
7900 for (idx = 0; idx < def_functions.ga_len; ++idx)
7901 {
7902 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7903
7904 delete_def_function_contents(dfunc);
7905 }
7906
7907 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908}
7909#endif
7910
7911
7912#endif // FEAT_EVAL