blob: bda1ab3e47790f0290ac9206a3393f66e9a034db [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264 * Lookup a script-local variable in the current script, possibly defined in a
265 * block that contains the function "cctx->ctx_ufunc".
266 * "cctx" is NULL at the script level.
267 * if "len" is <= 0 "name" must be NUL terminated.
268 * Return NULL when not found.
269 */
270 static sallvar_T *
271find_script_var(char_u *name, size_t len, cctx_T *cctx)
272{
273 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
274 hashitem_T *hi;
275 int cc;
276 sallvar_T *sav;
277 ufunc_T *ufunc;
278
279 // Find the list of all script variables with the right name.
280 if (len > 0)
281 {
282 cc = name[len];
283 name[len] = NUL;
284 }
285 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
286 if (len > 0)
287 name[len] = cc;
288 if (HASHITEM_EMPTY(hi))
289 return NULL;
290
291 sav = HI2SAV(hi);
292 if (sav->sav_block_id == 0 || cctx == NULL)
293 // variable defined in the script scope or not in a function.
294 return sav;
295
296 // Go over the variables with this name and find one that was visible
297 // from the function.
298 ufunc = cctx->ctx_ufunc;
299 while (sav != NULL)
300 {
301 int idx;
302
303 // Go over the blocks that this function was defined in. If the
304 // variable block ID matches it was visible to the function.
305 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
306 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
307 return sav;
308 sav = sav->sav_next;
309 }
310
311 return NULL;
312}
313
314/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200315 * Returnd TRUE if the script context is Vim9 script.
316 */
317 static int
318script_is_vim9()
319{
320 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
321}
322
323/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200324 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200325 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
326 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 * Returns OK or FAIL.
329 */
330 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200335 if (current_sctx.sc_sid <= 0)
336 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 is_vim9_script = script_is_vim9();
338 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200339 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340 if (is_vim9_script)
341 {
342 // Check script variables that were visible where the function was
343 // defined.
344 if (find_script_var(name, len, cctx) != NULL)
345 return OK;
346 }
347 else
348 {
349 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
350 dictitem_T *di;
351 int cc;
352
353 // Check script variables that are currently visible
354 cc = name[len];
355 name[len] = NUL;
356 di = find_var_in_ht(ht, 0, name, TRUE);
357 name[len] = cc;
358 if (di != NULL)
359 return OK;
360 }
361
362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363}
364
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365/*
366 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200367 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200368 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 * Return FAIL and give an error if it defined.
370 */
371 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 int c = p[len];
375 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200376
377 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200378 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200380 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100384 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200385 // A local or script-local function can shadow a global function.
386 if (ufunc == NULL || !func_is_global(ufunc)
387 || (p[0] == 'g' && p[1] == ':'))
388 {
389 p[len] = c;
390 semsg(_(e_name_already_defined_str), p);
391 return FAIL;
392 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200394 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100395 return OK;
396}
397
Bram Moolenaar65b95452020-07-19 14:03:09 +0200398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200455 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200463 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 */
465 static int
466may_generate_2STRING(int offset, cctx_T *cctx)
467{
468 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 switch ((*type)->tt_type)
474 {
475 // nothing to be done
476 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 // conversion possible
479 case VAR_SPECIAL:
480 case VAR_BOOL:
481 case VAR_NUMBER:
482 case VAR_FLOAT:
483 break;
484
485 // conversion possible (with runtime check)
486 case VAR_ANY:
487 case VAR_UNKNOWN:
488 isntype = ISN_2STRING_ANY;
489 break;
490
491 // conversion not possible
492 case VAR_VOID:
493 case VAR_BLOB:
494 case VAR_FUNC:
495 case VAR_PARTIAL:
496 case VAR_LIST:
497 case VAR_DICT:
498 case VAR_JOB:
499 case VAR_CHANNEL:
500 to_string_error((*type)->tt_type);
501 return FAIL;
502 }
503
504 *type = &t_string;
505 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507 isn->isn_arg.number = offset;
508
509 return OK;
510}
511
512 static int
513check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
514{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200517 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 {
519 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return FAIL;
524 }
525 return OK;
526}
527
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200528 static int
529generate_add_instr(
530 cctx_T *cctx,
531 vartype_T vartype,
532 type_T *type1,
533 type_T *type2)
534{
535 isn_T *isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR
537 : vartype == VAR_LIST ? ISN_ADDLIST
538 : vartype == VAR_BLOB ? ISN_ADDBLOB
539#ifdef FEAT_FLOAT
540 : vartype == VAR_FLOAT ? ISN_OPFLOAT
541#endif
542 : ISN_OPANY, 1);
543
544 if (vartype != VAR_LIST && vartype != VAR_BLOB
545 && type1->tt_type != VAR_ANY
546 && type2->tt_type != VAR_ANY
547 && check_number_or_float(
548 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
549 return FAIL;
550
551 if (isn != NULL)
552 isn->isn_arg.op.op_type = EXPR_ADD;
553 return isn == NULL ? FAIL : OK;
554}
555
556/*
557 * Get the type to use for an instruction for an operation on "type1" and
558 * "type2". If they are matching use a type-specific instruction. Otherwise
559 * fall back to runtime type checking.
560 */
561 static vartype_T
562operator_type(type_T *type1, type_T *type2)
563{
564 if (type1->tt_type == type2->tt_type
565 && (type1->tt_type == VAR_NUMBER
566 || type1->tt_type == VAR_LIST
567#ifdef FEAT_FLOAT
568 || type1->tt_type == VAR_FLOAT
569#endif
570 || type1->tt_type == VAR_BLOB))
571 return type1->tt_type;
572 return VAR_ANY;
573}
574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575/*
576 * Generate an instruction with two arguments. The instruction depends on the
577 * type of the arguments.
578 */
579 static int
580generate_two_op(cctx_T *cctx, char_u *op)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583 type_T *type1;
584 type_T *type2;
585 vartype_T vartype;
586 isn_T *isn;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_OK_IF_SKIP(cctx);
589
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200590 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
592 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 switch (*op)
596 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200597 case '+':
598 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 break;
601
602 case '-':
603 case '*':
604 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
605 op) == FAIL)
606 return FAIL;
607 if (vartype == VAR_NUMBER)
608 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
609#ifdef FEAT_FLOAT
610 else if (vartype == VAR_FLOAT)
611 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
612#endif
613 else
614 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
615 if (isn != NULL)
616 isn->isn_arg.op.op_type = *op == '*'
617 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
618 break;
619
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200622 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 && type2->tt_type != VAR_NUMBER))
624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200625 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 }
628 isn = generate_instr_drop(cctx,
629 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = EXPR_REM;
632 break;
633 }
634
635 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 type_T *type = &t_any;
639
640#ifdef FEAT_FLOAT
641 // float+number and number+float results in float
642 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
643 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
644 type = &t_float;
645#endif
646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
647 }
648
649 return OK;
650}
651
652/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200653 * Get the instruction to use for comparing "type1" with "type2"
654 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200656 static isntype_T
657get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658{
659 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 if (type1 == VAR_UNKNOWN)
662 type1 = VAR_ANY;
663 if (type2 == VAR_UNKNOWN)
664 type2 = VAR_ANY;
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (type1 == type2)
667 {
668 switch (type1)
669 {
670 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
671 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
672 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
673 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
674 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
675 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
676 case VAR_LIST: isntype = ISN_COMPARELIST; break;
677 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
678 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 default: isntype = ISN_COMPAREANY; break;
680 }
681 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
684 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
685 isntype = ISN_COMPAREANY;
686
687 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
688 && (isntype == ISN_COMPAREBOOL
689 || isntype == ISN_COMPARESPECIAL
690 || isntype == ISN_COMPARENR
691 || isntype == ISN_COMPAREFLOAT))
692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200693 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200695 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 }
697 if (isntype == ISN_DROP
698 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
699 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
700 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
701 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
702 && exptype != EXPR_IS && exptype != EXPR_ISNOT
703 && (type1 == VAR_BLOB || type2 == VAR_BLOB
704 || type1 == VAR_LIST || type2 == VAR_LIST))))
705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200706 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return isntype;
711}
712
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200713 int
714check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
715{
716 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
717 return FAIL;
718 return OK;
719}
720
Bram Moolenaara5565e42020-05-09 15:44:01 +0200721/*
722 * Generate an ISN_COMPARE* instruction with a boolean result.
723 */
724 static int
725generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
726{
727 isntype_T isntype;
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730 vartype_T type1;
731 vartype_T type2;
732
733 RETURN_OK_IF_SKIP(cctx);
734
735 // Get the known type of the two items on the stack. If they are matching
736 // use a type-specific instruction. Otherwise fall back to runtime type
737 // checking.
738 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
739 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
740 isntype = get_compare_isn(exptype, type1, type2);
741 if (isntype == ISN_DROP)
742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if ((isn = generate_instr(cctx, isntype)) == NULL)
745 return FAIL;
746 isn->isn_arg.op.op_type = exptype;
747 isn->isn_arg.op.op_ic = ic;
748
749 // takes two arguments, puts one bool back
750 if (stack->ga_len >= 2)
751 {
752 --stack->ga_len;
753 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
754 }
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_2BOOL instruction.
761 */
762 static int
763generate_2BOOL(cctx_T *cctx, int invert)
764{
765 isn_T *isn;
766 garray_T *stack = &cctx->ctx_type_stack;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
770 return FAIL;
771 isn->isn_arg.number = invert;
772
773 // type becomes bool
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775
776 return OK;
777}
778
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200779/*
780 * Generate an ISN_COND2BOOL instruction.
781 */
782 static int
783generate_COND2BOOL(cctx_T *cctx)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
788 RETURN_OK_IF_SKIP(cctx);
789 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
790 return FAIL;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200799generate_TYPECHECK(
800 cctx_T *cctx,
801 type_T *expected,
802 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
Bram Moolenaar080457c2020-03-03 21:53:32 +0100807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
809 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200810 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 isn->isn_arg.type.ct_off = offset;
812
Bram Moolenaar5e654232020-09-16 15:22:00 +0200813 // type becomes expected
814 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 return OK;
817}
818
819/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200820 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821 * used. Return FALSE if the types will never match.
822 */
823 static int
824use_typecheck(type_T *actual, type_T *expected)
825{
826 if (actual->tt_type == VAR_ANY
827 || actual->tt_type == VAR_UNKNOWN
828 || (actual->tt_type == VAR_FUNC
829 && (expected->tt_type == VAR_FUNC
830 || expected->tt_type == VAR_PARTIAL)
831 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
832 return TRUE;
833 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
834 && actual->tt_type == expected->tt_type)
835 // This takes care of a nested list or dict.
836 return use_typecheck(actual->tt_member, expected->tt_member);
837 return FALSE;
838}
839
840/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200843 * - "actual" is a type that can be "expected" type: add a runtime check; or
844 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200845 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
846 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 */
848 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200849need_type(
850 type_T *actual,
851 type_T *expected,
852 int offset,
853 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200854 int silent,
855 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200857 if (expected == &t_bool && actual != &t_bool
858 && (actual->tt_flags & TTFLAG_BOOL_OK))
859 {
860 // Using "0", "1" or the result of an expression with "&&" or "||" as a
861 // boolean is OK but requires a conversion.
862 generate_2BOOL(cctx, FALSE);
863 return OK;
864 }
865
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200866 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200868
869 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 // If it's a constant a runtime check makes no sense.
871 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 generate_TYPECHECK(cctx, expected, offset);
874 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200875 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876
877 if (!silent)
878 type_mismatch(expected, actual);
879 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880}
881
882/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 * Generate an ISN_PUSHNR instruction.
884 */
885 static int
886generate_PUSHNR(cctx_T *cctx, varnumber_T number)
887{
888 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200889 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar080457c2020-03-03 21:53:32 +0100891 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
893 return FAIL;
894 isn->isn_arg.number = number;
895
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200896 if (number == 0 || number == 1)
897 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200898 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200899
900 // A 0 or 1 number can also be used as a bool.
901 if (type != NULL)
902 {
903 type->tt_type = VAR_NUMBER;
904 type->tt_flags = TTFLAG_BOOL_OK;
905 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
906 }
907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 return OK;
909}
910
911/*
912 * Generate an ISN_PUSHBOOL instruction.
913 */
914 static int
915generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
921 return FAIL;
922 isn->isn_arg.number = number;
923
924 return OK;
925}
926
927/*
928 * Generate an ISN_PUSHSPEC instruction.
929 */
930 static int
931generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
937 return FAIL;
938 isn->isn_arg.number = number;
939
940 return OK;
941}
942
943#ifdef FEAT_FLOAT
944/*
945 * Generate an ISN_PUSHF instruction.
946 */
947 static int
948generate_PUSHF(cctx_T *cctx, float_T fnumber)
949{
950 isn_T *isn;
951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
954 return FAIL;
955 isn->isn_arg.fnumber = fnumber;
956
957 return OK;
958}
959#endif
960
961/*
962 * Generate an ISN_PUSHS instruction.
963 * Consumes "str".
964 */
965 static int
966generate_PUSHS(cctx_T *cctx, char_u *str)
967{
968 isn_T *isn;
969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
972 return FAIL;
973 isn->isn_arg.string = str;
974
975 return OK;
976}
977
978/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100979 * Generate an ISN_PUSHCHANNEL instruction.
980 * Consumes "channel".
981 */
982 static int
983generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
984{
985 isn_T *isn;
986
Bram Moolenaar080457c2020-03-03 21:53:32 +0100987 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100988 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
989 return FAIL;
990 isn->isn_arg.channel = channel;
991
992 return OK;
993}
994
995/*
996 * Generate an ISN_PUSHJOB instruction.
997 * Consumes "job".
998 */
999 static int
1000generate_PUSHJOB(cctx_T *cctx, job_T *job)
1001{
1002 isn_T *isn;
1003
Bram Moolenaar080457c2020-03-03 21:53:32 +01001004 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001005 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001006 return FAIL;
1007 isn->isn_arg.job = job;
1008
1009 return OK;
1010}
1011
1012/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 * Generate an ISN_PUSHBLOB instruction.
1014 * Consumes "blob".
1015 */
1016 static int
1017generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.blob = blob;
1025
1026 return OK;
1027}
1028
1029/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 * Generate an ISN_PUSHFUNC instruction with name "name".
1031 * Consumes "name".
1032 */
1033 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001034generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001039 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001041 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 * Generate an ISN_GETITEM instruction with "index".
1048 */
1049 static int
1050generate_GETITEM(cctx_T *cctx, int index)
1051{
1052 isn_T *isn;
1053 garray_T *stack = &cctx->ctx_type_stack;
1054 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1055 type_T *item_type = &t_any;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001061 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001062 emsg(_(e_listreq));
1063 return FAIL;
1064 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001065 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001066 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.number = index;
1069
1070 // add the item type to the type stack
1071 if (ga_grow(stack, 1) == FAIL)
1072 return FAIL;
1073 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1074 ++stack->ga_len;
1075 return OK;
1076}
1077
1078/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001079 * Generate an ISN_SLICE instruction with "count".
1080 */
1081 static int
1082generate_SLICE(cctx_T *cctx, int count)
1083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
1087 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = count;
1090 return OK;
1091}
1092
1093/*
1094 * Generate an ISN_CHECKLEN instruction with "min_len".
1095 */
1096 static int
1097generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1098{
1099 isn_T *isn;
1100
1101 RETURN_OK_IF_SKIP(cctx);
1102
1103 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.checklen.cl_min_len = min_len;
1106 isn->isn_arg.checklen.cl_more_OK = more_OK;
1107
1108 return OK;
1109}
1110
1111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 * Generate an ISN_STORE instruction.
1113 */
1114 static int
1115generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1116{
1117 isn_T *isn;
1118
Bram Moolenaar080457c2020-03-03 21:53:32 +01001119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1121 return FAIL;
1122 if (name != NULL)
1123 isn->isn_arg.string = vim_strsave(name);
1124 else
1125 isn->isn_arg.number = idx;
1126
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1132 */
1133 static int
1134generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1140 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001141 isn->isn_arg.storenr.stnr_idx = idx;
1142 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143
1144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_STOREOPT instruction
1149 */
1150 static int
1151generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001156 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 return FAIL;
1158 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1159 isn->isn_arg.storeopt.so_flags = opt_flags;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_LOAD or similar instruction.
1166 */
1167 static int
1168generate_LOAD(
1169 cctx_T *cctx,
1170 isntype_T isn_type,
1171 int idx,
1172 char_u *name,
1173 type_T *type)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1179 return FAIL;
1180 if (name != NULL)
1181 isn->isn_arg.string = vim_strsave(name);
1182 else
1183 isn->isn_arg.number = idx;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001189 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001190 */
1191 static int
1192generate_LOADV(
1193 cctx_T *cctx,
1194 char_u *name,
1195 int error)
1196{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001197 int di_flags;
1198 int vidx = find_vim_var(name, &di_flags);
1199 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001202 if (vidx < 0)
1203 {
1204 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001205 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 return FAIL;
1207 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001208 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001210 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211}
1212
1213/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001214 * Generate an ISN_UNLET instruction.
1215 */
1216 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001217generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001218{
1219 isn_T *isn;
1220
1221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001222 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001223 return FAIL;
1224 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1225 isn->isn_arg.unlet.ul_forceit = forceit;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001231 * Generate an ISN_LOCKCONST instruction.
1232 */
1233 static int
1234generate_LOCKCONST(cctx_T *cctx)
1235{
1236 isn_T *isn;
1237
1238 RETURN_OK_IF_SKIP(cctx);
1239 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1240 return FAIL;
1241 return OK;
1242}
1243
1244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 * Generate an ISN_LOADS instruction.
1246 */
1247 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001252 int sid,
1253 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254{
1255 isn_T *isn;
1256
Bram Moolenaar080457c2020-03-03 21:53:32 +01001257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 if (isn_type == ISN_LOADS)
1259 isn = generate_instr_type(cctx, isn_type, type);
1260 else
1261 isn = generate_instr_drop(cctx, isn_type, 1);
1262 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1265 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267 return OK;
1268}
1269
1270/*
1271 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1272 */
1273 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 cctx_T *cctx,
1276 isntype_T isn_type,
1277 int sid,
1278 int idx,
1279 type_T *type)
1280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if (isn_type == ISN_LOADSCRIPT)
1285 isn = generate_instr_type(cctx, isn_type, type);
1286 else
1287 isn = generate_instr_drop(cctx, isn_type, 1);
1288 if (isn == NULL)
1289 return FAIL;
1290 isn->isn_arg.script.script_sid = sid;
1291 isn->isn_arg.script.script_idx = idx;
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_NEWLIST instruction.
1297 */
1298 static int
1299generate_NEWLIST(cctx_T *cctx, int count)
1300{
1301 isn_T *isn;
1302 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 type_T *type;
1304 type_T *member;
1305
Bram Moolenaar080457c2020-03-03 21:53:32 +01001306 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.number = count;
1310
Bram Moolenaar127542b2020-08-09 17:22:04 +02001311 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001312 if (count == 0)
1313 member = &t_void;
1314 else
1315 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001316 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1317 cctx->ctx_type_list);
1318 type = get_list_type(member, cctx->ctx_type_list);
1319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 // drop the value types
1321 stack->ga_len -= count;
1322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 // add the list type to the type stack
1324 if (ga_grow(stack, 1) == FAIL)
1325 return FAIL;
1326 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1327 ++stack->ga_len;
1328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_NEWDICT instruction.
1334 */
1335 static int
1336generate_NEWDICT(cctx_T *cctx, int count)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 type_T *type;
1341 type_T *member;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.number = count;
1347
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001348 if (count == 0)
1349 member = &t_void;
1350 else
1351 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001352 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1353 cctx->ctx_type_list);
1354 type = get_dict_type(member, cctx->ctx_type_list);
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 // drop the key and value types
1357 stack->ga_len -= 2 * count;
1358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 // add the dict type to the type stack
1360 if (ga_grow(stack, 1) == FAIL)
1361 return FAIL;
1362 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1363 ++stack->ga_len;
1364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_FUNCREF instruction.
1370 */
1371 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001372generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
1376
Bram Moolenaar080457c2020-03-03 21:53:32 +01001377 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1379 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001380 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001381 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
1383 if (ga_grow(stack, 1) == FAIL)
1384 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001385 ((type_T **)stack->ga_data)[stack->ga_len] =
1386 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 ++stack->ga_len;
1388
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001393 * Generate an ISN_NEWFUNC instruction.
1394 */
1395 static int
1396generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1397{
1398 isn_T *isn;
1399 char_u *name;
1400
1401 RETURN_OK_IF_SKIP(cctx);
1402 name = vim_strsave(lambda_name);
1403 if (name == NULL)
1404 return FAIL;
1405 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1406 return FAIL;
1407 isn->isn_arg.newfunc.nf_lambda = name;
1408 isn->isn_arg.newfunc.nf_global = func_name;
1409
1410 return OK;
1411}
1412
1413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 * Generate an ISN_JUMP instruction.
1415 */
1416 static int
1417generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1418{
1419 isn_T *isn;
1420 garray_T *stack = &cctx->ctx_type_stack;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1424 return FAIL;
1425 isn->isn_arg.jump.jump_when = when;
1426 isn->isn_arg.jump.jump_where = where;
1427
1428 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1429 --stack->ga_len;
1430
1431 return OK;
1432}
1433
1434 static int
1435generate_FOR(cctx_T *cctx, int loop_idx)
1436{
1437 isn_T *isn;
1438 garray_T *stack = &cctx->ctx_type_stack;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.forloop.for_idx = loop_idx;
1444
1445 if (ga_grow(stack, 1) == FAIL)
1446 return FAIL;
1447 // type doesn't matter, will be stored next
1448 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1449 ++stack->ga_len;
1450
1451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001456 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Return FAIL if the number of arguments is wrong.
1458 */
1459 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001460generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461{
1462 isn_T *isn;
1463 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001464 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001465 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
Bram Moolenaar080457c2020-03-03 21:53:32 +01001467 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001468 argoff = check_internal_func(func_idx, argcount);
1469 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 return FAIL;
1471
Bram Moolenaar389df252020-07-09 21:20:47 +02001472 if (method_call && argoff > 1)
1473 {
1474 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.shuffle.shfl_item = argcount;
1477 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1478 }
1479
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001480 if (argcount > 0)
1481 {
1482 // Check the types of the arguments.
1483 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1484 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001486 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.bfunc.cbf_idx = func_idx;
1491 isn->isn_arg.bfunc.cbf_argcount = argcount;
1492
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 // Drop the argument types and push the return type.
1494 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 if (ga_grow(stack, 1) == FAIL)
1496 return FAIL;
1497 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001498 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001499 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
1501 return OK;
1502}
1503
1504/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001505 * Generate an ISN_LISTAPPEND instruction. Works like add().
1506 * Argument count is already checked.
1507 */
1508 static int
1509generate_LISTAPPEND(cctx_T *cctx)
1510{
1511 garray_T *stack = &cctx->ctx_type_stack;
1512 type_T *list_type;
1513 type_T *item_type;
1514 type_T *expected;
1515
1516 // Caller already checked that list_type is a list.
1517 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1518 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1519 expected = list_type->tt_member;
1520 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1521 return FAIL;
1522
1523 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1524 return FAIL;
1525
1526 --stack->ga_len; // drop the argument
1527 return OK;
1528}
1529
1530/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001531 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1532 * Argument count is already checked.
1533 */
1534 static int
1535generate_BLOBAPPEND(cctx_T *cctx)
1536{
1537 garray_T *stack = &cctx->ctx_type_stack;
1538 type_T *item_type;
1539
1540 // Caller already checked that blob_type is a blob.
1541 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1542 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 * Generate an ISN_DCALL or ISN_UCALL instruction.
1554 * Return FAIL if the number of arguments is wrong.
1555 */
1556 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001557generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558{
1559 isn_T *isn;
1560 garray_T *stack = &cctx->ctx_type_stack;
1561 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001562 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
Bram Moolenaar080457c2020-03-03 21:53:32 +01001564 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 if (argcount > regular_args && !has_varargs(ufunc))
1566 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001567 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 return FAIL;
1569 }
1570 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1571 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001572 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 return FAIL;
1574 }
1575
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001576 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001577 {
1578 int i;
1579
1580 for (i = 0; i < argcount; ++i)
1581 {
1582 type_T *expected;
1583 type_T *actual;
1584
1585 if (i < regular_args)
1586 {
1587 if (ufunc->uf_arg_types == NULL)
1588 continue;
1589 expected = ufunc->uf_arg_types[i];
1590 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001591 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1592 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001593 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001594 else
1595 expected = ufunc->uf_va_type->tt_member;
1596 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001597 if (need_type(actual, expected, -argcount + i, cctx,
1598 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 arg_type_mismatch(expected, actual, i + 1);
1601 return FAIL;
1602 }
1603 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001605 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1606 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001607 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001608 }
1609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001611 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001612 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001614 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
1616 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1617 isn->isn_arg.dfunc.cdf_argcount = argcount;
1618 }
1619 else
1620 {
1621 // A user function may be deleted and redefined later, can't use the
1622 // ufunc pointer, need to look it up again at runtime.
1623 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1624 isn->isn_arg.ufunc.cuf_argcount = argcount;
1625 }
1626
1627 stack->ga_len -= argcount; // drop the arguments
1628 if (ga_grow(stack, 1) == FAIL)
1629 return FAIL;
1630 // add return value
1631 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1632 ++stack->ga_len;
1633
1634 return OK;
1635}
1636
1637/*
1638 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1639 */
1640 static int
1641generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1648 return FAIL;
1649 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1650 isn->isn_arg.ufunc.cuf_argcount = argcount;
1651
1652 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001653 if (ga_grow(stack, 1) == FAIL)
1654 return FAIL;
1655 // add return value
1656 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1657 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658
1659 return OK;
1660}
1661
1662/*
1663 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001664 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 */
1666 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001667generate_PCALL(
1668 cctx_T *cctx,
1669 int argcount,
1670 char_u *name,
1671 type_T *type,
1672 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673{
1674 isn_T *isn;
1675 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001676 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
Bram Moolenaar080457c2020-03-03 21:53:32 +01001678 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001679
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001680 if (type->tt_type == VAR_ANY)
1681 ret_type = &t_any;
1682 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001683 {
1684 if (type->tt_argcount != -1)
1685 {
1686 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1687
1688 if (argcount < type->tt_min_argcount - varargs)
1689 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001690 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001691 return FAIL;
1692 }
1693 if (!varargs && argcount > type->tt_argcount)
1694 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001695 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001696 return FAIL;
1697 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001698 if (type->tt_args != NULL)
1699 {
1700 int i;
1701
1702 for (i = 0; i < argcount; ++i)
1703 {
1704 int offset = -argcount + i - 1;
1705 type_T *actual = ((type_T **)stack->ga_data)[
1706 stack->ga_len + offset];
1707 type_T *expected;
1708
1709 if (varargs && i >= type->tt_min_argcount - 1)
1710 expected = type->tt_args[
1711 type->tt_min_argcount - 1]->tt_member;
1712 else
1713 expected = type->tt_args[i];
1714 if (need_type(actual, expected, offset,
1715 cctx, TRUE, FALSE) == FAIL)
1716 {
1717 arg_type_mismatch(expected, actual, i + 1);
1718 return FAIL;
1719 }
1720 }
1721 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001722 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001723 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001724 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001725 else
1726 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001727 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001728 return FAIL;
1729 }
1730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1732 return FAIL;
1733 isn->isn_arg.pfunc.cpf_top = at_top;
1734 isn->isn_arg.pfunc.cpf_argcount = argcount;
1735
1736 stack->ga_len -= argcount; // drop the arguments
1737
1738 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001739 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001741 // If partial is above the arguments it must be cleared and replaced with
1742 // the return value.
1743 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1744 return FAIL;
1745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 return OK;
1747}
1748
1749/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001750 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 */
1752 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001753generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754{
1755 isn_T *isn;
1756 garray_T *stack = &cctx->ctx_type_stack;
1757 type_T *type;
1758
Bram Moolenaar080457c2020-03-03 21:53:32 +01001759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001760 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001762 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001764 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001766 if (type->tt_type != VAR_DICT && type != &t_any)
1767 {
1768 emsg(_(e_dictreq));
1769 return FAIL;
1770 }
1771 // change dict type to dict member type
1772 if (type->tt_type == VAR_DICT)
1773 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774
1775 return OK;
1776}
1777
1778/*
1779 * Generate an ISN_ECHO instruction.
1780 */
1781 static int
1782generate_ECHO(cctx_T *cctx, int with_white, int count)
1783{
1784 isn_T *isn;
1785
Bram Moolenaar080457c2020-03-03 21:53:32 +01001786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1788 return FAIL;
1789 isn->isn_arg.echo.echo_with_white = with_white;
1790 isn->isn_arg.echo.echo_count = count;
1791
1792 return OK;
1793}
1794
Bram Moolenaarad39c092020-02-26 18:23:43 +01001795/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001796 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001797 */
1798 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001799generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001800{
1801 isn_T *isn;
1802
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001803 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001804 return FAIL;
1805 isn->isn_arg.number = count;
1806
1807 return OK;
1808}
1809
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001810/*
1811 * Generate an ISN_PUT instruction.
1812 */
1813 static int
1814generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1815{
1816 isn_T *isn;
1817
1818 RETURN_OK_IF_SKIP(cctx);
1819 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1820 return FAIL;
1821 isn->isn_arg.put.put_regname = regname;
1822 isn->isn_arg.put.put_lnum = lnum;
1823 return OK;
1824}
1825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 static int
1827generate_EXEC(cctx_T *cctx, char_u *line)
1828{
1829 isn_T *isn;
1830
Bram Moolenaar080457c2020-03-03 21:53:32 +01001831 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1833 return FAIL;
1834 isn->isn_arg.string = vim_strsave(line);
1835 return OK;
1836}
1837
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001838 static int
1839generate_EXECCONCAT(cctx_T *cctx, int count)
1840{
1841 isn_T *isn;
1842
1843 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1844 return FAIL;
1845 isn->isn_arg.number = count;
1846 return OK;
1847}
1848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001850 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001851 */
1852 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001853generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001854{
1855 isn_T *isn;
1856
Bram Moolenaar02194d22020-10-24 23:08:38 +02001857 if (cmod->cmod_flags != 0
1858 || cmod->cmod_split != 0
1859 || cmod->cmod_verbose != 0
1860 || cmod->cmod_tab != 0
1861 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001862 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001863 cctx->ctx_has_cmdmod = TRUE;
1864
1865 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001866 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001867 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1868 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1869 return FAIL;
1870 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1871 // filter progam now belongs to the instruction
1872 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001873 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001874
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001875 return OK;
1876}
1877
1878 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001879generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001880{
1881 isn_T *isn;
1882
Bram Moolenaar02194d22020-10-24 23:08:38 +02001883 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001884 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001885 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001886 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001887 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001888
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001889 return OK;
1890}
1891
1892/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001894 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001896 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001897reserve_local(
1898 cctx_T *cctx,
1899 char_u *name,
1900 size_t len,
1901 int isConst,
1902 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 lvar_T *lvar;
1905
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001906 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001908 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001909 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 }
1911
1912 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001913 return NULL;
1914 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001915 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001917 // Every local variable uses the next entry on the stack. We could re-use
1918 // the last ones when leaving a scope, but then variables used in a closure
1919 // might get overwritten. To keep things simple do not re-use stack
1920 // entries. This is less efficient, but memory is cheap these days.
1921 lvar->lv_idx = cctx->ctx_locals_count++;
1922
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001923 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 lvar->lv_const = isConst;
1925 lvar->lv_type = type;
1926
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001927 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928}
1929
1930/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001931 * Remove local variables above "new_top".
1932 */
1933 static void
1934unwind_locals(cctx_T *cctx, int new_top)
1935{
1936 if (cctx->ctx_locals.ga_len > new_top)
1937 {
1938 int idx;
1939 lvar_T *lvar;
1940
1941 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1942 {
1943 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1944 vim_free(lvar->lv_name);
1945 }
1946 }
1947 cctx->ctx_locals.ga_len = new_top;
1948}
1949
1950/*
1951 * Free all local variables.
1952 */
1953 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001954free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001955{
1956 unwind_locals(cctx, 0);
1957 ga_clear(&cctx->ctx_locals);
1958}
1959
1960/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 * Find "name" in script-local items of script "sid".
1962 * Returns the index in "sn_var_vals" if found.
1963 * If found but not in "sn_var_vals" returns -1.
1964 * If not found returns -2.
1965 */
1966 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001967get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968{
1969 hashtab_T *ht;
1970 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001971 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001972 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 int idx;
1974
Bram Moolenaare3d46852020-08-29 13:39:17 +02001975 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001977 if (sid == current_sctx.sc_sid)
1978 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001979 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001980
1981 if (sav == NULL)
1982 return -2;
1983 idx = sav->sav_var_vals_idx;
1984 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1985 if (check_writable && sv->sv_const)
1986 semsg(_(e_readonlyvar), name);
1987 return idx;
1988 }
1989
1990 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 ht = &SCRIPT_VARS(sid);
1992 di = find_var_in_ht(ht, 0, name, TRUE);
1993 if (di == NULL)
1994 return -2;
1995
1996 // Now find the svar_T index in sn_var_vals.
1997 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1998 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001999 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 if (sv->sv_tv == &di->di_tv)
2001 {
2002 if (check_writable && sv->sv_const)
2003 semsg(_(e_readonlyvar), name);
2004 return idx;
2005 }
2006 }
2007 return -1;
2008}
2009
2010/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002011 * Find "name" in imported items of the current script or in "cctx" if not
2012 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 */
2014 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002015find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 int idx;
2018
Bram Moolenaare3d46852020-08-29 13:39:17 +02002019 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002020 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 if (cctx != NULL)
2022 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2023 {
2024 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2025 + idx;
2026
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002027 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2028 : STRLEN(import->imp_name) == len
2029 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 return import;
2031 }
2032
Bram Moolenaarefa94442020-08-08 22:16:00 +02002033 return find_imported_in_script(name, len, current_sctx.sc_sid);
2034}
2035
2036 imported_T *
2037find_imported_in_script(char_u *name, size_t len, int sid)
2038{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002039 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002040 int idx;
2041
Bram Moolenaare3d46852020-08-29 13:39:17 +02002042 if (!SCRIPT_ID_VALID(sid))
2043 return NULL;
2044 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2046 {
2047 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2048
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002049 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2050 : STRLEN(import->imp_name) == len
2051 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return import;
2053 }
2054 return NULL;
2055}
2056
2057/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002058 * Free all imported variables.
2059 */
2060 static void
2061free_imported(cctx_T *cctx)
2062{
2063 int idx;
2064
2065 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2066 {
2067 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2068
2069 vim_free(import->imp_name);
2070 }
2071 ga_clear(&cctx->ctx_imports);
2072}
2073
2074/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002075 * Return TRUE if "p" points at a "#" but not at "#{".
2076 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002077 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002078vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002079{
2080 return p[0] == '#' && p[1] != '{';
2081}
2082
2083/*
2084 * Return a pointer to the next line that isn't empty or only contains a
2085 * comment. Skips over white space.
2086 * Returns NULL if there is none.
2087 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002088 char_u *
2089peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002090{
2091 int lnum = cctx->ctx_lnum;
2092
2093 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2094 {
2095 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002096 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002097
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002098 // ignore NULLs inserted for continuation lines
2099 if (line != NULL)
2100 {
2101 p = skipwhite(line);
2102 if (*p != NUL && !vim9_comment_start(p))
2103 return p;
2104 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002105 }
2106 return NULL;
2107}
2108
2109/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002110 * Called when checking for a following operator at "arg". When the rest of
2111 * the line is empty or only a comment, peek the next line. If there is a next
2112 * line return a pointer to it and set "nextp".
2113 * Otherwise skip over white space.
2114 */
2115 static char_u *
2116may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2117{
2118 char_u *p = skipwhite(arg);
2119
2120 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002121 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002122 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002123 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002124 if (*nextp != NULL)
2125 return *nextp;
2126 }
2127 return p;
2128}
2129
2130/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002131 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002132 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002133 * Returns NULL when at the end.
2134 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002135 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002136next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002137{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002138 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002139
2140 do
2141 {
2142 ++cctx->ctx_lnum;
2143 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002144 {
2145 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002146 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002147 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002148 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002149 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002150 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002151 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002152 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002153 return line;
2154}
2155
2156/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002157 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002158 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002159 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2160 */
2161 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002162may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002163{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002164 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002166 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002167
2168 if (next == NULL)
2169 return FAIL;
2170 *arg = skipwhite(next);
2171 }
2172 return OK;
2173}
2174
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002175/*
2176 * Idem, and give an error when failed.
2177 */
2178 static int
2179may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2180{
2181 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2182 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002183 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002184 return FAIL;
2185 }
2186 return OK;
2187}
2188
2189
Bram Moolenaara5565e42020-05-09 15:44:01 +02002190// Structure passed between the compile_expr* functions to keep track of
2191// constants that have been parsed but for which no code was produced yet. If
2192// possible expressions on these constants are applied at compile time. If
2193// that is not possible, the code to push the constants needs to be generated
2194// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002195// Using 50 should be more than enough of 5 levels of ().
2196#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002197typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002198 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002199 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002200 int pp_is_const; // all generated code was constants, used for a
2201 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002202} ppconst_T;
2203
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002204static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002205static int compile_expr0(char_u **arg, cctx_T *cctx);
2206static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2207
Bram Moolenaara5565e42020-05-09 15:44:01 +02002208/*
2209 * Generate a PUSH instruction for "tv".
2210 * "tv" will be consumed or cleared.
2211 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2212 */
2213 static int
2214generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2215{
2216 if (tv != NULL)
2217 {
2218 switch (tv->v_type)
2219 {
2220 case VAR_UNKNOWN:
2221 break;
2222 case VAR_BOOL:
2223 generate_PUSHBOOL(cctx, tv->vval.v_number);
2224 break;
2225 case VAR_SPECIAL:
2226 generate_PUSHSPEC(cctx, tv->vval.v_number);
2227 break;
2228 case VAR_NUMBER:
2229 generate_PUSHNR(cctx, tv->vval.v_number);
2230 break;
2231#ifdef FEAT_FLOAT
2232 case VAR_FLOAT:
2233 generate_PUSHF(cctx, tv->vval.v_float);
2234 break;
2235#endif
2236 case VAR_BLOB:
2237 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2238 tv->vval.v_blob = NULL;
2239 break;
2240 case VAR_STRING:
2241 generate_PUSHS(cctx, tv->vval.v_string);
2242 tv->vval.v_string = NULL;
2243 break;
2244 default:
2245 iemsg("constant type not supported");
2246 clear_tv(tv);
2247 return FAIL;
2248 }
2249 tv->v_type = VAR_UNKNOWN;
2250 }
2251 return OK;
2252}
2253
2254/*
2255 * Generate code for any ppconst entries.
2256 */
2257 static int
2258generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2259{
2260 int i;
2261 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002262 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002264 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002265 for (i = 0; i < ppconst->pp_used; ++i)
2266 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2267 ret = FAIL;
2268 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002269 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002270 return ret;
2271}
2272
2273/*
2274 * Clear ppconst constants. Used when failing.
2275 */
2276 static void
2277clear_ppconst(ppconst_T *ppconst)
2278{
2279 int i;
2280
2281 for (i = 0; i < ppconst->pp_used; ++i)
2282 clear_tv(&ppconst->pp_tv[i]);
2283 ppconst->pp_used = 0;
2284}
2285
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002286/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002287 * Generate an instruction to load script-local variable "name", without the
2288 * leading "s:".
2289 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 */
2291 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002292compile_load_scriptvar(
2293 cctx_T *cctx,
2294 char_u *name, // variable NUL terminated
2295 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002296 char_u **end, // end of variable
2297 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002299 scriptitem_T *si;
2300 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 imported_T *import;
2302
Bram Moolenaare3d46852020-08-29 13:39:17 +02002303 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2304 return FAIL;
2305 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002306 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002307 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002309 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002310 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2311 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 }
2313 if (idx >= 0)
2314 {
2315 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2316
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002317 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 current_sctx.sc_sid, idx, sv->sv_type);
2319 return OK;
2320 }
2321
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002322 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 if (import != NULL)
2324 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002325 if (import->imp_all)
2326 {
2327 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002328 char_u *exp_name;
2329 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002330 ufunc_T *ufunc;
2331 type_T *type;
2332
2333 // Used "import * as Name", need to lookup the member.
2334 if (*p != '.')
2335 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002336 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002337 return FAIL;
2338 }
2339 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002340 if (VIM_ISWHITE(*p))
2341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002342 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002343 return FAIL;
2344 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002345
Bram Moolenaar1c991142020-07-04 13:15:31 +02002346 // isolate one name
2347 exp_name = p;
2348 while (eval_isnamec(*p))
2349 ++p;
2350 cc = *p;
2351 *p = NUL;
2352
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002353 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002354 *p = cc;
2355 p = skipwhite(p);
2356
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002357 // TODO: what if it is a function?
2358 if (idx < 0)
2359 return FAIL;
2360 *end = p;
2361
2362 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2363 import->imp_sid,
2364 idx,
2365 type);
2366 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002367 else if (import->imp_funcname != NULL)
2368 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002369 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002370 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2371 import->imp_sid,
2372 import->imp_var_vals_idx,
2373 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 return OK;
2375 }
2376
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002377 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002378 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 return FAIL;
2380}
2381
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002382 static int
2383generate_funcref(cctx_T *cctx, char_u *name)
2384{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002385 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002386
2387 if (ufunc == NULL)
2388 return FAIL;
2389
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002390 // Need to compile any default values to get the argument types.
2391 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2392 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2393 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002394 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002395}
2396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397/*
2398 * Compile a variable name into a load instruction.
2399 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002400 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 * When "error" is FALSE do not give an error when not found.
2402 */
2403 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002404compile_load(
2405 char_u **arg,
2406 char_u *end_arg,
2407 cctx_T *cctx,
2408 int is_expr,
2409 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410{
2411 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002412 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002413 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002415 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416
2417 if (*(*arg + 1) == ':')
2418 {
2419 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002420 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002421 {
2422 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002424 switch (**arg)
2425 {
2426 case 'g': isn_type = ISN_LOADGDICT; break;
2427 case 'w': isn_type = ISN_LOADWDICT; break;
2428 case 't': isn_type = ISN_LOADTDICT; break;
2429 case 'b': isn_type = ISN_LOADBDICT; break;
2430 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002431 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002432 goto theend;
2433 }
2434 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2435 goto theend;
2436 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 else
2439 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002440 isntype_T isn_type = ISN_DROP;
2441
2442 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2443 if (name == NULL)
2444 return FAIL;
2445
2446 switch (**arg)
2447 {
2448 case 'v': res = generate_LOADV(cctx, name, error);
2449 break;
2450 case 's': res = compile_load_scriptvar(cctx, name,
2451 NULL, NULL, error);
2452 break;
2453 case 'g': isn_type = ISN_LOADG; break;
2454 case 'w': isn_type = ISN_LOADW; break;
2455 case 't': isn_type = ISN_LOADT; break;
2456 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002457 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002458 goto theend;
2459 }
2460 if (isn_type != ISN_DROP)
2461 {
2462 // Global, Buffer-local, Window-local and Tabpage-local
2463 // variables can be defined later, thus we don't check if it
2464 // exists, give error at runtime.
2465 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 }
2468 }
2469 else
2470 {
2471 size_t len = end - *arg;
2472 int idx;
2473 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002474 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475
2476 name = vim_strnsave(*arg, end - *arg);
2477 if (name == NULL)
2478 return FAIL;
2479
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002480 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002482 if (!gen_load_outer)
2483 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 }
2485 else
2486 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002487 lvar_T *lvar = lookup_local(*arg, len, cctx);
2488
2489 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002491 type = lvar->lv_type;
2492 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002493 if (lvar->lv_from_outer)
2494 gen_load_outer = TRUE;
2495 else
2496 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498 else
2499 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002500 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002501 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002503 || find_imported(name, 0, cctx) != NULL)
2504 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002505
Bram Moolenaar0f769812020-09-12 18:32:34 +02002506 // When evaluating an expression and the name starts with an
2507 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002508 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002509 if (res == FAIL && is_expr
2510 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002511 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 }
2513 }
2514 if (gen_load)
2515 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002516 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002517 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002518 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002519 cctx->ctx_outer_used = TRUE;
2520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 }
2522
2523 *arg = end;
2524
2525theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002526 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002527 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 vim_free(name);
2529 return res;
2530}
2531
2532/*
2533 * Compile the argument expressions.
2534 * "arg" points to just after the "(" and is advanced to after the ")"
2535 */
2536 static int
2537compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2538{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002539 char_u *p = *arg;
2540 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002541 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542
Bram Moolenaare6085c52020-04-12 20:19:16 +02002543 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002545 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2546 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002547 if (*p == ')')
2548 {
2549 *arg = p + 1;
2550 return OK;
2551 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002552 if (must_end)
2553 {
2554 semsg(_(e_missing_comma_before_argument_str), p);
2555 return FAIL;
2556 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002557
Bram Moolenaara5565e42020-05-09 15:44:01 +02002558 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 return FAIL;
2560 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002561
2562 if (*p != ',' && *skipwhite(p) == ',')
2563 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002564 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002565 p = skipwhite(p);
2566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002568 {
2569 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002570 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002571 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002572 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002573 else
2574 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002575 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002576 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002578failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002579 emsg(_(e_missing_close));
2580 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581}
2582
2583/*
2584 * Compile a function call: name(arg1, arg2)
2585 * "arg" points to "name", "arg + varlen" to the "(".
2586 * "argcount_init" is 1 for "value->method()"
2587 * Instructions:
2588 * EVAL arg1
2589 * EVAL arg2
2590 * BCALL / DCALL / UCALL
2591 */
2592 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002593compile_call(
2594 char_u **arg,
2595 size_t varlen,
2596 cctx_T *cctx,
2597 ppconst_T *ppconst,
2598 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599{
2600 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002601 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 int argcount = argcount_init;
2603 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002604 char_u fname_buf[FLEN_FIXED + 1];
2605 char_u *tofree = NULL;
2606 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002608 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002609 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610
Bram Moolenaara5565e42020-05-09 15:44:01 +02002611 // we can evaluate "has('name')" at compile time
2612 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2613 {
2614 char_u *s = skipwhite(*arg + varlen + 1);
2615 typval_T argvars[2];
2616
2617 argvars[0].v_type = VAR_UNKNOWN;
2618 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002619 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002620 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002621 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002622 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002623 if (*s == ')' && argvars[0].v_type == VAR_STRING
2624 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002625 {
2626 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2627
2628 *arg = s + 1;
2629 argvars[1].v_type = VAR_UNKNOWN;
2630 tv->v_type = VAR_NUMBER;
2631 tv->vval.v_number = 0;
2632 f_has(argvars, tv);
2633 clear_tv(&argvars[0]);
2634 ++ppconst->pp_used;
2635 return OK;
2636 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002637 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002638 }
2639
2640 if (generate_ppconst(cctx, ppconst) == FAIL)
2641 return FAIL;
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 if (varlen >= sizeof(namebuf))
2644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002645 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 return FAIL;
2647 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002648 vim_strncpy(namebuf, *arg, varlen);
2649 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
2651 *arg = skipwhite(*arg + varlen + 1);
2652 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002653 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654
Bram Moolenaara1773442020-08-12 15:21:22 +02002655 is_autoload = vim_strchr(name, '#') != NULL;
2656 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 {
2658 int idx;
2659
2660 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002661 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002663 {
2664 if (STRCMP(name, "add") == 0 && argcount == 2)
2665 {
2666 garray_T *stack = &cctx->ctx_type_stack;
2667 type_T *type = ((type_T **)stack->ga_data)[
2668 stack->ga_len - 2];
2669
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002670 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002671 if (type->tt_type == VAR_LIST)
2672 {
2673 // inline "add(list, item)" so that the type can be checked
2674 res = generate_LISTAPPEND(cctx);
2675 idx = -1;
2676 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002677 else if (type->tt_type == VAR_BLOB)
2678 {
2679 // inline "add(blob, nr)" so that the type can be checked
2680 res = generate_BLOBAPPEND(cctx);
2681 idx = -1;
2682 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002683 }
2684
2685 if (idx >= 0)
2686 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2687 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002688 else
2689 semsg(_(e_unknownfunc), namebuf);
2690 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 }
2692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002694 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002695 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002696 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002697 {
2698 res = generate_CALL(cctx, ufunc, argcount);
2699 goto theend;
2700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002703 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002704 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002706 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002707 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002708 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002709 garray_T *stack = &cctx->ctx_type_stack;
2710 type_T *type;
2711
2712 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2713 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002714 goto theend;
2715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
Bram Moolenaar0f769812020-09-12 18:32:34 +02002717 // If we can find a global function by name generate the right call.
2718 if (ufunc != NULL)
2719 {
2720 res = generate_CALL(cctx, ufunc, argcount);
2721 goto theend;
2722 }
2723
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002724 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002725 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002726 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002727 res = generate_UCALL(cctx, name, argcount);
2728 else
2729 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002730
2731theend:
2732 vim_free(tofree);
2733 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734}
2735
2736// like NAMESPACE_CHAR but with 'a' and 'l'.
2737#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2738
2739/*
2740 * Find the end of a variable or function name. Unlike find_name_end() this
2741 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002742 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 * Return a pointer to just after the name. Equal to "arg" if there is no
2744 * valid name.
2745 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002746 static char_u *
2747to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748{
2749 char_u *p;
2750
2751 // Quick check for valid starting character.
2752 if (!eval_isnamec1(*arg))
2753 return arg;
2754
2755 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2756 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2757 // and can be used in slice "[n:]".
2758 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002759 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2761 break;
2762 return p;
2763}
2764
2765/*
2766 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002767 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 */
2769 char_u *
2770to_name_const_end(char_u *arg)
2771{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002772 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 typval_T rettv;
2774
2775 if (p == arg && *arg == '[')
2776 {
2777
2778 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002779 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 p = arg;
2781 }
2782 else if (p == arg && *arg == '#' && arg[1] == '{')
2783 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002784 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002786 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 p = arg;
2788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 return p;
2791}
2792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793/*
2794 * parse a list: [expr, expr]
2795 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002796 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 */
2798 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002799compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800{
2801 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002802 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002804 int is_const;
2805 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002807 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002809 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002810 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002811 semsg(_(e_list_end), *arg);
2812 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002813 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002814 if (*p == ',')
2815 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002816 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002817 return FAIL;
2818 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002819 if (*p == ']')
2820 {
2821 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002822 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002823 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002824 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002825 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002826 if (!is_const)
2827 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 ++count;
2829 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002830 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002832 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2833 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002834 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002835 return FAIL;
2836 }
2837 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002838 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 p = skipwhite(p);
2840 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002841 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002843 ppconst->pp_is_const = is_all_const;
2844 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845}
2846
2847/*
2848 * parse a lambda: {arg, arg -> expr}
2849 * "*arg" points to the '{'.
2850 */
2851 static int
2852compile_lambda(char_u **arg, cctx_T *cctx)
2853{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 typval_T rettv;
2855 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002856 evalarg_T evalarg;
2857
2858 CLEAR_FIELD(evalarg);
2859 evalarg.eval_flags = EVAL_EVALUATE;
2860 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861
2862 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002863 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002864 {
2865 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002867 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002870 ++ufunc->uf_refcount;
2871 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872
2873 // The function will have one line: "return {expr}".
2874 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002875 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002877 clear_evalarg(&evalarg, NULL);
2878
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002879 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002880 {
2881 // The return type will now be known.
2882 set_function_type(ufunc);
2883
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002884 // The function reference count will be 1. When the ISN_FUNCREF
2885 // instruction is deleted the reference count is decremented and the
2886 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002887 return generate_FUNCREF(cctx, ufunc);
2888 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002889
2890 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 return FAIL;
2892}
2893
2894/*
2895 * Compile a lamda call: expr->{lambda}(args)
2896 * "arg" points to the "{".
2897 */
2898 static int
2899compile_lambda_call(char_u **arg, cctx_T *cctx)
2900{
2901 ufunc_T *ufunc;
2902 typval_T rettv;
2903 int argcount = 1;
2904 int ret = FAIL;
2905
2906 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002907 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 return FAIL;
2909
2910 if (**arg != '(')
2911 {
2912 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002913 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 else
2915 semsg(_(e_missing_paren), "lambda");
2916 clear_tv(&rettv);
2917 return FAIL;
2918 }
2919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 ufunc = rettv.vval.v_partial->pt_func;
2921 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002922 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002923 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002924
Bram Moolenaara05e5242020-09-19 18:19:19 +02002925 // The function will have one line: "return {expr}". Compile it into
2926 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002927 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928
2929 // compile the arguments
2930 *arg = skipwhite(*arg + 1);
2931 if (compile_arguments(arg, cctx, &argcount) == OK)
2932 // call the compiled function
2933 ret = generate_CALL(cctx, ufunc, argcount);
2934
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002935 if (ret == FAIL)
2936 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 return ret;
2938}
2939
2940/*
2941 * parse a dict: {'key': val} or #{key: val}
2942 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002943 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 */
2945 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002946compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947{
2948 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 int count = 0;
2951 dict_T *d = dict_alloc();
2952 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002953 char_u *whitep = *arg;
2954 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002955 int is_const;
2956 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
2958 if (d == NULL)
2959 return FAIL;
2960 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002961 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 {
2963 char_u *key = NULL;
2964
Bram Moolenaar23c55272020-06-21 16:58:13 +02002965 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002966 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002967 *arg = NULL;
2968 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002969 }
2970
2971 if (**arg == '}')
2972 break;
2973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 if (literal)
2975 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002976 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
Bram Moolenaar2c330432020-04-13 14:41:35 +02002978 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002980 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 return FAIL;
2982 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002983 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 if (generate_PUSHS(cctx, key) == FAIL)
2985 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002986 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 }
2988 else
2989 {
2990 isn_T *isn;
2991
Bram Moolenaara5565e42020-05-09 15:44:01 +02002992 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2995 if (isn->isn_type == ISN_PUSHS)
2996 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002997 else
2998 {
2999 type_T *keytype = ((type_T **)stack->ga_data)
3000 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003001 if (need_type(keytype, &t_string, -1, cctx,
3002 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003003 return FAIL;
3004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 }
3006
3007 // Check for duplicate keys, if using string keys.
3008 if (key != NULL)
3009 {
3010 item = dict_find(d, key, -1);
3011 if (item != NULL)
3012 {
3013 semsg(_(e_duplicate_key), key);
3014 goto failret;
3015 }
3016 item = dictitem_alloc(key);
3017 if (item != NULL)
3018 {
3019 item->di_tv.v_type = VAR_UNKNOWN;
3020 item->di_tv.v_lock = 0;
3021 if (dict_add(d, item) == FAIL)
3022 dictitem_free(item);
3023 }
3024 }
3025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 if (**arg != ':')
3027 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003028 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003029 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003030 else
3031 semsg(_(e_missing_dict_colon), *arg);
3032 return FAIL;
3033 }
3034 whitep = *arg + 1;
3035 if (!IS_WHITE_OR_NUL(*whitep))
3036 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003037 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 return FAIL;
3039 }
3040
3041 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003042 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003043 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003044 *arg = NULL;
3045 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003046 }
3047
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003048 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003050 if (!is_const)
3051 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 ++count;
3053
Bram Moolenaar2c330432020-04-13 14:41:35 +02003054 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003055 *arg = skipwhite(*arg);
3056 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003057 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003058 *arg = NULL;
3059 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (**arg == '}')
3062 break;
3063 if (**arg != ',')
3064 {
3065 semsg(_(e_missing_dict_comma), *arg);
3066 goto failret;
3067 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003068 if (IS_WHITE_OR_NUL(*whitep))
3069 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003070 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003071 return FAIL;
3072 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003073 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003074 if (!IS_WHITE_OR_NUL(*whitep))
3075 {
3076 semsg(_(e_white_space_required_after_str), ",");
3077 return FAIL;
3078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 *arg = skipwhite(*arg + 1);
3080 }
3081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 *arg = *arg + 1;
3083
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003084 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003085 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003086 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003087 *arg += STRLEN(*arg);
3088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003090 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 return generate_NEWDICT(cctx, count);
3092
3093failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003094 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003095 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003096 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003097 *arg = (char_u *)"";
3098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 dict_unref(d);
3100 return FAIL;
3101}
3102
3103/*
3104 * Compile "&option".
3105 */
3106 static int
3107compile_get_option(char_u **arg, cctx_T *cctx)
3108{
3109 typval_T rettv;
3110 char_u *start = *arg;
3111 int ret;
3112
3113 // parse the option and get the current value to get the type.
3114 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003115 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 if (ret == OK)
3117 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003118 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 char_u *name = vim_strnsave(start, *arg - start);
3120 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3121
3122 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3123 vim_free(name);
3124 }
3125 clear_tv(&rettv);
3126
3127 return ret;
3128}
3129
3130/*
3131 * Compile "$VAR".
3132 */
3133 static int
3134compile_get_env(char_u **arg, cctx_T *cctx)
3135{
3136 char_u *start = *arg;
3137 int len;
3138 int ret;
3139 char_u *name;
3140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 ++*arg;
3142 len = get_env_len(arg);
3143 if (len == 0)
3144 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003145 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 return FAIL;
3147 }
3148
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003149 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 name = vim_strnsave(start, len + 1);
3151 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3152 vim_free(name);
3153 return ret;
3154}
3155
3156/*
3157 * Compile "@r".
3158 */
3159 static int
3160compile_get_register(char_u **arg, cctx_T *cctx)
3161{
3162 int ret;
3163
3164 ++*arg;
3165 if (**arg == NUL)
3166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003167 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 return FAIL;
3169 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003170 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 {
3172 emsg_invreg(**arg);
3173 return FAIL;
3174 }
3175 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3176 ++*arg;
3177 return ret;
3178}
3179
3180/*
3181 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003182 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 */
3184 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003185apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003187 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188
3189 // this works from end to start
3190 while (p > start)
3191 {
3192 --p;
3193 if (*p == '-' || *p == '+')
3194 {
3195 // only '-' has an effect, for '+' we only check the type
3196#ifdef FEAT_FLOAT
3197 if (rettv->v_type == VAR_FLOAT)
3198 {
3199 if (*p == '-')
3200 rettv->vval.v_float = -rettv->vval.v_float;
3201 }
3202 else
3203#endif
3204 {
3205 varnumber_T val;
3206 int error = FALSE;
3207
3208 // tv_get_number_chk() accepts a string, but we don't want that
3209 // here
3210 if (check_not_string(rettv) == FAIL)
3211 return FAIL;
3212 val = tv_get_number_chk(rettv, &error);
3213 clear_tv(rettv);
3214 if (error)
3215 return FAIL;
3216 if (*p == '-')
3217 val = -val;
3218 rettv->v_type = VAR_NUMBER;
3219 rettv->vval.v_number = val;
3220 }
3221 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003222 else if (numeric_only)
3223 {
3224 ++p;
3225 break;
3226 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003227 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
3229 int v = tv2bool(rettv);
3230
3231 // '!' is permissive in the type.
3232 clear_tv(rettv);
3233 rettv->v_type = VAR_BOOL;
3234 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3235 }
3236 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003237 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 return OK;
3239}
3240
3241/*
3242 * Recognize v: variables that are constants and set "rettv".
3243 */
3244 static void
3245get_vim_constant(char_u **arg, typval_T *rettv)
3246{
3247 if (STRNCMP(*arg, "v:true", 6) == 0)
3248 {
3249 rettv->v_type = VAR_BOOL;
3250 rettv->vval.v_number = VVAL_TRUE;
3251 *arg += 6;
3252 }
3253 else if (STRNCMP(*arg, "v:false", 7) == 0)
3254 {
3255 rettv->v_type = VAR_BOOL;
3256 rettv->vval.v_number = VVAL_FALSE;
3257 *arg += 7;
3258 }
3259 else if (STRNCMP(*arg, "v:null", 6) == 0)
3260 {
3261 rettv->v_type = VAR_SPECIAL;
3262 rettv->vval.v_number = VVAL_NULL;
3263 *arg += 6;
3264 }
3265 else if (STRNCMP(*arg, "v:none", 6) == 0)
3266 {
3267 rettv->v_type = VAR_SPECIAL;
3268 rettv->vval.v_number = VVAL_NONE;
3269 *arg += 6;
3270 }
3271}
3272
Bram Moolenaar696ba232020-07-29 21:20:41 +02003273 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003274get_compare_type(char_u *p, int *len, int *type_is)
3275{
3276 exptype_T type = EXPR_UNKNOWN;
3277 int i;
3278
3279 switch (p[0])
3280 {
3281 case '=': if (p[1] == '=')
3282 type = EXPR_EQUAL;
3283 else if (p[1] == '~')
3284 type = EXPR_MATCH;
3285 break;
3286 case '!': if (p[1] == '=')
3287 type = EXPR_NEQUAL;
3288 else if (p[1] == '~')
3289 type = EXPR_NOMATCH;
3290 break;
3291 case '>': if (p[1] != '=')
3292 {
3293 type = EXPR_GREATER;
3294 *len = 1;
3295 }
3296 else
3297 type = EXPR_GEQUAL;
3298 break;
3299 case '<': if (p[1] != '=')
3300 {
3301 type = EXPR_SMALLER;
3302 *len = 1;
3303 }
3304 else
3305 type = EXPR_SEQUAL;
3306 break;
3307 case 'i': if (p[1] == 's')
3308 {
3309 // "is" and "isnot"; but not a prefix of a name
3310 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3311 *len = 5;
3312 i = p[*len];
3313 if (!isalnum(i) && i != '_')
3314 {
3315 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3316 *type_is = TRUE;
3317 }
3318 }
3319 break;
3320 }
3321 return type;
3322}
3323
3324/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003326 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 */
3328 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003329compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003331 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332
3333 // this works from end to start
3334 while (p > start)
3335 {
3336 --p;
3337 if (*p == '-' || *p == '+')
3338 {
3339 int negate = *p == '-';
3340 isn_T *isn;
3341
3342 // TODO: check type
3343 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3344 {
3345 --p;
3346 if (*p == '-')
3347 negate = !negate;
3348 }
3349 // only '-' has an effect, for '+' we only check the type
3350 if (negate)
3351 isn = generate_instr(cctx, ISN_NEGATENR);
3352 else
3353 isn = generate_instr(cctx, ISN_CHECKNR);
3354 if (isn == NULL)
3355 return FAIL;
3356 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003357 else if (numeric_only)
3358 {
3359 ++p;
3360 break;
3361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 else
3363 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003364 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003366 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003368 if (p[-1] == '!')
3369 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 }
3372 if (generate_2BOOL(cctx, invert) == FAIL)
3373 return FAIL;
3374 }
3375 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003376 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 return OK;
3378}
3379
3380/*
3381 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003382 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 */
3384 static int
3385compile_subscript(
3386 char_u **arg,
3387 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003388 char_u *start_leader,
3389 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003390 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003392 char_u *name_start = *end_leader;
3393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 for (;;)
3395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003396 char_u *p = skipwhite(*arg);
3397
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003398 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003399 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003400 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003401
3402 // If a following line starts with "->{" or "->X" advance to that
3403 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003404 // Also if a following line starts with ".x".
3405 if (next != NULL &&
3406 ((next[0] == '-' && next[1] == '>'
3407 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003408 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003409 {
3410 next = next_line_from_context(cctx, TRUE);
3411 if (next == NULL)
3412 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003413 *arg = next;
3414 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003415 }
3416 }
3417
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003418 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3419 // not a function call.
3420 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003422 garray_T *stack = &cctx->ctx_type_stack;
3423 type_T *type;
3424 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003426 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003427 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003428 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003431 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3432
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003433 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3435 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003436 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 return FAIL;
3438 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003439 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003441 char_u *pstart = p;
3442
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003443 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003444 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003445 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 // something->method()
3448 // Apply the '!', '-' and '+' first:
3449 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003450 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003453 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003454 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003455 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (**arg == '{')
3457 {
3458 // lambda call: list->{lambda}
3459 if (compile_lambda_call(arg, cctx) == FAIL)
3460 return FAIL;
3461 }
3462 else
3463 {
3464 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003465 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003466 if (!eval_isnamec1(*p))
3467 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003468 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003469 return FAIL;
3470 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003471 if (ASCII_ISALPHA(*p) && p[1] == ':')
3472 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003473 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 ;
3475 if (*p != '(')
3476 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003477 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 return FAIL;
3479 }
3480 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003481 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 return FAIL;
3483 }
3484 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003485 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003487 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003488 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003489 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003490 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003491 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003492
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003493 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003494 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003495 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003496 // TODO: blob index
3497 // TODO: more arguments
3498 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003499 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003500 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003501 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003502
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003503 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003504 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003505 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003506 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003507 if (**arg == ':')
3508 // missing first index is equal to zero
3509 generate_PUSHNR(cctx, 0);
3510 else
3511 {
3512 if (compile_expr0(arg, cctx) == FAIL)
3513 return FAIL;
3514 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3515 return FAIL;
3516 *arg = skipwhite(*arg);
3517 }
3518 if (**arg == ':')
3519 {
3520 *arg = skipwhite(*arg + 1);
3521 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3522 return FAIL;
3523 if (**arg == ']')
3524 // missing second index is equal to end of string
3525 generate_PUSHNR(cctx, -1);
3526 else
3527 {
3528 if (compile_expr0(arg, cctx) == FAIL)
3529 return FAIL;
3530 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3531 return FAIL;
3532 *arg = skipwhite(*arg);
3533 }
3534 is_slice = TRUE;
3535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536
3537 if (**arg != ']')
3538 {
3539 emsg(_(e_missbrac));
3540 return FAIL;
3541 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003542 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003544 // We can index a list and a dict. If we don't know the type
3545 // we can use the index value type.
3546 // TODO: If we don't know use an instruction to figure it out at
3547 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003548 typep = ((type_T **)stack->ga_data) + stack->ga_len
3549 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003550 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003551 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3552 // If the index is a string, the variable must be a Dict.
3553 if (*typep == &t_any && valtype == &t_string)
3554 vtype = VAR_DICT;
3555 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003556 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003557 if (need_type(valtype, &t_number, -1, cctx,
3558 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003559 return FAIL;
3560 if (is_slice)
3561 {
3562 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003563 if (need_type(valtype, &t_number, -2, cctx,
3564 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003565 return FAIL;
3566 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003567 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003568
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003569 if (vtype == VAR_DICT)
3570 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003571 if (is_slice)
3572 {
3573 emsg(_(e_cannot_slice_dictionary));
3574 return FAIL;
3575 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003576 if ((*typep)->tt_type == VAR_DICT)
3577 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003578 else
3579 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003580 if (need_type(*typep, &t_dict_any, -2, cctx,
3581 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003582 return FAIL;
3583 *typep = &t_any;
3584 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003585 if (may_generate_2STRING(-1, cctx) == FAIL)
3586 return FAIL;
3587 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3588 return FAIL;
3589 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003590 else if (vtype == VAR_STRING)
3591 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003592 *typep = &t_string;
3593 if ((is_slice
3594 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3595 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003596 return FAIL;
3597 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003598 else if (vtype == VAR_BLOB)
3599 {
3600 emsg("Sorry, blob index and slice not implemented yet");
3601 return FAIL;
3602 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003603 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003604 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003605 if (is_slice)
3606 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003607 if (generate_instr_drop(cctx,
3608 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3609 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003610 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003611 }
Bram Moolenaared591872020-08-15 22:14:53 +02003612 else
3613 {
3614 if ((*typep)->tt_type == VAR_LIST)
3615 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003616 if (generate_instr_drop(cctx,
3617 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3618 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003619 return FAIL;
3620 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003621 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003622 else
3623 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003624 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003625 return FAIL;
3626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003628 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003630 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003631 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003632 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003633 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003634
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003635 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003636 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003637 {
3638 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003639 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003640 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003641 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003642 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 while (eval_isnamec(*p))
3644 MB_PTR_ADV(p);
3645 if (p == *arg)
3646 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003647 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 return FAIL;
3649 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003650 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 return FAIL;
3652 *arg = p;
3653 }
3654 else
3655 break;
3656 }
3657
3658 // TODO - see handle_subscript():
3659 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3660 // Don't do this when "Func" is already a partial that was bound
3661 // explicitly (pt_auto is FALSE).
3662
3663 return OK;
3664}
3665
3666/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3668 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003670 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003671 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003672 *
3673 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 */
3675
3676/*
3677 * number number constant
3678 * 0zFFFFFFFF Blob constant
3679 * "string" string constant
3680 * 'string' literal string constant
3681 * &option-name option value
3682 * @r register contents
3683 * identifier variable value
3684 * function() function call
3685 * $VAR environment variable
3686 * (expression) nested expression
3687 * [expr, expr] List
3688 * {key: val, key: val} Dictionary
3689 * #{key: val, key: val} Dictionary with literal keys
3690 *
3691 * Also handle:
3692 * ! in front logical NOT
3693 * - in front unary minus
3694 * + in front unary plus (ignored)
3695 * trailing (arg) funcref/partial call
3696 * trailing [] subscript in String or List
3697 * trailing .name entry in Dictionary
3698 * trailing ->name() method call
3699 */
3700 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701compile_expr7(
3702 char_u **arg,
3703 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003704 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 char_u *start_leader, *end_leader;
3707 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003708 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003709 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003711 ppconst->pp_is_const = FALSE;
3712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 /*
3714 * Skip '!', '-' and '+' characters. They are handled later.
3715 */
3716 start_leader = *arg;
3717 while (**arg == '!' || **arg == '-' || **arg == '+')
3718 *arg = skipwhite(*arg + 1);
3719 end_leader = *arg;
3720
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003721 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 switch (**arg)
3723 {
3724 /*
3725 * Number constant.
3726 */
3727 case '0': // also for blob starting with 0z
3728 case '1':
3729 case '2':
3730 case '3':
3731 case '4':
3732 case '5':
3733 case '6':
3734 case '7':
3735 case '8':
3736 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003737 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003739 // Apply "-" and "+" just before the number now, right to
3740 // left. Matters especially when "->" follows. Stops at
3741 // '!'.
3742 if (apply_leader(rettv, TRUE,
3743 start_leader, &end_leader) == FAIL)
3744 {
3745 clear_tv(rettv);
3746 return FAIL;
3747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 break;
3749
3750 /*
3751 * String constant: "string".
3752 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003753 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 return FAIL;
3755 break;
3756
3757 /*
3758 * Literal string constant: 'str''ing'.
3759 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003760 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 return FAIL;
3762 break;
3763
3764 /*
3765 * Constant Vim variable.
3766 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003767 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 ret = NOTDONE;
3769 break;
3770
3771 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003772 * "true" constant
3773 */
3774 case 't': if (STRNCMP(*arg, "true", 4) == 0
3775 && !eval_isnamec((*arg)[4]))
3776 {
3777 *arg += 4;
3778 rettv->v_type = VAR_BOOL;
3779 rettv->vval.v_number = VVAL_TRUE;
3780 }
3781 else
3782 ret = NOTDONE;
3783 break;
3784
3785 /*
3786 * "false" constant
3787 */
3788 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3789 && !eval_isnamec((*arg)[5]))
3790 {
3791 *arg += 5;
3792 rettv->v_type = VAR_BOOL;
3793 rettv->vval.v_number = VVAL_FALSE;
3794 }
3795 else
3796 ret = NOTDONE;
3797 break;
3798
3799 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 * List: [expr, expr]
3801 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003802 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 break;
3804
3805 /*
3806 * Dictionary: #{key: val, key: val}
3807 */
3808 case '#': if ((*arg)[1] == '{')
3809 {
3810 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003811 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 }
3813 else
3814 ret = NOTDONE;
3815 break;
3816
3817 /*
3818 * Lambda: {arg, arg -> expr}
3819 * Dictionary: {'key': val, 'key': val}
3820 */
3821 case '{': {
3822 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003823 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
3825 // Find out what comes after the arguments.
3826 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003827 &ga_arg, TRUE, NULL, NULL,
3828 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 if (ret != FAIL && *start == '>')
3830 ret = compile_lambda(arg, cctx);
3831 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003832 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834 break;
3835
3836 /*
3837 * Option value: &name
3838 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003839 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3840 return FAIL;
3841 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 break;
3843
3844 /*
3845 * Environment variable: $VAR.
3846 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003847 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3848 return FAIL;
3849 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851
3852 /*
3853 * Register contents: @r.
3854 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003855 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3856 return FAIL;
3857 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 break;
3859 /*
3860 * nested expression: (expression).
3861 */
3862 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003863
3864 // recursive!
3865 if (ppconst->pp_used <= PPSIZE - 10)
3866 {
3867 ret = compile_expr1(arg, cctx, ppconst);
3868 }
3869 else
3870 {
3871 // Not enough space in ppconst, flush constants.
3872 if (generate_ppconst(cctx, ppconst) == FAIL)
3873 return FAIL;
3874 ret = compile_expr0(arg, cctx);
3875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 *arg = skipwhite(*arg);
3877 if (**arg == ')')
3878 ++*arg;
3879 else if (ret == OK)
3880 {
3881 emsg(_(e_missing_close));
3882 ret = FAIL;
3883 }
3884 break;
3885
3886 default: ret = NOTDONE;
3887 break;
3888 }
3889 if (ret == FAIL)
3890 return FAIL;
3891
Bram Moolenaar1c747212020-05-09 18:28:34 +02003892 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003894 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895 clear_tv(rettv);
3896 else
3897 // A constant expression can possibly be handled compile time,
3898 // return the value instead of generating code.
3899 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 }
3901 else if (ret == NOTDONE)
3902 {
3903 char_u *p;
3904 int r;
3905
3906 if (!eval_isnamec1(**arg))
3907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003908 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 return FAIL;
3910 }
3911
3912 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003913 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003915 {
3916 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 {
3920 if (generate_ppconst(cctx, ppconst) == FAIL)
3921 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003922 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 if (r == FAIL)
3925 return FAIL;
3926 }
3927
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003928 // Handle following "[]", ".member", etc.
3929 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003930 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931 ppconst) == FAIL)
3932 return FAIL;
3933 if (ppconst->pp_used > 0)
3934 {
3935 // apply the '!', '-' and '+' before the constant
3936 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003937 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938 return FAIL;
3939 return OK;
3940 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003941 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003943 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944}
3945
3946/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003947 * Give the "white on both sides" error, taking the operator from "p[len]".
3948 */
3949 void
3950error_white_both(char_u *op, int len)
3951{
3952 char_u buf[10];
3953
3954 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003955 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003956}
3957
3958/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003959 * <type>expr7: runtime type check / conversion
3960 */
3961 static int
3962compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3963{
3964 type_T *want_type = NULL;
3965
3966 // Recognize <type>
3967 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3968 {
3969 int called_emsg_before = called_emsg;
3970
3971 ++*arg;
3972 want_type = parse_type(arg, cctx->ctx_type_list);
3973 if (called_emsg != called_emsg_before)
3974 return FAIL;
3975
3976 if (**arg != '>')
3977 {
3978 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003979 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003980 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003981 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003982 return FAIL;
3983 }
3984 ++*arg;
3985 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3986 return FAIL;
3987 }
3988
3989 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3990 return FAIL;
3991
3992 if (want_type != NULL)
3993 {
3994 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003995 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003996
Bram Moolenaard1103582020-08-14 22:44:25 +02003997 generate_ppconst(cctx, ppconst);
3998 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003999 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004000 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004001 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004002 return FAIL;
4003 }
4004 }
4005
4006 return OK;
4007}
4008
4009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 * * number multiplication
4011 * / number division
4012 * % number modulo
4013 */
4014 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016{
4017 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004018 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004019 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004021 // get the first 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;
4024
4025 /*
4026 * Repeat computing, until no "*", "/" or "%" is following.
4027 */
4028 for (;;)
4029 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004030 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 if (*op != '*' && *op != '/' && *op != '%')
4032 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004033 if (next != NULL)
4034 {
4035 *arg = next_line_from_context(cctx, TRUE);
4036 op = skipwhite(*arg);
4037 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004038
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004039 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004041 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004045 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004046 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004048 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004049 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004051
4052 if (ppconst->pp_used == ppconst_used + 2
4053 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4054 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004055 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4057 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004060 // both are numbers: compute the result
4061 switch (*op)
4062 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004063 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004064 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004065 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004066 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004068 break;
4069 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004070 tv1->vval.v_number = res;
4071 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004072 }
4073 else
4074 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004075 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004076 generate_two_op(cctx, op);
4077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 }
4079
4080 return OK;
4081}
4082
4083/*
4084 * + number addition
4085 * - number subtraction
4086 * .. string concatenation
4087 */
4088 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090{
4091 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004092 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004094 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095
4096 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004097 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 return FAIL;
4099
4100 /*
4101 * Repeat computing, until no "+", "-" or ".." is following.
4102 */
4103 for (;;)
4104 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004105 op = may_peek_next_line(cctx, *arg, &next);
4106 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 break;
4108 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004109 if (next != NULL)
4110 {
4111 *arg = next_line_from_context(cctx, TRUE);
4112 op = skipwhite(*arg);
4113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004115 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004117 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004118 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 }
4120
4121 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004122 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004123 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004125 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004126 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 return FAIL;
4128
Bram Moolenaara5565e42020-05-09 15:44:01 +02004129 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004130 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4132 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4133 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4134 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4137 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004139 // concat/subtract/add constant numbers
4140 if (*op == '+')
4141 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4142 else if (*op == '-')
4143 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4144 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004145 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004146 // concatenate constant strings
4147 char_u *s1 = tv1->vval.v_string;
4148 char_u *s2 = tv2->vval.v_string;
4149 size_t len1 = STRLEN(s1);
4150
4151 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4152 if (tv1->vval.v_string == NULL)
4153 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004154 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004155 return FAIL;
4156 }
4157 mch_memmove(tv1->vval.v_string, s1, len1);
4158 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159 vim_free(s1);
4160 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 }
4164 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 if (*op == '.')
4168 {
4169 if (may_generate_2STRING(-2, cctx) == FAIL
4170 || may_generate_2STRING(-1, cctx) == FAIL)
4171 return FAIL;
4172 generate_instr_drop(cctx, ISN_CONCAT, 1);
4173 }
4174 else
4175 generate_two_op(cctx, op);
4176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
4178
4179 return OK;
4180}
4181
4182/*
4183 * expr5a == expr5b
4184 * expr5a =~ expr5b
4185 * expr5a != expr5b
4186 * expr5a !~ expr5b
4187 * expr5a > expr5b
4188 * expr5a >= expr5b
4189 * expr5a < expr5b
4190 * expr5a <= expr5b
4191 * expr5a is expr5b
4192 * expr5a isnot expr5b
4193 *
4194 * Produces instructions:
4195 * EVAL expr5a Push result of "expr5a"
4196 * EVAL expr5b Push result of "expr5b"
4197 * COMPARE one of the compare instructions
4198 */
4199 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004200compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201{
4202 exptype_T type = EXPR_UNKNOWN;
4203 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004204 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004207 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208
4209 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 return FAIL;
4212
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004213 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004214 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
4216 /*
4217 * If there is a comparative operator, use it.
4218 */
4219 if (type != EXPR_UNKNOWN)
4220 {
4221 int ic = FALSE; // Default: do not ignore case
4222
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004223 if (next != NULL)
4224 {
4225 *arg = next_line_from_context(cctx, TRUE);
4226 p = skipwhite(*arg);
4227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (type_is && (p[len] == '?' || p[len] == '#'))
4229 {
4230 semsg(_(e_invexpr2), *arg);
4231 return FAIL;
4232 }
4233 // extra question mark appended: ignore case
4234 if (p[len] == '?')
4235 {
4236 ic = TRUE;
4237 ++len;
4238 }
4239 // extra '#' appended: match case (ignored)
4240 else if (p[len] == '#')
4241 ++len;
4242 // nothing appended: match case
4243
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004244 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004246 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004247 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 }
4249
4250 // get the second variable
4251 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004252 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004253 return FAIL;
4254
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 return FAIL;
4257
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 if (ppconst->pp_used == ppconst_used + 2)
4259 {
4260 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4261 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4262 int ret;
4263
4264 // Both sides are a constant, compute the result now.
4265 // First check for a valid combination of types, this is more
4266 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004267 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268 ret = FAIL;
4269 else
4270 {
4271 ret = typval_compare(tv1, tv2, type, ic);
4272 tv1->v_type = VAR_BOOL;
4273 tv1->vval.v_number = tv1->vval.v_number
4274 ? VVAL_TRUE : VVAL_FALSE;
4275 clear_tv(tv2);
4276 --ppconst->pp_used;
4277 }
4278 return ret;
4279 }
4280
4281 generate_ppconst(cctx, ppconst);
4282 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
4284
4285 return OK;
4286}
4287
Bram Moolenaar7f141552020-05-09 17:35:53 +02004288static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290/*
4291 * Compile || or &&.
4292 */
4293 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294compile_and_or(
4295 char_u **arg,
4296 cctx_T *cctx,
4297 char *op,
4298 ppconst_T *ppconst,
4299 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004301 char_u *next;
4302 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 int opchar = *op;
4304
4305 if (p[0] == opchar && p[1] == opchar)
4306 {
4307 garray_T *instr = &cctx->ctx_instr;
4308 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004309 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004310 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311
4312 /*
4313 * Repeat until there is no following "||" or "&&"
4314 */
4315 ga_init2(&end_ga, sizeof(int), 10);
4316 while (p[0] == opchar && p[1] == opchar)
4317 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004318 if (next != NULL)
4319 {
4320 *arg = next_line_from_context(cctx, TRUE);
4321 p = skipwhite(*arg);
4322 }
4323
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004324 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4325 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004326 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004327 return FAIL;
4328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004330 // TODO: use ppconst if the value is a constant and check
4331 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332 generate_ppconst(cctx, ppconst);
4333
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004334 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4335 all_bool_values = FALSE;
4336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 if (ga_grow(&end_ga, 1) == FAIL)
4338 {
4339 ga_clear(&end_ga);
4340 return FAIL;
4341 }
4342 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4343 ++end_ga.ga_len;
4344 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004345 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346
4347 // eval the next expression
4348 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004349 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004350 return FAIL;
4351
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4353 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 {
4355 ga_clear(&end_ga);
4356 return FAIL;
4357 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358
4359 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362
4363 // Fill in the end label in all jumps.
4364 while (end_ga.ga_len > 0)
4365 {
4366 isn_T *isn;
4367
4368 --end_ga.ga_len;
4369 isn = ((isn_T *)instr->ga_data)
4370 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4371 isn->isn_arg.jump.jump_where = instr->ga_len;
4372 }
4373 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004374
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004375 // The resulting type is converted to bool if needed.
4376 if (!all_bool_values)
4377 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379
4380 return OK;
4381}
4382
4383/*
4384 * expr4a && expr4a && expr4a logical AND
4385 *
4386 * Produces instructions:
4387 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004388 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004390 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004392 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 * end:
4394 */
4395 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 int ppconst_used = ppconst->pp_used;
4399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 return FAIL;
4403
4404 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406}
4407
4408/*
4409 * expr3a || expr3b || expr3c logical OR
4410 *
4411 * Produces instructions:
4412 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004413 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004415 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004417 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 * end:
4419 */
4420 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 int ppconst_used = ppconst->pp_used;
4424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 return FAIL;
4428
4429 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431}
4432
4433/*
4434 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004436 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 * JUMP_IF_FALSE alt jump if false
4438 * EVAL expr1a
4439 * JUMP_ALWAYS end
4440 * alt: EVAL expr1b
4441 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004442 *
4443 * Toplevel expression: expr2 ?? expr1
4444 * Produces instructions:
4445 * EVAL expr2 Push result of "expr2"
4446 * JUMP_AND_KEEP_IF_TRUE end jump if true
4447 * EVAL expr1
4448 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 */
4450 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452{
4453 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004455 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456
Bram Moolenaar3988f642020-08-27 22:43:03 +02004457 // Ignore all kinds of errors when not producing code.
4458 if (cctx->ctx_skip == SKIP_YES)
4459 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004460 evalarg_T evalarg;
4461
4462 CLEAR_FIELD(evalarg);
4463 evalarg.eval_cctx = cctx;
4464 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004465 return OK;
4466 }
4467
Bram Moolenaar61a89812020-05-07 16:58:17 +02004468 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004469 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 return FAIL;
4471
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004472 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 if (*p == '?')
4474 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004475 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 garray_T *instr = &cctx->ctx_instr;
4477 garray_T *stack = &cctx->ctx_type_stack;
4478 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004479 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004481 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004482 int has_const_expr = FALSE;
4483 int const_value = FALSE;
4484 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004486 if (next != NULL)
4487 {
4488 *arg = next_line_from_context(cctx, TRUE);
4489 p = skipwhite(*arg);
4490 }
4491
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004492 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004493 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004494 semsg(_(e_white_space_required_before_and_after_str),
4495 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004496 return FAIL;
4497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 if (ppconst->pp_used == ppconst_used + 1)
4500 {
4501 // the condition is a constant, we know whether the ? or the :
4502 // expression is to be evaluated.
4503 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004504 if (op_falsy)
4505 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4506 else
4507 {
4508 int error = FALSE;
4509
4510 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4511 &error);
4512 if (error)
4513 return FAIL;
4514 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004515 cctx->ctx_skip = save_skip == SKIP_YES ||
4516 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4517
4518 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4519 // "left ?? right" and "left" is truthy: produce "left"
4520 generate_ppconst(cctx, ppconst);
4521 else
4522 {
4523 clear_tv(&ppconst->pp_tv[ppconst_used]);
4524 --ppconst->pp_used;
4525 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 }
4527 else
4528 {
4529 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004530 if (op_falsy)
4531 end_idx = instr->ga_len;
4532 generate_JUMP(cctx, op_falsy
4533 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4534 if (op_falsy)
4535 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
4538 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004539 *arg = skipwhite(p + 1 + op_falsy);
4540 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004541 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004543 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545 if (!has_const_expr)
4546 {
4547 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004549 if (!op_falsy)
4550 {
4551 // remember the type and drop it
4552 --stack->ga_len;
4553 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004555 end_idx = instr->ga_len;
4556 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004558 // jump here from JUMP_IF_FALSE
4559 isn = ((isn_T *)instr->ga_data) + alt_idx;
4560 isn->isn_arg.jump.jump_where = instr->ga_len;
4561 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004564 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004566 // Check for the ":".
4567 p = may_peek_next_line(cctx, *arg, &next);
4568 if (*p != ':')
4569 {
4570 emsg(_(e_missing_colon));
4571 return FAIL;
4572 }
4573 if (next != NULL)
4574 {
4575 *arg = next_line_from_context(cctx, TRUE);
4576 p = skipwhite(*arg);
4577 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004578
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004579 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4580 {
4581 semsg(_(e_white_space_required_before_and_after_str), ":");
4582 return FAIL;
4583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004585 // evaluate the third expression
4586 if (has_const_expr)
4587 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004588 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004589 *arg = skipwhite(p + 1);
4590 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4591 return FAIL;
4592 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4593 return FAIL;
4594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
Bram Moolenaara5565e42020-05-09 15:44:01 +02004596 if (!has_const_expr)
4597 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004598 type_T **typep;
4599
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004603 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4604 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004606 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 isn = ((isn_T *)instr->ga_data) + end_idx;
4608 isn->isn_arg.jump.jump_where = instr->ga_len;
4609 }
4610
4611 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 }
4613 return OK;
4614}
4615
4616/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004618 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4619 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620 */
4621 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004622compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623{
4624 ppconst_T ppconst;
4625
4626 CLEAR_FIELD(ppconst);
4627 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4628 {
4629 clear_ppconst(&ppconst);
4630 return FAIL;
4631 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004632 if (is_const != NULL)
4633 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 if (generate_ppconst(cctx, &ppconst) == FAIL)
4635 return FAIL;
4636 return OK;
4637}
4638
4639/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004640 * Toplevel expression.
4641 */
4642 static int
4643compile_expr0(char_u **arg, cctx_T *cctx)
4644{
4645 return compile_expr0_ext(arg, cctx, NULL);
4646}
4647
4648/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 * compile "return [expr]"
4650 */
4651 static char_u *
4652compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4653{
4654 char_u *p = arg;
4655 garray_T *stack = &cctx->ctx_type_stack;
4656 type_T *stack_type;
4657
4658 if (*p != NUL && *p != '|' && *p != '\n')
4659 {
4660 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 return NULL;
4663
4664 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4665 if (set_return_type)
4666 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004667 else
4668 {
4669 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4670 && stack_type->tt_type != VAR_VOID
4671 && stack_type->tt_type != VAR_UNKNOWN)
4672 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004673 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004674 return NULL;
4675 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004676 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004677 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004678 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 }
4681 else
4682 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004683 // "set_return_type" cannot be TRUE, only used for a lambda which
4684 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004685 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4686 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004688 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 return NULL;
4690 }
4691
4692 // No argument, return zero.
4693 generate_PUSHNR(cctx, 0);
4694 }
4695
4696 if (generate_instr(cctx, ISN_RETURN) == NULL)
4697 return NULL;
4698
4699 // "return val | endif" is possible
4700 return skipwhite(p);
4701}
4702
4703/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704 * Get a line from the compilation context, compatible with exarg_T getline().
4705 * Return a pointer to the line in allocated memory.
4706 * Return NULL for end-of-file or some error.
4707 */
4708 static char_u *
4709exarg_getline(
4710 int c UNUSED,
4711 void *cookie,
4712 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004713 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004714{
4715 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004716 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004717
Bram Moolenaar66250c92020-08-20 15:02:42 +02004718 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004719 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004720 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004721 return NULL;
4722 ++cctx->ctx_lnum;
4723 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4724 // Comment lines result in NULL pointers, skip them.
4725 if (p != NULL)
4726 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004727 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004728}
4729
4730/*
4731 * Compile a nested :def command.
4732 */
4733 static char_u *
4734compile_nested_function(exarg_T *eap, cctx_T *cctx)
4735{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004736 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004737 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004738 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004739 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004740 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004741 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004742
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004743 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004744 {
4745 emsg(_(e_cannot_use_bang_with_nested_def));
4746 return NULL;
4747 }
4748
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004749 // Only g:Func() can use a namespace.
4750 if (name_start[1] == ':' && !is_global)
4751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004752 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004753 return NULL;
4754 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004755 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4756 return NULL;
4757
Bram Moolenaar04b12692020-05-04 23:24:44 +02004758 eap->arg = name_end;
4759 eap->getline = exarg_getline;
4760 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004761 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004762 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004763 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004764 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004765
Bram Moolenaar822ba242020-05-24 23:00:18 +02004766 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004767 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004768 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004769 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004770 {
4771 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004772 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004773 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004774
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004775 if (is_global)
4776 {
4777 char_u *func_name = vim_strnsave(name_start + 2,
4778 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004779
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004780 if (func_name == NULL)
4781 r = FAIL;
4782 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004783 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004784 }
4785 else
4786 {
4787 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004788 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004789 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004790 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004791
Bram Moolenaareef21022020-08-01 22:16:43 +02004792 if (lvar == NULL)
4793 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004794 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004795 return NULL;
4796 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004797
4798 // copy over the block scope IDs
4799 if (block_depth > 0)
4800 {
4801 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4802 if (ufunc->uf_block_ids != NULL)
4803 {
4804 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4805 sizeof(int) * block_depth);
4806 ufunc->uf_block_depth = block_depth;
4807 }
4808 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004809 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004810
Bram Moolenaar61a89812020-05-07 16:58:17 +02004811 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004812 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004813}
4814
4815/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 * Return the length of an assignment operator, or zero if there isn't one.
4817 */
4818 int
4819assignment_len(char_u *p, int *heredoc)
4820{
4821 if (*p == '=')
4822 {
4823 if (p[1] == '<' && p[2] == '<')
4824 {
4825 *heredoc = TRUE;
4826 return 3;
4827 }
4828 return 1;
4829 }
4830 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4831 return 2;
4832 if (STRNCMP(p, "..=", 3) == 0)
4833 return 3;
4834 return 0;
4835}
4836
4837// words that cannot be used as a variable
4838static char *reserved[] = {
4839 "true",
4840 "false",
4841 NULL
4842};
4843
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004844typedef enum {
4845 dest_local,
4846 dest_option,
4847 dest_env,
4848 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004849 dest_buffer,
4850 dest_window,
4851 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004852 dest_vimvar,
4853 dest_script,
4854 dest_reg,
4855} assign_dest_T;
4856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004858 * Generate the load instruction for "name".
4859 */
4860 static void
4861generate_loadvar(
4862 cctx_T *cctx,
4863 assign_dest_T dest,
4864 char_u *name,
4865 lvar_T *lvar,
4866 type_T *type)
4867{
4868 switch (dest)
4869 {
4870 case dest_option:
4871 // TODO: check the option exists
4872 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4873 break;
4874 case dest_global:
4875 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4876 break;
4877 case dest_buffer:
4878 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4879 break;
4880 case dest_window:
4881 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4882 break;
4883 case dest_tab:
4884 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4885 break;
4886 case dest_script:
4887 compile_load_scriptvar(cctx,
4888 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4889 break;
4890 case dest_env:
4891 // Include $ in the name here
4892 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4893 break;
4894 case dest_reg:
4895 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4896 break;
4897 case dest_vimvar:
4898 generate_LOADV(cctx, name + 2, TRUE);
4899 break;
4900 case dest_local:
4901 if (lvar->lv_from_outer)
4902 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4903 NULL, type);
4904 else
4905 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4906 break;
4907 }
4908}
4909
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004910 void
4911vim9_declare_error(char_u *name)
4912{
4913 char *scope = "";
4914
4915 switch (*name)
4916 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004917 case 'g': scope = _("global"); break;
4918 case 'b': scope = _("buffer"); break;
4919 case 'w': scope = _("window"); break;
4920 case 't': scope = _("tab"); break;
4921 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004922 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004923 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004924 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004925 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004926 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004927 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004928 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004929 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004930 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004931}
4932
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004933/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004935 * "let name"
4936 * "var name = expr"
4937 * "final name = expr"
4938 * "const name = expr"
4939 * "name = expr"
4940 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 * Return NULL for an error.
4942 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 */
4944 static char_u *
4945compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4946{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004949 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 char_u *ret = NULL;
4951 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004953 int scriptvar_sid = 0;
4954 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004957 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 int oplen = 0;
4960 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004961 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004962 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004965 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4966 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 // Skip over the "var" or "[var, var]" to get to any "=".
4969 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4970 if (p == NULL)
4971 return *arg == '[' ? arg : NULL;
4972
4973 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004975 // TODO: should we allow this, and figure out type inference from list
4976 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004977 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 return NULL;
4979 }
4980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 sp = p;
4982 p = skipwhite(p);
4983 op = p;
4984 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985
4986 if (var_count > 0 && oplen == 0)
4987 // can be something like "[1, 2]->func()"
4988 return arg;
4989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4991 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004992 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004994 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 if (heredoc)
4997 {
4998 list_T *l;
4999 listitem_T *li;
5000
5001 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005002 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005004 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005005 if (l == NULL)
5006 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007
Bram Moolenaar078269b2020-09-21 20:35:55 +02005008 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005010 // Push each line and the create the list.
5011 FOR_ALL_LIST_ITEMS(l, li)
5012 {
5013 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5014 li->li_tv.vval.v_string = NULL;
5015 }
5016 generate_NEWLIST(cctx, l->lv_len);
5017 type = &t_list_string;
5018 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 list_free(l);
5021 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 // for "[var, var] = expr" evaluate the expression here, loop over the
5027 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005028
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005029 p = skipwhite(op + oplen);
5030 if (compile_expr0(&p, cctx) == FAIL)
5031 return NULL;
5032 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005034 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005036 type_T *stacktype;
5037
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005038 stacktype = stack->ga_len == 0 ? &t_void
5039 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005042 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 goto theend;
5044 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005045 if (need_type(stacktype, &t_list_any, -1, cctx,
5046 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005048 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005049 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5050 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 }
5052 }
5053
5054 /*
5055 * Loop over variables in "[var, var] = expr".
5056 * For "var = expr" and "let var: type" this is done only once.
5057 */
5058 if (var_count > 0)
5059 var_start = skipwhite(arg + 1); // skip over the "["
5060 else
5061 var_start = arg;
5062 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5063 {
5064 char_u *var_end = skip_var_one(var_start, FALSE);
5065 size_t varlen;
5066 int new_local = FALSE;
5067 int opt_type;
5068 int opt_flags = 0;
5069 assign_dest_T dest = dest_local;
5070 int vimvaridx = -1;
5071 lvar_T *lvar = NULL;
5072 lvar_T arg_lvar;
5073 int has_type = FALSE;
5074 int has_index = FALSE;
5075 int instr_count = -1;
5076
Bram Moolenaar65821722020-08-02 18:58:54 +02005077 if (*var_start == '@')
5078 p = var_start + 2;
5079 else
5080 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005081 // skip over the leading "&", "&l:", "&g:" and "$"
5082 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005083 p = to_name_end(p, TRUE);
5084 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085
5086 // "a: type" is declaring variable "a" with a type, not "a:".
5087 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5088 --var_end;
5089 if (is_decl && p == var_start + 2 && p[-1] == ':')
5090 --p;
5091
5092 varlen = p - var_start;
5093 vim_free(name);
5094 name = vim_strnsave(var_start, varlen);
5095 if (name == NULL)
5096 return NULL;
5097 if (!heredoc)
5098 type = &t_any;
5099
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005100 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005102 int declare_error = FALSE;
5103
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 if (*var_start == '&')
5105 {
5106 int cc;
5107 long numval;
5108
5109 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005110 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005112 emsg(_(e_const_option));
5113 goto theend;
5114 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005115 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005116 p = var_start;
5117 p = find_option_end(&p, &opt_flags);
5118 if (p == NULL)
5119 {
5120 // cannot happen?
5121 emsg(_(e_letunexp));
5122 goto theend;
5123 }
5124 cc = *p;
5125 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005126 opt_type = get_option_value(skip_option_env_lead(var_start),
5127 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 *p = cc;
5129 if (opt_type == -3)
5130 {
5131 semsg(_(e_unknown_option), var_start);
5132 goto theend;
5133 }
5134 if (opt_type == -2 || opt_type == 0)
5135 type = &t_string;
5136 else
5137 type = &t_number; // both number and boolean option
5138 }
5139 else if (*var_start == '$')
5140 {
5141 dest = dest_env;
5142 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005143 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 }
5145 else if (*var_start == '@')
5146 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005147 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 {
5149 emsg_invreg(var_start[1]);
5150 goto theend;
5151 }
5152 dest = dest_reg;
5153 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005154 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005156 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157 {
5158 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005159 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005160 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005161 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162 {
5163 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005164 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005166 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 {
5168 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005169 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005171 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172 {
5173 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005174 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005176 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 {
5178 typval_T *vtv;
5179 int di_flags;
5180
5181 vimvaridx = find_vim_var(name + 2, &di_flags);
5182 if (vimvaridx < 0)
5183 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005184 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 goto theend;
5186 }
5187 // We use the current value of "sandbox" here, is that OK?
5188 if (var_check_ro(di_flags, name, FALSE))
5189 goto theend;
5190 dest = dest_vimvar;
5191 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005192 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005193 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 }
5195 else
5196 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005197 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198
5199 for (idx = 0; reserved[idx] != NULL; ++idx)
5200 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005201 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005202 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005203 goto theend;
5204 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205
5206 lvar = lookup_local(var_start, varlen, cctx);
5207 if (lvar == NULL)
5208 {
5209 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005210 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5212 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005213 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 if (is_decl)
5215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005216 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 goto theend;
5218 }
5219 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005220 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005221 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 if (lvar != NULL)
5223 {
5224 if (is_decl)
5225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005226 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 goto theend;
5228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005230 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005232 int script_namespace = varlen > 1
5233 && STRNCMP(var_start, "s:", 2) == 0;
5234 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005235 ? script_var_exists(var_start + 2, varlen - 2,
5236 FALSE, cctx)
5237 : script_var_exists(var_start, varlen,
5238 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005239 imported_T *import =
5240 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005241
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005242 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005244 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5245
5246 if (is_decl)
5247 {
5248 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005249 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005250 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005251 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005252 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005253 name);
5254 goto theend;
5255 }
5256 else if (cctx->ctx_ufunc->uf_script_ctx_version
5257 == SCRIPT_VERSION_VIM9
5258 && script_namespace
5259 && !script_var && import == NULL)
5260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005262 goto theend;
5263 }
5264
5265 dest = dest_script;
5266
5267 // existing script-local variables should have a type
5268 scriptvar_sid = current_sctx.sc_sid;
5269 if (import != NULL)
5270 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005271 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005272 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005273 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005274 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005275 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005276 {
5277 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5278 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005279 ((svar_T *)si->sn_var_vals.ga_data)
5280 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005281 type = sv->sv_type;
5282 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005283 }
5284 }
5285 else if (name[1] == ':' && name[2] != NUL)
5286 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005287 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 goto theend;
5289 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005290 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005291 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005292 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005293 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005294 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005295 else if (check_defined(var_start, varlen, cctx) == FAIL)
5296 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005299
5300 if (declare_error)
5301 {
5302 vim9_declare_error(name);
5303 goto theend;
5304 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305 }
5306
5307 // handle "a:name" as a name, not index "name" on "a"
5308 if (varlen > 1 || var_start[varlen] != ':')
5309 p = var_end;
5310
5311 if (dest != dest_option)
5312 {
5313 if (is_decl && *p == ':')
5314 {
5315 // parse optional type: "let var: type = expr"
5316 if (!VIM_ISWHITE(p[1]))
5317 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005318 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 goto theend;
5320 }
5321 p = skipwhite(p + 1);
5322 type = parse_type(&p, cctx->ctx_type_list);
5323 has_type = TRUE;
5324 }
5325 else if (lvar != NULL)
5326 type = lvar->lv_type;
5327 }
5328
5329 if (oplen == 3 && !heredoc && dest != dest_global
5330 && type->tt_type != VAR_STRING
5331 && type->tt_type != VAR_ANY)
5332 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005333 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 goto theend;
5335 }
5336
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005337 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 {
5339 if (oplen > 1 && !heredoc)
5340 {
5341 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005342 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 goto theend;
5344 }
5345
5346 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005347 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5348 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005349 goto theend;
5350 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005351 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 if (lvar == NULL)
5353 goto theend;
5354 new_local = TRUE;
5355 }
5356
5357 member_type = type;
5358 if (var_end > var_start + varlen)
5359 {
5360 // Something follows after the variable: "var[idx]".
5361 if (is_decl)
5362 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005363 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364 goto theend;
5365 }
5366
5367 if (var_start[varlen] == '[')
5368 {
5369 has_index = TRUE;
5370 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005371 member_type = &t_any;
5372 else
5373 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 }
5375 else
5376 {
5377 semsg("Not supported yet: %s", var_start);
5378 goto theend;
5379 }
5380 }
5381 else if (lvar == &arg_lvar)
5382 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005383 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 goto theend;
5385 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005386 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5387 {
5388 semsg(_(e_cannot_assign_to_constant), name);
5389 goto theend;
5390 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391
5392 if (!heredoc)
5393 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005394 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005396 if (oplen > 0 && var_count == 0)
5397 {
5398 // skip over the "=" and the expression
5399 p = skipwhite(op + oplen);
5400 compile_expr0(&p, cctx);
5401 }
5402 }
5403 else if (oplen > 0)
5404 {
5405 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005406 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005407
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 // For "var = expr" evaluate the expression.
5409 if (var_count == 0)
5410 {
5411 int r;
5412
5413 // for "+=", "*=", "..=" etc. first load the current value
5414 if (*op != '=')
5415 {
5416 generate_loadvar(cctx, dest, name, lvar, type);
5417
5418 if (has_index)
5419 {
5420 // TODO: get member from list or dict
5421 emsg("Index with operation not supported yet");
5422 goto theend;
5423 }
5424 }
5425
5426 // Compile the expression. Temporarily hide the new local
5427 // variable here, it is not available to this expression.
5428 if (new_local)
5429 --cctx->ctx_locals.ga_len;
5430 instr_count = instr->ga_len;
5431 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005432 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 if (new_local)
5434 ++cctx->ctx_locals.ga_len;
5435 if (r == FAIL)
5436 goto theend;
5437 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005438 else if (semicolon && var_idx == var_count - 1)
5439 {
5440 // For "[var; var] = expr" get the rest of the list
5441 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5442 goto theend;
5443 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005444 else
5445 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005446 // For "[var, var] = expr" get the "var_idx" item from the
5447 // list.
5448 if (generate_GETITEM(cctx, var_idx) == FAIL)
5449 return FAIL;
5450 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005451
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005452 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005453 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005454 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005455 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005456 if ((stacktype->tt_type == VAR_FUNC
5457 || stacktype->tt_type == VAR_PARTIAL)
5458 && var_wrong_func_name(name, TRUE))
5459 goto theend;
5460
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005461 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005462 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005463 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005465 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005466 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005467 }
5468 else
5469 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005470 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005471 // for a variable that implies &t_any.
5472 if (stacktype == &t_list_empty)
5473 lvar->lv_type = &t_list_any;
5474 else if (stacktype == &t_dict_empty)
5475 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005476 else if (stacktype == &t_unknown)
5477 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005478 else
5479 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005480 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005482 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005483 {
5484 type_T *use_type = lvar->lv_type;
5485
Bram Moolenaar71abe482020-09-14 22:28:30 +02005486 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005487 if (has_index)
5488 {
5489 use_type = use_type->tt_member;
5490 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005491 // could be indexing "any"
5492 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005493 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005494 if (need_type(stacktype, use_type, -1, cctx,
5495 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005496 goto theend;
5497 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005498 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005499 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005500 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005503 else if (cmdidx == CMD_final)
5504 {
5505 emsg(_(e_final_requires_a_value));
5506 goto theend;
5507 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005510 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 goto theend;
5512 }
5513 else if (!has_type || dest == dest_option)
5514 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005515 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005516 goto theend;
5517 }
5518 else
5519 {
5520 // variables are always initialized
5521 if (ga_grow(instr, 1) == FAIL)
5522 goto theend;
5523 switch (member_type->tt_type)
5524 {
5525 case VAR_BOOL:
5526 generate_PUSHBOOL(cctx, VVAL_FALSE);
5527 break;
5528 case VAR_FLOAT:
5529#ifdef FEAT_FLOAT
5530 generate_PUSHF(cctx, 0.0);
5531#endif
5532 break;
5533 case VAR_STRING:
5534 generate_PUSHS(cctx, NULL);
5535 break;
5536 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005537 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005538 break;
5539 case VAR_FUNC:
5540 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5541 break;
5542 case VAR_LIST:
5543 generate_NEWLIST(cctx, 0);
5544 break;
5545 case VAR_DICT:
5546 generate_NEWDICT(cctx, 0);
5547 break;
5548 case VAR_JOB:
5549 generate_PUSHJOB(cctx, NULL);
5550 break;
5551 case VAR_CHANNEL:
5552 generate_PUSHCHANNEL(cctx, NULL);
5553 break;
5554 case VAR_NUMBER:
5555 case VAR_UNKNOWN:
5556 case VAR_ANY:
5557 case VAR_PARTIAL:
5558 case VAR_VOID:
5559 case VAR_SPECIAL: // cannot happen
5560 generate_PUSHNR(cctx, 0);
5561 break;
5562 }
5563 }
5564 if (var_count == 0)
5565 end = p;
5566 }
5567
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005568 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005569 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005570 break;
5571
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 if (oplen > 0 && *op != '=')
5573 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005574 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005575 type_T *stacktype;
5576
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005577 if (*op == '.')
5578 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005579 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005580 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005582 if (
5583#ifdef FEAT_FLOAT
5584 // If variable is float operation with number is OK.
5585 !(expected == &t_float && stacktype == &t_number) &&
5586#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005587 need_type(stacktype, expected, -1, cctx,
5588 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005589 goto theend;
5590
5591 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005592 {
5593 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5594 goto theend;
5595 }
5596 else if (*op == '+')
5597 {
5598 if (generate_add_instr(cctx,
5599 operator_type(member_type, stacktype),
5600 member_type, stacktype) == FAIL)
5601 goto theend;
5602 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005603 else if (generate_two_op(cctx, op) == FAIL)
5604 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005607 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005608 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005609 int r;
5610
5611 // Compile the "idx" in "var[idx]".
5612 if (new_local)
5613 --cctx->ctx_locals.ga_len;
5614 p = skipwhite(var_start + varlen + 1);
5615 r = compile_expr0(&p, cctx);
5616 if (new_local)
5617 ++cctx->ctx_locals.ga_len;
5618 if (r == FAIL)
5619 goto theend;
5620 if (*skipwhite(p) != ']')
5621 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005622 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 emsg(_(e_missbrac));
5624 goto theend;
5625 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005626 if (type == &t_any)
5627 {
5628 type_T *idx_type = ((type_T **)stack->ga_data)[
5629 stack->ga_len - 1];
5630 // Index on variable of unknown type: guess the type from the
5631 // index type: number is dict, otherwise dict.
5632 // TODO: should do the assignment at runtime
5633 if (idx_type->tt_type == VAR_NUMBER)
5634 type = &t_list_any;
5635 else
5636 type = &t_dict_any;
5637 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005638 if (type->tt_type == VAR_DICT
5639 && may_generate_2STRING(-1, cctx) == FAIL)
5640 goto theend;
5641 if (type->tt_type == VAR_LIST
5642 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005643 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 {
5645 emsg(_(e_number_exp));
5646 goto theend;
5647 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005648
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005649 // Load the dict or list. On the stack we then have:
5650 // - value
5651 // - index
5652 // - variable
5653 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005654
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005655 if (type->tt_type == VAR_LIST)
5656 {
5657 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5658 return FAIL;
5659 }
5660 else if (type->tt_type == VAR_DICT)
5661 {
5662 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5663 return FAIL;
5664 }
5665 else
5666 {
5667 emsg(_(e_listreq));
5668 goto theend;
5669 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005670 }
5671 else
5672 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005673 if (is_decl && cmdidx == CMD_const
5674 && (dest == dest_script || dest == dest_local))
5675 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005676 generate_LOCKCONST(cctx);
5677
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005678 switch (dest)
5679 {
5680 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005681 generate_STOREOPT(cctx, skip_option_env_lead(name),
5682 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005683 break;
5684 case dest_global:
5685 // include g: with the name, easier to execute that way
5686 generate_STORE(cctx, ISN_STOREG, 0, name);
5687 break;
5688 case dest_buffer:
5689 // include b: with the name, easier to execute that way
5690 generate_STORE(cctx, ISN_STOREB, 0, name);
5691 break;
5692 case dest_window:
5693 // include w: with the name, easier to execute that way
5694 generate_STORE(cctx, ISN_STOREW, 0, name);
5695 break;
5696 case dest_tab:
5697 // include t: with the name, easier to execute that way
5698 generate_STORE(cctx, ISN_STORET, 0, name);
5699 break;
5700 case dest_env:
5701 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5702 break;
5703 case dest_reg:
5704 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5705 break;
5706 case dest_vimvar:
5707 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5708 break;
5709 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005710 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005711 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005712 {
5713 char_u *name_s = name;
5714
5715 // Include s: in the name for store_var()
5716 if (name[1] != ':')
5717 {
5718 int len = (int)STRLEN(name) + 3;
5719
5720 name_s = alloc(len);
5721 if (name_s == NULL)
5722 name_s = name;
5723 else
5724 vim_snprintf((char *)name_s, len,
5725 "s:%s", name);
5726 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005727 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5728 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005729 if (name_s != name)
5730 vim_free(name_s);
5731 }
5732 else
5733 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005734 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005735 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005736 break;
5737 case dest_local:
5738 if (lvar != NULL)
5739 {
5740 isn_T *isn = ((isn_T *)instr->ga_data)
5741 + instr->ga_len - 1;
5742
5743 // optimization: turn "var = 123" from ISN_PUSHNR +
5744 // ISN_STORE into ISN_STORENR
5745 if (!lvar->lv_from_outer
5746 && instr->ga_len == instr_count + 1
5747 && isn->isn_type == ISN_PUSHNR)
5748 {
5749 varnumber_T val = isn->isn_arg.number;
5750
5751 isn->isn_type = ISN_STORENR;
5752 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5753 isn->isn_arg.storenr.stnr_val = val;
5754 if (stack->ga_len > 0)
5755 --stack->ga_len;
5756 }
5757 else if (lvar->lv_from_outer)
5758 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005759 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005760 else
5761 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5762 }
5763 break;
5764 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005765 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005766
5767 if (var_idx + 1 < var_count)
5768 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005770
5771 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005772 if (var_count > 0 && !semicolon)
5773 {
5774 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5775 goto theend;
5776 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005777
Bram Moolenaarb2097502020-07-19 17:17:02 +02005778 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
5780theend:
5781 vim_free(name);
5782 return ret;
5783}
5784
5785/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005786 * Check if "name" can be "unlet".
5787 */
5788 int
5789check_vim9_unlet(char_u *name)
5790{
5791 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5792 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005793 // "unlet s:var" is allowed in legacy script.
5794 if (*name == 's' && !script_is_vim9())
5795 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005796 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005797 return FAIL;
5798 }
5799 return OK;
5800}
5801
5802/*
5803 * Callback passed to ex_unletlock().
5804 */
5805 static int
5806compile_unlet(
5807 lval_T *lvp,
5808 char_u *name_end,
5809 exarg_T *eap,
5810 int deep UNUSED,
5811 void *coookie)
5812{
5813 cctx_T *cctx = coookie;
5814
5815 if (lvp->ll_tv == NULL)
5816 {
5817 char_u *p = lvp->ll_name;
5818 int cc = *name_end;
5819 int ret = OK;
5820
5821 // Normal name. Only supports g:, w:, t: and b: namespaces.
5822 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005823 if (*p == '$')
5824 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5825 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005826 ret = FAIL;
5827 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005828 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005829
5830 *name_end = cc;
5831 return ret;
5832 }
5833
5834 // TODO: unlet {list}[idx]
5835 // TODO: unlet {dict}[key]
5836 emsg("Sorry, :unlet not fully implemented yet");
5837 return FAIL;
5838}
5839
5840/*
5841 * compile "unlet var", "lock var" and "unlock var"
5842 * "arg" points to "var".
5843 */
5844 static char_u *
5845compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5846{
5847 char_u *p = arg;
5848
5849 if (eap->cmdidx != CMD_unlet)
5850 {
5851 emsg("Sorry, :lock and unlock not implemented yet");
5852 return NULL;
5853 }
5854
5855 if (*p == '!')
5856 {
5857 p = skipwhite(p + 1);
5858 eap->forceit = TRUE;
5859 }
5860
5861 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5862 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5863}
5864
5865/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 * Compile an :import command.
5867 */
5868 static char_u *
5869compile_import(char_u *arg, cctx_T *cctx)
5870{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005871 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872}
5873
5874/*
5875 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5876 */
5877 static int
5878compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5879{
5880 garray_T *instr = &cctx->ctx_instr;
5881 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5882
5883 if (endlabel == NULL)
5884 return FAIL;
5885 endlabel->el_next = *el;
5886 *el = endlabel;
5887 endlabel->el_end_label = instr->ga_len;
5888
5889 generate_JUMP(cctx, when, 0);
5890 return OK;
5891}
5892
5893 static void
5894compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5895{
5896 garray_T *instr = &cctx->ctx_instr;
5897
5898 while (*el != NULL)
5899 {
5900 endlabel_T *cur = (*el);
5901 isn_T *isn;
5902
5903 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5904 isn->isn_arg.jump.jump_where = instr->ga_len;
5905 *el = cur->el_next;
5906 vim_free(cur);
5907 }
5908}
5909
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005910 static void
5911compile_free_jump_to_end(endlabel_T **el)
5912{
5913 while (*el != NULL)
5914 {
5915 endlabel_T *cur = (*el);
5916
5917 *el = cur->el_next;
5918 vim_free(cur);
5919 }
5920}
5921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922/*
5923 * Create a new scope and set up the generic items.
5924 */
5925 static scope_T *
5926new_scope(cctx_T *cctx, scopetype_T type)
5927{
5928 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5929
5930 if (scope == NULL)
5931 return NULL;
5932 scope->se_outer = cctx->ctx_scope;
5933 cctx->ctx_scope = scope;
5934 scope->se_type = type;
5935 scope->se_local_count = cctx->ctx_locals.ga_len;
5936 return scope;
5937}
5938
5939/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005940 * Free the current scope and go back to the outer scope.
5941 */
5942 static void
5943drop_scope(cctx_T *cctx)
5944{
5945 scope_T *scope = cctx->ctx_scope;
5946
5947 if (scope == NULL)
5948 {
5949 iemsg("calling drop_scope() without a scope");
5950 return;
5951 }
5952 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005953 switch (scope->se_type)
5954 {
5955 case IF_SCOPE:
5956 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5957 case FOR_SCOPE:
5958 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5959 case WHILE_SCOPE:
5960 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5961 case TRY_SCOPE:
5962 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5963 case NO_SCOPE:
5964 case BLOCK_SCOPE:
5965 break;
5966 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005967 vim_free(scope);
5968}
5969
5970/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005971 * Check that the top of the type stack has a type that can be used as a
5972 * condition. Give an error and return FAIL if not.
5973 */
5974 static int
5975bool_on_stack(cctx_T *cctx)
5976{
5977 garray_T *stack = &cctx->ctx_type_stack;
5978 type_T *type;
5979
5980 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5981 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005982 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005983 return FAIL;
5984 return OK;
5985}
5986
5987/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 * compile "if expr"
5989 *
5990 * "if expr" Produces instructions:
5991 * EVAL expr Push result of "expr"
5992 * JUMP_IF_FALSE end
5993 * ... body ...
5994 * end:
5995 *
5996 * "if expr | else" Produces instructions:
5997 * EVAL expr Push result of "expr"
5998 * JUMP_IF_FALSE else
5999 * ... body ...
6000 * JUMP_ALWAYS end
6001 * else:
6002 * ... body ...
6003 * end:
6004 *
6005 * "if expr1 | elseif expr2 | else" Produces instructions:
6006 * EVAL expr Push result of "expr"
6007 * JUMP_IF_FALSE elseif
6008 * ... body ...
6009 * JUMP_ALWAYS end
6010 * elseif:
6011 * EVAL expr Push result of "expr"
6012 * JUMP_IF_FALSE else
6013 * ... body ...
6014 * JUMP_ALWAYS end
6015 * else:
6016 * ... body ...
6017 * end:
6018 */
6019 static char_u *
6020compile_if(char_u *arg, cctx_T *cctx)
6021{
6022 char_u *p = arg;
6023 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006024 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006026 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006027 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028
Bram Moolenaara5565e42020-05-09 15:44:01 +02006029 CLEAR_FIELD(ppconst);
6030 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006031 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006032 clear_ppconst(&ppconst);
6033 return NULL;
6034 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006035 if (cctx->ctx_skip == SKIP_YES)
6036 clear_ppconst(&ppconst);
6037 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006038 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006039 int error = FALSE;
6040 int v;
6041
Bram Moolenaara5565e42020-05-09 15:44:01 +02006042 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006043 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006044 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006045 if (error)
6046 return NULL;
6047 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006048 }
6049 else
6050 {
6051 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006052 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006053 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006054 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006055 if (bool_on_stack(cctx) == FAIL)
6056 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058
6059 scope = new_scope(cctx, IF_SCOPE);
6060 if (scope == NULL)
6061 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006062 scope->se_skip_save = skip_save;
6063 // "is_had_return" will be reset if any block does not end in :return
6064 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006066 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006067 {
6068 // "where" is set when ":elseif", "else" or ":endif" is found
6069 scope->se_u.se_if.is_if_label = instr->ga_len;
6070 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6071 }
6072 else
6073 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074
6075 return p;
6076}
6077
6078 static char_u *
6079compile_elseif(char_u *arg, cctx_T *cctx)
6080{
6081 char_u *p = arg;
6082 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006083 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 isn_T *isn;
6085 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006086 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006087 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088
6089 if (scope == NULL || scope->se_type != IF_SCOPE)
6090 {
6091 emsg(_(e_elseif_without_if));
6092 return NULL;
6093 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006094 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006095 if (!cctx->ctx_had_return)
6096 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006098 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006099 {
6100 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006102 return NULL;
6103 // previous "if" or "elseif" jumps here
6104 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6105 isn->isn_arg.jump.jump_where = instr->ga_len;
6106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006108 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006109 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006110 if (cctx->ctx_skip == SKIP_YES)
6111 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006112 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006113 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006114 clear_ppconst(&ppconst);
6115 return NULL;
6116 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006117 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006118 if (scope->se_skip_save == SKIP_YES)
6119 clear_ppconst(&ppconst);
6120 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006121 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006122 int error = FALSE;
6123 int v;
6124
Bram Moolenaar7f141552020-05-09 17:35:53 +02006125 // The expression results in a constant.
6126 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006127 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6128 if (error)
6129 return NULL;
6130 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006131 clear_ppconst(&ppconst);
6132 scope->se_u.se_if.is_if_label = -1;
6133 }
6134 else
6135 {
6136 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006137 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006138 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006139 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006140 if (bool_on_stack(cctx) == FAIL)
6141 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006143 // "where" is set when ":elseif", "else" or ":endif" is found
6144 scope->se_u.se_if.is_if_label = instr->ga_len;
6145 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147
6148 return p;
6149}
6150
6151 static char_u *
6152compile_else(char_u *arg, cctx_T *cctx)
6153{
6154 char_u *p = arg;
6155 garray_T *instr = &cctx->ctx_instr;
6156 isn_T *isn;
6157 scope_T *scope = cctx->ctx_scope;
6158
6159 if (scope == NULL || scope->se_type != IF_SCOPE)
6160 {
6161 emsg(_(e_else_without_if));
6162 return NULL;
6163 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006164 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006165 if (!cctx->ctx_had_return)
6166 scope->se_u.se_if.is_had_return = FALSE;
6167 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
Bram Moolenaarefd88552020-06-18 20:50:10 +02006169 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006170 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006171 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006172 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006173 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006174 if (!cctx->ctx_had_return
6175 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6176 JUMP_ALWAYS, cctx) == FAIL)
6177 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006178 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006179
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006180 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006181 {
6182 if (scope->se_u.se_if.is_if_label >= 0)
6183 {
6184 // previous "if" or "elseif" jumps here
6185 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6186 isn->isn_arg.jump.jump_where = instr->ga_len;
6187 scope->se_u.se_if.is_if_label = -1;
6188 }
6189 }
6190
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006191 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006192 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194
6195 return p;
6196}
6197
6198 static char_u *
6199compile_endif(char_u *arg, cctx_T *cctx)
6200{
6201 scope_T *scope = cctx->ctx_scope;
6202 ifscope_T *ifscope;
6203 garray_T *instr = &cctx->ctx_instr;
6204 isn_T *isn;
6205
6206 if (scope == NULL || scope->se_type != IF_SCOPE)
6207 {
6208 emsg(_(e_endif_without_if));
6209 return NULL;
6210 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006211 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006212 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006213 if (!cctx->ctx_had_return)
6214 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006216 if (scope->se_u.se_if.is_if_label >= 0)
6217 {
6218 // previous "if" or "elseif" jumps here
6219 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6220 isn->isn_arg.jump.jump_where = instr->ga_len;
6221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 // Fill in the "end" label in jumps at the end of the blocks.
6223 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006224 cctx->ctx_skip = scope->se_skip_save;
6225
6226 // If all the blocks end in :return and there is an :else then the
6227 // had_return flag is set.
6228 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006230 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 return arg;
6232}
6233
6234/*
6235 * compile "for var in expr"
6236 *
6237 * Produces instructions:
6238 * PUSHNR -1
6239 * STORE loop-idx Set index to -1
6240 * EVAL expr Push result of "expr"
6241 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6242 * - if beyond end, jump to "end"
6243 * - otherwise get item from list and push it
6244 * STORE var Store item in "var"
6245 * ... body ...
6246 * JUMP top Jump back to repeat
6247 * end: DROP Drop the result of "expr"
6248 *
6249 */
6250 static char_u *
6251compile_for(char_u *arg, cctx_T *cctx)
6252{
6253 char_u *p;
6254 size_t varlen;
6255 garray_T *instr = &cctx->ctx_instr;
6256 garray_T *stack = &cctx->ctx_type_stack;
6257 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006258 lvar_T *loop_lvar; // loop iteration variable
6259 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 type_T *vartype;
6261
6262 // TODO: list of variables: "for [key, value] in dict"
6263 // parse "var"
6264 for (p = arg; eval_isnamec1(*p); ++p)
6265 ;
6266 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006267 var_lvar = lookup_local(arg, varlen, cctx);
6268 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006270 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 return NULL;
6272 }
6273
6274 // consume "in"
6275 p = skipwhite(p);
6276 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6277 {
6278 emsg(_(e_missing_in));
6279 return NULL;
6280 }
6281 p = skipwhite(p + 2);
6282
6283
6284 scope = new_scope(cctx, FOR_SCOPE);
6285 if (scope == NULL)
6286 return NULL;
6287
6288 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006289 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6290 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006291 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006292 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006293 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296
6297 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006298 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6299 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006300 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006301 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006302 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006306 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
6308 // compile "expr", it remains on the stack until "endfor"
6309 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006310 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006311 {
6312 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006316 // Now that we know the type of "var", check that it is a list, now or at
6317 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006319 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006321 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 return NULL;
6323 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006324 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006325 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
6327 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006328 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006330 generate_FOR(cctx, loop_lvar->lv_idx);
6331 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332
6333 return arg;
6334}
6335
6336/*
6337 * compile "endfor"
6338 */
6339 static char_u *
6340compile_endfor(char_u *arg, cctx_T *cctx)
6341{
6342 garray_T *instr = &cctx->ctx_instr;
6343 scope_T *scope = cctx->ctx_scope;
6344 forscope_T *forscope;
6345 isn_T *isn;
6346
6347 if (scope == NULL || scope->se_type != FOR_SCOPE)
6348 {
6349 emsg(_(e_for));
6350 return NULL;
6351 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006352 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006354 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355
6356 // At end of ":for" scope jump back to the FOR instruction.
6357 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6358
6359 // Fill in the "end" label in the FOR statement so it can jump here
6360 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6361 isn->isn_arg.forloop.for_end = instr->ga_len;
6362
6363 // Fill in the "end" label any BREAK statements
6364 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6365
6366 // Below the ":for" scope drop the "expr" list from the stack.
6367 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6368 return NULL;
6369
6370 vim_free(scope);
6371
6372 return arg;
6373}
6374
6375/*
6376 * compile "while expr"
6377 *
6378 * Produces instructions:
6379 * top: EVAL expr Push result of "expr"
6380 * JUMP_IF_FALSE end jump if false
6381 * ... body ...
6382 * JUMP top Jump back to repeat
6383 * end:
6384 *
6385 */
6386 static char_u *
6387compile_while(char_u *arg, cctx_T *cctx)
6388{
6389 char_u *p = arg;
6390 garray_T *instr = &cctx->ctx_instr;
6391 scope_T *scope;
6392
6393 scope = new_scope(cctx, WHILE_SCOPE);
6394 if (scope == NULL)
6395 return NULL;
6396
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006397 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398
6399 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006400 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 return NULL;
6402
Bram Moolenaar13106602020-10-04 16:06:05 +02006403 if (bool_on_stack(cctx) == FAIL)
6404 return FAIL;
6405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006407 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 JUMP_IF_FALSE, cctx) == FAIL)
6409 return FAIL;
6410
6411 return p;
6412}
6413
6414/*
6415 * compile "endwhile"
6416 */
6417 static char_u *
6418compile_endwhile(char_u *arg, cctx_T *cctx)
6419{
6420 scope_T *scope = cctx->ctx_scope;
6421
6422 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6423 {
6424 emsg(_(e_while));
6425 return NULL;
6426 }
6427 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006428 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429
6430 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006431 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432
6433 // Fill in the "end" label in the WHILE statement so it can jump here.
6434 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006435 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436
6437 vim_free(scope);
6438
6439 return arg;
6440}
6441
6442/*
6443 * compile "continue"
6444 */
6445 static char_u *
6446compile_continue(char_u *arg, cctx_T *cctx)
6447{
6448 scope_T *scope = cctx->ctx_scope;
6449
6450 for (;;)
6451 {
6452 if (scope == NULL)
6453 {
6454 emsg(_(e_continue));
6455 return NULL;
6456 }
6457 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6458 break;
6459 scope = scope->se_outer;
6460 }
6461
6462 // Jump back to the FOR or WHILE instruction.
6463 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006464 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6465 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 return arg;
6467}
6468
6469/*
6470 * compile "break"
6471 */
6472 static char_u *
6473compile_break(char_u *arg, cctx_T *cctx)
6474{
6475 scope_T *scope = cctx->ctx_scope;
6476 endlabel_T **el;
6477
6478 for (;;)
6479 {
6480 if (scope == NULL)
6481 {
6482 emsg(_(e_break));
6483 return NULL;
6484 }
6485 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6486 break;
6487 scope = scope->se_outer;
6488 }
6489
6490 // Jump to the end of the FOR or WHILE loop.
6491 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006492 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6496 return FAIL;
6497
6498 return arg;
6499}
6500
6501/*
6502 * compile "{" start of block
6503 */
6504 static char_u *
6505compile_block(char_u *arg, cctx_T *cctx)
6506{
6507 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6508 return NULL;
6509 return skipwhite(arg + 1);
6510}
6511
6512/*
6513 * compile end of block: drop one scope
6514 */
6515 static void
6516compile_endblock(cctx_T *cctx)
6517{
6518 scope_T *scope = cctx->ctx_scope;
6519
6520 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006521 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 vim_free(scope);
6523}
6524
6525/*
6526 * compile "try"
6527 * Creates a new scope for the try-endtry, pointing to the first catch and
6528 * finally.
6529 * Creates another scope for the "try" block itself.
6530 * TRY instruction sets up exception handling at runtime.
6531 *
6532 * "try"
6533 * TRY -> catch1, -> finally push trystack entry
6534 * ... try block
6535 * "throw {exception}"
6536 * EVAL {exception}
6537 * THROW create exception
6538 * ... try block
6539 * " catch {expr}"
6540 * JUMP -> finally
6541 * catch1: PUSH exeception
6542 * EVAL {expr}
6543 * MATCH
6544 * JUMP nomatch -> catch2
6545 * CATCH remove exception
6546 * ... catch block
6547 * " catch"
6548 * JUMP -> finally
6549 * catch2: CATCH remove exception
6550 * ... catch block
6551 * " finally"
6552 * finally:
6553 * ... finally block
6554 * " endtry"
6555 * ENDTRY pop trystack entry, may rethrow
6556 */
6557 static char_u *
6558compile_try(char_u *arg, cctx_T *cctx)
6559{
6560 garray_T *instr = &cctx->ctx_instr;
6561 scope_T *try_scope;
6562 scope_T *scope;
6563
6564 // scope that holds the jumps that go to catch/finally/endtry
6565 try_scope = new_scope(cctx, TRY_SCOPE);
6566 if (try_scope == NULL)
6567 return NULL;
6568
6569 // "catch" is set when the first ":catch" is found.
6570 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006571 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 if (generate_instr(cctx, ISN_TRY) == NULL)
6573 return NULL;
6574
6575 // scope for the try block itself
6576 scope = new_scope(cctx, BLOCK_SCOPE);
6577 if (scope == NULL)
6578 return NULL;
6579
6580 return arg;
6581}
6582
6583/*
6584 * compile "catch {expr}"
6585 */
6586 static char_u *
6587compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6588{
6589 scope_T *scope = cctx->ctx_scope;
6590 garray_T *instr = &cctx->ctx_instr;
6591 char_u *p;
6592 isn_T *isn;
6593
6594 // end block scope from :try or :catch
6595 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6596 compile_endblock(cctx);
6597 scope = cctx->ctx_scope;
6598
6599 // Error if not in a :try scope
6600 if (scope == NULL || scope->se_type != TRY_SCOPE)
6601 {
6602 emsg(_(e_catch));
6603 return NULL;
6604 }
6605
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006606 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006608 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 return NULL;
6610 }
6611
6612 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006613 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 JUMP_ALWAYS, cctx) == FAIL)
6615 return NULL;
6616
6617 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006618 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 if (isn->isn_arg.try.try_catch == 0)
6620 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006621 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 {
6623 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006624 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 isn->isn_arg.jump.jump_where = instr->ga_len;
6626 }
6627
6628 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006629 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006630 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006631 scope->se_u.se_try.ts_caught_all = TRUE;
6632 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 }
6634 else
6635 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006636 char_u *end;
6637 char_u *pat;
6638 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006639 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006640 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 // Push v:exception, push {expr} and MATCH
6643 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6644
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006645 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006646 if (*end != *p)
6647 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006648 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006649 vim_free(tofree);
6650 return FAIL;
6651 }
6652 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006653 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006654 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006655 len = (int)(end - tofree);
6656 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006657 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006658 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006659 if (pat == NULL)
6660 return FAIL;
6661 if (generate_PUSHS(cctx, pat) == FAIL)
6662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6665 return NULL;
6666
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006667 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6669 return NULL;
6670 }
6671
6672 if (generate_instr(cctx, ISN_CATCH) == NULL)
6673 return NULL;
6674
6675 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6676 return NULL;
6677 return p;
6678}
6679
6680 static char_u *
6681compile_finally(char_u *arg, cctx_T *cctx)
6682{
6683 scope_T *scope = cctx->ctx_scope;
6684 garray_T *instr = &cctx->ctx_instr;
6685 isn_T *isn;
6686
6687 // end block scope from :try or :catch
6688 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6689 compile_endblock(cctx);
6690 scope = cctx->ctx_scope;
6691
6692 // Error if not in a :try scope
6693 if (scope == NULL || scope->se_type != TRY_SCOPE)
6694 {
6695 emsg(_(e_finally));
6696 return NULL;
6697 }
6698
6699 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006700 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 if (isn->isn_arg.try.try_finally != 0)
6702 {
6703 emsg(_(e_finally_dup));
6704 return NULL;
6705 }
6706
6707 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006708 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709
Bram Moolenaar585fea72020-04-02 22:33:21 +02006710 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006711 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 {
6713 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006714 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006716 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 }
6718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 // TODO: set index in ts_finally_label jumps
6720
6721 return arg;
6722}
6723
6724 static char_u *
6725compile_endtry(char_u *arg, cctx_T *cctx)
6726{
6727 scope_T *scope = cctx->ctx_scope;
6728 garray_T *instr = &cctx->ctx_instr;
6729 isn_T *isn;
6730
6731 // end block scope from :catch or :finally
6732 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6733 compile_endblock(cctx);
6734 scope = cctx->ctx_scope;
6735
6736 // Error if not in a :try scope
6737 if (scope == NULL || scope->se_type != TRY_SCOPE)
6738 {
6739 if (scope == NULL)
6740 emsg(_(e_no_endtry));
6741 else if (scope->se_type == WHILE_SCOPE)
6742 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006743 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 emsg(_(e_endfor));
6745 else
6746 emsg(_(e_endif));
6747 return NULL;
6748 }
6749
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006750 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6752 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006753 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 return NULL;
6755 }
6756
6757 // Fill in the "end" label in jumps at the end of the blocks, if not done
6758 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006759 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
6761 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006762 if (isn->isn_arg.try.try_catch == 0)
6763 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 if (isn->isn_arg.try.try_finally == 0)
6765 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006766
6767 if (scope->se_u.se_try.ts_catch_label != 0)
6768 {
6769 // Last catch without match jumps here
6770 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6771 isn->isn_arg.jump.jump_where = instr->ga_len;
6772 }
6773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 compile_endblock(cctx);
6775
6776 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6777 return NULL;
6778 return arg;
6779}
6780
6781/*
6782 * compile "throw {expr}"
6783 */
6784 static char_u *
6785compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6786{
6787 char_u *p = skipwhite(arg);
6788
Bram Moolenaara5565e42020-05-09 15:44:01 +02006789 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 return NULL;
6791 if (may_generate_2STRING(-1, cctx) == FAIL)
6792 return NULL;
6793 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6794 return NULL;
6795
6796 return p;
6797}
6798
6799/*
6800 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006801 * compile "echomsg expr"
6802 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006803 * compile "execute expr"
6804 */
6805 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006806compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006807{
6808 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006809 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006810 int count = 0;
6811
6812 for (;;)
6813 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006814 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006815 return NULL;
6816 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006817 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006818 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006819 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006820 break;
6821 }
6822
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006823 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6824 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6825 else if (cmdidx == CMD_execute)
6826 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6827 else if (cmdidx == CMD_echomsg)
6828 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6829 else
6830 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 return p;
6832}
6833
6834/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006835 * :put r
6836 * :put ={expr}
6837 */
6838 static char_u *
6839compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6840{
6841 char_u *line = arg;
6842 linenr_T lnum;
6843 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006844 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006845
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006846 eap->regname = *line;
6847
6848 if (eap->regname == '=')
6849 {
6850 char_u *p = line + 1;
6851
6852 if (compile_expr0(&p, cctx) == FAIL)
6853 return NULL;
6854 line = p;
6855 }
6856 else if (eap->regname != NUL)
6857 ++line;
6858
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006859 // "errormsg" will not be set because the range is ADDR_LINES.
6860 // TODO: if the range contains something like "$" or "." need to evaluate
6861 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006862 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6863 return NULL;
6864 if (eap->addr_count == 0)
6865 lnum = -1;
6866 else
6867 lnum = eap->line2;
6868 if (above)
6869 --lnum;
6870
6871 generate_PUT(cctx, eap->regname, lnum);
6872 return line;
6873}
6874
6875/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006876 * A command that is not compiled, execute with legacy code.
6877 */
6878 static char_u *
6879compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6880{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006881 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006882 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006883 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006884
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006885 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006886 goto theend;
6887
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006888 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006889 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006890 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006891 int usefilter = FALSE;
6892
6893 has_expr = argt & (EX_XFILE | EX_EXPAND);
6894
6895 // If the command can be followed by a bar, find the bar and truncate
6896 // it, so that the following command can be compiled.
6897 // The '|' is overwritten with a NUL, it is put back below.
6898 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6899 && *eap->arg == '!')
6900 // :w !filter or :r !filter or :r! filter
6901 usefilter = TRUE;
6902 if ((argt & EX_TRLBAR) && !usefilter)
6903 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006904 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006905 separate_nextcmd(eap);
6906 if (eap->nextcmd != NULL)
6907 nextcmd = eap->nextcmd;
6908 }
6909 }
6910
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006911 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6912 {
6913 // expand filename in "syntax include [@group] filename"
6914 has_expr = TRUE;
6915 eap->arg = skipwhite(eap->arg + 7);
6916 if (*eap->arg == '@')
6917 eap->arg = skiptowhite(eap->arg);
6918 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006919
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006920 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006921 {
6922 int count = 0;
6923 char_u *start = skipwhite(line);
6924
6925 // :cmd xxx`=expr1`yyy`=expr2`zzz
6926 // PUSHS ":cmd xxx"
6927 // eval expr1
6928 // PUSHS "yyy"
6929 // eval expr2
6930 // PUSHS "zzz"
6931 // EXECCONCAT 5
6932 for (;;)
6933 {
6934 if (p > start)
6935 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006936 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006937 ++count;
6938 }
6939 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006940 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006941 return NULL;
6942 may_generate_2STRING(-1, cctx);
6943 ++count;
6944 p = skipwhite(p);
6945 if (*p != '`')
6946 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006947 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006948 return NULL;
6949 }
6950 start = p + 1;
6951
6952 p = (char_u *)strstr((char *)start, "`=");
6953 if (p == NULL)
6954 {
6955 if (*skipwhite(start) != NUL)
6956 {
6957 generate_PUSHS(cctx, vim_strsave(start));
6958 ++count;
6959 }
6960 break;
6961 }
6962 }
6963 generate_EXECCONCAT(cctx, count);
6964 }
6965 else
6966 generate_EXEC(cctx, line);
6967
6968theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006969 if (*nextcmd != NUL)
6970 {
6971 // the parser expects a pointer to the bar, put it back
6972 --nextcmd;
6973 *nextcmd = '|';
6974 }
6975
6976 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006977}
6978
6979/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006980 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006981 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006982 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006983 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006984add_def_function(ufunc_T *ufunc)
6985{
6986 dfunc_T *dfunc;
6987
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006988 if (def_functions.ga_len == 0)
6989 {
6990 // The first position is not used, so that a zero uf_dfunc_idx means it
6991 // wasn't set.
6992 if (ga_grow(&def_functions, 1) == FAIL)
6993 return FAIL;
6994 ++def_functions.ga_len;
6995 }
6996
Bram Moolenaar09689a02020-05-09 22:50:08 +02006997 // Add the function to "def_functions".
6998 if (ga_grow(&def_functions, 1) == FAIL)
6999 return FAIL;
7000 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7001 CLEAR_POINTER(dfunc);
7002 dfunc->df_idx = def_functions.ga_len;
7003 ufunc->uf_dfunc_idx = dfunc->df_idx;
7004 dfunc->df_ufunc = ufunc;
7005 ++def_functions.ga_len;
7006 return OK;
7007}
7008
7009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 * After ex_function() has collected all the function lines: parse and compile
7011 * the lines into instructions.
7012 * Adds the function to "def_functions".
7013 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7014 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007015 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007016 * This can be used recursively through compile_lambda(), which may reallocate
7017 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007018 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007020 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007021compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 char_u *line = NULL;
7024 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 cctx_T cctx;
7027 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007028 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 int ret = FAIL;
7030 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007031 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007032 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007033 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007035 // When using a function that was compiled before: Free old instructions.
7036 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007037 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7040 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007041 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007043 else
7044 {
7045 if (add_def_function(ufunc) == FAIL)
7046 return FAIL;
7047 new_def_function = TRUE;
7048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049
Bram Moolenaar985116a2020-07-12 17:31:09 +02007050 ufunc->uf_def_status = UF_COMPILING;
7051
Bram Moolenaara80faa82020-04-12 19:37:17 +02007052 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 cctx.ctx_ufunc = ufunc;
7054 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007055 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7057 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7058 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7059 cctx.ctx_type_list = &ufunc->uf_type_list;
7060 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7061 instr = &cctx.ctx_instr;
7062
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007063 // Set the context to the function, it may be compiled when called from
7064 // another script. Set the script version to the most modern one.
7065 // The line number will be set in next_line_from_context().
7066 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7068
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007069 // Make sure error messages are OK.
7070 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7071 if (do_estack_push)
7072 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007073 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007074
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007075 if (ufunc->uf_def_args.ga_len > 0)
7076 {
7077 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007078 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007079 int i;
7080 char_u *arg;
7081 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007082 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007083
7084 // Produce instructions for the default values of optional arguments.
7085 // Store the instruction index in uf_def_arg_idx[] so that we know
7086 // where to start when the function is called, depending on the number
7087 // of arguments.
7088 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7089 if (ufunc->uf_def_arg_idx == NULL)
7090 goto erret;
7091 for (i = 0; i < count; ++i)
7092 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007093 garray_T *stack = &cctx.ctx_type_stack;
7094 type_T *val_type;
7095 int arg_idx = first_def_arg + i;
7096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007097 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7098 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007099 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007100 goto erret;
7101
7102 // If no type specified use the type of the default value.
7103 // Otherwise check that the default value type matches the
7104 // specified type.
7105 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7106 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007107 {
7108 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007109 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007110 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007111 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7112 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007113 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007114
7115 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007116 goto erret;
7117 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007118 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007119
7120 if (did_set_arg_type)
7121 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007122 }
7123
7124 /*
7125 * Loop over all the lines of the function and generate instructions.
7126 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 for (;;)
7128 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007129 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007130 int starts_with_colon = FALSE;
7131 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007132 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007133
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007134 // Bail out on the first error to avoid a flood of errors and report
7135 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007136 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007137 goto erret;
7138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 if (line != NULL && *line == '|')
7140 // the line continues after a '|'
7141 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007142 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007143 && !(*line == '#' && (line == cctx.ctx_line_start
7144 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007146 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 goto erret;
7148 }
7149 else
7150 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007151 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007152 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007153 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 }
7156
Bram Moolenaara80faa82020-04-12 19:37:17 +02007157 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 ea.cmdlinep = &line;
7159 ea.cmd = skipwhite(line);
7160
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007161 // Some things can be recognized by the first character.
7162 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007164 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007165 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007166 if (ea.cmd[1] != '{')
7167 {
7168 line = (char_u *)"";
7169 continue;
7170 }
7171 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007173 case '}':
7174 {
7175 // "}" ends a block scope
7176 scopetype_T stype = cctx.ctx_scope == NULL
7177 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007179 if (stype == BLOCK_SCOPE)
7180 {
7181 compile_endblock(&cctx);
7182 line = ea.cmd;
7183 }
7184 else
7185 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007186 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007187 goto erret;
7188 }
7189 if (line != NULL)
7190 line = skipwhite(ea.cmd + 1);
7191 continue;
7192 }
7193
7194 case '{':
7195 // "{" starts a block scope
7196 // "{'a': 1}->func() is something else
7197 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7198 {
7199 line = compile_block(ea.cmd, &cctx);
7200 continue;
7201 }
7202 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 }
7204
7205 /*
7206 * COMMAND MODIFIERS
7207 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007208 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007209 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7210 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 {
7212 if (errormsg != NULL)
7213 goto erret;
7214 // empty line or comment
7215 line = (char_u *)"";
7216 continue;
7217 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007218 generate_cmdmods(&cctx, &local_cmdmod);
7219 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007221 // Check if there was a colon after the last command modifier or before
7222 // the current position.
7223 for (p = ea.cmd; p >= line; --p)
7224 {
7225 if (*p == ':')
7226 starts_with_colon = TRUE;
7227 if (p < ea.cmd && !VIM_ISWHITE(*p))
7228 break;
7229 }
7230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007232 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007234 {
7235 if (*ea.cmd == '(')
7236 // not for "call()"
7237 ea.cmd = p;
7238 else
7239 ea.cmd = skipwhite(ea.cmd);
7240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007242 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007244 char_u *pskip;
7245
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007246 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007247 // find what follows.
7248 // Skip over "var.member", "var[idx]" and the like.
7249 // Also "&opt = val", "$ENV = val" and "@r = val".
7250 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007251 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007252 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007253 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007255 char_u *var_end;
7256 int oplen;
7257 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258
Bram Moolenaar65821722020-08-02 18:58:54 +02007259 if (ea.cmd[0] == '@')
7260 var_end = ea.cmd + 2;
7261 else
7262 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007263 FNE_CHECK_START | FNE_INCL_BR);
7264 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007265 if (oplen > 0)
7266 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007267 size_t len = p - ea.cmd;
7268
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007269 // Recognize an assignment if we recognize the variable
7270 // name:
7271 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007272 // "local = expr" where "local" is a local var.
7273 // "script = expr" where "script" is a script-local var.
7274 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007275 // "&opt = expr"
7276 // "$ENV = expr"
7277 // "@r = expr"
7278 if (*ea.cmd == '&'
7279 || *ea.cmd == '$'
7280 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007281 || ((len) > 2 && ea.cmd[1] == ':')
7282 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007283 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007284 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007285 || script_var_exists(ea.cmd, len,
7286 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007287 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007288 {
7289 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007290 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007291 goto erret;
7292 continue;
7293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 }
7295 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007296
7297 if (*ea.cmd == '[')
7298 {
7299 // [var, var] = expr
7300 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7301 if (line == NULL)
7302 goto erret;
7303 if (line != ea.cmd)
7304 continue;
7305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 }
7307
7308 /*
7309 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007310 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007312 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007313 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007314 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007315 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007316 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007317 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007318 if (!starts_with_colon)
7319 {
7320 emsg(_(e_colon_required_before_a_range));
7321 goto erret;
7322 }
7323 if (ends_excmd2(line, ea.cmd))
7324 {
7325 // A range without a command: jump to the line.
7326 // TODO: compile to a more efficient command, possibly
7327 // calling parse_cmd_address().
7328 ea.cmdidx = CMD_SIZE;
7329 line = compile_exec(line, &ea, &cctx);
7330 goto nextline;
7331 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007332 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007333 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007334 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007335 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007336 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337
7338 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7339 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007340 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007341 {
7342 line += STRLEN(line);
7343 continue;
7344 }
7345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007347 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007349 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007350 iemsg("Command from find_ex_command() not handled");
7351 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 }
7354
Bram Moolenaar3988f642020-08-27 22:43:03 +02007355 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007356 && ea.cmdidx != CMD_elseif
7357 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007358 && ea.cmdidx != CMD_endif
7359 && ea.cmdidx != CMD_endfor
7360 && ea.cmdidx != CMD_endwhile
7361 && ea.cmdidx != CMD_catch
7362 && ea.cmdidx != CMD_finally
7363 && ea.cmdidx != CMD_endtry)
7364 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007365 emsg(_(e_unreachable_code_after_return));
7366 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007367 }
7368
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007369 p = skipwhite(p);
7370 if (ea.cmdidx != CMD_SIZE
7371 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7372 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007373 if (ea.cmdidx >= 0)
7374 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007375 if ((ea.argt & EX_BANG) && *p == '!')
7376 {
7377 ea.forceit = TRUE;
7378 p = skipwhite(p + 1);
7379 }
7380 }
7381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 switch (ea.cmdidx)
7383 {
7384 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007385 ea.arg = p;
7386 line = compile_nested_function(&ea, &cctx);
7387 break;
7388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007390 // TODO: should we allow this, e.g. to declare a global
7391 // function?
7392 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 goto erret;
7394
7395 case CMD_return:
7396 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007397 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 break;
7399
7400 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007401 emsg(_(e_cannot_use_let_in_vim9_script));
7402 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007403 case CMD_var:
7404 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 case CMD_const:
7406 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007407 if (line == p)
7408 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 break;
7410
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007411 case CMD_unlet:
7412 case CMD_unlockvar:
7413 case CMD_lockvar:
7414 line = compile_unletlock(p, &ea, &cctx);
7415 break;
7416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 case CMD_import:
7418 line = compile_import(p, &cctx);
7419 break;
7420
7421 case CMD_if:
7422 line = compile_if(p, &cctx);
7423 break;
7424 case CMD_elseif:
7425 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007426 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 break;
7428 case CMD_else:
7429 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007430 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 break;
7432 case CMD_endif:
7433 line = compile_endif(p, &cctx);
7434 break;
7435
7436 case CMD_while:
7437 line = compile_while(p, &cctx);
7438 break;
7439 case CMD_endwhile:
7440 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007441 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 break;
7443
7444 case CMD_for:
7445 line = compile_for(p, &cctx);
7446 break;
7447 case CMD_endfor:
7448 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007449 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 break;
7451 case CMD_continue:
7452 line = compile_continue(p, &cctx);
7453 break;
7454 case CMD_break:
7455 line = compile_break(p, &cctx);
7456 break;
7457
7458 case CMD_try:
7459 line = compile_try(p, &cctx);
7460 break;
7461 case CMD_catch:
7462 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007463 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 break;
7465 case CMD_finally:
7466 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007467 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 break;
7469 case CMD_endtry:
7470 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007471 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 break;
7473 case CMD_throw:
7474 line = compile_throw(p, &cctx);
7475 break;
7476
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007477 case CMD_eval:
7478 if (compile_expr0(&p, &cctx) == FAIL)
7479 goto erret;
7480
Bram Moolenaar3988f642020-08-27 22:43:03 +02007481 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007482 generate_instr_drop(&cctx, ISN_DROP, 1);
7483
7484 line = skipwhite(p);
7485 break;
7486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007489 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007490 case CMD_echomsg:
7491 case CMD_echoerr:
7492 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007493 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007495 case CMD_put:
7496 ea.cmd = cmd;
7497 line = compile_put(p, &ea, &cctx);
7498 break;
7499
Bram Moolenaar3988f642020-08-27 22:43:03 +02007500 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007501
Bram Moolenaarae616492020-07-28 20:07:27 +02007502 case CMD_append:
7503 case CMD_change:
7504 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007505 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007506 case CMD_xit:
7507 not_in_vim9(&ea);
7508 goto erret;
7509
Bram Moolenaar002262f2020-07-08 17:47:57 +02007510 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007511 if (cctx.ctx_skip != SKIP_YES)
7512 {
7513 semsg(_(e_invalid_command_str), ea.cmd);
7514 goto erret;
7515 }
7516 // We don't check for a next command here.
7517 line = (char_u *)"";
7518 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007521 if (cctx.ctx_skip == SKIP_YES)
7522 {
7523 // We don't check for a next command here.
7524 line = (char_u *)"";
7525 }
7526 else
7527 {
7528 // Not recognized, execute with do_cmdline_cmd().
7529 ea.arg = p;
7530 line = compile_exec(line, &ea, &cctx);
7531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 break;
7533 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007534nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535 if (line == NULL)
7536 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007537 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007539 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007540 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 if (cctx.ctx_type_stack.ga_len < 0)
7543 {
7544 iemsg("Type stack underflow");
7545 goto erret;
7546 }
7547 }
7548
7549 if (cctx.ctx_scope != NULL)
7550 {
7551 if (cctx.ctx_scope->se_type == IF_SCOPE)
7552 emsg(_(e_endif));
7553 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7554 emsg(_(e_endwhile));
7555 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7556 emsg(_(e_endfor));
7557 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007558 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 goto erret;
7560 }
7561
Bram Moolenaarefd88552020-06-18 20:50:10 +02007562 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 {
7564 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007566 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 goto erret;
7568 }
7569
7570 // Return zero if there is no return at the end.
7571 generate_PUSHNR(&cctx, 0);
7572 generate_instr(&cctx, ISN_RETURN);
7573 }
7574
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007575 {
7576 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7577 + ufunc->uf_dfunc_idx;
7578 dfunc->df_deleted = FALSE;
7579 dfunc->df_instr = instr->ga_data;
7580 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007581 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007582 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007583 if (cctx.ctx_outer_used)
7584 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007585 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587
7588 ret = OK;
7589
7590erret:
7591 if (ret == FAIL)
7592 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007593 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007594 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7595 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007596
7597 for (idx = 0; idx < instr->ga_len; ++idx)
7598 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007600
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007601 // If using the last entry in the table and it was added above, we
7602 // might as well remove it.
7603 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007604 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007605 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007606 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007607 ufunc->uf_dfunc_idx = 0;
7608 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007609 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007610
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007611 while (cctx.ctx_scope != NULL)
7612 drop_scope(&cctx);
7613
Bram Moolenaar20431c92020-03-20 18:39:46 +01007614 // Don't execute this function body.
7615 ga_clear_strings(&ufunc->uf_lines);
7616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 if (errormsg != NULL)
7618 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007619 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007620 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 }
7622
7623 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007624 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007625 if (do_estack_push)
7626 estack_pop();
7627
Bram Moolenaar20431c92020-03-20 18:39:46 +01007628 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007629 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007631 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007632}
7633
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007634 void
7635set_function_type(ufunc_T *ufunc)
7636{
7637 int varargs = ufunc->uf_va_name != NULL;
7638 int argcount = ufunc->uf_args.ga_len;
7639
7640 // Create a type for the function, with the return type and any
7641 // argument types.
7642 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7643 // The type is included in "tt_args".
7644 if (argcount > 0 || varargs)
7645 {
7646 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7647 argcount, &ufunc->uf_type_list);
7648 // Add argument types to the function type.
7649 if (func_type_add_arg_types(ufunc->uf_func_type,
7650 argcount + varargs,
7651 &ufunc->uf_type_list) == FAIL)
7652 return;
7653 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7654 ufunc->uf_func_type->tt_min_argcount =
7655 argcount - ufunc->uf_def_args.ga_len;
7656 if (ufunc->uf_arg_types == NULL)
7657 {
7658 int i;
7659
7660 // lambda does not have argument types.
7661 for (i = 0; i < argcount; ++i)
7662 ufunc->uf_func_type->tt_args[i] = &t_any;
7663 }
7664 else
7665 mch_memmove(ufunc->uf_func_type->tt_args,
7666 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7667 if (varargs)
7668 {
7669 ufunc->uf_func_type->tt_args[argcount] =
7670 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7671 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7672 }
7673 }
7674 else
7675 // No arguments, can use a predefined type.
7676 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7677 argcount, &ufunc->uf_type_list);
7678}
7679
7680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681/*
7682 * Delete an instruction, free what it contains.
7683 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007684 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685delete_instr(isn_T *isn)
7686{
7687 switch (isn->isn_type)
7688 {
7689 case ISN_EXEC:
7690 case ISN_LOADENV:
7691 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007692 case ISN_LOADB:
7693 case ISN_LOADW:
7694 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007696 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 case ISN_PUSHEXC:
7698 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007699 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007701 case ISN_STOREB:
7702 case ISN_STOREW:
7703 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007704 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 vim_free(isn->isn_arg.string);
7706 break;
7707
7708 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007709 case ISN_STORES:
7710 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711 break;
7712
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007713 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007714 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007715 vim_free(isn->isn_arg.unlet.ul_name);
7716 break;
7717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 case ISN_STOREOPT:
7719 vim_free(isn->isn_arg.storeopt.so_name);
7720 break;
7721
7722 case ISN_PUSHBLOB: // push blob isn_arg.blob
7723 blob_unref(isn->isn_arg.blob);
7724 break;
7725
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007726 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007728 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007729#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007730 break;
7731
7732 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007733#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007734 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007735#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007736 break;
7737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 case ISN_UCALL:
7739 vim_free(isn->isn_arg.ufunc.cuf_name);
7740 break;
7741
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007742 case ISN_FUNCREF:
7743 {
7744 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7745 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007746
7747 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7748 func_ptr_unref(dfunc->df_ufunc);
7749 }
7750 break;
7751
7752 case ISN_DCALL:
7753 {
7754 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7755 + isn->isn_arg.dfunc.cdf_idx;
7756
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007757 if (dfunc->df_ufunc != NULL
7758 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007759 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007760 }
7761 break;
7762
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007763 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007764 {
7765 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7766 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7767
7768 if (ufunc != NULL)
7769 {
7770 // Clear uf_dfunc_idx so that the function is deleted.
7771 clear_def_function(ufunc);
7772 ufunc->uf_dfunc_idx = 0;
7773 func_ptr_unref(ufunc);
7774 }
7775
7776 vim_free(lambda);
7777 vim_free(isn->isn_arg.newfunc.nf_global);
7778 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007779 break;
7780
Bram Moolenaar5e654232020-09-16 15:22:00 +02007781 case ISN_CHECKTYPE:
7782 free_type(isn->isn_arg.type.ct_type);
7783 break;
7784
Bram Moolenaar02194d22020-10-24 23:08:38 +02007785 case ISN_CMDMOD:
7786 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7787 ->cmod_filter_regmatch.regprog);
7788 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7789 break;
7790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 case ISN_2BOOL:
7792 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007793 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 case ISN_ADDBLOB:
7795 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007796 case ISN_ANYINDEX:
7797 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007799 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007801 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007803 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804 case ISN_COMPAREANY:
7805 case ISN_COMPAREBLOB:
7806 case ISN_COMPAREBOOL:
7807 case ISN_COMPAREDICT:
7808 case ISN_COMPAREFLOAT:
7809 case ISN_COMPAREFUNC:
7810 case ISN_COMPARELIST:
7811 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 case ISN_COMPARESPECIAL:
7813 case ISN_COMPARESTRING:
7814 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007815 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 case ISN_DROP:
7817 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007818 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007819 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007821 case ISN_EXECCONCAT:
7822 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007824 case ISN_GETITEM:
7825 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007826 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007827 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007828 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007830 case ISN_LOADBDICT:
7831 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007832 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007834 case ISN_LOADSCRIPT:
7835 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007837 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007838 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007839 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 case ISN_NEGATENR:
7841 case ISN_NEWDICT:
7842 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007844 case ISN_OPFLOAT:
7845 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007847 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007848 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 case ISN_PUSHF:
7850 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007852 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007854 case ISN_SHUFFLE:
7855 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007856 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007857 case ISN_STOREDICT:
7858 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007859 case ISN_STORENR:
7860 case ISN_STOREOUTER:
7861 case ISN_STOREREG:
7862 case ISN_STORESCRIPT:
7863 case ISN_STOREV:
7864 case ISN_STRINDEX:
7865 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 case ISN_THROW:
7867 case ISN_TRY:
7868 // nothing allocated
7869 break;
7870 }
7871}
7872
7873/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007874 * Free all instructions for "dfunc".
7875 */
7876 static void
7877delete_def_function_contents(dfunc_T *dfunc)
7878{
7879 int idx;
7880
7881 ga_clear(&dfunc->df_def_args_isn);
7882
7883 if (dfunc->df_instr != NULL)
7884 {
7885 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7886 delete_instr(dfunc->df_instr + idx);
7887 VIM_CLEAR(dfunc->df_instr);
7888 }
7889
7890 dfunc->df_deleted = TRUE;
7891}
7892
7893/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007894 * When a user function is deleted, clear the contents of any associated def
7895 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 */
7897 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007898clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007900 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 {
7902 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7903 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904
Bram Moolenaar20431c92020-03-20 18:39:46 +01007905 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007906 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007907 }
7908}
7909
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007910/*
7911 * Used when a user function is about to be deleted: remove the pointer to it.
7912 * The entry in def_functions is then unused.
7913 */
7914 void
7915unlink_def_function(ufunc_T *ufunc)
7916{
7917 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7918
7919 dfunc->df_ufunc = NULL;
7920}
7921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007923/*
7924 * Free all functions defined with ":def".
7925 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 void
7927free_def_functions(void)
7928{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007929 int idx;
7930
7931 for (idx = 0; idx < def_functions.ga_len; ++idx)
7932 {
7933 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7934
7935 delete_def_function_contents(dfunc);
7936 }
7937
7938 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939}
7940#endif
7941
7942
7943#endif // FEAT_EVAL