blob: 6b2b766bea33a6e978870a37bae5a800c6322d73 [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 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100823 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200824use_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 Moolenaarea2d4072020-11-12 12:08:51 +0100883 * Check that the top of the type stack has a type that can be used as a
884 * condition. Give an error and return FAIL if not.
885 */
886 static int
887bool_on_stack(cctx_T *cctx)
888{
889 garray_T *stack = &cctx->ctx_type_stack;
890 type_T *type;
891
892 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
893 if (type == &t_bool)
894 return OK;
895
896 if (type == &t_any || type == &t_number)
897 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
898 // This requires a runtime type check.
899 return generate_COND2BOOL(cctx);
900
901 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
902}
903
904/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 * Generate an ISN_PUSHNR instruction.
906 */
907 static int
908generate_PUSHNR(cctx_T *cctx, varnumber_T number)
909{
910 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200911 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200918 if (number == 0 || number == 1)
919 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200920 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200921
922 // A 0 or 1 number can also be used as a bool.
923 if (type != NULL)
924 {
925 type->tt_type = VAR_NUMBER;
926 type->tt_flags = TTFLAG_BOOL_OK;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
928 }
929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 return OK;
931}
932
933/*
934 * Generate an ISN_PUSHBOOL instruction.
935 */
936 static int
937generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
940
Bram Moolenaar080457c2020-03-03 21:53:32 +0100941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
943 return FAIL;
944 isn->isn_arg.number = number;
945
946 return OK;
947}
948
949/*
950 * Generate an ISN_PUSHSPEC instruction.
951 */
952 static int
953generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
954{
955 isn_T *isn;
956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
959 return FAIL;
960 isn->isn_arg.number = number;
961
962 return OK;
963}
964
965#ifdef FEAT_FLOAT
966/*
967 * Generate an ISN_PUSHF instruction.
968 */
969 static int
970generate_PUSHF(cctx_T *cctx, float_T fnumber)
971{
972 isn_T *isn;
973
Bram Moolenaar080457c2020-03-03 21:53:32 +0100974 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
976 return FAIL;
977 isn->isn_arg.fnumber = fnumber;
978
979 return OK;
980}
981#endif
982
983/*
984 * Generate an ISN_PUSHS instruction.
985 * Consumes "str".
986 */
987 static int
988generate_PUSHS(cctx_T *cctx, char_u *str)
989{
990 isn_T *isn;
991
Bram Moolenaar080457c2020-03-03 21:53:32 +0100992 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
994 return FAIL;
995 isn->isn_arg.string = str;
996
997 return OK;
998}
999
1000/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001001 * Generate an ISN_PUSHCHANNEL instruction.
1002 * Consumes "channel".
1003 */
1004 static int
1005generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1006{
1007 isn_T *isn;
1008
Bram Moolenaar080457c2020-03-03 21:53:32 +01001009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001010 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1011 return FAIL;
1012 isn->isn_arg.channel = channel;
1013
1014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHJOB instruction.
1019 * Consumes "job".
1020 */
1021 static int
1022generate_PUSHJOB(cctx_T *cctx, job_T *job)
1023{
1024 isn_T *isn;
1025
Bram Moolenaar080457c2020-03-03 21:53:32 +01001026 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001027 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 return FAIL;
1029 isn->isn_arg.job = job;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 * Generate an ISN_PUSHBLOB instruction.
1036 * Consumes "blob".
1037 */
1038 static int
1039generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.blob = blob;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001052 * Generate an ISN_PUSHFUNC instruction with name "name".
1053 * Consumes "name".
1054 */
1055 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001056generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001057{
1058 isn_T *isn;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001061 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001063 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001064
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 * Generate an ISN_GETITEM instruction with "index".
1070 */
1071 static int
1072generate_GETITEM(cctx_T *cctx, int index)
1073{
1074 isn_T *isn;
1075 garray_T *stack = &cctx->ctx_type_stack;
1076 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1077 type_T *item_type = &t_any;
1078
1079 RETURN_OK_IF_SKIP(cctx);
1080
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001081 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001083 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 emsg(_(e_listreq));
1085 return FAIL;
1086 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = index;
1091
1092 // add the item type to the type stack
1093 if (ga_grow(stack, 1) == FAIL)
1094 return FAIL;
1095 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1096 ++stack->ga_len;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001101 * Generate an ISN_SLICE instruction with "count".
1102 */
1103 static int
1104generate_SLICE(cctx_T *cctx, int count)
1105{
1106 isn_T *isn;
1107
1108 RETURN_OK_IF_SKIP(cctx);
1109 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_CHECKLEN instruction with "min_len".
1117 */
1118 static int
1119generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124
1125 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.checklen.cl_min_len = min_len;
1128 isn->isn_arg.checklen.cl_more_OK = more_OK;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 * Generate an ISN_STORE instruction.
1135 */
1136 static int
1137generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1138{
1139 isn_T *isn;
1140
Bram Moolenaar080457c2020-03-03 21:53:32 +01001141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1143 return FAIL;
1144 if (name != NULL)
1145 isn->isn_arg.string = vim_strsave(name);
1146 else
1147 isn->isn_arg.number = idx;
1148
1149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1154 */
1155 static int
1156generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1157{
1158 isn_T *isn;
1159
Bram Moolenaar080457c2020-03-03 21:53:32 +01001160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1162 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001163 isn->isn_arg.storenr.stnr_idx = idx;
1164 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_STOREOPT instruction
1171 */
1172 static int
1173generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001178 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 return FAIL;
1180 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1181 isn->isn_arg.storeopt.so_flags = opt_flags;
1182
1183 return OK;
1184}
1185
1186/*
1187 * Generate an ISN_LOAD or similar instruction.
1188 */
1189 static int
1190generate_LOAD(
1191 cctx_T *cctx,
1192 isntype_T isn_type,
1193 int idx,
1194 char_u *name,
1195 type_T *type)
1196{
1197 isn_T *isn;
1198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1201 return FAIL;
1202 if (name != NULL)
1203 isn->isn_arg.string = vim_strsave(name);
1204 else
1205 isn->isn_arg.number = idx;
1206
1207 return OK;
1208}
1209
1210/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001211 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001212 */
1213 static int
1214generate_LOADV(
1215 cctx_T *cctx,
1216 char_u *name,
1217 int error)
1218{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001219 int di_flags;
1220 int vidx = find_vim_var(name, &di_flags);
1221 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 if (vidx < 0)
1225 {
1226 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001227 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228 return FAIL;
1229 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001230 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001232 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001233}
1234
1235/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001236 * Generate an ISN_UNLET instruction.
1237 */
1238 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001239generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001240{
1241 isn_T *isn;
1242
1243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001244 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001245 return FAIL;
1246 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1247 isn->isn_arg.unlet.ul_forceit = forceit;
1248
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001253 * Generate an ISN_LOCKCONST instruction.
1254 */
1255 static int
1256generate_LOCKCONST(cctx_T *cctx)
1257{
1258 isn_T *isn;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1262 return FAIL;
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_LOADS instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 int sid,
1275 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 if (isn_type == ISN_LOADS)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1287 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1294 */
1295 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int sid,
1300 int idx,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (isn_type == ISN_LOADSCRIPT)
1307 isn = generate_instr_type(cctx, isn_type, type);
1308 else
1309 isn = generate_instr_drop(cctx, isn_type, 1);
1310 if (isn == NULL)
1311 return FAIL;
1312 isn->isn_arg.script.script_sid = sid;
1313 isn->isn_arg.script.script_idx = idx;
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWLIST instruction.
1319 */
1320 static int
1321generate_NEWLIST(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
Bram Moolenaar127542b2020-08-09 17:22:04 +02001333 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001334 if (count == 0)
1335 member = &t_void;
1336 else
1337 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001338 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1339 cctx->ctx_type_list);
1340 type = get_list_type(member, cctx->ctx_type_list);
1341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 // drop the value types
1343 stack->ga_len -= count;
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // add the list type to the type stack
1346 if (ga_grow(stack, 1) == FAIL)
1347 return FAIL;
1348 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_NEWDICT instruction.
1356 */
1357 static int
1358generate_NEWDICT(cctx_T *cctx, int count)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 type_T *type;
1363 type_T *member;
1364
Bram Moolenaar080457c2020-03-03 21:53:32 +01001365 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.number = count;
1369
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001370 if (count == 0)
1371 member = &t_void;
1372 else
1373 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001374 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1375 cctx->ctx_type_list);
1376 type = get_dict_type(member, cctx->ctx_type_list);
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 // drop the key and value types
1379 stack->ga_len -= 2 * count;
1380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 // add the dict type to the type stack
1382 if (ga_grow(stack, 1) == FAIL)
1383 return FAIL;
1384 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
1391 * Generate an ISN_FUNCREF instruction.
1392 */
1393 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001394generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395{
1396 isn_T *isn;
1397 garray_T *stack = &cctx->ctx_type_stack;
1398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1401 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001402 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001403 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 if (ga_grow(stack, 1) == FAIL)
1406 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001407 ((type_T **)stack->ga_data)[stack->ga_len] =
1408 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 ++stack->ga_len;
1410
1411 return OK;
1412}
1413
1414/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001415 * Generate an ISN_NEWFUNC instruction.
1416 */
1417 static int
1418generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1419{
1420 isn_T *isn;
1421 char_u *name;
1422
1423 RETURN_OK_IF_SKIP(cctx);
1424 name = vim_strsave(lambda_name);
1425 if (name == NULL)
1426 return FAIL;
1427 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1428 return FAIL;
1429 isn->isn_arg.newfunc.nf_lambda = name;
1430 isn->isn_arg.newfunc.nf_global = func_name;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 * Generate an ISN_JUMP instruction.
1437 */
1438 static int
1439generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1440{
1441 isn_T *isn;
1442 garray_T *stack = &cctx->ctx_type_stack;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1446 return FAIL;
1447 isn->isn_arg.jump.jump_when = when;
1448 isn->isn_arg.jump.jump_where = where;
1449
1450 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1451 --stack->ga_len;
1452
1453 return OK;
1454}
1455
1456 static int
1457generate_FOR(cctx_T *cctx, int loop_idx)
1458{
1459 isn_T *isn;
1460 garray_T *stack = &cctx->ctx_type_stack;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1464 return FAIL;
1465 isn->isn_arg.forloop.for_idx = loop_idx;
1466
1467 if (ga_grow(stack, 1) == FAIL)
1468 return FAIL;
1469 // type doesn't matter, will be stored next
1470 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1471 ++stack->ga_len;
1472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001478 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Return FAIL if the number of arguments is wrong.
1480 */
1481 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001482generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001486 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001487 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488
Bram Moolenaar080457c2020-03-03 21:53:32 +01001489 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001490 argoff = check_internal_func(func_idx, argcount);
1491 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 return FAIL;
1493
Bram Moolenaar389df252020-07-09 21:20:47 +02001494 if (method_call && argoff > 1)
1495 {
1496 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1497 return FAIL;
1498 isn->isn_arg.shuffle.shfl_item = argcount;
1499 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1500 }
1501
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001502 if (argcount > 0)
1503 {
1504 // Check the types of the arguments.
1505 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1506 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001507 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001508 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.bfunc.cbf_idx = func_idx;
1513 isn->isn_arg.bfunc.cbf_argcount = argcount;
1514
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 // Drop the argument types and push the return type.
1516 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 if (ga_grow(stack, 1) == FAIL)
1518 return FAIL;
1519 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001520 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001521 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 return OK;
1524}
1525
1526/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001527 * Generate an ISN_LISTAPPEND instruction. Works like add().
1528 * Argument count is already checked.
1529 */
1530 static int
1531generate_LISTAPPEND(cctx_T *cctx)
1532{
1533 garray_T *stack = &cctx->ctx_type_stack;
1534 type_T *list_type;
1535 type_T *item_type;
1536 type_T *expected;
1537
1538 // Caller already checked that list_type is a list.
1539 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1540 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1541 expected = list_type->tt_member;
1542 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001553 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1554 * Argument count is already checked.
1555 */
1556 static int
1557generate_BLOBAPPEND(cctx_T *cctx)
1558{
1559 garray_T *stack = &cctx->ctx_type_stack;
1560 type_T *item_type;
1561
1562 // Caller already checked that blob_type is a blob.
1563 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1564 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1565 return FAIL;
1566
1567 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1568 return FAIL;
1569
1570 --stack->ga_len; // drop the argument
1571 return OK;
1572}
1573
1574/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 * Generate an ISN_DCALL or ISN_UCALL instruction.
1576 * Return FAIL if the number of arguments is wrong.
1577 */
1578 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001579generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580{
1581 isn_T *isn;
1582 garray_T *stack = &cctx->ctx_type_stack;
1583 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001584 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585
Bram Moolenaar080457c2020-03-03 21:53:32 +01001586 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (argcount > regular_args && !has_varargs(ufunc))
1588 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001589 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return FAIL;
1591 }
1592 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1593 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001594 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 return FAIL;
1596 }
1597
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001598 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 {
1604 type_T *expected;
1605 type_T *actual;
1606
1607 if (i < regular_args)
1608 {
1609 if (ufunc->uf_arg_types == NULL)
1610 continue;
1611 expected = ufunc->uf_arg_types[i];
1612 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001613 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1614 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001615 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001616 else
1617 expected = ufunc->uf_va_type->tt_member;
1618 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001619 if (need_type(actual, expected, -argcount + i, cctx,
1620 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001621 {
1622 arg_type_mismatch(expected, actual, i + 1);
1623 return FAIL;
1624 }
1625 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001626 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001627 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1628 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001629 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001630 }
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001634 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001636 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 {
1638 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1639 isn->isn_arg.dfunc.cdf_argcount = argcount;
1640 }
1641 else
1642 {
1643 // A user function may be deleted and redefined later, can't use the
1644 // ufunc pointer, need to look it up again at runtime.
1645 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1646 isn->isn_arg.ufunc.cuf_argcount = argcount;
1647 }
1648
1649 stack->ga_len -= argcount; // drop the arguments
1650 if (ga_grow(stack, 1) == FAIL)
1651 return FAIL;
1652 // add return value
1653 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1654 ++stack->ga_len;
1655
1656 return OK;
1657}
1658
1659/*
1660 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1661 */
1662 static int
1663generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1664{
1665 isn_T *isn;
1666 garray_T *stack = &cctx->ctx_type_stack;
1667
Bram Moolenaar080457c2020-03-03 21:53:32 +01001668 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1672 isn->isn_arg.ufunc.cuf_argcount = argcount;
1673
1674 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001675 if (ga_grow(stack, 1) == FAIL)
1676 return FAIL;
1677 // add return value
1678 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1679 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
1681 return OK;
1682}
1683
1684/*
1685 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001686 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 */
1688 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001689generate_PCALL(
1690 cctx_T *cctx,
1691 int argcount,
1692 char_u *name,
1693 type_T *type,
1694 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695{
1696 isn_T *isn;
1697 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001698 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
Bram Moolenaar080457c2020-03-03 21:53:32 +01001700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001701
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001702 if (type->tt_type == VAR_ANY)
1703 ret_type = &t_any;
1704 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001705 {
1706 if (type->tt_argcount != -1)
1707 {
1708 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1709
1710 if (argcount < type->tt_min_argcount - varargs)
1711 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001712 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001713 return FAIL;
1714 }
1715 if (!varargs && argcount > type->tt_argcount)
1716 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001717 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001718 return FAIL;
1719 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001720 if (type->tt_args != NULL)
1721 {
1722 int i;
1723
1724 for (i = 0; i < argcount; ++i)
1725 {
1726 int offset = -argcount + i - 1;
1727 type_T *actual = ((type_T **)stack->ga_data)[
1728 stack->ga_len + offset];
1729 type_T *expected;
1730
1731 if (varargs && i >= type->tt_min_argcount - 1)
1732 expected = type->tt_args[
1733 type->tt_min_argcount - 1]->tt_member;
1734 else
1735 expected = type->tt_args[i];
1736 if (need_type(actual, expected, offset,
1737 cctx, TRUE, FALSE) == FAIL)
1738 {
1739 arg_type_mismatch(expected, actual, i + 1);
1740 return FAIL;
1741 }
1742 }
1743 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001744 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001745 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001746 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001747 else
1748 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001749 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001750 return FAIL;
1751 }
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.pfunc.cpf_top = at_top;
1756 isn->isn_arg.pfunc.cpf_argcount = argcount;
1757
1758 stack->ga_len -= argcount; // drop the arguments
1759
1760 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001761 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001763 // If partial is above the arguments it must be cleared and replaced with
1764 // the return value.
1765 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1766 return FAIL;
1767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 return OK;
1769}
1770
1771/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001772 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 */
1774 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001775generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776{
1777 isn_T *isn;
1778 garray_T *stack = &cctx->ctx_type_stack;
1779 type_T *type;
1780
Bram Moolenaar080457c2020-03-03 21:53:32 +01001781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001784 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001786 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001788 if (type->tt_type != VAR_DICT && type != &t_any)
1789 {
1790 emsg(_(e_dictreq));
1791 return FAIL;
1792 }
1793 // change dict type to dict member type
1794 if (type->tt_type == VAR_DICT)
1795 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796
1797 return OK;
1798}
1799
1800/*
1801 * Generate an ISN_ECHO instruction.
1802 */
1803 static int
1804generate_ECHO(cctx_T *cctx, int with_white, int count)
1805{
1806 isn_T *isn;
1807
Bram Moolenaar080457c2020-03-03 21:53:32 +01001808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.echo.echo_with_white = with_white;
1812 isn->isn_arg.echo.echo_count = count;
1813
1814 return OK;
1815}
1816
Bram Moolenaarad39c092020-02-26 18:23:43 +01001817/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001818 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001819 */
1820 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001821generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001822{
1823 isn_T *isn;
1824
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001825 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001826 return FAIL;
1827 isn->isn_arg.number = count;
1828
1829 return OK;
1830}
1831
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001832/*
1833 * Generate an ISN_PUT instruction.
1834 */
1835 static int
1836generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1837{
1838 isn_T *isn;
1839
1840 RETURN_OK_IF_SKIP(cctx);
1841 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1842 return FAIL;
1843 isn->isn_arg.put.put_regname = regname;
1844 isn->isn_arg.put.put_lnum = lnum;
1845 return OK;
1846}
1847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 static int
1849generate_EXEC(cctx_T *cctx, char_u *line)
1850{
1851 isn_T *isn;
1852
Bram Moolenaar080457c2020-03-03 21:53:32 +01001853 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1855 return FAIL;
1856 isn->isn_arg.string = vim_strsave(line);
1857 return OK;
1858}
1859
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001860 static int
1861generate_EXECCONCAT(cctx_T *cctx, int count)
1862{
1863 isn_T *isn;
1864
1865 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1866 return FAIL;
1867 isn->isn_arg.number = count;
1868 return OK;
1869}
1870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001872 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001873 */
1874 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001875generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001876{
1877 isn_T *isn;
1878
Bram Moolenaar02194d22020-10-24 23:08:38 +02001879 if (cmod->cmod_flags != 0
1880 || cmod->cmod_split != 0
1881 || cmod->cmod_verbose != 0
1882 || cmod->cmod_tab != 0
1883 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001884 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001885 cctx->ctx_has_cmdmod = TRUE;
1886
1887 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001888 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001889 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1890 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1891 return FAIL;
1892 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1893 // filter progam now belongs to the instruction
1894 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001895 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001896
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001897 return OK;
1898}
1899
1900 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001901generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001902{
1903 isn_T *isn;
1904
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001906 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001907 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001908 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001909 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001910
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001911 return OK;
1912}
1913
1914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001916 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001918 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001919reserve_local(
1920 cctx_T *cctx,
1921 char_u *name,
1922 size_t len,
1923 int isConst,
1924 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 lvar_T *lvar;
1927
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001928 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001930 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933
1934 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001935 return NULL;
1936 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001937 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001939 // Every local variable uses the next entry on the stack. We could re-use
1940 // the last ones when leaving a scope, but then variables used in a closure
1941 // might get overwritten. To keep things simple do not re-use stack
1942 // entries. This is less efficient, but memory is cheap these days.
1943 lvar->lv_idx = cctx->ctx_locals_count++;
1944
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001945 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 lvar->lv_const = isConst;
1947 lvar->lv_type = type;
1948
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001949 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950}
1951
1952/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001953 * Remove local variables above "new_top".
1954 */
1955 static void
1956unwind_locals(cctx_T *cctx, int new_top)
1957{
1958 if (cctx->ctx_locals.ga_len > new_top)
1959 {
1960 int idx;
1961 lvar_T *lvar;
1962
1963 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1964 {
1965 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1966 vim_free(lvar->lv_name);
1967 }
1968 }
1969 cctx->ctx_locals.ga_len = new_top;
1970}
1971
1972/*
1973 * Free all local variables.
1974 */
1975 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001976free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001977{
1978 unwind_locals(cctx, 0);
1979 ga_clear(&cctx->ctx_locals);
1980}
1981
1982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 * Find "name" in script-local items of script "sid".
1984 * Returns the index in "sn_var_vals" if found.
1985 * If found but not in "sn_var_vals" returns -1.
1986 * If not found returns -2.
1987 */
1988 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001989get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
1991 hashtab_T *ht;
1992 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001993 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001994 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 int idx;
1996
Bram Moolenaare3d46852020-08-29 13:39:17 +02001997 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001999 if (sid == current_sctx.sc_sid)
2000 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002001 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002002
2003 if (sav == NULL)
2004 return -2;
2005 idx = sav->sav_var_vals_idx;
2006 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2007 if (check_writable && sv->sv_const)
2008 semsg(_(e_readonlyvar), name);
2009 return idx;
2010 }
2011
2012 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 ht = &SCRIPT_VARS(sid);
2014 di = find_var_in_ht(ht, 0, name, TRUE);
2015 if (di == NULL)
2016 return -2;
2017
2018 // Now find the svar_T index in sn_var_vals.
2019 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2020 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002021 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 if (sv->sv_tv == &di->di_tv)
2023 {
2024 if (check_writable && sv->sv_const)
2025 semsg(_(e_readonlyvar), name);
2026 return idx;
2027 }
2028 }
2029 return -1;
2030}
2031
2032/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002033 * Find "name" in imported items of the current script or in "cctx" if not
2034 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 */
2036 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002037find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 int idx;
2040
Bram Moolenaare3d46852020-08-29 13:39:17 +02002041 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002042 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 if (cctx != NULL)
2044 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2045 {
2046 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2047 + 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
Bram Moolenaarefa94442020-08-08 22:16:00 +02002055 return find_imported_in_script(name, len, current_sctx.sc_sid);
2056}
2057
2058 imported_T *
2059find_imported_in_script(char_u *name, size_t len, int sid)
2060{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002061 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002062 int idx;
2063
Bram Moolenaare3d46852020-08-29 13:39:17 +02002064 if (!SCRIPT_ID_VALID(sid))
2065 return NULL;
2066 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2068 {
2069 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2070
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002071 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2072 : STRLEN(import->imp_name) == len
2073 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 return import;
2075 }
2076 return NULL;
2077}
2078
2079/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002080 * Free all imported variables.
2081 */
2082 static void
2083free_imported(cctx_T *cctx)
2084{
2085 int idx;
2086
2087 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2088 {
2089 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2090
2091 vim_free(import->imp_name);
2092 }
2093 ga_clear(&cctx->ctx_imports);
2094}
2095
2096/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002097 * Return TRUE if "p" points at a "#" but not at "#{".
2098 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002099 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002100vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002101{
2102 return p[0] == '#' && p[1] != '{';
2103}
2104
2105/*
2106 * Return a pointer to the next line that isn't empty or only contains a
2107 * comment. Skips over white space.
2108 * Returns NULL if there is none.
2109 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002110 char_u *
2111peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002112{
2113 int lnum = cctx->ctx_lnum;
2114
2115 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2116 {
2117 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002118 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002119
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002120 // ignore NULLs inserted for continuation lines
2121 if (line != NULL)
2122 {
2123 p = skipwhite(line);
2124 if (*p != NUL && !vim9_comment_start(p))
2125 return p;
2126 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002127 }
2128 return NULL;
2129}
2130
2131/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002132 * Called when checking for a following operator at "arg". When the rest of
2133 * the line is empty or only a comment, peek the next line. If there is a next
2134 * line return a pointer to it and set "nextp".
2135 * Otherwise skip over white space.
2136 */
2137 static char_u *
2138may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2139{
2140 char_u *p = skipwhite(arg);
2141
2142 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002143 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002144 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002145 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002146 if (*nextp != NULL)
2147 return *nextp;
2148 }
2149 return p;
2150}
2151
2152/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002153 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002155 * Returns NULL when at the end.
2156 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002157 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002158next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002159{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002160 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002161
2162 do
2163 {
2164 ++cctx->ctx_lnum;
2165 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002166 {
2167 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002168 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002169 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002170 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002171 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002172 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002173 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002174 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002175 return line;
2176}
2177
2178/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002179 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002180 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002181 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2182 */
2183 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002184may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002185{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002186 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002187 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002188 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002189
2190 if (next == NULL)
2191 return FAIL;
2192 *arg = skipwhite(next);
2193 }
2194 return OK;
2195}
2196
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002197/*
2198 * Idem, and give an error when failed.
2199 */
2200 static int
2201may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2202{
2203 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2204 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002205 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002206 return FAIL;
2207 }
2208 return OK;
2209}
2210
2211
Bram Moolenaara5565e42020-05-09 15:44:01 +02002212// Structure passed between the compile_expr* functions to keep track of
2213// constants that have been parsed but for which no code was produced yet. If
2214// possible expressions on these constants are applied at compile time. If
2215// that is not possible, the code to push the constants needs to be generated
2216// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002217// Using 50 should be more than enough of 5 levels of ().
2218#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002219typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002220 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002221 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002222 int pp_is_const; // all generated code was constants, used for a
2223 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224} ppconst_T;
2225
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002226static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002227static int compile_expr0(char_u **arg, cctx_T *cctx);
2228static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2229
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230/*
2231 * Generate a PUSH instruction for "tv".
2232 * "tv" will be consumed or cleared.
2233 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2234 */
2235 static int
2236generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2237{
2238 if (tv != NULL)
2239 {
2240 switch (tv->v_type)
2241 {
2242 case VAR_UNKNOWN:
2243 break;
2244 case VAR_BOOL:
2245 generate_PUSHBOOL(cctx, tv->vval.v_number);
2246 break;
2247 case VAR_SPECIAL:
2248 generate_PUSHSPEC(cctx, tv->vval.v_number);
2249 break;
2250 case VAR_NUMBER:
2251 generate_PUSHNR(cctx, tv->vval.v_number);
2252 break;
2253#ifdef FEAT_FLOAT
2254 case VAR_FLOAT:
2255 generate_PUSHF(cctx, tv->vval.v_float);
2256 break;
2257#endif
2258 case VAR_BLOB:
2259 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2260 tv->vval.v_blob = NULL;
2261 break;
2262 case VAR_STRING:
2263 generate_PUSHS(cctx, tv->vval.v_string);
2264 tv->vval.v_string = NULL;
2265 break;
2266 default:
2267 iemsg("constant type not supported");
2268 clear_tv(tv);
2269 return FAIL;
2270 }
2271 tv->v_type = VAR_UNKNOWN;
2272 }
2273 return OK;
2274}
2275
2276/*
2277 * Generate code for any ppconst entries.
2278 */
2279 static int
2280generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2281{
2282 int i;
2283 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002284 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002285
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002286 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002287 for (i = 0; i < ppconst->pp_used; ++i)
2288 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2289 ret = FAIL;
2290 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002291 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002292 return ret;
2293}
2294
2295/*
2296 * Clear ppconst constants. Used when failing.
2297 */
2298 static void
2299clear_ppconst(ppconst_T *ppconst)
2300{
2301 int i;
2302
2303 for (i = 0; i < ppconst->pp_used; ++i)
2304 clear_tv(&ppconst->pp_tv[i]);
2305 ppconst->pp_used = 0;
2306}
2307
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002308/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002309 * Generate an instruction to load script-local variable "name", without the
2310 * leading "s:".
2311 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 */
2313 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002314compile_load_scriptvar(
2315 cctx_T *cctx,
2316 char_u *name, // variable NUL terminated
2317 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002318 char_u **end, // end of variable
2319 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002321 scriptitem_T *si;
2322 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 imported_T *import;
2324
Bram Moolenaare3d46852020-08-29 13:39:17 +02002325 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2326 return FAIL;
2327 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002328 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002329 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002331 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002332 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2333 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 }
2335 if (idx >= 0)
2336 {
2337 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 current_sctx.sc_sid, idx, sv->sv_type);
2341 return OK;
2342 }
2343
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002344 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (import != NULL)
2346 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002347 if (import->imp_all)
2348 {
2349 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002350 char_u *exp_name;
2351 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002352 ufunc_T *ufunc;
2353 type_T *type;
2354
2355 // Used "import * as Name", need to lookup the member.
2356 if (*p != '.')
2357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002358 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002359 return FAIL;
2360 }
2361 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002362 if (VIM_ISWHITE(*p))
2363 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002364 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002365 return FAIL;
2366 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002367
Bram Moolenaar1c991142020-07-04 13:15:31 +02002368 // isolate one name
2369 exp_name = p;
2370 while (eval_isnamec(*p))
2371 ++p;
2372 cc = *p;
2373 *p = NUL;
2374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002375 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002376 *p = cc;
2377 p = skipwhite(p);
2378
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 // TODO: what if it is a function?
2380 if (idx < 0)
2381 return FAIL;
2382 *end = p;
2383
2384 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2385 import->imp_sid,
2386 idx,
2387 type);
2388 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002389 else if (import->imp_funcname != NULL)
2390 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002391 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002392 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2393 import->imp_sid,
2394 import->imp_var_vals_idx,
2395 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 return OK;
2397 }
2398
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002399 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002400 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return FAIL;
2402}
2403
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002404 static int
2405generate_funcref(cctx_T *cctx, char_u *name)
2406{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002407 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002408
2409 if (ufunc == NULL)
2410 return FAIL;
2411
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002412 // Need to compile any default values to get the argument types.
2413 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2414 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2415 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002416 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002417}
2418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419/*
2420 * Compile a variable name into a load instruction.
2421 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002422 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 * When "error" is FALSE do not give an error when not found.
2424 */
2425 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426compile_load(
2427 char_u **arg,
2428 char_u *end_arg,
2429 cctx_T *cctx,
2430 int is_expr,
2431 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432{
2433 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002434 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002435 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002437 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 if (*(*arg + 1) == ':')
2440 {
2441 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002442 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002443 {
2444 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002446 switch (**arg)
2447 {
2448 case 'g': isn_type = ISN_LOADGDICT; break;
2449 case 'w': isn_type = ISN_LOADWDICT; break;
2450 case 't': isn_type = ISN_LOADTDICT; break;
2451 case 'b': isn_type = ISN_LOADBDICT; break;
2452 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002453 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002454 goto theend;
2455 }
2456 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2457 goto theend;
2458 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 else
2461 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002462 isntype_T isn_type = ISN_DROP;
2463
2464 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2465 if (name == NULL)
2466 return FAIL;
2467
2468 switch (**arg)
2469 {
2470 case 'v': res = generate_LOADV(cctx, name, error);
2471 break;
2472 case 's': res = compile_load_scriptvar(cctx, name,
2473 NULL, NULL, error);
2474 break;
2475 case 'g': isn_type = ISN_LOADG; break;
2476 case 'w': isn_type = ISN_LOADW; break;
2477 case 't': isn_type = ISN_LOADT; break;
2478 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002479 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002480 goto theend;
2481 }
2482 if (isn_type != ISN_DROP)
2483 {
2484 // Global, Buffer-local, Window-local and Tabpage-local
2485 // variables can be defined later, thus we don't check if it
2486 // exists, give error at runtime.
2487 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 }
2490 }
2491 else
2492 {
2493 size_t len = end - *arg;
2494 int idx;
2495 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002496 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497
2498 name = vim_strnsave(*arg, end - *arg);
2499 if (name == NULL)
2500 return FAIL;
2501
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002504 if (!gen_load_outer)
2505 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
2507 else
2508 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002509 lvar_T *lvar = lookup_local(*arg, len, cctx);
2510
2511 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002513 type = lvar->lv_type;
2514 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002515 if (lvar->lv_from_outer)
2516 gen_load_outer = TRUE;
2517 else
2518 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 }
2520 else
2521 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002522 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002523 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002524 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002525 || find_imported(name, 0, cctx) != NULL)
2526 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002527
Bram Moolenaar0f769812020-09-12 18:32:34 +02002528 // When evaluating an expression and the name starts with an
2529 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002530 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002531 if (res == FAIL && is_expr
2532 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 }
2535 }
2536 if (gen_load)
2537 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002538 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002539 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002540 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002541 cctx->ctx_outer_used = TRUE;
2542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
2544
2545 *arg = end;
2546
2547theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002548 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 vim_free(name);
2551 return res;
2552}
2553
2554/*
2555 * Compile the argument expressions.
2556 * "arg" points to just after the "(" and is advanced to after the ")"
2557 */
2558 static int
2559compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2560{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002561 char_u *p = *arg;
2562 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002563 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002567 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2568 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 if (*p == ')')
2570 {
2571 *arg = p + 1;
2572 return OK;
2573 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002574 if (must_end)
2575 {
2576 semsg(_(e_missing_comma_before_argument_str), p);
2577 return FAIL;
2578 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002579
Bram Moolenaara5565e42020-05-09 15:44:01 +02002580 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 return FAIL;
2582 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002583
2584 if (*p != ',' && *skipwhite(p) == ',')
2585 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002586 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002587 p = skipwhite(p);
2588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002590 {
2591 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002592 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002593 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002594 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002595 else
2596 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002597 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002598 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002601 emsg(_(e_missing_close));
2602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603}
2604
2605/*
2606 * Compile a function call: name(arg1, arg2)
2607 * "arg" points to "name", "arg + varlen" to the "(".
2608 * "argcount_init" is 1 for "value->method()"
2609 * Instructions:
2610 * EVAL arg1
2611 * EVAL arg2
2612 * BCALL / DCALL / UCALL
2613 */
2614 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002615compile_call(
2616 char_u **arg,
2617 size_t varlen,
2618 cctx_T *cctx,
2619 ppconst_T *ppconst,
2620 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621{
2622 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002623 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 int argcount = argcount_init;
2625 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002626 char_u fname_buf[FLEN_FIXED + 1];
2627 char_u *tofree = NULL;
2628 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002630 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002631 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633 // we can evaluate "has('name')" at compile time
2634 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2635 {
2636 char_u *s = skipwhite(*arg + varlen + 1);
2637 typval_T argvars[2];
2638
2639 argvars[0].v_type = VAR_UNKNOWN;
2640 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002641 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002642 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002643 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002644 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002645 if (*s == ')' && argvars[0].v_type == VAR_STRING
2646 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647 {
2648 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2649
2650 *arg = s + 1;
2651 argvars[1].v_type = VAR_UNKNOWN;
2652 tv->v_type = VAR_NUMBER;
2653 tv->vval.v_number = 0;
2654 f_has(argvars, tv);
2655 clear_tv(&argvars[0]);
2656 ++ppconst->pp_used;
2657 return OK;
2658 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002659 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 }
2661
2662 if (generate_ppconst(cctx, ppconst) == FAIL)
2663 return FAIL;
2664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (varlen >= sizeof(namebuf))
2666 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002667 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 return FAIL;
2669 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002670 vim_strncpy(namebuf, *arg, varlen);
2671 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 *arg = skipwhite(*arg + varlen + 1);
2674 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
Bram Moolenaara1773442020-08-12 15:21:22 +02002677 is_autoload = vim_strchr(name, '#') != NULL;
2678 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
2680 int idx;
2681
2682 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002683 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002685 {
2686 if (STRCMP(name, "add") == 0 && argcount == 2)
2687 {
2688 garray_T *stack = &cctx->ctx_type_stack;
2689 type_T *type = ((type_T **)stack->ga_data)[
2690 stack->ga_len - 2];
2691
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002692 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002693 if (type->tt_type == VAR_LIST)
2694 {
2695 // inline "add(list, item)" so that the type can be checked
2696 res = generate_LISTAPPEND(cctx);
2697 idx = -1;
2698 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002699 else if (type->tt_type == VAR_BLOB)
2700 {
2701 // inline "add(blob, nr)" so that the type can be checked
2702 res = generate_BLOBAPPEND(cctx);
2703 idx = -1;
2704 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002705 }
2706
2707 if (idx >= 0)
2708 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2709 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002710 else
2711 semsg(_(e_unknownfunc), namebuf);
2712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002716 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002717 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002718 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002719 {
2720 res = generate_CALL(cctx, ufunc, argcount);
2721 goto theend;
2722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723
2724 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002725 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002726 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002728 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002729 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002730 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002731 garray_T *stack = &cctx->ctx_type_stack;
2732 type_T *type;
2733
2734 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2735 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002736 goto theend;
2737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738
Bram Moolenaar0f769812020-09-12 18:32:34 +02002739 // If we can find a global function by name generate the right call.
2740 if (ufunc != NULL)
2741 {
2742 res = generate_CALL(cctx, ufunc, argcount);
2743 goto theend;
2744 }
2745
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002746 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002747 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002748 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002749 res = generate_UCALL(cctx, name, argcount);
2750 else
2751 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002752
2753theend:
2754 vim_free(tofree);
2755 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756}
2757
2758// like NAMESPACE_CHAR but with 'a' and 'l'.
2759#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2760
2761/*
2762 * Find the end of a variable or function name. Unlike find_name_end() this
2763 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002764 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 * Return a pointer to just after the name. Equal to "arg" if there is no
2766 * valid name.
2767 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002768 static char_u *
2769to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770{
2771 char_u *p;
2772
2773 // Quick check for valid starting character.
2774 if (!eval_isnamec1(*arg))
2775 return arg;
2776
2777 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2778 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2779 // and can be used in slice "[n:]".
2780 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002781 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2783 break;
2784 return p;
2785}
2786
2787/*
2788 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002789 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 */
2791 char_u *
2792to_name_const_end(char_u *arg)
2793{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002794 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 typval_T rettv;
2796
2797 if (p == arg && *arg == '[')
2798 {
2799
2800 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002801 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 p = arg;
2803 }
2804 else if (p == arg && *arg == '#' && arg[1] == '{')
2805 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002806 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002808 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 p = arg;
2810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811
2812 return p;
2813}
2814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815/*
2816 * parse a list: [expr, expr]
2817 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002818 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 */
2820 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002821compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822{
2823 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002824 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002826 int is_const;
2827 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002829 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002831 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002832 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002833 semsg(_(e_list_end), *arg);
2834 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002835 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002836 if (*p == ',')
2837 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002838 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002839 return FAIL;
2840 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002841 if (*p == ']')
2842 {
2843 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002844 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002845 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002846 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002847 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002848 if (!is_const)
2849 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 ++count;
2851 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002852 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002854 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2855 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002856 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002857 return FAIL;
2858 }
2859 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002860 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 p = skipwhite(p);
2862 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002863 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002865 ppconst->pp_is_const = is_all_const;
2866 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867}
2868
2869/*
2870 * parse a lambda: {arg, arg -> expr}
2871 * "*arg" points to the '{'.
2872 */
2873 static int
2874compile_lambda(char_u **arg, cctx_T *cctx)
2875{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 typval_T rettv;
2877 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002878 evalarg_T evalarg;
2879
2880 CLEAR_FIELD(evalarg);
2881 evalarg.eval_flags = EVAL_EVALUATE;
2882 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883
2884 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002885 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002886 {
2887 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002889 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002892 ++ufunc->uf_refcount;
2893 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
2895 // The function will have one line: "return {expr}".
2896 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002897 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002899 clear_evalarg(&evalarg, NULL);
2900
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002901 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002902 {
2903 // The return type will now be known.
2904 set_function_type(ufunc);
2905
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002906 // The function reference count will be 1. When the ISN_FUNCREF
2907 // instruction is deleted the reference count is decremented and the
2908 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002909 return generate_FUNCREF(cctx, ufunc);
2910 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002911
2912 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 return FAIL;
2914}
2915
2916/*
2917 * Compile a lamda call: expr->{lambda}(args)
2918 * "arg" points to the "{".
2919 */
2920 static int
2921compile_lambda_call(char_u **arg, cctx_T *cctx)
2922{
2923 ufunc_T *ufunc;
2924 typval_T rettv;
2925 int argcount = 1;
2926 int ret = FAIL;
2927
2928 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002929 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 return FAIL;
2931
2932 if (**arg != '(')
2933 {
2934 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002935 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 else
2937 semsg(_(e_missing_paren), "lambda");
2938 clear_tv(&rettv);
2939 return FAIL;
2940 }
2941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 ufunc = rettv.vval.v_partial->pt_func;
2943 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002944 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002945 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002946
Bram Moolenaara05e5242020-09-19 18:19:19 +02002947 // The function will have one line: "return {expr}". Compile it into
2948 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002949 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950
2951 // compile the arguments
2952 *arg = skipwhite(*arg + 1);
2953 if (compile_arguments(arg, cctx, &argcount) == OK)
2954 // call the compiled function
2955 ret = generate_CALL(cctx, ufunc, argcount);
2956
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002957 if (ret == FAIL)
2958 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 return ret;
2960}
2961
2962/*
2963 * parse a dict: {'key': val} or #{key: val}
2964 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002965 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 */
2967 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002968compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969{
2970 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002971 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 int count = 0;
2973 dict_T *d = dict_alloc();
2974 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002975 char_u *whitep = *arg;
2976 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002977 int is_const;
2978 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979
2980 if (d == NULL)
2981 return FAIL;
2982 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002983 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 {
2985 char_u *key = NULL;
2986
Bram Moolenaar23c55272020-06-21 16:58:13 +02002987 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002988 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002989 *arg = NULL;
2990 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002991 }
2992
2993 if (**arg == '}')
2994 break;
2995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 if (literal)
2997 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002998 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999
Bram Moolenaar2c330432020-04-13 14:41:35 +02003000 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003002 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 return FAIL;
3004 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003005 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 if (generate_PUSHS(cctx, key) == FAIL)
3007 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003008 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 }
3010 else
3011 {
3012 isn_T *isn;
3013
Bram Moolenaara5565e42020-05-09 15:44:01 +02003014 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3017 if (isn->isn_type == ISN_PUSHS)
3018 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003019 else
3020 {
3021 type_T *keytype = ((type_T **)stack->ga_data)
3022 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003023 if (need_type(keytype, &t_string, -1, cctx,
3024 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003025 return FAIL;
3026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028
3029 // Check for duplicate keys, if using string keys.
3030 if (key != NULL)
3031 {
3032 item = dict_find(d, key, -1);
3033 if (item != NULL)
3034 {
3035 semsg(_(e_duplicate_key), key);
3036 goto failret;
3037 }
3038 item = dictitem_alloc(key);
3039 if (item != NULL)
3040 {
3041 item->di_tv.v_type = VAR_UNKNOWN;
3042 item->di_tv.v_lock = 0;
3043 if (dict_add(d, item) == FAIL)
3044 dictitem_free(item);
3045 }
3046 }
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 if (**arg != ':')
3049 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003050 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003051 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003052 else
3053 semsg(_(e_missing_dict_colon), *arg);
3054 return FAIL;
3055 }
3056 whitep = *arg + 1;
3057 if (!IS_WHITE_OR_NUL(*whitep))
3058 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003059 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return FAIL;
3061 }
3062
3063 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003064 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003065 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003066 *arg = NULL;
3067 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003068 }
3069
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003070 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003072 if (!is_const)
3073 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 ++count;
3075
Bram Moolenaar2c330432020-04-13 14:41:35 +02003076 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003077 *arg = skipwhite(*arg);
3078 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003079 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003080 *arg = NULL;
3081 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 if (**arg == '}')
3084 break;
3085 if (**arg != ',')
3086 {
3087 semsg(_(e_missing_dict_comma), *arg);
3088 goto failret;
3089 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003090 if (IS_WHITE_OR_NUL(*whitep))
3091 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003092 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003093 return FAIL;
3094 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003095 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003096 if (!IS_WHITE_OR_NUL(*whitep))
3097 {
3098 semsg(_(e_white_space_required_after_str), ",");
3099 return FAIL;
3100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 *arg = skipwhite(*arg + 1);
3102 }
3103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 *arg = *arg + 1;
3105
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003106 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003108 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003109 *arg += STRLEN(*arg);
3110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003112 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 return generate_NEWDICT(cctx, count);
3114
3115failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003116 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003117 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003118 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003119 *arg = (char_u *)"";
3120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 dict_unref(d);
3122 return FAIL;
3123}
3124
3125/*
3126 * Compile "&option".
3127 */
3128 static int
3129compile_get_option(char_u **arg, cctx_T *cctx)
3130{
3131 typval_T rettv;
3132 char_u *start = *arg;
3133 int ret;
3134
3135 // parse the option and get the current value to get the type.
3136 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003137 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 if (ret == OK)
3139 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003140 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 char_u *name = vim_strnsave(start, *arg - start);
3142 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3143
3144 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3145 vim_free(name);
3146 }
3147 clear_tv(&rettv);
3148
3149 return ret;
3150}
3151
3152/*
3153 * Compile "$VAR".
3154 */
3155 static int
3156compile_get_env(char_u **arg, cctx_T *cctx)
3157{
3158 char_u *start = *arg;
3159 int len;
3160 int ret;
3161 char_u *name;
3162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 ++*arg;
3164 len = get_env_len(arg);
3165 if (len == 0)
3166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003167 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 return FAIL;
3169 }
3170
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003171 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 name = vim_strnsave(start, len + 1);
3173 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3174 vim_free(name);
3175 return ret;
3176}
3177
3178/*
3179 * Compile "@r".
3180 */
3181 static int
3182compile_get_register(char_u **arg, cctx_T *cctx)
3183{
3184 int ret;
3185
3186 ++*arg;
3187 if (**arg == NUL)
3188 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003189 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 return FAIL;
3191 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003192 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 {
3194 emsg_invreg(**arg);
3195 return FAIL;
3196 }
3197 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3198 ++*arg;
3199 return ret;
3200}
3201
3202/*
3203 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003204 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 */
3206 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003207apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003209 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210
3211 // this works from end to start
3212 while (p > start)
3213 {
3214 --p;
3215 if (*p == '-' || *p == '+')
3216 {
3217 // only '-' has an effect, for '+' we only check the type
3218#ifdef FEAT_FLOAT
3219 if (rettv->v_type == VAR_FLOAT)
3220 {
3221 if (*p == '-')
3222 rettv->vval.v_float = -rettv->vval.v_float;
3223 }
3224 else
3225#endif
3226 {
3227 varnumber_T val;
3228 int error = FALSE;
3229
3230 // tv_get_number_chk() accepts a string, but we don't want that
3231 // here
3232 if (check_not_string(rettv) == FAIL)
3233 return FAIL;
3234 val = tv_get_number_chk(rettv, &error);
3235 clear_tv(rettv);
3236 if (error)
3237 return FAIL;
3238 if (*p == '-')
3239 val = -val;
3240 rettv->v_type = VAR_NUMBER;
3241 rettv->vval.v_number = val;
3242 }
3243 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003244 else if (numeric_only)
3245 {
3246 ++p;
3247 break;
3248 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003249 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 {
3251 int v = tv2bool(rettv);
3252
3253 // '!' is permissive in the type.
3254 clear_tv(rettv);
3255 rettv->v_type = VAR_BOOL;
3256 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3257 }
3258 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003259 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 return OK;
3261}
3262
3263/*
3264 * Recognize v: variables that are constants and set "rettv".
3265 */
3266 static void
3267get_vim_constant(char_u **arg, typval_T *rettv)
3268{
3269 if (STRNCMP(*arg, "v:true", 6) == 0)
3270 {
3271 rettv->v_type = VAR_BOOL;
3272 rettv->vval.v_number = VVAL_TRUE;
3273 *arg += 6;
3274 }
3275 else if (STRNCMP(*arg, "v:false", 7) == 0)
3276 {
3277 rettv->v_type = VAR_BOOL;
3278 rettv->vval.v_number = VVAL_FALSE;
3279 *arg += 7;
3280 }
3281 else if (STRNCMP(*arg, "v:null", 6) == 0)
3282 {
3283 rettv->v_type = VAR_SPECIAL;
3284 rettv->vval.v_number = VVAL_NULL;
3285 *arg += 6;
3286 }
3287 else if (STRNCMP(*arg, "v:none", 6) == 0)
3288 {
3289 rettv->v_type = VAR_SPECIAL;
3290 rettv->vval.v_number = VVAL_NONE;
3291 *arg += 6;
3292 }
3293}
3294
Bram Moolenaar696ba232020-07-29 21:20:41 +02003295 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003296get_compare_type(char_u *p, int *len, int *type_is)
3297{
3298 exptype_T type = EXPR_UNKNOWN;
3299 int i;
3300
3301 switch (p[0])
3302 {
3303 case '=': if (p[1] == '=')
3304 type = EXPR_EQUAL;
3305 else if (p[1] == '~')
3306 type = EXPR_MATCH;
3307 break;
3308 case '!': if (p[1] == '=')
3309 type = EXPR_NEQUAL;
3310 else if (p[1] == '~')
3311 type = EXPR_NOMATCH;
3312 break;
3313 case '>': if (p[1] != '=')
3314 {
3315 type = EXPR_GREATER;
3316 *len = 1;
3317 }
3318 else
3319 type = EXPR_GEQUAL;
3320 break;
3321 case '<': if (p[1] != '=')
3322 {
3323 type = EXPR_SMALLER;
3324 *len = 1;
3325 }
3326 else
3327 type = EXPR_SEQUAL;
3328 break;
3329 case 'i': if (p[1] == 's')
3330 {
3331 // "is" and "isnot"; but not a prefix of a name
3332 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3333 *len = 5;
3334 i = p[*len];
3335 if (!isalnum(i) && i != '_')
3336 {
3337 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3338 *type_is = TRUE;
3339 }
3340 }
3341 break;
3342 }
3343 return type;
3344}
3345
3346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003348 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 */
3350 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003351compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003353 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354
3355 // this works from end to start
3356 while (p > start)
3357 {
3358 --p;
3359 if (*p == '-' || *p == '+')
3360 {
3361 int negate = *p == '-';
3362 isn_T *isn;
3363
3364 // TODO: check type
3365 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3366 {
3367 --p;
3368 if (*p == '-')
3369 negate = !negate;
3370 }
3371 // only '-' has an effect, for '+' we only check the type
3372 if (negate)
3373 isn = generate_instr(cctx, ISN_NEGATENR);
3374 else
3375 isn = generate_instr(cctx, ISN_CHECKNR);
3376 if (isn == NULL)
3377 return FAIL;
3378 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003379 else if (numeric_only)
3380 {
3381 ++p;
3382 break;
3383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 else
3385 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003386 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003388 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003390 if (p[-1] == '!')
3391 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 }
3394 if (generate_2BOOL(cctx, invert) == FAIL)
3395 return FAIL;
3396 }
3397 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003398 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 return OK;
3400}
3401
3402/*
3403 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003404 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 */
3406 static int
3407compile_subscript(
3408 char_u **arg,
3409 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003410 char_u *start_leader,
3411 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003412 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003414 char_u *name_start = *end_leader;
3415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 for (;;)
3417 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003418 char_u *p = skipwhite(*arg);
3419
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003420 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003421 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003422 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003423
3424 // If a following line starts with "->{" or "->X" advance to that
3425 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003426 // Also if a following line starts with ".x".
3427 if (next != NULL &&
3428 ((next[0] == '-' && next[1] == '>'
3429 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003430 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003431 {
3432 next = next_line_from_context(cctx, TRUE);
3433 if (next == NULL)
3434 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003435 *arg = next;
3436 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003437 }
3438 }
3439
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003440 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3441 // not a function call.
3442 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003444 garray_T *stack = &cctx->ctx_type_stack;
3445 type_T *type;
3446 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003448 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003449 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003450 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003453 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3454
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003455 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3457 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003458 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 return FAIL;
3460 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003461 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003463 char_u *pstart = p;
3464
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003465 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003466 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003467 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 // something->method()
3470 // Apply the '!', '-' and '+' first:
3471 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003472 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003475 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003476 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003477 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 if (**arg == '{')
3479 {
3480 // lambda call: list->{lambda}
3481 if (compile_lambda_call(arg, cctx) == FAIL)
3482 return FAIL;
3483 }
3484 else
3485 {
3486 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003487 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003488 if (!eval_isnamec1(*p))
3489 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003490 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003491 return FAIL;
3492 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003493 if (ASCII_ISALPHA(*p) && p[1] == ':')
3494 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003495 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 ;
3497 if (*p != '(')
3498 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003499 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return FAIL;
3501 }
3502 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003503 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 return FAIL;
3505 }
3506 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003507 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003509 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003510 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003511 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003512 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003513 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003514
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003516 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003517 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003518 // TODO: blob index
3519 // TODO: more arguments
3520 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003521 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003522 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003523 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003524
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003525 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003526 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003527 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003528 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003529 if (**arg == ':')
3530 // missing first index is equal to zero
3531 generate_PUSHNR(cctx, 0);
3532 else
3533 {
3534 if (compile_expr0(arg, cctx) == FAIL)
3535 return FAIL;
3536 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3537 return FAIL;
3538 *arg = skipwhite(*arg);
3539 }
3540 if (**arg == ':')
3541 {
3542 *arg = skipwhite(*arg + 1);
3543 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3544 return FAIL;
3545 if (**arg == ']')
3546 // missing second index is equal to end of string
3547 generate_PUSHNR(cctx, -1);
3548 else
3549 {
3550 if (compile_expr0(arg, cctx) == FAIL)
3551 return FAIL;
3552 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3553 return FAIL;
3554 *arg = skipwhite(*arg);
3555 }
3556 is_slice = TRUE;
3557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558
3559 if (**arg != ']')
3560 {
3561 emsg(_(e_missbrac));
3562 return FAIL;
3563 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003564 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003566 // We can index a list and a dict. If we don't know the type
3567 // we can use the index value type.
3568 // TODO: If we don't know use an instruction to figure it out at
3569 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003570 typep = ((type_T **)stack->ga_data) + stack->ga_len
3571 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003572 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003573 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3574 // If the index is a string, the variable must be a Dict.
3575 if (*typep == &t_any && valtype == &t_string)
3576 vtype = VAR_DICT;
3577 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003578 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003579 if (need_type(valtype, &t_number, -1, cctx,
3580 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003581 return FAIL;
3582 if (is_slice)
3583 {
3584 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003585 if (need_type(valtype, &t_number, -2, cctx,
3586 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003587 return FAIL;
3588 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003589 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003590
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003591 if (vtype == VAR_DICT)
3592 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003593 if (is_slice)
3594 {
3595 emsg(_(e_cannot_slice_dictionary));
3596 return FAIL;
3597 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003598 if ((*typep)->tt_type == VAR_DICT)
3599 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003600 else
3601 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003602 if (need_type(*typep, &t_dict_any, -2, cctx,
3603 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003604 return FAIL;
3605 *typep = &t_any;
3606 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003607 if (may_generate_2STRING(-1, cctx) == FAIL)
3608 return FAIL;
3609 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3610 return FAIL;
3611 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003612 else if (vtype == VAR_STRING)
3613 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003614 *typep = &t_string;
3615 if ((is_slice
3616 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3617 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003618 return FAIL;
3619 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003620 else if (vtype == VAR_BLOB)
3621 {
3622 emsg("Sorry, blob index and slice not implemented yet");
3623 return FAIL;
3624 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003625 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003626 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003627 if (is_slice)
3628 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003629 if (generate_instr_drop(cctx,
3630 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3631 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003632 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003633 }
Bram Moolenaared591872020-08-15 22:14:53 +02003634 else
3635 {
3636 if ((*typep)->tt_type == VAR_LIST)
3637 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003638 if (generate_instr_drop(cctx,
3639 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3640 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003641 return FAIL;
3642 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003643 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003644 else
3645 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003646 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003647 return FAIL;
3648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003650 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003652 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003653 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003655 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003656
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003657 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003658 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003659 {
3660 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003661 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003662 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003663 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003664 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 while (eval_isnamec(*p))
3666 MB_PTR_ADV(p);
3667 if (p == *arg)
3668 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003669 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 return FAIL;
3671 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003672 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 return FAIL;
3674 *arg = p;
3675 }
3676 else
3677 break;
3678 }
3679
3680 // TODO - see handle_subscript():
3681 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3682 // Don't do this when "Func" is already a partial that was bound
3683 // explicitly (pt_auto is FALSE).
3684
3685 return OK;
3686}
3687
3688/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3690 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003692 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003693 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003694 *
3695 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 */
3697
3698/*
3699 * number number constant
3700 * 0zFFFFFFFF Blob constant
3701 * "string" string constant
3702 * 'string' literal string constant
3703 * &option-name option value
3704 * @r register contents
3705 * identifier variable value
3706 * function() function call
3707 * $VAR environment variable
3708 * (expression) nested expression
3709 * [expr, expr] List
3710 * {key: val, key: val} Dictionary
3711 * #{key: val, key: val} Dictionary with literal keys
3712 *
3713 * Also handle:
3714 * ! in front logical NOT
3715 * - in front unary minus
3716 * + in front unary plus (ignored)
3717 * trailing (arg) funcref/partial call
3718 * trailing [] subscript in String or List
3719 * trailing .name entry in Dictionary
3720 * trailing ->name() method call
3721 */
3722 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723compile_expr7(
3724 char_u **arg,
3725 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003726 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 char_u *start_leader, *end_leader;
3729 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003730 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003731 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003733 ppconst->pp_is_const = FALSE;
3734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 /*
3736 * Skip '!', '-' and '+' characters. They are handled later.
3737 */
3738 start_leader = *arg;
3739 while (**arg == '!' || **arg == '-' || **arg == '+')
3740 *arg = skipwhite(*arg + 1);
3741 end_leader = *arg;
3742
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003743 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 switch (**arg)
3745 {
3746 /*
3747 * Number constant.
3748 */
3749 case '0': // also for blob starting with 0z
3750 case '1':
3751 case '2':
3752 case '3':
3753 case '4':
3754 case '5':
3755 case '6':
3756 case '7':
3757 case '8':
3758 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003759 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003761 // Apply "-" and "+" just before the number now, right to
3762 // left. Matters especially when "->" follows. Stops at
3763 // '!'.
3764 if (apply_leader(rettv, TRUE,
3765 start_leader, &end_leader) == FAIL)
3766 {
3767 clear_tv(rettv);
3768 return FAIL;
3769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 break;
3771
3772 /*
3773 * String constant: "string".
3774 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003775 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 break;
3778
3779 /*
3780 * Literal string constant: 'str''ing'.
3781 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003782 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 return FAIL;
3784 break;
3785
3786 /*
3787 * Constant Vim variable.
3788 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003789 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 ret = NOTDONE;
3791 break;
3792
3793 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003794 * "true" constant
3795 */
3796 case 't': if (STRNCMP(*arg, "true", 4) == 0
3797 && !eval_isnamec((*arg)[4]))
3798 {
3799 *arg += 4;
3800 rettv->v_type = VAR_BOOL;
3801 rettv->vval.v_number = VVAL_TRUE;
3802 }
3803 else
3804 ret = NOTDONE;
3805 break;
3806
3807 /*
3808 * "false" constant
3809 */
3810 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3811 && !eval_isnamec((*arg)[5]))
3812 {
3813 *arg += 5;
3814 rettv->v_type = VAR_BOOL;
3815 rettv->vval.v_number = VVAL_FALSE;
3816 }
3817 else
3818 ret = NOTDONE;
3819 break;
3820
3821 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 * List: [expr, expr]
3823 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003824 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 break;
3826
3827 /*
3828 * Dictionary: #{key: val, key: val}
3829 */
3830 case '#': if ((*arg)[1] == '{')
3831 {
3832 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003833 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 }
3835 else
3836 ret = NOTDONE;
3837 break;
3838
3839 /*
3840 * Lambda: {arg, arg -> expr}
3841 * Dictionary: {'key': val, 'key': val}
3842 */
3843 case '{': {
3844 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003845 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846
3847 // Find out what comes after the arguments.
3848 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003849 &ga_arg, TRUE, NULL, NULL,
3850 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 if (ret != FAIL && *start == '>')
3852 ret = compile_lambda(arg, cctx);
3853 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003854 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 }
3856 break;
3857
3858 /*
3859 * Option value: &name
3860 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003861 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3862 return FAIL;
3863 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 break;
3865
3866 /*
3867 * Environment variable: $VAR.
3868 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003869 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3870 return FAIL;
3871 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 break;
3873
3874 /*
3875 * Register contents: @r.
3876 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003877 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3878 return FAIL;
3879 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 break;
3881 /*
3882 * nested expression: (expression).
3883 */
3884 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003885
3886 // recursive!
3887 if (ppconst->pp_used <= PPSIZE - 10)
3888 {
3889 ret = compile_expr1(arg, cctx, ppconst);
3890 }
3891 else
3892 {
3893 // Not enough space in ppconst, flush constants.
3894 if (generate_ppconst(cctx, ppconst) == FAIL)
3895 return FAIL;
3896 ret = compile_expr0(arg, cctx);
3897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 *arg = skipwhite(*arg);
3899 if (**arg == ')')
3900 ++*arg;
3901 else if (ret == OK)
3902 {
3903 emsg(_(e_missing_close));
3904 ret = FAIL;
3905 }
3906 break;
3907
3908 default: ret = NOTDONE;
3909 break;
3910 }
3911 if (ret == FAIL)
3912 return FAIL;
3913
Bram Moolenaar1c747212020-05-09 18:28:34 +02003914 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003916 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003917 clear_tv(rettv);
3918 else
3919 // A constant expression can possibly be handled compile time,
3920 // return the value instead of generating code.
3921 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 }
3923 else if (ret == NOTDONE)
3924 {
3925 char_u *p;
3926 int r;
3927
3928 if (!eval_isnamec1(**arg))
3929 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003930 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 return FAIL;
3932 }
3933
3934 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003935 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 {
3938 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941 {
3942 if (generate_ppconst(cctx, ppconst) == FAIL)
3943 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003944 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 if (r == FAIL)
3947 return FAIL;
3948 }
3949
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003950 // Handle following "[]", ".member", etc.
3951 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003952 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003953 ppconst) == FAIL)
3954 return FAIL;
3955 if (ppconst->pp_used > 0)
3956 {
3957 // apply the '!', '-' and '+' before the constant
3958 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003959 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003960 return FAIL;
3961 return OK;
3962 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003963 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003965 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966}
3967
3968/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003969 * Give the "white on both sides" error, taking the operator from "p[len]".
3970 */
3971 void
3972error_white_both(char_u *op, int len)
3973{
3974 char_u buf[10];
3975
3976 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003977 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003978}
3979
3980/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003981 * <type>expr7: runtime type check / conversion
3982 */
3983 static int
3984compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3985{
3986 type_T *want_type = NULL;
3987
3988 // Recognize <type>
3989 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3990 {
3991 int called_emsg_before = called_emsg;
3992
3993 ++*arg;
3994 want_type = parse_type(arg, cctx->ctx_type_list);
3995 if (called_emsg != called_emsg_before)
3996 return FAIL;
3997
3998 if (**arg != '>')
3999 {
4000 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004001 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004002 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004003 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004004 return FAIL;
4005 }
4006 ++*arg;
4007 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4008 return FAIL;
4009 }
4010
4011 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4012 return FAIL;
4013
4014 if (want_type != NULL)
4015 {
4016 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004017 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004018
Bram Moolenaard1103582020-08-14 22:44:25 +02004019 generate_ppconst(cctx, ppconst);
4020 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004021 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004022 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004023 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004024 return FAIL;
4025 }
4026 }
4027
4028 return OK;
4029}
4030
4031/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 * * number multiplication
4033 * / number division
4034 * % number modulo
4035 */
4036 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004037compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038{
4039 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004040 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004041 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004043 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004044 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
4046
4047 /*
4048 * Repeat computing, until no "*", "/" or "%" is following.
4049 */
4050 for (;;)
4051 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004052 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 if (*op != '*' && *op != '/' && *op != '%')
4054 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004055 if (next != NULL)
4056 {
4057 *arg = next_line_from_context(cctx, TRUE);
4058 op = skipwhite(*arg);
4059 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004060
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004061 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004063 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004064 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 }
4066 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004067 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004068 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004070 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004071 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073
4074 if (ppconst->pp_used == ppconst_used + 2
4075 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4076 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004077 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004078 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4079 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004080 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082 // both are numbers: compute the result
4083 switch (*op)
4084 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004085 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004087 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004089 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004090 break;
4091 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004092 tv1->vval.v_number = res;
4093 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 }
4095 else
4096 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004097 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 generate_two_op(cctx, op);
4099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 }
4101
4102 return OK;
4103}
4104
4105/*
4106 * + number addition
4107 * - number subtraction
4108 * .. string concatenation
4109 */
4110 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004111compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112{
4113 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004114 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
4118 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004119 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 return FAIL;
4121
4122 /*
4123 * Repeat computing, until no "+", "-" or ".." is following.
4124 */
4125 for (;;)
4126 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004127 op = may_peek_next_line(cctx, *arg, &next);
4128 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 break;
4130 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004131 if (next != NULL)
4132 {
4133 *arg = next_line_from_context(cctx, TRUE);
4134 op = skipwhite(*arg);
4135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004137 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004139 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004140 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142
4143 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004144 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004145 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004147 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 return FAIL;
4150
Bram Moolenaara5565e42020-05-09 15:44:01 +02004151 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004152 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4154 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4155 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4156 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4159 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 // concat/subtract/add constant numbers
4162 if (*op == '+')
4163 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4164 else if (*op == '-')
4165 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4166 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004168 // concatenate constant strings
4169 char_u *s1 = tv1->vval.v_string;
4170 char_u *s2 = tv2->vval.v_string;
4171 size_t len1 = STRLEN(s1);
4172
4173 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4174 if (tv1->vval.v_string == NULL)
4175 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004177 return FAIL;
4178 }
4179 mch_memmove(tv1->vval.v_string, s1, len1);
4180 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004181 vim_free(s1);
4182 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004183 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 if (*op == '.')
4190 {
4191 if (may_generate_2STRING(-2, cctx) == FAIL
4192 || may_generate_2STRING(-1, cctx) == FAIL)
4193 return FAIL;
4194 generate_instr_drop(cctx, ISN_CONCAT, 1);
4195 }
4196 else
4197 generate_two_op(cctx, op);
4198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 }
4200
4201 return OK;
4202}
4203
4204/*
4205 * expr5a == expr5b
4206 * expr5a =~ expr5b
4207 * expr5a != expr5b
4208 * expr5a !~ expr5b
4209 * expr5a > expr5b
4210 * expr5a >= expr5b
4211 * expr5a < expr5b
4212 * expr5a <= expr5b
4213 * expr5a is expr5b
4214 * expr5a isnot expr5b
4215 *
4216 * Produces instructions:
4217 * EVAL expr5a Push result of "expr5a"
4218 * EVAL expr5b Push result of "expr5b"
4219 * COMPARE one of the compare instructions
4220 */
4221 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004222compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223{
4224 exptype_T type = EXPR_UNKNOWN;
4225 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004226 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230
4231 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return FAIL;
4234
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004235 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004236 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
4238 /*
4239 * If there is a comparative operator, use it.
4240 */
4241 if (type != EXPR_UNKNOWN)
4242 {
4243 int ic = FALSE; // Default: do not ignore case
4244
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004245 if (next != NULL)
4246 {
4247 *arg = next_line_from_context(cctx, TRUE);
4248 p = skipwhite(*arg);
4249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 if (type_is && (p[len] == '?' || p[len] == '#'))
4251 {
4252 semsg(_(e_invexpr2), *arg);
4253 return FAIL;
4254 }
4255 // extra question mark appended: ignore case
4256 if (p[len] == '?')
4257 {
4258 ic = TRUE;
4259 ++len;
4260 }
4261 // extra '#' appended: match case (ignored)
4262 else if (p[len] == '#')
4263 ++len;
4264 // nothing appended: match case
4265
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004266 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004268 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004269 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 }
4271
4272 // get the second variable
4273 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004274 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004275 return FAIL;
4276
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 return FAIL;
4279
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 if (ppconst->pp_used == ppconst_used + 2)
4281 {
4282 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4283 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4284 int ret;
4285
4286 // Both sides are a constant, compute the result now.
4287 // First check for a valid combination of types, this is more
4288 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004289 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004290 ret = FAIL;
4291 else
4292 {
4293 ret = typval_compare(tv1, tv2, type, ic);
4294 tv1->v_type = VAR_BOOL;
4295 tv1->vval.v_number = tv1->vval.v_number
4296 ? VVAL_TRUE : VVAL_FALSE;
4297 clear_tv(tv2);
4298 --ppconst->pp_used;
4299 }
4300 return ret;
4301 }
4302
4303 generate_ppconst(cctx, ppconst);
4304 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306
4307 return OK;
4308}
4309
Bram Moolenaar7f141552020-05-09 17:35:53 +02004310static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312/*
4313 * Compile || or &&.
4314 */
4315 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316compile_and_or(
4317 char_u **arg,
4318 cctx_T *cctx,
4319 char *op,
4320 ppconst_T *ppconst,
4321 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004323 char_u *next;
4324 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 int opchar = *op;
4326
4327 if (p[0] == opchar && p[1] == opchar)
4328 {
4329 garray_T *instr = &cctx->ctx_instr;
4330 garray_T end_ga;
4331
4332 /*
4333 * Repeat until there is no following "||" or "&&"
4334 */
4335 ga_init2(&end_ga, sizeof(int), 10);
4336 while (p[0] == opchar && p[1] == opchar)
4337 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004338 if (next != NULL)
4339 {
4340 *arg = next_line_from_context(cctx, TRUE);
4341 p = skipwhite(*arg);
4342 }
4343
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004344 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4345 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004346 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004347 return FAIL;
4348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004350 // TODO: use ppconst if the value is a constant and check
4351 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 generate_ppconst(cctx, ppconst);
4353
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004354 // Every part must evaluate to a bool.
4355 if (bool_on_stack(cctx) == FAIL)
4356 {
4357 ga_clear(&end_ga);
4358 return FAIL;
4359 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 if (ga_grow(&end_ga, 1) == FAIL)
4362 {
4363 ga_clear(&end_ga);
4364 return FAIL;
4365 }
4366 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4367 ++end_ga.ga_len;
4368 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004369 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
4371 // eval the next expression
4372 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004373 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 return FAIL;
4375
Bram Moolenaara5565e42020-05-09 15:44:01 +02004376 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4377 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 {
4379 ga_clear(&end_ga);
4380 return FAIL;
4381 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004382
4383 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004387 // Every part must evaluate to a bool.
4388 if (bool_on_stack(cctx) == FAIL)
4389 {
4390 ga_clear(&end_ga);
4391 return FAIL;
4392 }
4393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 // Fill in the end label in all jumps.
4395 while (end_ga.ga_len > 0)
4396 {
4397 isn_T *isn;
4398
4399 --end_ga.ga_len;
4400 isn = ((isn_T *)instr->ga_data)
4401 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4402 isn->isn_arg.jump.jump_where = instr->ga_len;
4403 }
4404 ga_clear(&end_ga);
4405 }
4406
4407 return OK;
4408}
4409
4410/*
4411 * expr4a && expr4a && expr4a logical AND
4412 *
4413 * Produces instructions:
4414 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004415 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004416 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004418 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 * EVAL expr4c Push result of "expr4c"
4420 * end:
4421 */
4422 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425 int ppconst_used = ppconst->pp_used;
4426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004428 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 return FAIL;
4430
4431 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004432 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433}
4434
4435/*
4436 * expr3a || expr3b || expr3c logical OR
4437 *
4438 * Produces instructions:
4439 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004440 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004441 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004443 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 * EVAL expr3c Push result of "expr3c"
4445 * end:
4446 */
4447 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 int ppconst_used = ppconst->pp_used;
4451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 return FAIL;
4455
4456 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004457 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458}
4459
4460/*
4461 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004463 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 * JUMP_IF_FALSE alt jump if false
4465 * EVAL expr1a
4466 * JUMP_ALWAYS end
4467 * alt: EVAL expr1b
4468 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004469 *
4470 * Toplevel expression: expr2 ?? expr1
4471 * Produces instructions:
4472 * EVAL expr2 Push result of "expr2"
4473 * JUMP_AND_KEEP_IF_TRUE end jump if true
4474 * EVAL expr1
4475 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 */
4477 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479{
4480 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004482 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483
Bram Moolenaar3988f642020-08-27 22:43:03 +02004484 // Ignore all kinds of errors when not producing code.
4485 if (cctx->ctx_skip == SKIP_YES)
4486 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004487 evalarg_T evalarg;
4488
4489 CLEAR_FIELD(evalarg);
4490 evalarg.eval_cctx = cctx;
4491 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004492 return OK;
4493 }
4494
Bram Moolenaar61a89812020-05-07 16:58:17 +02004495 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 return FAIL;
4498
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004499 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 if (*p == '?')
4501 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004502 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 garray_T *instr = &cctx->ctx_instr;
4504 garray_T *stack = &cctx->ctx_type_stack;
4505 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004506 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004508 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 int has_const_expr = FALSE;
4510 int const_value = FALSE;
4511 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004513 if (next != NULL)
4514 {
4515 *arg = next_line_from_context(cctx, TRUE);
4516 p = skipwhite(*arg);
4517 }
4518
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004519 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004520 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004521 semsg(_(e_white_space_required_before_and_after_str),
4522 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004523 return FAIL;
4524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 if (ppconst->pp_used == ppconst_used + 1)
4527 {
4528 // the condition is a constant, we know whether the ? or the :
4529 // expression is to be evaluated.
4530 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004531 if (op_falsy)
4532 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4533 else
4534 {
4535 int error = FALSE;
4536
4537 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4538 &error);
4539 if (error)
4540 return FAIL;
4541 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004542 cctx->ctx_skip = save_skip == SKIP_YES ||
4543 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4544
4545 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4546 // "left ?? right" and "left" is truthy: produce "left"
4547 generate_ppconst(cctx, ppconst);
4548 else
4549 {
4550 clear_tv(&ppconst->pp_tv[ppconst_used]);
4551 --ppconst->pp_used;
4552 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 }
4554 else
4555 {
4556 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004557 if (op_falsy)
4558 end_idx = instr->ga_len;
4559 generate_JUMP(cctx, op_falsy
4560 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4561 if (op_falsy)
4562 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564
4565 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004566 *arg = skipwhite(p + 1 + op_falsy);
4567 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004568 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004570 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572 if (!has_const_expr)
4573 {
4574 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004576 if (!op_falsy)
4577 {
4578 // remember the type and drop it
4579 --stack->ga_len;
4580 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004582 end_idx = instr->ga_len;
4583 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004584
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004585 // jump here from JUMP_IF_FALSE
4586 isn = ((isn_T *)instr->ga_data) + alt_idx;
4587 isn->isn_arg.jump.jump_where = instr->ga_len;
4588 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004591 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004593 // Check for the ":".
4594 p = may_peek_next_line(cctx, *arg, &next);
4595 if (*p != ':')
4596 {
4597 emsg(_(e_missing_colon));
4598 return FAIL;
4599 }
4600 if (next != NULL)
4601 {
4602 *arg = next_line_from_context(cctx, TRUE);
4603 p = skipwhite(*arg);
4604 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004606 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4607 {
4608 semsg(_(e_white_space_required_before_and_after_str), ":");
4609 return FAIL;
4610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004612 // evaluate the third expression
4613 if (has_const_expr)
4614 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004615 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004616 *arg = skipwhite(p + 1);
4617 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4618 return FAIL;
4619 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4620 return FAIL;
4621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (!has_const_expr)
4624 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004625 type_T **typep;
4626
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004630 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4631 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004633 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 isn = ((isn_T *)instr->ga_data) + end_idx;
4635 isn->isn_arg.jump.jump_where = instr->ga_len;
4636 }
4637
4638 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640 return OK;
4641}
4642
4643/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004644 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004645 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4646 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 */
4648 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004649compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650{
4651 ppconst_T ppconst;
4652
4653 CLEAR_FIELD(ppconst);
4654 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4655 {
4656 clear_ppconst(&ppconst);
4657 return FAIL;
4658 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004659 if (is_const != NULL)
4660 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 if (generate_ppconst(cctx, &ppconst) == FAIL)
4662 return FAIL;
4663 return OK;
4664}
4665
4666/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004667 * Toplevel expression.
4668 */
4669 static int
4670compile_expr0(char_u **arg, cctx_T *cctx)
4671{
4672 return compile_expr0_ext(arg, cctx, NULL);
4673}
4674
4675/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 * compile "return [expr]"
4677 */
4678 static char_u *
4679compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4680{
4681 char_u *p = arg;
4682 garray_T *stack = &cctx->ctx_type_stack;
4683 type_T *stack_type;
4684
4685 if (*p != NUL && *p != '|' && *p != '\n')
4686 {
4687 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 return NULL;
4690
4691 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4692 if (set_return_type)
4693 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004694 else
4695 {
4696 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4697 && stack_type->tt_type != VAR_VOID
4698 && stack_type->tt_type != VAR_UNKNOWN)
4699 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004700 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004701 return NULL;
4702 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004703 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004704 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004705 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
4708 else
4709 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004710 // "set_return_type" cannot be TRUE, only used for a lambda which
4711 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004712 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4713 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004715 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 return NULL;
4717 }
4718
4719 // No argument, return zero.
4720 generate_PUSHNR(cctx, 0);
4721 }
4722
4723 if (generate_instr(cctx, ISN_RETURN) == NULL)
4724 return NULL;
4725
4726 // "return val | endif" is possible
4727 return skipwhite(p);
4728}
4729
4730/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004731 * Get a line from the compilation context, compatible with exarg_T getline().
4732 * Return a pointer to the line in allocated memory.
4733 * Return NULL for end-of-file or some error.
4734 */
4735 static char_u *
4736exarg_getline(
4737 int c UNUSED,
4738 void *cookie,
4739 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004740 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004741{
4742 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004743 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004744
Bram Moolenaar66250c92020-08-20 15:02:42 +02004745 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004746 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004747 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004748 return NULL;
4749 ++cctx->ctx_lnum;
4750 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4751 // Comment lines result in NULL pointers, skip them.
4752 if (p != NULL)
4753 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004755}
4756
4757/*
4758 * Compile a nested :def command.
4759 */
4760 static char_u *
4761compile_nested_function(exarg_T *eap, cctx_T *cctx)
4762{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004763 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004764 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004765 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004766 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004767 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004768 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004769
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004770 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004771 {
4772 emsg(_(e_cannot_use_bang_with_nested_def));
4773 return NULL;
4774 }
4775
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004776 // Only g:Func() can use a namespace.
4777 if (name_start[1] == ':' && !is_global)
4778 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004779 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004780 return NULL;
4781 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004782 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4783 return NULL;
4784
Bram Moolenaar04b12692020-05-04 23:24:44 +02004785 eap->arg = name_end;
4786 eap->getline = exarg_getline;
4787 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004788 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004789 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004790 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004791 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792
Bram Moolenaar822ba242020-05-24 23:00:18 +02004793 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004794 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004795 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004796 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004797 {
4798 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004799 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004800 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004801
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004802 if (is_global)
4803 {
4804 char_u *func_name = vim_strnsave(name_start + 2,
4805 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004806
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004807 if (func_name == NULL)
4808 r = FAIL;
4809 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004810 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004811 }
4812 else
4813 {
4814 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004815 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004816 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004817 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004818
Bram Moolenaareef21022020-08-01 22:16:43 +02004819 if (lvar == NULL)
4820 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004821 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004822 return NULL;
4823 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004824
4825 // copy over the block scope IDs
4826 if (block_depth > 0)
4827 {
4828 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4829 if (ufunc->uf_block_ids != NULL)
4830 {
4831 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4832 sizeof(int) * block_depth);
4833 ufunc->uf_block_depth = block_depth;
4834 }
4835 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004836 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004837
Bram Moolenaar61a89812020-05-07 16:58:17 +02004838 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004839 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004840}
4841
4842/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 * Return the length of an assignment operator, or zero if there isn't one.
4844 */
4845 int
4846assignment_len(char_u *p, int *heredoc)
4847{
4848 if (*p == '=')
4849 {
4850 if (p[1] == '<' && p[2] == '<')
4851 {
4852 *heredoc = TRUE;
4853 return 3;
4854 }
4855 return 1;
4856 }
4857 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4858 return 2;
4859 if (STRNCMP(p, "..=", 3) == 0)
4860 return 3;
4861 return 0;
4862}
4863
4864// words that cannot be used as a variable
4865static char *reserved[] = {
4866 "true",
4867 "false",
4868 NULL
4869};
4870
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004871typedef enum {
4872 dest_local,
4873 dest_option,
4874 dest_env,
4875 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004876 dest_buffer,
4877 dest_window,
4878 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004879 dest_vimvar,
4880 dest_script,
4881 dest_reg,
4882} assign_dest_T;
4883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004885 * Generate the load instruction for "name".
4886 */
4887 static void
4888generate_loadvar(
4889 cctx_T *cctx,
4890 assign_dest_T dest,
4891 char_u *name,
4892 lvar_T *lvar,
4893 type_T *type)
4894{
4895 switch (dest)
4896 {
4897 case dest_option:
4898 // TODO: check the option exists
4899 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4900 break;
4901 case dest_global:
4902 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4903 break;
4904 case dest_buffer:
4905 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4906 break;
4907 case dest_window:
4908 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4909 break;
4910 case dest_tab:
4911 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4912 break;
4913 case dest_script:
4914 compile_load_scriptvar(cctx,
4915 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4916 break;
4917 case dest_env:
4918 // Include $ in the name here
4919 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4920 break;
4921 case dest_reg:
4922 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4923 break;
4924 case dest_vimvar:
4925 generate_LOADV(cctx, name + 2, TRUE);
4926 break;
4927 case dest_local:
4928 if (lvar->lv_from_outer)
4929 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4930 NULL, type);
4931 else
4932 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4933 break;
4934 }
4935}
4936
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004937 void
4938vim9_declare_error(char_u *name)
4939{
4940 char *scope = "";
4941
4942 switch (*name)
4943 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004944 case 'g': scope = _("global"); break;
4945 case 'b': scope = _("buffer"); break;
4946 case 'w': scope = _("window"); break;
4947 case 't': scope = _("tab"); break;
4948 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004949 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004950 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004951 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004952 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004953 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004954 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004955 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004956 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004957 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004958}
4959
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004960/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004962 * "let name"
4963 * "var name = expr"
4964 * "final name = expr"
4965 * "const name = expr"
4966 * "name = expr"
4967 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 * Return NULL for an error.
4969 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 */
4971 static char_u *
4972compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4973{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004976 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 char_u *ret = NULL;
4978 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004980 int scriptvar_sid = 0;
4981 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004984 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 int oplen = 0;
4987 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004988 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004989 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004992 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4993 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 // Skip over the "var" or "[var, var]" to get to any "=".
4996 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4997 if (p == NULL)
4998 return *arg == '[' ? arg : NULL;
4999
5000 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005002 // TODO: should we allow this, and figure out type inference from list
5003 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005004 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 return NULL;
5006 }
5007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 sp = p;
5009 p = skipwhite(p);
5010 op = p;
5011 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012
5013 if (var_count > 0 && oplen == 0)
5014 // can be something like "[1, 2]->func()"
5015 return arg;
5016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5018 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005019 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005021 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 if (heredoc)
5024 {
5025 list_T *l;
5026 listitem_T *li;
5027
5028 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005029 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005031 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005032 if (l == NULL)
5033 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034
Bram Moolenaar078269b2020-09-21 20:35:55 +02005035 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005037 // Push each line and the create the list.
5038 FOR_ALL_LIST_ITEMS(l, li)
5039 {
5040 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5041 li->li_tv.vval.v_string = NULL;
5042 }
5043 generate_NEWLIST(cctx, l->lv_len);
5044 type = &t_list_string;
5045 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 list_free(l);
5048 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 // for "[var, var] = expr" evaluate the expression here, loop over the
5054 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005055
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 p = skipwhite(op + oplen);
5057 if (compile_expr0(&p, cctx) == FAIL)
5058 return NULL;
5059 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005061 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005063 type_T *stacktype;
5064
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005065 stacktype = stack->ga_len == 0 ? &t_void
5066 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005069 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005070 goto theend;
5071 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005072 if (need_type(stacktype, &t_list_any, -1, cctx,
5073 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005075 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005076 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5077 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 }
5079 }
5080
5081 /*
5082 * Loop over variables in "[var, var] = expr".
5083 * For "var = expr" and "let var: type" this is done only once.
5084 */
5085 if (var_count > 0)
5086 var_start = skipwhite(arg + 1); // skip over the "["
5087 else
5088 var_start = arg;
5089 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5090 {
5091 char_u *var_end = skip_var_one(var_start, FALSE);
5092 size_t varlen;
5093 int new_local = FALSE;
5094 int opt_type;
5095 int opt_flags = 0;
5096 assign_dest_T dest = dest_local;
5097 int vimvaridx = -1;
5098 lvar_T *lvar = NULL;
5099 lvar_T arg_lvar;
5100 int has_type = FALSE;
5101 int has_index = FALSE;
5102 int instr_count = -1;
5103
Bram Moolenaar65821722020-08-02 18:58:54 +02005104 if (*var_start == '@')
5105 p = var_start + 2;
5106 else
5107 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005108 // skip over the leading "&", "&l:", "&g:" and "$"
5109 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005110 p = to_name_end(p, TRUE);
5111 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005112
5113 // "a: type" is declaring variable "a" with a type, not "a:".
5114 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5115 --var_end;
5116 if (is_decl && p == var_start + 2 && p[-1] == ':')
5117 --p;
5118
5119 varlen = p - var_start;
5120 vim_free(name);
5121 name = vim_strnsave(var_start, varlen);
5122 if (name == NULL)
5123 return NULL;
5124 if (!heredoc)
5125 type = &t_any;
5126
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005127 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005129 int declare_error = FALSE;
5130
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 if (*var_start == '&')
5132 {
5133 int cc;
5134 long numval;
5135
5136 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005137 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 emsg(_(e_const_option));
5140 goto theend;
5141 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005142 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 p = var_start;
5144 p = find_option_end(&p, &opt_flags);
5145 if (p == NULL)
5146 {
5147 // cannot happen?
5148 emsg(_(e_letunexp));
5149 goto theend;
5150 }
5151 cc = *p;
5152 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005153 opt_type = get_option_value(skip_option_env_lead(var_start),
5154 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 *p = cc;
5156 if (opt_type == -3)
5157 {
5158 semsg(_(e_unknown_option), var_start);
5159 goto theend;
5160 }
5161 if (opt_type == -2 || opt_type == 0)
5162 type = &t_string;
5163 else
5164 type = &t_number; // both number and boolean option
5165 }
5166 else if (*var_start == '$')
5167 {
5168 dest = dest_env;
5169 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005170 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 }
5172 else if (*var_start == '@')
5173 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005174 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 {
5176 emsg_invreg(var_start[1]);
5177 goto theend;
5178 }
5179 dest = dest_reg;
5180 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005181 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005183 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 {
5185 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005186 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005188 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005189 {
5190 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005191 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005193 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 {
5195 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005196 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005198 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 {
5200 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005201 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005202 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005203 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 {
5205 typval_T *vtv;
5206 int di_flags;
5207
5208 vimvaridx = find_vim_var(name + 2, &di_flags);
5209 if (vimvaridx < 0)
5210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005211 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 goto theend;
5213 }
5214 // We use the current value of "sandbox" here, is that OK?
5215 if (var_check_ro(di_flags, name, FALSE))
5216 goto theend;
5217 dest = dest_vimvar;
5218 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005219 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005220 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 }
5222 else
5223 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005224 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225
5226 for (idx = 0; reserved[idx] != NULL; ++idx)
5227 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005228 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005229 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005230 goto theend;
5231 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232
5233 lvar = lookup_local(var_start, varlen, cctx);
5234 if (lvar == NULL)
5235 {
5236 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005237 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5239 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005240 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 if (is_decl)
5242 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005243 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 goto theend;
5245 }
5246 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005247 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005248 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 if (lvar != NULL)
5250 {
5251 if (is_decl)
5252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005253 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 goto theend;
5255 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005257 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005259 int script_namespace = varlen > 1
5260 && STRNCMP(var_start, "s:", 2) == 0;
5261 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005262 ? script_var_exists(var_start + 2, varlen - 2,
5263 FALSE, cctx)
5264 : script_var_exists(var_start, varlen,
5265 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005266 imported_T *import =
5267 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005268
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005269 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005270 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005271 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5272
5273 if (is_decl)
5274 {
5275 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005276 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005277 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005278 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005279 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005280 name);
5281 goto theend;
5282 }
5283 else if (cctx->ctx_ufunc->uf_script_ctx_version
5284 == SCRIPT_VERSION_VIM9
5285 && script_namespace
5286 && !script_var && import == NULL)
5287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005288 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005289 goto theend;
5290 }
5291
5292 dest = dest_script;
5293
5294 // existing script-local variables should have a type
5295 scriptvar_sid = current_sctx.sc_sid;
5296 if (import != NULL)
5297 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005298 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005299 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005300 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005301 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005302 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005303 {
5304 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5305 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005306 ((svar_T *)si->sn_var_vals.ga_data)
5307 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005308 type = sv->sv_type;
5309 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005310 }
5311 }
5312 else if (name[1] == ':' && name[2] != NUL)
5313 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005314 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005315 goto theend;
5316 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005317 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005318 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005319 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005320 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005321 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005322 else if (check_defined(var_start, varlen, cctx) == FAIL)
5323 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005326
5327 if (declare_error)
5328 {
5329 vim9_declare_error(name);
5330 goto theend;
5331 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005332 }
5333
5334 // handle "a:name" as a name, not index "name" on "a"
5335 if (varlen > 1 || var_start[varlen] != ':')
5336 p = var_end;
5337
5338 if (dest != dest_option)
5339 {
5340 if (is_decl && *p == ':')
5341 {
5342 // parse optional type: "let var: type = expr"
5343 if (!VIM_ISWHITE(p[1]))
5344 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005345 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 goto theend;
5347 }
5348 p = skipwhite(p + 1);
5349 type = parse_type(&p, cctx->ctx_type_list);
5350 has_type = TRUE;
5351 }
5352 else if (lvar != NULL)
5353 type = lvar->lv_type;
5354 }
5355
5356 if (oplen == 3 && !heredoc && dest != dest_global
5357 && type->tt_type != VAR_STRING
5358 && type->tt_type != VAR_ANY)
5359 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005360 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 goto theend;
5362 }
5363
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005364 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 {
5366 if (oplen > 1 && !heredoc)
5367 {
5368 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005369 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 goto theend;
5371 }
5372
5373 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005374 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5375 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 goto theend;
5377 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005378 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 if (lvar == NULL)
5380 goto theend;
5381 new_local = TRUE;
5382 }
5383
5384 member_type = type;
5385 if (var_end > var_start + varlen)
5386 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005387 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005388 if (is_decl)
5389 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005390 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 goto theend;
5392 }
5393
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005394 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 {
5396 has_index = TRUE;
5397 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005398 member_type = &t_any;
5399 else
5400 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 }
5402 else
5403 {
5404 semsg("Not supported yet: %s", var_start);
5405 goto theend;
5406 }
5407 }
5408 else if (lvar == &arg_lvar)
5409 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005410 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411 goto theend;
5412 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005413 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5414 {
5415 semsg(_(e_cannot_assign_to_constant), name);
5416 goto theend;
5417 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005418
5419 if (!heredoc)
5420 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005421 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005422 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005423 if (oplen > 0 && var_count == 0)
5424 {
5425 // skip over the "=" and the expression
5426 p = skipwhite(op + oplen);
5427 compile_expr0(&p, cctx);
5428 }
5429 }
5430 else if (oplen > 0)
5431 {
5432 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005433 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005434
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 // For "var = expr" evaluate the expression.
5436 if (var_count == 0)
5437 {
5438 int r;
5439
5440 // for "+=", "*=", "..=" etc. first load the current value
5441 if (*op != '=')
5442 {
5443 generate_loadvar(cctx, dest, name, lvar, type);
5444
5445 if (has_index)
5446 {
5447 // TODO: get member from list or dict
5448 emsg("Index with operation not supported yet");
5449 goto theend;
5450 }
5451 }
5452
5453 // Compile the expression. Temporarily hide the new local
5454 // variable here, it is not available to this expression.
5455 if (new_local)
5456 --cctx->ctx_locals.ga_len;
5457 instr_count = instr->ga_len;
5458 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005459 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 if (new_local)
5461 ++cctx->ctx_locals.ga_len;
5462 if (r == FAIL)
5463 goto theend;
5464 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005465 else if (semicolon && var_idx == var_count - 1)
5466 {
5467 // For "[var; var] = expr" get the rest of the list
5468 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5469 goto theend;
5470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 else
5472 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005473 // For "[var, var] = expr" get the "var_idx" item from the
5474 // list.
5475 if (generate_GETITEM(cctx, var_idx) == FAIL)
5476 return FAIL;
5477 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005478
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005479 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005480 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005481 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005483 if ((stacktype->tt_type == VAR_FUNC
5484 || stacktype->tt_type == VAR_PARTIAL)
5485 && var_wrong_func_name(name, TRUE))
5486 goto theend;
5487
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005488 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005489 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005490 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005492 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005493 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 }
5495 else
5496 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005497 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 // for a variable that implies &t_any.
5499 if (stacktype == &t_list_empty)
5500 lvar->lv_type = &t_list_any;
5501 else if (stacktype == &t_dict_empty)
5502 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005503 else if (stacktype == &t_unknown)
5504 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005505 else
5506 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005507 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005508 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005509 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005510 {
5511 type_T *use_type = lvar->lv_type;
5512
Bram Moolenaar71abe482020-09-14 22:28:30 +02005513 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005514 if (has_index)
5515 {
5516 use_type = use_type->tt_member;
5517 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005518 // could be indexing "any"
5519 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005520 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005521 if (need_type(stacktype, use_type, -1, cctx,
5522 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005523 goto theend;
5524 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005525 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005526 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005527 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005528 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005530 else if (cmdidx == CMD_final)
5531 {
5532 emsg(_(e_final_requires_a_value));
5533 goto theend;
5534 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005537 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005538 goto theend;
5539 }
5540 else if (!has_type || dest == dest_option)
5541 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005542 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 goto theend;
5544 }
5545 else
5546 {
5547 // variables are always initialized
5548 if (ga_grow(instr, 1) == FAIL)
5549 goto theend;
5550 switch (member_type->tt_type)
5551 {
5552 case VAR_BOOL:
5553 generate_PUSHBOOL(cctx, VVAL_FALSE);
5554 break;
5555 case VAR_FLOAT:
5556#ifdef FEAT_FLOAT
5557 generate_PUSHF(cctx, 0.0);
5558#endif
5559 break;
5560 case VAR_STRING:
5561 generate_PUSHS(cctx, NULL);
5562 break;
5563 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005564 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565 break;
5566 case VAR_FUNC:
5567 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5568 break;
5569 case VAR_LIST:
5570 generate_NEWLIST(cctx, 0);
5571 break;
5572 case VAR_DICT:
5573 generate_NEWDICT(cctx, 0);
5574 break;
5575 case VAR_JOB:
5576 generate_PUSHJOB(cctx, NULL);
5577 break;
5578 case VAR_CHANNEL:
5579 generate_PUSHCHANNEL(cctx, NULL);
5580 break;
5581 case VAR_NUMBER:
5582 case VAR_UNKNOWN:
5583 case VAR_ANY:
5584 case VAR_PARTIAL:
5585 case VAR_VOID:
5586 case VAR_SPECIAL: // cannot happen
5587 generate_PUSHNR(cctx, 0);
5588 break;
5589 }
5590 }
5591 if (var_count == 0)
5592 end = p;
5593 }
5594
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005595 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005596 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005597 break;
5598
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005599 if (oplen > 0 && *op != '=')
5600 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005601 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 type_T *stacktype;
5603
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604 if (*op == '.')
5605 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005606 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005607 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005609 if (
5610#ifdef FEAT_FLOAT
5611 // If variable is float operation with number is OK.
5612 !(expected == &t_float && stacktype == &t_number) &&
5613#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005614 need_type(stacktype, expected, -1, cctx,
5615 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005616 goto theend;
5617
5618 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005619 {
5620 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5621 goto theend;
5622 }
5623 else if (*op == '+')
5624 {
5625 if (generate_add_instr(cctx,
5626 operator_type(member_type, stacktype),
5627 member_type, stacktype) == FAIL)
5628 goto theend;
5629 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005630 else if (generate_two_op(cctx, op) == FAIL)
5631 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005635 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636 int r;
5637
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005638 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005639 if (new_local)
5640 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005641 p = var_start + varlen;
5642 if (*p == '[')
5643 {
5644 p = skipwhite(p + 1);
5645 r = compile_expr0(&p, cctx);
5646 if (r == OK && *skipwhite(p) != ']')
5647 {
5648 // this should not happen
5649 emsg(_(e_missbrac));
5650 r = FAIL;
5651 }
5652 }
5653 else // if (*p == '.')
5654 {
5655 char_u *key_end = to_name_end(p + 1, TRUE);
5656 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5657
5658 r = generate_PUSHS(cctx, key);
5659 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005660 if (new_local)
5661 ++cctx->ctx_locals.ga_len;
5662 if (r == FAIL)
5663 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005664
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005665 if (type == &t_any)
5666 {
5667 type_T *idx_type = ((type_T **)stack->ga_data)[
5668 stack->ga_len - 1];
5669 // Index on variable of unknown type: guess the type from the
5670 // index type: number is dict, otherwise dict.
5671 // TODO: should do the assignment at runtime
5672 if (idx_type->tt_type == VAR_NUMBER)
5673 type = &t_list_any;
5674 else
5675 type = &t_dict_any;
5676 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005677 if (type->tt_type == VAR_DICT
5678 && may_generate_2STRING(-1, cctx) == FAIL)
5679 goto theend;
5680 if (type->tt_type == VAR_LIST
5681 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005682 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005683 {
5684 emsg(_(e_number_exp));
5685 goto theend;
5686 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005687
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688 // Load the dict or list. On the stack we then have:
5689 // - value
5690 // - index
5691 // - variable
5692 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005693
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005694 if (type->tt_type == VAR_LIST)
5695 {
5696 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5697 return FAIL;
5698 }
5699 else if (type->tt_type == VAR_DICT)
5700 {
5701 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5702 return FAIL;
5703 }
5704 else
5705 {
5706 emsg(_(e_listreq));
5707 goto theend;
5708 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005709 }
5710 else
5711 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005712 if (is_decl && cmdidx == CMD_const
5713 && (dest == dest_script || dest == dest_local))
5714 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005715 generate_LOCKCONST(cctx);
5716
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005717 switch (dest)
5718 {
5719 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005720 generate_STOREOPT(cctx, skip_option_env_lead(name),
5721 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005722 break;
5723 case dest_global:
5724 // include g: with the name, easier to execute that way
5725 generate_STORE(cctx, ISN_STOREG, 0, name);
5726 break;
5727 case dest_buffer:
5728 // include b: with the name, easier to execute that way
5729 generate_STORE(cctx, ISN_STOREB, 0, name);
5730 break;
5731 case dest_window:
5732 // include w: with the name, easier to execute that way
5733 generate_STORE(cctx, ISN_STOREW, 0, name);
5734 break;
5735 case dest_tab:
5736 // include t: with the name, easier to execute that way
5737 generate_STORE(cctx, ISN_STORET, 0, name);
5738 break;
5739 case dest_env:
5740 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5741 break;
5742 case dest_reg:
5743 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5744 break;
5745 case dest_vimvar:
5746 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5747 break;
5748 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005749 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005750 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005751 {
5752 char_u *name_s = name;
5753
5754 // Include s: in the name for store_var()
5755 if (name[1] != ':')
5756 {
5757 int len = (int)STRLEN(name) + 3;
5758
5759 name_s = alloc(len);
5760 if (name_s == NULL)
5761 name_s = name;
5762 else
5763 vim_snprintf((char *)name_s, len,
5764 "s:%s", name);
5765 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005766 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5767 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005768 if (name_s != name)
5769 vim_free(name_s);
5770 }
5771 else
5772 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005773 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005774 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005775 break;
5776 case dest_local:
5777 if (lvar != NULL)
5778 {
5779 isn_T *isn = ((isn_T *)instr->ga_data)
5780 + instr->ga_len - 1;
5781
5782 // optimization: turn "var = 123" from ISN_PUSHNR +
5783 // ISN_STORE into ISN_STORENR
5784 if (!lvar->lv_from_outer
5785 && instr->ga_len == instr_count + 1
5786 && isn->isn_type == ISN_PUSHNR)
5787 {
5788 varnumber_T val = isn->isn_arg.number;
5789
5790 isn->isn_type = ISN_STORENR;
5791 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5792 isn->isn_arg.storenr.stnr_val = val;
5793 if (stack->ga_len > 0)
5794 --stack->ga_len;
5795 }
5796 else if (lvar->lv_from_outer)
5797 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005798 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005799 else
5800 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5801 }
5802 break;
5803 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005804 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005805
5806 if (var_idx + 1 < var_count)
5807 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005809
5810 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005811 if (var_count > 0 && !semicolon)
5812 {
5813 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5814 goto theend;
5815 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005816
Bram Moolenaarb2097502020-07-19 17:17:02 +02005817 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818
5819theend:
5820 vim_free(name);
5821 return ret;
5822}
5823
5824/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005825 * Check if "name" can be "unlet".
5826 */
5827 int
5828check_vim9_unlet(char_u *name)
5829{
5830 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5831 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005832 // "unlet s:var" is allowed in legacy script.
5833 if (*name == 's' && !script_is_vim9())
5834 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005835 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005836 return FAIL;
5837 }
5838 return OK;
5839}
5840
5841/*
5842 * Callback passed to ex_unletlock().
5843 */
5844 static int
5845compile_unlet(
5846 lval_T *lvp,
5847 char_u *name_end,
5848 exarg_T *eap,
5849 int deep UNUSED,
5850 void *coookie)
5851{
5852 cctx_T *cctx = coookie;
5853
5854 if (lvp->ll_tv == NULL)
5855 {
5856 char_u *p = lvp->ll_name;
5857 int cc = *name_end;
5858 int ret = OK;
5859
5860 // Normal name. Only supports g:, w:, t: and b: namespaces.
5861 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005862 if (*p == '$')
5863 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5864 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005865 ret = FAIL;
5866 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005867 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005868
5869 *name_end = cc;
5870 return ret;
5871 }
5872
5873 // TODO: unlet {list}[idx]
5874 // TODO: unlet {dict}[key]
5875 emsg("Sorry, :unlet not fully implemented yet");
5876 return FAIL;
5877}
5878
5879/*
5880 * compile "unlet var", "lock var" and "unlock var"
5881 * "arg" points to "var".
5882 */
5883 static char_u *
5884compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5885{
5886 char_u *p = arg;
5887
5888 if (eap->cmdidx != CMD_unlet)
5889 {
5890 emsg("Sorry, :lock and unlock not implemented yet");
5891 return NULL;
5892 }
5893
5894 if (*p == '!')
5895 {
5896 p = skipwhite(p + 1);
5897 eap->forceit = TRUE;
5898 }
5899
5900 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5901 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5902}
5903
5904/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 * Compile an :import command.
5906 */
5907 static char_u *
5908compile_import(char_u *arg, cctx_T *cctx)
5909{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005910 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911}
5912
5913/*
5914 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5915 */
5916 static int
5917compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5918{
5919 garray_T *instr = &cctx->ctx_instr;
5920 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5921
5922 if (endlabel == NULL)
5923 return FAIL;
5924 endlabel->el_next = *el;
5925 *el = endlabel;
5926 endlabel->el_end_label = instr->ga_len;
5927
5928 generate_JUMP(cctx, when, 0);
5929 return OK;
5930}
5931
5932 static void
5933compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5934{
5935 garray_T *instr = &cctx->ctx_instr;
5936
5937 while (*el != NULL)
5938 {
5939 endlabel_T *cur = (*el);
5940 isn_T *isn;
5941
5942 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5943 isn->isn_arg.jump.jump_where = instr->ga_len;
5944 *el = cur->el_next;
5945 vim_free(cur);
5946 }
5947}
5948
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005949 static void
5950compile_free_jump_to_end(endlabel_T **el)
5951{
5952 while (*el != NULL)
5953 {
5954 endlabel_T *cur = (*el);
5955
5956 *el = cur->el_next;
5957 vim_free(cur);
5958 }
5959}
5960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961/*
5962 * Create a new scope and set up the generic items.
5963 */
5964 static scope_T *
5965new_scope(cctx_T *cctx, scopetype_T type)
5966{
5967 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5968
5969 if (scope == NULL)
5970 return NULL;
5971 scope->se_outer = cctx->ctx_scope;
5972 cctx->ctx_scope = scope;
5973 scope->se_type = type;
5974 scope->se_local_count = cctx->ctx_locals.ga_len;
5975 return scope;
5976}
5977
5978/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005979 * Free the current scope and go back to the outer scope.
5980 */
5981 static void
5982drop_scope(cctx_T *cctx)
5983{
5984 scope_T *scope = cctx->ctx_scope;
5985
5986 if (scope == NULL)
5987 {
5988 iemsg("calling drop_scope() without a scope");
5989 return;
5990 }
5991 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005992 switch (scope->se_type)
5993 {
5994 case IF_SCOPE:
5995 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5996 case FOR_SCOPE:
5997 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5998 case WHILE_SCOPE:
5999 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6000 case TRY_SCOPE:
6001 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6002 case NO_SCOPE:
6003 case BLOCK_SCOPE:
6004 break;
6005 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006006 vim_free(scope);
6007}
6008
6009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010 * compile "if expr"
6011 *
6012 * "if expr" Produces instructions:
6013 * EVAL expr Push result of "expr"
6014 * JUMP_IF_FALSE end
6015 * ... body ...
6016 * end:
6017 *
6018 * "if expr | else" Produces instructions:
6019 * EVAL expr Push result of "expr"
6020 * JUMP_IF_FALSE else
6021 * ... body ...
6022 * JUMP_ALWAYS end
6023 * else:
6024 * ... body ...
6025 * end:
6026 *
6027 * "if expr1 | elseif expr2 | else" Produces instructions:
6028 * EVAL expr Push result of "expr"
6029 * JUMP_IF_FALSE elseif
6030 * ... body ...
6031 * JUMP_ALWAYS end
6032 * elseif:
6033 * EVAL expr Push result of "expr"
6034 * JUMP_IF_FALSE else
6035 * ... body ...
6036 * JUMP_ALWAYS end
6037 * else:
6038 * ... body ...
6039 * end:
6040 */
6041 static char_u *
6042compile_if(char_u *arg, cctx_T *cctx)
6043{
6044 char_u *p = arg;
6045 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006046 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006048 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006049 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050
Bram Moolenaara5565e42020-05-09 15:44:01 +02006051 CLEAR_FIELD(ppconst);
6052 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006053 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006054 clear_ppconst(&ppconst);
6055 return NULL;
6056 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006057 if (cctx->ctx_skip == SKIP_YES)
6058 clear_ppconst(&ppconst);
6059 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006060 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006061 int error = FALSE;
6062 int v;
6063
Bram Moolenaara5565e42020-05-09 15:44:01 +02006064 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006065 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006066 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006067 if (error)
6068 return NULL;
6069 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006070 }
6071 else
6072 {
6073 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006074 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006075 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006076 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006077 if (bool_on_stack(cctx) == FAIL)
6078 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 scope = new_scope(cctx, IF_SCOPE);
6082 if (scope == NULL)
6083 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006084 scope->se_skip_save = skip_save;
6085 // "is_had_return" will be reset if any block does not end in :return
6086 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006088 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006089 {
6090 // "where" is set when ":elseif", "else" or ":endif" is found
6091 scope->se_u.se_if.is_if_label = instr->ga_len;
6092 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6093 }
6094 else
6095 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
6097 return p;
6098}
6099
6100 static char_u *
6101compile_elseif(char_u *arg, cctx_T *cctx)
6102{
6103 char_u *p = arg;
6104 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006105 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 isn_T *isn;
6107 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006108 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006109 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110
6111 if (scope == NULL || scope->se_type != IF_SCOPE)
6112 {
6113 emsg(_(e_elseif_without_if));
6114 return NULL;
6115 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006116 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006117 if (!cctx->ctx_had_return)
6118 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006120 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006121 {
6122 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006124 return NULL;
6125 // previous "if" or "elseif" jumps here
6126 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6127 isn->isn_arg.jump.jump_where = instr->ga_len;
6128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006130 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006131 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006132 if (cctx->ctx_skip == SKIP_YES)
6133 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006134 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006135 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006136 clear_ppconst(&ppconst);
6137 return NULL;
6138 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006139 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006140 if (scope->se_skip_save == SKIP_YES)
6141 clear_ppconst(&ppconst);
6142 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006143 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006144 int error = FALSE;
6145 int v;
6146
Bram Moolenaar7f141552020-05-09 17:35:53 +02006147 // The expression results in a constant.
6148 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006149 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6150 if (error)
6151 return NULL;
6152 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006153 clear_ppconst(&ppconst);
6154 scope->se_u.se_if.is_if_label = -1;
6155 }
6156 else
6157 {
6158 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006159 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006160 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006161 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006162 if (bool_on_stack(cctx) == FAIL)
6163 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006165 // "where" is set when ":elseif", "else" or ":endif" is found
6166 scope->se_u.se_if.is_if_label = instr->ga_len;
6167 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169
6170 return p;
6171}
6172
6173 static char_u *
6174compile_else(char_u *arg, cctx_T *cctx)
6175{
6176 char_u *p = arg;
6177 garray_T *instr = &cctx->ctx_instr;
6178 isn_T *isn;
6179 scope_T *scope = cctx->ctx_scope;
6180
6181 if (scope == NULL || scope->se_type != IF_SCOPE)
6182 {
6183 emsg(_(e_else_without_if));
6184 return NULL;
6185 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006186 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006187 if (!cctx->ctx_had_return)
6188 scope->se_u.se_if.is_had_return = FALSE;
6189 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190
Bram Moolenaarefd88552020-06-18 20:50:10 +02006191 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006192 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006193 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006194 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006195 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006196 if (!cctx->ctx_had_return
6197 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6198 JUMP_ALWAYS, cctx) == FAIL)
6199 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006200 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006201
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006202 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006203 {
6204 if (scope->se_u.se_if.is_if_label >= 0)
6205 {
6206 // previous "if" or "elseif" jumps here
6207 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6208 isn->isn_arg.jump.jump_where = instr->ga_len;
6209 scope->se_u.se_if.is_if_label = -1;
6210 }
6211 }
6212
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006213 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006214 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
6217 return p;
6218}
6219
6220 static char_u *
6221compile_endif(char_u *arg, cctx_T *cctx)
6222{
6223 scope_T *scope = cctx->ctx_scope;
6224 ifscope_T *ifscope;
6225 garray_T *instr = &cctx->ctx_instr;
6226 isn_T *isn;
6227
6228 if (scope == NULL || scope->se_type != IF_SCOPE)
6229 {
6230 emsg(_(e_endif_without_if));
6231 return NULL;
6232 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006233 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006234 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006235 if (!cctx->ctx_had_return)
6236 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006238 if (scope->se_u.se_if.is_if_label >= 0)
6239 {
6240 // previous "if" or "elseif" jumps here
6241 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6242 isn->isn_arg.jump.jump_where = instr->ga_len;
6243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 // Fill in the "end" label in jumps at the end of the blocks.
6245 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006246 cctx->ctx_skip = scope->se_skip_save;
6247
6248 // If all the blocks end in :return and there is an :else then the
6249 // had_return flag is set.
6250 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006252 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 return arg;
6254}
6255
6256/*
6257 * compile "for var in expr"
6258 *
6259 * Produces instructions:
6260 * PUSHNR -1
6261 * STORE loop-idx Set index to -1
6262 * EVAL expr Push result of "expr"
6263 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6264 * - if beyond end, jump to "end"
6265 * - otherwise get item from list and push it
6266 * STORE var Store item in "var"
6267 * ... body ...
6268 * JUMP top Jump back to repeat
6269 * end: DROP Drop the result of "expr"
6270 *
6271 */
6272 static char_u *
6273compile_for(char_u *arg, cctx_T *cctx)
6274{
6275 char_u *p;
6276 size_t varlen;
6277 garray_T *instr = &cctx->ctx_instr;
6278 garray_T *stack = &cctx->ctx_type_stack;
6279 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006280 lvar_T *loop_lvar; // loop iteration variable
6281 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 type_T *vartype;
6283
6284 // TODO: list of variables: "for [key, value] in dict"
6285 // parse "var"
6286 for (p = arg; eval_isnamec1(*p); ++p)
6287 ;
6288 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006289 var_lvar = lookup_local(arg, varlen, cctx);
6290 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006292 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 return NULL;
6294 }
6295
6296 // consume "in"
6297 p = skipwhite(p);
6298 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6299 {
6300 emsg(_(e_missing_in));
6301 return NULL;
6302 }
6303 p = skipwhite(p + 2);
6304
6305
6306 scope = new_scope(cctx, FOR_SCOPE);
6307 if (scope == NULL)
6308 return NULL;
6309
6310 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006311 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6312 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006313 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006314 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006315 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318
6319 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006320 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6321 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006322 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006323 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006324 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006328 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 // compile "expr", it remains on the stack until "endfor"
6331 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006332 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006333 {
6334 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006338 // Now that we know the type of "var", check that it is a list, now or at
6339 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006341 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006343 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 return NULL;
6345 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006346 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006347 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
6349 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006350 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006352 generate_FOR(cctx, loop_lvar->lv_idx);
6353 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 return arg;
6356}
6357
6358/*
6359 * compile "endfor"
6360 */
6361 static char_u *
6362compile_endfor(char_u *arg, cctx_T *cctx)
6363{
6364 garray_T *instr = &cctx->ctx_instr;
6365 scope_T *scope = cctx->ctx_scope;
6366 forscope_T *forscope;
6367 isn_T *isn;
6368
6369 if (scope == NULL || scope->se_type != FOR_SCOPE)
6370 {
6371 emsg(_(e_for));
6372 return NULL;
6373 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006374 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006376 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
6378 // At end of ":for" scope jump back to the FOR instruction.
6379 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6380
6381 // Fill in the "end" label in the FOR statement so it can jump here
6382 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6383 isn->isn_arg.forloop.for_end = instr->ga_len;
6384
6385 // Fill in the "end" label any BREAK statements
6386 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6387
6388 // Below the ":for" scope drop the "expr" list from the stack.
6389 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6390 return NULL;
6391
6392 vim_free(scope);
6393
6394 return arg;
6395}
6396
6397/*
6398 * compile "while expr"
6399 *
6400 * Produces instructions:
6401 * top: EVAL expr Push result of "expr"
6402 * JUMP_IF_FALSE end jump if false
6403 * ... body ...
6404 * JUMP top Jump back to repeat
6405 * end:
6406 *
6407 */
6408 static char_u *
6409compile_while(char_u *arg, cctx_T *cctx)
6410{
6411 char_u *p = arg;
6412 garray_T *instr = &cctx->ctx_instr;
6413 scope_T *scope;
6414
6415 scope = new_scope(cctx, WHILE_SCOPE);
6416 if (scope == NULL)
6417 return NULL;
6418
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
6421 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006422 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 return NULL;
6424
Bram Moolenaar13106602020-10-04 16:06:05 +02006425 if (bool_on_stack(cctx) == FAIL)
6426 return FAIL;
6427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006429 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 JUMP_IF_FALSE, cctx) == FAIL)
6431 return FAIL;
6432
6433 return p;
6434}
6435
6436/*
6437 * compile "endwhile"
6438 */
6439 static char_u *
6440compile_endwhile(char_u *arg, cctx_T *cctx)
6441{
6442 scope_T *scope = cctx->ctx_scope;
6443
6444 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6445 {
6446 emsg(_(e_while));
6447 return NULL;
6448 }
6449 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006450 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451
6452 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454
6455 // Fill in the "end" label in the WHILE statement so it can jump here.
6456 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006457 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458
6459 vim_free(scope);
6460
6461 return arg;
6462}
6463
6464/*
6465 * compile "continue"
6466 */
6467 static char_u *
6468compile_continue(char_u *arg, cctx_T *cctx)
6469{
6470 scope_T *scope = cctx->ctx_scope;
6471
6472 for (;;)
6473 {
6474 if (scope == NULL)
6475 {
6476 emsg(_(e_continue));
6477 return NULL;
6478 }
6479 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6480 break;
6481 scope = scope->se_outer;
6482 }
6483
6484 // Jump back to the FOR or WHILE instruction.
6485 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006486 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6487 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 return arg;
6489}
6490
6491/*
6492 * compile "break"
6493 */
6494 static char_u *
6495compile_break(char_u *arg, cctx_T *cctx)
6496{
6497 scope_T *scope = cctx->ctx_scope;
6498 endlabel_T **el;
6499
6500 for (;;)
6501 {
6502 if (scope == NULL)
6503 {
6504 emsg(_(e_break));
6505 return NULL;
6506 }
6507 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6508 break;
6509 scope = scope->se_outer;
6510 }
6511
6512 // Jump to the end of the FOR or WHILE loop.
6513 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006514 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006516 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6518 return FAIL;
6519
6520 return arg;
6521}
6522
6523/*
6524 * compile "{" start of block
6525 */
6526 static char_u *
6527compile_block(char_u *arg, cctx_T *cctx)
6528{
6529 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6530 return NULL;
6531 return skipwhite(arg + 1);
6532}
6533
6534/*
6535 * compile end of block: drop one scope
6536 */
6537 static void
6538compile_endblock(cctx_T *cctx)
6539{
6540 scope_T *scope = cctx->ctx_scope;
6541
6542 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006543 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 vim_free(scope);
6545}
6546
6547/*
6548 * compile "try"
6549 * Creates a new scope for the try-endtry, pointing to the first catch and
6550 * finally.
6551 * Creates another scope for the "try" block itself.
6552 * TRY instruction sets up exception handling at runtime.
6553 *
6554 * "try"
6555 * TRY -> catch1, -> finally push trystack entry
6556 * ... try block
6557 * "throw {exception}"
6558 * EVAL {exception}
6559 * THROW create exception
6560 * ... try block
6561 * " catch {expr}"
6562 * JUMP -> finally
6563 * catch1: PUSH exeception
6564 * EVAL {expr}
6565 * MATCH
6566 * JUMP nomatch -> catch2
6567 * CATCH remove exception
6568 * ... catch block
6569 * " catch"
6570 * JUMP -> finally
6571 * catch2: CATCH remove exception
6572 * ... catch block
6573 * " finally"
6574 * finally:
6575 * ... finally block
6576 * " endtry"
6577 * ENDTRY pop trystack entry, may rethrow
6578 */
6579 static char_u *
6580compile_try(char_u *arg, cctx_T *cctx)
6581{
6582 garray_T *instr = &cctx->ctx_instr;
6583 scope_T *try_scope;
6584 scope_T *scope;
6585
6586 // scope that holds the jumps that go to catch/finally/endtry
6587 try_scope = new_scope(cctx, TRY_SCOPE);
6588 if (try_scope == NULL)
6589 return NULL;
6590
6591 // "catch" is set when the first ":catch" is found.
6592 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006593 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 if (generate_instr(cctx, ISN_TRY) == NULL)
6595 return NULL;
6596
6597 // scope for the try block itself
6598 scope = new_scope(cctx, BLOCK_SCOPE);
6599 if (scope == NULL)
6600 return NULL;
6601
6602 return arg;
6603}
6604
6605/*
6606 * compile "catch {expr}"
6607 */
6608 static char_u *
6609compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6610{
6611 scope_T *scope = cctx->ctx_scope;
6612 garray_T *instr = &cctx->ctx_instr;
6613 char_u *p;
6614 isn_T *isn;
6615
6616 // end block scope from :try or :catch
6617 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6618 compile_endblock(cctx);
6619 scope = cctx->ctx_scope;
6620
6621 // Error if not in a :try scope
6622 if (scope == NULL || scope->se_type != TRY_SCOPE)
6623 {
6624 emsg(_(e_catch));
6625 return NULL;
6626 }
6627
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006630 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 return NULL;
6632 }
6633
6634 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006635 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 JUMP_ALWAYS, cctx) == FAIL)
6637 return NULL;
6638
6639 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006640 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 if (isn->isn_arg.try.try_catch == 0)
6642 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006643 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 {
6645 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006646 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 isn->isn_arg.jump.jump_where = instr->ga_len;
6648 }
6649
6650 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006651 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006653 scope->se_u.se_try.ts_caught_all = TRUE;
6654 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 }
6656 else
6657 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006658 char_u *end;
6659 char_u *pat;
6660 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006661 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006662 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 // Push v:exception, push {expr} and MATCH
6665 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6666
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006667 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006668 if (*end != *p)
6669 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006670 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006671 vim_free(tofree);
6672 return FAIL;
6673 }
6674 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006675 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006676 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006677 len = (int)(end - tofree);
6678 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006679 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006680 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006681 if (pat == NULL)
6682 return FAIL;
6683 if (generate_PUSHS(cctx, pat) == FAIL)
6684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6687 return NULL;
6688
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006689 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6691 return NULL;
6692 }
6693
6694 if (generate_instr(cctx, ISN_CATCH) == NULL)
6695 return NULL;
6696
6697 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6698 return NULL;
6699 return p;
6700}
6701
6702 static char_u *
6703compile_finally(char_u *arg, cctx_T *cctx)
6704{
6705 scope_T *scope = cctx->ctx_scope;
6706 garray_T *instr = &cctx->ctx_instr;
6707 isn_T *isn;
6708
6709 // end block scope from :try or :catch
6710 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6711 compile_endblock(cctx);
6712 scope = cctx->ctx_scope;
6713
6714 // Error if not in a :try scope
6715 if (scope == NULL || scope->se_type != TRY_SCOPE)
6716 {
6717 emsg(_(e_finally));
6718 return NULL;
6719 }
6720
6721 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006722 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 if (isn->isn_arg.try.try_finally != 0)
6724 {
6725 emsg(_(e_finally_dup));
6726 return NULL;
6727 }
6728
6729 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006730 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731
Bram Moolenaar585fea72020-04-02 22:33:21 +02006732 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006733 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 {
6735 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006736 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006738 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 }
6740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 // TODO: set index in ts_finally_label jumps
6742
6743 return arg;
6744}
6745
6746 static char_u *
6747compile_endtry(char_u *arg, cctx_T *cctx)
6748{
6749 scope_T *scope = cctx->ctx_scope;
6750 garray_T *instr = &cctx->ctx_instr;
6751 isn_T *isn;
6752
6753 // end block scope from :catch or :finally
6754 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6755 compile_endblock(cctx);
6756 scope = cctx->ctx_scope;
6757
6758 // Error if not in a :try scope
6759 if (scope == NULL || scope->se_type != TRY_SCOPE)
6760 {
6761 if (scope == NULL)
6762 emsg(_(e_no_endtry));
6763 else if (scope->se_type == WHILE_SCOPE)
6764 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006765 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 emsg(_(e_endfor));
6767 else
6768 emsg(_(e_endif));
6769 return NULL;
6770 }
6771
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006772 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6774 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006775 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 return NULL;
6777 }
6778
6779 // Fill in the "end" label in jumps at the end of the blocks, if not done
6780 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006781 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782
6783 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006784 if (isn->isn_arg.try.try_catch == 0)
6785 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 if (isn->isn_arg.try.try_finally == 0)
6787 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006788
6789 if (scope->se_u.se_try.ts_catch_label != 0)
6790 {
6791 // Last catch without match jumps here
6792 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6793 isn->isn_arg.jump.jump_where = instr->ga_len;
6794 }
6795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 compile_endblock(cctx);
6797
6798 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6799 return NULL;
6800 return arg;
6801}
6802
6803/*
6804 * compile "throw {expr}"
6805 */
6806 static char_u *
6807compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6808{
6809 char_u *p = skipwhite(arg);
6810
Bram Moolenaara5565e42020-05-09 15:44:01 +02006811 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 return NULL;
6813 if (may_generate_2STRING(-1, cctx) == FAIL)
6814 return NULL;
6815 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6816 return NULL;
6817
6818 return p;
6819}
6820
6821/*
6822 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006823 * compile "echomsg expr"
6824 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006825 * compile "execute expr"
6826 */
6827 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006828compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006829{
6830 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006831 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006832 int count = 0;
6833
6834 for (;;)
6835 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006836 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006837 return NULL;
6838 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006839 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006840 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006841 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006842 break;
6843 }
6844
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006845 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6846 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6847 else if (cmdidx == CMD_execute)
6848 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6849 else if (cmdidx == CMD_echomsg)
6850 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6851 else
6852 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 return p;
6854}
6855
6856/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006857 * :put r
6858 * :put ={expr}
6859 */
6860 static char_u *
6861compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6862{
6863 char_u *line = arg;
6864 linenr_T lnum;
6865 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006866 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006867
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006868 eap->regname = *line;
6869
6870 if (eap->regname == '=')
6871 {
6872 char_u *p = line + 1;
6873
6874 if (compile_expr0(&p, cctx) == FAIL)
6875 return NULL;
6876 line = p;
6877 }
6878 else if (eap->regname != NUL)
6879 ++line;
6880
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006881 // "errormsg" will not be set because the range is ADDR_LINES.
6882 // TODO: if the range contains something like "$" or "." need to evaluate
6883 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006884 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6885 return NULL;
6886 if (eap->addr_count == 0)
6887 lnum = -1;
6888 else
6889 lnum = eap->line2;
6890 if (above)
6891 --lnum;
6892
6893 generate_PUT(cctx, eap->regname, lnum);
6894 return line;
6895}
6896
6897/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006898 * A command that is not compiled, execute with legacy code.
6899 */
6900 static char_u *
6901compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6902{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006903 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006904 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006905 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006906
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006907 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006908 goto theend;
6909
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006910 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006911 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006912 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006913 int usefilter = FALSE;
6914
6915 has_expr = argt & (EX_XFILE | EX_EXPAND);
6916
6917 // If the command can be followed by a bar, find the bar and truncate
6918 // it, so that the following command can be compiled.
6919 // The '|' is overwritten with a NUL, it is put back below.
6920 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6921 && *eap->arg == '!')
6922 // :w !filter or :r !filter or :r! filter
6923 usefilter = TRUE;
6924 if ((argt & EX_TRLBAR) && !usefilter)
6925 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006926 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006927 separate_nextcmd(eap);
6928 if (eap->nextcmd != NULL)
6929 nextcmd = eap->nextcmd;
6930 }
6931 }
6932
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006933 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6934 {
6935 // expand filename in "syntax include [@group] filename"
6936 has_expr = TRUE;
6937 eap->arg = skipwhite(eap->arg + 7);
6938 if (*eap->arg == '@')
6939 eap->arg = skiptowhite(eap->arg);
6940 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006941
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006942 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006943 {
6944 int count = 0;
6945 char_u *start = skipwhite(line);
6946
6947 // :cmd xxx`=expr1`yyy`=expr2`zzz
6948 // PUSHS ":cmd xxx"
6949 // eval expr1
6950 // PUSHS "yyy"
6951 // eval expr2
6952 // PUSHS "zzz"
6953 // EXECCONCAT 5
6954 for (;;)
6955 {
6956 if (p > start)
6957 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006958 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006959 ++count;
6960 }
6961 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006962 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006963 return NULL;
6964 may_generate_2STRING(-1, cctx);
6965 ++count;
6966 p = skipwhite(p);
6967 if (*p != '`')
6968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006969 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006970 return NULL;
6971 }
6972 start = p + 1;
6973
6974 p = (char_u *)strstr((char *)start, "`=");
6975 if (p == NULL)
6976 {
6977 if (*skipwhite(start) != NUL)
6978 {
6979 generate_PUSHS(cctx, vim_strsave(start));
6980 ++count;
6981 }
6982 break;
6983 }
6984 }
6985 generate_EXECCONCAT(cctx, count);
6986 }
6987 else
6988 generate_EXEC(cctx, line);
6989
6990theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006991 if (*nextcmd != NUL)
6992 {
6993 // the parser expects a pointer to the bar, put it back
6994 --nextcmd;
6995 *nextcmd = '|';
6996 }
6997
6998 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006999}
7000
7001/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007002 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007003 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007004 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007005 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007006add_def_function(ufunc_T *ufunc)
7007{
7008 dfunc_T *dfunc;
7009
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007010 if (def_functions.ga_len == 0)
7011 {
7012 // The first position is not used, so that a zero uf_dfunc_idx means it
7013 // wasn't set.
7014 if (ga_grow(&def_functions, 1) == FAIL)
7015 return FAIL;
7016 ++def_functions.ga_len;
7017 }
7018
Bram Moolenaar09689a02020-05-09 22:50:08 +02007019 // Add the function to "def_functions".
7020 if (ga_grow(&def_functions, 1) == FAIL)
7021 return FAIL;
7022 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7023 CLEAR_POINTER(dfunc);
7024 dfunc->df_idx = def_functions.ga_len;
7025 ufunc->uf_dfunc_idx = dfunc->df_idx;
7026 dfunc->df_ufunc = ufunc;
7027 ++def_functions.ga_len;
7028 return OK;
7029}
7030
7031/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 * After ex_function() has collected all the function lines: parse and compile
7033 * the lines into instructions.
7034 * Adds the function to "def_functions".
7035 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7036 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007037 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007038 * This can be used recursively through compile_lambda(), which may reallocate
7039 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007040 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007042 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007043compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 char_u *line = NULL;
7046 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 cctx_T cctx;
7049 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007050 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 int ret = FAIL;
7052 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007053 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007054 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007055 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007057 // When using a function that was compiled before: Free old instructions.
7058 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007059 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007061 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7062 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007063 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007065 else
7066 {
7067 if (add_def_function(ufunc) == FAIL)
7068 return FAIL;
7069 new_def_function = TRUE;
7070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071
Bram Moolenaar985116a2020-07-12 17:31:09 +02007072 ufunc->uf_def_status = UF_COMPILING;
7073
Bram Moolenaara80faa82020-04-12 19:37:17 +02007074 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 cctx.ctx_ufunc = ufunc;
7076 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007077 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7079 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7080 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7081 cctx.ctx_type_list = &ufunc->uf_type_list;
7082 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7083 instr = &cctx.ctx_instr;
7084
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007085 // Set the context to the function, it may be compiled when called from
7086 // another script. Set the script version to the most modern one.
7087 // The line number will be set in next_line_from_context().
7088 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7090
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007091 // Make sure error messages are OK.
7092 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7093 if (do_estack_push)
7094 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007095 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007097 if (ufunc->uf_def_args.ga_len > 0)
7098 {
7099 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007100 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007101 int i;
7102 char_u *arg;
7103 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007104 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007105
7106 // Produce instructions for the default values of optional arguments.
7107 // Store the instruction index in uf_def_arg_idx[] so that we know
7108 // where to start when the function is called, depending on the number
7109 // of arguments.
7110 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7111 if (ufunc->uf_def_arg_idx == NULL)
7112 goto erret;
7113 for (i = 0; i < count; ++i)
7114 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007115 garray_T *stack = &cctx.ctx_type_stack;
7116 type_T *val_type;
7117 int arg_idx = first_def_arg + i;
7118
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007119 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7120 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007121 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007122 goto erret;
7123
7124 // If no type specified use the type of the default value.
7125 // Otherwise check that the default value type matches the
7126 // specified type.
7127 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7128 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007129 {
7130 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007131 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007132 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007133 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7134 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007135 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007136
7137 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007138 goto erret;
7139 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007140 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007141
7142 if (did_set_arg_type)
7143 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007144 }
7145
7146 /*
7147 * Loop over all the lines of the function and generate instructions.
7148 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 for (;;)
7150 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007151 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007152 int starts_with_colon = FALSE;
7153 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007154 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007155
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007156 // Bail out on the first error to avoid a flood of errors and report
7157 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007158 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007159 goto erret;
7160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 if (line != NULL && *line == '|')
7162 // the line continues after a '|'
7163 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007164 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007165 && !(*line == '#' && (line == cctx.ctx_line_start
7166 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007168 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 goto erret;
7170 }
7171 else
7172 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007173 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007174 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007175 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 }
7178
Bram Moolenaara80faa82020-04-12 19:37:17 +02007179 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 ea.cmdlinep = &line;
7181 ea.cmd = skipwhite(line);
7182
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007183 // Some things can be recognized by the first character.
7184 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007186 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007187 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007188 if (ea.cmd[1] != '{')
7189 {
7190 line = (char_u *)"";
7191 continue;
7192 }
7193 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007195 case '}':
7196 {
7197 // "}" ends a block scope
7198 scopetype_T stype = cctx.ctx_scope == NULL
7199 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007201 if (stype == BLOCK_SCOPE)
7202 {
7203 compile_endblock(&cctx);
7204 line = ea.cmd;
7205 }
7206 else
7207 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007208 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007209 goto erret;
7210 }
7211 if (line != NULL)
7212 line = skipwhite(ea.cmd + 1);
7213 continue;
7214 }
7215
7216 case '{':
7217 // "{" starts a block scope
7218 // "{'a': 1}->func() is something else
7219 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7220 {
7221 line = compile_block(ea.cmd, &cctx);
7222 continue;
7223 }
7224 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 }
7226
7227 /*
7228 * COMMAND MODIFIERS
7229 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007230 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007231 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7232 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 {
7234 if (errormsg != NULL)
7235 goto erret;
7236 // empty line or comment
7237 line = (char_u *)"";
7238 continue;
7239 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007240 generate_cmdmods(&cctx, &local_cmdmod);
7241 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007243 // Check if there was a colon after the last command modifier or before
7244 // the current position.
7245 for (p = ea.cmd; p >= line; --p)
7246 {
7247 if (*p == ':')
7248 starts_with_colon = TRUE;
7249 if (p < ea.cmd && !VIM_ISWHITE(*p))
7250 break;
7251 }
7252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007254 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007256 {
7257 if (*ea.cmd == '(')
7258 // not for "call()"
7259 ea.cmd = p;
7260 else
7261 ea.cmd = skipwhite(ea.cmd);
7262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007264 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007266 char_u *pskip;
7267
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007268 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007269 // find what follows.
7270 // Skip over "var.member", "var[idx]" and the like.
7271 // Also "&opt = val", "$ENV = val" and "@r = val".
7272 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007273 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007274 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007275 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007277 char_u *var_end;
7278 int oplen;
7279 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280
Bram Moolenaar65821722020-08-02 18:58:54 +02007281 if (ea.cmd[0] == '@')
7282 var_end = ea.cmd + 2;
7283 else
7284 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007285 FNE_CHECK_START | FNE_INCL_BR);
7286 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007287 if (oplen > 0)
7288 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007289 size_t len = p - ea.cmd;
7290
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007291 // Recognize an assignment if we recognize the variable
7292 // name:
7293 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007294 // "local = expr" where "local" is a local var.
7295 // "script = expr" where "script" is a script-local var.
7296 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007297 // "&opt = expr"
7298 // "$ENV = expr"
7299 // "@r = expr"
7300 if (*ea.cmd == '&'
7301 || *ea.cmd == '$'
7302 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007303 || ((len) > 2 && ea.cmd[1] == ':')
7304 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007305 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007306 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007307 || script_var_exists(ea.cmd, len,
7308 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007309 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007310 {
7311 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007312 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007313 goto erret;
7314 continue;
7315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 }
7317 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007318
7319 if (*ea.cmd == '[')
7320 {
7321 // [var, var] = expr
7322 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7323 if (line == NULL)
7324 goto erret;
7325 if (line != ea.cmd)
7326 continue;
7327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 }
7329
7330 /*
7331 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007332 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007334 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007335 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007336 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007337 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007338 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007339 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007340 if (!starts_with_colon)
7341 {
7342 emsg(_(e_colon_required_before_a_range));
7343 goto erret;
7344 }
7345 if (ends_excmd2(line, ea.cmd))
7346 {
7347 // A range without a command: jump to the line.
7348 // TODO: compile to a more efficient command, possibly
7349 // calling parse_cmd_address().
7350 ea.cmdidx = CMD_SIZE;
7351 line = compile_exec(line, &ea, &cctx);
7352 goto nextline;
7353 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007354 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007355 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007356 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007357 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007358 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359
7360 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7361 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007362 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007363 {
7364 line += STRLEN(line);
7365 continue;
7366 }
7367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007369 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007371 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007372 iemsg("Command from find_ex_command() not handled");
7373 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 }
7376
Bram Moolenaar3988f642020-08-27 22:43:03 +02007377 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007378 && ea.cmdidx != CMD_elseif
7379 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007380 && ea.cmdidx != CMD_endif
7381 && ea.cmdidx != CMD_endfor
7382 && ea.cmdidx != CMD_endwhile
7383 && ea.cmdidx != CMD_catch
7384 && ea.cmdidx != CMD_finally
7385 && ea.cmdidx != CMD_endtry)
7386 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007387 emsg(_(e_unreachable_code_after_return));
7388 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007389 }
7390
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007391 p = skipwhite(p);
7392 if (ea.cmdidx != CMD_SIZE
7393 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7394 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007395 if (ea.cmdidx >= 0)
7396 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007397 if ((ea.argt & EX_BANG) && *p == '!')
7398 {
7399 ea.forceit = TRUE;
7400 p = skipwhite(p + 1);
7401 }
7402 }
7403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 switch (ea.cmdidx)
7405 {
7406 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007407 ea.arg = p;
7408 line = compile_nested_function(&ea, &cctx);
7409 break;
7410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007412 // TODO: should we allow this, e.g. to declare a global
7413 // function?
7414 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 goto erret;
7416
7417 case CMD_return:
7418 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007419 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 break;
7421
7422 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007423 emsg(_(e_cannot_use_let_in_vim9_script));
7424 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007425 case CMD_var:
7426 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case CMD_const:
7428 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007429 if (line == p)
7430 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 break;
7432
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007433 case CMD_unlet:
7434 case CMD_unlockvar:
7435 case CMD_lockvar:
7436 line = compile_unletlock(p, &ea, &cctx);
7437 break;
7438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 case CMD_import:
7440 line = compile_import(p, &cctx);
7441 break;
7442
7443 case CMD_if:
7444 line = compile_if(p, &cctx);
7445 break;
7446 case CMD_elseif:
7447 line = compile_elseif(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_else:
7451 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007452 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 break;
7454 case CMD_endif:
7455 line = compile_endif(p, &cctx);
7456 break;
7457
7458 case CMD_while:
7459 line = compile_while(p, &cctx);
7460 break;
7461 case CMD_endwhile:
7462 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007463 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 break;
7465
7466 case CMD_for:
7467 line = compile_for(p, &cctx);
7468 break;
7469 case CMD_endfor:
7470 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007471 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 break;
7473 case CMD_continue:
7474 line = compile_continue(p, &cctx);
7475 break;
7476 case CMD_break:
7477 line = compile_break(p, &cctx);
7478 break;
7479
7480 case CMD_try:
7481 line = compile_try(p, &cctx);
7482 break;
7483 case CMD_catch:
7484 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007485 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 break;
7487 case CMD_finally:
7488 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007489 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490 break;
7491 case CMD_endtry:
7492 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007493 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 break;
7495 case CMD_throw:
7496 line = compile_throw(p, &cctx);
7497 break;
7498
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007499 case CMD_eval:
7500 if (compile_expr0(&p, &cctx) == FAIL)
7501 goto erret;
7502
Bram Moolenaar3988f642020-08-27 22:43:03 +02007503 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007504 generate_instr_drop(&cctx, ISN_DROP, 1);
7505
7506 line = skipwhite(p);
7507 break;
7508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007511 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007512 case CMD_echomsg:
7513 case CMD_echoerr:
7514 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007515 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007517 case CMD_put:
7518 ea.cmd = cmd;
7519 line = compile_put(p, &ea, &cctx);
7520 break;
7521
Bram Moolenaar3988f642020-08-27 22:43:03 +02007522 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007523
Bram Moolenaarae616492020-07-28 20:07:27 +02007524 case CMD_append:
7525 case CMD_change:
7526 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007527 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007528 case CMD_xit:
7529 not_in_vim9(&ea);
7530 goto erret;
7531
Bram Moolenaar002262f2020-07-08 17:47:57 +02007532 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007533 if (cctx.ctx_skip != SKIP_YES)
7534 {
7535 semsg(_(e_invalid_command_str), ea.cmd);
7536 goto erret;
7537 }
7538 // We don't check for a next command here.
7539 line = (char_u *)"";
7540 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007543 if (cctx.ctx_skip == SKIP_YES)
7544 {
7545 // We don't check for a next command here.
7546 line = (char_u *)"";
7547 }
7548 else
7549 {
7550 // Not recognized, execute with do_cmdline_cmd().
7551 ea.arg = p;
7552 line = compile_exec(line, &ea, &cctx);
7553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 break;
7555 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007556nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 if (line == NULL)
7558 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007559 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007561 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007562 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 if (cctx.ctx_type_stack.ga_len < 0)
7565 {
7566 iemsg("Type stack underflow");
7567 goto erret;
7568 }
7569 }
7570
7571 if (cctx.ctx_scope != NULL)
7572 {
7573 if (cctx.ctx_scope->se_type == IF_SCOPE)
7574 emsg(_(e_endif));
7575 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7576 emsg(_(e_endwhile));
7577 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7578 emsg(_(e_endfor));
7579 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007580 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581 goto erret;
7582 }
7583
Bram Moolenaarefd88552020-06-18 20:50:10 +02007584 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 {
7586 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7587 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007588 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 goto erret;
7590 }
7591
7592 // Return zero if there is no return at the end.
7593 generate_PUSHNR(&cctx, 0);
7594 generate_instr(&cctx, ISN_RETURN);
7595 }
7596
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007597 {
7598 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7599 + ufunc->uf_dfunc_idx;
7600 dfunc->df_deleted = FALSE;
7601 dfunc->df_instr = instr->ga_data;
7602 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007603 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007604 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007605 if (cctx.ctx_outer_used)
7606 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007607 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609
7610 ret = OK;
7611
7612erret:
7613 if (ret == FAIL)
7614 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007615 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007616 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7617 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007618
7619 for (idx = 0; idx < instr->ga_len; ++idx)
7620 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007622
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007623 // If using the last entry in the table and it was added above, we
7624 // might as well remove it.
7625 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007626 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007627 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007628 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007629 ufunc->uf_dfunc_idx = 0;
7630 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007631 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007632
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007633 while (cctx.ctx_scope != NULL)
7634 drop_scope(&cctx);
7635
Bram Moolenaar20431c92020-03-20 18:39:46 +01007636 // Don't execute this function body.
7637 ga_clear_strings(&ufunc->uf_lines);
7638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 if (errormsg != NULL)
7640 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007641 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007642 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 }
7644
7645 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007646 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007647 if (do_estack_push)
7648 estack_pop();
7649
Bram Moolenaar20431c92020-03-20 18:39:46 +01007650 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007651 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007653 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654}
7655
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007656 void
7657set_function_type(ufunc_T *ufunc)
7658{
7659 int varargs = ufunc->uf_va_name != NULL;
7660 int argcount = ufunc->uf_args.ga_len;
7661
7662 // Create a type for the function, with the return type and any
7663 // argument types.
7664 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7665 // The type is included in "tt_args".
7666 if (argcount > 0 || varargs)
7667 {
7668 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7669 argcount, &ufunc->uf_type_list);
7670 // Add argument types to the function type.
7671 if (func_type_add_arg_types(ufunc->uf_func_type,
7672 argcount + varargs,
7673 &ufunc->uf_type_list) == FAIL)
7674 return;
7675 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7676 ufunc->uf_func_type->tt_min_argcount =
7677 argcount - ufunc->uf_def_args.ga_len;
7678 if (ufunc->uf_arg_types == NULL)
7679 {
7680 int i;
7681
7682 // lambda does not have argument types.
7683 for (i = 0; i < argcount; ++i)
7684 ufunc->uf_func_type->tt_args[i] = &t_any;
7685 }
7686 else
7687 mch_memmove(ufunc->uf_func_type->tt_args,
7688 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7689 if (varargs)
7690 {
7691 ufunc->uf_func_type->tt_args[argcount] =
7692 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7693 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7694 }
7695 }
7696 else
7697 // No arguments, can use a predefined type.
7698 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7699 argcount, &ufunc->uf_type_list);
7700}
7701
7702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703/*
7704 * Delete an instruction, free what it contains.
7705 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007706 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707delete_instr(isn_T *isn)
7708{
7709 switch (isn->isn_type)
7710 {
7711 case ISN_EXEC:
7712 case ISN_LOADENV:
7713 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007714 case ISN_LOADB:
7715 case ISN_LOADW:
7716 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007718 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 case ISN_PUSHEXC:
7720 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007721 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007723 case ISN_STOREB:
7724 case ISN_STOREW:
7725 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007726 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 vim_free(isn->isn_arg.string);
7728 break;
7729
7730 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007731 case ISN_STORES:
7732 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 break;
7734
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007735 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007736 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007737 vim_free(isn->isn_arg.unlet.ul_name);
7738 break;
7739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 case ISN_STOREOPT:
7741 vim_free(isn->isn_arg.storeopt.so_name);
7742 break;
7743
7744 case ISN_PUSHBLOB: // push blob isn_arg.blob
7745 blob_unref(isn->isn_arg.blob);
7746 break;
7747
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007748 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007749#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007750 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007751#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007752 break;
7753
7754 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007755#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007756 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007757#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007758 break;
7759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 case ISN_UCALL:
7761 vim_free(isn->isn_arg.ufunc.cuf_name);
7762 break;
7763
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007764 case ISN_FUNCREF:
7765 {
7766 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7767 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007768
7769 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7770 func_ptr_unref(dfunc->df_ufunc);
7771 }
7772 break;
7773
7774 case ISN_DCALL:
7775 {
7776 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7777 + isn->isn_arg.dfunc.cdf_idx;
7778
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007779 if (dfunc->df_ufunc != NULL
7780 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007781 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007782 }
7783 break;
7784
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007785 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007786 {
7787 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7788 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7789
7790 if (ufunc != NULL)
7791 {
7792 // Clear uf_dfunc_idx so that the function is deleted.
7793 clear_def_function(ufunc);
7794 ufunc->uf_dfunc_idx = 0;
7795 func_ptr_unref(ufunc);
7796 }
7797
7798 vim_free(lambda);
7799 vim_free(isn->isn_arg.newfunc.nf_global);
7800 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007801 break;
7802
Bram Moolenaar5e654232020-09-16 15:22:00 +02007803 case ISN_CHECKTYPE:
7804 free_type(isn->isn_arg.type.ct_type);
7805 break;
7806
Bram Moolenaar02194d22020-10-24 23:08:38 +02007807 case ISN_CMDMOD:
7808 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7809 ->cmod_filter_regmatch.regprog);
7810 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7811 break;
7812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813 case ISN_2BOOL:
7814 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007815 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 case ISN_ADDBLOB:
7817 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007818 case ISN_ANYINDEX:
7819 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007821 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007823 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007825 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 case ISN_COMPAREANY:
7827 case ISN_COMPAREBLOB:
7828 case ISN_COMPAREBOOL:
7829 case ISN_COMPAREDICT:
7830 case ISN_COMPAREFLOAT:
7831 case ISN_COMPAREFUNC:
7832 case ISN_COMPARELIST:
7833 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 case ISN_COMPARESPECIAL:
7835 case ISN_COMPARESTRING:
7836 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007837 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 case ISN_DROP:
7839 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007840 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007841 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007843 case ISN_EXECCONCAT:
7844 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007846 case ISN_GETITEM:
7847 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007848 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007849 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007850 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007852 case ISN_LOADBDICT:
7853 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007854 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007856 case ISN_LOADSCRIPT:
7857 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007859 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007860 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007861 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 case ISN_NEGATENR:
7863 case ISN_NEWDICT:
7864 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007866 case ISN_OPFLOAT:
7867 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007869 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007870 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 case ISN_PUSHF:
7872 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007874 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007876 case ISN_SHUFFLE:
7877 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007879 case ISN_STOREDICT:
7880 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007881 case ISN_STORENR:
7882 case ISN_STOREOUTER:
7883 case ISN_STOREREG:
7884 case ISN_STORESCRIPT:
7885 case ISN_STOREV:
7886 case ISN_STRINDEX:
7887 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 case ISN_THROW:
7889 case ISN_TRY:
7890 // nothing allocated
7891 break;
7892 }
7893}
7894
7895/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007896 * Free all instructions for "dfunc".
7897 */
7898 static void
7899delete_def_function_contents(dfunc_T *dfunc)
7900{
7901 int idx;
7902
7903 ga_clear(&dfunc->df_def_args_isn);
7904
7905 if (dfunc->df_instr != NULL)
7906 {
7907 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7908 delete_instr(dfunc->df_instr + idx);
7909 VIM_CLEAR(dfunc->df_instr);
7910 }
7911
7912 dfunc->df_deleted = TRUE;
7913}
7914
7915/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007916 * When a user function is deleted, clear the contents of any associated def
7917 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918 */
7919 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007920clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007922 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923 {
7924 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7925 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
Bram Moolenaar20431c92020-03-20 18:39:46 +01007927 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007928 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 }
7930}
7931
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007932/*
7933 * Used when a user function is about to be deleted: remove the pointer to it.
7934 * The entry in def_functions is then unused.
7935 */
7936 void
7937unlink_def_function(ufunc_T *ufunc)
7938{
7939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7940
7941 dfunc->df_ufunc = NULL;
7942}
7943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007945/*
7946 * Free all functions defined with ":def".
7947 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948 void
7949free_def_functions(void)
7950{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007951 int idx;
7952
7953 for (idx = 0; idx < def_functions.ga_len; ++idx)
7954 {
7955 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7956
7957 delete_def_function_contents(dfunc);
7958 }
7959
7960 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961}
7962#endif
7963
7964
7965#endif // FEAT_EVAL