blob: da069f405b6c7cca6e4bff6438ef3a11fd9ce83b [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 Moolenaar6abdcf82020-11-22 18:15:44 +01001436 * Generate an ISN_DEF instruction: list functions
1437 */
1438 static int
1439generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1440{
1441 isn_T *isn;
1442
1443 RETURN_OK_IF_SKIP(cctx);
1444 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1445 return FAIL;
1446 if (len > 0)
1447 {
1448 isn->isn_arg.string = vim_strnsave(name, len);
1449 if (isn->isn_arg.string == NULL)
1450 return FAIL;
1451 }
1452 return OK;
1453}
1454
1455/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 * Generate an ISN_JUMP instruction.
1457 */
1458 static int
1459generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1460{
1461 isn_T *isn;
1462 garray_T *stack = &cctx->ctx_type_stack;
1463
Bram Moolenaar080457c2020-03-03 21:53:32 +01001464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1466 return FAIL;
1467 isn->isn_arg.jump.jump_when = when;
1468 isn->isn_arg.jump.jump_where = where;
1469
1470 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1471 --stack->ga_len;
1472
1473 return OK;
1474}
1475
1476 static int
1477generate_FOR(cctx_T *cctx, int loop_idx)
1478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1484 return FAIL;
1485 isn->isn_arg.forloop.for_idx = loop_idx;
1486
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 // type doesn't matter, will be stored next
1490 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1491 ++stack->ga_len;
1492
1493 return OK;
1494}
1495
1496/*
1497 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001498 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 * Return FAIL if the number of arguments is wrong.
1500 */
1501 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001502generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001506 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001507 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001510 argoff = check_internal_func(func_idx, argcount);
1511 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 return FAIL;
1513
Bram Moolenaar389df252020-07-09 21:20:47 +02001514 if (method_call && argoff > 1)
1515 {
1516 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1517 return FAIL;
1518 isn->isn_arg.shuffle.shfl_item = argcount;
1519 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1520 }
1521
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001522 if (argcount > 0)
1523 {
1524 // Check the types of the arguments.
1525 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1526 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001527 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001528 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1531 return FAIL;
1532 isn->isn_arg.bfunc.cbf_idx = func_idx;
1533 isn->isn_arg.bfunc.cbf_argcount = argcount;
1534
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535 // Drop the argument types and push the return type.
1536 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (ga_grow(stack, 1) == FAIL)
1538 return FAIL;
1539 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001540 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001547 * Generate an ISN_LISTAPPEND instruction. Works like add().
1548 * Argument count is already checked.
1549 */
1550 static int
1551generate_LISTAPPEND(cctx_T *cctx)
1552{
1553 garray_T *stack = &cctx->ctx_type_stack;
1554 type_T *list_type;
1555 type_T *item_type;
1556 type_T *expected;
1557
1558 // Caller already checked that list_type is a list.
1559 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1560 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1561 expected = list_type->tt_member;
1562 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1563 return FAIL;
1564
1565 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1566 return FAIL;
1567
1568 --stack->ga_len; // drop the argument
1569 return OK;
1570}
1571
1572/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001573 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1574 * Argument count is already checked.
1575 */
1576 static int
1577generate_BLOBAPPEND(cctx_T *cctx)
1578{
1579 garray_T *stack = &cctx->ctx_type_stack;
1580 type_T *item_type;
1581
1582 // Caller already checked that blob_type is a blob.
1583 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1584 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1585 return FAIL;
1586
1587 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1588 return FAIL;
1589
1590 --stack->ga_len; // drop the argument
1591 return OK;
1592}
1593
1594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 * Generate an ISN_DCALL or ISN_UCALL instruction.
1596 * Return FAIL if the number of arguments is wrong.
1597 */
1598 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001599generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600{
1601 isn_T *isn;
1602 garray_T *stack = &cctx->ctx_type_stack;
1603 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001604 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if (argcount > regular_args && !has_varargs(ufunc))
1608 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001609 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return FAIL;
1611 }
1612 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1613 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001614 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 return FAIL;
1616 }
1617
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001618 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001619 {
1620 int i;
1621
1622 for (i = 0; i < argcount; ++i)
1623 {
1624 type_T *expected;
1625 type_T *actual;
1626
1627 if (i < regular_args)
1628 {
1629 if (ufunc->uf_arg_types == NULL)
1630 continue;
1631 expected = ufunc->uf_arg_types[i];
1632 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001633 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1634 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001635 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001636 else
1637 expected = ufunc->uf_va_type->tt_member;
1638 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001639 if (need_type(actual, expected, -argcount + i, cctx,
1640 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001641 {
1642 arg_type_mismatch(expected, actual, i + 1);
1643 return FAIL;
1644 }
1645 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001646 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001647 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1648 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001649 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001650 }
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001653 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001654 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001656 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 {
1658 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1659 isn->isn_arg.dfunc.cdf_argcount = argcount;
1660 }
1661 else
1662 {
1663 // A user function may be deleted and redefined later, can't use the
1664 // ufunc pointer, need to look it up again at runtime.
1665 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1666 isn->isn_arg.ufunc.cuf_argcount = argcount;
1667 }
1668
1669 stack->ga_len -= argcount; // drop the arguments
1670 if (ga_grow(stack, 1) == FAIL)
1671 return FAIL;
1672 // add return value
1673 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1674 ++stack->ga_len;
1675
1676 return OK;
1677}
1678
1679/*
1680 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1681 */
1682 static int
1683generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1684{
1685 isn_T *isn;
1686 garray_T *stack = &cctx->ctx_type_stack;
1687
Bram Moolenaar080457c2020-03-03 21:53:32 +01001688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1690 return FAIL;
1691 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1692 isn->isn_arg.ufunc.cuf_argcount = argcount;
1693
1694 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001695 if (ga_grow(stack, 1) == FAIL)
1696 return FAIL;
1697 // add return value
1698 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1699 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700
1701 return OK;
1702}
1703
1704/*
1705 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001706 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 */
1708 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001709generate_PCALL(
1710 cctx_T *cctx,
1711 int argcount,
1712 char_u *name,
1713 type_T *type,
1714 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715{
1716 isn_T *isn;
1717 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001718 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar080457c2020-03-03 21:53:32 +01001720 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001721
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001722 if (type->tt_type == VAR_ANY)
1723 ret_type = &t_any;
1724 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001725 {
1726 if (type->tt_argcount != -1)
1727 {
1728 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1729
1730 if (argcount < type->tt_min_argcount - varargs)
1731 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001732 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001733 return FAIL;
1734 }
1735 if (!varargs && argcount > type->tt_argcount)
1736 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001737 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001738 return FAIL;
1739 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001740 if (type->tt_args != NULL)
1741 {
1742 int i;
1743
1744 for (i = 0; i < argcount; ++i)
1745 {
1746 int offset = -argcount + i - 1;
1747 type_T *actual = ((type_T **)stack->ga_data)[
1748 stack->ga_len + offset];
1749 type_T *expected;
1750
1751 if (varargs && i >= type->tt_min_argcount - 1)
1752 expected = type->tt_args[
1753 type->tt_min_argcount - 1]->tt_member;
1754 else
1755 expected = type->tt_args[i];
1756 if (need_type(actual, expected, offset,
1757 cctx, TRUE, FALSE) == FAIL)
1758 {
1759 arg_type_mismatch(expected, actual, i + 1);
1760 return FAIL;
1761 }
1762 }
1763 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001764 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001765 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001766 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001767 else
1768 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001769 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001770 return FAIL;
1771 }
1772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.pfunc.cpf_top = at_top;
1776 isn->isn_arg.pfunc.cpf_argcount = argcount;
1777
1778 stack->ga_len -= argcount; // drop the arguments
1779
1780 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001781 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001783 // If partial is above the arguments it must be cleared and replaced with
1784 // the return value.
1785 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1786 return FAIL;
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return OK;
1789}
1790
1791/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001792 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 */
1794 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001795generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796{
1797 isn_T *isn;
1798 garray_T *stack = &cctx->ctx_type_stack;
1799 type_T *type;
1800
Bram Moolenaar080457c2020-03-03 21:53:32 +01001801 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001802 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001804 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001806 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001808 if (type->tt_type != VAR_DICT && type != &t_any)
1809 {
1810 emsg(_(e_dictreq));
1811 return FAIL;
1812 }
1813 // change dict type to dict member type
1814 if (type->tt_type == VAR_DICT)
1815 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
1817 return OK;
1818}
1819
1820/*
1821 * Generate an ISN_ECHO instruction.
1822 */
1823 static int
1824generate_ECHO(cctx_T *cctx, int with_white, int count)
1825{
1826 isn_T *isn;
1827
Bram Moolenaar080457c2020-03-03 21:53:32 +01001828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1830 return FAIL;
1831 isn->isn_arg.echo.echo_with_white = with_white;
1832 isn->isn_arg.echo.echo_count = count;
1833
1834 return OK;
1835}
1836
Bram Moolenaarad39c092020-02-26 18:23:43 +01001837/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001838 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001839 */
1840 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001841generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001842{
1843 isn_T *isn;
1844
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001845 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846 return FAIL;
1847 isn->isn_arg.number = count;
1848
1849 return OK;
1850}
1851
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001852/*
1853 * Generate an ISN_PUT instruction.
1854 */
1855 static int
1856generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1857{
1858 isn_T *isn;
1859
1860 RETURN_OK_IF_SKIP(cctx);
1861 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1862 return FAIL;
1863 isn->isn_arg.put.put_regname = regname;
1864 isn->isn_arg.put.put_lnum = lnum;
1865 return OK;
1866}
1867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 static int
1869generate_EXEC(cctx_T *cctx, char_u *line)
1870{
1871 isn_T *isn;
1872
Bram Moolenaar080457c2020-03-03 21:53:32 +01001873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1875 return FAIL;
1876 isn->isn_arg.string = vim_strsave(line);
1877 return OK;
1878}
1879
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001880 static int
1881generate_EXECCONCAT(cctx_T *cctx, int count)
1882{
1883 isn_T *isn;
1884
1885 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.number = count;
1888 return OK;
1889}
1890
Bram Moolenaar792f7862020-11-23 08:31:18 +01001891 static int
1892generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1893{
1894 isn_T *isn;
1895
1896 RETURN_OK_IF_SKIP(cctx);
1897 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1898 return FAIL;
1899 isn->isn_arg.unpack.unp_count = var_count;
1900 isn->isn_arg.unpack.unp_semicolon = semicolon;
1901 return OK;
1902}
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001906 */
1907 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001908generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001909{
1910 isn_T *isn;
1911
Bram Moolenaar02194d22020-10-24 23:08:38 +02001912 if (cmod->cmod_flags != 0
1913 || cmod->cmod_split != 0
1914 || cmod->cmod_verbose != 0
1915 || cmod->cmod_tab != 0
1916 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001917 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001918 cctx->ctx_has_cmdmod = TRUE;
1919
1920 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001921 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001922 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1923 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1924 return FAIL;
1925 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1926 // filter progam now belongs to the instruction
1927 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001928 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001929
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001930 return OK;
1931}
1932
1933 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001934generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001935{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001936 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1937 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001938 return OK;
1939}
1940
1941/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001943 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001945 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001946reserve_local(
1947 cctx_T *cctx,
1948 char_u *name,
1949 size_t len,
1950 int isConst,
1951 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 lvar_T *lvar;
1954
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001955 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001957 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001958 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 }
1960
1961 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001962 return NULL;
1963 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001964 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001966 // Every local variable uses the next entry on the stack. We could re-use
1967 // the last ones when leaving a scope, but then variables used in a closure
1968 // might get overwritten. To keep things simple do not re-use stack
1969 // entries. This is less efficient, but memory is cheap these days.
1970 lvar->lv_idx = cctx->ctx_locals_count++;
1971
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001972 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 lvar->lv_const = isConst;
1974 lvar->lv_type = type;
1975
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001976 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977}
1978
1979/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001980 * Remove local variables above "new_top".
1981 */
1982 static void
1983unwind_locals(cctx_T *cctx, int new_top)
1984{
1985 if (cctx->ctx_locals.ga_len > new_top)
1986 {
1987 int idx;
1988 lvar_T *lvar;
1989
1990 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1991 {
1992 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1993 vim_free(lvar->lv_name);
1994 }
1995 }
1996 cctx->ctx_locals.ga_len = new_top;
1997}
1998
1999/*
2000 * Free all local variables.
2001 */
2002 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002003free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002004{
2005 unwind_locals(cctx, 0);
2006 ga_clear(&cctx->ctx_locals);
2007}
2008
2009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 * Find "name" in script-local items of script "sid".
2011 * Returns the index in "sn_var_vals" if found.
2012 * If found but not in "sn_var_vals" returns -1.
2013 * If not found returns -2.
2014 */
2015 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002016get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017{
2018 hashtab_T *ht;
2019 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002020 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002021 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 int idx;
2023
Bram Moolenaare3d46852020-08-29 13:39:17 +02002024 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002026 if (sid == current_sctx.sc_sid)
2027 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002028 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002029
2030 if (sav == NULL)
2031 return -2;
2032 idx = sav->sav_var_vals_idx;
2033 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2034 if (check_writable && sv->sv_const)
2035 semsg(_(e_readonlyvar), name);
2036 return idx;
2037 }
2038
2039 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ht = &SCRIPT_VARS(sid);
2041 di = find_var_in_ht(ht, 0, name, TRUE);
2042 if (di == NULL)
2043 return -2;
2044
2045 // Now find the svar_T index in sn_var_vals.
2046 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2047 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002048 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 if (sv->sv_tv == &di->di_tv)
2050 {
2051 if (check_writable && sv->sv_const)
2052 semsg(_(e_readonlyvar), name);
2053 return idx;
2054 }
2055 }
2056 return -1;
2057}
2058
2059/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002060 * Find "name" in imported items of the current script or in "cctx" if not
2061 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 */
2063 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002064find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 int idx;
2067
Bram Moolenaare3d46852020-08-29 13:39:17 +02002068 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002069 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 if (cctx != NULL)
2071 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2072 {
2073 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2074 + idx;
2075
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002076 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2077 : STRLEN(import->imp_name) == len
2078 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 return import;
2080 }
2081
Bram Moolenaarefa94442020-08-08 22:16:00 +02002082 return find_imported_in_script(name, len, current_sctx.sc_sid);
2083}
2084
2085 imported_T *
2086find_imported_in_script(char_u *name, size_t len, int sid)
2087{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002088 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002089 int idx;
2090
Bram Moolenaare3d46852020-08-29 13:39:17 +02002091 if (!SCRIPT_ID_VALID(sid))
2092 return NULL;
2093 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2095 {
2096 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2097
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002098 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2099 : STRLEN(import->imp_name) == len
2100 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 return import;
2102 }
2103 return NULL;
2104}
2105
2106/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002107 * Free all imported variables.
2108 */
2109 static void
2110free_imported(cctx_T *cctx)
2111{
2112 int idx;
2113
2114 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2115 {
2116 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2117
2118 vim_free(import->imp_name);
2119 }
2120 ga_clear(&cctx->ctx_imports);
2121}
2122
2123/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002124 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002125 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002126 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002127vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002128{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002129 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002130}
2131
2132/*
2133 * Return a pointer to the next line that isn't empty or only contains a
2134 * comment. Skips over white space.
2135 * Returns NULL if there is none.
2136 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002137 char_u *
2138peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002139{
2140 int lnum = cctx->ctx_lnum;
2141
2142 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2143 {
2144 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002145 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002146
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002147 // ignore NULLs inserted for continuation lines
2148 if (line != NULL)
2149 {
2150 p = skipwhite(line);
2151 if (*p != NUL && !vim9_comment_start(p))
2152 return p;
2153 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154 }
2155 return NULL;
2156}
2157
2158/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002159 * Called when checking for a following operator at "arg". When the rest of
2160 * the line is empty or only a comment, peek the next line. If there is a next
2161 * line return a pointer to it and set "nextp".
2162 * Otherwise skip over white space.
2163 */
2164 static char_u *
2165may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2166{
2167 char_u *p = skipwhite(arg);
2168
2169 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002170 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002171 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002172 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002173 if (*nextp != NULL)
2174 return *nextp;
2175 }
2176 return p;
2177}
2178
2179/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002180 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002181 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002182 * Returns NULL when at the end.
2183 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002184 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002185next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002186{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002187 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002188
2189 do
2190 {
2191 ++cctx->ctx_lnum;
2192 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002193 {
2194 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002195 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002196 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002197 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002198 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002199 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002200 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002201 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002202 return line;
2203}
2204
2205/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002206 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002207 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002208 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2209 */
2210 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002211may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002212{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002213 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002214 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002215 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002216
2217 if (next == NULL)
2218 return FAIL;
2219 *arg = skipwhite(next);
2220 }
2221 return OK;
2222}
2223
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002224/*
2225 * Idem, and give an error when failed.
2226 */
2227 static int
2228may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2229{
2230 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2231 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002232 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002233 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002234 return FAIL;
2235 }
2236 return OK;
2237}
2238
2239
Bram Moolenaara5565e42020-05-09 15:44:01 +02002240// Structure passed between the compile_expr* functions to keep track of
2241// constants that have been parsed but for which no code was produced yet. If
2242// possible expressions on these constants are applied at compile time. If
2243// that is not possible, the code to push the constants needs to be generated
2244// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002245// Using 50 should be more than enough of 5 levels of ().
2246#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002247typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002248 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002249 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002250 int pp_is_const; // all generated code was constants, used for a
2251 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002252} ppconst_T;
2253
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002254static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002255static int compile_expr0(char_u **arg, cctx_T *cctx);
2256static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2257
Bram Moolenaara5565e42020-05-09 15:44:01 +02002258/*
2259 * Generate a PUSH instruction for "tv".
2260 * "tv" will be consumed or cleared.
2261 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2262 */
2263 static int
2264generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2265{
2266 if (tv != NULL)
2267 {
2268 switch (tv->v_type)
2269 {
2270 case VAR_UNKNOWN:
2271 break;
2272 case VAR_BOOL:
2273 generate_PUSHBOOL(cctx, tv->vval.v_number);
2274 break;
2275 case VAR_SPECIAL:
2276 generate_PUSHSPEC(cctx, tv->vval.v_number);
2277 break;
2278 case VAR_NUMBER:
2279 generate_PUSHNR(cctx, tv->vval.v_number);
2280 break;
2281#ifdef FEAT_FLOAT
2282 case VAR_FLOAT:
2283 generate_PUSHF(cctx, tv->vval.v_float);
2284 break;
2285#endif
2286 case VAR_BLOB:
2287 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2288 tv->vval.v_blob = NULL;
2289 break;
2290 case VAR_STRING:
2291 generate_PUSHS(cctx, tv->vval.v_string);
2292 tv->vval.v_string = NULL;
2293 break;
2294 default:
2295 iemsg("constant type not supported");
2296 clear_tv(tv);
2297 return FAIL;
2298 }
2299 tv->v_type = VAR_UNKNOWN;
2300 }
2301 return OK;
2302}
2303
2304/*
2305 * Generate code for any ppconst entries.
2306 */
2307 static int
2308generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2309{
2310 int i;
2311 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002312 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002313
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002314 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002315 for (i = 0; i < ppconst->pp_used; ++i)
2316 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2317 ret = FAIL;
2318 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002319 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002320 return ret;
2321}
2322
2323/*
2324 * Clear ppconst constants. Used when failing.
2325 */
2326 static void
2327clear_ppconst(ppconst_T *ppconst)
2328{
2329 int i;
2330
2331 for (i = 0; i < ppconst->pp_used; ++i)
2332 clear_tv(&ppconst->pp_tv[i]);
2333 ppconst->pp_used = 0;
2334}
2335
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002336/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002337 * Generate an instruction to load script-local variable "name", without the
2338 * leading "s:".
2339 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 */
2341 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002342compile_load_scriptvar(
2343 cctx_T *cctx,
2344 char_u *name, // variable NUL terminated
2345 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002346 char_u **end, // end of variable
2347 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002349 scriptitem_T *si;
2350 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 imported_T *import;
2352
Bram Moolenaare3d46852020-08-29 13:39:17 +02002353 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2354 return FAIL;
2355 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002356 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002357 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002359 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002360 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2361 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 }
2363 if (idx >= 0)
2364 {
2365 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2366
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002367 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 current_sctx.sc_sid, idx, sv->sv_type);
2369 return OK;
2370 }
2371
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002372 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 if (import != NULL)
2374 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002375 if (import->imp_all)
2376 {
2377 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002378 char_u *exp_name;
2379 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002380 ufunc_T *ufunc;
2381 type_T *type;
2382
2383 // Used "import * as Name", need to lookup the member.
2384 if (*p != '.')
2385 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002386 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002387 return FAIL;
2388 }
2389 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002390 if (VIM_ISWHITE(*p))
2391 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002392 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002393 return FAIL;
2394 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002395
Bram Moolenaar1c991142020-07-04 13:15:31 +02002396 // isolate one name
2397 exp_name = p;
2398 while (eval_isnamec(*p))
2399 ++p;
2400 cc = *p;
2401 *p = NUL;
2402
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002403 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002404 *p = cc;
2405 p = skipwhite(p);
2406
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002407 // TODO: what if it is a function?
2408 if (idx < 0)
2409 return FAIL;
2410 *end = p;
2411
2412 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2413 import->imp_sid,
2414 idx,
2415 type);
2416 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002417 else if (import->imp_funcname != NULL)
2418 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002419 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002420 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2421 import->imp_sid,
2422 import->imp_var_vals_idx,
2423 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 return OK;
2425 }
2426
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002427 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002428 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 return FAIL;
2430}
2431
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002432 static int
2433generate_funcref(cctx_T *cctx, char_u *name)
2434{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002435 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002436
2437 if (ufunc == NULL)
2438 return FAIL;
2439
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002440 // Need to compile any default values to get the argument types.
2441 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2442 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2443 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002444 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002445}
2446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447/*
2448 * Compile a variable name into a load instruction.
2449 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002450 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 * When "error" is FALSE do not give an error when not found.
2452 */
2453 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002454compile_load(
2455 char_u **arg,
2456 char_u *end_arg,
2457 cctx_T *cctx,
2458 int is_expr,
2459 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460{
2461 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002462 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002463 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002465 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466
2467 if (*(*arg + 1) == ':')
2468 {
2469 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002470 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002471 {
2472 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002474 switch (**arg)
2475 {
2476 case 'g': isn_type = ISN_LOADGDICT; break;
2477 case 'w': isn_type = ISN_LOADWDICT; break;
2478 case 't': isn_type = ISN_LOADTDICT; break;
2479 case 'b': isn_type = ISN_LOADBDICT; break;
2480 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002481 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002482 goto theend;
2483 }
2484 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2485 goto theend;
2486 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 else
2489 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002490 isntype_T isn_type = ISN_DROP;
2491
2492 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2493 if (name == NULL)
2494 return FAIL;
2495
2496 switch (**arg)
2497 {
2498 case 'v': res = generate_LOADV(cctx, name, error);
2499 break;
2500 case 's': res = compile_load_scriptvar(cctx, name,
2501 NULL, NULL, error);
2502 break;
2503 case 'g': isn_type = ISN_LOADG; break;
2504 case 'w': isn_type = ISN_LOADW; break;
2505 case 't': isn_type = ISN_LOADT; break;
2506 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002507 default: // cannot happen, just in case
2508 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002509 goto theend;
2510 }
2511 if (isn_type != ISN_DROP)
2512 {
2513 // Global, Buffer-local, Window-local and Tabpage-local
2514 // variables can be defined later, thus we don't check if it
2515 // exists, give error at runtime.
2516 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 }
2519 }
2520 else
2521 {
2522 size_t len = end - *arg;
2523 int idx;
2524 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002525 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526
2527 name = vim_strnsave(*arg, end - *arg);
2528 if (name == NULL)
2529 return FAIL;
2530
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002531 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002533 if (!gen_load_outer)
2534 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 }
2536 else
2537 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002538 lvar_T *lvar = lookup_local(*arg, len, cctx);
2539
2540 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002542 type = lvar->lv_type;
2543 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002544 if (lvar->lv_from_outer)
2545 gen_load_outer = TRUE;
2546 else
2547 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 }
2549 else
2550 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002551 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002552 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002553 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002554 || find_imported(name, 0, cctx) != NULL)
2555 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002556
Bram Moolenaar0f769812020-09-12 18:32:34 +02002557 // When evaluating an expression and the name starts with an
2558 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002559 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002560 if (res == FAIL && is_expr
2561 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002562 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564 }
2565 if (gen_load)
2566 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002567 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002568 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002569 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002570 cctx->ctx_outer_used = TRUE;
2571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573
2574 *arg = end;
2575
2576theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002577 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002578 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 vim_free(name);
2580 return res;
2581}
2582
2583/*
2584 * Compile the argument expressions.
2585 * "arg" points to just after the "(" and is advanced to after the ")"
2586 */
2587 static int
2588compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2589{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002590 char_u *p = *arg;
2591 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002592 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593
Bram Moolenaare6085c52020-04-12 20:19:16 +02002594 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002596 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2597 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002598 if (*p == ')')
2599 {
2600 *arg = p + 1;
2601 return OK;
2602 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002603 if (must_end)
2604 {
2605 semsg(_(e_missing_comma_before_argument_str), p);
2606 return FAIL;
2607 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002608
Bram Moolenaara5565e42020-05-09 15:44:01 +02002609 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 return FAIL;
2611 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002612
2613 if (*p != ',' && *skipwhite(p) == ',')
2614 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002615 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002616 p = skipwhite(p);
2617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002619 {
2620 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002621 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002622 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002623 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002624 else
2625 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002626 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002627 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002629failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002630 emsg(_(e_missing_close));
2631 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632}
2633
2634/*
2635 * Compile a function call: name(arg1, arg2)
2636 * "arg" points to "name", "arg + varlen" to the "(".
2637 * "argcount_init" is 1 for "value->method()"
2638 * Instructions:
2639 * EVAL arg1
2640 * EVAL arg2
2641 * BCALL / DCALL / UCALL
2642 */
2643 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002644compile_call(
2645 char_u **arg,
2646 size_t varlen,
2647 cctx_T *cctx,
2648 ppconst_T *ppconst,
2649 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650{
2651 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002652 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 int argcount = argcount_init;
2654 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002655 char_u fname_buf[FLEN_FIXED + 1];
2656 char_u *tofree = NULL;
2657 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002658 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002659 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002660 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661
Bram Moolenaara5565e42020-05-09 15:44:01 +02002662 // we can evaluate "has('name')" at compile time
2663 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2664 {
2665 char_u *s = skipwhite(*arg + varlen + 1);
2666 typval_T argvars[2];
2667
2668 argvars[0].v_type = VAR_UNKNOWN;
2669 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002670 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002671 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002672 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002673 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002674 if (*s == ')' && argvars[0].v_type == VAR_STRING
2675 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002676 {
2677 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2678
2679 *arg = s + 1;
2680 argvars[1].v_type = VAR_UNKNOWN;
2681 tv->v_type = VAR_NUMBER;
2682 tv->vval.v_number = 0;
2683 f_has(argvars, tv);
2684 clear_tv(&argvars[0]);
2685 ++ppconst->pp_used;
2686 return OK;
2687 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002688 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002689 }
2690
2691 if (generate_ppconst(cctx, ppconst) == FAIL)
2692 return FAIL;
2693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 if (varlen >= sizeof(namebuf))
2695 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002696 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 return FAIL;
2698 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002699 vim_strncpy(namebuf, *arg, varlen);
2700 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 *arg = skipwhite(*arg + varlen + 1);
2703 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002704 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
Bram Moolenaara1773442020-08-12 15:21:22 +02002706 is_autoload = vim_strchr(name, '#') != NULL;
2707 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 {
2709 int idx;
2710
2711 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002712 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002714 {
2715 if (STRCMP(name, "add") == 0 && argcount == 2)
2716 {
2717 garray_T *stack = &cctx->ctx_type_stack;
2718 type_T *type = ((type_T **)stack->ga_data)[
2719 stack->ga_len - 2];
2720
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002721 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002722 if (type->tt_type == VAR_LIST)
2723 {
2724 // inline "add(list, item)" so that the type can be checked
2725 res = generate_LISTAPPEND(cctx);
2726 idx = -1;
2727 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002728 else if (type->tt_type == VAR_BLOB)
2729 {
2730 // inline "add(blob, nr)" so that the type can be checked
2731 res = generate_BLOBAPPEND(cctx);
2732 idx = -1;
2733 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002734 }
2735
2736 if (idx >= 0)
2737 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2738 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002739 else
2740 semsg(_(e_unknownfunc), namebuf);
2741 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 }
2743
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002744 // An argument or local variable can be a function reference, this
2745 // overrules a function name.
2746 if (lookup_local(namebuf, varlen, cctx) == NULL
2747 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002748 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002749 // If we can find the function by name generate the right call.
2750 // Skip global functions here, a local funcref takes precedence.
2751 ufunc = find_func(name, FALSE, cctx);
2752 if (ufunc != NULL && !func_is_global(ufunc))
2753 {
2754 res = generate_CALL(cctx, ufunc, argcount);
2755 goto theend;
2756 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758
2759 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002760 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002761 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002763 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002764 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002765 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002766 garray_T *stack = &cctx->ctx_type_stack;
2767 type_T *type;
2768
2769 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2770 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002771 goto theend;
2772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
Bram Moolenaar0f769812020-09-12 18:32:34 +02002774 // If we can find a global function by name generate the right call.
2775 if (ufunc != NULL)
2776 {
2777 res = generate_CALL(cctx, ufunc, argcount);
2778 goto theend;
2779 }
2780
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002781 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002782 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002783 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002784 res = generate_UCALL(cctx, name, argcount);
2785 else
2786 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002787
2788theend:
2789 vim_free(tofree);
2790 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791}
2792
2793// like NAMESPACE_CHAR but with 'a' and 'l'.
2794#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2795
2796/*
2797 * Find the end of a variable or function name. Unlike find_name_end() this
2798 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002799 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 * Return a pointer to just after the name. Equal to "arg" if there is no
2801 * valid name.
2802 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002803 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002804to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805{
2806 char_u *p;
2807
2808 // Quick check for valid starting character.
2809 if (!eval_isnamec1(*arg))
2810 return arg;
2811
2812 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2813 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2814 // and can be used in slice "[n:]".
2815 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002816 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2818 break;
2819 return p;
2820}
2821
2822/*
2823 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002824 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 */
2826 char_u *
2827to_name_const_end(char_u *arg)
2828{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002829 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 typval_T rettv;
2831
2832 if (p == arg && *arg == '[')
2833 {
2834
2835 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002836 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 p = arg;
2838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 return p;
2840}
2841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842/*
2843 * parse a list: [expr, expr]
2844 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002845 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 */
2847 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002848compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849{
2850 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002851 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002853 int is_const;
2854 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002856 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002858 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002859 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002860 semsg(_(e_list_end), *arg);
2861 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002862 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002863 if (*p == ',')
2864 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002865 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002866 return FAIL;
2867 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002868 if (*p == ']')
2869 {
2870 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002872 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002873 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002874 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002875 if (!is_const)
2876 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 ++count;
2878 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002879 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002881 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2882 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002883 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002884 return FAIL;
2885 }
2886 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002887 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 p = skipwhite(p);
2889 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002890 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002892 ppconst->pp_is_const = is_all_const;
2893 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894}
2895
2896/*
2897 * parse a lambda: {arg, arg -> expr}
2898 * "*arg" points to the '{'.
2899 */
2900 static int
2901compile_lambda(char_u **arg, cctx_T *cctx)
2902{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 typval_T rettv;
2904 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002905 evalarg_T evalarg;
2906
2907 CLEAR_FIELD(evalarg);
2908 evalarg.eval_flags = EVAL_EVALUATE;
2909 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910
2911 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002912 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002913 {
2914 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002916 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002919 ++ufunc->uf_refcount;
2920 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
2922 // The function will have one line: "return {expr}".
2923 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002924 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002926 clear_evalarg(&evalarg, NULL);
2927
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002928 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002929 {
2930 // The return type will now be known.
2931 set_function_type(ufunc);
2932
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002933 // The function reference count will be 1. When the ISN_FUNCREF
2934 // instruction is deleted the reference count is decremented and the
2935 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002936 return generate_FUNCREF(cctx, ufunc);
2937 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002938
2939 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 return FAIL;
2941}
2942
2943/*
2944 * Compile a lamda call: expr->{lambda}(args)
2945 * "arg" points to the "{".
2946 */
2947 static int
2948compile_lambda_call(char_u **arg, cctx_T *cctx)
2949{
2950 ufunc_T *ufunc;
2951 typval_T rettv;
2952 int argcount = 1;
2953 int ret = FAIL;
2954
2955 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002956 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 return FAIL;
2958
2959 if (**arg != '(')
2960 {
2961 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002962 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 else
2964 semsg(_(e_missing_paren), "lambda");
2965 clear_tv(&rettv);
2966 return FAIL;
2967 }
2968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 ufunc = rettv.vval.v_partial->pt_func;
2970 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002971 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002972 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002973
Bram Moolenaara05e5242020-09-19 18:19:19 +02002974 // The function will have one line: "return {expr}". Compile it into
2975 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002976 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
2978 // compile the arguments
2979 *arg = skipwhite(*arg + 1);
2980 if (compile_arguments(arg, cctx, &argcount) == OK)
2981 // call the compiled function
2982 ret = generate_CALL(cctx, ufunc, argcount);
2983
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002984 if (ret == FAIL)
2985 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 return ret;
2987}
2988
2989/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002990 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002992 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 */
2994 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01002995compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
2997 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002998 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 int count = 0;
3000 dict_T *d = dict_alloc();
3001 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003002 char_u *whitep = *arg;
3003 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003004 int is_const;
3005 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006
3007 if (d == NULL)
3008 return FAIL;
3009 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003010 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003012 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
Bram Moolenaar23c55272020-06-21 16:58:13 +02003014 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003015 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003016 *arg = NULL;
3017 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003018 }
3019
3020 if (**arg == '}')
3021 break;
3022
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003023 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003025 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003027 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003028 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003029 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3032 if (isn->isn_type == ISN_PUSHS)
3033 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003034 else
3035 {
3036 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003037 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003038 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003039 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003040 return FAIL;
3041 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003042 *arg = skipwhite(*arg);
3043 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003044 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003045 emsg(_(e_missing_matching_bracket_after_dict_key));
3046 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003047 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003048 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003050 else
3051 {
3052 // {"name": value},
3053 // {'name': value},
3054 // {name: value} use "name" as a literal key
3055 key = get_literal_key(arg);
3056 if (key == NULL)
3057 return FAIL;
3058 if (generate_PUSHS(cctx, key) == FAIL)
3059 return FAIL;
3060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061
3062 // Check for duplicate keys, if using string keys.
3063 if (key != NULL)
3064 {
3065 item = dict_find(d, key, -1);
3066 if (item != NULL)
3067 {
3068 semsg(_(e_duplicate_key), key);
3069 goto failret;
3070 }
3071 item = dictitem_alloc(key);
3072 if (item != NULL)
3073 {
3074 item->di_tv.v_type = VAR_UNKNOWN;
3075 item->di_tv.v_lock = 0;
3076 if (dict_add(d, item) == FAIL)
3077 dictitem_free(item);
3078 }
3079 }
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 if (**arg != ':')
3082 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003083 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003084 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003085 else
3086 semsg(_(e_missing_dict_colon), *arg);
3087 return FAIL;
3088 }
3089 whitep = *arg + 1;
3090 if (!IS_WHITE_OR_NUL(*whitep))
3091 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003092 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 return FAIL;
3094 }
3095
3096 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003097 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003098 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003099 *arg = NULL;
3100 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003101 }
3102
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003103 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003105 if (!is_const)
3106 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 ++count;
3108
Bram Moolenaar2c330432020-04-13 14:41:35 +02003109 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003110 *arg = skipwhite(*arg);
3111 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003113 *arg = NULL;
3114 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 if (**arg == '}')
3117 break;
3118 if (**arg != ',')
3119 {
3120 semsg(_(e_missing_dict_comma), *arg);
3121 goto failret;
3122 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003123 if (IS_WHITE_OR_NUL(*whitep))
3124 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003125 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003126 return FAIL;
3127 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003128 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003129 if (!IS_WHITE_OR_NUL(*whitep))
3130 {
3131 semsg(_(e_white_space_required_after_str), ",");
3132 return FAIL;
3133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 *arg = skipwhite(*arg + 1);
3135 }
3136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 *arg = *arg + 1;
3138
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003139 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003140 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003141 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003142 *arg += STRLEN(*arg);
3143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003145 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 return generate_NEWDICT(cctx, count);
3147
3148failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003149 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003150 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003151 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003152 *arg = (char_u *)"";
3153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 dict_unref(d);
3155 return FAIL;
3156}
3157
3158/*
3159 * Compile "&option".
3160 */
3161 static int
3162compile_get_option(char_u **arg, cctx_T *cctx)
3163{
3164 typval_T rettv;
3165 char_u *start = *arg;
3166 int ret;
3167
3168 // parse the option and get the current value to get the type.
3169 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003170 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (ret == OK)
3172 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003173 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 char_u *name = vim_strnsave(start, *arg - start);
3175 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3176
3177 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3178 vim_free(name);
3179 }
3180 clear_tv(&rettv);
3181
3182 return ret;
3183}
3184
3185/*
3186 * Compile "$VAR".
3187 */
3188 static int
3189compile_get_env(char_u **arg, cctx_T *cctx)
3190{
3191 char_u *start = *arg;
3192 int len;
3193 int ret;
3194 char_u *name;
3195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 ++*arg;
3197 len = get_env_len(arg);
3198 if (len == 0)
3199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003200 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 return FAIL;
3202 }
3203
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003204 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 name = vim_strnsave(start, len + 1);
3206 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3207 vim_free(name);
3208 return ret;
3209}
3210
3211/*
3212 * Compile "@r".
3213 */
3214 static int
3215compile_get_register(char_u **arg, cctx_T *cctx)
3216{
3217 int ret;
3218
3219 ++*arg;
3220 if (**arg == NUL)
3221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003222 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 return FAIL;
3224 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003225 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 {
3227 emsg_invreg(**arg);
3228 return FAIL;
3229 }
3230 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3231 ++*arg;
3232 return ret;
3233}
3234
3235/*
3236 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003237 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 */
3239 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003240apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003242 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243
3244 // this works from end to start
3245 while (p > start)
3246 {
3247 --p;
3248 if (*p == '-' || *p == '+')
3249 {
3250 // only '-' has an effect, for '+' we only check the type
3251#ifdef FEAT_FLOAT
3252 if (rettv->v_type == VAR_FLOAT)
3253 {
3254 if (*p == '-')
3255 rettv->vval.v_float = -rettv->vval.v_float;
3256 }
3257 else
3258#endif
3259 {
3260 varnumber_T val;
3261 int error = FALSE;
3262
3263 // tv_get_number_chk() accepts a string, but we don't want that
3264 // here
3265 if (check_not_string(rettv) == FAIL)
3266 return FAIL;
3267 val = tv_get_number_chk(rettv, &error);
3268 clear_tv(rettv);
3269 if (error)
3270 return FAIL;
3271 if (*p == '-')
3272 val = -val;
3273 rettv->v_type = VAR_NUMBER;
3274 rettv->vval.v_number = val;
3275 }
3276 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003277 else if (numeric_only)
3278 {
3279 ++p;
3280 break;
3281 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003282 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 {
3284 int v = tv2bool(rettv);
3285
3286 // '!' is permissive in the type.
3287 clear_tv(rettv);
3288 rettv->v_type = VAR_BOOL;
3289 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3290 }
3291 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003292 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 return OK;
3294}
3295
3296/*
3297 * Recognize v: variables that are constants and set "rettv".
3298 */
3299 static void
3300get_vim_constant(char_u **arg, typval_T *rettv)
3301{
3302 if (STRNCMP(*arg, "v:true", 6) == 0)
3303 {
3304 rettv->v_type = VAR_BOOL;
3305 rettv->vval.v_number = VVAL_TRUE;
3306 *arg += 6;
3307 }
3308 else if (STRNCMP(*arg, "v:false", 7) == 0)
3309 {
3310 rettv->v_type = VAR_BOOL;
3311 rettv->vval.v_number = VVAL_FALSE;
3312 *arg += 7;
3313 }
3314 else if (STRNCMP(*arg, "v:null", 6) == 0)
3315 {
3316 rettv->v_type = VAR_SPECIAL;
3317 rettv->vval.v_number = VVAL_NULL;
3318 *arg += 6;
3319 }
3320 else if (STRNCMP(*arg, "v:none", 6) == 0)
3321 {
3322 rettv->v_type = VAR_SPECIAL;
3323 rettv->vval.v_number = VVAL_NONE;
3324 *arg += 6;
3325 }
3326}
3327
Bram Moolenaar696ba232020-07-29 21:20:41 +02003328 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003329get_compare_type(char_u *p, int *len, int *type_is)
3330{
3331 exptype_T type = EXPR_UNKNOWN;
3332 int i;
3333
3334 switch (p[0])
3335 {
3336 case '=': if (p[1] == '=')
3337 type = EXPR_EQUAL;
3338 else if (p[1] == '~')
3339 type = EXPR_MATCH;
3340 break;
3341 case '!': if (p[1] == '=')
3342 type = EXPR_NEQUAL;
3343 else if (p[1] == '~')
3344 type = EXPR_NOMATCH;
3345 break;
3346 case '>': if (p[1] != '=')
3347 {
3348 type = EXPR_GREATER;
3349 *len = 1;
3350 }
3351 else
3352 type = EXPR_GEQUAL;
3353 break;
3354 case '<': if (p[1] != '=')
3355 {
3356 type = EXPR_SMALLER;
3357 *len = 1;
3358 }
3359 else
3360 type = EXPR_SEQUAL;
3361 break;
3362 case 'i': if (p[1] == 's')
3363 {
3364 // "is" and "isnot"; but not a prefix of a name
3365 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3366 *len = 5;
3367 i = p[*len];
3368 if (!isalnum(i) && i != '_')
3369 {
3370 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3371 *type_is = TRUE;
3372 }
3373 }
3374 break;
3375 }
3376 return type;
3377}
3378
3379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003381 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 */
3383 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003384compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003386 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387
3388 // this works from end to start
3389 while (p > start)
3390 {
3391 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003392 while (VIM_ISWHITE(*p))
3393 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 if (*p == '-' || *p == '+')
3395 {
3396 int negate = *p == '-';
3397 isn_T *isn;
3398
3399 // TODO: check type
3400 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3401 {
3402 --p;
3403 if (*p == '-')
3404 negate = !negate;
3405 }
3406 // only '-' has an effect, for '+' we only check the type
3407 if (negate)
3408 isn = generate_instr(cctx, ISN_NEGATENR);
3409 else
3410 isn = generate_instr(cctx, ISN_CHECKNR);
3411 if (isn == NULL)
3412 return FAIL;
3413 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003414 else if (numeric_only)
3415 {
3416 ++p;
3417 break;
3418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 else
3420 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003421 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003423 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003425 if (p[-1] == '!')
3426 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 }
3429 if (generate_2BOOL(cctx, invert) == FAIL)
3430 return FAIL;
3431 }
3432 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003433 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 return OK;
3435}
3436
3437/*
3438 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003439 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 */
3441 static int
3442compile_subscript(
3443 char_u **arg,
3444 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003445 char_u *start_leader,
3446 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003447 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003449 char_u *name_start = *end_leader;
3450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 for (;;)
3452 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003453 char_u *p = skipwhite(*arg);
3454
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003455 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003456 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003457 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003458
3459 // If a following line starts with "->{" or "->X" advance to that
3460 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003461 // Also if a following line starts with ".x".
3462 if (next != NULL &&
3463 ((next[0] == '-' && next[1] == '>'
3464 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003465 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003466 {
3467 next = next_line_from_context(cctx, TRUE);
3468 if (next == NULL)
3469 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003470 *arg = next;
3471 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003472 }
3473 }
3474
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003475 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3476 // not a function call.
3477 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003479 garray_T *stack = &cctx->ctx_type_stack;
3480 type_T *type;
3481 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003483 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003484 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003485 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003488 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3489
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003490 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3492 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003493 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 return FAIL;
3495 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003496 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003498 char_u *pstart = p;
3499
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003500 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003501 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003502 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 // something->method()
3505 // Apply the '!', '-' and '+' first:
3506 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003507 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003510 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003511 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003512 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 if (**arg == '{')
3514 {
3515 // lambda call: list->{lambda}
3516 if (compile_lambda_call(arg, cctx) == FAIL)
3517 return FAIL;
3518 }
3519 else
3520 {
3521 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003522 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003523 if (!eval_isnamec1(*p))
3524 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003525 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003526 return FAIL;
3527 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003528 if (ASCII_ISALPHA(*p) && p[1] == ':')
3529 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003530 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 ;
3532 if (*p != '(')
3533 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003534 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 return FAIL;
3536 }
3537 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003538 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 return FAIL;
3540 }
3541 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003542 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003544 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003545 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003546 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003547 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003548 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003549
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003550 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003551 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003552 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003553 // TODO: blob index
3554 // TODO: more arguments
3555 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003556 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003557 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003558 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003559
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003560 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003561 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003562 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003563 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003564 if (**arg == ':')
3565 // missing first index is equal to zero
3566 generate_PUSHNR(cctx, 0);
3567 else
3568 {
3569 if (compile_expr0(arg, cctx) == FAIL)
3570 return FAIL;
3571 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3572 return FAIL;
3573 *arg = skipwhite(*arg);
3574 }
3575 if (**arg == ':')
3576 {
3577 *arg = skipwhite(*arg + 1);
3578 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3579 return FAIL;
3580 if (**arg == ']')
3581 // missing second index is equal to end of string
3582 generate_PUSHNR(cctx, -1);
3583 else
3584 {
3585 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003586 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003587 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3588 return FAIL;
3589 *arg = skipwhite(*arg);
3590 }
3591 is_slice = TRUE;
3592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593
3594 if (**arg != ']')
3595 {
3596 emsg(_(e_missbrac));
3597 return FAIL;
3598 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003599 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003601 // We can index a list and a dict. If we don't know the type
3602 // we can use the index value type.
3603 // TODO: If we don't know use an instruction to figure it out at
3604 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003605 typep = ((type_T **)stack->ga_data) + stack->ga_len
3606 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003607 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003608 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3609 // If the index is a string, the variable must be a Dict.
3610 if (*typep == &t_any && valtype == &t_string)
3611 vtype = VAR_DICT;
3612 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003613 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003614 if (need_type(valtype, &t_number, -1, cctx,
3615 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003616 return FAIL;
3617 if (is_slice)
3618 {
3619 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003620 if (need_type(valtype, &t_number, -2, cctx,
3621 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003622 return FAIL;
3623 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003624 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003625
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003626 if (vtype == VAR_DICT)
3627 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003628 if (is_slice)
3629 {
3630 emsg(_(e_cannot_slice_dictionary));
3631 return FAIL;
3632 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003633 if ((*typep)->tt_type == VAR_DICT)
3634 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003635 else
3636 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003637 if (need_type(*typep, &t_dict_any, -2, cctx,
3638 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003639 return FAIL;
3640 *typep = &t_any;
3641 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003642 if (may_generate_2STRING(-1, cctx) == FAIL)
3643 return FAIL;
3644 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3645 return FAIL;
3646 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003647 else if (vtype == VAR_STRING)
3648 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003649 *typep = &t_string;
3650 if ((is_slice
3651 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3652 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003653 return FAIL;
3654 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003655 else if (vtype == VAR_BLOB)
3656 {
3657 emsg("Sorry, blob index and slice not implemented yet");
3658 return FAIL;
3659 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003660 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003661 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003662 if (is_slice)
3663 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003664 if (generate_instr_drop(cctx,
3665 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3666 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003667 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003668 }
Bram Moolenaared591872020-08-15 22:14:53 +02003669 else
3670 {
3671 if ((*typep)->tt_type == VAR_LIST)
3672 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003673 if (generate_instr_drop(cctx,
3674 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3675 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003676 return FAIL;
3677 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003679 else
3680 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003681 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003682 return FAIL;
3683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003685 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003687 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003688 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003690 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003691
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003692 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003693 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003694 {
3695 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003696 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003697 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003698 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003699 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 while (eval_isnamec(*p))
3701 MB_PTR_ADV(p);
3702 if (p == *arg)
3703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003704 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003707 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 return FAIL;
3709 *arg = p;
3710 }
3711 else
3712 break;
3713 }
3714
3715 // TODO - see handle_subscript():
3716 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3717 // Don't do this when "Func" is already a partial that was bound
3718 // explicitly (pt_auto is FALSE).
3719
3720 return OK;
3721}
3722
3723/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003724 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3725 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003727 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003728 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729 *
3730 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 */
3732
3733/*
3734 * number number constant
3735 * 0zFFFFFFFF Blob constant
3736 * "string" string constant
3737 * 'string' literal string constant
3738 * &option-name option value
3739 * @r register contents
3740 * identifier variable value
3741 * function() function call
3742 * $VAR environment variable
3743 * (expression) nested expression
3744 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003745 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 *
3747 * Also handle:
3748 * ! in front logical NOT
3749 * - in front unary minus
3750 * + in front unary plus (ignored)
3751 * trailing (arg) funcref/partial call
3752 * trailing [] subscript in String or List
3753 * trailing .name entry in Dictionary
3754 * trailing ->name() method call
3755 */
3756 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003757compile_expr7(
3758 char_u **arg,
3759 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 char_u *start_leader, *end_leader;
3763 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003764 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003765 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003767 ppconst->pp_is_const = FALSE;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 /*
3770 * Skip '!', '-' and '+' characters. They are handled later.
3771 */
3772 start_leader = *arg;
3773 while (**arg == '!' || **arg == '-' || **arg == '+')
3774 *arg = skipwhite(*arg + 1);
3775 end_leader = *arg;
3776
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003777 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 switch (**arg)
3779 {
3780 /*
3781 * Number constant.
3782 */
3783 case '0': // also for blob starting with 0z
3784 case '1':
3785 case '2':
3786 case '3':
3787 case '4':
3788 case '5':
3789 case '6':
3790 case '7':
3791 case '8':
3792 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003793 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003795 // Apply "-" and "+" just before the number now, right to
3796 // left. Matters especially when "->" follows. Stops at
3797 // '!'.
3798 if (apply_leader(rettv, TRUE,
3799 start_leader, &end_leader) == FAIL)
3800 {
3801 clear_tv(rettv);
3802 return FAIL;
3803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 break;
3805
3806 /*
3807 * String constant: "string".
3808 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003809 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 return FAIL;
3811 break;
3812
3813 /*
3814 * Literal string constant: 'str''ing'.
3815 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003816 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818 break;
3819
3820 /*
3821 * Constant Vim variable.
3822 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003823 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 ret = NOTDONE;
3825 break;
3826
3827 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003828 * "true" constant
3829 */
3830 case 't': if (STRNCMP(*arg, "true", 4) == 0
3831 && !eval_isnamec((*arg)[4]))
3832 {
3833 *arg += 4;
3834 rettv->v_type = VAR_BOOL;
3835 rettv->vval.v_number = VVAL_TRUE;
3836 }
3837 else
3838 ret = NOTDONE;
3839 break;
3840
3841 /*
3842 * "false" constant
3843 */
3844 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3845 && !eval_isnamec((*arg)[5]))
3846 {
3847 *arg += 5;
3848 rettv->v_type = VAR_BOOL;
3849 rettv->vval.v_number = VVAL_FALSE;
3850 }
3851 else
3852 ret = NOTDONE;
3853 break;
3854
3855 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 * List: [expr, expr]
3857 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003858 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 break;
3860
3861 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 * Lambda: {arg, arg -> expr}
3863 * Dictionary: {'key': val, 'key': val}
3864 */
3865 case '{': {
3866 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003867 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868
3869 // Find out what comes after the arguments.
3870 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003871 &ga_arg, TRUE, NULL, NULL,
3872 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 if (ret != FAIL && *start == '>')
3874 ret = compile_lambda(arg, cctx);
3875 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003876 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 }
3878 break;
3879
3880 /*
3881 * Option value: &name
3882 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003883 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
3885 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 break;
3887
3888 /*
3889 * Environment variable: $VAR.
3890 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003891 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3892 return FAIL;
3893 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 break;
3895
3896 /*
3897 * Register contents: @r.
3898 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003899 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3900 return FAIL;
3901 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 break;
3903 /*
3904 * nested expression: (expression).
3905 */
3906 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003907
3908 // recursive!
3909 if (ppconst->pp_used <= PPSIZE - 10)
3910 {
3911 ret = compile_expr1(arg, cctx, ppconst);
3912 }
3913 else
3914 {
3915 // Not enough space in ppconst, flush constants.
3916 if (generate_ppconst(cctx, ppconst) == FAIL)
3917 return FAIL;
3918 ret = compile_expr0(arg, cctx);
3919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 *arg = skipwhite(*arg);
3921 if (**arg == ')')
3922 ++*arg;
3923 else if (ret == OK)
3924 {
3925 emsg(_(e_missing_close));
3926 ret = FAIL;
3927 }
3928 break;
3929
3930 default: ret = NOTDONE;
3931 break;
3932 }
3933 if (ret == FAIL)
3934 return FAIL;
3935
Bram Moolenaar1c747212020-05-09 18:28:34 +02003936 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003938 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003939 clear_tv(rettv);
3940 else
3941 // A constant expression can possibly be handled compile time,
3942 // return the value instead of generating code.
3943 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
3945 else if (ret == NOTDONE)
3946 {
3947 char_u *p;
3948 int r;
3949
3950 if (!eval_isnamec1(**arg))
3951 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003952 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 return FAIL;
3954 }
3955
3956 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003957 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 {
3960 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003963 {
3964 if (generate_ppconst(cctx, ppconst) == FAIL)
3965 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003966 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 if (r == FAIL)
3969 return FAIL;
3970 }
3971
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003972 // Handle following "[]", ".member", etc.
3973 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003974 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003975 ppconst) == FAIL)
3976 return FAIL;
3977 if (ppconst->pp_used > 0)
3978 {
3979 // apply the '!', '-' and '+' before the constant
3980 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003981 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003982 return FAIL;
3983 return OK;
3984 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003985 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003987 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988}
3989
3990/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003991 * Give the "white on both sides" error, taking the operator from "p[len]".
3992 */
3993 void
3994error_white_both(char_u *op, int len)
3995{
3996 char_u buf[10];
3997
3998 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003999 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004000}
4001
4002/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004003 * <type>expr7: runtime type check / conversion
4004 */
4005 static int
4006compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4007{
4008 type_T *want_type = NULL;
4009
4010 // Recognize <type>
4011 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4012 {
4013 int called_emsg_before = called_emsg;
4014
4015 ++*arg;
4016 want_type = parse_type(arg, cctx->ctx_type_list);
4017 if (called_emsg != called_emsg_before)
4018 return FAIL;
4019
4020 if (**arg != '>')
4021 {
4022 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004023 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004024 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004025 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004026 return FAIL;
4027 }
4028 ++*arg;
4029 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4030 return FAIL;
4031 }
4032
4033 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4034 return FAIL;
4035
4036 if (want_type != NULL)
4037 {
4038 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004039 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004040
Bram Moolenaard1103582020-08-14 22:44:25 +02004041 generate_ppconst(cctx, ppconst);
4042 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004043 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004044 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004045 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004046 return FAIL;
4047 }
4048 }
4049
4050 return OK;
4051}
4052
4053/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 * * number multiplication
4055 * / number division
4056 * % number modulo
4057 */
4058 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060{
4061 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004062 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004063 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004065 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004066 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068
4069 /*
4070 * Repeat computing, until no "*", "/" or "%" is following.
4071 */
4072 for (;;)
4073 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004074 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 if (*op != '*' && *op != '/' && *op != '%')
4076 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004077 if (next != NULL)
4078 {
4079 *arg = next_line_from_context(cctx, TRUE);
4080 op = skipwhite(*arg);
4081 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004083 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004085 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004086 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004089 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004090 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004092 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004093 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004095
4096 if (ppconst->pp_used == ppconst_used + 2
4097 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4098 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004100 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4101 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004104 // both are numbers: compute the result
4105 switch (*op)
4106 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004107 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004108 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004109 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004110 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004111 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112 break;
4113 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004114 tv1->vval.v_number = res;
4115 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 }
4117 else
4118 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004119 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004120 generate_two_op(cctx, op);
4121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * + number addition
4129 * - number subtraction
4130 * .. string concatenation
4131 */
4132 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134{
4135 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004136 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
4140 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 return FAIL;
4143
4144 /*
4145 * Repeat computing, until no "+", "-" or ".." is following.
4146 */
4147 for (;;)
4148 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004149 op = may_peek_next_line(cctx, *arg, &next);
4150 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 break;
4152 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004153 if (next != NULL)
4154 {
4155 *arg = next_line_from_context(cctx, TRUE);
4156 op = skipwhite(*arg);
4157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004159 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004161 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004162 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 }
4164
4165 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004166 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004167 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004169 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004170 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 return FAIL;
4172
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004174 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004175 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4176 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4177 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4178 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4181 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004182
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 // concat/subtract/add constant numbers
4184 if (*op == '+')
4185 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4186 else if (*op == '-')
4187 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4188 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004190 // concatenate constant strings
4191 char_u *s1 = tv1->vval.v_string;
4192 char_u *s2 = tv2->vval.v_string;
4193 size_t len1 = STRLEN(s1);
4194
4195 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4196 if (tv1->vval.v_string == NULL)
4197 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004198 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 return FAIL;
4200 }
4201 mch_memmove(tv1->vval.v_string, s1, len1);
4202 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004203 vim_free(s1);
4204 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004205 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 }
4208 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004211 if (*op == '.')
4212 {
4213 if (may_generate_2STRING(-2, cctx) == FAIL
4214 || may_generate_2STRING(-1, cctx) == FAIL)
4215 return FAIL;
4216 generate_instr_drop(cctx, ISN_CONCAT, 1);
4217 }
4218 else
4219 generate_two_op(cctx, op);
4220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 }
4222
4223 return OK;
4224}
4225
4226/*
4227 * expr5a == expr5b
4228 * expr5a =~ expr5b
4229 * expr5a != expr5b
4230 * expr5a !~ expr5b
4231 * expr5a > expr5b
4232 * expr5a >= expr5b
4233 * expr5a < expr5b
4234 * expr5a <= expr5b
4235 * expr5a is expr5b
4236 * expr5a isnot expr5b
4237 *
4238 * Produces instructions:
4239 * EVAL expr5a Push result of "expr5a"
4240 * EVAL expr5b Push result of "expr5b"
4241 * COMPARE one of the compare instructions
4242 */
4243 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245{
4246 exptype_T type = EXPR_UNKNOWN;
4247 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004248 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252
4253 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 return FAIL;
4256
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004257 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004258 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
4260 /*
4261 * If there is a comparative operator, use it.
4262 */
4263 if (type != EXPR_UNKNOWN)
4264 {
4265 int ic = FALSE; // Default: do not ignore case
4266
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004267 if (next != NULL)
4268 {
4269 *arg = next_line_from_context(cctx, TRUE);
4270 p = skipwhite(*arg);
4271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 if (type_is && (p[len] == '?' || p[len] == '#'))
4273 {
4274 semsg(_(e_invexpr2), *arg);
4275 return FAIL;
4276 }
4277 // extra question mark appended: ignore case
4278 if (p[len] == '?')
4279 {
4280 ic = TRUE;
4281 ++len;
4282 }
4283 // extra '#' appended: match case (ignored)
4284 else if (p[len] == '#')
4285 ++len;
4286 // nothing appended: match case
4287
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004288 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004290 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004291 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293
4294 // get the second variable
4295 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004296 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004297 return FAIL;
4298
Bram Moolenaara5565e42020-05-09 15:44:01 +02004299 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 return FAIL;
4301
Bram Moolenaara5565e42020-05-09 15:44:01 +02004302 if (ppconst->pp_used == ppconst_used + 2)
4303 {
4304 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4305 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4306 int ret;
4307
4308 // Both sides are a constant, compute the result now.
4309 // First check for a valid combination of types, this is more
4310 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004311 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312 ret = FAIL;
4313 else
4314 {
4315 ret = typval_compare(tv1, tv2, type, ic);
4316 tv1->v_type = VAR_BOOL;
4317 tv1->vval.v_number = tv1->vval.v_number
4318 ? VVAL_TRUE : VVAL_FALSE;
4319 clear_tv(tv2);
4320 --ppconst->pp_used;
4321 }
4322 return ret;
4323 }
4324
4325 generate_ppconst(cctx, ppconst);
4326 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
4328
4329 return OK;
4330}
4331
Bram Moolenaar7f141552020-05-09 17:35:53 +02004332static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334/*
4335 * Compile || or &&.
4336 */
4337 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338compile_and_or(
4339 char_u **arg,
4340 cctx_T *cctx,
4341 char *op,
4342 ppconst_T *ppconst,
4343 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004345 char_u *next;
4346 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 int opchar = *op;
4348
4349 if (p[0] == opchar && p[1] == opchar)
4350 {
4351 garray_T *instr = &cctx->ctx_instr;
4352 garray_T end_ga;
4353
4354 /*
4355 * Repeat until there is no following "||" or "&&"
4356 */
4357 ga_init2(&end_ga, sizeof(int), 10);
4358 while (p[0] == opchar && p[1] == opchar)
4359 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004360 if (next != NULL)
4361 {
4362 *arg = next_line_from_context(cctx, TRUE);
4363 p = skipwhite(*arg);
4364 }
4365
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004366 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4367 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004368 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004369 return FAIL;
4370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004372 // TODO: use ppconst if the value is a constant and check
4373 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 generate_ppconst(cctx, ppconst);
4375
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004376 // Every part must evaluate to a bool.
4377 if (bool_on_stack(cctx) == FAIL)
4378 {
4379 ga_clear(&end_ga);
4380 return FAIL;
4381 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 if (ga_grow(&end_ga, 1) == FAIL)
4384 {
4385 ga_clear(&end_ga);
4386 return FAIL;
4387 }
4388 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4389 ++end_ga.ga_len;
4390 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004391 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 // eval the next expression
4394 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004395 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004396 {
4397 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004398 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004399 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004400
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4402 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 {
4404 ga_clear(&end_ga);
4405 return FAIL;
4406 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004407
4408 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004412 // Every part must evaluate to a bool.
4413 if (bool_on_stack(cctx) == FAIL)
4414 {
4415 ga_clear(&end_ga);
4416 return FAIL;
4417 }
4418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 // Fill in the end label in all jumps.
4420 while (end_ga.ga_len > 0)
4421 {
4422 isn_T *isn;
4423
4424 --end_ga.ga_len;
4425 isn = ((isn_T *)instr->ga_data)
4426 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4427 isn->isn_arg.jump.jump_where = instr->ga_len;
4428 }
4429 ga_clear(&end_ga);
4430 }
4431
4432 return OK;
4433}
4434
4435/*
4436 * expr4a && expr4a && expr4a logical AND
4437 *
4438 * Produces instructions:
4439 * EVAL expr4a Push result of "expr4a"
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_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004443 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 * EVAL expr4c Push result of "expr4c"
4445 * end:
4446 */
4447 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448compile_expr3(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 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 if (compile_expr4(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 * expr3a || expr3b || expr3c logical OR
4462 *
4463 * Produces instructions:
4464 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004465 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004466 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004468 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 * EVAL expr3c Push result of "expr3c"
4470 * end:
4471 */
4472 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004475 int ppconst_used = ppconst->pp_used;
4476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 return FAIL;
4480
4481 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004482 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483}
4484
4485/*
4486 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004488 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 * JUMP_IF_FALSE alt jump if false
4490 * EVAL expr1a
4491 * JUMP_ALWAYS end
4492 * alt: EVAL expr1b
4493 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004494 *
4495 * Toplevel expression: expr2 ?? expr1
4496 * Produces instructions:
4497 * EVAL expr2 Push result of "expr2"
4498 * JUMP_AND_KEEP_IF_TRUE end jump if true
4499 * EVAL expr1
4500 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 */
4502 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504{
4505 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004507 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaar3988f642020-08-27 22:43:03 +02004509 // Ignore all kinds of errors when not producing code.
4510 if (cctx->ctx_skip == SKIP_YES)
4511 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004512 evalarg_T evalarg;
4513
4514 CLEAR_FIELD(evalarg);
4515 evalarg.eval_cctx = cctx;
4516 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004517 return OK;
4518 }
4519
Bram Moolenaar61a89812020-05-07 16:58:17 +02004520 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 return FAIL;
4523
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004524 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 if (*p == '?')
4526 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004527 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 garray_T *instr = &cctx->ctx_instr;
4529 garray_T *stack = &cctx->ctx_type_stack;
4530 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004531 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004533 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 int has_const_expr = FALSE;
4535 int const_value = FALSE;
4536 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004538 if (next != NULL)
4539 {
4540 *arg = next_line_from_context(cctx, TRUE);
4541 p = skipwhite(*arg);
4542 }
4543
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004544 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004545 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004546 semsg(_(e_white_space_required_before_and_after_str),
4547 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004548 return FAIL;
4549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551 if (ppconst->pp_used == ppconst_used + 1)
4552 {
4553 // the condition is a constant, we know whether the ? or the :
4554 // expression is to be evaluated.
4555 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004556 if (op_falsy)
4557 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4558 else
4559 {
4560 int error = FALSE;
4561
4562 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4563 &error);
4564 if (error)
4565 return FAIL;
4566 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004567 cctx->ctx_skip = save_skip == SKIP_YES ||
4568 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4569
4570 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4571 // "left ?? right" and "left" is truthy: produce "left"
4572 generate_ppconst(cctx, ppconst);
4573 else
4574 {
4575 clear_tv(&ppconst->pp_tv[ppconst_used]);
4576 --ppconst->pp_used;
4577 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 }
4579 else
4580 {
4581 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004582 if (op_falsy)
4583 end_idx = instr->ga_len;
4584 generate_JUMP(cctx, op_falsy
4585 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4586 if (op_falsy)
4587 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
4590 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004591 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004592 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004593 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 if (!has_const_expr)
4598 {
4599 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004601 if (!op_falsy)
4602 {
4603 // remember the type and drop it
4604 --stack->ga_len;
4605 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004607 end_idx = instr->ga_len;
4608 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004610 // jump here from JUMP_IF_FALSE
4611 isn = ((isn_T *)instr->ga_data) + alt_idx;
4612 isn->isn_arg.jump.jump_where = instr->ga_len;
4613 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004616 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004618 // Check for the ":".
4619 p = may_peek_next_line(cctx, *arg, &next);
4620 if (*p != ':')
4621 {
4622 emsg(_(e_missing_colon));
4623 return FAIL;
4624 }
4625 if (next != NULL)
4626 {
4627 *arg = next_line_from_context(cctx, TRUE);
4628 p = skipwhite(*arg);
4629 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004630
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004631 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4632 {
4633 semsg(_(e_white_space_required_before_and_after_str), ":");
4634 return FAIL;
4635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004637 // evaluate the third expression
4638 if (has_const_expr)
4639 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004640 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004641 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004642 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004643 return FAIL;
4644 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4645 return FAIL;
4646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 if (!has_const_expr)
4649 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004650 type_T **typep;
4651
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004655 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4656 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004658 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004659 isn = ((isn_T *)instr->ga_data) + end_idx;
4660 isn->isn_arg.jump.jump_where = instr->ga_len;
4661 }
4662
4663 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 }
4665 return OK;
4666}
4667
4668/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004670 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4671 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 */
4673 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004674compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675{
4676 ppconst_T ppconst;
4677
4678 CLEAR_FIELD(ppconst);
4679 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4680 {
4681 clear_ppconst(&ppconst);
4682 return FAIL;
4683 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004684 if (is_const != NULL)
4685 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 if (generate_ppconst(cctx, &ppconst) == FAIL)
4687 return FAIL;
4688 return OK;
4689}
4690
4691/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004692 * Toplevel expression.
4693 */
4694 static int
4695compile_expr0(char_u **arg, cctx_T *cctx)
4696{
4697 return compile_expr0_ext(arg, cctx, NULL);
4698}
4699
4700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 * compile "return [expr]"
4702 */
4703 static char_u *
4704compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4705{
4706 char_u *p = arg;
4707 garray_T *stack = &cctx->ctx_type_stack;
4708 type_T *stack_type;
4709
4710 if (*p != NUL && *p != '|' && *p != '\n')
4711 {
4712 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 return NULL;
4715
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004716 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004717 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004718 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4719 if (set_return_type)
4720 cctx->ctx_ufunc->uf_ret_type = stack_type;
4721 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004722 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004723 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4724 && stack_type->tt_type != VAR_VOID
4725 && stack_type->tt_type != VAR_UNKNOWN)
4726 {
4727 emsg(_(e_returning_value_in_function_without_return_type));
4728 return NULL;
4729 }
4730 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004731 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004732 return NULL;
4733 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 }
4736 else
4737 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004738 // "set_return_type" cannot be TRUE, only used for a lambda which
4739 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004740 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4741 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004743 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 return NULL;
4745 }
4746
4747 // No argument, return zero.
4748 generate_PUSHNR(cctx, 0);
4749 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004750 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 return NULL;
4752
4753 // "return val | endif" is possible
4754 return skipwhite(p);
4755}
4756
4757/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004758 * Get a line from the compilation context, compatible with exarg_T getline().
4759 * Return a pointer to the line in allocated memory.
4760 * Return NULL for end-of-file or some error.
4761 */
4762 static char_u *
4763exarg_getline(
4764 int c UNUSED,
4765 void *cookie,
4766 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004767 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004768{
4769 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004770 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004771
Bram Moolenaar66250c92020-08-20 15:02:42 +02004772 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004774 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004775 return NULL;
4776 ++cctx->ctx_lnum;
4777 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4778 // Comment lines result in NULL pointers, skip them.
4779 if (p != NULL)
4780 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004781 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004782}
4783
4784/*
4785 * Compile a nested :def command.
4786 */
4787 static char_u *
4788compile_nested_function(exarg_T *eap, cctx_T *cctx)
4789{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004790 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004791 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004792 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004793 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004794 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004795 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004796
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004797 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004798 {
4799 emsg(_(e_cannot_use_bang_with_nested_def));
4800 return NULL;
4801 }
4802
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004803 if (*name_start == '/')
4804 {
4805 name_end = skip_regexp(name_start + 1, '/', TRUE);
4806 if (*name_end == '/')
4807 ++name_end;
4808 eap->nextcmd = check_nextcmd(name_end);
4809 }
4810 if (name_end == name_start || *skipwhite(name_end) != '(')
4811 {
4812 if (!ends_excmd2(name_start, name_end))
4813 {
4814 semsg(_(e_invalid_command_str), eap->cmd);
4815 return NULL;
4816 }
4817
4818 // "def" or "def Name": list functions
4819 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4820 return NULL;
4821 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4822 }
4823
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004824 // Only g:Func() can use a namespace.
4825 if (name_start[1] == ':' && !is_global)
4826 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004827 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004828 return NULL;
4829 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004830 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4831 return NULL;
4832
Bram Moolenaar04b12692020-05-04 23:24:44 +02004833 eap->arg = name_end;
4834 eap->getline = exarg_getline;
4835 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004836 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004837 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004838 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004839 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004840
Bram Moolenaar822ba242020-05-24 23:00:18 +02004841 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004842 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004843 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004844 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004845 {
4846 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004847 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004848 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004849
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004850 if (is_global)
4851 {
4852 char_u *func_name = vim_strnsave(name_start + 2,
4853 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004854
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004855 if (func_name == NULL)
4856 r = FAIL;
4857 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004858 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004859 }
4860 else
4861 {
4862 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004863 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004864 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004865 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004866
Bram Moolenaareef21022020-08-01 22:16:43 +02004867 if (lvar == NULL)
4868 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004869 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004870 return NULL;
4871 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004872
4873 // copy over the block scope IDs
4874 if (block_depth > 0)
4875 {
4876 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4877 if (ufunc->uf_block_ids != NULL)
4878 {
4879 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4880 sizeof(int) * block_depth);
4881 ufunc->uf_block_depth = block_depth;
4882 }
4883 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004884 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004885
Bram Moolenaar61a89812020-05-07 16:58:17 +02004886 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004887 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004888}
4889
4890/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 * Return the length of an assignment operator, or zero if there isn't one.
4892 */
4893 int
4894assignment_len(char_u *p, int *heredoc)
4895{
4896 if (*p == '=')
4897 {
4898 if (p[1] == '<' && p[2] == '<')
4899 {
4900 *heredoc = TRUE;
4901 return 3;
4902 }
4903 return 1;
4904 }
4905 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4906 return 2;
4907 if (STRNCMP(p, "..=", 3) == 0)
4908 return 3;
4909 return 0;
4910}
4911
4912// words that cannot be used as a variable
4913static char *reserved[] = {
4914 "true",
4915 "false",
4916 NULL
4917};
4918
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004919typedef enum {
4920 dest_local,
4921 dest_option,
4922 dest_env,
4923 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004924 dest_buffer,
4925 dest_window,
4926 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004927 dest_vimvar,
4928 dest_script,
4929 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004930 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004931} assign_dest_T;
4932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004934 * Generate the load instruction for "name".
4935 */
4936 static void
4937generate_loadvar(
4938 cctx_T *cctx,
4939 assign_dest_T dest,
4940 char_u *name,
4941 lvar_T *lvar,
4942 type_T *type)
4943{
4944 switch (dest)
4945 {
4946 case dest_option:
4947 // TODO: check the option exists
4948 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4949 break;
4950 case dest_global:
4951 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4952 break;
4953 case dest_buffer:
4954 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4955 break;
4956 case dest_window:
4957 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4958 break;
4959 case dest_tab:
4960 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4961 break;
4962 case dest_script:
4963 compile_load_scriptvar(cctx,
4964 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4965 break;
4966 case dest_env:
4967 // Include $ in the name here
4968 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4969 break;
4970 case dest_reg:
4971 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4972 break;
4973 case dest_vimvar:
4974 generate_LOADV(cctx, name + 2, TRUE);
4975 break;
4976 case dest_local:
4977 if (lvar->lv_from_outer)
4978 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4979 NULL, type);
4980 else
4981 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4982 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004983 case dest_expr:
4984 // list or dict value should already be on the stack.
4985 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004986 }
4987}
4988
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004989/*
4990 * Skip over "[expr]" or ".member".
4991 * Does not check for any errors.
4992 */
4993 static char_u *
4994skip_index(char_u *start)
4995{
4996 char_u *p = start;
4997
4998 if (*p == '[')
4999 {
5000 p = skipwhite(p + 1);
5001 (void)skip_expr(&p, NULL);
5002 p = skipwhite(p);
5003 if (*p == ']')
5004 return p + 1;
5005 return p;
5006 }
5007 // if (*p == '.')
5008 return to_name_end(p + 1, TRUE);
5009}
5010
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005011 void
5012vim9_declare_error(char_u *name)
5013{
5014 char *scope = "";
5015
5016 switch (*name)
5017 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005018 case 'g': scope = _("global"); break;
5019 case 'b': scope = _("buffer"); break;
5020 case 'w': scope = _("window"); break;
5021 case 't': scope = _("tab"); break;
5022 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005023 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005024 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005025 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005026 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005027 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005028 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005029 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005030 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005031 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005032}
5033
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005034/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005035 * For one assignment figure out the type of destination. Return it in "dest".
5036 * When not recognized "dest" is not set.
5037 * For an option "opt_flags" is set.
5038 * For a v:var "vimvaridx" is set.
5039 * "type" is set to the destination type if known, unchanted otherwise.
5040 * Return FAIL if an error message was given.
5041 */
5042 static int
5043get_var_dest(
5044 char_u *name,
5045 assign_dest_T *dest,
5046 int cmdidx,
5047 int *opt_flags,
5048 int *vimvaridx,
5049 type_T **type,
5050 cctx_T *cctx)
5051{
5052 char_u *p;
5053
5054 if (*name == '&')
5055 {
5056 int cc;
5057 long numval;
5058 int opt_type;
5059
5060 *dest = dest_option;
5061 if (cmdidx == CMD_final || cmdidx == CMD_const)
5062 {
5063 emsg(_(e_const_option));
5064 return FAIL;
5065 }
5066 p = name;
5067 p = find_option_end(&p, opt_flags);
5068 if (p == NULL)
5069 {
5070 // cannot happen?
5071 emsg(_(e_letunexp));
5072 return FAIL;
5073 }
5074 cc = *p;
5075 *p = NUL;
5076 opt_type = get_option_value(skip_option_env_lead(name),
5077 &numval, NULL, *opt_flags);
5078 *p = cc;
5079 if (opt_type == -3)
5080 {
5081 semsg(_(e_unknown_option), name);
5082 return FAIL;
5083 }
5084 if (opt_type == -2 || opt_type == 0)
5085 *type = &t_string;
5086 else
5087 *type = &t_number; // both number and boolean option
5088 }
5089 else if (*name == '$')
5090 {
5091 *dest = dest_env;
5092 *type = &t_string;
5093 }
5094 else if (*name == '@')
5095 {
5096 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5097 {
5098 emsg_invreg(name[1]);
5099 return FAIL;
5100 }
5101 *dest = dest_reg;
5102 *type = &t_string;
5103 }
5104 else if (STRNCMP(name, "g:", 2) == 0)
5105 {
5106 *dest = dest_global;
5107 }
5108 else if (STRNCMP(name, "b:", 2) == 0)
5109 {
5110 *dest = dest_buffer;
5111 }
5112 else if (STRNCMP(name, "w:", 2) == 0)
5113 {
5114 *dest = dest_window;
5115 }
5116 else if (STRNCMP(name, "t:", 2) == 0)
5117 {
5118 *dest = dest_tab;
5119 }
5120 else if (STRNCMP(name, "v:", 2) == 0)
5121 {
5122 typval_T *vtv;
5123 int di_flags;
5124
5125 *vimvaridx = find_vim_var(name + 2, &di_flags);
5126 if (*vimvaridx < 0)
5127 {
5128 semsg(_(e_variable_not_found_str), name);
5129 return FAIL;
5130 }
5131 // We use the current value of "sandbox" here, is that OK?
5132 if (var_check_ro(di_flags, name, FALSE))
5133 return FAIL;
5134 *dest = dest_vimvar;
5135 vtv = get_vim_var_tv(*vimvaridx);
5136 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5137 }
5138 return OK;
5139}
5140
5141/*
5142 * Generate a STORE instruction for "dest", not being "dest_local".
5143 * Return FAIL when out of memory.
5144 */
5145 static int
5146generate_store_var(
5147 cctx_T *cctx,
5148 assign_dest_T dest,
5149 int opt_flags,
5150 int vimvaridx,
5151 int scriptvar_idx,
5152 int scriptvar_sid,
5153 type_T *type,
5154 char_u *name)
5155{
5156 switch (dest)
5157 {
5158 case dest_option:
5159 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5160 opt_flags);
5161 case dest_global:
5162 // include g: with the name, easier to execute that way
5163 return generate_STORE(cctx, ISN_STOREG, 0, name);
5164 case dest_buffer:
5165 // include b: with the name, easier to execute that way
5166 return generate_STORE(cctx, ISN_STOREB, 0, name);
5167 case dest_window:
5168 // include w: with the name, easier to execute that way
5169 return generate_STORE(cctx, ISN_STOREW, 0, name);
5170 case dest_tab:
5171 // include t: with the name, easier to execute that way
5172 return generate_STORE(cctx, ISN_STORET, 0, name);
5173 case dest_env:
5174 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5175 case dest_reg:
5176 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5177 case dest_vimvar:
5178 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5179 case dest_script:
5180 if (scriptvar_idx < 0)
5181 {
5182 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005183 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005184
Bram Moolenaar41d61962020-12-06 20:12:43 +01005185 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005186 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5187 scriptvar_sid, type);
5188 if (name_s != name)
5189 vim_free(name_s);
5190 return r;
5191 }
5192 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5193 scriptvar_sid, scriptvar_idx, type);
5194 case dest_local:
5195 case dest_expr:
5196 // cannot happen
5197 break;
5198 }
5199 return FAIL;
5200}
5201
5202/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005204 * "let name"
5205 * "var name = expr"
5206 * "final name = expr"
5207 * "const name = expr"
5208 * "name = expr"
5209 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 * Return NULL for an error.
5211 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 */
5213 static char_u *
5214compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5215{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005218 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 char_u *ret = NULL;
5220 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005222 int scriptvar_sid = 0;
5223 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005226 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 int oplen = 0;
5229 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005230 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005232 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005235 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5236 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 // Skip over the "var" or "[var, var]" to get to any "=".
5239 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5240 if (p == NULL)
5241 return *arg == '[' ? arg : NULL;
5242
5243 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005245 // TODO: should we allow this, and figure out type inference from list
5246 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005247 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 return NULL;
5249 }
5250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 sp = p;
5252 p = skipwhite(p);
5253 op = p;
5254 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255
5256 if (var_count > 0 && oplen == 0)
5257 // can be something like "[1, 2]->func()"
5258 return arg;
5259
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005260 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005262 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005263 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005264 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 if (heredoc)
5267 {
5268 list_T *l;
5269 listitem_T *li;
5270
5271 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005272 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005274 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005275 if (l == NULL)
5276 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277
Bram Moolenaar078269b2020-09-21 20:35:55 +02005278 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005280 // Push each line and the create the list.
5281 FOR_ALL_LIST_ITEMS(l, li)
5282 {
5283 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5284 li->li_tv.vval.v_string = NULL;
5285 }
5286 generate_NEWLIST(cctx, l->lv_len);
5287 type = &t_list_string;
5288 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 list_free(l);
5291 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005296 char_u *wp;
5297
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 // for "[var, var] = expr" evaluate the expression here, loop over the
5299 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005300 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005301
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005302 wp = op + oplen;
5303 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005304 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005305 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 if (compile_expr0(&p, cctx) == FAIL)
5307 return NULL;
5308 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005309
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005310 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005312 type_T *stacktype;
5313
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005314 stacktype = stack->ga_len == 0 ? &t_void
5315 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005318 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 goto theend;
5320 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005321 if (need_type(stacktype, &t_list_any, -1, cctx,
5322 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005324 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005325 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5326 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005327 if (stacktype->tt_member != NULL)
5328 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 }
5330 }
5331
5332 /*
5333 * Loop over variables in "[var, var] = expr".
5334 * For "var = expr" and "let var: type" this is done only once.
5335 */
5336 if (var_count > 0)
5337 var_start = skipwhite(arg + 1); // skip over the "["
5338 else
5339 var_start = arg;
5340 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5341 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005342 char_u *var_end;
5343 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 size_t varlen;
5345 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005347 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 int vimvaridx = -1;
5349 lvar_T *lvar = NULL;
5350 lvar_T arg_lvar;
5351 int has_type = FALSE;
5352 int has_index = FALSE;
5353 int instr_count = -1;
5354
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005355 // "dest_end" is the end of the destination, including "[expr]" or
5356 // ".name".
5357 // "var_end" is the end of the variable/option/etc. name.
5358 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005359 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005360 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005361 else
5362 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005363 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005364 var_end = skip_option_env_lead(var_start);
5365 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005366 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005368 // "a: type" is declaring variable "a" with a type, not dict "a:".
5369 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5370 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5372 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005374 // compute the length of the destination without "[expr]" or ".name"
5375 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 vim_free(name);
5377 name = vim_strnsave(var_start, varlen);
5378 if (name == NULL)
5379 return NULL;
5380 if (!heredoc)
5381 type = &t_any;
5382
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005383 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005385 int declare_error = FALSE;
5386
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005387 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5388 &vimvaridx, &type, cctx) == FAIL)
5389 goto theend;
5390 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005392 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005393 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 }
5395 else
5396 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005397 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005399 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005400 for (idx = 0; reserved[idx] != NULL; ++idx)
5401 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005402 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005403 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005404 goto theend;
5405 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005406
5407 lvar = lookup_local(var_start, varlen, cctx);
5408 if (lvar == NULL)
5409 {
5410 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005411 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5413 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005414 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 if (is_decl)
5416 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005417 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005418 goto theend;
5419 }
5420 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005421 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005422 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 if (lvar != NULL)
5424 {
5425 if (is_decl)
5426 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005427 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 goto theend;
5429 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005431 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005433 int script_namespace = varlen > 1
5434 && STRNCMP(var_start, "s:", 2) == 0;
5435 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005436 ? script_var_exists(var_start + 2, varlen - 2,
5437 FALSE, cctx)
5438 : script_var_exists(var_start, varlen,
5439 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005440 imported_T *import =
5441 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005442
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005443 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005445 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5446
5447 if (is_decl)
5448 {
5449 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005450 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005451 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005452 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005453 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005454 name);
5455 goto theend;
5456 }
5457 else if (cctx->ctx_ufunc->uf_script_ctx_version
5458 == SCRIPT_VERSION_VIM9
5459 && script_namespace
5460 && !script_var && import == NULL)
5461 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005462 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005463 goto theend;
5464 }
5465
5466 dest = dest_script;
5467
5468 // existing script-local variables should have a type
5469 scriptvar_sid = current_sctx.sc_sid;
5470 if (import != NULL)
5471 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005472 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005473 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005474 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005475 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005476 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005477 {
5478 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5479 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005480 ((svar_T *)si->sn_var_vals.ga_data)
5481 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005482 type = sv->sv_type;
5483 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005484 }
5485 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005486 else if (check_defined(var_start, varlen, cctx) == FAIL)
5487 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005489 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005490
5491 if (declare_error)
5492 {
5493 vim9_declare_error(name);
5494 goto theend;
5495 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496 }
5497
5498 // handle "a:name" as a name, not index "name" on "a"
5499 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005500 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501
5502 if (dest != dest_option)
5503 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005504 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 {
5506 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005507 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005509 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 goto theend;
5511 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005512 p = skipwhite(var_end + 1);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513 type = parse_type(&p, cctx->ctx_type_list);
5514 has_type = TRUE;
5515 }
5516 else if (lvar != NULL)
5517 type = lvar->lv_type;
5518 }
5519
5520 if (oplen == 3 && !heredoc && dest != dest_global
5521 && type->tt_type != VAR_STRING
5522 && type->tt_type != VAR_ANY)
5523 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005524 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 goto theend;
5526 }
5527
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005528 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005529 {
5530 if (oplen > 1 && !heredoc)
5531 {
5532 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005533 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 goto theend;
5535 }
5536
5537 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005538 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5539 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 goto theend;
5541 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005542 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 if (lvar == NULL)
5544 goto theend;
5545 new_local = TRUE;
5546 }
5547
5548 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005549 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005550 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005551 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005552 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 if (is_decl)
5554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005555 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556 goto theend;
5557 }
5558
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005559 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005561 char_u *after = var_start + varlen;
5562
5563 // Only the last index is used below, if there are others
5564 // before it generate code for the expression. Thus for
5565 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5566 for (;;)
5567 {
5568 p = skip_index(after);
5569 if (*p != '[' && *p != '.')
5570 break;
5571 after = p;
5572 }
5573 if (after > var_start + varlen)
5574 {
5575 varlen = after - var_start;
5576 dest = dest_expr;
5577 // We don't know the type before evaluating the expression,
5578 // use "any" until then.
5579 type = &t_any;
5580 }
5581
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005582 has_index = TRUE;
5583 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005584 member_type = &t_any;
5585 else
5586 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005587 }
5588 else
5589 {
5590 semsg("Not supported yet: %s", var_start);
5591 goto theend;
5592 }
5593 }
5594 else if (lvar == &arg_lvar)
5595 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005596 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 goto theend;
5598 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005599 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5600 {
5601 semsg(_(e_cannot_assign_to_constant), name);
5602 goto theend;
5603 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604
5605 if (!heredoc)
5606 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005607 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005609 if (oplen > 0 && var_count == 0)
5610 {
5611 // skip over the "=" and the expression
5612 p = skipwhite(op + oplen);
5613 compile_expr0(&p, cctx);
5614 }
5615 }
5616 else if (oplen > 0)
5617 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005618 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005619 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005620
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005621 // For "var = expr" evaluate the expression.
5622 if (var_count == 0)
5623 {
5624 int r;
5625
5626 // for "+=", "*=", "..=" etc. first load the current value
5627 if (*op != '=')
5628 {
5629 generate_loadvar(cctx, dest, name, lvar, type);
5630
5631 if (has_index)
5632 {
5633 // TODO: get member from list or dict
5634 emsg("Index with operation not supported yet");
5635 goto theend;
5636 }
5637 }
5638
5639 // Compile the expression. Temporarily hide the new local
5640 // variable here, it is not available to this expression.
5641 if (new_local)
5642 --cctx->ctx_locals.ga_len;
5643 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005644 wp = op + oplen;
5645 p = skipwhite(wp);
5646 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005647 {
5648 if (new_local)
5649 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005650 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005651 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005652 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653 if (new_local)
5654 ++cctx->ctx_locals.ga_len;
5655 if (r == FAIL)
5656 goto theend;
5657 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005658 else if (semicolon && var_idx == var_count - 1)
5659 {
5660 // For "[var; var] = expr" get the rest of the list
5661 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5662 goto theend;
5663 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005664 else
5665 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005666 // For "[var, var] = expr" get the "var_idx" item from the
5667 // list.
5668 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005669 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005671
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005672 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005673 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005674 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005675 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005676 if ((rhs_type->tt_type == VAR_FUNC
5677 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005678 && var_wrong_func_name(name, TRUE))
5679 goto theend;
5680
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005681 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005682 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005683 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005685 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005686 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005687 }
5688 else
5689 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005690 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005691 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005692 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005693 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005694 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005695 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005696 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005697 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005698 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005699 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005700 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005701 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005702 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005703 {
5704 type_T *use_type = lvar->lv_type;
5705
Bram Moolenaar71abe482020-09-14 22:28:30 +02005706 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005707 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005708 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005709 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005710 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005711 goto theend;
5712 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005713 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005714 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005715 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005716 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005718 else if (cmdidx == CMD_final)
5719 {
5720 emsg(_(e_final_requires_a_value));
5721 goto theend;
5722 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005723 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005725 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005726 goto theend;
5727 }
5728 else if (!has_type || dest == dest_option)
5729 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005730 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005731 goto theend;
5732 }
5733 else
5734 {
5735 // variables are always initialized
5736 if (ga_grow(instr, 1) == FAIL)
5737 goto theend;
5738 switch (member_type->tt_type)
5739 {
5740 case VAR_BOOL:
5741 generate_PUSHBOOL(cctx, VVAL_FALSE);
5742 break;
5743 case VAR_FLOAT:
5744#ifdef FEAT_FLOAT
5745 generate_PUSHF(cctx, 0.0);
5746#endif
5747 break;
5748 case VAR_STRING:
5749 generate_PUSHS(cctx, NULL);
5750 break;
5751 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005752 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005753 break;
5754 case VAR_FUNC:
5755 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5756 break;
5757 case VAR_LIST:
5758 generate_NEWLIST(cctx, 0);
5759 break;
5760 case VAR_DICT:
5761 generate_NEWDICT(cctx, 0);
5762 break;
5763 case VAR_JOB:
5764 generate_PUSHJOB(cctx, NULL);
5765 break;
5766 case VAR_CHANNEL:
5767 generate_PUSHCHANNEL(cctx, NULL);
5768 break;
5769 case VAR_NUMBER:
5770 case VAR_UNKNOWN:
5771 case VAR_ANY:
5772 case VAR_PARTIAL:
5773 case VAR_VOID:
5774 case VAR_SPECIAL: // cannot happen
5775 generate_PUSHNR(cctx, 0);
5776 break;
5777 }
5778 }
5779 if (var_count == 0)
5780 end = p;
5781 }
5782
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005783 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005784 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005785 break;
5786
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005787 if (oplen > 0 && *op != '=')
5788 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005789 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005790 type_T *stacktype;
5791
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005792 if (*op == '.')
5793 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005794 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005795 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005796 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005797 if (
5798#ifdef FEAT_FLOAT
5799 // If variable is float operation with number is OK.
5800 !(expected == &t_float && stacktype == &t_number) &&
5801#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005802 need_type(stacktype, expected, -1, cctx,
5803 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005804 goto theend;
5805
5806 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005807 {
5808 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5809 goto theend;
5810 }
5811 else if (*op == '+')
5812 {
5813 if (generate_add_instr(cctx,
5814 operator_type(member_type, stacktype),
5815 member_type, stacktype) == FAIL)
5816 goto theend;
5817 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005818 else if (generate_two_op(cctx, op) == FAIL)
5819 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005822 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005823 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 int r;
5825
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005826 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005827 if (new_local)
5828 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005829 p = var_start + varlen;
5830 if (*p == '[')
5831 {
5832 p = skipwhite(p + 1);
5833 r = compile_expr0(&p, cctx);
5834 if (r == OK && *skipwhite(p) != ']')
5835 {
5836 // this should not happen
5837 emsg(_(e_missbrac));
5838 r = FAIL;
5839 }
5840 }
5841 else // if (*p == '.')
5842 {
5843 char_u *key_end = to_name_end(p + 1, TRUE);
5844 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5845
5846 r = generate_PUSHS(cctx, key);
5847 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005848 if (new_local)
5849 ++cctx->ctx_locals.ga_len;
5850 if (r == FAIL)
5851 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005852
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005853 if (type == &t_any)
5854 {
5855 type_T *idx_type = ((type_T **)stack->ga_data)[
5856 stack->ga_len - 1];
5857 // Index on variable of unknown type: guess the type from the
5858 // index type: number is dict, otherwise dict.
5859 // TODO: should do the assignment at runtime
5860 if (idx_type->tt_type == VAR_NUMBER)
5861 type = &t_list_any;
5862 else
5863 type = &t_dict_any;
5864 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005865 if (type->tt_type == VAR_DICT
5866 && may_generate_2STRING(-1, cctx) == FAIL)
5867 goto theend;
5868 if (type->tt_type == VAR_LIST
5869 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005870 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005871 {
5872 emsg(_(e_number_exp));
5873 goto theend;
5874 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005875
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005876 // Load the dict or list. On the stack we then have:
5877 // - value
5878 // - index
5879 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005880 if (dest == dest_expr)
5881 {
5882 int c = var_start[varlen];
5883
5884 // Evaluate "ll[expr]" of "ll[expr][idx]"
5885 p = var_start;
5886 var_start[varlen] = NUL;
5887 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5888 {
5889 // this should not happen
5890 emsg(_(e_missbrac));
5891 goto theend;
5892 }
5893 var_start[varlen] = c;
5894
5895 type = stack->ga_len == 0 ? &t_void
5896 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5897 // now we can properly check the type
5898 if (type->tt_member != NULL
5899 && need_type(rhs_type, type->tt_member, -2, cctx,
5900 FALSE, FALSE) == FAIL)
5901 goto theend;
5902 }
5903 else
5904 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005905
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005906 if (type->tt_type == VAR_LIST)
5907 {
5908 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005909 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005910 }
5911 else if (type->tt_type == VAR_DICT)
5912 {
5913 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005914 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005915 }
5916 else
5917 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005918 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005919 goto theend;
5920 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005921 }
5922 else
5923 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005924 if (is_decl && cmdidx == CMD_const
5925 && (dest == dest_script || dest == dest_local))
5926 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005927 generate_LOCKCONST(cctx);
5928
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005929 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005930 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005931 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
5932 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
5933 goto theend;
5934 }
5935 else if (lvar != NULL)
5936 {
5937 isn_T *isn = ((isn_T *)instr->ga_data)
5938 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005939
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005940 // optimization: turn "var = 123" from ISN_PUSHNR +
5941 // ISN_STORE into ISN_STORENR
5942 if (!lvar->lv_from_outer
5943 && instr->ga_len == instr_count + 1
5944 && isn->isn_type == ISN_PUSHNR)
5945 {
5946 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005947
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005948 isn->isn_type = ISN_STORENR;
5949 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5950 isn->isn_arg.storenr.stnr_val = val;
5951 if (stack->ga_len > 0)
5952 --stack->ga_len;
5953 }
5954 else if (lvar->lv_from_outer)
5955 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
5956 else
5957 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005958 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005959 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005960
5961 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005962 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005964
5965 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005966 if (var_count > 0 && !semicolon)
5967 {
5968 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5969 goto theend;
5970 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005971
Bram Moolenaarb2097502020-07-19 17:17:02 +02005972 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
5974theend:
5975 vim_free(name);
5976 return ret;
5977}
5978
5979/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005980 * Check if "name" can be "unlet".
5981 */
5982 int
5983check_vim9_unlet(char_u *name)
5984{
5985 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5986 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005987 // "unlet s:var" is allowed in legacy script.
5988 if (*name == 's' && !script_is_vim9())
5989 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005990 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005991 return FAIL;
5992 }
5993 return OK;
5994}
5995
5996/*
5997 * Callback passed to ex_unletlock().
5998 */
5999 static int
6000compile_unlet(
6001 lval_T *lvp,
6002 char_u *name_end,
6003 exarg_T *eap,
6004 int deep UNUSED,
6005 void *coookie)
6006{
6007 cctx_T *cctx = coookie;
6008
6009 if (lvp->ll_tv == NULL)
6010 {
6011 char_u *p = lvp->ll_name;
6012 int cc = *name_end;
6013 int ret = OK;
6014
6015 // Normal name. Only supports g:, w:, t: and b: namespaces.
6016 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006017 if (*p == '$')
6018 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6019 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006020 ret = FAIL;
6021 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006022 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006023
6024 *name_end = cc;
6025 return ret;
6026 }
6027
6028 // TODO: unlet {list}[idx]
6029 // TODO: unlet {dict}[key]
6030 emsg("Sorry, :unlet not fully implemented yet");
6031 return FAIL;
6032}
6033
6034/*
6035 * compile "unlet var", "lock var" and "unlock var"
6036 * "arg" points to "var".
6037 */
6038 static char_u *
6039compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6040{
6041 char_u *p = arg;
6042
6043 if (eap->cmdidx != CMD_unlet)
6044 {
6045 emsg("Sorry, :lock and unlock not implemented yet");
6046 return NULL;
6047 }
6048
6049 if (*p == '!')
6050 {
6051 p = skipwhite(p + 1);
6052 eap->forceit = TRUE;
6053 }
6054
6055 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6056 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6057}
6058
6059/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 * Compile an :import command.
6061 */
6062 static char_u *
6063compile_import(char_u *arg, cctx_T *cctx)
6064{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006065 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066}
6067
6068/*
6069 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6070 */
6071 static int
6072compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6073{
6074 garray_T *instr = &cctx->ctx_instr;
6075 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6076
6077 if (endlabel == NULL)
6078 return FAIL;
6079 endlabel->el_next = *el;
6080 *el = endlabel;
6081 endlabel->el_end_label = instr->ga_len;
6082
6083 generate_JUMP(cctx, when, 0);
6084 return OK;
6085}
6086
6087 static void
6088compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6089{
6090 garray_T *instr = &cctx->ctx_instr;
6091
6092 while (*el != NULL)
6093 {
6094 endlabel_T *cur = (*el);
6095 isn_T *isn;
6096
6097 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6098 isn->isn_arg.jump.jump_where = instr->ga_len;
6099 *el = cur->el_next;
6100 vim_free(cur);
6101 }
6102}
6103
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006104 static void
6105compile_free_jump_to_end(endlabel_T **el)
6106{
6107 while (*el != NULL)
6108 {
6109 endlabel_T *cur = (*el);
6110
6111 *el = cur->el_next;
6112 vim_free(cur);
6113 }
6114}
6115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116/*
6117 * Create a new scope and set up the generic items.
6118 */
6119 static scope_T *
6120new_scope(cctx_T *cctx, scopetype_T type)
6121{
6122 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6123
6124 if (scope == NULL)
6125 return NULL;
6126 scope->se_outer = cctx->ctx_scope;
6127 cctx->ctx_scope = scope;
6128 scope->se_type = type;
6129 scope->se_local_count = cctx->ctx_locals.ga_len;
6130 return scope;
6131}
6132
6133/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006134 * Free the current scope and go back to the outer scope.
6135 */
6136 static void
6137drop_scope(cctx_T *cctx)
6138{
6139 scope_T *scope = cctx->ctx_scope;
6140
6141 if (scope == NULL)
6142 {
6143 iemsg("calling drop_scope() without a scope");
6144 return;
6145 }
6146 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006147 switch (scope->se_type)
6148 {
6149 case IF_SCOPE:
6150 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6151 case FOR_SCOPE:
6152 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6153 case WHILE_SCOPE:
6154 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6155 case TRY_SCOPE:
6156 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6157 case NO_SCOPE:
6158 case BLOCK_SCOPE:
6159 break;
6160 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006161 vim_free(scope);
6162}
6163
6164/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 * compile "if expr"
6166 *
6167 * "if expr" Produces instructions:
6168 * EVAL expr Push result of "expr"
6169 * JUMP_IF_FALSE end
6170 * ... body ...
6171 * end:
6172 *
6173 * "if expr | else" Produces instructions:
6174 * EVAL expr Push result of "expr"
6175 * JUMP_IF_FALSE else
6176 * ... body ...
6177 * JUMP_ALWAYS end
6178 * else:
6179 * ... body ...
6180 * end:
6181 *
6182 * "if expr1 | elseif expr2 | else" Produces instructions:
6183 * EVAL expr Push result of "expr"
6184 * JUMP_IF_FALSE elseif
6185 * ... body ...
6186 * JUMP_ALWAYS end
6187 * elseif:
6188 * EVAL expr Push result of "expr"
6189 * JUMP_IF_FALSE else
6190 * ... body ...
6191 * JUMP_ALWAYS end
6192 * else:
6193 * ... body ...
6194 * end:
6195 */
6196 static char_u *
6197compile_if(char_u *arg, cctx_T *cctx)
6198{
6199 char_u *p = arg;
6200 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006201 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006203 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006204 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205
Bram Moolenaara5565e42020-05-09 15:44:01 +02006206 CLEAR_FIELD(ppconst);
6207 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006208 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006209 clear_ppconst(&ppconst);
6210 return NULL;
6211 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006212 if (cctx->ctx_skip == SKIP_YES)
6213 clear_ppconst(&ppconst);
6214 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006215 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006216 int error = FALSE;
6217 int v;
6218
Bram Moolenaara5565e42020-05-09 15:44:01 +02006219 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006220 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006221 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006222 if (error)
6223 return NULL;
6224 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006225 }
6226 else
6227 {
6228 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006229 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006230 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006231 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006232 if (bool_on_stack(cctx) == FAIL)
6233 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235
6236 scope = new_scope(cctx, IF_SCOPE);
6237 if (scope == NULL)
6238 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006239 scope->se_skip_save = skip_save;
6240 // "is_had_return" will be reset if any block does not end in :return
6241 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006243 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006244 {
6245 // "where" is set when ":elseif", "else" or ":endif" is found
6246 scope->se_u.se_if.is_if_label = instr->ga_len;
6247 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6248 }
6249 else
6250 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
6252 return p;
6253}
6254
6255 static char_u *
6256compile_elseif(char_u *arg, cctx_T *cctx)
6257{
6258 char_u *p = arg;
6259 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006260 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 isn_T *isn;
6262 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006263 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006264 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265
6266 if (scope == NULL || scope->se_type != IF_SCOPE)
6267 {
6268 emsg(_(e_elseif_without_if));
6269 return NULL;
6270 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006271 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006272 if (!cctx->ctx_had_return)
6273 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006275 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006276 {
6277 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006279 return NULL;
6280 // previous "if" or "elseif" jumps here
6281 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6282 isn->isn_arg.jump.jump_where = instr->ga_len;
6283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006285 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006286 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006287 if (cctx->ctx_skip == SKIP_YES)
6288 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006289 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006290 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006291 clear_ppconst(&ppconst);
6292 return NULL;
6293 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006294 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006295 if (scope->se_skip_save == SKIP_YES)
6296 clear_ppconst(&ppconst);
6297 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006298 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006299 int error = FALSE;
6300 int v;
6301
Bram Moolenaar7f141552020-05-09 17:35:53 +02006302 // The expression results in a constant.
6303 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006304 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6305 if (error)
6306 return NULL;
6307 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006308 clear_ppconst(&ppconst);
6309 scope->se_u.se_if.is_if_label = -1;
6310 }
6311 else
6312 {
6313 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006314 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006315 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006316 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006317 if (bool_on_stack(cctx) == FAIL)
6318 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006320 // "where" is set when ":elseif", "else" or ":endif" is found
6321 scope->se_u.se_if.is_if_label = instr->ga_len;
6322 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324
6325 return p;
6326}
6327
6328 static char_u *
6329compile_else(char_u *arg, cctx_T *cctx)
6330{
6331 char_u *p = arg;
6332 garray_T *instr = &cctx->ctx_instr;
6333 isn_T *isn;
6334 scope_T *scope = cctx->ctx_scope;
6335
6336 if (scope == NULL || scope->se_type != IF_SCOPE)
6337 {
6338 emsg(_(e_else_without_if));
6339 return NULL;
6340 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006341 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006342 if (!cctx->ctx_had_return)
6343 scope->se_u.se_if.is_had_return = FALSE;
6344 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345
Bram Moolenaarefd88552020-06-18 20:50:10 +02006346 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006347 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006348 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006349 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006350 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006351 if (!cctx->ctx_had_return
6352 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6353 JUMP_ALWAYS, cctx) == FAIL)
6354 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006355 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006356
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006357 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006358 {
6359 if (scope->se_u.se_if.is_if_label >= 0)
6360 {
6361 // previous "if" or "elseif" jumps here
6362 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6363 isn->isn_arg.jump.jump_where = instr->ga_len;
6364 scope->se_u.se_if.is_if_label = -1;
6365 }
6366 }
6367
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006368 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006369 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371
6372 return p;
6373}
6374
6375 static char_u *
6376compile_endif(char_u *arg, cctx_T *cctx)
6377{
6378 scope_T *scope = cctx->ctx_scope;
6379 ifscope_T *ifscope;
6380 garray_T *instr = &cctx->ctx_instr;
6381 isn_T *isn;
6382
6383 if (scope == NULL || scope->se_type != IF_SCOPE)
6384 {
6385 emsg(_(e_endif_without_if));
6386 return NULL;
6387 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006388 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006389 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006390 if (!cctx->ctx_had_return)
6391 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006393 if (scope->se_u.se_if.is_if_label >= 0)
6394 {
6395 // previous "if" or "elseif" jumps here
6396 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6397 isn->isn_arg.jump.jump_where = instr->ga_len;
6398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 // Fill in the "end" label in jumps at the end of the blocks.
6400 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006401 cctx->ctx_skip = scope->se_skip_save;
6402
6403 // If all the blocks end in :return and there is an :else then the
6404 // had_return flag is set.
6405 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006407 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 return arg;
6409}
6410
6411/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006412 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 *
6414 * Produces instructions:
6415 * PUSHNR -1
6416 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006417 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6419 * - if beyond end, jump to "end"
6420 * - otherwise get item from list and push it
6421 * STORE var Store item in "var"
6422 * ... body ...
6423 * JUMP top Jump back to repeat
6424 * end: DROP Drop the result of "expr"
6425 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006426 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6427 * UNPACK 2 Split item in 2
6428 * STORE var1 Store item in "var1"
6429 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 */
6431 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006432compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006434 char_u *arg;
6435 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006436 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006438 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006439 int var_count = 0;
6440 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 size_t varlen;
6442 garray_T *instr = &cctx->ctx_instr;
6443 garray_T *stack = &cctx->ctx_type_stack;
6444 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006445 lvar_T *loop_lvar; // loop iteration variable
6446 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006448 type_T *item_type = &t_any;
6449 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450
Bram Moolenaar792f7862020-11-23 08:31:18 +01006451 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6452 if (var_count == 0)
6453 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454
6455 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006456 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006458 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6459 return NULL;
6460 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461 {
6462 emsg(_(e_missing_in));
6463 return NULL;
6464 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006465 wp = p + 2;
6466 p = skipwhite(wp);
6467 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6468 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 scope = new_scope(cctx, FOR_SCOPE);
6471 if (scope == NULL)
6472 return NULL;
6473
Bram Moolenaar792f7862020-11-23 08:31:18 +01006474 // Reserve a variable to store the loop iteration counter and initialize it
6475 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006476 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6477 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006478 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006479 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006480 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006482 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006483 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484
6485 // compile "expr", it remains on the stack until "endfor"
6486 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006487 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006488 {
6489 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006491 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006492 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006494 // Now that we know the type of "var", check that it is a list, now or at
6495 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006497 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006499 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500 return NULL;
6501 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006502
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006503 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006504 {
6505 if (var_count == 1)
6506 item_type = vartype->tt_member;
6507 else if (vartype->tt_member->tt_type == VAR_LIST
6508 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6509 item_type = vartype->tt_member->tt_member;
6510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511
6512 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006513 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006514 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515
Bram Moolenaar792f7862020-11-23 08:31:18 +01006516 arg = arg_start;
6517 if (var_count > 1)
6518 {
6519 generate_UNPACK(cctx, var_count, semicolon);
6520 arg = skipwhite(arg + 1); // skip white after '['
6521
6522 // the list item is replaced by a number of items
6523 if (ga_grow(stack, var_count - 1) == FAIL)
6524 {
6525 drop_scope(cctx);
6526 return NULL;
6527 }
6528 --stack->ga_len;
6529 for (idx = 0; idx < var_count; ++idx)
6530 {
6531 ((type_T **)stack->ga_data)[stack->ga_len] =
6532 (semicolon && idx == 0) ? vartype : item_type;
6533 ++stack->ga_len;
6534 }
6535 }
6536
6537 for (idx = 0; idx < var_count; ++idx)
6538 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006539 assign_dest_T dest = dest_local;
6540 int opt_flags = 0;
6541 int vimvaridx = -1;
6542 type_T *type = &t_any;
6543
6544 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006545 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006546 name = vim_strnsave(arg, varlen);
6547 if (name == NULL)
6548 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006549
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006550 // TODO: script var not supported?
6551 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6552 &vimvaridx, &type, cctx) == FAIL)
6553 goto failed;
6554 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006555 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006556 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6557 0, 0, type, name) == FAIL)
6558 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006559 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006560 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006561 {
6562 var_lvar = lookup_local(arg, varlen, cctx);
6563 if (var_lvar != NULL)
6564 {
6565 semsg(_(e_variable_already_declared), arg);
6566 goto failed;
6567 }
6568
Bram Moolenaarea870692020-12-02 14:24:30 +01006569 if (STRNCMP(name, "s:", 2) == 0)
6570 {
6571 semsg(_(e_cannot_declare_script_variable_in_function), name);
6572 goto failed;
6573 }
6574
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006575 // Reserve a variable to store "var".
6576 // TODO: check for type
6577 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6578 if (var_lvar == NULL)
6579 // out of memory or used as an argument
6580 goto failed;
6581
6582 if (semicolon && idx == var_count - 1)
6583 var_lvar->lv_type = vartype;
6584 else
6585 var_lvar->lv_type = item_type;
6586 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6587 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006588
6589 if (*p == ',' || *p == ';')
6590 ++p;
6591 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006592 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006593 }
6594
6595 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006596
6597failed:
6598 vim_free(name);
6599 drop_scope(cctx);
6600 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601}
6602
6603/*
6604 * compile "endfor"
6605 */
6606 static char_u *
6607compile_endfor(char_u *arg, cctx_T *cctx)
6608{
6609 garray_T *instr = &cctx->ctx_instr;
6610 scope_T *scope = cctx->ctx_scope;
6611 forscope_T *forscope;
6612 isn_T *isn;
6613
6614 if (scope == NULL || scope->se_type != FOR_SCOPE)
6615 {
6616 emsg(_(e_for));
6617 return NULL;
6618 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006619 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006621 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622
6623 // At end of ":for" scope jump back to the FOR instruction.
6624 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6625
6626 // Fill in the "end" label in the FOR statement so it can jump here
6627 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6628 isn->isn_arg.forloop.for_end = instr->ga_len;
6629
6630 // Fill in the "end" label any BREAK statements
6631 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6632
6633 // Below the ":for" scope drop the "expr" list from the stack.
6634 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6635 return NULL;
6636
6637 vim_free(scope);
6638
6639 return arg;
6640}
6641
6642/*
6643 * compile "while expr"
6644 *
6645 * Produces instructions:
6646 * top: EVAL expr Push result of "expr"
6647 * JUMP_IF_FALSE end jump if false
6648 * ... body ...
6649 * JUMP top Jump back to repeat
6650 * end:
6651 *
6652 */
6653 static char_u *
6654compile_while(char_u *arg, cctx_T *cctx)
6655{
6656 char_u *p = arg;
6657 garray_T *instr = &cctx->ctx_instr;
6658 scope_T *scope;
6659
6660 scope = new_scope(cctx, WHILE_SCOPE);
6661 if (scope == NULL)
6662 return NULL;
6663
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006664 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665
6666 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006667 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 return NULL;
6669
Bram Moolenaar13106602020-10-04 16:06:05 +02006670 if (bool_on_stack(cctx) == FAIL)
6671 return FAIL;
6672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006674 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 JUMP_IF_FALSE, cctx) == FAIL)
6676 return FAIL;
6677
6678 return p;
6679}
6680
6681/*
6682 * compile "endwhile"
6683 */
6684 static char_u *
6685compile_endwhile(char_u *arg, cctx_T *cctx)
6686{
6687 scope_T *scope = cctx->ctx_scope;
6688
6689 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6690 {
6691 emsg(_(e_while));
6692 return NULL;
6693 }
6694 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006695 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696
6697 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006698 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699
6700 // Fill in the "end" label in the WHILE statement so it can jump here.
6701 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006702 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703
6704 vim_free(scope);
6705
6706 return arg;
6707}
6708
6709/*
6710 * compile "continue"
6711 */
6712 static char_u *
6713compile_continue(char_u *arg, cctx_T *cctx)
6714{
6715 scope_T *scope = cctx->ctx_scope;
6716
6717 for (;;)
6718 {
6719 if (scope == NULL)
6720 {
6721 emsg(_(e_continue));
6722 return NULL;
6723 }
6724 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6725 break;
6726 scope = scope->se_outer;
6727 }
6728
6729 // Jump back to the FOR or WHILE instruction.
6730 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006731 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6732 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 return arg;
6734}
6735
6736/*
6737 * compile "break"
6738 */
6739 static char_u *
6740compile_break(char_u *arg, cctx_T *cctx)
6741{
6742 scope_T *scope = cctx->ctx_scope;
6743 endlabel_T **el;
6744
6745 for (;;)
6746 {
6747 if (scope == NULL)
6748 {
6749 emsg(_(e_break));
6750 return NULL;
6751 }
6752 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6753 break;
6754 scope = scope->se_outer;
6755 }
6756
6757 // Jump to the end of the FOR or WHILE loop.
6758 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006759 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006761 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6763 return FAIL;
6764
6765 return arg;
6766}
6767
6768/*
6769 * compile "{" start of block
6770 */
6771 static char_u *
6772compile_block(char_u *arg, cctx_T *cctx)
6773{
6774 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6775 return NULL;
6776 return skipwhite(arg + 1);
6777}
6778
6779/*
6780 * compile end of block: drop one scope
6781 */
6782 static void
6783compile_endblock(cctx_T *cctx)
6784{
6785 scope_T *scope = cctx->ctx_scope;
6786
6787 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006788 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 vim_free(scope);
6790}
6791
6792/*
6793 * compile "try"
6794 * Creates a new scope for the try-endtry, pointing to the first catch and
6795 * finally.
6796 * Creates another scope for the "try" block itself.
6797 * TRY instruction sets up exception handling at runtime.
6798 *
6799 * "try"
6800 * TRY -> catch1, -> finally push trystack entry
6801 * ... try block
6802 * "throw {exception}"
6803 * EVAL {exception}
6804 * THROW create exception
6805 * ... try block
6806 * " catch {expr}"
6807 * JUMP -> finally
6808 * catch1: PUSH exeception
6809 * EVAL {expr}
6810 * MATCH
6811 * JUMP nomatch -> catch2
6812 * CATCH remove exception
6813 * ... catch block
6814 * " catch"
6815 * JUMP -> finally
6816 * catch2: CATCH remove exception
6817 * ... catch block
6818 * " finally"
6819 * finally:
6820 * ... finally block
6821 * " endtry"
6822 * ENDTRY pop trystack entry, may rethrow
6823 */
6824 static char_u *
6825compile_try(char_u *arg, cctx_T *cctx)
6826{
6827 garray_T *instr = &cctx->ctx_instr;
6828 scope_T *try_scope;
6829 scope_T *scope;
6830
6831 // scope that holds the jumps that go to catch/finally/endtry
6832 try_scope = new_scope(cctx, TRY_SCOPE);
6833 if (try_scope == NULL)
6834 return NULL;
6835
6836 // "catch" is set when the first ":catch" is found.
6837 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006838 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 if (generate_instr(cctx, ISN_TRY) == NULL)
6840 return NULL;
6841
6842 // scope for the try block itself
6843 scope = new_scope(cctx, BLOCK_SCOPE);
6844 if (scope == NULL)
6845 return NULL;
6846
6847 return arg;
6848}
6849
6850/*
6851 * compile "catch {expr}"
6852 */
6853 static char_u *
6854compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6855{
6856 scope_T *scope = cctx->ctx_scope;
6857 garray_T *instr = &cctx->ctx_instr;
6858 char_u *p;
6859 isn_T *isn;
6860
6861 // end block scope from :try or :catch
6862 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6863 compile_endblock(cctx);
6864 scope = cctx->ctx_scope;
6865
6866 // Error if not in a :try scope
6867 if (scope == NULL || scope->se_type != TRY_SCOPE)
6868 {
6869 emsg(_(e_catch));
6870 return NULL;
6871 }
6872
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006873 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006875 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 return NULL;
6877 }
6878
6879 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006880 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 JUMP_ALWAYS, cctx) == FAIL)
6882 return NULL;
6883
6884 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006885 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 if (isn->isn_arg.try.try_catch == 0)
6887 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006888 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 {
6890 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006891 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 isn->isn_arg.jump.jump_where = instr->ga_len;
6893 }
6894
6895 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006896 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006898 scope->se_u.se_try.ts_caught_all = TRUE;
6899 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 }
6901 else
6902 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006903 char_u *end;
6904 char_u *pat;
6905 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006906 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006907 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 // Push v:exception, push {expr} and MATCH
6910 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6911
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006912 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006913 if (*end != *p)
6914 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006915 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006916 vim_free(tofree);
6917 return FAIL;
6918 }
6919 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006920 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006921 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006922 len = (int)(end - tofree);
6923 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006924 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006925 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006926 if (pat == NULL)
6927 return FAIL;
6928 if (generate_PUSHS(cctx, pat) == FAIL)
6929 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6932 return NULL;
6933
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006934 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6936 return NULL;
6937 }
6938
6939 if (generate_instr(cctx, ISN_CATCH) == NULL)
6940 return NULL;
6941
6942 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6943 return NULL;
6944 return p;
6945}
6946
6947 static char_u *
6948compile_finally(char_u *arg, cctx_T *cctx)
6949{
6950 scope_T *scope = cctx->ctx_scope;
6951 garray_T *instr = &cctx->ctx_instr;
6952 isn_T *isn;
6953
6954 // end block scope from :try or :catch
6955 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6956 compile_endblock(cctx);
6957 scope = cctx->ctx_scope;
6958
6959 // Error if not in a :try scope
6960 if (scope == NULL || scope->se_type != TRY_SCOPE)
6961 {
6962 emsg(_(e_finally));
6963 return NULL;
6964 }
6965
6966 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006967 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 if (isn->isn_arg.try.try_finally != 0)
6969 {
6970 emsg(_(e_finally_dup));
6971 return NULL;
6972 }
6973
6974 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006975 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976
Bram Moolenaar585fea72020-04-02 22:33:21 +02006977 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006978 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 {
6980 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006981 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006983 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 }
6985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 // TODO: set index in ts_finally_label jumps
6987
6988 return arg;
6989}
6990
6991 static char_u *
6992compile_endtry(char_u *arg, cctx_T *cctx)
6993{
6994 scope_T *scope = cctx->ctx_scope;
6995 garray_T *instr = &cctx->ctx_instr;
6996 isn_T *isn;
6997
6998 // end block scope from :catch or :finally
6999 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7000 compile_endblock(cctx);
7001 scope = cctx->ctx_scope;
7002
7003 // Error if not in a :try scope
7004 if (scope == NULL || scope->se_type != TRY_SCOPE)
7005 {
7006 if (scope == NULL)
7007 emsg(_(e_no_endtry));
7008 else if (scope->se_type == WHILE_SCOPE)
7009 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007010 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 emsg(_(e_endfor));
7012 else
7013 emsg(_(e_endif));
7014 return NULL;
7015 }
7016
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007017 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7019 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007020 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 return NULL;
7022 }
7023
7024 // Fill in the "end" label in jumps at the end of the blocks, if not done
7025 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007026 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027
7028 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007029 if (isn->isn_arg.try.try_catch == 0)
7030 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 if (isn->isn_arg.try.try_finally == 0)
7032 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007033
7034 if (scope->se_u.se_try.ts_catch_label != 0)
7035 {
7036 // Last catch without match jumps here
7037 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7038 isn->isn_arg.jump.jump_where = instr->ga_len;
7039 }
7040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 compile_endblock(cctx);
7042
7043 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7044 return NULL;
7045 return arg;
7046}
7047
7048/*
7049 * compile "throw {expr}"
7050 */
7051 static char_u *
7052compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7053{
7054 char_u *p = skipwhite(arg);
7055
Bram Moolenaara5565e42020-05-09 15:44:01 +02007056 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 return NULL;
7058 if (may_generate_2STRING(-1, cctx) == FAIL)
7059 return NULL;
7060 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7061 return NULL;
7062
7063 return p;
7064}
7065
7066/*
7067 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007068 * compile "echomsg expr"
7069 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007070 * compile "execute expr"
7071 */
7072 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007073compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007074{
7075 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007076 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007077 int count = 0;
7078
7079 for (;;)
7080 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007081 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007082 return NULL;
7083 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007084 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007085 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007086 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01007087 break;
7088 }
7089
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007090 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7091 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7092 else if (cmdidx == CMD_execute)
7093 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7094 else if (cmdidx == CMD_echomsg)
7095 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7096 else
7097 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 return p;
7099}
7100
7101/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007102 * :put r
7103 * :put ={expr}
7104 */
7105 static char_u *
7106compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7107{
7108 char_u *line = arg;
7109 linenr_T lnum;
7110 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007111 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007112
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007113 eap->regname = *line;
7114
7115 if (eap->regname == '=')
7116 {
7117 char_u *p = line + 1;
7118
7119 if (compile_expr0(&p, cctx) == FAIL)
7120 return NULL;
7121 line = p;
7122 }
7123 else if (eap->regname != NUL)
7124 ++line;
7125
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007126 // "errormsg" will not be set because the range is ADDR_LINES.
7127 // TODO: if the range contains something like "$" or "." need to evaluate
7128 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007129 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
7130 return NULL;
7131 if (eap->addr_count == 0)
7132 lnum = -1;
7133 else
7134 lnum = eap->line2;
7135 if (above)
7136 --lnum;
7137
7138 generate_PUT(cctx, eap->regname, lnum);
7139 return line;
7140}
7141
7142/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007143 * A command that is not compiled, execute with legacy code.
7144 */
7145 static char_u *
7146compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7147{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007148 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007149 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007150 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007151
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007152 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007153 goto theend;
7154
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007155 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007156 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007157 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007158 int usefilter = FALSE;
7159
7160 has_expr = argt & (EX_XFILE | EX_EXPAND);
7161
7162 // If the command can be followed by a bar, find the bar and truncate
7163 // it, so that the following command can be compiled.
7164 // The '|' is overwritten with a NUL, it is put back below.
7165 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7166 && *eap->arg == '!')
7167 // :w !filter or :r !filter or :r! filter
7168 usefilter = TRUE;
7169 if ((argt & EX_TRLBAR) && !usefilter)
7170 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007171 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007172 separate_nextcmd(eap);
7173 if (eap->nextcmd != NULL)
7174 nextcmd = eap->nextcmd;
7175 }
7176 }
7177
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007178 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7179 {
7180 // expand filename in "syntax include [@group] filename"
7181 has_expr = TRUE;
7182 eap->arg = skipwhite(eap->arg + 7);
7183 if (*eap->arg == '@')
7184 eap->arg = skiptowhite(eap->arg);
7185 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007186
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007187 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007188 {
7189 int count = 0;
7190 char_u *start = skipwhite(line);
7191
7192 // :cmd xxx`=expr1`yyy`=expr2`zzz
7193 // PUSHS ":cmd xxx"
7194 // eval expr1
7195 // PUSHS "yyy"
7196 // eval expr2
7197 // PUSHS "zzz"
7198 // EXECCONCAT 5
7199 for (;;)
7200 {
7201 if (p > start)
7202 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007203 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007204 ++count;
7205 }
7206 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007207 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007208 return NULL;
7209 may_generate_2STRING(-1, cctx);
7210 ++count;
7211 p = skipwhite(p);
7212 if (*p != '`')
7213 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007214 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007215 return NULL;
7216 }
7217 start = p + 1;
7218
7219 p = (char_u *)strstr((char *)start, "`=");
7220 if (p == NULL)
7221 {
7222 if (*skipwhite(start) != NUL)
7223 {
7224 generate_PUSHS(cctx, vim_strsave(start));
7225 ++count;
7226 }
7227 break;
7228 }
7229 }
7230 generate_EXECCONCAT(cctx, count);
7231 }
7232 else
7233 generate_EXEC(cctx, line);
7234
7235theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007236 if (*nextcmd != NUL)
7237 {
7238 // the parser expects a pointer to the bar, put it back
7239 --nextcmd;
7240 *nextcmd = '|';
7241 }
7242
7243 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007244}
7245
7246/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007247 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007248 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007249 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007250 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007251add_def_function(ufunc_T *ufunc)
7252{
7253 dfunc_T *dfunc;
7254
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007255 if (def_functions.ga_len == 0)
7256 {
7257 // The first position is not used, so that a zero uf_dfunc_idx means it
7258 // wasn't set.
7259 if (ga_grow(&def_functions, 1) == FAIL)
7260 return FAIL;
7261 ++def_functions.ga_len;
7262 }
7263
Bram Moolenaar09689a02020-05-09 22:50:08 +02007264 // Add the function to "def_functions".
7265 if (ga_grow(&def_functions, 1) == FAIL)
7266 return FAIL;
7267 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7268 CLEAR_POINTER(dfunc);
7269 dfunc->df_idx = def_functions.ga_len;
7270 ufunc->uf_dfunc_idx = dfunc->df_idx;
7271 dfunc->df_ufunc = ufunc;
7272 ++def_functions.ga_len;
7273 return OK;
7274}
7275
7276/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 * After ex_function() has collected all the function lines: parse and compile
7278 * the lines into instructions.
7279 * Adds the function to "def_functions".
7280 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7281 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007282 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007283 * This can be used recursively through compile_lambda(), which may reallocate
7284 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007285 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007287 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007288compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 char_u *line = NULL;
7291 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 cctx_T cctx;
7294 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007295 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 int ret = FAIL;
7297 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007298 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007299 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007300 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007302 // When using a function that was compiled before: Free old instructions.
7303 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007304 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007306 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7307 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007308 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007310 else
7311 {
7312 if (add_def_function(ufunc) == FAIL)
7313 return FAIL;
7314 new_def_function = TRUE;
7315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316
Bram Moolenaar985116a2020-07-12 17:31:09 +02007317 ufunc->uf_def_status = UF_COMPILING;
7318
Bram Moolenaara80faa82020-04-12 19:37:17 +02007319 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 cctx.ctx_ufunc = ufunc;
7321 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007322 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7324 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7325 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7326 cctx.ctx_type_list = &ufunc->uf_type_list;
7327 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7328 instr = &cctx.ctx_instr;
7329
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007330 // Set the context to the function, it may be compiled when called from
7331 // another script. Set the script version to the most modern one.
7332 // The line number will be set in next_line_from_context().
7333 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7335
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007336 // Make sure error messages are OK.
7337 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7338 if (do_estack_push)
7339 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007340 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007341
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007342 if (ufunc->uf_def_args.ga_len > 0)
7343 {
7344 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007345 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007346 int i;
7347 char_u *arg;
7348 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007349 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007350
7351 // Produce instructions for the default values of optional arguments.
7352 // Store the instruction index in uf_def_arg_idx[] so that we know
7353 // where to start when the function is called, depending on the number
7354 // of arguments.
7355 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7356 if (ufunc->uf_def_arg_idx == NULL)
7357 goto erret;
7358 for (i = 0; i < count; ++i)
7359 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007360 garray_T *stack = &cctx.ctx_type_stack;
7361 type_T *val_type;
7362 int arg_idx = first_def_arg + i;
7363
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007364 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7365 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007366 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007367 goto erret;
7368
7369 // If no type specified use the type of the default value.
7370 // Otherwise check that the default value type matches the
7371 // specified type.
7372 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7373 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007374 {
7375 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007376 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007377 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007378 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7379 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007380 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007381
7382 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007383 goto erret;
7384 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007385 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007386
7387 if (did_set_arg_type)
7388 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007389 }
7390
7391 /*
7392 * Loop over all the lines of the function and generate instructions.
7393 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 for (;;)
7395 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007396 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007397 int starts_with_colon = FALSE;
7398 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007399 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007401 // Bail out on the first error to avoid a flood of errors and report
7402 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007403 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007404 goto erret;
7405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 if (line != NULL && *line == '|')
7407 // the line continues after a '|'
7408 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007409 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007410 && !(*line == '#' && (line == cctx.ctx_line_start
7411 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007413 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 goto erret;
7415 }
7416 else
7417 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007418 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007419 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007420 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 }
7423
Bram Moolenaara80faa82020-04-12 19:37:17 +02007424 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 ea.cmdlinep = &line;
7426 ea.cmd = skipwhite(line);
7427
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007428 // Some things can be recognized by the first character.
7429 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007431 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007432 // "#" starts a comment
7433 line = (char_u *)"";
7434 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007436 case '}':
7437 {
7438 // "}" ends a block scope
7439 scopetype_T stype = cctx.ctx_scope == NULL
7440 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007442 if (stype == BLOCK_SCOPE)
7443 {
7444 compile_endblock(&cctx);
7445 line = ea.cmd;
7446 }
7447 else
7448 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007449 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007450 goto erret;
7451 }
7452 if (line != NULL)
7453 line = skipwhite(ea.cmd + 1);
7454 continue;
7455 }
7456
7457 case '{':
7458 // "{" starts a block scope
7459 // "{'a': 1}->func() is something else
7460 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7461 {
7462 line = compile_block(ea.cmd, &cctx);
7463 continue;
7464 }
7465 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 }
7467
7468 /*
7469 * COMMAND MODIFIERS
7470 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007471 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007472 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7473 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 {
7475 if (errormsg != NULL)
7476 goto erret;
7477 // empty line or comment
7478 line = (char_u *)"";
7479 continue;
7480 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007481 generate_cmdmods(&cctx, &local_cmdmod);
7482 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007484 // Check if there was a colon after the last command modifier or before
7485 // the current position.
7486 for (p = ea.cmd; p >= line; --p)
7487 {
7488 if (*p == ':')
7489 starts_with_colon = TRUE;
7490 if (p < ea.cmd && !VIM_ISWHITE(*p))
7491 break;
7492 }
7493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007495 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007497 {
7498 if (*ea.cmd == '(')
7499 // not for "call()"
7500 ea.cmd = p;
7501 else
7502 ea.cmd = skipwhite(ea.cmd);
7503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007505 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007507 char_u *pskip;
7508
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007509 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007510 // find what follows.
7511 // Skip over "var.member", "var[idx]" and the like.
7512 // Also "&opt = val", "$ENV = val" and "@r = val".
7513 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007514 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007515 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007516 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007518 char_u *var_end;
7519 int oplen;
7520 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521
Bram Moolenaar65821722020-08-02 18:58:54 +02007522 if (ea.cmd[0] == '@')
7523 var_end = ea.cmd + 2;
7524 else
7525 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007526 FNE_CHECK_START | FNE_INCL_BR);
7527 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007528 if (oplen > 0)
7529 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007530 size_t len = p - ea.cmd;
7531
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007532 // Recognize an assignment if we recognize the variable
7533 // name:
7534 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007535 // "local = expr" where "local" is a local var.
7536 // "script = expr" where "script" is a script-local var.
7537 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007538 // "&opt = expr"
7539 // "$ENV = expr"
7540 // "@r = expr"
7541 if (*ea.cmd == '&'
7542 || *ea.cmd == '$'
7543 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007544 || ((len) > 2 && ea.cmd[1] == ':')
7545 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007546 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007547 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007548 || script_var_exists(ea.cmd, len,
7549 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007550 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007551 {
7552 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007553 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007554 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007555 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 }
7558 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007559
7560 if (*ea.cmd == '[')
7561 {
7562 // [var, var] = expr
7563 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7564 if (line == NULL)
7565 goto erret;
7566 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007567 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569 }
7570
7571 /*
7572 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007573 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007575 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007576 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007577 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007578 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007579 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007580 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007581 if (!starts_with_colon)
7582 {
7583 emsg(_(e_colon_required_before_a_range));
7584 goto erret;
7585 }
7586 if (ends_excmd2(line, ea.cmd))
7587 {
7588 // A range without a command: jump to the line.
7589 // TODO: compile to a more efficient command, possibly
7590 // calling parse_cmd_address().
7591 ea.cmdidx = CMD_SIZE;
7592 line = compile_exec(line, &ea, &cctx);
7593 goto nextline;
7594 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007595 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007596 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007597 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007598 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007599 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600
7601 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7602 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007603 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007604 {
7605 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007606 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007607 }
7608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007610 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007612 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007613 iemsg("Command from find_ex_command() not handled");
7614 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 }
7617
Bram Moolenaar3988f642020-08-27 22:43:03 +02007618 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007619 && ea.cmdidx != CMD_elseif
7620 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007621 && ea.cmdidx != CMD_endif
7622 && ea.cmdidx != CMD_endfor
7623 && ea.cmdidx != CMD_endwhile
7624 && ea.cmdidx != CMD_catch
7625 && ea.cmdidx != CMD_finally
7626 && ea.cmdidx != CMD_endtry)
7627 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007628 emsg(_(e_unreachable_code_after_return));
7629 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007630 }
7631
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007632 p = skipwhite(p);
7633 if (ea.cmdidx != CMD_SIZE
7634 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7635 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007636 if (ea.cmdidx >= 0)
7637 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007638 if ((ea.argt & EX_BANG) && *p == '!')
7639 {
7640 ea.forceit = TRUE;
7641 p = skipwhite(p + 1);
7642 }
7643 }
7644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 switch (ea.cmdidx)
7646 {
7647 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007648 ea.arg = p;
7649 line = compile_nested_function(&ea, &cctx);
7650 break;
7651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007653 // TODO: should we allow this, e.g. to declare a global
7654 // function?
7655 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656 goto erret;
7657
7658 case CMD_return:
7659 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007660 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 break;
7662
7663 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007664 emsg(_(e_cannot_use_let_in_vim9_script));
7665 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007666 case CMD_var:
7667 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 case CMD_const:
7669 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007670 if (line == p)
7671 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 break;
7673
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007674 case CMD_unlet:
7675 case CMD_unlockvar:
7676 case CMD_lockvar:
7677 line = compile_unletlock(p, &ea, &cctx);
7678 break;
7679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 case CMD_import:
7681 line = compile_import(p, &cctx);
7682 break;
7683
7684 case CMD_if:
7685 line = compile_if(p, &cctx);
7686 break;
7687 case CMD_elseif:
7688 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007689 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690 break;
7691 case CMD_else:
7692 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007693 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 break;
7695 case CMD_endif:
7696 line = compile_endif(p, &cctx);
7697 break;
7698
7699 case CMD_while:
7700 line = compile_while(p, &cctx);
7701 break;
7702 case CMD_endwhile:
7703 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007704 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 break;
7706
7707 case CMD_for:
7708 line = compile_for(p, &cctx);
7709 break;
7710 case CMD_endfor:
7711 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007712 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 break;
7714 case CMD_continue:
7715 line = compile_continue(p, &cctx);
7716 break;
7717 case CMD_break:
7718 line = compile_break(p, &cctx);
7719 break;
7720
7721 case CMD_try:
7722 line = compile_try(p, &cctx);
7723 break;
7724 case CMD_catch:
7725 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007726 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 break;
7728 case CMD_finally:
7729 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007730 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 break;
7732 case CMD_endtry:
7733 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007734 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 break;
7736 case CMD_throw:
7737 line = compile_throw(p, &cctx);
7738 break;
7739
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007740 case CMD_eval:
7741 if (compile_expr0(&p, &cctx) == FAIL)
7742 goto erret;
7743
Bram Moolenaar3988f642020-08-27 22:43:03 +02007744 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007745 generate_instr_drop(&cctx, ISN_DROP, 1);
7746
7747 line = skipwhite(p);
7748 break;
7749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007752 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007753 case CMD_echomsg:
7754 case CMD_echoerr:
7755 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007756 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007758 case CMD_put:
7759 ea.cmd = cmd;
7760 line = compile_put(p, &ea, &cctx);
7761 break;
7762
Bram Moolenaar3988f642020-08-27 22:43:03 +02007763 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007764
Bram Moolenaarae616492020-07-28 20:07:27 +02007765 case CMD_append:
7766 case CMD_change:
7767 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007768 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007769 case CMD_xit:
7770 not_in_vim9(&ea);
7771 goto erret;
7772
Bram Moolenaar002262f2020-07-08 17:47:57 +02007773 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007774 if (cctx.ctx_skip != SKIP_YES)
7775 {
7776 semsg(_(e_invalid_command_str), ea.cmd);
7777 goto erret;
7778 }
7779 // We don't check for a next command here.
7780 line = (char_u *)"";
7781 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007784 if (cctx.ctx_skip == SKIP_YES)
7785 {
7786 // We don't check for a next command here.
7787 line = (char_u *)"";
7788 }
7789 else
7790 {
7791 // Not recognized, execute with do_cmdline_cmd().
7792 ea.arg = p;
7793 line = compile_exec(line, &ea, &cctx);
7794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 break;
7796 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007797nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 if (line == NULL)
7799 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007800 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007802 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007803 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 if (cctx.ctx_type_stack.ga_len < 0)
7806 {
7807 iemsg("Type stack underflow");
7808 goto erret;
7809 }
7810 }
7811
7812 if (cctx.ctx_scope != NULL)
7813 {
7814 if (cctx.ctx_scope->se_type == IF_SCOPE)
7815 emsg(_(e_endif));
7816 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7817 emsg(_(e_endwhile));
7818 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7819 emsg(_(e_endfor));
7820 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007821 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 goto erret;
7823 }
7824
Bram Moolenaarefd88552020-06-18 20:50:10 +02007825 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 {
7827 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7828 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007829 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 goto erret;
7831 }
7832
7833 // Return zero if there is no return at the end.
7834 generate_PUSHNR(&cctx, 0);
7835 generate_instr(&cctx, ISN_RETURN);
7836 }
7837
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007838 {
7839 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7840 + ufunc->uf_dfunc_idx;
7841 dfunc->df_deleted = FALSE;
7842 dfunc->df_instr = instr->ga_data;
7843 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007844 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007845 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007846 if (cctx.ctx_outer_used)
7847 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007848 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850
7851 ret = OK;
7852
7853erret:
7854 if (ret == FAIL)
7855 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007856 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7858 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007859
7860 for (idx = 0; idx < instr->ga_len; ++idx)
7861 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007863
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007864 // If using the last entry in the table and it was added above, we
7865 // might as well remove it.
7866 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007867 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007868 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007869 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007870 ufunc->uf_dfunc_idx = 0;
7871 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007872 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007873
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007874 while (cctx.ctx_scope != NULL)
7875 drop_scope(&cctx);
7876
Bram Moolenaar20431c92020-03-20 18:39:46 +01007877 // Don't execute this function body.
7878 ga_clear_strings(&ufunc->uf_lines);
7879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007880 if (errormsg != NULL)
7881 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007882 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007883 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 }
7885
7886 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007887 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007888 if (do_estack_push)
7889 estack_pop();
7890
Bram Moolenaar20431c92020-03-20 18:39:46 +01007891 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007892 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007894 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895}
7896
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007897 void
7898set_function_type(ufunc_T *ufunc)
7899{
7900 int varargs = ufunc->uf_va_name != NULL;
7901 int argcount = ufunc->uf_args.ga_len;
7902
7903 // Create a type for the function, with the return type and any
7904 // argument types.
7905 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7906 // The type is included in "tt_args".
7907 if (argcount > 0 || varargs)
7908 {
7909 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7910 argcount, &ufunc->uf_type_list);
7911 // Add argument types to the function type.
7912 if (func_type_add_arg_types(ufunc->uf_func_type,
7913 argcount + varargs,
7914 &ufunc->uf_type_list) == FAIL)
7915 return;
7916 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7917 ufunc->uf_func_type->tt_min_argcount =
7918 argcount - ufunc->uf_def_args.ga_len;
7919 if (ufunc->uf_arg_types == NULL)
7920 {
7921 int i;
7922
7923 // lambda does not have argument types.
7924 for (i = 0; i < argcount; ++i)
7925 ufunc->uf_func_type->tt_args[i] = &t_any;
7926 }
7927 else
7928 mch_memmove(ufunc->uf_func_type->tt_args,
7929 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7930 if (varargs)
7931 {
7932 ufunc->uf_func_type->tt_args[argcount] =
7933 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7934 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7935 }
7936 }
7937 else
7938 // No arguments, can use a predefined type.
7939 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7940 argcount, &ufunc->uf_type_list);
7941}
7942
7943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944/*
7945 * Delete an instruction, free what it contains.
7946 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007947 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948delete_instr(isn_T *isn)
7949{
7950 switch (isn->isn_type)
7951 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007952 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007953 case ISN_EXEC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007954 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955 case ISN_LOADENV:
7956 case ISN_LOADG:
7957 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007958 case ISN_LOADT:
7959 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007961 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007962 case ISN_PUSHS:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007963 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007964 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007966 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007967 case ISN_STOREW:
7968 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969 vim_free(isn->isn_arg.string);
7970 break;
7971
7972 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007973 case ISN_STORES:
7974 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975 break;
7976
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007977 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007978 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007979 vim_free(isn->isn_arg.unlet.ul_name);
7980 break;
7981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 case ISN_STOREOPT:
7983 vim_free(isn->isn_arg.storeopt.so_name);
7984 break;
7985
7986 case ISN_PUSHBLOB: // push blob isn_arg.blob
7987 blob_unref(isn->isn_arg.blob);
7988 break;
7989
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007990 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007992 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007993#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007994 break;
7995
7996 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007997#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007998 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007999#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008000 break;
8001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002 case ISN_UCALL:
8003 vim_free(isn->isn_arg.ufunc.cuf_name);
8004 break;
8005
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008006 case ISN_FUNCREF:
8007 {
8008 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8009 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008010
8011 if (func_name_refcount(dfunc->df_ufunc->uf_name))
8012 func_ptr_unref(dfunc->df_ufunc);
8013 }
8014 break;
8015
8016 case ISN_DCALL:
8017 {
8018 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8019 + isn->isn_arg.dfunc.cdf_idx;
8020
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008021 if (dfunc->df_ufunc != NULL
8022 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008023 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008024 }
8025 break;
8026
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008027 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008028 {
8029 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8030 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8031
8032 if (ufunc != NULL)
8033 {
8034 // Clear uf_dfunc_idx so that the function is deleted.
8035 clear_def_function(ufunc);
8036 ufunc->uf_dfunc_idx = 0;
8037 func_ptr_unref(ufunc);
8038 }
8039
8040 vim_free(lambda);
8041 vim_free(isn->isn_arg.newfunc.nf_global);
8042 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008043 break;
8044
Bram Moolenaar5e654232020-09-16 15:22:00 +02008045 case ISN_CHECKTYPE:
8046 free_type(isn->isn_arg.type.ct_type);
8047 break;
8048
Bram Moolenaar02194d22020-10-24 23:08:38 +02008049 case ISN_CMDMOD:
8050 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8051 ->cmod_filter_regmatch.regprog);
8052 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8053 break;
8054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 case ISN_2BOOL:
8056 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008057 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 case ISN_ADDBLOB:
8059 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008060 case ISN_ANYINDEX:
8061 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008062 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008063 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008065 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008067 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068 case ISN_COMPAREANY:
8069 case ISN_COMPAREBLOB:
8070 case ISN_COMPAREBOOL:
8071 case ISN_COMPAREDICT:
8072 case ISN_COMPAREFLOAT:
8073 case ISN_COMPAREFUNC:
8074 case ISN_COMPARELIST:
8075 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 case ISN_COMPARESPECIAL:
8077 case ISN_COMPARESTRING:
8078 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008079 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080 case ISN_DROP:
8081 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008082 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008083 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008085 case ISN_EXECCONCAT:
8086 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008088 case ISN_GETITEM:
8089 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008090 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008091 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008092 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008094 case ISN_LOADBDICT:
8095 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008096 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008098 case ISN_LOADSCRIPT:
8099 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008101 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008102 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008103 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 case ISN_NEGATENR:
8105 case ISN_NEWDICT:
8106 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008108 case ISN_OPFLOAT:
8109 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008110 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008111 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008112 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 case ISN_PUSHF:
8114 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008116 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008118 case ISN_SHUFFLE:
8119 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008121 case ISN_STOREDICT:
8122 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008123 case ISN_STORENR:
8124 case ISN_STOREOUTER:
8125 case ISN_STOREREG:
8126 case ISN_STORESCRIPT:
8127 case ISN_STOREV:
8128 case ISN_STRINDEX:
8129 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008130 case ISN_THROW:
8131 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008132 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 // nothing allocated
8134 break;
8135 }
8136}
8137
8138/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01008139 * Free all instructions for "dfunc".
8140 */
8141 static void
8142delete_def_function_contents(dfunc_T *dfunc)
8143{
8144 int idx;
8145
8146 ga_clear(&dfunc->df_def_args_isn);
8147
8148 if (dfunc->df_instr != NULL)
8149 {
8150 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8151 delete_instr(dfunc->df_instr + idx);
8152 VIM_CLEAR(dfunc->df_instr);
8153 }
8154
8155 dfunc->df_deleted = TRUE;
8156}
8157
8158/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008159 * When a user function is deleted, clear the contents of any associated def
8160 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161 */
8162 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008163clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008165 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 {
8167 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8168 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169
Bram Moolenaar20431c92020-03-20 18:39:46 +01008170 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008171 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 }
8173}
8174
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008175/*
8176 * Used when a user function is about to be deleted: remove the pointer to it.
8177 * The entry in def_functions is then unused.
8178 */
8179 void
8180unlink_def_function(ufunc_T *ufunc)
8181{
8182 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
8183
8184 dfunc->df_ufunc = NULL;
8185}
8186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008187#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008188/*
8189 * Free all functions defined with ":def".
8190 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 void
8192free_def_functions(void)
8193{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008194 int idx;
8195
8196 for (idx = 0; idx < def_functions.ga_len; ++idx)
8197 {
8198 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8199
8200 delete_def_function_contents(dfunc);
8201 }
8202
8203 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008204}
8205#endif
8206
8207
8208#endif // FEAT_EVAL