blob: 5ade42ad5fdabd0c5eba9c7e5261c6dccad31a6f [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);
2623 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2624 {
2625 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2626
2627 *arg = s + 1;
2628 argvars[1].v_type = VAR_UNKNOWN;
2629 tv->v_type = VAR_NUMBER;
2630 tv->vval.v_number = 0;
2631 f_has(argvars, tv);
2632 clear_tv(&argvars[0]);
2633 ++ppconst->pp_used;
2634 return OK;
2635 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002636 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002637 }
2638
2639 if (generate_ppconst(cctx, ppconst) == FAIL)
2640 return FAIL;
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 if (varlen >= sizeof(namebuf))
2643 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002644 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 return FAIL;
2646 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002647 vim_strncpy(namebuf, *arg, varlen);
2648 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 *arg = skipwhite(*arg + varlen + 1);
2651 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002652 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653
Bram Moolenaara1773442020-08-12 15:21:22 +02002654 is_autoload = vim_strchr(name, '#') != NULL;
2655 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 {
2657 int idx;
2658
2659 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002660 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002662 {
2663 if (STRCMP(name, "add") == 0 && argcount == 2)
2664 {
2665 garray_T *stack = &cctx->ctx_type_stack;
2666 type_T *type = ((type_T **)stack->ga_data)[
2667 stack->ga_len - 2];
2668
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002669 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002670 if (type->tt_type == VAR_LIST)
2671 {
2672 // inline "add(list, item)" so that the type can be checked
2673 res = generate_LISTAPPEND(cctx);
2674 idx = -1;
2675 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002676 else if (type->tt_type == VAR_BLOB)
2677 {
2678 // inline "add(blob, nr)" so that the type can be checked
2679 res = generate_BLOBAPPEND(cctx);
2680 idx = -1;
2681 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002682 }
2683
2684 if (idx >= 0)
2685 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2686 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002687 else
2688 semsg(_(e_unknownfunc), namebuf);
2689 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 }
2691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002693 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002694 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002695 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002696 {
2697 res = generate_CALL(cctx, ufunc, argcount);
2698 goto theend;
2699 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700
2701 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002702 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002703 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002705 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002706 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002707 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002708 garray_T *stack = &cctx->ctx_type_stack;
2709 type_T *type;
2710
2711 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2712 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002713 goto theend;
2714 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715
Bram Moolenaar0f769812020-09-12 18:32:34 +02002716 // If we can find a global function by name generate the right call.
2717 if (ufunc != NULL)
2718 {
2719 res = generate_CALL(cctx, ufunc, argcount);
2720 goto theend;
2721 }
2722
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002723 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002724 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002725 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002726 res = generate_UCALL(cctx, name, argcount);
2727 else
2728 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002729
2730theend:
2731 vim_free(tofree);
2732 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733}
2734
2735// like NAMESPACE_CHAR but with 'a' and 'l'.
2736#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2737
2738/*
2739 * Find the end of a variable or function name. Unlike find_name_end() this
2740 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002741 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 * Return a pointer to just after the name. Equal to "arg" if there is no
2743 * valid name.
2744 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002745 static char_u *
2746to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747{
2748 char_u *p;
2749
2750 // Quick check for valid starting character.
2751 if (!eval_isnamec1(*arg))
2752 return arg;
2753
2754 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2755 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2756 // and can be used in slice "[n:]".
2757 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002758 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2760 break;
2761 return p;
2762}
2763
2764/*
2765 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002766 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 */
2768 char_u *
2769to_name_const_end(char_u *arg)
2770{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002771 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 typval_T rettv;
2773
2774 if (p == arg && *arg == '[')
2775 {
2776
2777 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002778 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 p = arg;
2780 }
2781 else if (p == arg && *arg == '#' && arg[1] == '{')
2782 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002783 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002785 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 p = arg;
2787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 return p;
2790}
2791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792/*
2793 * parse a list: [expr, expr]
2794 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002795 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 */
2797 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002798compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799{
2800 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002801 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002803 int is_const;
2804 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002806 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002808 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002809 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002810 semsg(_(e_list_end), *arg);
2811 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002812 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002813 if (*p == ',')
2814 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002815 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002816 return FAIL;
2817 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002818 if (*p == ']')
2819 {
2820 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002821 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002822 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002823 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002824 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002825 if (!is_const)
2826 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 ++count;
2828 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002829 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002831 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2832 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002833 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002834 return FAIL;
2835 }
2836 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002837 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 p = skipwhite(p);
2839 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002840 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002842 ppconst->pp_is_const = is_all_const;
2843 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844}
2845
2846/*
2847 * parse a lambda: {arg, arg -> expr}
2848 * "*arg" points to the '{'.
2849 */
2850 static int
2851compile_lambda(char_u **arg, cctx_T *cctx)
2852{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 typval_T rettv;
2854 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002855 evalarg_T evalarg;
2856
2857 CLEAR_FIELD(evalarg);
2858 evalarg.eval_flags = EVAL_EVALUATE;
2859 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860
2861 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002862 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002863 {
2864 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002866 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002869 ++ufunc->uf_refcount;
2870 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871
2872 // The function will have one line: "return {expr}".
2873 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002874 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002876 clear_evalarg(&evalarg, NULL);
2877
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002878 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002879 {
2880 // The return type will now be known.
2881 set_function_type(ufunc);
2882
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002883 // The function reference count will be 1. When the ISN_FUNCREF
2884 // instruction is deleted the reference count is decremented and the
2885 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002886 return generate_FUNCREF(cctx, ufunc);
2887 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002888
2889 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 return FAIL;
2891}
2892
2893/*
2894 * Compile a lamda call: expr->{lambda}(args)
2895 * "arg" points to the "{".
2896 */
2897 static int
2898compile_lambda_call(char_u **arg, cctx_T *cctx)
2899{
2900 ufunc_T *ufunc;
2901 typval_T rettv;
2902 int argcount = 1;
2903 int ret = FAIL;
2904
2905 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002906 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 return FAIL;
2908
2909 if (**arg != '(')
2910 {
2911 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002912 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 else
2914 semsg(_(e_missing_paren), "lambda");
2915 clear_tv(&rettv);
2916 return FAIL;
2917 }
2918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 ufunc = rettv.vval.v_partial->pt_func;
2920 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002921 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002922 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002923
Bram Moolenaara05e5242020-09-19 18:19:19 +02002924 // The function will have one line: "return {expr}". Compile it into
2925 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002926 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927
2928 // compile the arguments
2929 *arg = skipwhite(*arg + 1);
2930 if (compile_arguments(arg, cctx, &argcount) == OK)
2931 // call the compiled function
2932 ret = generate_CALL(cctx, ufunc, argcount);
2933
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002934 if (ret == FAIL)
2935 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return ret;
2937}
2938
2939/*
2940 * parse a dict: {'key': val} or #{key: val}
2941 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002942 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 */
2944 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002945compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946{
2947 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002948 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 int count = 0;
2950 dict_T *d = dict_alloc();
2951 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002952 char_u *whitep = *arg;
2953 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002954 int is_const;
2955 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 if (d == NULL)
2958 return FAIL;
2959 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002960 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 {
2962 char_u *key = NULL;
2963
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002965 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002966 *arg = NULL;
2967 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002968 }
2969
2970 if (**arg == '}')
2971 break;
2972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 if (literal)
2974 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002975 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
Bram Moolenaar2c330432020-04-13 14:41:35 +02002977 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002979 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 return FAIL;
2981 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002982 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 if (generate_PUSHS(cctx, key) == FAIL)
2984 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002985 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 }
2987 else
2988 {
2989 isn_T *isn;
2990
Bram Moolenaara5565e42020-05-09 15:44:01 +02002991 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2994 if (isn->isn_type == ISN_PUSHS)
2995 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002996 else
2997 {
2998 type_T *keytype = ((type_T **)stack->ga_data)
2999 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003000 if (need_type(keytype, &t_string, -1, cctx,
3001 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003002 return FAIL;
3003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 }
3005
3006 // Check for duplicate keys, if using string keys.
3007 if (key != NULL)
3008 {
3009 item = dict_find(d, key, -1);
3010 if (item != NULL)
3011 {
3012 semsg(_(e_duplicate_key), key);
3013 goto failret;
3014 }
3015 item = dictitem_alloc(key);
3016 if (item != NULL)
3017 {
3018 item->di_tv.v_type = VAR_UNKNOWN;
3019 item->di_tv.v_lock = 0;
3020 if (dict_add(d, item) == FAIL)
3021 dictitem_free(item);
3022 }
3023 }
3024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (**arg != ':')
3026 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003027 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003028 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003029 else
3030 semsg(_(e_missing_dict_colon), *arg);
3031 return FAIL;
3032 }
3033 whitep = *arg + 1;
3034 if (!IS_WHITE_OR_NUL(*whitep))
3035 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003036 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 return FAIL;
3038 }
3039
3040 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003041 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003042 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003043 *arg = NULL;
3044 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003045 }
3046
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003047 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003049 if (!is_const)
3050 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 ++count;
3052
Bram Moolenaar2c330432020-04-13 14:41:35 +02003053 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003054 *arg = skipwhite(*arg);
3055 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003056 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003057 *arg = NULL;
3058 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 if (**arg == '}')
3061 break;
3062 if (**arg != ',')
3063 {
3064 semsg(_(e_missing_dict_comma), *arg);
3065 goto failret;
3066 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003067 if (IS_WHITE_OR_NUL(*whitep))
3068 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003069 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003070 return FAIL;
3071 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003072 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003073 if (!IS_WHITE_OR_NUL(*whitep))
3074 {
3075 semsg(_(e_white_space_required_after_str), ",");
3076 return FAIL;
3077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 *arg = skipwhite(*arg + 1);
3079 }
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 *arg = *arg + 1;
3082
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003083 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003084 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003085 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003086 *arg += STRLEN(*arg);
3087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003089 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 return generate_NEWDICT(cctx, count);
3091
3092failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003093 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003094 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003095 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003096 *arg = (char_u *)"";
3097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 dict_unref(d);
3099 return FAIL;
3100}
3101
3102/*
3103 * Compile "&option".
3104 */
3105 static int
3106compile_get_option(char_u **arg, cctx_T *cctx)
3107{
3108 typval_T rettv;
3109 char_u *start = *arg;
3110 int ret;
3111
3112 // parse the option and get the current value to get the type.
3113 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 if (ret == OK)
3116 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003117 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 char_u *name = vim_strnsave(start, *arg - start);
3119 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3120
3121 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3122 vim_free(name);
3123 }
3124 clear_tv(&rettv);
3125
3126 return ret;
3127}
3128
3129/*
3130 * Compile "$VAR".
3131 */
3132 static int
3133compile_get_env(char_u **arg, cctx_T *cctx)
3134{
3135 char_u *start = *arg;
3136 int len;
3137 int ret;
3138 char_u *name;
3139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 ++*arg;
3141 len = get_env_len(arg);
3142 if (len == 0)
3143 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003144 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
3146 }
3147
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003148 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 name = vim_strnsave(start, len + 1);
3150 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3151 vim_free(name);
3152 return ret;
3153}
3154
3155/*
3156 * Compile "@r".
3157 */
3158 static int
3159compile_get_register(char_u **arg, cctx_T *cctx)
3160{
3161 int ret;
3162
3163 ++*arg;
3164 if (**arg == NUL)
3165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003166 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 return FAIL;
3168 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003169 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 {
3171 emsg_invreg(**arg);
3172 return FAIL;
3173 }
3174 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3175 ++*arg;
3176 return ret;
3177}
3178
3179/*
3180 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003181 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 */
3183 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003184apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003186 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187
3188 // this works from end to start
3189 while (p > start)
3190 {
3191 --p;
3192 if (*p == '-' || *p == '+')
3193 {
3194 // only '-' has an effect, for '+' we only check the type
3195#ifdef FEAT_FLOAT
3196 if (rettv->v_type == VAR_FLOAT)
3197 {
3198 if (*p == '-')
3199 rettv->vval.v_float = -rettv->vval.v_float;
3200 }
3201 else
3202#endif
3203 {
3204 varnumber_T val;
3205 int error = FALSE;
3206
3207 // tv_get_number_chk() accepts a string, but we don't want that
3208 // here
3209 if (check_not_string(rettv) == FAIL)
3210 return FAIL;
3211 val = tv_get_number_chk(rettv, &error);
3212 clear_tv(rettv);
3213 if (error)
3214 return FAIL;
3215 if (*p == '-')
3216 val = -val;
3217 rettv->v_type = VAR_NUMBER;
3218 rettv->vval.v_number = val;
3219 }
3220 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003221 else if (numeric_only)
3222 {
3223 ++p;
3224 break;
3225 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003226 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 {
3228 int v = tv2bool(rettv);
3229
3230 // '!' is permissive in the type.
3231 clear_tv(rettv);
3232 rettv->v_type = VAR_BOOL;
3233 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3234 }
3235 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003236 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 return OK;
3238}
3239
3240/*
3241 * Recognize v: variables that are constants and set "rettv".
3242 */
3243 static void
3244get_vim_constant(char_u **arg, typval_T *rettv)
3245{
3246 if (STRNCMP(*arg, "v:true", 6) == 0)
3247 {
3248 rettv->v_type = VAR_BOOL;
3249 rettv->vval.v_number = VVAL_TRUE;
3250 *arg += 6;
3251 }
3252 else if (STRNCMP(*arg, "v:false", 7) == 0)
3253 {
3254 rettv->v_type = VAR_BOOL;
3255 rettv->vval.v_number = VVAL_FALSE;
3256 *arg += 7;
3257 }
3258 else if (STRNCMP(*arg, "v:null", 6) == 0)
3259 {
3260 rettv->v_type = VAR_SPECIAL;
3261 rettv->vval.v_number = VVAL_NULL;
3262 *arg += 6;
3263 }
3264 else if (STRNCMP(*arg, "v:none", 6) == 0)
3265 {
3266 rettv->v_type = VAR_SPECIAL;
3267 rettv->vval.v_number = VVAL_NONE;
3268 *arg += 6;
3269 }
3270}
3271
Bram Moolenaar696ba232020-07-29 21:20:41 +02003272 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003273get_compare_type(char_u *p, int *len, int *type_is)
3274{
3275 exptype_T type = EXPR_UNKNOWN;
3276 int i;
3277
3278 switch (p[0])
3279 {
3280 case '=': if (p[1] == '=')
3281 type = EXPR_EQUAL;
3282 else if (p[1] == '~')
3283 type = EXPR_MATCH;
3284 break;
3285 case '!': if (p[1] == '=')
3286 type = EXPR_NEQUAL;
3287 else if (p[1] == '~')
3288 type = EXPR_NOMATCH;
3289 break;
3290 case '>': if (p[1] != '=')
3291 {
3292 type = EXPR_GREATER;
3293 *len = 1;
3294 }
3295 else
3296 type = EXPR_GEQUAL;
3297 break;
3298 case '<': if (p[1] != '=')
3299 {
3300 type = EXPR_SMALLER;
3301 *len = 1;
3302 }
3303 else
3304 type = EXPR_SEQUAL;
3305 break;
3306 case 'i': if (p[1] == 's')
3307 {
3308 // "is" and "isnot"; but not a prefix of a name
3309 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3310 *len = 5;
3311 i = p[*len];
3312 if (!isalnum(i) && i != '_')
3313 {
3314 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3315 *type_is = TRUE;
3316 }
3317 }
3318 break;
3319 }
3320 return type;
3321}
3322
3323/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003325 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 */
3327 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003328compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003330 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331
3332 // this works from end to start
3333 while (p > start)
3334 {
3335 --p;
3336 if (*p == '-' || *p == '+')
3337 {
3338 int negate = *p == '-';
3339 isn_T *isn;
3340
3341 // TODO: check type
3342 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3343 {
3344 --p;
3345 if (*p == '-')
3346 negate = !negate;
3347 }
3348 // only '-' has an effect, for '+' we only check the type
3349 if (negate)
3350 isn = generate_instr(cctx, ISN_NEGATENR);
3351 else
3352 isn = generate_instr(cctx, ISN_CHECKNR);
3353 if (isn == NULL)
3354 return FAIL;
3355 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003356 else if (numeric_only)
3357 {
3358 ++p;
3359 break;
3360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 else
3362 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003363 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003365 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003367 if (p[-1] == '!')
3368 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 }
3371 if (generate_2BOOL(cctx, invert) == FAIL)
3372 return FAIL;
3373 }
3374 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003375 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 return OK;
3377}
3378
3379/*
3380 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003381 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 */
3383 static int
3384compile_subscript(
3385 char_u **arg,
3386 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003387 char_u *start_leader,
3388 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003389 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003391 char_u *name_start = *end_leader;
3392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 for (;;)
3394 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003395 char_u *p = skipwhite(*arg);
3396
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003397 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003398 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003399 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003400
3401 // If a following line starts with "->{" or "->X" advance to that
3402 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003403 // Also if a following line starts with ".x".
3404 if (next != NULL &&
3405 ((next[0] == '-' && next[1] == '>'
3406 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003407 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003408 {
3409 next = next_line_from_context(cctx, TRUE);
3410 if (next == NULL)
3411 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003412 *arg = next;
3413 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003414 }
3415 }
3416
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003417 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3418 // not a function call.
3419 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003421 garray_T *stack = &cctx->ctx_type_stack;
3422 type_T *type;
3423 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003425 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003426 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003427 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003430 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3431
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003432 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3434 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003435 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 return FAIL;
3437 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003438 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003440 char_u *pstart = p;
3441
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003442 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003443 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003444 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 // something->method()
3447 // Apply the '!', '-' and '+' first:
3448 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003449 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003452 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003453 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003454 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 if (**arg == '{')
3456 {
3457 // lambda call: list->{lambda}
3458 if (compile_lambda_call(arg, cctx) == FAIL)
3459 return FAIL;
3460 }
3461 else
3462 {
3463 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003464 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003465 if (!eval_isnamec1(*p))
3466 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003467 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003468 return FAIL;
3469 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003470 if (ASCII_ISALPHA(*p) && p[1] == ':')
3471 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003472 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 ;
3474 if (*p != '(')
3475 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003476 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 return FAIL;
3478 }
3479 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003480 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return FAIL;
3482 }
3483 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003484 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003486 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003487 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003488 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003489 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003490 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003491
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003492 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003493 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003494 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003495 // TODO: blob index
3496 // TODO: more arguments
3497 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003498 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003499 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003500 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003501
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003502 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003503 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003504 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003505 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003506 if (**arg == ':')
3507 // missing first index is equal to zero
3508 generate_PUSHNR(cctx, 0);
3509 else
3510 {
3511 if (compile_expr0(arg, cctx) == FAIL)
3512 return FAIL;
3513 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3514 return FAIL;
3515 *arg = skipwhite(*arg);
3516 }
3517 if (**arg == ':')
3518 {
3519 *arg = skipwhite(*arg + 1);
3520 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3521 return FAIL;
3522 if (**arg == ']')
3523 // missing second index is equal to end of string
3524 generate_PUSHNR(cctx, -1);
3525 else
3526 {
3527 if (compile_expr0(arg, cctx) == FAIL)
3528 return FAIL;
3529 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3530 return FAIL;
3531 *arg = skipwhite(*arg);
3532 }
3533 is_slice = TRUE;
3534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535
3536 if (**arg != ']')
3537 {
3538 emsg(_(e_missbrac));
3539 return FAIL;
3540 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003541 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003543 // We can index a list and a dict. If we don't know the type
3544 // we can use the index value type.
3545 // TODO: If we don't know use an instruction to figure it out at
3546 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003547 typep = ((type_T **)stack->ga_data) + stack->ga_len
3548 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003549 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003550 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3551 // If the index is a string, the variable must be a Dict.
3552 if (*typep == &t_any && valtype == &t_string)
3553 vtype = VAR_DICT;
3554 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003555 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003556 if (need_type(valtype, &t_number, -1, cctx,
3557 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003558 return FAIL;
3559 if (is_slice)
3560 {
3561 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003562 if (need_type(valtype, &t_number, -2, cctx,
3563 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003564 return FAIL;
3565 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003566 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003567
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003568 if (vtype == VAR_DICT)
3569 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003570 if (is_slice)
3571 {
3572 emsg(_(e_cannot_slice_dictionary));
3573 return FAIL;
3574 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003575 if ((*typep)->tt_type == VAR_DICT)
3576 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003577 else
3578 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003579 if (need_type(*typep, &t_dict_any, -2, cctx,
3580 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003581 return FAIL;
3582 *typep = &t_any;
3583 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003584 if (may_generate_2STRING(-1, cctx) == FAIL)
3585 return FAIL;
3586 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3587 return FAIL;
3588 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003589 else if (vtype == VAR_STRING)
3590 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003591 *typep = &t_string;
3592 if ((is_slice
3593 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3594 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003595 return FAIL;
3596 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003597 else if (vtype == VAR_BLOB)
3598 {
3599 emsg("Sorry, blob index and slice not implemented yet");
3600 return FAIL;
3601 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003602 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003603 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003604 if (is_slice)
3605 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003606 if (generate_instr_drop(cctx,
3607 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3608 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003609 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003610 }
Bram Moolenaared591872020-08-15 22:14:53 +02003611 else
3612 {
3613 if ((*typep)->tt_type == VAR_LIST)
3614 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003615 if (generate_instr_drop(cctx,
3616 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3617 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003618 return FAIL;
3619 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003620 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003621 else
3622 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003623 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003624 return FAIL;
3625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003627 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003629 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003630 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003631 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003632 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003634 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003635 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003636 {
3637 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003638 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003639 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003640 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003641 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 while (eval_isnamec(*p))
3643 MB_PTR_ADV(p);
3644 if (p == *arg)
3645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003646 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 return FAIL;
3648 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003649 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 return FAIL;
3651 *arg = p;
3652 }
3653 else
3654 break;
3655 }
3656
3657 // TODO - see handle_subscript():
3658 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3659 // Don't do this when "Func" is already a partial that was bound
3660 // explicitly (pt_auto is FALSE).
3661
3662 return OK;
3663}
3664
3665/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003666 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3667 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003669 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003670 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003671 *
3672 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 */
3674
3675/*
3676 * number number constant
3677 * 0zFFFFFFFF Blob constant
3678 * "string" string constant
3679 * 'string' literal string constant
3680 * &option-name option value
3681 * @r register contents
3682 * identifier variable value
3683 * function() function call
3684 * $VAR environment variable
3685 * (expression) nested expression
3686 * [expr, expr] List
3687 * {key: val, key: val} Dictionary
3688 * #{key: val, key: val} Dictionary with literal keys
3689 *
3690 * Also handle:
3691 * ! in front logical NOT
3692 * - in front unary minus
3693 * + in front unary plus (ignored)
3694 * trailing (arg) funcref/partial call
3695 * trailing [] subscript in String or List
3696 * trailing .name entry in Dictionary
3697 * trailing ->name() method call
3698 */
3699 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700compile_expr7(
3701 char_u **arg,
3702 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003703 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 char_u *start_leader, *end_leader;
3706 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003707 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003708 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003710 ppconst->pp_is_const = FALSE;
3711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 /*
3713 * Skip '!', '-' and '+' characters. They are handled later.
3714 */
3715 start_leader = *arg;
3716 while (**arg == '!' || **arg == '-' || **arg == '+')
3717 *arg = skipwhite(*arg + 1);
3718 end_leader = *arg;
3719
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003720 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 switch (**arg)
3722 {
3723 /*
3724 * Number constant.
3725 */
3726 case '0': // also for blob starting with 0z
3727 case '1':
3728 case '2':
3729 case '3':
3730 case '4':
3731 case '5':
3732 case '6':
3733 case '7':
3734 case '8':
3735 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003736 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003738 // Apply "-" and "+" just before the number now, right to
3739 // left. Matters especially when "->" follows. Stops at
3740 // '!'.
3741 if (apply_leader(rettv, TRUE,
3742 start_leader, &end_leader) == FAIL)
3743 {
3744 clear_tv(rettv);
3745 return FAIL;
3746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 break;
3748
3749 /*
3750 * String constant: "string".
3751 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003752 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 return FAIL;
3754 break;
3755
3756 /*
3757 * Literal string constant: 'str''ing'.
3758 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003759 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761 break;
3762
3763 /*
3764 * Constant Vim variable.
3765 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 ret = NOTDONE;
3768 break;
3769
3770 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003771 * "true" constant
3772 */
3773 case 't': if (STRNCMP(*arg, "true", 4) == 0
3774 && !eval_isnamec((*arg)[4]))
3775 {
3776 *arg += 4;
3777 rettv->v_type = VAR_BOOL;
3778 rettv->vval.v_number = VVAL_TRUE;
3779 }
3780 else
3781 ret = NOTDONE;
3782 break;
3783
3784 /*
3785 * "false" constant
3786 */
3787 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3788 && !eval_isnamec((*arg)[5]))
3789 {
3790 *arg += 5;
3791 rettv->v_type = VAR_BOOL;
3792 rettv->vval.v_number = VVAL_FALSE;
3793 }
3794 else
3795 ret = NOTDONE;
3796 break;
3797
3798 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 * List: [expr, expr]
3800 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003801 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 break;
3803
3804 /*
3805 * Dictionary: #{key: val, key: val}
3806 */
3807 case '#': if ((*arg)[1] == '{')
3808 {
3809 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003810 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 }
3812 else
3813 ret = NOTDONE;
3814 break;
3815
3816 /*
3817 * Lambda: {arg, arg -> expr}
3818 * Dictionary: {'key': val, 'key': val}
3819 */
3820 case '{': {
3821 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003822 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823
3824 // Find out what comes after the arguments.
3825 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003826 &ga_arg, TRUE, NULL, NULL,
3827 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 if (ret != FAIL && *start == '>')
3829 ret = compile_lambda(arg, cctx);
3830 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003831 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 }
3833 break;
3834
3835 /*
3836 * Option value: &name
3837 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003838 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3839 return FAIL;
3840 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 break;
3842
3843 /*
3844 * Environment variable: $VAR.
3845 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003846 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3847 return FAIL;
3848 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 break;
3850
3851 /*
3852 * Register contents: @r.
3853 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003854 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3855 return FAIL;
3856 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 break;
3858 /*
3859 * nested expression: (expression).
3860 */
3861 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003862
3863 // recursive!
3864 if (ppconst->pp_used <= PPSIZE - 10)
3865 {
3866 ret = compile_expr1(arg, cctx, ppconst);
3867 }
3868 else
3869 {
3870 // Not enough space in ppconst, flush constants.
3871 if (generate_ppconst(cctx, ppconst) == FAIL)
3872 return FAIL;
3873 ret = compile_expr0(arg, cctx);
3874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 *arg = skipwhite(*arg);
3876 if (**arg == ')')
3877 ++*arg;
3878 else if (ret == OK)
3879 {
3880 emsg(_(e_missing_close));
3881 ret = FAIL;
3882 }
3883 break;
3884
3885 default: ret = NOTDONE;
3886 break;
3887 }
3888 if (ret == FAIL)
3889 return FAIL;
3890
Bram Moolenaar1c747212020-05-09 18:28:34 +02003891 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003893 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003894 clear_tv(rettv);
3895 else
3896 // A constant expression can possibly be handled compile time,
3897 // return the value instead of generating code.
3898 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 }
3900 else if (ret == NOTDONE)
3901 {
3902 char_u *p;
3903 int r;
3904
3905 if (!eval_isnamec1(**arg))
3906 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003907 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
3909 }
3910
3911 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003912 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914 {
3915 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918 {
3919 if (generate_ppconst(cctx, ppconst) == FAIL)
3920 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003921 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 if (r == FAIL)
3924 return FAIL;
3925 }
3926
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003927 // Handle following "[]", ".member", etc.
3928 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003929 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003930 ppconst) == FAIL)
3931 return FAIL;
3932 if (ppconst->pp_used > 0)
3933 {
3934 // apply the '!', '-' and '+' before the constant
3935 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003936 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 return FAIL;
3938 return OK;
3939 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003940 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003942 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943}
3944
3945/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003946 * Give the "white on both sides" error, taking the operator from "p[len]".
3947 */
3948 void
3949error_white_both(char_u *op, int len)
3950{
3951 char_u buf[10];
3952
3953 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003954 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003955}
3956
3957/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003958 * <type>expr7: runtime type check / conversion
3959 */
3960 static int
3961compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3962{
3963 type_T *want_type = NULL;
3964
3965 // Recognize <type>
3966 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3967 {
3968 int called_emsg_before = called_emsg;
3969
3970 ++*arg;
3971 want_type = parse_type(arg, cctx->ctx_type_list);
3972 if (called_emsg != called_emsg_before)
3973 return FAIL;
3974
3975 if (**arg != '>')
3976 {
3977 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003978 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003979 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003980 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003981 return FAIL;
3982 }
3983 ++*arg;
3984 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3985 return FAIL;
3986 }
3987
3988 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3989 return FAIL;
3990
3991 if (want_type != NULL)
3992 {
3993 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003994 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003995
Bram Moolenaard1103582020-08-14 22:44:25 +02003996 generate_ppconst(cctx, ppconst);
3997 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003998 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003999 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004000 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004001 return FAIL;
4002 }
4003 }
4004
4005 return OK;
4006}
4007
4008/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 * * number multiplication
4010 * / number division
4011 * % number modulo
4012 */
4013 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004014compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015{
4016 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004017 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004018 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004020 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004021 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 return FAIL;
4023
4024 /*
4025 * Repeat computing, until no "*", "/" or "%" is following.
4026 */
4027 for (;;)
4028 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004029 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 if (*op != '*' && *op != '/' && *op != '%')
4031 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004032 if (next != NULL)
4033 {
4034 *arg = next_line_from_context(cctx, TRUE);
4035 op = skipwhite(*arg);
4036 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004038 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004040 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004044 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004045 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004047 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004048 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004050
4051 if (ppconst->pp_used == ppconst_used + 2
4052 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4053 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004054 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004055 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4056 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004057 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004059 // both are numbers: compute the result
4060 switch (*op)
4061 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004062 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004063 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004064 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004065 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004066 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004067 break;
4068 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004069 tv1->vval.v_number = res;
4070 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004071 }
4072 else
4073 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004074 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004075 generate_two_op(cctx, op);
4076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 }
4078
4079 return OK;
4080}
4081
4082/*
4083 * + number addition
4084 * - number subtraction
4085 * .. string concatenation
4086 */
4087 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089{
4090 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004091 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094
4095 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098
4099 /*
4100 * Repeat computing, until no "+", "-" or ".." is following.
4101 */
4102 for (;;)
4103 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004104 op = may_peek_next_line(cctx, *arg, &next);
4105 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 break;
4107 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004108 if (next != NULL)
4109 {
4110 *arg = next_line_from_context(cctx, TRUE);
4111 op = skipwhite(*arg);
4112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004114 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004116 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004117 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119
4120 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004121 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004122 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004124 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
4127
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4131 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4132 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4133 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4136 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004137
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004138 // concat/subtract/add constant numbers
4139 if (*op == '+')
4140 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4141 else if (*op == '-')
4142 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4143 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004144 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004145 // concatenate constant strings
4146 char_u *s1 = tv1->vval.v_string;
4147 char_u *s2 = tv2->vval.v_string;
4148 size_t len1 = STRLEN(s1);
4149
4150 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4151 if (tv1->vval.v_string == NULL)
4152 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004154 return FAIL;
4155 }
4156 mch_memmove(tv1->vval.v_string, s1, len1);
4157 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004158 vim_free(s1);
4159 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004161 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004164 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004166 if (*op == '.')
4167 {
4168 if (may_generate_2STRING(-2, cctx) == FAIL
4169 || may_generate_2STRING(-1, cctx) == FAIL)
4170 return FAIL;
4171 generate_instr_drop(cctx, ISN_CONCAT, 1);
4172 }
4173 else
4174 generate_two_op(cctx, op);
4175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 }
4177
4178 return OK;
4179}
4180
4181/*
4182 * expr5a == expr5b
4183 * expr5a =~ expr5b
4184 * expr5a != expr5b
4185 * expr5a !~ expr5b
4186 * expr5a > expr5b
4187 * expr5a >= expr5b
4188 * expr5a < expr5b
4189 * expr5a <= expr5b
4190 * expr5a is expr5b
4191 * expr5a isnot expr5b
4192 *
4193 * Produces instructions:
4194 * EVAL expr5a Push result of "expr5a"
4195 * EVAL expr5b Push result of "expr5b"
4196 * COMPARE one of the compare instructions
4197 */
4198 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200{
4201 exptype_T type = EXPR_UNKNOWN;
4202 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004203 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207
4208 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 return FAIL;
4211
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004212 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004213 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
4215 /*
4216 * If there is a comparative operator, use it.
4217 */
4218 if (type != EXPR_UNKNOWN)
4219 {
4220 int ic = FALSE; // Default: do not ignore case
4221
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004222 if (next != NULL)
4223 {
4224 *arg = next_line_from_context(cctx, TRUE);
4225 p = skipwhite(*arg);
4226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 if (type_is && (p[len] == '?' || p[len] == '#'))
4228 {
4229 semsg(_(e_invexpr2), *arg);
4230 return FAIL;
4231 }
4232 // extra question mark appended: ignore case
4233 if (p[len] == '?')
4234 {
4235 ic = TRUE;
4236 ++len;
4237 }
4238 // extra '#' appended: match case (ignored)
4239 else if (p[len] == '#')
4240 ++len;
4241 // nothing appended: match case
4242
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004243 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004245 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004246 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248
4249 // get the second variable
4250 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004251 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 return FAIL;
4253
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 return FAIL;
4256
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257 if (ppconst->pp_used == ppconst_used + 2)
4258 {
4259 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4260 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4261 int ret;
4262
4263 // Both sides are a constant, compute the result now.
4264 // First check for a valid combination of types, this is more
4265 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004266 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 ret = FAIL;
4268 else
4269 {
4270 ret = typval_compare(tv1, tv2, type, ic);
4271 tv1->v_type = VAR_BOOL;
4272 tv1->vval.v_number = tv1->vval.v_number
4273 ? VVAL_TRUE : VVAL_FALSE;
4274 clear_tv(tv2);
4275 --ppconst->pp_used;
4276 }
4277 return ret;
4278 }
4279
4280 generate_ppconst(cctx, ppconst);
4281 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 }
4283
4284 return OK;
4285}
4286
Bram Moolenaar7f141552020-05-09 17:35:53 +02004287static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289/*
4290 * Compile || or &&.
4291 */
4292 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293compile_and_or(
4294 char_u **arg,
4295 cctx_T *cctx,
4296 char *op,
4297 ppconst_T *ppconst,
4298 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004300 char_u *next;
4301 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 int opchar = *op;
4303
4304 if (p[0] == opchar && p[1] == opchar)
4305 {
4306 garray_T *instr = &cctx->ctx_instr;
4307 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004308 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004309 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310
4311 /*
4312 * Repeat until there is no following "||" or "&&"
4313 */
4314 ga_init2(&end_ga, sizeof(int), 10);
4315 while (p[0] == opchar && p[1] == opchar)
4316 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317 if (next != NULL)
4318 {
4319 *arg = next_line_from_context(cctx, TRUE);
4320 p = skipwhite(*arg);
4321 }
4322
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4324 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004325 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004326 return FAIL;
4327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004329 // TODO: use ppconst if the value is a constant and check
4330 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 generate_ppconst(cctx, ppconst);
4332
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004333 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4334 all_bool_values = FALSE;
4335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 if (ga_grow(&end_ga, 1) == FAIL)
4337 {
4338 ga_clear(&end_ga);
4339 return FAIL;
4340 }
4341 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4342 ++end_ga.ga_len;
4343 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004344 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
4346 // eval the next expression
4347 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004348 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004349 return FAIL;
4350
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4352 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 {
4354 ga_clear(&end_ga);
4355 return FAIL;
4356 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004357
4358 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361
4362 // Fill in the end label in all jumps.
4363 while (end_ga.ga_len > 0)
4364 {
4365 isn_T *isn;
4366
4367 --end_ga.ga_len;
4368 isn = ((isn_T *)instr->ga_data)
4369 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4370 isn->isn_arg.jump.jump_where = instr->ga_len;
4371 }
4372 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004373
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004374 // The resulting type is converted to bool if needed.
4375 if (!all_bool_values)
4376 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378
4379 return OK;
4380}
4381
4382/*
4383 * expr4a && expr4a && expr4a logical AND
4384 *
4385 * Produces instructions:
4386 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004387 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004389 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004391 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 * end:
4393 */
4394 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 int ppconst_used = ppconst->pp_used;
4398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 return FAIL;
4402
4403 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405}
4406
4407/*
4408 * expr3a || expr3b || expr3c logical OR
4409 *
4410 * Produces instructions:
4411 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004412 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004414 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004416 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 * end:
4418 */
4419 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 int ppconst_used = ppconst->pp_used;
4423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 return FAIL;
4427
4428 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430}
4431
4432/*
4433 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004435 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 * JUMP_IF_FALSE alt jump if false
4437 * EVAL expr1a
4438 * JUMP_ALWAYS end
4439 * alt: EVAL expr1b
4440 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004441 *
4442 * Toplevel expression: expr2 ?? expr1
4443 * Produces instructions:
4444 * EVAL expr2 Push result of "expr2"
4445 * JUMP_AND_KEEP_IF_TRUE end jump if true
4446 * EVAL expr1
4447 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 */
4449 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451{
4452 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004454 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455
Bram Moolenaar3988f642020-08-27 22:43:03 +02004456 // Ignore all kinds of errors when not producing code.
4457 if (cctx->ctx_skip == SKIP_YES)
4458 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004459 evalarg_T evalarg;
4460
4461 CLEAR_FIELD(evalarg);
4462 evalarg.eval_cctx = cctx;
4463 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004464 return OK;
4465 }
4466
Bram Moolenaar61a89812020-05-07 16:58:17 +02004467 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 return FAIL;
4470
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004471 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 if (*p == '?')
4473 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004474 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 garray_T *instr = &cctx->ctx_instr;
4476 garray_T *stack = &cctx->ctx_type_stack;
4477 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004478 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004480 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481 int has_const_expr = FALSE;
4482 int const_value = FALSE;
4483 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004485 if (next != NULL)
4486 {
4487 *arg = next_line_from_context(cctx, TRUE);
4488 p = skipwhite(*arg);
4489 }
4490
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004491 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004492 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004493 semsg(_(e_white_space_required_before_and_after_str),
4494 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004495 return FAIL;
4496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 if (ppconst->pp_used == ppconst_used + 1)
4499 {
4500 // the condition is a constant, we know whether the ? or the :
4501 // expression is to be evaluated.
4502 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004503 if (op_falsy)
4504 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4505 else
4506 {
4507 int error = FALSE;
4508
4509 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4510 &error);
4511 if (error)
4512 return FAIL;
4513 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004514 cctx->ctx_skip = save_skip == SKIP_YES ||
4515 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4516
4517 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4518 // "left ?? right" and "left" is truthy: produce "left"
4519 generate_ppconst(cctx, ppconst);
4520 else
4521 {
4522 clear_tv(&ppconst->pp_tv[ppconst_used]);
4523 --ppconst->pp_used;
4524 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004525 }
4526 else
4527 {
4528 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004529 if (op_falsy)
4530 end_idx = instr->ga_len;
4531 generate_JUMP(cctx, op_falsy
4532 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4533 if (op_falsy)
4534 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
4537 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004538 *arg = skipwhite(p + 1 + op_falsy);
4539 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004540 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004542 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 if (!has_const_expr)
4545 {
4546 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004548 if (!op_falsy)
4549 {
4550 // remember the type and drop it
4551 --stack->ga_len;
4552 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004554 end_idx = instr->ga_len;
4555 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004556
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004557 // jump here from JUMP_IF_FALSE
4558 isn = ((isn_T *)instr->ga_data) + alt_idx;
4559 isn->isn_arg.jump.jump_where = instr->ga_len;
4560 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004563 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004565 // Check for the ":".
4566 p = may_peek_next_line(cctx, *arg, &next);
4567 if (*p != ':')
4568 {
4569 emsg(_(e_missing_colon));
4570 return FAIL;
4571 }
4572 if (next != NULL)
4573 {
4574 *arg = next_line_from_context(cctx, TRUE);
4575 p = skipwhite(*arg);
4576 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004577
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004578 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4579 {
4580 semsg(_(e_white_space_required_before_and_after_str), ":");
4581 return FAIL;
4582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004584 // evaluate the third expression
4585 if (has_const_expr)
4586 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004587 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004588 *arg = skipwhite(p + 1);
4589 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4590 return FAIL;
4591 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4592 return FAIL;
4593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 if (!has_const_expr)
4596 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004597 type_T **typep;
4598
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004602 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4603 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004605 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004606 isn = ((isn_T *)instr->ga_data) + end_idx;
4607 isn->isn_arg.jump.jump_where = instr->ga_len;
4608 }
4609
4610 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 }
4612 return OK;
4613}
4614
4615/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004616 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004617 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4618 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619 */
4620 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004621compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004622{
4623 ppconst_T ppconst;
4624
4625 CLEAR_FIELD(ppconst);
4626 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4627 {
4628 clear_ppconst(&ppconst);
4629 return FAIL;
4630 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004631 if (is_const != NULL)
4632 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 if (generate_ppconst(cctx, &ppconst) == FAIL)
4634 return FAIL;
4635 return OK;
4636}
4637
4638/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004639 * Toplevel expression.
4640 */
4641 static int
4642compile_expr0(char_u **arg, cctx_T *cctx)
4643{
4644 return compile_expr0_ext(arg, cctx, NULL);
4645}
4646
4647/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 * compile "return [expr]"
4649 */
4650 static char_u *
4651compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4652{
4653 char_u *p = arg;
4654 garray_T *stack = &cctx->ctx_type_stack;
4655 type_T *stack_type;
4656
4657 if (*p != NUL && *p != '|' && *p != '\n')
4658 {
4659 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 return NULL;
4662
4663 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4664 if (set_return_type)
4665 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004666 else
4667 {
4668 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4669 && stack_type->tt_type != VAR_VOID
4670 && stack_type->tt_type != VAR_UNKNOWN)
4671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004672 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004673 return NULL;
4674 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004675 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004676 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004677 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004678 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 }
4680 else
4681 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004682 // "set_return_type" cannot be TRUE, only used for a lambda which
4683 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004684 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4685 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004687 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 return NULL;
4689 }
4690
4691 // No argument, return zero.
4692 generate_PUSHNR(cctx, 0);
4693 }
4694
4695 if (generate_instr(cctx, ISN_RETURN) == NULL)
4696 return NULL;
4697
4698 // "return val | endif" is possible
4699 return skipwhite(p);
4700}
4701
4702/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004703 * Get a line from the compilation context, compatible with exarg_T getline().
4704 * Return a pointer to the line in allocated memory.
4705 * Return NULL for end-of-file or some error.
4706 */
4707 static char_u *
4708exarg_getline(
4709 int c UNUSED,
4710 void *cookie,
4711 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004712 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004713{
4714 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004715 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004716
Bram Moolenaar66250c92020-08-20 15:02:42 +02004717 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004718 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004719 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004720 return NULL;
4721 ++cctx->ctx_lnum;
4722 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4723 // Comment lines result in NULL pointers, skip them.
4724 if (p != NULL)
4725 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004726 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004727}
4728
4729/*
4730 * Compile a nested :def command.
4731 */
4732 static char_u *
4733compile_nested_function(exarg_T *eap, cctx_T *cctx)
4734{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004735 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004736 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004737 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004738 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004739 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004740 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004741
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004742 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004743 {
4744 emsg(_(e_cannot_use_bang_with_nested_def));
4745 return NULL;
4746 }
4747
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004748 // Only g:Func() can use a namespace.
4749 if (name_start[1] == ':' && !is_global)
4750 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004751 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004752 return NULL;
4753 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004754 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4755 return NULL;
4756
Bram Moolenaar04b12692020-05-04 23:24:44 +02004757 eap->arg = name_end;
4758 eap->getline = exarg_getline;
4759 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004760 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004761 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004762 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004763 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004764
Bram Moolenaar822ba242020-05-24 23:00:18 +02004765 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004766 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004767 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004768 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004769 {
4770 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004771 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004772 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004774 if (is_global)
4775 {
4776 char_u *func_name = vim_strnsave(name_start + 2,
4777 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004778
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004779 if (func_name == NULL)
4780 r = FAIL;
4781 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004782 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004783 }
4784 else
4785 {
4786 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004787 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004788 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004789 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004790
Bram Moolenaareef21022020-08-01 22:16:43 +02004791 if (lvar == NULL)
4792 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004793 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004794 return NULL;
4795 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004796
4797 // copy over the block scope IDs
4798 if (block_depth > 0)
4799 {
4800 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4801 if (ufunc->uf_block_ids != NULL)
4802 {
4803 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4804 sizeof(int) * block_depth);
4805 ufunc->uf_block_depth = block_depth;
4806 }
4807 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004808 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004809
Bram Moolenaar61a89812020-05-07 16:58:17 +02004810 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004811 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004812}
4813
4814/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 * Return the length of an assignment operator, or zero if there isn't one.
4816 */
4817 int
4818assignment_len(char_u *p, int *heredoc)
4819{
4820 if (*p == '=')
4821 {
4822 if (p[1] == '<' && p[2] == '<')
4823 {
4824 *heredoc = TRUE;
4825 return 3;
4826 }
4827 return 1;
4828 }
4829 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4830 return 2;
4831 if (STRNCMP(p, "..=", 3) == 0)
4832 return 3;
4833 return 0;
4834}
4835
4836// words that cannot be used as a variable
4837static char *reserved[] = {
4838 "true",
4839 "false",
4840 NULL
4841};
4842
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004843typedef enum {
4844 dest_local,
4845 dest_option,
4846 dest_env,
4847 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004848 dest_buffer,
4849 dest_window,
4850 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004851 dest_vimvar,
4852 dest_script,
4853 dest_reg,
4854} assign_dest_T;
4855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004857 * Generate the load instruction for "name".
4858 */
4859 static void
4860generate_loadvar(
4861 cctx_T *cctx,
4862 assign_dest_T dest,
4863 char_u *name,
4864 lvar_T *lvar,
4865 type_T *type)
4866{
4867 switch (dest)
4868 {
4869 case dest_option:
4870 // TODO: check the option exists
4871 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4872 break;
4873 case dest_global:
4874 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4875 break;
4876 case dest_buffer:
4877 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4878 break;
4879 case dest_window:
4880 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4881 break;
4882 case dest_tab:
4883 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4884 break;
4885 case dest_script:
4886 compile_load_scriptvar(cctx,
4887 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4888 break;
4889 case dest_env:
4890 // Include $ in the name here
4891 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4892 break;
4893 case dest_reg:
4894 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4895 break;
4896 case dest_vimvar:
4897 generate_LOADV(cctx, name + 2, TRUE);
4898 break;
4899 case dest_local:
4900 if (lvar->lv_from_outer)
4901 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4902 NULL, type);
4903 else
4904 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4905 break;
4906 }
4907}
4908
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004909 void
4910vim9_declare_error(char_u *name)
4911{
4912 char *scope = "";
4913
4914 switch (*name)
4915 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004916 case 'g': scope = _("global"); break;
4917 case 'b': scope = _("buffer"); break;
4918 case 'w': scope = _("window"); break;
4919 case 't': scope = _("tab"); break;
4920 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004921 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004922 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004923 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004924 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004925 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004926 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004927 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004928 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004929 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004930}
4931
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004932/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004933 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004934 * "let name"
4935 * "var name = expr"
4936 * "final name = expr"
4937 * "const name = expr"
4938 * "name = expr"
4939 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004940 * Return NULL for an error.
4941 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 */
4943 static char_u *
4944compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4945{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004948 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 char_u *ret = NULL;
4950 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004952 int scriptvar_sid = 0;
4953 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004956 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 int oplen = 0;
4959 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004960 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004961 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004964 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4965 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 // Skip over the "var" or "[var, var]" to get to any "=".
4968 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4969 if (p == NULL)
4970 return *arg == '[' ? arg : NULL;
4971
4972 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004974 // TODO: should we allow this, and figure out type inference from list
4975 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004976 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 return NULL;
4978 }
4979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 sp = p;
4981 p = skipwhite(p);
4982 op = p;
4983 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984
4985 if (var_count > 0 && oplen == 0)
4986 // can be something like "[1, 2]->func()"
4987 return arg;
4988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4990 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004991 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004993 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 if (heredoc)
4996 {
4997 list_T *l;
4998 listitem_T *li;
4999
5000 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005001 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005003 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005004 if (l == NULL)
5005 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
Bram Moolenaar078269b2020-09-21 20:35:55 +02005007 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005009 // Push each line and the create the list.
5010 FOR_ALL_LIST_ITEMS(l, li)
5011 {
5012 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5013 li->li_tv.vval.v_string = NULL;
5014 }
5015 generate_NEWLIST(cctx, l->lv_len);
5016 type = &t_list_string;
5017 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 list_free(l);
5020 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 // for "[var, var] = expr" evaluate the expression here, loop over the
5026 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005027
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 p = skipwhite(op + oplen);
5029 if (compile_expr0(&p, cctx) == FAIL)
5030 return NULL;
5031 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005033 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005035 type_T *stacktype;
5036
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005037 stacktype = stack->ga_len == 0 ? &t_void
5038 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005039 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005041 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 goto theend;
5043 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005044 if (need_type(stacktype, &t_list_any, -1, cctx,
5045 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005047 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005048 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5049 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 }
5051 }
5052
5053 /*
5054 * Loop over variables in "[var, var] = expr".
5055 * For "var = expr" and "let var: type" this is done only once.
5056 */
5057 if (var_count > 0)
5058 var_start = skipwhite(arg + 1); // skip over the "["
5059 else
5060 var_start = arg;
5061 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5062 {
5063 char_u *var_end = skip_var_one(var_start, FALSE);
5064 size_t varlen;
5065 int new_local = FALSE;
5066 int opt_type;
5067 int opt_flags = 0;
5068 assign_dest_T dest = dest_local;
5069 int vimvaridx = -1;
5070 lvar_T *lvar = NULL;
5071 lvar_T arg_lvar;
5072 int has_type = FALSE;
5073 int has_index = FALSE;
5074 int instr_count = -1;
5075
Bram Moolenaar65821722020-08-02 18:58:54 +02005076 if (*var_start == '@')
5077 p = var_start + 2;
5078 else
5079 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005080 // skip over the leading "&", "&l:", "&g:" and "$"
5081 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005082 p = to_name_end(p, TRUE);
5083 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084
5085 // "a: type" is declaring variable "a" with a type, not "a:".
5086 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5087 --var_end;
5088 if (is_decl && p == var_start + 2 && p[-1] == ':')
5089 --p;
5090
5091 varlen = p - var_start;
5092 vim_free(name);
5093 name = vim_strnsave(var_start, varlen);
5094 if (name == NULL)
5095 return NULL;
5096 if (!heredoc)
5097 type = &t_any;
5098
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005099 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005100 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005101 int declare_error = FALSE;
5102
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 if (*var_start == '&')
5104 {
5105 int cc;
5106 long numval;
5107
5108 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005109 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005111 emsg(_(e_const_option));
5112 goto theend;
5113 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005114 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005115 p = var_start;
5116 p = find_option_end(&p, &opt_flags);
5117 if (p == NULL)
5118 {
5119 // cannot happen?
5120 emsg(_(e_letunexp));
5121 goto theend;
5122 }
5123 cc = *p;
5124 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005125 opt_type = get_option_value(skip_option_env_lead(var_start),
5126 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 *p = cc;
5128 if (opt_type == -3)
5129 {
5130 semsg(_(e_unknown_option), var_start);
5131 goto theend;
5132 }
5133 if (opt_type == -2 || opt_type == 0)
5134 type = &t_string;
5135 else
5136 type = &t_number; // both number and boolean option
5137 }
5138 else if (*var_start == '$')
5139 {
5140 dest = dest_env;
5141 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005142 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 }
5144 else if (*var_start == '@')
5145 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005146 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005147 {
5148 emsg_invreg(var_start[1]);
5149 goto theend;
5150 }
5151 dest = dest_reg;
5152 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005153 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005155 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156 {
5157 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005158 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005160 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005161 {
5162 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005163 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005165 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005166 {
5167 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005168 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005170 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 {
5172 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005173 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005174 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005175 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005176 {
5177 typval_T *vtv;
5178 int di_flags;
5179
5180 vimvaridx = find_vim_var(name + 2, &di_flags);
5181 if (vimvaridx < 0)
5182 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005183 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 goto theend;
5185 }
5186 // We use the current value of "sandbox" here, is that OK?
5187 if (var_check_ro(di_flags, name, FALSE))
5188 goto theend;
5189 dest = dest_vimvar;
5190 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005191 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005192 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005193 }
5194 else
5195 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005196 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197
5198 for (idx = 0; reserved[idx] != NULL; ++idx)
5199 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005200 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005201 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005202 goto theend;
5203 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204
5205 lvar = lookup_local(var_start, varlen, cctx);
5206 if (lvar == NULL)
5207 {
5208 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005209 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5211 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005212 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005213 if (is_decl)
5214 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005215 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 goto theend;
5217 }
5218 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005219 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005220 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 if (lvar != NULL)
5222 {
5223 if (is_decl)
5224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005225 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226 goto theend;
5227 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005228 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005229 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005231 int script_namespace = varlen > 1
5232 && STRNCMP(var_start, "s:", 2) == 0;
5233 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005234 ? script_var_exists(var_start + 2, varlen - 2,
5235 FALSE, cctx)
5236 : script_var_exists(var_start, varlen,
5237 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005238 imported_T *import =
5239 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005240
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005241 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005243 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5244
5245 if (is_decl)
5246 {
5247 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005248 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005249 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005250 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005251 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005252 name);
5253 goto theend;
5254 }
5255 else if (cctx->ctx_ufunc->uf_script_ctx_version
5256 == SCRIPT_VERSION_VIM9
5257 && script_namespace
5258 && !script_var && import == NULL)
5259 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005260 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005261 goto theend;
5262 }
5263
5264 dest = dest_script;
5265
5266 // existing script-local variables should have a type
5267 scriptvar_sid = current_sctx.sc_sid;
5268 if (import != NULL)
5269 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005270 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005271 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005272 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005273 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005274 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005275 {
5276 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5277 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005278 ((svar_T *)si->sn_var_vals.ga_data)
5279 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005280 type = sv->sv_type;
5281 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005282 }
5283 }
5284 else if (name[1] == ':' && name[2] != NUL)
5285 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005286 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287 goto theend;
5288 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005289 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005290 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005291 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005292 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005293 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005294 else if (check_defined(var_start, varlen, cctx) == FAIL)
5295 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005298
5299 if (declare_error)
5300 {
5301 vim9_declare_error(name);
5302 goto theend;
5303 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005304 }
5305
5306 // handle "a:name" as a name, not index "name" on "a"
5307 if (varlen > 1 || var_start[varlen] != ':')
5308 p = var_end;
5309
5310 if (dest != dest_option)
5311 {
5312 if (is_decl && *p == ':')
5313 {
5314 // parse optional type: "let var: type = expr"
5315 if (!VIM_ISWHITE(p[1]))
5316 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005317 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 goto theend;
5319 }
5320 p = skipwhite(p + 1);
5321 type = parse_type(&p, cctx->ctx_type_list);
5322 has_type = TRUE;
5323 }
5324 else if (lvar != NULL)
5325 type = lvar->lv_type;
5326 }
5327
5328 if (oplen == 3 && !heredoc && dest != dest_global
5329 && type->tt_type != VAR_STRING
5330 && type->tt_type != VAR_ANY)
5331 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005332 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 goto theend;
5334 }
5335
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005336 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005337 {
5338 if (oplen > 1 && !heredoc)
5339 {
5340 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005341 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 goto theend;
5343 }
5344
5345 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005346 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5347 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 goto theend;
5349 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005350 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 if (lvar == NULL)
5352 goto theend;
5353 new_local = TRUE;
5354 }
5355
5356 member_type = type;
5357 if (var_end > var_start + varlen)
5358 {
5359 // Something follows after the variable: "var[idx]".
5360 if (is_decl)
5361 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005362 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005363 goto theend;
5364 }
5365
5366 if (var_start[varlen] == '[')
5367 {
5368 has_index = TRUE;
5369 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005370 member_type = &t_any;
5371 else
5372 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 }
5374 else
5375 {
5376 semsg("Not supported yet: %s", var_start);
5377 goto theend;
5378 }
5379 }
5380 else if (lvar == &arg_lvar)
5381 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005382 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005383 goto theend;
5384 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005385 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5386 {
5387 semsg(_(e_cannot_assign_to_constant), name);
5388 goto theend;
5389 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005390
5391 if (!heredoc)
5392 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005393 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005395 if (oplen > 0 && var_count == 0)
5396 {
5397 // skip over the "=" and the expression
5398 p = skipwhite(op + oplen);
5399 compile_expr0(&p, cctx);
5400 }
5401 }
5402 else if (oplen > 0)
5403 {
5404 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005405 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005406
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 // For "var = expr" evaluate the expression.
5408 if (var_count == 0)
5409 {
5410 int r;
5411
5412 // for "+=", "*=", "..=" etc. first load the current value
5413 if (*op != '=')
5414 {
5415 generate_loadvar(cctx, dest, name, lvar, type);
5416
5417 if (has_index)
5418 {
5419 // TODO: get member from list or dict
5420 emsg("Index with operation not supported yet");
5421 goto theend;
5422 }
5423 }
5424
5425 // Compile the expression. Temporarily hide the new local
5426 // variable here, it is not available to this expression.
5427 if (new_local)
5428 --cctx->ctx_locals.ga_len;
5429 instr_count = instr->ga_len;
5430 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005431 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 if (new_local)
5433 ++cctx->ctx_locals.ga_len;
5434 if (r == FAIL)
5435 goto theend;
5436 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005437 else if (semicolon && var_idx == var_count - 1)
5438 {
5439 // For "[var; var] = expr" get the rest of the list
5440 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5441 goto theend;
5442 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005443 else
5444 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005445 // For "[var, var] = expr" get the "var_idx" item from the
5446 // list.
5447 if (generate_GETITEM(cctx, var_idx) == FAIL)
5448 return FAIL;
5449 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005450
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005451 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005452 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005453 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005454 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005455 if ((stacktype->tt_type == VAR_FUNC
5456 || stacktype->tt_type == VAR_PARTIAL)
5457 && var_wrong_func_name(name, TRUE))
5458 goto theend;
5459
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005460 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005461 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005462 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005464 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005465 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 }
5467 else
5468 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005469 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005470 // for a variable that implies &t_any.
5471 if (stacktype == &t_list_empty)
5472 lvar->lv_type = &t_list_any;
5473 else if (stacktype == &t_dict_empty)
5474 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005475 else if (stacktype == &t_unknown)
5476 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005477 else
5478 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005479 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005480 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005481 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005482 {
5483 type_T *use_type = lvar->lv_type;
5484
Bram Moolenaar71abe482020-09-14 22:28:30 +02005485 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005486 if (has_index)
5487 {
5488 use_type = use_type->tt_member;
5489 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005490 // could be indexing "any"
5491 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005493 if (need_type(stacktype, use_type, -1, cctx,
5494 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005495 goto theend;
5496 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005497 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005499 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005500 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005502 else if (cmdidx == CMD_final)
5503 {
5504 emsg(_(e_final_requires_a_value));
5505 goto theend;
5506 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005507 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005509 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 goto theend;
5511 }
5512 else if (!has_type || dest == dest_option)
5513 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005514 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 goto theend;
5516 }
5517 else
5518 {
5519 // variables are always initialized
5520 if (ga_grow(instr, 1) == FAIL)
5521 goto theend;
5522 switch (member_type->tt_type)
5523 {
5524 case VAR_BOOL:
5525 generate_PUSHBOOL(cctx, VVAL_FALSE);
5526 break;
5527 case VAR_FLOAT:
5528#ifdef FEAT_FLOAT
5529 generate_PUSHF(cctx, 0.0);
5530#endif
5531 break;
5532 case VAR_STRING:
5533 generate_PUSHS(cctx, NULL);
5534 break;
5535 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005536 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 break;
5538 case VAR_FUNC:
5539 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5540 break;
5541 case VAR_LIST:
5542 generate_NEWLIST(cctx, 0);
5543 break;
5544 case VAR_DICT:
5545 generate_NEWDICT(cctx, 0);
5546 break;
5547 case VAR_JOB:
5548 generate_PUSHJOB(cctx, NULL);
5549 break;
5550 case VAR_CHANNEL:
5551 generate_PUSHCHANNEL(cctx, NULL);
5552 break;
5553 case VAR_NUMBER:
5554 case VAR_UNKNOWN:
5555 case VAR_ANY:
5556 case VAR_PARTIAL:
5557 case VAR_VOID:
5558 case VAR_SPECIAL: // cannot happen
5559 generate_PUSHNR(cctx, 0);
5560 break;
5561 }
5562 }
5563 if (var_count == 0)
5564 end = p;
5565 }
5566
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005567 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005568 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005569 break;
5570
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 if (oplen > 0 && *op != '=')
5572 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005573 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005574 type_T *stacktype;
5575
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005576 if (*op == '.')
5577 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005578 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005579 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005580 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005581 if (
5582#ifdef FEAT_FLOAT
5583 // If variable is float operation with number is OK.
5584 !(expected == &t_float && stacktype == &t_number) &&
5585#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005586 need_type(stacktype, expected, -1, cctx,
5587 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005588 goto theend;
5589
5590 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005591 {
5592 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5593 goto theend;
5594 }
5595 else if (*op == '+')
5596 {
5597 if (generate_add_instr(cctx,
5598 operator_type(member_type, stacktype),
5599 member_type, stacktype) == FAIL)
5600 goto theend;
5601 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005602 else if (generate_two_op(cctx, op) == FAIL)
5603 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005607 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 int r;
5609
5610 // Compile the "idx" in "var[idx]".
5611 if (new_local)
5612 --cctx->ctx_locals.ga_len;
5613 p = skipwhite(var_start + varlen + 1);
5614 r = compile_expr0(&p, cctx);
5615 if (new_local)
5616 ++cctx->ctx_locals.ga_len;
5617 if (r == FAIL)
5618 goto theend;
5619 if (*skipwhite(p) != ']')
5620 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005621 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 emsg(_(e_missbrac));
5623 goto theend;
5624 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005625 if (type == &t_any)
5626 {
5627 type_T *idx_type = ((type_T **)stack->ga_data)[
5628 stack->ga_len - 1];
5629 // Index on variable of unknown type: guess the type from the
5630 // index type: number is dict, otherwise dict.
5631 // TODO: should do the assignment at runtime
5632 if (idx_type->tt_type == VAR_NUMBER)
5633 type = &t_list_any;
5634 else
5635 type = &t_dict_any;
5636 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005637 if (type->tt_type == VAR_DICT
5638 && may_generate_2STRING(-1, cctx) == FAIL)
5639 goto theend;
5640 if (type->tt_type == VAR_LIST
5641 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005642 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005643 {
5644 emsg(_(e_number_exp));
5645 goto theend;
5646 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005647
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005648 // Load the dict or list. On the stack we then have:
5649 // - value
5650 // - index
5651 // - variable
5652 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005654 if (type->tt_type == VAR_LIST)
5655 {
5656 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5657 return FAIL;
5658 }
5659 else if (type->tt_type == VAR_DICT)
5660 {
5661 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5662 return FAIL;
5663 }
5664 else
5665 {
5666 emsg(_(e_listreq));
5667 goto theend;
5668 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005669 }
5670 else
5671 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005672 if (is_decl && cmdidx == CMD_const
5673 && (dest == dest_script || dest == dest_local))
5674 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005675 generate_LOCKCONST(cctx);
5676
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005677 switch (dest)
5678 {
5679 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005680 generate_STOREOPT(cctx, skip_option_env_lead(name),
5681 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005682 break;
5683 case dest_global:
5684 // include g: with the name, easier to execute that way
5685 generate_STORE(cctx, ISN_STOREG, 0, name);
5686 break;
5687 case dest_buffer:
5688 // include b: with the name, easier to execute that way
5689 generate_STORE(cctx, ISN_STOREB, 0, name);
5690 break;
5691 case dest_window:
5692 // include w: with the name, easier to execute that way
5693 generate_STORE(cctx, ISN_STOREW, 0, name);
5694 break;
5695 case dest_tab:
5696 // include t: with the name, easier to execute that way
5697 generate_STORE(cctx, ISN_STORET, 0, name);
5698 break;
5699 case dest_env:
5700 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5701 break;
5702 case dest_reg:
5703 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5704 break;
5705 case dest_vimvar:
5706 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5707 break;
5708 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005709 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005710 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005711 {
5712 char_u *name_s = name;
5713
5714 // Include s: in the name for store_var()
5715 if (name[1] != ':')
5716 {
5717 int len = (int)STRLEN(name) + 3;
5718
5719 name_s = alloc(len);
5720 if (name_s == NULL)
5721 name_s = name;
5722 else
5723 vim_snprintf((char *)name_s, len,
5724 "s:%s", name);
5725 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005726 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5727 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005728 if (name_s != name)
5729 vim_free(name_s);
5730 }
5731 else
5732 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005733 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005734 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005735 break;
5736 case dest_local:
5737 if (lvar != NULL)
5738 {
5739 isn_T *isn = ((isn_T *)instr->ga_data)
5740 + instr->ga_len - 1;
5741
5742 // optimization: turn "var = 123" from ISN_PUSHNR +
5743 // ISN_STORE into ISN_STORENR
5744 if (!lvar->lv_from_outer
5745 && instr->ga_len == instr_count + 1
5746 && isn->isn_type == ISN_PUSHNR)
5747 {
5748 varnumber_T val = isn->isn_arg.number;
5749
5750 isn->isn_type = ISN_STORENR;
5751 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5752 isn->isn_arg.storenr.stnr_val = val;
5753 if (stack->ga_len > 0)
5754 --stack->ga_len;
5755 }
5756 else if (lvar->lv_from_outer)
5757 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005758 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005759 else
5760 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5761 }
5762 break;
5763 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005764 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005765
5766 if (var_idx + 1 < var_count)
5767 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005769
5770 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005771 if (var_count > 0 && !semicolon)
5772 {
5773 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5774 goto theend;
5775 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005776
Bram Moolenaarb2097502020-07-19 17:17:02 +02005777 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778
5779theend:
5780 vim_free(name);
5781 return ret;
5782}
5783
5784/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005785 * Check if "name" can be "unlet".
5786 */
5787 int
5788check_vim9_unlet(char_u *name)
5789{
5790 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5791 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005792 // "unlet s:var" is allowed in legacy script.
5793 if (*name == 's' && !script_is_vim9())
5794 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005795 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005796 return FAIL;
5797 }
5798 return OK;
5799}
5800
5801/*
5802 * Callback passed to ex_unletlock().
5803 */
5804 static int
5805compile_unlet(
5806 lval_T *lvp,
5807 char_u *name_end,
5808 exarg_T *eap,
5809 int deep UNUSED,
5810 void *coookie)
5811{
5812 cctx_T *cctx = coookie;
5813
5814 if (lvp->ll_tv == NULL)
5815 {
5816 char_u *p = lvp->ll_name;
5817 int cc = *name_end;
5818 int ret = OK;
5819
5820 // Normal name. Only supports g:, w:, t: and b: namespaces.
5821 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005822 if (*p == '$')
5823 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5824 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005825 ret = FAIL;
5826 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005827 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005828
5829 *name_end = cc;
5830 return ret;
5831 }
5832
5833 // TODO: unlet {list}[idx]
5834 // TODO: unlet {dict}[key]
5835 emsg("Sorry, :unlet not fully implemented yet");
5836 return FAIL;
5837}
5838
5839/*
5840 * compile "unlet var", "lock var" and "unlock var"
5841 * "arg" points to "var".
5842 */
5843 static char_u *
5844compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5845{
5846 char_u *p = arg;
5847
5848 if (eap->cmdidx != CMD_unlet)
5849 {
5850 emsg("Sorry, :lock and unlock not implemented yet");
5851 return NULL;
5852 }
5853
5854 if (*p == '!')
5855 {
5856 p = skipwhite(p + 1);
5857 eap->forceit = TRUE;
5858 }
5859
5860 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5861 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5862}
5863
5864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865 * Compile an :import command.
5866 */
5867 static char_u *
5868compile_import(char_u *arg, cctx_T *cctx)
5869{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005870 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871}
5872
5873/*
5874 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5875 */
5876 static int
5877compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5878{
5879 garray_T *instr = &cctx->ctx_instr;
5880 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5881
5882 if (endlabel == NULL)
5883 return FAIL;
5884 endlabel->el_next = *el;
5885 *el = endlabel;
5886 endlabel->el_end_label = instr->ga_len;
5887
5888 generate_JUMP(cctx, when, 0);
5889 return OK;
5890}
5891
5892 static void
5893compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5894{
5895 garray_T *instr = &cctx->ctx_instr;
5896
5897 while (*el != NULL)
5898 {
5899 endlabel_T *cur = (*el);
5900 isn_T *isn;
5901
5902 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5903 isn->isn_arg.jump.jump_where = instr->ga_len;
5904 *el = cur->el_next;
5905 vim_free(cur);
5906 }
5907}
5908
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005909 static void
5910compile_free_jump_to_end(endlabel_T **el)
5911{
5912 while (*el != NULL)
5913 {
5914 endlabel_T *cur = (*el);
5915
5916 *el = cur->el_next;
5917 vim_free(cur);
5918 }
5919}
5920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921/*
5922 * Create a new scope and set up the generic items.
5923 */
5924 static scope_T *
5925new_scope(cctx_T *cctx, scopetype_T type)
5926{
5927 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5928
5929 if (scope == NULL)
5930 return NULL;
5931 scope->se_outer = cctx->ctx_scope;
5932 cctx->ctx_scope = scope;
5933 scope->se_type = type;
5934 scope->se_local_count = cctx->ctx_locals.ga_len;
5935 return scope;
5936}
5937
5938/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005939 * Free the current scope and go back to the outer scope.
5940 */
5941 static void
5942drop_scope(cctx_T *cctx)
5943{
5944 scope_T *scope = cctx->ctx_scope;
5945
5946 if (scope == NULL)
5947 {
5948 iemsg("calling drop_scope() without a scope");
5949 return;
5950 }
5951 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005952 switch (scope->se_type)
5953 {
5954 case IF_SCOPE:
5955 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5956 case FOR_SCOPE:
5957 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5958 case WHILE_SCOPE:
5959 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5960 case TRY_SCOPE:
5961 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5962 case NO_SCOPE:
5963 case BLOCK_SCOPE:
5964 break;
5965 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005966 vim_free(scope);
5967}
5968
5969/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005970 * Check that the top of the type stack has a type that can be used as a
5971 * condition. Give an error and return FAIL if not.
5972 */
5973 static int
5974bool_on_stack(cctx_T *cctx)
5975{
5976 garray_T *stack = &cctx->ctx_type_stack;
5977 type_T *type;
5978
5979 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5980 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005981 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005982 return FAIL;
5983 return OK;
5984}
5985
5986/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 * compile "if expr"
5988 *
5989 * "if expr" Produces instructions:
5990 * EVAL expr Push result of "expr"
5991 * JUMP_IF_FALSE end
5992 * ... body ...
5993 * end:
5994 *
5995 * "if expr | else" Produces instructions:
5996 * EVAL expr Push result of "expr"
5997 * JUMP_IF_FALSE else
5998 * ... body ...
5999 * JUMP_ALWAYS end
6000 * else:
6001 * ... body ...
6002 * end:
6003 *
6004 * "if expr1 | elseif expr2 | else" Produces instructions:
6005 * EVAL expr Push result of "expr"
6006 * JUMP_IF_FALSE elseif
6007 * ... body ...
6008 * JUMP_ALWAYS end
6009 * elseif:
6010 * EVAL expr Push result of "expr"
6011 * JUMP_IF_FALSE else
6012 * ... body ...
6013 * JUMP_ALWAYS end
6014 * else:
6015 * ... body ...
6016 * end:
6017 */
6018 static char_u *
6019compile_if(char_u *arg, cctx_T *cctx)
6020{
6021 char_u *p = arg;
6022 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006023 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006025 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006026 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027
Bram Moolenaara5565e42020-05-09 15:44:01 +02006028 CLEAR_FIELD(ppconst);
6029 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006031 clear_ppconst(&ppconst);
6032 return NULL;
6033 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006034 if (cctx->ctx_skip == SKIP_YES)
6035 clear_ppconst(&ppconst);
6036 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006037 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006038 int error = FALSE;
6039 int v;
6040
Bram Moolenaara5565e42020-05-09 15:44:01 +02006041 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006042 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006043 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006044 if (error)
6045 return NULL;
6046 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006047 }
6048 else
6049 {
6050 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006051 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006052 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006053 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006054 if (bool_on_stack(cctx) == FAIL)
6055 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057
6058 scope = new_scope(cctx, IF_SCOPE);
6059 if (scope == NULL)
6060 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006061 scope->se_skip_save = skip_save;
6062 // "is_had_return" will be reset if any block does not end in :return
6063 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006065 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006066 {
6067 // "where" is set when ":elseif", "else" or ":endif" is found
6068 scope->se_u.se_if.is_if_label = instr->ga_len;
6069 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6070 }
6071 else
6072 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073
6074 return p;
6075}
6076
6077 static char_u *
6078compile_elseif(char_u *arg, cctx_T *cctx)
6079{
6080 char_u *p = arg;
6081 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006082 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 isn_T *isn;
6084 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006085 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006086 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087
6088 if (scope == NULL || scope->se_type != IF_SCOPE)
6089 {
6090 emsg(_(e_elseif_without_if));
6091 return NULL;
6092 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006093 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006094 if (!cctx->ctx_had_return)
6095 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006097 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006098 {
6099 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006101 return NULL;
6102 // previous "if" or "elseif" jumps here
6103 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6104 isn->isn_arg.jump.jump_where = instr->ga_len;
6105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006108 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006109 if (cctx->ctx_skip == SKIP_YES)
6110 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006111 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006112 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006113 clear_ppconst(&ppconst);
6114 return NULL;
6115 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006116 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006117 if (scope->se_skip_save == SKIP_YES)
6118 clear_ppconst(&ppconst);
6119 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006120 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006121 int error = FALSE;
6122 int v;
6123
Bram Moolenaar7f141552020-05-09 17:35:53 +02006124 // The expression results in a constant.
6125 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006126 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6127 if (error)
6128 return NULL;
6129 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006130 clear_ppconst(&ppconst);
6131 scope->se_u.se_if.is_if_label = -1;
6132 }
6133 else
6134 {
6135 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006136 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006137 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006138 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006139 if (bool_on_stack(cctx) == FAIL)
6140 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006142 // "where" is set when ":elseif", "else" or ":endif" is found
6143 scope->se_u.se_if.is_if_label = instr->ga_len;
6144 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
6147 return p;
6148}
6149
6150 static char_u *
6151compile_else(char_u *arg, cctx_T *cctx)
6152{
6153 char_u *p = arg;
6154 garray_T *instr = &cctx->ctx_instr;
6155 isn_T *isn;
6156 scope_T *scope = cctx->ctx_scope;
6157
6158 if (scope == NULL || scope->se_type != IF_SCOPE)
6159 {
6160 emsg(_(e_else_without_if));
6161 return NULL;
6162 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006163 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006164 if (!cctx->ctx_had_return)
6165 scope->se_u.se_if.is_had_return = FALSE;
6166 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006167
Bram Moolenaarefd88552020-06-18 20:50:10 +02006168 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006169 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006170 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006171 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006172 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006173 if (!cctx->ctx_had_return
6174 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6175 JUMP_ALWAYS, cctx) == FAIL)
6176 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006177 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006178
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006179 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006180 {
6181 if (scope->se_u.se_if.is_if_label >= 0)
6182 {
6183 // previous "if" or "elseif" jumps here
6184 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6185 isn->isn_arg.jump.jump_where = instr->ga_len;
6186 scope->se_u.se_if.is_if_label = -1;
6187 }
6188 }
6189
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006190 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006191 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193
6194 return p;
6195}
6196
6197 static char_u *
6198compile_endif(char_u *arg, cctx_T *cctx)
6199{
6200 scope_T *scope = cctx->ctx_scope;
6201 ifscope_T *ifscope;
6202 garray_T *instr = &cctx->ctx_instr;
6203 isn_T *isn;
6204
6205 if (scope == NULL || scope->se_type != IF_SCOPE)
6206 {
6207 emsg(_(e_endif_without_if));
6208 return NULL;
6209 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006210 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006211 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006212 if (!cctx->ctx_had_return)
6213 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006215 if (scope->se_u.se_if.is_if_label >= 0)
6216 {
6217 // previous "if" or "elseif" jumps here
6218 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6219 isn->isn_arg.jump.jump_where = instr->ga_len;
6220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 // Fill in the "end" label in jumps at the end of the blocks.
6222 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006223 cctx->ctx_skip = scope->se_skip_save;
6224
6225 // If all the blocks end in :return and there is an :else then the
6226 // had_return flag is set.
6227 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006229 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 return arg;
6231}
6232
6233/*
6234 * compile "for var in expr"
6235 *
6236 * Produces instructions:
6237 * PUSHNR -1
6238 * STORE loop-idx Set index to -1
6239 * EVAL expr Push result of "expr"
6240 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6241 * - if beyond end, jump to "end"
6242 * - otherwise get item from list and push it
6243 * STORE var Store item in "var"
6244 * ... body ...
6245 * JUMP top Jump back to repeat
6246 * end: DROP Drop the result of "expr"
6247 *
6248 */
6249 static char_u *
6250compile_for(char_u *arg, cctx_T *cctx)
6251{
6252 char_u *p;
6253 size_t varlen;
6254 garray_T *instr = &cctx->ctx_instr;
6255 garray_T *stack = &cctx->ctx_type_stack;
6256 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006257 lvar_T *loop_lvar; // loop iteration variable
6258 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 type_T *vartype;
6260
6261 // TODO: list of variables: "for [key, value] in dict"
6262 // parse "var"
6263 for (p = arg; eval_isnamec1(*p); ++p)
6264 ;
6265 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006266 var_lvar = lookup_local(arg, varlen, cctx);
6267 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006269 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 return NULL;
6271 }
6272
6273 // consume "in"
6274 p = skipwhite(p);
6275 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6276 {
6277 emsg(_(e_missing_in));
6278 return NULL;
6279 }
6280 p = skipwhite(p + 2);
6281
6282
6283 scope = new_scope(cctx, FOR_SCOPE);
6284 if (scope == NULL)
6285 return NULL;
6286
6287 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006288 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6289 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006290 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006291 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006292 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295
6296 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006297 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6298 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006299 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006300 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006301 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006305 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
6307 // compile "expr", it remains on the stack until "endfor"
6308 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006309 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006310 {
6311 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006315 // Now that we know the type of "var", check that it is a list, now or at
6316 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006318 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006320 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 return NULL;
6322 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006323 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006324 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325
6326 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006327 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006329 generate_FOR(cctx, loop_lvar->lv_idx);
6330 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331
6332 return arg;
6333}
6334
6335/*
6336 * compile "endfor"
6337 */
6338 static char_u *
6339compile_endfor(char_u *arg, cctx_T *cctx)
6340{
6341 garray_T *instr = &cctx->ctx_instr;
6342 scope_T *scope = cctx->ctx_scope;
6343 forscope_T *forscope;
6344 isn_T *isn;
6345
6346 if (scope == NULL || scope->se_type != FOR_SCOPE)
6347 {
6348 emsg(_(e_for));
6349 return NULL;
6350 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006351 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006353 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 // At end of ":for" scope jump back to the FOR instruction.
6356 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6357
6358 // Fill in the "end" label in the FOR statement so it can jump here
6359 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6360 isn->isn_arg.forloop.for_end = instr->ga_len;
6361
6362 // Fill in the "end" label any BREAK statements
6363 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6364
6365 // Below the ":for" scope drop the "expr" list from the stack.
6366 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6367 return NULL;
6368
6369 vim_free(scope);
6370
6371 return arg;
6372}
6373
6374/*
6375 * compile "while expr"
6376 *
6377 * Produces instructions:
6378 * top: EVAL expr Push result of "expr"
6379 * JUMP_IF_FALSE end jump if false
6380 * ... body ...
6381 * JUMP top Jump back to repeat
6382 * end:
6383 *
6384 */
6385 static char_u *
6386compile_while(char_u *arg, cctx_T *cctx)
6387{
6388 char_u *p = arg;
6389 garray_T *instr = &cctx->ctx_instr;
6390 scope_T *scope;
6391
6392 scope = new_scope(cctx, WHILE_SCOPE);
6393 if (scope == NULL)
6394 return NULL;
6395
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006396 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397
6398 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006399 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 return NULL;
6401
Bram Moolenaar13106602020-10-04 16:06:05 +02006402 if (bool_on_stack(cctx) == FAIL)
6403 return FAIL;
6404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006406 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 JUMP_IF_FALSE, cctx) == FAIL)
6408 return FAIL;
6409
6410 return p;
6411}
6412
6413/*
6414 * compile "endwhile"
6415 */
6416 static char_u *
6417compile_endwhile(char_u *arg, cctx_T *cctx)
6418{
6419 scope_T *scope = cctx->ctx_scope;
6420
6421 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6422 {
6423 emsg(_(e_while));
6424 return NULL;
6425 }
6426 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006427 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428
6429 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006430 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431
6432 // Fill in the "end" label in the WHILE statement so it can jump here.
6433 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006434 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435
6436 vim_free(scope);
6437
6438 return arg;
6439}
6440
6441/*
6442 * compile "continue"
6443 */
6444 static char_u *
6445compile_continue(char_u *arg, cctx_T *cctx)
6446{
6447 scope_T *scope = cctx->ctx_scope;
6448
6449 for (;;)
6450 {
6451 if (scope == NULL)
6452 {
6453 emsg(_(e_continue));
6454 return NULL;
6455 }
6456 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6457 break;
6458 scope = scope->se_outer;
6459 }
6460
6461 // Jump back to the FOR or WHILE instruction.
6462 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006463 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6464 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 return arg;
6466}
6467
6468/*
6469 * compile "break"
6470 */
6471 static char_u *
6472compile_break(char_u *arg, cctx_T *cctx)
6473{
6474 scope_T *scope = cctx->ctx_scope;
6475 endlabel_T **el;
6476
6477 for (;;)
6478 {
6479 if (scope == NULL)
6480 {
6481 emsg(_(e_break));
6482 return NULL;
6483 }
6484 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6485 break;
6486 scope = scope->se_outer;
6487 }
6488
6489 // Jump to the end of the FOR or WHILE loop.
6490 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006491 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006493 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6495 return FAIL;
6496
6497 return arg;
6498}
6499
6500/*
6501 * compile "{" start of block
6502 */
6503 static char_u *
6504compile_block(char_u *arg, cctx_T *cctx)
6505{
6506 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6507 return NULL;
6508 return skipwhite(arg + 1);
6509}
6510
6511/*
6512 * compile end of block: drop one scope
6513 */
6514 static void
6515compile_endblock(cctx_T *cctx)
6516{
6517 scope_T *scope = cctx->ctx_scope;
6518
6519 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006520 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 vim_free(scope);
6522}
6523
6524/*
6525 * compile "try"
6526 * Creates a new scope for the try-endtry, pointing to the first catch and
6527 * finally.
6528 * Creates another scope for the "try" block itself.
6529 * TRY instruction sets up exception handling at runtime.
6530 *
6531 * "try"
6532 * TRY -> catch1, -> finally push trystack entry
6533 * ... try block
6534 * "throw {exception}"
6535 * EVAL {exception}
6536 * THROW create exception
6537 * ... try block
6538 * " catch {expr}"
6539 * JUMP -> finally
6540 * catch1: PUSH exeception
6541 * EVAL {expr}
6542 * MATCH
6543 * JUMP nomatch -> catch2
6544 * CATCH remove exception
6545 * ... catch block
6546 * " catch"
6547 * JUMP -> finally
6548 * catch2: CATCH remove exception
6549 * ... catch block
6550 * " finally"
6551 * finally:
6552 * ... finally block
6553 * " endtry"
6554 * ENDTRY pop trystack entry, may rethrow
6555 */
6556 static char_u *
6557compile_try(char_u *arg, cctx_T *cctx)
6558{
6559 garray_T *instr = &cctx->ctx_instr;
6560 scope_T *try_scope;
6561 scope_T *scope;
6562
6563 // scope that holds the jumps that go to catch/finally/endtry
6564 try_scope = new_scope(cctx, TRY_SCOPE);
6565 if (try_scope == NULL)
6566 return NULL;
6567
6568 // "catch" is set when the first ":catch" is found.
6569 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006570 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 if (generate_instr(cctx, ISN_TRY) == NULL)
6572 return NULL;
6573
6574 // scope for the try block itself
6575 scope = new_scope(cctx, BLOCK_SCOPE);
6576 if (scope == NULL)
6577 return NULL;
6578
6579 return arg;
6580}
6581
6582/*
6583 * compile "catch {expr}"
6584 */
6585 static char_u *
6586compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6587{
6588 scope_T *scope = cctx->ctx_scope;
6589 garray_T *instr = &cctx->ctx_instr;
6590 char_u *p;
6591 isn_T *isn;
6592
6593 // end block scope from :try or :catch
6594 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6595 compile_endblock(cctx);
6596 scope = cctx->ctx_scope;
6597
6598 // Error if not in a :try scope
6599 if (scope == NULL || scope->se_type != TRY_SCOPE)
6600 {
6601 emsg(_(e_catch));
6602 return NULL;
6603 }
6604
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006605 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006607 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 return NULL;
6609 }
6610
6611 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006612 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 JUMP_ALWAYS, cctx) == FAIL)
6614 return NULL;
6615
6616 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006617 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 if (isn->isn_arg.try.try_catch == 0)
6619 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006620 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 {
6622 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006623 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 isn->isn_arg.jump.jump_where = instr->ga_len;
6625 }
6626
6627 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006628 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006630 scope->se_u.se_try.ts_caught_all = TRUE;
6631 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 }
6633 else
6634 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006635 char_u *end;
6636 char_u *pat;
6637 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006638 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006639 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 // Push v:exception, push {expr} and MATCH
6642 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6643
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006644 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006645 if (*end != *p)
6646 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006647 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006648 vim_free(tofree);
6649 return FAIL;
6650 }
6651 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006652 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006653 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006654 len = (int)(end - tofree);
6655 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006656 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006657 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006658 if (pat == NULL)
6659 return FAIL;
6660 if (generate_PUSHS(cctx, pat) == FAIL)
6661 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6664 return NULL;
6665
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006666 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6668 return NULL;
6669 }
6670
6671 if (generate_instr(cctx, ISN_CATCH) == NULL)
6672 return NULL;
6673
6674 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6675 return NULL;
6676 return p;
6677}
6678
6679 static char_u *
6680compile_finally(char_u *arg, cctx_T *cctx)
6681{
6682 scope_T *scope = cctx->ctx_scope;
6683 garray_T *instr = &cctx->ctx_instr;
6684 isn_T *isn;
6685
6686 // end block scope from :try or :catch
6687 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6688 compile_endblock(cctx);
6689 scope = cctx->ctx_scope;
6690
6691 // Error if not in a :try scope
6692 if (scope == NULL || scope->se_type != TRY_SCOPE)
6693 {
6694 emsg(_(e_finally));
6695 return NULL;
6696 }
6697
6698 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006699 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 if (isn->isn_arg.try.try_finally != 0)
6701 {
6702 emsg(_(e_finally_dup));
6703 return NULL;
6704 }
6705
6706 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006707 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaar585fea72020-04-02 22:33:21 +02006709 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006710 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 {
6712 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006713 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006715 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 }
6717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 // TODO: set index in ts_finally_label jumps
6719
6720 return arg;
6721}
6722
6723 static char_u *
6724compile_endtry(char_u *arg, cctx_T *cctx)
6725{
6726 scope_T *scope = cctx->ctx_scope;
6727 garray_T *instr = &cctx->ctx_instr;
6728 isn_T *isn;
6729
6730 // end block scope from :catch or :finally
6731 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6732 compile_endblock(cctx);
6733 scope = cctx->ctx_scope;
6734
6735 // Error if not in a :try scope
6736 if (scope == NULL || scope->se_type != TRY_SCOPE)
6737 {
6738 if (scope == NULL)
6739 emsg(_(e_no_endtry));
6740 else if (scope->se_type == WHILE_SCOPE)
6741 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006742 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743 emsg(_(e_endfor));
6744 else
6745 emsg(_(e_endif));
6746 return NULL;
6747 }
6748
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006749 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006752 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 return NULL;
6754 }
6755
6756 // Fill in the "end" label in jumps at the end of the blocks, if not done
6757 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006758 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
6760 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006761 if (isn->isn_arg.try.try_catch == 0)
6762 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 if (isn->isn_arg.try.try_finally == 0)
6764 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006765
6766 if (scope->se_u.se_try.ts_catch_label != 0)
6767 {
6768 // Last catch without match jumps here
6769 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6770 isn->isn_arg.jump.jump_where = instr->ga_len;
6771 }
6772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 compile_endblock(cctx);
6774
6775 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6776 return NULL;
6777 return arg;
6778}
6779
6780/*
6781 * compile "throw {expr}"
6782 */
6783 static char_u *
6784compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6785{
6786 char_u *p = skipwhite(arg);
6787
Bram Moolenaara5565e42020-05-09 15:44:01 +02006788 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 return NULL;
6790 if (may_generate_2STRING(-1, cctx) == FAIL)
6791 return NULL;
6792 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6793 return NULL;
6794
6795 return p;
6796}
6797
6798/*
6799 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006800 * compile "echomsg expr"
6801 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006802 * compile "execute expr"
6803 */
6804 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006805compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006806{
6807 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006808 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006809 int count = 0;
6810
6811 for (;;)
6812 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006813 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006814 return NULL;
6815 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006816 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006817 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006818 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006819 break;
6820 }
6821
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006822 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6823 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6824 else if (cmdidx == CMD_execute)
6825 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6826 else if (cmdidx == CMD_echomsg)
6827 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6828 else
6829 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830 return p;
6831}
6832
6833/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006834 * :put r
6835 * :put ={expr}
6836 */
6837 static char_u *
6838compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6839{
6840 char_u *line = arg;
6841 linenr_T lnum;
6842 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006843 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006844
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006845 eap->regname = *line;
6846
6847 if (eap->regname == '=')
6848 {
6849 char_u *p = line + 1;
6850
6851 if (compile_expr0(&p, cctx) == FAIL)
6852 return NULL;
6853 line = p;
6854 }
6855 else if (eap->regname != NUL)
6856 ++line;
6857
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006858 // "errormsg" will not be set because the range is ADDR_LINES.
6859 // TODO: if the range contains something like "$" or "." need to evaluate
6860 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006861 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6862 return NULL;
6863 if (eap->addr_count == 0)
6864 lnum = -1;
6865 else
6866 lnum = eap->line2;
6867 if (above)
6868 --lnum;
6869
6870 generate_PUT(cctx, eap->regname, lnum);
6871 return line;
6872}
6873
6874/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006875 * A command that is not compiled, execute with legacy code.
6876 */
6877 static char_u *
6878compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6879{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006880 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006881 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006882 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006883
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006884 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006885 goto theend;
6886
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006887 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006888 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006889 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006890 int usefilter = FALSE;
6891
6892 has_expr = argt & (EX_XFILE | EX_EXPAND);
6893
6894 // If the command can be followed by a bar, find the bar and truncate
6895 // it, so that the following command can be compiled.
6896 // The '|' is overwritten with a NUL, it is put back below.
6897 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6898 && *eap->arg == '!')
6899 // :w !filter or :r !filter or :r! filter
6900 usefilter = TRUE;
6901 if ((argt & EX_TRLBAR) && !usefilter)
6902 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006903 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006904 separate_nextcmd(eap);
6905 if (eap->nextcmd != NULL)
6906 nextcmd = eap->nextcmd;
6907 }
6908 }
6909
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006910 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6911 {
6912 // expand filename in "syntax include [@group] filename"
6913 has_expr = TRUE;
6914 eap->arg = skipwhite(eap->arg + 7);
6915 if (*eap->arg == '@')
6916 eap->arg = skiptowhite(eap->arg);
6917 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006918
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006919 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006920 {
6921 int count = 0;
6922 char_u *start = skipwhite(line);
6923
6924 // :cmd xxx`=expr1`yyy`=expr2`zzz
6925 // PUSHS ":cmd xxx"
6926 // eval expr1
6927 // PUSHS "yyy"
6928 // eval expr2
6929 // PUSHS "zzz"
6930 // EXECCONCAT 5
6931 for (;;)
6932 {
6933 if (p > start)
6934 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006935 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006936 ++count;
6937 }
6938 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006939 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006940 return NULL;
6941 may_generate_2STRING(-1, cctx);
6942 ++count;
6943 p = skipwhite(p);
6944 if (*p != '`')
6945 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006946 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006947 return NULL;
6948 }
6949 start = p + 1;
6950
6951 p = (char_u *)strstr((char *)start, "`=");
6952 if (p == NULL)
6953 {
6954 if (*skipwhite(start) != NUL)
6955 {
6956 generate_PUSHS(cctx, vim_strsave(start));
6957 ++count;
6958 }
6959 break;
6960 }
6961 }
6962 generate_EXECCONCAT(cctx, count);
6963 }
6964 else
6965 generate_EXEC(cctx, line);
6966
6967theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006968 if (*nextcmd != NUL)
6969 {
6970 // the parser expects a pointer to the bar, put it back
6971 --nextcmd;
6972 *nextcmd = '|';
6973 }
6974
6975 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006976}
6977
6978/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006979 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006980 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006981 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006982 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006983add_def_function(ufunc_T *ufunc)
6984{
6985 dfunc_T *dfunc;
6986
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006987 if (def_functions.ga_len == 0)
6988 {
6989 // The first position is not used, so that a zero uf_dfunc_idx means it
6990 // wasn't set.
6991 if (ga_grow(&def_functions, 1) == FAIL)
6992 return FAIL;
6993 ++def_functions.ga_len;
6994 }
6995
Bram Moolenaar09689a02020-05-09 22:50:08 +02006996 // Add the function to "def_functions".
6997 if (ga_grow(&def_functions, 1) == FAIL)
6998 return FAIL;
6999 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7000 CLEAR_POINTER(dfunc);
7001 dfunc->df_idx = def_functions.ga_len;
7002 ufunc->uf_dfunc_idx = dfunc->df_idx;
7003 dfunc->df_ufunc = ufunc;
7004 ++def_functions.ga_len;
7005 return OK;
7006}
7007
7008/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 * After ex_function() has collected all the function lines: parse and compile
7010 * the lines into instructions.
7011 * Adds the function to "def_functions".
7012 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7013 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007014 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007015 * This can be used recursively through compile_lambda(), which may reallocate
7016 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007017 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007019 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007020compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 char_u *line = NULL;
7023 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 cctx_T cctx;
7026 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007027 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 int ret = FAIL;
7029 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007030 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007031 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007032 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007034 // When using a function that was compiled before: Free old instructions.
7035 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007036 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007038 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7039 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007040 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007042 else
7043 {
7044 if (add_def_function(ufunc) == FAIL)
7045 return FAIL;
7046 new_def_function = TRUE;
7047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048
Bram Moolenaar985116a2020-07-12 17:31:09 +02007049 ufunc->uf_def_status = UF_COMPILING;
7050
Bram Moolenaara80faa82020-04-12 19:37:17 +02007051 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 cctx.ctx_ufunc = ufunc;
7053 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007054 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7056 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7057 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7058 cctx.ctx_type_list = &ufunc->uf_type_list;
7059 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7060 instr = &cctx.ctx_instr;
7061
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007062 // Set the context to the function, it may be compiled when called from
7063 // another script. Set the script version to the most modern one.
7064 // The line number will be set in next_line_from_context().
7065 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7067
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007068 // Make sure error messages are OK.
7069 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7070 if (do_estack_push)
7071 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007072 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007073
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007074 if (ufunc->uf_def_args.ga_len > 0)
7075 {
7076 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007077 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007078 int i;
7079 char_u *arg;
7080 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007081 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007082
7083 // Produce instructions for the default values of optional arguments.
7084 // Store the instruction index in uf_def_arg_idx[] so that we know
7085 // where to start when the function is called, depending on the number
7086 // of arguments.
7087 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7088 if (ufunc->uf_def_arg_idx == NULL)
7089 goto erret;
7090 for (i = 0; i < count; ++i)
7091 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007092 garray_T *stack = &cctx.ctx_type_stack;
7093 type_T *val_type;
7094 int arg_idx = first_def_arg + i;
7095
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007096 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7097 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007098 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007099 goto erret;
7100
7101 // If no type specified use the type of the default value.
7102 // Otherwise check that the default value type matches the
7103 // specified type.
7104 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7105 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007106 {
7107 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007108 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007109 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007110 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7111 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007112 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007113
7114 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007115 goto erret;
7116 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007117 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007118
7119 if (did_set_arg_type)
7120 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007121 }
7122
7123 /*
7124 * Loop over all the lines of the function and generate instructions.
7125 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 for (;;)
7127 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007128 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007129 int starts_with_colon = FALSE;
7130 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007131 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007132
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007133 // Bail out on the first error to avoid a flood of errors and report
7134 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007135 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007136 goto erret;
7137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 if (line != NULL && *line == '|')
7139 // the line continues after a '|'
7140 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007141 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007142 && !(*line == '#' && (line == cctx.ctx_line_start
7143 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007145 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 goto erret;
7147 }
7148 else
7149 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007150 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007151 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007152 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 }
7155
Bram Moolenaara80faa82020-04-12 19:37:17 +02007156 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 ea.cmdlinep = &line;
7158 ea.cmd = skipwhite(line);
7159
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007160 // Some things can be recognized by the first character.
7161 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007163 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007164 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007165 if (ea.cmd[1] != '{')
7166 {
7167 line = (char_u *)"";
7168 continue;
7169 }
7170 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007172 case '}':
7173 {
7174 // "}" ends a block scope
7175 scopetype_T stype = cctx.ctx_scope == NULL
7176 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007178 if (stype == BLOCK_SCOPE)
7179 {
7180 compile_endblock(&cctx);
7181 line = ea.cmd;
7182 }
7183 else
7184 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007185 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007186 goto erret;
7187 }
7188 if (line != NULL)
7189 line = skipwhite(ea.cmd + 1);
7190 continue;
7191 }
7192
7193 case '{':
7194 // "{" starts a block scope
7195 // "{'a': 1}->func() is something else
7196 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7197 {
7198 line = compile_block(ea.cmd, &cctx);
7199 continue;
7200 }
7201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 }
7203
7204 /*
7205 * COMMAND MODIFIERS
7206 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007207 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007208 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7209 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 {
7211 if (errormsg != NULL)
7212 goto erret;
7213 // empty line or comment
7214 line = (char_u *)"";
7215 continue;
7216 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007217 generate_cmdmods(&cctx, &local_cmdmod);
7218 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007220 // Check if there was a colon after the last command modifier or before
7221 // the current position.
7222 for (p = ea.cmd; p >= line; --p)
7223 {
7224 if (*p == ':')
7225 starts_with_colon = TRUE;
7226 if (p < ea.cmd && !VIM_ISWHITE(*p))
7227 break;
7228 }
7229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007231 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007233 {
7234 if (*ea.cmd == '(')
7235 // not for "call()"
7236 ea.cmd = p;
7237 else
7238 ea.cmd = skipwhite(ea.cmd);
7239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007241 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007243 char_u *pskip;
7244
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007245 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007246 // find what follows.
7247 // Skip over "var.member", "var[idx]" and the like.
7248 // Also "&opt = val", "$ENV = val" and "@r = val".
7249 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007250 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007251 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007252 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007254 char_u *var_end;
7255 int oplen;
7256 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257
Bram Moolenaar65821722020-08-02 18:58:54 +02007258 if (ea.cmd[0] == '@')
7259 var_end = ea.cmd + 2;
7260 else
7261 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007262 FNE_CHECK_START | FNE_INCL_BR);
7263 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007264 if (oplen > 0)
7265 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007266 size_t len = p - ea.cmd;
7267
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007268 // Recognize an assignment if we recognize the variable
7269 // name:
7270 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007271 // "local = expr" where "local" is a local var.
7272 // "script = expr" where "script" is a script-local var.
7273 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007274 // "&opt = expr"
7275 // "$ENV = expr"
7276 // "@r = expr"
7277 if (*ea.cmd == '&'
7278 || *ea.cmd == '$'
7279 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007280 || ((len) > 2 && ea.cmd[1] == ':')
7281 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007282 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007283 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007284 || script_var_exists(ea.cmd, len,
7285 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007286 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007287 {
7288 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007289 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007290 goto erret;
7291 continue;
7292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 }
7294 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007295
7296 if (*ea.cmd == '[')
7297 {
7298 // [var, var] = expr
7299 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7300 if (line == NULL)
7301 goto erret;
7302 if (line != ea.cmd)
7303 continue;
7304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 }
7306
7307 /*
7308 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007309 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007311 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007312 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007313 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007314 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007315 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007316 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007317 if (!starts_with_colon)
7318 {
7319 emsg(_(e_colon_required_before_a_range));
7320 goto erret;
7321 }
7322 if (ends_excmd2(line, ea.cmd))
7323 {
7324 // A range without a command: jump to the line.
7325 // TODO: compile to a more efficient command, possibly
7326 // calling parse_cmd_address().
7327 ea.cmdidx = CMD_SIZE;
7328 line = compile_exec(line, &ea, &cctx);
7329 goto nextline;
7330 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007331 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007332 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007333 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007334 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007335 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336
7337 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7338 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007339 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007340 {
7341 line += STRLEN(line);
7342 continue;
7343 }
7344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007346 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007348 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007349 iemsg("Command from find_ex_command() not handled");
7350 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 }
7353
Bram Moolenaar3988f642020-08-27 22:43:03 +02007354 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007355 && ea.cmdidx != CMD_elseif
7356 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007357 && ea.cmdidx != CMD_endif
7358 && ea.cmdidx != CMD_endfor
7359 && ea.cmdidx != CMD_endwhile
7360 && ea.cmdidx != CMD_catch
7361 && ea.cmdidx != CMD_finally
7362 && ea.cmdidx != CMD_endtry)
7363 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007364 emsg(_(e_unreachable_code_after_return));
7365 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007366 }
7367
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007368 p = skipwhite(p);
7369 if (ea.cmdidx != CMD_SIZE
7370 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7371 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007372 if (ea.cmdidx >= 0)
7373 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007374 if ((ea.argt & EX_BANG) && *p == '!')
7375 {
7376 ea.forceit = TRUE;
7377 p = skipwhite(p + 1);
7378 }
7379 }
7380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 switch (ea.cmdidx)
7382 {
7383 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007384 ea.arg = p;
7385 line = compile_nested_function(&ea, &cctx);
7386 break;
7387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007389 // TODO: should we allow this, e.g. to declare a global
7390 // function?
7391 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 goto erret;
7393
7394 case CMD_return:
7395 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007396 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 break;
7398
7399 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007400 emsg(_(e_cannot_use_let_in_vim9_script));
7401 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007402 case CMD_var:
7403 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 case CMD_const:
7405 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007406 if (line == p)
7407 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 break;
7409
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007410 case CMD_unlet:
7411 case CMD_unlockvar:
7412 case CMD_lockvar:
7413 line = compile_unletlock(p, &ea, &cctx);
7414 break;
7415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 case CMD_import:
7417 line = compile_import(p, &cctx);
7418 break;
7419
7420 case CMD_if:
7421 line = compile_if(p, &cctx);
7422 break;
7423 case CMD_elseif:
7424 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 break;
7427 case CMD_else:
7428 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007429 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 break;
7431 case CMD_endif:
7432 line = compile_endif(p, &cctx);
7433 break;
7434
7435 case CMD_while:
7436 line = compile_while(p, &cctx);
7437 break;
7438 case CMD_endwhile:
7439 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007440 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 break;
7442
7443 case CMD_for:
7444 line = compile_for(p, &cctx);
7445 break;
7446 case CMD_endfor:
7447 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007448 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 break;
7450 case CMD_continue:
7451 line = compile_continue(p, &cctx);
7452 break;
7453 case CMD_break:
7454 line = compile_break(p, &cctx);
7455 break;
7456
7457 case CMD_try:
7458 line = compile_try(p, &cctx);
7459 break;
7460 case CMD_catch:
7461 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007462 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 break;
7464 case CMD_finally:
7465 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007466 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 break;
7468 case CMD_endtry:
7469 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007470 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 break;
7472 case CMD_throw:
7473 line = compile_throw(p, &cctx);
7474 break;
7475
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007476 case CMD_eval:
7477 if (compile_expr0(&p, &cctx) == FAIL)
7478 goto erret;
7479
Bram Moolenaar3988f642020-08-27 22:43:03 +02007480 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007481 generate_instr_drop(&cctx, ISN_DROP, 1);
7482
7483 line = skipwhite(p);
7484 break;
7485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007488 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007489 case CMD_echomsg:
7490 case CMD_echoerr:
7491 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007492 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007494 case CMD_put:
7495 ea.cmd = cmd;
7496 line = compile_put(p, &ea, &cctx);
7497 break;
7498
Bram Moolenaar3988f642020-08-27 22:43:03 +02007499 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007500
Bram Moolenaarae616492020-07-28 20:07:27 +02007501 case CMD_append:
7502 case CMD_change:
7503 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007504 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007505 case CMD_xit:
7506 not_in_vim9(&ea);
7507 goto erret;
7508
Bram Moolenaar002262f2020-07-08 17:47:57 +02007509 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007510 if (cctx.ctx_skip != SKIP_YES)
7511 {
7512 semsg(_(e_invalid_command_str), ea.cmd);
7513 goto erret;
7514 }
7515 // We don't check for a next command here.
7516 line = (char_u *)"";
7517 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007520 if (cctx.ctx_skip == SKIP_YES)
7521 {
7522 // We don't check for a next command here.
7523 line = (char_u *)"";
7524 }
7525 else
7526 {
7527 // Not recognized, execute with do_cmdline_cmd().
7528 ea.arg = p;
7529 line = compile_exec(line, &ea, &cctx);
7530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 break;
7532 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007533nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 if (line == NULL)
7535 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007536 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007538 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007539 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 if (cctx.ctx_type_stack.ga_len < 0)
7542 {
7543 iemsg("Type stack underflow");
7544 goto erret;
7545 }
7546 }
7547
7548 if (cctx.ctx_scope != NULL)
7549 {
7550 if (cctx.ctx_scope->se_type == IF_SCOPE)
7551 emsg(_(e_endif));
7552 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7553 emsg(_(e_endwhile));
7554 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7555 emsg(_(e_endfor));
7556 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007557 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 goto erret;
7559 }
7560
Bram Moolenaarefd88552020-06-18 20:50:10 +02007561 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 {
7563 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7564 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007565 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566 goto erret;
7567 }
7568
7569 // Return zero if there is no return at the end.
7570 generate_PUSHNR(&cctx, 0);
7571 generate_instr(&cctx, ISN_RETURN);
7572 }
7573
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007574 {
7575 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7576 + ufunc->uf_dfunc_idx;
7577 dfunc->df_deleted = FALSE;
7578 dfunc->df_instr = instr->ga_data;
7579 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007580 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007581 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007582 if (cctx.ctx_outer_used)
7583 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007584 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586
7587 ret = OK;
7588
7589erret:
7590 if (ret == FAIL)
7591 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007592 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007593 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7594 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007595
7596 for (idx = 0; idx < instr->ga_len; ++idx)
7597 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007599
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007600 // If using the last entry in the table and it was added above, we
7601 // might as well remove it.
7602 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007603 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007604 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007605 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007606 ufunc->uf_dfunc_idx = 0;
7607 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007608 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007609
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007610 while (cctx.ctx_scope != NULL)
7611 drop_scope(&cctx);
7612
Bram Moolenaar20431c92020-03-20 18:39:46 +01007613 // Don't execute this function body.
7614 ga_clear_strings(&ufunc->uf_lines);
7615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 if (errormsg != NULL)
7617 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007618 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007619 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 }
7621
7622 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007623 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007624 if (do_estack_push)
7625 estack_pop();
7626
Bram Moolenaar20431c92020-03-20 18:39:46 +01007627 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007628 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007630 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631}
7632
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007633 void
7634set_function_type(ufunc_T *ufunc)
7635{
7636 int varargs = ufunc->uf_va_name != NULL;
7637 int argcount = ufunc->uf_args.ga_len;
7638
7639 // Create a type for the function, with the return type and any
7640 // argument types.
7641 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7642 // The type is included in "tt_args".
7643 if (argcount > 0 || varargs)
7644 {
7645 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7646 argcount, &ufunc->uf_type_list);
7647 // Add argument types to the function type.
7648 if (func_type_add_arg_types(ufunc->uf_func_type,
7649 argcount + varargs,
7650 &ufunc->uf_type_list) == FAIL)
7651 return;
7652 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7653 ufunc->uf_func_type->tt_min_argcount =
7654 argcount - ufunc->uf_def_args.ga_len;
7655 if (ufunc->uf_arg_types == NULL)
7656 {
7657 int i;
7658
7659 // lambda does not have argument types.
7660 for (i = 0; i < argcount; ++i)
7661 ufunc->uf_func_type->tt_args[i] = &t_any;
7662 }
7663 else
7664 mch_memmove(ufunc->uf_func_type->tt_args,
7665 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7666 if (varargs)
7667 {
7668 ufunc->uf_func_type->tt_args[argcount] =
7669 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7670 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7671 }
7672 }
7673 else
7674 // No arguments, can use a predefined type.
7675 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7676 argcount, &ufunc->uf_type_list);
7677}
7678
7679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680/*
7681 * Delete an instruction, free what it contains.
7682 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007683 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684delete_instr(isn_T *isn)
7685{
7686 switch (isn->isn_type)
7687 {
7688 case ISN_EXEC:
7689 case ISN_LOADENV:
7690 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007691 case ISN_LOADB:
7692 case ISN_LOADW:
7693 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007695 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 case ISN_PUSHEXC:
7697 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007698 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007700 case ISN_STOREB:
7701 case ISN_STOREW:
7702 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007703 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 vim_free(isn->isn_arg.string);
7705 break;
7706
7707 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007708 case ISN_STORES:
7709 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 break;
7711
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007712 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007713 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007714 vim_free(isn->isn_arg.unlet.ul_name);
7715 break;
7716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 case ISN_STOREOPT:
7718 vim_free(isn->isn_arg.storeopt.so_name);
7719 break;
7720
7721 case ISN_PUSHBLOB: // push blob isn_arg.blob
7722 blob_unref(isn->isn_arg.blob);
7723 break;
7724
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007725 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007727 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007728#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007729 break;
7730
7731 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007732#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007733 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007734#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007735 break;
7736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 case ISN_UCALL:
7738 vim_free(isn->isn_arg.ufunc.cuf_name);
7739 break;
7740
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007741 case ISN_FUNCREF:
7742 {
7743 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7744 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007745
7746 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7747 func_ptr_unref(dfunc->df_ufunc);
7748 }
7749 break;
7750
7751 case ISN_DCALL:
7752 {
7753 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7754 + isn->isn_arg.dfunc.cdf_idx;
7755
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007756 if (dfunc->df_ufunc != NULL
7757 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007758 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007759 }
7760 break;
7761
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007762 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007763 {
7764 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7765 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7766
7767 if (ufunc != NULL)
7768 {
7769 // Clear uf_dfunc_idx so that the function is deleted.
7770 clear_def_function(ufunc);
7771 ufunc->uf_dfunc_idx = 0;
7772 func_ptr_unref(ufunc);
7773 }
7774
7775 vim_free(lambda);
7776 vim_free(isn->isn_arg.newfunc.nf_global);
7777 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007778 break;
7779
Bram Moolenaar5e654232020-09-16 15:22:00 +02007780 case ISN_CHECKTYPE:
7781 free_type(isn->isn_arg.type.ct_type);
7782 break;
7783
Bram Moolenaar02194d22020-10-24 23:08:38 +02007784 case ISN_CMDMOD:
7785 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7786 ->cmod_filter_regmatch.regprog);
7787 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7788 break;
7789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 case ISN_2BOOL:
7791 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007792 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 case ISN_ADDBLOB:
7794 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007795 case ISN_ANYINDEX:
7796 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007798 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007800 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007802 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 case ISN_COMPAREANY:
7804 case ISN_COMPAREBLOB:
7805 case ISN_COMPAREBOOL:
7806 case ISN_COMPAREDICT:
7807 case ISN_COMPAREFLOAT:
7808 case ISN_COMPAREFUNC:
7809 case ISN_COMPARELIST:
7810 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 case ISN_COMPARESPECIAL:
7812 case ISN_COMPARESTRING:
7813 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007814 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 case ISN_DROP:
7816 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007817 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007818 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007820 case ISN_EXECCONCAT:
7821 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007823 case ISN_GETITEM:
7824 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007825 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007826 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007827 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007829 case ISN_LOADBDICT:
7830 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007831 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007833 case ISN_LOADSCRIPT:
7834 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007836 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007837 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007838 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 case ISN_NEGATENR:
7840 case ISN_NEWDICT:
7841 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007843 case ISN_OPFLOAT:
7844 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007846 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007847 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 case ISN_PUSHF:
7849 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007851 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007853 case ISN_SHUFFLE:
7854 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007856 case ISN_STOREDICT:
7857 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007858 case ISN_STORENR:
7859 case ISN_STOREOUTER:
7860 case ISN_STOREREG:
7861 case ISN_STORESCRIPT:
7862 case ISN_STOREV:
7863 case ISN_STRINDEX:
7864 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 case ISN_THROW:
7866 case ISN_TRY:
7867 // nothing allocated
7868 break;
7869 }
7870}
7871
7872/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007873 * Free all instructions for "dfunc".
7874 */
7875 static void
7876delete_def_function_contents(dfunc_T *dfunc)
7877{
7878 int idx;
7879
7880 ga_clear(&dfunc->df_def_args_isn);
7881
7882 if (dfunc->df_instr != NULL)
7883 {
7884 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7885 delete_instr(dfunc->df_instr + idx);
7886 VIM_CLEAR(dfunc->df_instr);
7887 }
7888
7889 dfunc->df_deleted = TRUE;
7890}
7891
7892/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007893 * When a user function is deleted, clear the contents of any associated def
7894 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 */
7896 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007897clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007899 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 {
7901 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7902 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903
Bram Moolenaar20431c92020-03-20 18:39:46 +01007904 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007905 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 }
7907}
7908
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007909/*
7910 * Used when a user function is about to be deleted: remove the pointer to it.
7911 * The entry in def_functions is then unused.
7912 */
7913 void
7914unlink_def_function(ufunc_T *ufunc)
7915{
7916 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7917
7918 dfunc->df_ufunc = NULL;
7919}
7920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007922/*
7923 * Free all functions defined with ":def".
7924 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007925 void
7926free_def_functions(void)
7927{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007928 int idx;
7929
7930 for (idx = 0; idx < def_functions.ga_len; ++idx)
7931 {
7932 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7933
7934 delete_def_function_contents(dfunc);
7935 }
7936
7937 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938}
7939#endif
7940
7941
7942#endif // FEAT_EVAL