blob: ebe48548e2a9c2358504d53ceb1aeb97d89b52c6 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264 * Lookup a script-local variable in the current script, possibly defined in a
265 * block that contains the function "cctx->ctx_ufunc".
266 * "cctx" is NULL at the script level.
267 * if "len" is <= 0 "name" must be NUL terminated.
268 * Return NULL when not found.
269 */
270 static sallvar_T *
271find_script_var(char_u *name, size_t len, cctx_T *cctx)
272{
273 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
274 hashitem_T *hi;
275 int cc;
276 sallvar_T *sav;
277 ufunc_T *ufunc;
278
279 // Find the list of all script variables with the right name.
280 if (len > 0)
281 {
282 cc = name[len];
283 name[len] = NUL;
284 }
285 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
286 if (len > 0)
287 name[len] = cc;
288 if (HASHITEM_EMPTY(hi))
289 return NULL;
290
291 sav = HI2SAV(hi);
292 if (sav->sav_block_id == 0 || cctx == NULL)
293 // variable defined in the script scope or not in a function.
294 return sav;
295
296 // Go over the variables with this name and find one that was visible
297 // from the function.
298 ufunc = cctx->ctx_ufunc;
299 while (sav != NULL)
300 {
301 int idx;
302
303 // Go over the blocks that this function was defined in. If the
304 // variable block ID matches it was visible to the function.
305 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
306 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
307 return sav;
308 sav = sav->sav_next;
309 }
310
311 return NULL;
312}
313
314/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200315 * Returnd TRUE if the script context is Vim9 script.
316 */
317 static int
318script_is_vim9()
319{
320 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
321}
322
323/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200324 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200325 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
326 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 * Returns OK or FAIL.
329 */
330 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200335 if (current_sctx.sc_sid <= 0)
336 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 is_vim9_script = script_is_vim9();
338 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200339 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340 if (is_vim9_script)
341 {
342 // Check script variables that were visible where the function was
343 // defined.
344 if (find_script_var(name, len, cctx) != NULL)
345 return OK;
346 }
347 else
348 {
349 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
350 dictitem_T *di;
351 int cc;
352
353 // Check script variables that are currently visible
354 cc = name[len];
355 name[len] = NUL;
356 di = find_var_in_ht(ht, 0, name, TRUE);
357 name[len] = cc;
358 if (di != NULL)
359 return OK;
360 }
361
362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363}
364
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365/*
366 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200367 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200368 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 * Return FAIL and give an error if it defined.
370 */
371 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 int c = p[len];
375 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200376
377 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200378 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200380 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100384 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200385 // A local or script-local function can shadow a global function.
386 if (ufunc == NULL || !func_is_global(ufunc)
387 || (p[0] == 'g' && p[1] == ':'))
388 {
389 p[len] = c;
390 semsg(_(e_name_already_defined_str), p);
391 return FAIL;
392 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200394 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100395 return OK;
396}
397
Bram Moolenaar65b95452020-07-19 14:03:09 +0200398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200455 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200463 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 */
465 static int
466may_generate_2STRING(int offset, cctx_T *cctx)
467{
468 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 switch ((*type)->tt_type)
474 {
475 // nothing to be done
476 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 // conversion possible
479 case VAR_SPECIAL:
480 case VAR_BOOL:
481 case VAR_NUMBER:
482 case VAR_FLOAT:
483 break;
484
485 // conversion possible (with runtime check)
486 case VAR_ANY:
487 case VAR_UNKNOWN:
488 isntype = ISN_2STRING_ANY;
489 break;
490
491 // conversion not possible
492 case VAR_VOID:
493 case VAR_BLOB:
494 case VAR_FUNC:
495 case VAR_PARTIAL:
496 case VAR_LIST:
497 case VAR_DICT:
498 case VAR_JOB:
499 case VAR_CHANNEL:
500 to_string_error((*type)->tt_type);
501 return FAIL;
502 }
503
504 *type = &t_string;
505 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507 isn->isn_arg.number = offset;
508
509 return OK;
510}
511
512 static int
513check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
514{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200517 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 {
519 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return FAIL;
524 }
525 return OK;
526}
527
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200528 static int
529generate_add_instr(
530 cctx_T *cctx,
531 vartype_T vartype,
532 type_T *type1,
533 type_T *type2)
534{
535 isn_T *isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR
537 : vartype == VAR_LIST ? ISN_ADDLIST
538 : vartype == VAR_BLOB ? ISN_ADDBLOB
539#ifdef FEAT_FLOAT
540 : vartype == VAR_FLOAT ? ISN_OPFLOAT
541#endif
542 : ISN_OPANY, 1);
543
544 if (vartype != VAR_LIST && vartype != VAR_BLOB
545 && type1->tt_type != VAR_ANY
546 && type2->tt_type != VAR_ANY
547 && check_number_or_float(
548 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
549 return FAIL;
550
551 if (isn != NULL)
552 isn->isn_arg.op.op_type = EXPR_ADD;
553 return isn == NULL ? FAIL : OK;
554}
555
556/*
557 * Get the type to use for an instruction for an operation on "type1" and
558 * "type2". If they are matching use a type-specific instruction. Otherwise
559 * fall back to runtime type checking.
560 */
561 static vartype_T
562operator_type(type_T *type1, type_T *type2)
563{
564 if (type1->tt_type == type2->tt_type
565 && (type1->tt_type == VAR_NUMBER
566 || type1->tt_type == VAR_LIST
567#ifdef FEAT_FLOAT
568 || type1->tt_type == VAR_FLOAT
569#endif
570 || type1->tt_type == VAR_BLOB))
571 return type1->tt_type;
572 return VAR_ANY;
573}
574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575/*
576 * Generate an instruction with two arguments. The instruction depends on the
577 * type of the arguments.
578 */
579 static int
580generate_two_op(cctx_T *cctx, char_u *op)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583 type_T *type1;
584 type_T *type2;
585 vartype_T vartype;
586 isn_T *isn;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_OK_IF_SKIP(cctx);
589
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200590 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
592 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 switch (*op)
596 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200597 case '+':
598 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 break;
601
602 case '-':
603 case '*':
604 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
605 op) == FAIL)
606 return FAIL;
607 if (vartype == VAR_NUMBER)
608 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
609#ifdef FEAT_FLOAT
610 else if (vartype == VAR_FLOAT)
611 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
612#endif
613 else
614 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
615 if (isn != NULL)
616 isn->isn_arg.op.op_type = *op == '*'
617 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
618 break;
619
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200622 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 && type2->tt_type != VAR_NUMBER))
624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200625 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 }
628 isn = generate_instr_drop(cctx,
629 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = EXPR_REM;
632 break;
633 }
634
635 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 type_T *type = &t_any;
639
640#ifdef FEAT_FLOAT
641 // float+number and number+float results in float
642 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
643 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
644 type = &t_float;
645#endif
646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
647 }
648
649 return OK;
650}
651
652/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200653 * Get the instruction to use for comparing "type1" with "type2"
654 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200656 static isntype_T
657get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658{
659 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 if (type1 == VAR_UNKNOWN)
662 type1 = VAR_ANY;
663 if (type2 == VAR_UNKNOWN)
664 type2 = VAR_ANY;
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (type1 == type2)
667 {
668 switch (type1)
669 {
670 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
671 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
672 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
673 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
674 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
675 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
676 case VAR_LIST: isntype = ISN_COMPARELIST; break;
677 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
678 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 default: isntype = ISN_COMPAREANY; break;
680 }
681 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
684 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
685 isntype = ISN_COMPAREANY;
686
687 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
688 && (isntype == ISN_COMPAREBOOL
689 || isntype == ISN_COMPARESPECIAL
690 || isntype == ISN_COMPARENR
691 || isntype == ISN_COMPAREFLOAT))
692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200693 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200695 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 }
697 if (isntype == ISN_DROP
698 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
699 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
700 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
701 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
702 && exptype != EXPR_IS && exptype != EXPR_ISNOT
703 && (type1 == VAR_BLOB || type2 == VAR_BLOB
704 || type1 == VAR_LIST || type2 == VAR_LIST))))
705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200706 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return isntype;
711}
712
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200713 int
714check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
715{
716 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
717 return FAIL;
718 return OK;
719}
720
Bram Moolenaara5565e42020-05-09 15:44:01 +0200721/*
722 * Generate an ISN_COMPARE* instruction with a boolean result.
723 */
724 static int
725generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
726{
727 isntype_T isntype;
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730 vartype_T type1;
731 vartype_T type2;
732
733 RETURN_OK_IF_SKIP(cctx);
734
735 // Get the known type of the two items on the stack. If they are matching
736 // use a type-specific instruction. Otherwise fall back to runtime type
737 // checking.
738 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
739 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
740 isntype = get_compare_isn(exptype, type1, type2);
741 if (isntype == ISN_DROP)
742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if ((isn = generate_instr(cctx, isntype)) == NULL)
745 return FAIL;
746 isn->isn_arg.op.op_type = exptype;
747 isn->isn_arg.op.op_ic = ic;
748
749 // takes two arguments, puts one bool back
750 if (stack->ga_len >= 2)
751 {
752 --stack->ga_len;
753 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
754 }
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_2BOOL instruction.
761 */
762 static int
763generate_2BOOL(cctx_T *cctx, int invert)
764{
765 isn_T *isn;
766 garray_T *stack = &cctx->ctx_type_stack;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
770 return FAIL;
771 isn->isn_arg.number = invert;
772
773 // type becomes bool
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775
776 return OK;
777}
778
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200779/*
780 * Generate an ISN_COND2BOOL instruction.
781 */
782 static int
783generate_COND2BOOL(cctx_T *cctx)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
788 RETURN_OK_IF_SKIP(cctx);
789 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
790 return FAIL;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200799generate_TYPECHECK(
800 cctx_T *cctx,
801 type_T *expected,
802 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
Bram Moolenaar080457c2020-03-03 21:53:32 +0100807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
809 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200810 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 isn->isn_arg.type.ct_off = offset;
812
Bram Moolenaar5e654232020-09-16 15:22:00 +0200813 // type becomes expected
814 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 return OK;
817}
818
819/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200820 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821 * used. Return FALSE if the types will never match.
822 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100823 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200824use_typecheck(type_T *actual, type_T *expected)
825{
826 if (actual->tt_type == VAR_ANY
827 || actual->tt_type == VAR_UNKNOWN
828 || (actual->tt_type == VAR_FUNC
829 && (expected->tt_type == VAR_FUNC
830 || expected->tt_type == VAR_PARTIAL)
831 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
832 return TRUE;
833 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
834 && actual->tt_type == expected->tt_type)
835 // This takes care of a nested list or dict.
836 return use_typecheck(actual->tt_member, expected->tt_member);
837 return FALSE;
838}
839
840/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200843 * - "actual" is a type that can be "expected" type: add a runtime check; or
844 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200845 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
846 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 */
848 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200849need_type(
850 type_T *actual,
851 type_T *expected,
852 int offset,
853 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200854 int silent,
855 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200857 if (expected == &t_bool && actual != &t_bool
858 && (actual->tt_flags & TTFLAG_BOOL_OK))
859 {
860 // Using "0", "1" or the result of an expression with "&&" or "||" as a
861 // boolean is OK but requires a conversion.
862 generate_2BOOL(cctx, FALSE);
863 return OK;
864 }
865
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200866 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200868
869 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 // If it's a constant a runtime check makes no sense.
871 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 generate_TYPECHECK(cctx, expected, offset);
874 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200875 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876
877 if (!silent)
878 type_mismatch(expected, actual);
879 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880}
881
882/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100883 * Check that the top of the type stack has a type that can be used as a
884 * condition. Give an error and return FAIL if not.
885 */
886 static int
887bool_on_stack(cctx_T *cctx)
888{
889 garray_T *stack = &cctx->ctx_type_stack;
890 type_T *type;
891
892 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
893 if (type == &t_bool)
894 return OK;
895
896 if (type == &t_any || type == &t_number)
897 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
898 // This requires a runtime type check.
899 return generate_COND2BOOL(cctx);
900
901 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
902}
903
904/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 * Generate an ISN_PUSHNR instruction.
906 */
907 static int
908generate_PUSHNR(cctx_T *cctx, varnumber_T number)
909{
910 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200911 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200918 if (number == 0 || number == 1)
919 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200920 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200921
922 // A 0 or 1 number can also be used as a bool.
923 if (type != NULL)
924 {
925 type->tt_type = VAR_NUMBER;
926 type->tt_flags = TTFLAG_BOOL_OK;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
928 }
929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 return OK;
931}
932
933/*
934 * Generate an ISN_PUSHBOOL instruction.
935 */
936 static int
937generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
940
Bram Moolenaar080457c2020-03-03 21:53:32 +0100941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
943 return FAIL;
944 isn->isn_arg.number = number;
945
946 return OK;
947}
948
949/*
950 * Generate an ISN_PUSHSPEC instruction.
951 */
952 static int
953generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
954{
955 isn_T *isn;
956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
959 return FAIL;
960 isn->isn_arg.number = number;
961
962 return OK;
963}
964
965#ifdef FEAT_FLOAT
966/*
967 * Generate an ISN_PUSHF instruction.
968 */
969 static int
970generate_PUSHF(cctx_T *cctx, float_T fnumber)
971{
972 isn_T *isn;
973
Bram Moolenaar080457c2020-03-03 21:53:32 +0100974 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
976 return FAIL;
977 isn->isn_arg.fnumber = fnumber;
978
979 return OK;
980}
981#endif
982
983/*
984 * Generate an ISN_PUSHS instruction.
985 * Consumes "str".
986 */
987 static int
988generate_PUSHS(cctx_T *cctx, char_u *str)
989{
990 isn_T *isn;
991
Bram Moolenaar080457c2020-03-03 21:53:32 +0100992 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
994 return FAIL;
995 isn->isn_arg.string = str;
996
997 return OK;
998}
999
1000/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001001 * Generate an ISN_PUSHCHANNEL instruction.
1002 * Consumes "channel".
1003 */
1004 static int
1005generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1006{
1007 isn_T *isn;
1008
Bram Moolenaar080457c2020-03-03 21:53:32 +01001009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001010 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1011 return FAIL;
1012 isn->isn_arg.channel = channel;
1013
1014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHJOB instruction.
1019 * Consumes "job".
1020 */
1021 static int
1022generate_PUSHJOB(cctx_T *cctx, job_T *job)
1023{
1024 isn_T *isn;
1025
Bram Moolenaar080457c2020-03-03 21:53:32 +01001026 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001027 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 return FAIL;
1029 isn->isn_arg.job = job;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 * Generate an ISN_PUSHBLOB instruction.
1036 * Consumes "blob".
1037 */
1038 static int
1039generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.blob = blob;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001052 * Generate an ISN_PUSHFUNC instruction with name "name".
1053 * Consumes "name".
1054 */
1055 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001056generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001057{
1058 isn_T *isn;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001061 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001063 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001064
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 * Generate an ISN_GETITEM instruction with "index".
1070 */
1071 static int
1072generate_GETITEM(cctx_T *cctx, int index)
1073{
1074 isn_T *isn;
1075 garray_T *stack = &cctx->ctx_type_stack;
1076 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1077 type_T *item_type = &t_any;
1078
1079 RETURN_OK_IF_SKIP(cctx);
1080
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001081 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001083 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 emsg(_(e_listreq));
1085 return FAIL;
1086 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = index;
1091
1092 // add the item type to the type stack
1093 if (ga_grow(stack, 1) == FAIL)
1094 return FAIL;
1095 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1096 ++stack->ga_len;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001101 * Generate an ISN_SLICE instruction with "count".
1102 */
1103 static int
1104generate_SLICE(cctx_T *cctx, int count)
1105{
1106 isn_T *isn;
1107
1108 RETURN_OK_IF_SKIP(cctx);
1109 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_CHECKLEN instruction with "min_len".
1117 */
1118 static int
1119generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124
1125 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.checklen.cl_min_len = min_len;
1128 isn->isn_arg.checklen.cl_more_OK = more_OK;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 * Generate an ISN_STORE instruction.
1135 */
1136 static int
1137generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1138{
1139 isn_T *isn;
1140
Bram Moolenaar080457c2020-03-03 21:53:32 +01001141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1143 return FAIL;
1144 if (name != NULL)
1145 isn->isn_arg.string = vim_strsave(name);
1146 else
1147 isn->isn_arg.number = idx;
1148
1149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1154 */
1155 static int
1156generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1157{
1158 isn_T *isn;
1159
Bram Moolenaar080457c2020-03-03 21:53:32 +01001160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1162 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001163 isn->isn_arg.storenr.stnr_idx = idx;
1164 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_STOREOPT instruction
1171 */
1172 static int
1173generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001178 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 return FAIL;
1180 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1181 isn->isn_arg.storeopt.so_flags = opt_flags;
1182
1183 return OK;
1184}
1185
1186/*
1187 * Generate an ISN_LOAD or similar instruction.
1188 */
1189 static int
1190generate_LOAD(
1191 cctx_T *cctx,
1192 isntype_T isn_type,
1193 int idx,
1194 char_u *name,
1195 type_T *type)
1196{
1197 isn_T *isn;
1198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1201 return FAIL;
1202 if (name != NULL)
1203 isn->isn_arg.string = vim_strsave(name);
1204 else
1205 isn->isn_arg.number = idx;
1206
1207 return OK;
1208}
1209
1210/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001211 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001212 */
1213 static int
1214generate_LOADV(
1215 cctx_T *cctx,
1216 char_u *name,
1217 int error)
1218{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001219 int di_flags;
1220 int vidx = find_vim_var(name, &di_flags);
1221 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 if (vidx < 0)
1225 {
1226 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001227 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228 return FAIL;
1229 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001230 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001232 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001233}
1234
1235/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001236 * Generate an ISN_UNLET instruction.
1237 */
1238 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001239generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001240{
1241 isn_T *isn;
1242
1243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001244 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001245 return FAIL;
1246 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1247 isn->isn_arg.unlet.ul_forceit = forceit;
1248
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001253 * Generate an ISN_LOCKCONST instruction.
1254 */
1255 static int
1256generate_LOCKCONST(cctx_T *cctx)
1257{
1258 isn_T *isn;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1262 return FAIL;
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_LOADS instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 int sid,
1275 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 if (isn_type == ISN_LOADS)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1287 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1294 */
1295 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int sid,
1300 int idx,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (isn_type == ISN_LOADSCRIPT)
1307 isn = generate_instr_type(cctx, isn_type, type);
1308 else
1309 isn = generate_instr_drop(cctx, isn_type, 1);
1310 if (isn == NULL)
1311 return FAIL;
1312 isn->isn_arg.script.script_sid = sid;
1313 isn->isn_arg.script.script_idx = idx;
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWLIST instruction.
1319 */
1320 static int
1321generate_NEWLIST(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
Bram Moolenaar127542b2020-08-09 17:22:04 +02001333 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001334 if (count == 0)
1335 member = &t_void;
1336 else
1337 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001338 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1339 cctx->ctx_type_list);
1340 type = get_list_type(member, cctx->ctx_type_list);
1341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 // drop the value types
1343 stack->ga_len -= count;
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // add the list type to the type stack
1346 if (ga_grow(stack, 1) == FAIL)
1347 return FAIL;
1348 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_NEWDICT instruction.
1356 */
1357 static int
1358generate_NEWDICT(cctx_T *cctx, int count)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 type_T *type;
1363 type_T *member;
1364
Bram Moolenaar080457c2020-03-03 21:53:32 +01001365 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.number = count;
1369
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001370 if (count == 0)
1371 member = &t_void;
1372 else
1373 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001374 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1375 cctx->ctx_type_list);
1376 type = get_dict_type(member, cctx->ctx_type_list);
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 // drop the key and value types
1379 stack->ga_len -= 2 * count;
1380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 // add the dict type to the type stack
1382 if (ga_grow(stack, 1) == FAIL)
1383 return FAIL;
1384 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
1391 * Generate an ISN_FUNCREF instruction.
1392 */
1393 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001394generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395{
1396 isn_T *isn;
1397 garray_T *stack = &cctx->ctx_type_stack;
1398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1401 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001402 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001403 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 if (ga_grow(stack, 1) == FAIL)
1406 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001407 ((type_T **)stack->ga_data)[stack->ga_len] =
1408 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 ++stack->ga_len;
1410
1411 return OK;
1412}
1413
1414/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001415 * Generate an ISN_NEWFUNC instruction.
1416 */
1417 static int
1418generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1419{
1420 isn_T *isn;
1421 char_u *name;
1422
1423 RETURN_OK_IF_SKIP(cctx);
1424 name = vim_strsave(lambda_name);
1425 if (name == NULL)
1426 return FAIL;
1427 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1428 return FAIL;
1429 isn->isn_arg.newfunc.nf_lambda = name;
1430 isn->isn_arg.newfunc.nf_global = func_name;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 * Generate an ISN_JUMP instruction.
1437 */
1438 static int
1439generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1440{
1441 isn_T *isn;
1442 garray_T *stack = &cctx->ctx_type_stack;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1446 return FAIL;
1447 isn->isn_arg.jump.jump_when = when;
1448 isn->isn_arg.jump.jump_where = where;
1449
1450 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1451 --stack->ga_len;
1452
1453 return OK;
1454}
1455
1456 static int
1457generate_FOR(cctx_T *cctx, int loop_idx)
1458{
1459 isn_T *isn;
1460 garray_T *stack = &cctx->ctx_type_stack;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1464 return FAIL;
1465 isn->isn_arg.forloop.for_idx = loop_idx;
1466
1467 if (ga_grow(stack, 1) == FAIL)
1468 return FAIL;
1469 // type doesn't matter, will be stored next
1470 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1471 ++stack->ga_len;
1472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001478 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Return FAIL if the number of arguments is wrong.
1480 */
1481 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001482generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001486 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001487 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488
Bram Moolenaar080457c2020-03-03 21:53:32 +01001489 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001490 argoff = check_internal_func(func_idx, argcount);
1491 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 return FAIL;
1493
Bram Moolenaar389df252020-07-09 21:20:47 +02001494 if (method_call && argoff > 1)
1495 {
1496 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1497 return FAIL;
1498 isn->isn_arg.shuffle.shfl_item = argcount;
1499 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1500 }
1501
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001502 if (argcount > 0)
1503 {
1504 // Check the types of the arguments.
1505 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1506 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001507 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001508 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.bfunc.cbf_idx = func_idx;
1513 isn->isn_arg.bfunc.cbf_argcount = argcount;
1514
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 // Drop the argument types and push the return type.
1516 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 if (ga_grow(stack, 1) == FAIL)
1518 return FAIL;
1519 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001520 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001521 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 return OK;
1524}
1525
1526/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001527 * Generate an ISN_LISTAPPEND instruction. Works like add().
1528 * Argument count is already checked.
1529 */
1530 static int
1531generate_LISTAPPEND(cctx_T *cctx)
1532{
1533 garray_T *stack = &cctx->ctx_type_stack;
1534 type_T *list_type;
1535 type_T *item_type;
1536 type_T *expected;
1537
1538 // Caller already checked that list_type is a list.
1539 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1540 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1541 expected = list_type->tt_member;
1542 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001553 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1554 * Argument count is already checked.
1555 */
1556 static int
1557generate_BLOBAPPEND(cctx_T *cctx)
1558{
1559 garray_T *stack = &cctx->ctx_type_stack;
1560 type_T *item_type;
1561
1562 // Caller already checked that blob_type is a blob.
1563 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1564 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1565 return FAIL;
1566
1567 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1568 return FAIL;
1569
1570 --stack->ga_len; // drop the argument
1571 return OK;
1572}
1573
1574/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 * Generate an ISN_DCALL or ISN_UCALL instruction.
1576 * Return FAIL if the number of arguments is wrong.
1577 */
1578 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001579generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580{
1581 isn_T *isn;
1582 garray_T *stack = &cctx->ctx_type_stack;
1583 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001584 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585
Bram Moolenaar080457c2020-03-03 21:53:32 +01001586 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (argcount > regular_args && !has_varargs(ufunc))
1588 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001589 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return FAIL;
1591 }
1592 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1593 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001594 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 return FAIL;
1596 }
1597
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001598 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 {
1604 type_T *expected;
1605 type_T *actual;
1606
1607 if (i < regular_args)
1608 {
1609 if (ufunc->uf_arg_types == NULL)
1610 continue;
1611 expected = ufunc->uf_arg_types[i];
1612 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001613 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1614 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001615 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001616 else
1617 expected = ufunc->uf_va_type->tt_member;
1618 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001619 if (need_type(actual, expected, -argcount + i, cctx,
1620 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001621 {
1622 arg_type_mismatch(expected, actual, i + 1);
1623 return FAIL;
1624 }
1625 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001626 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001627 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1628 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001629 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001630 }
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001634 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001636 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 {
1638 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1639 isn->isn_arg.dfunc.cdf_argcount = argcount;
1640 }
1641 else
1642 {
1643 // A user function may be deleted and redefined later, can't use the
1644 // ufunc pointer, need to look it up again at runtime.
1645 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1646 isn->isn_arg.ufunc.cuf_argcount = argcount;
1647 }
1648
1649 stack->ga_len -= argcount; // drop the arguments
1650 if (ga_grow(stack, 1) == FAIL)
1651 return FAIL;
1652 // add return value
1653 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1654 ++stack->ga_len;
1655
1656 return OK;
1657}
1658
1659/*
1660 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1661 */
1662 static int
1663generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1664{
1665 isn_T *isn;
1666 garray_T *stack = &cctx->ctx_type_stack;
1667
Bram Moolenaar080457c2020-03-03 21:53:32 +01001668 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1672 isn->isn_arg.ufunc.cuf_argcount = argcount;
1673
1674 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001675 if (ga_grow(stack, 1) == FAIL)
1676 return FAIL;
1677 // add return value
1678 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1679 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
1681 return OK;
1682}
1683
1684/*
1685 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001686 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 */
1688 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001689generate_PCALL(
1690 cctx_T *cctx,
1691 int argcount,
1692 char_u *name,
1693 type_T *type,
1694 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695{
1696 isn_T *isn;
1697 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001698 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
Bram Moolenaar080457c2020-03-03 21:53:32 +01001700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001701
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001702 if (type->tt_type == VAR_ANY)
1703 ret_type = &t_any;
1704 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001705 {
1706 if (type->tt_argcount != -1)
1707 {
1708 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1709
1710 if (argcount < type->tt_min_argcount - varargs)
1711 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001712 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001713 return FAIL;
1714 }
1715 if (!varargs && argcount > type->tt_argcount)
1716 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001717 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001718 return FAIL;
1719 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001720 if (type->tt_args != NULL)
1721 {
1722 int i;
1723
1724 for (i = 0; i < argcount; ++i)
1725 {
1726 int offset = -argcount + i - 1;
1727 type_T *actual = ((type_T **)stack->ga_data)[
1728 stack->ga_len + offset];
1729 type_T *expected;
1730
1731 if (varargs && i >= type->tt_min_argcount - 1)
1732 expected = type->tt_args[
1733 type->tt_min_argcount - 1]->tt_member;
1734 else
1735 expected = type->tt_args[i];
1736 if (need_type(actual, expected, offset,
1737 cctx, TRUE, FALSE) == FAIL)
1738 {
1739 arg_type_mismatch(expected, actual, i + 1);
1740 return FAIL;
1741 }
1742 }
1743 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001744 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001745 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001746 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001747 else
1748 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001749 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001750 return FAIL;
1751 }
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.pfunc.cpf_top = at_top;
1756 isn->isn_arg.pfunc.cpf_argcount = argcount;
1757
1758 stack->ga_len -= argcount; // drop the arguments
1759
1760 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001761 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001763 // If partial is above the arguments it must be cleared and replaced with
1764 // the return value.
1765 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1766 return FAIL;
1767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 return OK;
1769}
1770
1771/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001772 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 */
1774 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001775generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776{
1777 isn_T *isn;
1778 garray_T *stack = &cctx->ctx_type_stack;
1779 type_T *type;
1780
Bram Moolenaar080457c2020-03-03 21:53:32 +01001781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001784 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001786 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001788 if (type->tt_type != VAR_DICT && type != &t_any)
1789 {
1790 emsg(_(e_dictreq));
1791 return FAIL;
1792 }
1793 // change dict type to dict member type
1794 if (type->tt_type == VAR_DICT)
1795 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796
1797 return OK;
1798}
1799
1800/*
1801 * Generate an ISN_ECHO instruction.
1802 */
1803 static int
1804generate_ECHO(cctx_T *cctx, int with_white, int count)
1805{
1806 isn_T *isn;
1807
Bram Moolenaar080457c2020-03-03 21:53:32 +01001808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.echo.echo_with_white = with_white;
1812 isn->isn_arg.echo.echo_count = count;
1813
1814 return OK;
1815}
1816
Bram Moolenaarad39c092020-02-26 18:23:43 +01001817/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001818 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001819 */
1820 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001821generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001822{
1823 isn_T *isn;
1824
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001825 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001826 return FAIL;
1827 isn->isn_arg.number = count;
1828
1829 return OK;
1830}
1831
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001832/*
1833 * Generate an ISN_PUT instruction.
1834 */
1835 static int
1836generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1837{
1838 isn_T *isn;
1839
1840 RETURN_OK_IF_SKIP(cctx);
1841 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1842 return FAIL;
1843 isn->isn_arg.put.put_regname = regname;
1844 isn->isn_arg.put.put_lnum = lnum;
1845 return OK;
1846}
1847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 static int
1849generate_EXEC(cctx_T *cctx, char_u *line)
1850{
1851 isn_T *isn;
1852
Bram Moolenaar080457c2020-03-03 21:53:32 +01001853 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1855 return FAIL;
1856 isn->isn_arg.string = vim_strsave(line);
1857 return OK;
1858}
1859
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001860 static int
1861generate_EXECCONCAT(cctx_T *cctx, int count)
1862{
1863 isn_T *isn;
1864
1865 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1866 return FAIL;
1867 isn->isn_arg.number = count;
1868 return OK;
1869}
1870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001872 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001873 */
1874 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001875generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001876{
1877 isn_T *isn;
1878
Bram Moolenaar02194d22020-10-24 23:08:38 +02001879 if (cmod->cmod_flags != 0
1880 || cmod->cmod_split != 0
1881 || cmod->cmod_verbose != 0
1882 || cmod->cmod_tab != 0
1883 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001884 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001885 cctx->ctx_has_cmdmod = TRUE;
1886
1887 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001888 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001889 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1890 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1891 return FAIL;
1892 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1893 // filter progam now belongs to the instruction
1894 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001895 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001896
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001897 return OK;
1898}
1899
1900 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001901generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001902{
1903 isn_T *isn;
1904
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001906 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001907 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001908 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001909 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001910
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001911 return OK;
1912}
1913
1914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001916 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001918 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001919reserve_local(
1920 cctx_T *cctx,
1921 char_u *name,
1922 size_t len,
1923 int isConst,
1924 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 lvar_T *lvar;
1927
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001928 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001930 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933
1934 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001935 return NULL;
1936 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001937 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001939 // Every local variable uses the next entry on the stack. We could re-use
1940 // the last ones when leaving a scope, but then variables used in a closure
1941 // might get overwritten. To keep things simple do not re-use stack
1942 // entries. This is less efficient, but memory is cheap these days.
1943 lvar->lv_idx = cctx->ctx_locals_count++;
1944
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001945 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 lvar->lv_const = isConst;
1947 lvar->lv_type = type;
1948
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001949 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950}
1951
1952/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001953 * Remove local variables above "new_top".
1954 */
1955 static void
1956unwind_locals(cctx_T *cctx, int new_top)
1957{
1958 if (cctx->ctx_locals.ga_len > new_top)
1959 {
1960 int idx;
1961 lvar_T *lvar;
1962
1963 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1964 {
1965 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1966 vim_free(lvar->lv_name);
1967 }
1968 }
1969 cctx->ctx_locals.ga_len = new_top;
1970}
1971
1972/*
1973 * Free all local variables.
1974 */
1975 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001976free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001977{
1978 unwind_locals(cctx, 0);
1979 ga_clear(&cctx->ctx_locals);
1980}
1981
1982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 * Find "name" in script-local items of script "sid".
1984 * Returns the index in "sn_var_vals" if found.
1985 * If found but not in "sn_var_vals" returns -1.
1986 * If not found returns -2.
1987 */
1988 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001989get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
1991 hashtab_T *ht;
1992 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001993 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001994 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 int idx;
1996
Bram Moolenaare3d46852020-08-29 13:39:17 +02001997 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001999 if (sid == current_sctx.sc_sid)
2000 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002001 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002002
2003 if (sav == NULL)
2004 return -2;
2005 idx = sav->sav_var_vals_idx;
2006 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2007 if (check_writable && sv->sv_const)
2008 semsg(_(e_readonlyvar), name);
2009 return idx;
2010 }
2011
2012 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 ht = &SCRIPT_VARS(sid);
2014 di = find_var_in_ht(ht, 0, name, TRUE);
2015 if (di == NULL)
2016 return -2;
2017
2018 // Now find the svar_T index in sn_var_vals.
2019 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2020 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002021 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 if (sv->sv_tv == &di->di_tv)
2023 {
2024 if (check_writable && sv->sv_const)
2025 semsg(_(e_readonlyvar), name);
2026 return idx;
2027 }
2028 }
2029 return -1;
2030}
2031
2032/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002033 * Find "name" in imported items of the current script or in "cctx" if not
2034 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 */
2036 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002037find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 int idx;
2040
Bram Moolenaare3d46852020-08-29 13:39:17 +02002041 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002042 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 if (cctx != NULL)
2044 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2045 {
2046 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2047 + idx;
2048
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002049 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2050 : STRLEN(import->imp_name) == len
2051 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return import;
2053 }
2054
Bram Moolenaarefa94442020-08-08 22:16:00 +02002055 return find_imported_in_script(name, len, current_sctx.sc_sid);
2056}
2057
2058 imported_T *
2059find_imported_in_script(char_u *name, size_t len, int sid)
2060{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002061 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002062 int idx;
2063
Bram Moolenaare3d46852020-08-29 13:39:17 +02002064 if (!SCRIPT_ID_VALID(sid))
2065 return NULL;
2066 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2068 {
2069 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2070
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002071 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2072 : STRLEN(import->imp_name) == len
2073 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 return import;
2075 }
2076 return NULL;
2077}
2078
2079/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002080 * Free all imported variables.
2081 */
2082 static void
2083free_imported(cctx_T *cctx)
2084{
2085 int idx;
2086
2087 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2088 {
2089 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2090
2091 vim_free(import->imp_name);
2092 }
2093 ga_clear(&cctx->ctx_imports);
2094}
2095
2096/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002097 * Return TRUE if "p" points at a "#" but not at "#{".
2098 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002099 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002100vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002101{
2102 return p[0] == '#' && p[1] != '{';
2103}
2104
2105/*
2106 * Return a pointer to the next line that isn't empty or only contains a
2107 * comment. Skips over white space.
2108 * Returns NULL if there is none.
2109 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002110 char_u *
2111peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002112{
2113 int lnum = cctx->ctx_lnum;
2114
2115 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2116 {
2117 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002118 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002119
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002120 // ignore NULLs inserted for continuation lines
2121 if (line != NULL)
2122 {
2123 p = skipwhite(line);
2124 if (*p != NUL && !vim9_comment_start(p))
2125 return p;
2126 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002127 }
2128 return NULL;
2129}
2130
2131/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002132 * Called when checking for a following operator at "arg". When the rest of
2133 * the line is empty or only a comment, peek the next line. If there is a next
2134 * line return a pointer to it and set "nextp".
2135 * Otherwise skip over white space.
2136 */
2137 static char_u *
2138may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2139{
2140 char_u *p = skipwhite(arg);
2141
2142 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002143 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002144 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002145 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002146 if (*nextp != NULL)
2147 return *nextp;
2148 }
2149 return p;
2150}
2151
2152/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002153 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002155 * Returns NULL when at the end.
2156 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002157 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002158next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002159{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002160 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002161
2162 do
2163 {
2164 ++cctx->ctx_lnum;
2165 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002166 {
2167 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002168 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002169 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002170 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002171 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002172 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002173 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002174 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002175 return line;
2176}
2177
2178/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002179 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002180 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002181 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2182 */
2183 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002184may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002185{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002186 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002187 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002188 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002189
2190 if (next == NULL)
2191 return FAIL;
2192 *arg = skipwhite(next);
2193 }
2194 return OK;
2195}
2196
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002197/*
2198 * Idem, and give an error when failed.
2199 */
2200 static int
2201may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2202{
2203 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2204 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002205 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002206 return FAIL;
2207 }
2208 return OK;
2209}
2210
2211
Bram Moolenaara5565e42020-05-09 15:44:01 +02002212// Structure passed between the compile_expr* functions to keep track of
2213// constants that have been parsed but for which no code was produced yet. If
2214// possible expressions on these constants are applied at compile time. If
2215// that is not possible, the code to push the constants needs to be generated
2216// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002217// Using 50 should be more than enough of 5 levels of ().
2218#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002219typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002220 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002221 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002222 int pp_is_const; // all generated code was constants, used for a
2223 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224} ppconst_T;
2225
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002226static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002227static int compile_expr0(char_u **arg, cctx_T *cctx);
2228static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2229
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230/*
2231 * Generate a PUSH instruction for "tv".
2232 * "tv" will be consumed or cleared.
2233 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2234 */
2235 static int
2236generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2237{
2238 if (tv != NULL)
2239 {
2240 switch (tv->v_type)
2241 {
2242 case VAR_UNKNOWN:
2243 break;
2244 case VAR_BOOL:
2245 generate_PUSHBOOL(cctx, tv->vval.v_number);
2246 break;
2247 case VAR_SPECIAL:
2248 generate_PUSHSPEC(cctx, tv->vval.v_number);
2249 break;
2250 case VAR_NUMBER:
2251 generate_PUSHNR(cctx, tv->vval.v_number);
2252 break;
2253#ifdef FEAT_FLOAT
2254 case VAR_FLOAT:
2255 generate_PUSHF(cctx, tv->vval.v_float);
2256 break;
2257#endif
2258 case VAR_BLOB:
2259 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2260 tv->vval.v_blob = NULL;
2261 break;
2262 case VAR_STRING:
2263 generate_PUSHS(cctx, tv->vval.v_string);
2264 tv->vval.v_string = NULL;
2265 break;
2266 default:
2267 iemsg("constant type not supported");
2268 clear_tv(tv);
2269 return FAIL;
2270 }
2271 tv->v_type = VAR_UNKNOWN;
2272 }
2273 return OK;
2274}
2275
2276/*
2277 * Generate code for any ppconst entries.
2278 */
2279 static int
2280generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2281{
2282 int i;
2283 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002284 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002285
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002286 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002287 for (i = 0; i < ppconst->pp_used; ++i)
2288 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2289 ret = FAIL;
2290 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002291 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002292 return ret;
2293}
2294
2295/*
2296 * Clear ppconst constants. Used when failing.
2297 */
2298 static void
2299clear_ppconst(ppconst_T *ppconst)
2300{
2301 int i;
2302
2303 for (i = 0; i < ppconst->pp_used; ++i)
2304 clear_tv(&ppconst->pp_tv[i]);
2305 ppconst->pp_used = 0;
2306}
2307
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002308/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002309 * Generate an instruction to load script-local variable "name", without the
2310 * leading "s:".
2311 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 */
2313 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002314compile_load_scriptvar(
2315 cctx_T *cctx,
2316 char_u *name, // variable NUL terminated
2317 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002318 char_u **end, // end of variable
2319 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002321 scriptitem_T *si;
2322 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 imported_T *import;
2324
Bram Moolenaare3d46852020-08-29 13:39:17 +02002325 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2326 return FAIL;
2327 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002328 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002329 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002331 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002332 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2333 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 }
2335 if (idx >= 0)
2336 {
2337 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 current_sctx.sc_sid, idx, sv->sv_type);
2341 return OK;
2342 }
2343
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002344 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (import != NULL)
2346 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002347 if (import->imp_all)
2348 {
2349 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002350 char_u *exp_name;
2351 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002352 ufunc_T *ufunc;
2353 type_T *type;
2354
2355 // Used "import * as Name", need to lookup the member.
2356 if (*p != '.')
2357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002358 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002359 return FAIL;
2360 }
2361 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002362 if (VIM_ISWHITE(*p))
2363 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002364 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002365 return FAIL;
2366 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002367
Bram Moolenaar1c991142020-07-04 13:15:31 +02002368 // isolate one name
2369 exp_name = p;
2370 while (eval_isnamec(*p))
2371 ++p;
2372 cc = *p;
2373 *p = NUL;
2374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002375 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002376 *p = cc;
2377 p = skipwhite(p);
2378
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 // TODO: what if it is a function?
2380 if (idx < 0)
2381 return FAIL;
2382 *end = p;
2383
2384 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2385 import->imp_sid,
2386 idx,
2387 type);
2388 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002389 else if (import->imp_funcname != NULL)
2390 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002391 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002392 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2393 import->imp_sid,
2394 import->imp_var_vals_idx,
2395 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 return OK;
2397 }
2398
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002399 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002400 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return FAIL;
2402}
2403
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002404 static int
2405generate_funcref(cctx_T *cctx, char_u *name)
2406{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002407 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002408
2409 if (ufunc == NULL)
2410 return FAIL;
2411
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002412 // Need to compile any default values to get the argument types.
2413 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2414 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2415 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002416 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002417}
2418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419/*
2420 * Compile a variable name into a load instruction.
2421 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002422 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 * When "error" is FALSE do not give an error when not found.
2424 */
2425 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426compile_load(
2427 char_u **arg,
2428 char_u *end_arg,
2429 cctx_T *cctx,
2430 int is_expr,
2431 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432{
2433 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002434 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002435 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002437 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 if (*(*arg + 1) == ':')
2440 {
2441 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002442 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002443 {
2444 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002446 switch (**arg)
2447 {
2448 case 'g': isn_type = ISN_LOADGDICT; break;
2449 case 'w': isn_type = ISN_LOADWDICT; break;
2450 case 't': isn_type = ISN_LOADTDICT; break;
2451 case 'b': isn_type = ISN_LOADBDICT; break;
2452 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002453 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002454 goto theend;
2455 }
2456 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2457 goto theend;
2458 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 else
2461 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002462 isntype_T isn_type = ISN_DROP;
2463
2464 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2465 if (name == NULL)
2466 return FAIL;
2467
2468 switch (**arg)
2469 {
2470 case 'v': res = generate_LOADV(cctx, name, error);
2471 break;
2472 case 's': res = compile_load_scriptvar(cctx, name,
2473 NULL, NULL, error);
2474 break;
2475 case 'g': isn_type = ISN_LOADG; break;
2476 case 'w': isn_type = ISN_LOADW; break;
2477 case 't': isn_type = ISN_LOADT; break;
2478 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002479 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002480 goto theend;
2481 }
2482 if (isn_type != ISN_DROP)
2483 {
2484 // Global, Buffer-local, Window-local and Tabpage-local
2485 // variables can be defined later, thus we don't check if it
2486 // exists, give error at runtime.
2487 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 }
2490 }
2491 else
2492 {
2493 size_t len = end - *arg;
2494 int idx;
2495 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002496 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497
2498 name = vim_strnsave(*arg, end - *arg);
2499 if (name == NULL)
2500 return FAIL;
2501
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002504 if (!gen_load_outer)
2505 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
2507 else
2508 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002509 lvar_T *lvar = lookup_local(*arg, len, cctx);
2510
2511 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002513 type = lvar->lv_type;
2514 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002515 if (lvar->lv_from_outer)
2516 gen_load_outer = TRUE;
2517 else
2518 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 }
2520 else
2521 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002522 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002523 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002524 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002525 || find_imported(name, 0, cctx) != NULL)
2526 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002527
Bram Moolenaar0f769812020-09-12 18:32:34 +02002528 // When evaluating an expression and the name starts with an
2529 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002530 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002531 if (res == FAIL && is_expr
2532 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 }
2535 }
2536 if (gen_load)
2537 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002538 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002539 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002540 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002541 cctx->ctx_outer_used = TRUE;
2542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
2544
2545 *arg = end;
2546
2547theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002548 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 vim_free(name);
2551 return res;
2552}
2553
2554/*
2555 * Compile the argument expressions.
2556 * "arg" points to just after the "(" and is advanced to after the ")"
2557 */
2558 static int
2559compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2560{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002561 char_u *p = *arg;
2562 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002563 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002567 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2568 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 if (*p == ')')
2570 {
2571 *arg = p + 1;
2572 return OK;
2573 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002574 if (must_end)
2575 {
2576 semsg(_(e_missing_comma_before_argument_str), p);
2577 return FAIL;
2578 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002579
Bram Moolenaara5565e42020-05-09 15:44:01 +02002580 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 return FAIL;
2582 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002583
2584 if (*p != ',' && *skipwhite(p) == ',')
2585 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002586 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002587 p = skipwhite(p);
2588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002590 {
2591 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002592 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002593 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002594 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002595 else
2596 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002597 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002598 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002601 emsg(_(e_missing_close));
2602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603}
2604
2605/*
2606 * Compile a function call: name(arg1, arg2)
2607 * "arg" points to "name", "arg + varlen" to the "(".
2608 * "argcount_init" is 1 for "value->method()"
2609 * Instructions:
2610 * EVAL arg1
2611 * EVAL arg2
2612 * BCALL / DCALL / UCALL
2613 */
2614 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002615compile_call(
2616 char_u **arg,
2617 size_t varlen,
2618 cctx_T *cctx,
2619 ppconst_T *ppconst,
2620 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621{
2622 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002623 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 int argcount = argcount_init;
2625 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002626 char_u fname_buf[FLEN_FIXED + 1];
2627 char_u *tofree = NULL;
2628 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002630 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002631 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633 // we can evaluate "has('name')" at compile time
2634 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2635 {
2636 char_u *s = skipwhite(*arg + varlen + 1);
2637 typval_T argvars[2];
2638
2639 argvars[0].v_type = VAR_UNKNOWN;
2640 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002641 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002642 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002643 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002644 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002645 if (*s == ')' && argvars[0].v_type == VAR_STRING
2646 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647 {
2648 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2649
2650 *arg = s + 1;
2651 argvars[1].v_type = VAR_UNKNOWN;
2652 tv->v_type = VAR_NUMBER;
2653 tv->vval.v_number = 0;
2654 f_has(argvars, tv);
2655 clear_tv(&argvars[0]);
2656 ++ppconst->pp_used;
2657 return OK;
2658 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002659 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 }
2661
2662 if (generate_ppconst(cctx, ppconst) == FAIL)
2663 return FAIL;
2664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (varlen >= sizeof(namebuf))
2666 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002667 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 return FAIL;
2669 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002670 vim_strncpy(namebuf, *arg, varlen);
2671 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 *arg = skipwhite(*arg + varlen + 1);
2674 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
Bram Moolenaara1773442020-08-12 15:21:22 +02002677 is_autoload = vim_strchr(name, '#') != NULL;
2678 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
2680 int idx;
2681
2682 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002683 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002685 {
2686 if (STRCMP(name, "add") == 0 && argcount == 2)
2687 {
2688 garray_T *stack = &cctx->ctx_type_stack;
2689 type_T *type = ((type_T **)stack->ga_data)[
2690 stack->ga_len - 2];
2691
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002692 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002693 if (type->tt_type == VAR_LIST)
2694 {
2695 // inline "add(list, item)" so that the type can be checked
2696 res = generate_LISTAPPEND(cctx);
2697 idx = -1;
2698 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002699 else if (type->tt_type == VAR_BLOB)
2700 {
2701 // inline "add(blob, nr)" so that the type can be checked
2702 res = generate_BLOBAPPEND(cctx);
2703 idx = -1;
2704 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002705 }
2706
2707 if (idx >= 0)
2708 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2709 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002710 else
2711 semsg(_(e_unknownfunc), namebuf);
2712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002715 // An argument or local variable can be a function reference, this
2716 // overrules a function name.
2717 if (lookup_local(namebuf, varlen, cctx) == NULL
2718 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002719 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002720 // If we can find the function by name generate the right call.
2721 // Skip global functions here, a local funcref takes precedence.
2722 ufunc = find_func(name, FALSE, cctx);
2723 if (ufunc != NULL && !func_is_global(ufunc))
2724 {
2725 res = generate_CALL(cctx, ufunc, argcount);
2726 goto theend;
2727 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729
2730 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002731 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002732 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002734 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002735 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002736 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002737 garray_T *stack = &cctx->ctx_type_stack;
2738 type_T *type;
2739
2740 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2741 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002742 goto theend;
2743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
Bram Moolenaar0f769812020-09-12 18:32:34 +02002745 // If we can find a global function by name generate the right call.
2746 if (ufunc != NULL)
2747 {
2748 res = generate_CALL(cctx, ufunc, argcount);
2749 goto theend;
2750 }
2751
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002752 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002753 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002754 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002755 res = generate_UCALL(cctx, name, argcount);
2756 else
2757 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002758
2759theend:
2760 vim_free(tofree);
2761 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762}
2763
2764// like NAMESPACE_CHAR but with 'a' and 'l'.
2765#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2766
2767/*
2768 * Find the end of a variable or function name. Unlike find_name_end() this
2769 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002770 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 * Return a pointer to just after the name. Equal to "arg" if there is no
2772 * valid name.
2773 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002774 static char_u *
2775to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776{
2777 char_u *p;
2778
2779 // Quick check for valid starting character.
2780 if (!eval_isnamec1(*arg))
2781 return arg;
2782
2783 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2784 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2785 // and can be used in slice "[n:]".
2786 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002787 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2789 break;
2790 return p;
2791}
2792
2793/*
2794 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002795 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 */
2797 char_u *
2798to_name_const_end(char_u *arg)
2799{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002800 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 typval_T rettv;
2802
2803 if (p == arg && *arg == '[')
2804 {
2805
2806 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002807 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 p = arg;
2809 }
2810 else if (p == arg && *arg == '#' && arg[1] == '{')
2811 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002812 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002814 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 p = arg;
2816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
2818 return p;
2819}
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821/*
2822 * parse a list: [expr, expr]
2823 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002824 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 */
2826 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002827compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828{
2829 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002830 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002832 int is_const;
2833 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002835 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002837 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002838 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002839 semsg(_(e_list_end), *arg);
2840 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002841 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002842 if (*p == ',')
2843 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002844 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002845 return FAIL;
2846 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002847 if (*p == ']')
2848 {
2849 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002850 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002851 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002852 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002853 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002854 if (!is_const)
2855 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 ++count;
2857 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002860 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2861 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002862 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002863 return FAIL;
2864 }
2865 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002866 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 p = skipwhite(p);
2868 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002869 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 ppconst->pp_is_const = is_all_const;
2872 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873}
2874
2875/*
2876 * parse a lambda: {arg, arg -> expr}
2877 * "*arg" points to the '{'.
2878 */
2879 static int
2880compile_lambda(char_u **arg, cctx_T *cctx)
2881{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 typval_T rettv;
2883 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002884 evalarg_T evalarg;
2885
2886 CLEAR_FIELD(evalarg);
2887 evalarg.eval_flags = EVAL_EVALUATE;
2888 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002891 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002892 {
2893 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002895 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002898 ++ufunc->uf_refcount;
2899 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
2901 // The function will have one line: "return {expr}".
2902 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002903 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002905 clear_evalarg(&evalarg, NULL);
2906
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002907 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002908 {
2909 // The return type will now be known.
2910 set_function_type(ufunc);
2911
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002912 // The function reference count will be 1. When the ISN_FUNCREF
2913 // instruction is deleted the reference count is decremented and the
2914 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002915 return generate_FUNCREF(cctx, ufunc);
2916 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002917
2918 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 return FAIL;
2920}
2921
2922/*
2923 * Compile a lamda call: expr->{lambda}(args)
2924 * "arg" points to the "{".
2925 */
2926 static int
2927compile_lambda_call(char_u **arg, cctx_T *cctx)
2928{
2929 ufunc_T *ufunc;
2930 typval_T rettv;
2931 int argcount = 1;
2932 int ret = FAIL;
2933
2934 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002935 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return FAIL;
2937
2938 if (**arg != '(')
2939 {
2940 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002941 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 else
2943 semsg(_(e_missing_paren), "lambda");
2944 clear_tv(&rettv);
2945 return FAIL;
2946 }
2947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 ufunc = rettv.vval.v_partial->pt_func;
2949 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002950 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002951 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002952
Bram Moolenaara05e5242020-09-19 18:19:19 +02002953 // The function will have one line: "return {expr}". Compile it into
2954 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002955 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 // compile the arguments
2958 *arg = skipwhite(*arg + 1);
2959 if (compile_arguments(arg, cctx, &argcount) == OK)
2960 // call the compiled function
2961 ret = generate_CALL(cctx, ufunc, argcount);
2962
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002963 if (ret == FAIL)
2964 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 return ret;
2966}
2967
2968/*
2969 * parse a dict: {'key': val} or #{key: val}
2970 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002971 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 */
2973 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002974compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975{
2976 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002977 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 int count = 0;
2979 dict_T *d = dict_alloc();
2980 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002981 char_u *whitep = *arg;
2982 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002983 int is_const;
2984 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985
2986 if (d == NULL)
2987 return FAIL;
2988 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
2991 char_u *key = NULL;
2992
Bram Moolenaar23c55272020-06-21 16:58:13 +02002993 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002994 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002995 *arg = NULL;
2996 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002997 }
2998
2999 if (**arg == '}')
3000 break;
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (literal)
3003 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003004 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
Bram Moolenaar2c330432020-04-13 14:41:35 +02003006 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003008 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003011 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 if (generate_PUSHS(cctx, key) == FAIL)
3013 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003014 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016 else
3017 {
3018 isn_T *isn;
3019
Bram Moolenaara5565e42020-05-09 15:44:01 +02003020 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3023 if (isn->isn_type == ISN_PUSHS)
3024 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003025 else
3026 {
3027 type_T *keytype = ((type_T **)stack->ga_data)
3028 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003029 if (need_type(keytype, &t_string, -1, cctx,
3030 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003031 return FAIL;
3032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034
3035 // Check for duplicate keys, if using string keys.
3036 if (key != NULL)
3037 {
3038 item = dict_find(d, key, -1);
3039 if (item != NULL)
3040 {
3041 semsg(_(e_duplicate_key), key);
3042 goto failret;
3043 }
3044 item = dictitem_alloc(key);
3045 if (item != NULL)
3046 {
3047 item->di_tv.v_type = VAR_UNKNOWN;
3048 item->di_tv.v_lock = 0;
3049 if (dict_add(d, item) == FAIL)
3050 dictitem_free(item);
3051 }
3052 }
3053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 if (**arg != ':')
3055 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003056 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003057 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003058 else
3059 semsg(_(e_missing_dict_colon), *arg);
3060 return FAIL;
3061 }
3062 whitep = *arg + 1;
3063 if (!IS_WHITE_OR_NUL(*whitep))
3064 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003065 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068
3069 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003070 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003072 *arg = NULL;
3073 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003074 }
3075
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003076 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003078 if (!is_const)
3079 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 ++count;
3081
Bram Moolenaar2c330432020-04-13 14:41:35 +02003082 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003083 *arg = skipwhite(*arg);
3084 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003085 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003086 *arg = NULL;
3087 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (**arg == '}')
3090 break;
3091 if (**arg != ',')
3092 {
3093 semsg(_(e_missing_dict_comma), *arg);
3094 goto failret;
3095 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003096 if (IS_WHITE_OR_NUL(*whitep))
3097 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003098 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003099 return FAIL;
3100 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003101 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003102 if (!IS_WHITE_OR_NUL(*whitep))
3103 {
3104 semsg(_(e_white_space_required_after_str), ",");
3105 return FAIL;
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 *arg = skipwhite(*arg + 1);
3108 }
3109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 *arg = *arg + 1;
3111
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003113 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003114 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003115 *arg += STRLEN(*arg);
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003118 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return generate_NEWDICT(cctx, count);
3120
3121failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003123 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003125 *arg = (char_u *)"";
3126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 dict_unref(d);
3128 return FAIL;
3129}
3130
3131/*
3132 * Compile "&option".
3133 */
3134 static int
3135compile_get_option(char_u **arg, cctx_T *cctx)
3136{
3137 typval_T rettv;
3138 char_u *start = *arg;
3139 int ret;
3140
3141 // parse the option and get the current value to get the type.
3142 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003143 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (ret == OK)
3145 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003146 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 char_u *name = vim_strnsave(start, *arg - start);
3148 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3149
3150 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3151 vim_free(name);
3152 }
3153 clear_tv(&rettv);
3154
3155 return ret;
3156}
3157
3158/*
3159 * Compile "$VAR".
3160 */
3161 static int
3162compile_get_env(char_u **arg, cctx_T *cctx)
3163{
3164 char_u *start = *arg;
3165 int len;
3166 int ret;
3167 char_u *name;
3168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 ++*arg;
3170 len = get_env_len(arg);
3171 if (len == 0)
3172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003173 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 return FAIL;
3175 }
3176
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003177 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 name = vim_strnsave(start, len + 1);
3179 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3180 vim_free(name);
3181 return ret;
3182}
3183
3184/*
3185 * Compile "@r".
3186 */
3187 static int
3188compile_get_register(char_u **arg, cctx_T *cctx)
3189{
3190 int ret;
3191
3192 ++*arg;
3193 if (**arg == NUL)
3194 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003195 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 return FAIL;
3197 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003198 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 {
3200 emsg_invreg(**arg);
3201 return FAIL;
3202 }
3203 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3204 ++*arg;
3205 return ret;
3206}
3207
3208/*
3209 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003210 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 */
3212 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003213apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003215 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 // this works from end to start
3218 while (p > start)
3219 {
3220 --p;
3221 if (*p == '-' || *p == '+')
3222 {
3223 // only '-' has an effect, for '+' we only check the type
3224#ifdef FEAT_FLOAT
3225 if (rettv->v_type == VAR_FLOAT)
3226 {
3227 if (*p == '-')
3228 rettv->vval.v_float = -rettv->vval.v_float;
3229 }
3230 else
3231#endif
3232 {
3233 varnumber_T val;
3234 int error = FALSE;
3235
3236 // tv_get_number_chk() accepts a string, but we don't want that
3237 // here
3238 if (check_not_string(rettv) == FAIL)
3239 return FAIL;
3240 val = tv_get_number_chk(rettv, &error);
3241 clear_tv(rettv);
3242 if (error)
3243 return FAIL;
3244 if (*p == '-')
3245 val = -val;
3246 rettv->v_type = VAR_NUMBER;
3247 rettv->vval.v_number = val;
3248 }
3249 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003250 else if (numeric_only)
3251 {
3252 ++p;
3253 break;
3254 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003255 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
3257 int v = tv2bool(rettv);
3258
3259 // '!' is permissive in the type.
3260 clear_tv(rettv);
3261 rettv->v_type = VAR_BOOL;
3262 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3263 }
3264 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003265 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 return OK;
3267}
3268
3269/*
3270 * Recognize v: variables that are constants and set "rettv".
3271 */
3272 static void
3273get_vim_constant(char_u **arg, typval_T *rettv)
3274{
3275 if (STRNCMP(*arg, "v:true", 6) == 0)
3276 {
3277 rettv->v_type = VAR_BOOL;
3278 rettv->vval.v_number = VVAL_TRUE;
3279 *arg += 6;
3280 }
3281 else if (STRNCMP(*arg, "v:false", 7) == 0)
3282 {
3283 rettv->v_type = VAR_BOOL;
3284 rettv->vval.v_number = VVAL_FALSE;
3285 *arg += 7;
3286 }
3287 else if (STRNCMP(*arg, "v:null", 6) == 0)
3288 {
3289 rettv->v_type = VAR_SPECIAL;
3290 rettv->vval.v_number = VVAL_NULL;
3291 *arg += 6;
3292 }
3293 else if (STRNCMP(*arg, "v:none", 6) == 0)
3294 {
3295 rettv->v_type = VAR_SPECIAL;
3296 rettv->vval.v_number = VVAL_NONE;
3297 *arg += 6;
3298 }
3299}
3300
Bram Moolenaar696ba232020-07-29 21:20:41 +02003301 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003302get_compare_type(char_u *p, int *len, int *type_is)
3303{
3304 exptype_T type = EXPR_UNKNOWN;
3305 int i;
3306
3307 switch (p[0])
3308 {
3309 case '=': if (p[1] == '=')
3310 type = EXPR_EQUAL;
3311 else if (p[1] == '~')
3312 type = EXPR_MATCH;
3313 break;
3314 case '!': if (p[1] == '=')
3315 type = EXPR_NEQUAL;
3316 else if (p[1] == '~')
3317 type = EXPR_NOMATCH;
3318 break;
3319 case '>': if (p[1] != '=')
3320 {
3321 type = EXPR_GREATER;
3322 *len = 1;
3323 }
3324 else
3325 type = EXPR_GEQUAL;
3326 break;
3327 case '<': if (p[1] != '=')
3328 {
3329 type = EXPR_SMALLER;
3330 *len = 1;
3331 }
3332 else
3333 type = EXPR_SEQUAL;
3334 break;
3335 case 'i': if (p[1] == 's')
3336 {
3337 // "is" and "isnot"; but not a prefix of a name
3338 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3339 *len = 5;
3340 i = p[*len];
3341 if (!isalnum(i) && i != '_')
3342 {
3343 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3344 *type_is = TRUE;
3345 }
3346 }
3347 break;
3348 }
3349 return type;
3350}
3351
3352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 */
3356 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003357compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003359 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361 // this works from end to start
3362 while (p > start)
3363 {
3364 --p;
3365 if (*p == '-' || *p == '+')
3366 {
3367 int negate = *p == '-';
3368 isn_T *isn;
3369
3370 // TODO: check type
3371 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3372 {
3373 --p;
3374 if (*p == '-')
3375 negate = !negate;
3376 }
3377 // only '-' has an effect, for '+' we only check the type
3378 if (negate)
3379 isn = generate_instr(cctx, ISN_NEGATENR);
3380 else
3381 isn = generate_instr(cctx, ISN_CHECKNR);
3382 if (isn == NULL)
3383 return FAIL;
3384 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003385 else if (numeric_only)
3386 {
3387 ++p;
3388 break;
3389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 else
3391 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003392 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003394 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003396 if (p[-1] == '!')
3397 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 }
3400 if (generate_2BOOL(cctx, invert) == FAIL)
3401 return FAIL;
3402 }
3403 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003404 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 return OK;
3406}
3407
3408/*
3409 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003410 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 */
3412 static int
3413compile_subscript(
3414 char_u **arg,
3415 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003416 char_u *start_leader,
3417 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003418 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003420 char_u *name_start = *end_leader;
3421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 for (;;)
3423 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003424 char_u *p = skipwhite(*arg);
3425
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003426 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003427 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003428 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003429
3430 // If a following line starts with "->{" or "->X" advance to that
3431 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003432 // Also if a following line starts with ".x".
3433 if (next != NULL &&
3434 ((next[0] == '-' && next[1] == '>'
3435 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003436 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003437 {
3438 next = next_line_from_context(cctx, TRUE);
3439 if (next == NULL)
3440 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003441 *arg = next;
3442 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003443 }
3444 }
3445
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003446 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3447 // not a function call.
3448 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003450 garray_T *stack = &cctx->ctx_type_stack;
3451 type_T *type;
3452 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003454 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003455 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003456 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003459 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3460
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003461 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3463 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003464 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 return FAIL;
3466 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003467 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003469 char_u *pstart = p;
3470
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003471 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003472 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003473 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 // something->method()
3476 // Apply the '!', '-' and '+' first:
3477 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003478 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003481 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003482 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003483 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (**arg == '{')
3485 {
3486 // lambda call: list->{lambda}
3487 if (compile_lambda_call(arg, cctx) == FAIL)
3488 return FAIL;
3489 }
3490 else
3491 {
3492 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003493 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003494 if (!eval_isnamec1(*p))
3495 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003496 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003497 return FAIL;
3498 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003499 if (ASCII_ISALPHA(*p) && p[1] == ':')
3500 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003501 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 ;
3503 if (*p != '(')
3504 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003505 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 return FAIL;
3507 }
3508 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003509 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 return FAIL;
3511 }
3512 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003513 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003516 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003517 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003518 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003519 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003520
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003521 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003522 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003523 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 // TODO: blob index
3525 // TODO: more arguments
3526 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003527 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003528 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003529 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003530
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003531 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003532 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003533 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003534 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003535 if (**arg == ':')
3536 // missing first index is equal to zero
3537 generate_PUSHNR(cctx, 0);
3538 else
3539 {
3540 if (compile_expr0(arg, cctx) == FAIL)
3541 return FAIL;
3542 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3543 return FAIL;
3544 *arg = skipwhite(*arg);
3545 }
3546 if (**arg == ':')
3547 {
3548 *arg = skipwhite(*arg + 1);
3549 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3550 return FAIL;
3551 if (**arg == ']')
3552 // missing second index is equal to end of string
3553 generate_PUSHNR(cctx, -1);
3554 else
3555 {
3556 if (compile_expr0(arg, cctx) == FAIL)
3557 return FAIL;
3558 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3559 return FAIL;
3560 *arg = skipwhite(*arg);
3561 }
3562 is_slice = TRUE;
3563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
3565 if (**arg != ']')
3566 {
3567 emsg(_(e_missbrac));
3568 return FAIL;
3569 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003570 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003572 // We can index a list and a dict. If we don't know the type
3573 // we can use the index value type.
3574 // TODO: If we don't know use an instruction to figure it out at
3575 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003576 typep = ((type_T **)stack->ga_data) + stack->ga_len
3577 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003578 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003579 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3580 // If the index is a string, the variable must be a Dict.
3581 if (*typep == &t_any && valtype == &t_string)
3582 vtype = VAR_DICT;
3583 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003584 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003585 if (need_type(valtype, &t_number, -1, cctx,
3586 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003587 return FAIL;
3588 if (is_slice)
3589 {
3590 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003591 if (need_type(valtype, &t_number, -2, cctx,
3592 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003593 return FAIL;
3594 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003595 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003596
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003597 if (vtype == VAR_DICT)
3598 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003599 if (is_slice)
3600 {
3601 emsg(_(e_cannot_slice_dictionary));
3602 return FAIL;
3603 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003604 if ((*typep)->tt_type == VAR_DICT)
3605 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003606 else
3607 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003608 if (need_type(*typep, &t_dict_any, -2, cctx,
3609 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003610 return FAIL;
3611 *typep = &t_any;
3612 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003613 if (may_generate_2STRING(-1, cctx) == FAIL)
3614 return FAIL;
3615 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3616 return FAIL;
3617 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003618 else if (vtype == VAR_STRING)
3619 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003620 *typep = &t_string;
3621 if ((is_slice
3622 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3623 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003624 return FAIL;
3625 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 else if (vtype == VAR_BLOB)
3627 {
3628 emsg("Sorry, blob index and slice not implemented yet");
3629 return FAIL;
3630 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003631 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003632 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003633 if (is_slice)
3634 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003635 if (generate_instr_drop(cctx,
3636 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3637 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003638 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003639 }
Bram Moolenaared591872020-08-15 22:14:53 +02003640 else
3641 {
3642 if ((*typep)->tt_type == VAR_LIST)
3643 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003644 if (generate_instr_drop(cctx,
3645 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3646 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003647 return FAIL;
3648 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003649 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003650 else
3651 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003652 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003653 return FAIL;
3654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003656 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003658 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003659 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003660 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003661 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003662
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003663 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003664 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003665 {
3666 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003667 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003668 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003669 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003670 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 while (eval_isnamec(*p))
3672 MB_PTR_ADV(p);
3673 if (p == *arg)
3674 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003675 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 return FAIL;
3677 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003678 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
3680 *arg = p;
3681 }
3682 else
3683 break;
3684 }
3685
3686 // TODO - see handle_subscript():
3687 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3688 // Don't do this when "Func" is already a partial that was bound
3689 // explicitly (pt_auto is FALSE).
3690
3691 return OK;
3692}
3693
3694/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003695 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3696 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003698 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700 *
3701 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 */
3703
3704/*
3705 * number number constant
3706 * 0zFFFFFFFF Blob constant
3707 * "string" string constant
3708 * 'string' literal string constant
3709 * &option-name option value
3710 * @r register contents
3711 * identifier variable value
3712 * function() function call
3713 * $VAR environment variable
3714 * (expression) nested expression
3715 * [expr, expr] List
3716 * {key: val, key: val} Dictionary
3717 * #{key: val, key: val} Dictionary with literal keys
3718 *
3719 * Also handle:
3720 * ! in front logical NOT
3721 * - in front unary minus
3722 * + in front unary plus (ignored)
3723 * trailing (arg) funcref/partial call
3724 * trailing [] subscript in String or List
3725 * trailing .name entry in Dictionary
3726 * trailing ->name() method call
3727 */
3728 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729compile_expr7(
3730 char_u **arg,
3731 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003732 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 char_u *start_leader, *end_leader;
3735 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003736 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003737 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003739 ppconst->pp_is_const = FALSE;
3740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 /*
3742 * Skip '!', '-' and '+' characters. They are handled later.
3743 */
3744 start_leader = *arg;
3745 while (**arg == '!' || **arg == '-' || **arg == '+')
3746 *arg = skipwhite(*arg + 1);
3747 end_leader = *arg;
3748
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 switch (**arg)
3751 {
3752 /*
3753 * Number constant.
3754 */
3755 case '0': // also for blob starting with 0z
3756 case '1':
3757 case '2':
3758 case '3':
3759 case '4':
3760 case '5':
3761 case '6':
3762 case '7':
3763 case '8':
3764 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003765 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003767 // Apply "-" and "+" just before the number now, right to
3768 // left. Matters especially when "->" follows. Stops at
3769 // '!'.
3770 if (apply_leader(rettv, TRUE,
3771 start_leader, &end_leader) == FAIL)
3772 {
3773 clear_tv(rettv);
3774 return FAIL;
3775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 break;
3777
3778 /*
3779 * String constant: "string".
3780 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003781 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 return FAIL;
3783 break;
3784
3785 /*
3786 * Literal string constant: 'str''ing'.
3787 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003788 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
3790 break;
3791
3792 /*
3793 * Constant Vim variable.
3794 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003795 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 ret = NOTDONE;
3797 break;
3798
3799 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003800 * "true" constant
3801 */
3802 case 't': if (STRNCMP(*arg, "true", 4) == 0
3803 && !eval_isnamec((*arg)[4]))
3804 {
3805 *arg += 4;
3806 rettv->v_type = VAR_BOOL;
3807 rettv->vval.v_number = VVAL_TRUE;
3808 }
3809 else
3810 ret = NOTDONE;
3811 break;
3812
3813 /*
3814 * "false" constant
3815 */
3816 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3817 && !eval_isnamec((*arg)[5]))
3818 {
3819 *arg += 5;
3820 rettv->v_type = VAR_BOOL;
3821 rettv->vval.v_number = VVAL_FALSE;
3822 }
3823 else
3824 ret = NOTDONE;
3825 break;
3826
3827 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 * List: [expr, expr]
3829 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003830 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 break;
3832
3833 /*
3834 * Dictionary: #{key: val, key: val}
3835 */
3836 case '#': if ((*arg)[1] == '{')
3837 {
3838 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003839 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 }
3841 else
3842 ret = NOTDONE;
3843 break;
3844
3845 /*
3846 * Lambda: {arg, arg -> expr}
3847 * Dictionary: {'key': val, 'key': val}
3848 */
3849 case '{': {
3850 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003851 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852
3853 // Find out what comes after the arguments.
3854 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003855 &ga_arg, TRUE, NULL, NULL,
3856 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 if (ret != FAIL && *start == '>')
3858 ret = compile_lambda(arg, cctx);
3859 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003860 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 }
3862 break;
3863
3864 /*
3865 * Option value: &name
3866 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003867 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3868 return FAIL;
3869 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 break;
3871
3872 /*
3873 * Environment variable: $VAR.
3874 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003875 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3876 return FAIL;
3877 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 break;
3879
3880 /*
3881 * Register contents: @r.
3882 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003883 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
3885 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 break;
3887 /*
3888 * nested expression: (expression).
3889 */
3890 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003891
3892 // recursive!
3893 if (ppconst->pp_used <= PPSIZE - 10)
3894 {
3895 ret = compile_expr1(arg, cctx, ppconst);
3896 }
3897 else
3898 {
3899 // Not enough space in ppconst, flush constants.
3900 if (generate_ppconst(cctx, ppconst) == FAIL)
3901 return FAIL;
3902 ret = compile_expr0(arg, cctx);
3903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 *arg = skipwhite(*arg);
3905 if (**arg == ')')
3906 ++*arg;
3907 else if (ret == OK)
3908 {
3909 emsg(_(e_missing_close));
3910 ret = FAIL;
3911 }
3912 break;
3913
3914 default: ret = NOTDONE;
3915 break;
3916 }
3917 if (ret == FAIL)
3918 return FAIL;
3919
Bram Moolenaar1c747212020-05-09 18:28:34 +02003920 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003922 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 clear_tv(rettv);
3924 else
3925 // A constant expression can possibly be handled compile time,
3926 // return the value instead of generating code.
3927 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 }
3929 else if (ret == NOTDONE)
3930 {
3931 char_u *p;
3932 int r;
3933
3934 if (!eval_isnamec1(**arg))
3935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003936 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 return FAIL;
3938 }
3939
3940 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003941 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003943 {
3944 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003947 {
3948 if (generate_ppconst(cctx, ppconst) == FAIL)
3949 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003950 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (r == FAIL)
3953 return FAIL;
3954 }
3955
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003956 // Handle following "[]", ".member", etc.
3957 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 ppconst) == FAIL)
3960 return FAIL;
3961 if (ppconst->pp_used > 0)
3962 {
3963 // apply the '!', '-' and '+' before the constant
3964 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003965 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003966 return FAIL;
3967 return OK;
3968 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003971 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972}
3973
3974/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003975 * Give the "white on both sides" error, taking the operator from "p[len]".
3976 */
3977 void
3978error_white_both(char_u *op, int len)
3979{
3980 char_u buf[10];
3981
3982 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003983 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003984}
3985
3986/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003987 * <type>expr7: runtime type check / conversion
3988 */
3989 static int
3990compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3991{
3992 type_T *want_type = NULL;
3993
3994 // Recognize <type>
3995 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3996 {
3997 int called_emsg_before = called_emsg;
3998
3999 ++*arg;
4000 want_type = parse_type(arg, cctx->ctx_type_list);
4001 if (called_emsg != called_emsg_before)
4002 return FAIL;
4003
4004 if (**arg != '>')
4005 {
4006 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004007 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004008 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004009 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004010 return FAIL;
4011 }
4012 ++*arg;
4013 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4014 return FAIL;
4015 }
4016
4017 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4018 return FAIL;
4019
4020 if (want_type != NULL)
4021 {
4022 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004023 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004024
Bram Moolenaard1103582020-08-14 22:44:25 +02004025 generate_ppconst(cctx, ppconst);
4026 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004027 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004028 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004029 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004030 return FAIL;
4031 }
4032 }
4033
4034 return OK;
4035}
4036
4037/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 * * number multiplication
4039 * / number division
4040 * % number modulo
4041 */
4042 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004043compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044{
4045 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004046 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004047 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004049 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004050 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052
4053 /*
4054 * Repeat computing, until no "*", "/" or "%" is following.
4055 */
4056 for (;;)
4057 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004058 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 if (*op != '*' && *op != '/' && *op != '%')
4060 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004061 if (next != NULL)
4062 {
4063 *arg = next_line_from_context(cctx, TRUE);
4064 op = skipwhite(*arg);
4065 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004066
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004067 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004069 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004073 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004076 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004077 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004079
4080 if (ppconst->pp_used == ppconst_used + 2
4081 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4082 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004083 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004084 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4085 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 // both are numbers: compute the result
4089 switch (*op)
4090 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004091 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004092 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004095 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004096 break;
4097 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004098 tv1->vval.v_number = res;
4099 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100 }
4101 else
4102 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004104 generate_two_op(cctx, op);
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 }
4107
4108 return OK;
4109}
4110
4111/*
4112 * + number addition
4113 * - number subtraction
4114 * .. string concatenation
4115 */
4116 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118{
4119 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004120 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004122 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
4124 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
4127
4128 /*
4129 * Repeat computing, until no "+", "-" or ".." is following.
4130 */
4131 for (;;)
4132 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004133 op = may_peek_next_line(cctx, *arg, &next);
4134 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 break;
4136 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004137 if (next != NULL)
4138 {
4139 *arg = next_line_from_context(cctx, TRUE);
4140 op = skipwhite(*arg);
4141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004143 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004145 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004146 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 }
4148
4149 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004150 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004151 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004153 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004154 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 return FAIL;
4156
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004158 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004159 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4160 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4161 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4162 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4165 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004166
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004167 // concat/subtract/add constant numbers
4168 if (*op == '+')
4169 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4170 else if (*op == '-')
4171 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4172 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004174 // concatenate constant strings
4175 char_u *s1 = tv1->vval.v_string;
4176 char_u *s2 = tv2->vval.v_string;
4177 size_t len1 = STRLEN(s1);
4178
4179 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4180 if (tv1->vval.v_string == NULL)
4181 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004182 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 return FAIL;
4184 }
4185 mch_memmove(tv1->vval.v_string, s1, len1);
4186 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 vim_free(s1);
4188 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004190 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 }
4192 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 if (*op == '.')
4196 {
4197 if (may_generate_2STRING(-2, cctx) == FAIL
4198 || may_generate_2STRING(-1, cctx) == FAIL)
4199 return FAIL;
4200 generate_instr_drop(cctx, ISN_CONCAT, 1);
4201 }
4202 else
4203 generate_two_op(cctx, op);
4204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206
4207 return OK;
4208}
4209
4210/*
4211 * expr5a == expr5b
4212 * expr5a =~ expr5b
4213 * expr5a != expr5b
4214 * expr5a !~ expr5b
4215 * expr5a > expr5b
4216 * expr5a >= expr5b
4217 * expr5a < expr5b
4218 * expr5a <= expr5b
4219 * expr5a is expr5b
4220 * expr5a isnot expr5b
4221 *
4222 * Produces instructions:
4223 * EVAL expr5a Push result of "expr5a"
4224 * EVAL expr5b Push result of "expr5b"
4225 * COMPARE one of the compare instructions
4226 */
4227 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229{
4230 exptype_T type = EXPR_UNKNOWN;
4231 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004232 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
4237 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004238 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 return FAIL;
4240
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004241 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004242 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
4244 /*
4245 * If there is a comparative operator, use it.
4246 */
4247 if (type != EXPR_UNKNOWN)
4248 {
4249 int ic = FALSE; // Default: do not ignore case
4250
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004251 if (next != NULL)
4252 {
4253 *arg = next_line_from_context(cctx, TRUE);
4254 p = skipwhite(*arg);
4255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 if (type_is && (p[len] == '?' || p[len] == '#'))
4257 {
4258 semsg(_(e_invexpr2), *arg);
4259 return FAIL;
4260 }
4261 // extra question mark appended: ignore case
4262 if (p[len] == '?')
4263 {
4264 ic = TRUE;
4265 ++len;
4266 }
4267 // extra '#' appended: match case (ignored)
4268 else if (p[len] == '#')
4269 ++len;
4270 // nothing appended: match case
4271
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004272 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004274 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004275 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 }
4277
4278 // get the second variable
4279 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004280 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004281 return FAIL;
4282
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 return FAIL;
4285
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286 if (ppconst->pp_used == ppconst_used + 2)
4287 {
4288 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4289 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4290 int ret;
4291
4292 // Both sides are a constant, compute the result now.
4293 // First check for a valid combination of types, this is more
4294 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004295 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 ret = FAIL;
4297 else
4298 {
4299 ret = typval_compare(tv1, tv2, type, ic);
4300 tv1->v_type = VAR_BOOL;
4301 tv1->vval.v_number = tv1->vval.v_number
4302 ? VVAL_TRUE : VVAL_FALSE;
4303 clear_tv(tv2);
4304 --ppconst->pp_used;
4305 }
4306 return ret;
4307 }
4308
4309 generate_ppconst(cctx, ppconst);
4310 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 }
4312
4313 return OK;
4314}
4315
Bram Moolenaar7f141552020-05-09 17:35:53 +02004316static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318/*
4319 * Compile || or &&.
4320 */
4321 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322compile_and_or(
4323 char_u **arg,
4324 cctx_T *cctx,
4325 char *op,
4326 ppconst_T *ppconst,
4327 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004329 char_u *next;
4330 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 int opchar = *op;
4332
4333 if (p[0] == opchar && p[1] == opchar)
4334 {
4335 garray_T *instr = &cctx->ctx_instr;
4336 garray_T end_ga;
4337
4338 /*
4339 * Repeat until there is no following "||" or "&&"
4340 */
4341 ga_init2(&end_ga, sizeof(int), 10);
4342 while (p[0] == opchar && p[1] == opchar)
4343 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004344 if (next != NULL)
4345 {
4346 *arg = next_line_from_context(cctx, TRUE);
4347 p = skipwhite(*arg);
4348 }
4349
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004350 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4351 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004352 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004353 return FAIL;
4354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004356 // TODO: use ppconst if the value is a constant and check
4357 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358 generate_ppconst(cctx, ppconst);
4359
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004360 // Every part must evaluate to a bool.
4361 if (bool_on_stack(cctx) == FAIL)
4362 {
4363 ga_clear(&end_ga);
4364 return FAIL;
4365 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 if (ga_grow(&end_ga, 1) == FAIL)
4368 {
4369 ga_clear(&end_ga);
4370 return FAIL;
4371 }
4372 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4373 ++end_ga.ga_len;
4374 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004375 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 // eval the next expression
4378 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004379 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004380 return FAIL;
4381
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4383 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
4385 ga_clear(&end_ga);
4386 return FAIL;
4387 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004388
4389 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004393 // Every part must evaluate to a bool.
4394 if (bool_on_stack(cctx) == FAIL)
4395 {
4396 ga_clear(&end_ga);
4397 return FAIL;
4398 }
4399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 // Fill in the end label in all jumps.
4401 while (end_ga.ga_len > 0)
4402 {
4403 isn_T *isn;
4404
4405 --end_ga.ga_len;
4406 isn = ((isn_T *)instr->ga_data)
4407 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4408 isn->isn_arg.jump.jump_where = instr->ga_len;
4409 }
4410 ga_clear(&end_ga);
4411 }
4412
4413 return OK;
4414}
4415
4416/*
4417 * expr4a && expr4a && expr4a logical AND
4418 *
4419 * Produces instructions:
4420 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004421 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004422 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 * EVAL expr4c Push result of "expr4c"
4426 * end:
4427 */
4428 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 int ppconst_used = ppconst->pp_used;
4432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 return FAIL;
4436
4437 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439}
4440
4441/*
4442 * expr3a || expr3b || expr3c logical OR
4443 *
4444 * Produces instructions:
4445 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004446 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004447 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004449 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 * EVAL expr3c Push result of "expr3c"
4451 * end:
4452 */
4453 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 int ppconst_used = ppconst->pp_used;
4457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 return FAIL;
4461
4462 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464}
4465
4466/*
4467 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004469 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 * JUMP_IF_FALSE alt jump if false
4471 * EVAL expr1a
4472 * JUMP_ALWAYS end
4473 * alt: EVAL expr1b
4474 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004475 *
4476 * Toplevel expression: expr2 ?? expr1
4477 * Produces instructions:
4478 * EVAL expr2 Push result of "expr2"
4479 * JUMP_AND_KEEP_IF_TRUE end jump if true
4480 * EVAL expr1
4481 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 */
4483 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485{
4486 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004488 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
Bram Moolenaar3988f642020-08-27 22:43:03 +02004490 // Ignore all kinds of errors when not producing code.
4491 if (cctx->ctx_skip == SKIP_YES)
4492 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004493 evalarg_T evalarg;
4494
4495 CLEAR_FIELD(evalarg);
4496 evalarg.eval_cctx = cctx;
4497 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004498 return OK;
4499 }
4500
Bram Moolenaar61a89812020-05-07 16:58:17 +02004501 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004502 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 return FAIL;
4504
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004505 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 if (*p == '?')
4507 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004508 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 garray_T *instr = &cctx->ctx_instr;
4510 garray_T *stack = &cctx->ctx_type_stack;
4511 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004512 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004514 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 int has_const_expr = FALSE;
4516 int const_value = FALSE;
4517 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 if (next != NULL)
4520 {
4521 *arg = next_line_from_context(cctx, TRUE);
4522 p = skipwhite(*arg);
4523 }
4524
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004525 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004526 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004527 semsg(_(e_white_space_required_before_and_after_str),
4528 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004529 return FAIL;
4530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (ppconst->pp_used == ppconst_used + 1)
4533 {
4534 // the condition is a constant, we know whether the ? or the :
4535 // expression is to be evaluated.
4536 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004537 if (op_falsy)
4538 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4539 else
4540 {
4541 int error = FALSE;
4542
4543 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4544 &error);
4545 if (error)
4546 return FAIL;
4547 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004548 cctx->ctx_skip = save_skip == SKIP_YES ||
4549 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4550
4551 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4552 // "left ?? right" and "left" is truthy: produce "left"
4553 generate_ppconst(cctx, ppconst);
4554 else
4555 {
4556 clear_tv(&ppconst->pp_tv[ppconst_used]);
4557 --ppconst->pp_used;
4558 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 }
4560 else
4561 {
4562 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004563 if (op_falsy)
4564 end_idx = instr->ga_len;
4565 generate_JUMP(cctx, op_falsy
4566 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4567 if (op_falsy)
4568 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570
4571 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004572 *arg = skipwhite(p + 1 + op_falsy);
4573 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (!has_const_expr)
4579 {
4580 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004582 if (!op_falsy)
4583 {
4584 // remember the type and drop it
4585 --stack->ga_len;
4586 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004588 end_idx = instr->ga_len;
4589 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004591 // jump here from JUMP_IF_FALSE
4592 isn = ((isn_T *)instr->ga_data) + alt_idx;
4593 isn->isn_arg.jump.jump_where = instr->ga_len;
4594 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004597 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004599 // Check for the ":".
4600 p = may_peek_next_line(cctx, *arg, &next);
4601 if (*p != ':')
4602 {
4603 emsg(_(e_missing_colon));
4604 return FAIL;
4605 }
4606 if (next != NULL)
4607 {
4608 *arg = next_line_from_context(cctx, TRUE);
4609 p = skipwhite(*arg);
4610 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004611
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004612 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4613 {
4614 semsg(_(e_white_space_required_before_and_after_str), ":");
4615 return FAIL;
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004618 // evaluate the third expression
4619 if (has_const_expr)
4620 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004621 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004622 *arg = skipwhite(p + 1);
4623 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4624 return FAIL;
4625 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4626 return FAIL;
4627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 if (!has_const_expr)
4630 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004631 type_T **typep;
4632
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004636 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4637 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 isn = ((isn_T *)instr->ga_data) + end_idx;
4641 isn->isn_arg.jump.jump_where = instr->ga_len;
4642 }
4643
4644 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 }
4646 return OK;
4647}
4648
4649/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004651 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4652 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 */
4654 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004655compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656{
4657 ppconst_T ppconst;
4658
4659 CLEAR_FIELD(ppconst);
4660 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4661 {
4662 clear_ppconst(&ppconst);
4663 return FAIL;
4664 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004665 if (is_const != NULL)
4666 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 if (generate_ppconst(cctx, &ppconst) == FAIL)
4668 return FAIL;
4669 return OK;
4670}
4671
4672/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004673 * Toplevel expression.
4674 */
4675 static int
4676compile_expr0(char_u **arg, cctx_T *cctx)
4677{
4678 return compile_expr0_ext(arg, cctx, NULL);
4679}
4680
4681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 * compile "return [expr]"
4683 */
4684 static char_u *
4685compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4686{
4687 char_u *p = arg;
4688 garray_T *stack = &cctx->ctx_type_stack;
4689 type_T *stack_type;
4690
4691 if (*p != NUL && *p != '|' && *p != '\n')
4692 {
4693 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 return NULL;
4696
4697 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4698 if (set_return_type)
4699 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004700 else
4701 {
4702 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4703 && stack_type->tt_type != VAR_VOID
4704 && stack_type->tt_type != VAR_UNKNOWN)
4705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004706 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004707 return NULL;
4708 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004709 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004710 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004711 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 }
4714 else
4715 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004716 // "set_return_type" cannot be TRUE, only used for a lambda which
4717 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004718 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4719 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004721 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 return NULL;
4723 }
4724
4725 // No argument, return zero.
4726 generate_PUSHNR(cctx, 0);
4727 }
4728
4729 if (generate_instr(cctx, ISN_RETURN) == NULL)
4730 return NULL;
4731
4732 // "return val | endif" is possible
4733 return skipwhite(p);
4734}
4735
4736/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004737 * Get a line from the compilation context, compatible with exarg_T getline().
4738 * Return a pointer to the line in allocated memory.
4739 * Return NULL for end-of-file or some error.
4740 */
4741 static char_u *
4742exarg_getline(
4743 int c UNUSED,
4744 void *cookie,
4745 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004746 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004747{
4748 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004749 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004750
Bram Moolenaar66250c92020-08-20 15:02:42 +02004751 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004752 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004753 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004754 return NULL;
4755 ++cctx->ctx_lnum;
4756 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4757 // Comment lines result in NULL pointers, skip them.
4758 if (p != NULL)
4759 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004760 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004761}
4762
4763/*
4764 * Compile a nested :def command.
4765 */
4766 static char_u *
4767compile_nested_function(exarg_T *eap, cctx_T *cctx)
4768{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004769 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004770 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004771 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004772 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004774 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004775
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004776 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004777 {
4778 emsg(_(e_cannot_use_bang_with_nested_def));
4779 return NULL;
4780 }
4781
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004782 // Only g:Func() can use a namespace.
4783 if (name_start[1] == ':' && !is_global)
4784 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004785 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004786 return NULL;
4787 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004788 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4789 return NULL;
4790
Bram Moolenaar04b12692020-05-04 23:24:44 +02004791 eap->arg = name_end;
4792 eap->getline = exarg_getline;
4793 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004794 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004796 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004797 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004798
Bram Moolenaar822ba242020-05-24 23:00:18 +02004799 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004800 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004801 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004802 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004803 {
4804 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004805 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004806 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004807
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004808 if (is_global)
4809 {
4810 char_u *func_name = vim_strnsave(name_start + 2,
4811 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004812
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004813 if (func_name == NULL)
4814 r = FAIL;
4815 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004816 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004817 }
4818 else
4819 {
4820 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004821 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004822 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004823 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004824
Bram Moolenaareef21022020-08-01 22:16:43 +02004825 if (lvar == NULL)
4826 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004827 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004828 return NULL;
4829 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004830
4831 // copy over the block scope IDs
4832 if (block_depth > 0)
4833 {
4834 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4835 if (ufunc->uf_block_ids != NULL)
4836 {
4837 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4838 sizeof(int) * block_depth);
4839 ufunc->uf_block_depth = block_depth;
4840 }
4841 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004842 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004843
Bram Moolenaar61a89812020-05-07 16:58:17 +02004844 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004845 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004846}
4847
4848/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 * Return the length of an assignment operator, or zero if there isn't one.
4850 */
4851 int
4852assignment_len(char_u *p, int *heredoc)
4853{
4854 if (*p == '=')
4855 {
4856 if (p[1] == '<' && p[2] == '<')
4857 {
4858 *heredoc = TRUE;
4859 return 3;
4860 }
4861 return 1;
4862 }
4863 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4864 return 2;
4865 if (STRNCMP(p, "..=", 3) == 0)
4866 return 3;
4867 return 0;
4868}
4869
4870// words that cannot be used as a variable
4871static char *reserved[] = {
4872 "true",
4873 "false",
4874 NULL
4875};
4876
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004877typedef enum {
4878 dest_local,
4879 dest_option,
4880 dest_env,
4881 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004882 dest_buffer,
4883 dest_window,
4884 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004885 dest_vimvar,
4886 dest_script,
4887 dest_reg,
4888} assign_dest_T;
4889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004891 * Generate the load instruction for "name".
4892 */
4893 static void
4894generate_loadvar(
4895 cctx_T *cctx,
4896 assign_dest_T dest,
4897 char_u *name,
4898 lvar_T *lvar,
4899 type_T *type)
4900{
4901 switch (dest)
4902 {
4903 case dest_option:
4904 // TODO: check the option exists
4905 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4906 break;
4907 case dest_global:
4908 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4909 break;
4910 case dest_buffer:
4911 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4912 break;
4913 case dest_window:
4914 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4915 break;
4916 case dest_tab:
4917 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4918 break;
4919 case dest_script:
4920 compile_load_scriptvar(cctx,
4921 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4922 break;
4923 case dest_env:
4924 // Include $ in the name here
4925 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4926 break;
4927 case dest_reg:
4928 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4929 break;
4930 case dest_vimvar:
4931 generate_LOADV(cctx, name + 2, TRUE);
4932 break;
4933 case dest_local:
4934 if (lvar->lv_from_outer)
4935 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4936 NULL, type);
4937 else
4938 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4939 break;
4940 }
4941}
4942
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004943 void
4944vim9_declare_error(char_u *name)
4945{
4946 char *scope = "";
4947
4948 switch (*name)
4949 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004950 case 'g': scope = _("global"); break;
4951 case 'b': scope = _("buffer"); break;
4952 case 'w': scope = _("window"); break;
4953 case 't': scope = _("tab"); break;
4954 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004955 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004956 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004957 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004958 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004959 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004960 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004961 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004962 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004963 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004964}
4965
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004966/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004968 * "let name"
4969 * "var name = expr"
4970 * "final name = expr"
4971 * "const name = expr"
4972 * "name = expr"
4973 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 * Return NULL for an error.
4975 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 */
4977 static char_u *
4978compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4979{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004982 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 char_u *ret = NULL;
4984 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004986 int scriptvar_sid = 0;
4987 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004990 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 int oplen = 0;
4993 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004994 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004995 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004998 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4999 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 // Skip over the "var" or "[var, var]" to get to any "=".
5002 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5003 if (p == NULL)
5004 return *arg == '[' ? arg : NULL;
5005
5006 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005008 // TODO: should we allow this, and figure out type inference from list
5009 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005010 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 return NULL;
5012 }
5013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 sp = p;
5015 p = skipwhite(p);
5016 op = p;
5017 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018
5019 if (var_count > 0 && oplen == 0)
5020 // can be something like "[1, 2]->func()"
5021 return arg;
5022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5024 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005025 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005027 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 if (heredoc)
5030 {
5031 list_T *l;
5032 listitem_T *li;
5033
5034 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005035 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005037 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005038 if (l == NULL)
5039 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040
Bram Moolenaar078269b2020-09-21 20:35:55 +02005041 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005043 // Push each line and the create the list.
5044 FOR_ALL_LIST_ITEMS(l, li)
5045 {
5046 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5047 li->li_tv.vval.v_string = NULL;
5048 }
5049 generate_NEWLIST(cctx, l->lv_len);
5050 type = &t_list_string;
5051 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 list_free(l);
5054 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005055 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 // for "[var, var] = expr" evaluate the expression here, loop over the
5060 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005061
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 p = skipwhite(op + oplen);
5063 if (compile_expr0(&p, cctx) == FAIL)
5064 return NULL;
5065 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005067 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005069 type_T *stacktype;
5070
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005071 stacktype = stack->ga_len == 0 ? &t_void
5072 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005075 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 goto theend;
5077 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005078 if (need_type(stacktype, &t_list_any, -1, cctx,
5079 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005081 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005082 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5083 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 }
5085 }
5086
5087 /*
5088 * Loop over variables in "[var, var] = expr".
5089 * For "var = expr" and "let var: type" this is done only once.
5090 */
5091 if (var_count > 0)
5092 var_start = skipwhite(arg + 1); // skip over the "["
5093 else
5094 var_start = arg;
5095 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5096 {
5097 char_u *var_end = skip_var_one(var_start, FALSE);
5098 size_t varlen;
5099 int new_local = FALSE;
5100 int opt_type;
5101 int opt_flags = 0;
5102 assign_dest_T dest = dest_local;
5103 int vimvaridx = -1;
5104 lvar_T *lvar = NULL;
5105 lvar_T arg_lvar;
5106 int has_type = FALSE;
5107 int has_index = FALSE;
5108 int instr_count = -1;
5109
Bram Moolenaar65821722020-08-02 18:58:54 +02005110 if (*var_start == '@')
5111 p = var_start + 2;
5112 else
5113 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005114 // skip over the leading "&", "&l:", "&g:" and "$"
5115 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005116 p = to_name_end(p, TRUE);
5117 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118
5119 // "a: type" is declaring variable "a" with a type, not "a:".
5120 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5121 --var_end;
5122 if (is_decl && p == var_start + 2 && p[-1] == ':')
5123 --p;
5124
5125 varlen = p - var_start;
5126 vim_free(name);
5127 name = vim_strnsave(var_start, varlen);
5128 if (name == NULL)
5129 return NULL;
5130 if (!heredoc)
5131 type = &t_any;
5132
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005133 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005135 int declare_error = FALSE;
5136
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 if (*var_start == '&')
5138 {
5139 int cc;
5140 long numval;
5141
5142 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005143 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 emsg(_(e_const_option));
5146 goto theend;
5147 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005148 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 p = var_start;
5150 p = find_option_end(&p, &opt_flags);
5151 if (p == NULL)
5152 {
5153 // cannot happen?
5154 emsg(_(e_letunexp));
5155 goto theend;
5156 }
5157 cc = *p;
5158 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005159 opt_type = get_option_value(skip_option_env_lead(var_start),
5160 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005161 *p = cc;
5162 if (opt_type == -3)
5163 {
5164 semsg(_(e_unknown_option), var_start);
5165 goto theend;
5166 }
5167 if (opt_type == -2 || opt_type == 0)
5168 type = &t_string;
5169 else
5170 type = &t_number; // both number and boolean option
5171 }
5172 else if (*var_start == '$')
5173 {
5174 dest = dest_env;
5175 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005176 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 }
5178 else if (*var_start == '@')
5179 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005180 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 {
5182 emsg_invreg(var_start[1]);
5183 goto theend;
5184 }
5185 dest = dest_reg;
5186 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005187 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005189 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005190 {
5191 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005192 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005193 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005194 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 {
5196 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005197 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005199 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 {
5201 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005202 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005204 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205 {
5206 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005207 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005208 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005209 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 {
5211 typval_T *vtv;
5212 int di_flags;
5213
5214 vimvaridx = find_vim_var(name + 2, &di_flags);
5215 if (vimvaridx < 0)
5216 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005217 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005218 goto theend;
5219 }
5220 // We use the current value of "sandbox" here, is that OK?
5221 if (var_check_ro(di_flags, name, FALSE))
5222 goto theend;
5223 dest = dest_vimvar;
5224 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005225 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005226 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 }
5228 else
5229 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005230 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231
5232 for (idx = 0; reserved[idx] != NULL; ++idx)
5233 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005234 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005235 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005236 goto theend;
5237 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238
5239 lvar = lookup_local(var_start, varlen, cctx);
5240 if (lvar == NULL)
5241 {
5242 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005243 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5245 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005246 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005247 if (is_decl)
5248 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005249 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005250 goto theend;
5251 }
5252 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005253 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005254 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255 if (lvar != NULL)
5256 {
5257 if (is_decl)
5258 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005259 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 goto theend;
5261 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005263 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005265 int script_namespace = varlen > 1
5266 && STRNCMP(var_start, "s:", 2) == 0;
5267 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005268 ? script_var_exists(var_start + 2, varlen - 2,
5269 FALSE, cctx)
5270 : script_var_exists(var_start, varlen,
5271 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005272 imported_T *import =
5273 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005274
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005275 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005276 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005277 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5278
5279 if (is_decl)
5280 {
5281 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005282 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005283 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005284 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005285 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005286 name);
5287 goto theend;
5288 }
5289 else if (cctx->ctx_ufunc->uf_script_ctx_version
5290 == SCRIPT_VERSION_VIM9
5291 && script_namespace
5292 && !script_var && import == NULL)
5293 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005294 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005295 goto theend;
5296 }
5297
5298 dest = dest_script;
5299
5300 // existing script-local variables should have a type
5301 scriptvar_sid = current_sctx.sc_sid;
5302 if (import != NULL)
5303 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005304 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005305 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005306 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005307 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005308 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005309 {
5310 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5311 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005312 ((svar_T *)si->sn_var_vals.ga_data)
5313 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005314 type = sv->sv_type;
5315 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005316 }
5317 }
5318 else if (name[1] == ':' && name[2] != NUL)
5319 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005320 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 goto theend;
5322 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005323 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005325 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005326 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005327 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005328 else if (check_defined(var_start, varlen, cctx) == FAIL)
5329 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005331 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005332
5333 if (declare_error)
5334 {
5335 vim9_declare_error(name);
5336 goto theend;
5337 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 }
5339
5340 // handle "a:name" as a name, not index "name" on "a"
5341 if (varlen > 1 || var_start[varlen] != ':')
5342 p = var_end;
5343
5344 if (dest != dest_option)
5345 {
5346 if (is_decl && *p == ':')
5347 {
5348 // parse optional type: "let var: type = expr"
5349 if (!VIM_ISWHITE(p[1]))
5350 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005351 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 goto theend;
5353 }
5354 p = skipwhite(p + 1);
5355 type = parse_type(&p, cctx->ctx_type_list);
5356 has_type = TRUE;
5357 }
5358 else if (lvar != NULL)
5359 type = lvar->lv_type;
5360 }
5361
5362 if (oplen == 3 && !heredoc && dest != dest_global
5363 && type->tt_type != VAR_STRING
5364 && type->tt_type != VAR_ANY)
5365 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005366 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367 goto theend;
5368 }
5369
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005370 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 {
5372 if (oplen > 1 && !heredoc)
5373 {
5374 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005375 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 goto theend;
5377 }
5378
5379 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005380 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5381 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 goto theend;
5383 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005384 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005385 if (lvar == NULL)
5386 goto theend;
5387 new_local = TRUE;
5388 }
5389
5390 member_type = type;
5391 if (var_end > var_start + varlen)
5392 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005393 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 if (is_decl)
5395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005396 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 goto theend;
5398 }
5399
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005400 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 {
5402 has_index = TRUE;
5403 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005404 member_type = &t_any;
5405 else
5406 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 }
5408 else
5409 {
5410 semsg("Not supported yet: %s", var_start);
5411 goto theend;
5412 }
5413 }
5414 else if (lvar == &arg_lvar)
5415 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005416 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005417 goto theend;
5418 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005419 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5420 {
5421 semsg(_(e_cannot_assign_to_constant), name);
5422 goto theend;
5423 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424
5425 if (!heredoc)
5426 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005427 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005429 if (oplen > 0 && var_count == 0)
5430 {
5431 // skip over the "=" and the expression
5432 p = skipwhite(op + oplen);
5433 compile_expr0(&p, cctx);
5434 }
5435 }
5436 else if (oplen > 0)
5437 {
5438 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005439 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005440
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 // For "var = expr" evaluate the expression.
5442 if (var_count == 0)
5443 {
5444 int r;
5445
5446 // for "+=", "*=", "..=" etc. first load the current value
5447 if (*op != '=')
5448 {
5449 generate_loadvar(cctx, dest, name, lvar, type);
5450
5451 if (has_index)
5452 {
5453 // TODO: get member from list or dict
5454 emsg("Index with operation not supported yet");
5455 goto theend;
5456 }
5457 }
5458
5459 // Compile the expression. Temporarily hide the new local
5460 // variable here, it is not available to this expression.
5461 if (new_local)
5462 --cctx->ctx_locals.ga_len;
5463 instr_count = instr->ga_len;
5464 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005465 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 if (new_local)
5467 ++cctx->ctx_locals.ga_len;
5468 if (r == FAIL)
5469 goto theend;
5470 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005471 else if (semicolon && var_idx == var_count - 1)
5472 {
5473 // For "[var; var] = expr" get the rest of the list
5474 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5475 goto theend;
5476 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005477 else
5478 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005479 // For "[var, var] = expr" get the "var_idx" item from the
5480 // list.
5481 if (generate_GETITEM(cctx, var_idx) == FAIL)
5482 return FAIL;
5483 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005484
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005485 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005486 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005487 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005489 if ((stacktype->tt_type == VAR_FUNC
5490 || stacktype->tt_type == VAR_PARTIAL)
5491 && var_wrong_func_name(name, TRUE))
5492 goto theend;
5493
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005494 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005495 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005496 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005498 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005499 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005500 }
5501 else
5502 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005503 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005504 // for a variable that implies &t_any.
5505 if (stacktype == &t_list_empty)
5506 lvar->lv_type = &t_list_any;
5507 else if (stacktype == &t_dict_empty)
5508 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005509 else if (stacktype == &t_unknown)
5510 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005511 else
5512 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005515 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005516 {
5517 type_T *use_type = lvar->lv_type;
5518
Bram Moolenaar71abe482020-09-14 22:28:30 +02005519 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005520 if (has_index)
5521 {
5522 use_type = use_type->tt_member;
5523 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005524 // could be indexing "any"
5525 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005526 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005527 if (need_type(stacktype, use_type, -1, cctx,
5528 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005529 goto theend;
5530 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005531 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005532 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005533 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005534 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005536 else if (cmdidx == CMD_final)
5537 {
5538 emsg(_(e_final_requires_a_value));
5539 goto theend;
5540 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005541 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005543 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 goto theend;
5545 }
5546 else if (!has_type || dest == dest_option)
5547 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005548 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 goto theend;
5550 }
5551 else
5552 {
5553 // variables are always initialized
5554 if (ga_grow(instr, 1) == FAIL)
5555 goto theend;
5556 switch (member_type->tt_type)
5557 {
5558 case VAR_BOOL:
5559 generate_PUSHBOOL(cctx, VVAL_FALSE);
5560 break;
5561 case VAR_FLOAT:
5562#ifdef FEAT_FLOAT
5563 generate_PUSHF(cctx, 0.0);
5564#endif
5565 break;
5566 case VAR_STRING:
5567 generate_PUSHS(cctx, NULL);
5568 break;
5569 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005570 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 break;
5572 case VAR_FUNC:
5573 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5574 break;
5575 case VAR_LIST:
5576 generate_NEWLIST(cctx, 0);
5577 break;
5578 case VAR_DICT:
5579 generate_NEWDICT(cctx, 0);
5580 break;
5581 case VAR_JOB:
5582 generate_PUSHJOB(cctx, NULL);
5583 break;
5584 case VAR_CHANNEL:
5585 generate_PUSHCHANNEL(cctx, NULL);
5586 break;
5587 case VAR_NUMBER:
5588 case VAR_UNKNOWN:
5589 case VAR_ANY:
5590 case VAR_PARTIAL:
5591 case VAR_VOID:
5592 case VAR_SPECIAL: // cannot happen
5593 generate_PUSHNR(cctx, 0);
5594 break;
5595 }
5596 }
5597 if (var_count == 0)
5598 end = p;
5599 }
5600
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005601 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005602 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005603 break;
5604
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005605 if (oplen > 0 && *op != '=')
5606 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005607 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 type_T *stacktype;
5609
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005610 if (*op == '.')
5611 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005612 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005613 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005615 if (
5616#ifdef FEAT_FLOAT
5617 // If variable is float operation with number is OK.
5618 !(expected == &t_float && stacktype == &t_number) &&
5619#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005620 need_type(stacktype, expected, -1, cctx,
5621 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 goto theend;
5623
5624 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005625 {
5626 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5627 goto theend;
5628 }
5629 else if (*op == '+')
5630 {
5631 if (generate_add_instr(cctx,
5632 operator_type(member_type, stacktype),
5633 member_type, stacktype) == FAIL)
5634 goto theend;
5635 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005636 else if (generate_two_op(cctx, op) == FAIL)
5637 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005641 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 int r;
5643
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005644 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 if (new_local)
5646 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005647 p = var_start + varlen;
5648 if (*p == '[')
5649 {
5650 p = skipwhite(p + 1);
5651 r = compile_expr0(&p, cctx);
5652 if (r == OK && *skipwhite(p) != ']')
5653 {
5654 // this should not happen
5655 emsg(_(e_missbrac));
5656 r = FAIL;
5657 }
5658 }
5659 else // if (*p == '.')
5660 {
5661 char_u *key_end = to_name_end(p + 1, TRUE);
5662 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5663
5664 r = generate_PUSHS(cctx, key);
5665 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005666 if (new_local)
5667 ++cctx->ctx_locals.ga_len;
5668 if (r == FAIL)
5669 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005670
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005671 if (type == &t_any)
5672 {
5673 type_T *idx_type = ((type_T **)stack->ga_data)[
5674 stack->ga_len - 1];
5675 // Index on variable of unknown type: guess the type from the
5676 // index type: number is dict, otherwise dict.
5677 // TODO: should do the assignment at runtime
5678 if (idx_type->tt_type == VAR_NUMBER)
5679 type = &t_list_any;
5680 else
5681 type = &t_dict_any;
5682 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005683 if (type->tt_type == VAR_DICT
5684 && may_generate_2STRING(-1, cctx) == FAIL)
5685 goto theend;
5686 if (type->tt_type == VAR_LIST
5687 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005688 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005689 {
5690 emsg(_(e_number_exp));
5691 goto theend;
5692 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005693
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005694 // Load the dict or list. On the stack we then have:
5695 // - value
5696 // - index
5697 // - variable
5698 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005699
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005700 if (type->tt_type == VAR_LIST)
5701 {
5702 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5703 return FAIL;
5704 }
5705 else if (type->tt_type == VAR_DICT)
5706 {
5707 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5708 return FAIL;
5709 }
5710 else
5711 {
5712 emsg(_(e_listreq));
5713 goto theend;
5714 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005715 }
5716 else
5717 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005718 if (is_decl && cmdidx == CMD_const
5719 && (dest == dest_script || dest == dest_local))
5720 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005721 generate_LOCKCONST(cctx);
5722
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005723 switch (dest)
5724 {
5725 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005726 generate_STOREOPT(cctx, skip_option_env_lead(name),
5727 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005728 break;
5729 case dest_global:
5730 // include g: with the name, easier to execute that way
5731 generate_STORE(cctx, ISN_STOREG, 0, name);
5732 break;
5733 case dest_buffer:
5734 // include b: with the name, easier to execute that way
5735 generate_STORE(cctx, ISN_STOREB, 0, name);
5736 break;
5737 case dest_window:
5738 // include w: with the name, easier to execute that way
5739 generate_STORE(cctx, ISN_STOREW, 0, name);
5740 break;
5741 case dest_tab:
5742 // include t: with the name, easier to execute that way
5743 generate_STORE(cctx, ISN_STORET, 0, name);
5744 break;
5745 case dest_env:
5746 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5747 break;
5748 case dest_reg:
5749 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5750 break;
5751 case dest_vimvar:
5752 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5753 break;
5754 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005755 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005756 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005757 {
5758 char_u *name_s = name;
5759
5760 // Include s: in the name for store_var()
5761 if (name[1] != ':')
5762 {
5763 int len = (int)STRLEN(name) + 3;
5764
5765 name_s = alloc(len);
5766 if (name_s == NULL)
5767 name_s = name;
5768 else
5769 vim_snprintf((char *)name_s, len,
5770 "s:%s", name);
5771 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005772 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5773 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005774 if (name_s != name)
5775 vim_free(name_s);
5776 }
5777 else
5778 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005779 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005780 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005781 break;
5782 case dest_local:
5783 if (lvar != NULL)
5784 {
5785 isn_T *isn = ((isn_T *)instr->ga_data)
5786 + instr->ga_len - 1;
5787
5788 // optimization: turn "var = 123" from ISN_PUSHNR +
5789 // ISN_STORE into ISN_STORENR
5790 if (!lvar->lv_from_outer
5791 && instr->ga_len == instr_count + 1
5792 && isn->isn_type == ISN_PUSHNR)
5793 {
5794 varnumber_T val = isn->isn_arg.number;
5795
5796 isn->isn_type = ISN_STORENR;
5797 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5798 isn->isn_arg.storenr.stnr_val = val;
5799 if (stack->ga_len > 0)
5800 --stack->ga_len;
5801 }
5802 else if (lvar->lv_from_outer)
5803 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005804 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005805 else
5806 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5807 }
5808 break;
5809 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005810 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005811
5812 if (var_idx + 1 < var_count)
5813 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005815
5816 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005817 if (var_count > 0 && !semicolon)
5818 {
5819 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5820 goto theend;
5821 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005822
Bram Moolenaarb2097502020-07-19 17:17:02 +02005823 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824
5825theend:
5826 vim_free(name);
5827 return ret;
5828}
5829
5830/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005831 * Check if "name" can be "unlet".
5832 */
5833 int
5834check_vim9_unlet(char_u *name)
5835{
5836 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5837 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005838 // "unlet s:var" is allowed in legacy script.
5839 if (*name == 's' && !script_is_vim9())
5840 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005841 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005842 return FAIL;
5843 }
5844 return OK;
5845}
5846
5847/*
5848 * Callback passed to ex_unletlock().
5849 */
5850 static int
5851compile_unlet(
5852 lval_T *lvp,
5853 char_u *name_end,
5854 exarg_T *eap,
5855 int deep UNUSED,
5856 void *coookie)
5857{
5858 cctx_T *cctx = coookie;
5859
5860 if (lvp->ll_tv == NULL)
5861 {
5862 char_u *p = lvp->ll_name;
5863 int cc = *name_end;
5864 int ret = OK;
5865
5866 // Normal name. Only supports g:, w:, t: and b: namespaces.
5867 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005868 if (*p == '$')
5869 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5870 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005871 ret = FAIL;
5872 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005873 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005874
5875 *name_end = cc;
5876 return ret;
5877 }
5878
5879 // TODO: unlet {list}[idx]
5880 // TODO: unlet {dict}[key]
5881 emsg("Sorry, :unlet not fully implemented yet");
5882 return FAIL;
5883}
5884
5885/*
5886 * compile "unlet var", "lock var" and "unlock var"
5887 * "arg" points to "var".
5888 */
5889 static char_u *
5890compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5891{
5892 char_u *p = arg;
5893
5894 if (eap->cmdidx != CMD_unlet)
5895 {
5896 emsg("Sorry, :lock and unlock not implemented yet");
5897 return NULL;
5898 }
5899
5900 if (*p == '!')
5901 {
5902 p = skipwhite(p + 1);
5903 eap->forceit = TRUE;
5904 }
5905
5906 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5907 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5908}
5909
5910/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 * Compile an :import command.
5912 */
5913 static char_u *
5914compile_import(char_u *arg, cctx_T *cctx)
5915{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005916 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917}
5918
5919/*
5920 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5921 */
5922 static int
5923compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5924{
5925 garray_T *instr = &cctx->ctx_instr;
5926 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5927
5928 if (endlabel == NULL)
5929 return FAIL;
5930 endlabel->el_next = *el;
5931 *el = endlabel;
5932 endlabel->el_end_label = instr->ga_len;
5933
5934 generate_JUMP(cctx, when, 0);
5935 return OK;
5936}
5937
5938 static void
5939compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5940{
5941 garray_T *instr = &cctx->ctx_instr;
5942
5943 while (*el != NULL)
5944 {
5945 endlabel_T *cur = (*el);
5946 isn_T *isn;
5947
5948 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5949 isn->isn_arg.jump.jump_where = instr->ga_len;
5950 *el = cur->el_next;
5951 vim_free(cur);
5952 }
5953}
5954
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005955 static void
5956compile_free_jump_to_end(endlabel_T **el)
5957{
5958 while (*el != NULL)
5959 {
5960 endlabel_T *cur = (*el);
5961
5962 *el = cur->el_next;
5963 vim_free(cur);
5964 }
5965}
5966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967/*
5968 * Create a new scope and set up the generic items.
5969 */
5970 static scope_T *
5971new_scope(cctx_T *cctx, scopetype_T type)
5972{
5973 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5974
5975 if (scope == NULL)
5976 return NULL;
5977 scope->se_outer = cctx->ctx_scope;
5978 cctx->ctx_scope = scope;
5979 scope->se_type = type;
5980 scope->se_local_count = cctx->ctx_locals.ga_len;
5981 return scope;
5982}
5983
5984/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005985 * Free the current scope and go back to the outer scope.
5986 */
5987 static void
5988drop_scope(cctx_T *cctx)
5989{
5990 scope_T *scope = cctx->ctx_scope;
5991
5992 if (scope == NULL)
5993 {
5994 iemsg("calling drop_scope() without a scope");
5995 return;
5996 }
5997 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005998 switch (scope->se_type)
5999 {
6000 case IF_SCOPE:
6001 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6002 case FOR_SCOPE:
6003 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6004 case WHILE_SCOPE:
6005 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6006 case TRY_SCOPE:
6007 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6008 case NO_SCOPE:
6009 case BLOCK_SCOPE:
6010 break;
6011 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006012 vim_free(scope);
6013}
6014
6015/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 * compile "if expr"
6017 *
6018 * "if expr" Produces instructions:
6019 * EVAL expr Push result of "expr"
6020 * JUMP_IF_FALSE end
6021 * ... body ...
6022 * end:
6023 *
6024 * "if expr | else" Produces instructions:
6025 * EVAL expr Push result of "expr"
6026 * JUMP_IF_FALSE else
6027 * ... body ...
6028 * JUMP_ALWAYS end
6029 * else:
6030 * ... body ...
6031 * end:
6032 *
6033 * "if expr1 | elseif expr2 | else" Produces instructions:
6034 * EVAL expr Push result of "expr"
6035 * JUMP_IF_FALSE elseif
6036 * ... body ...
6037 * JUMP_ALWAYS end
6038 * elseif:
6039 * EVAL expr Push result of "expr"
6040 * JUMP_IF_FALSE else
6041 * ... body ...
6042 * JUMP_ALWAYS end
6043 * else:
6044 * ... body ...
6045 * end:
6046 */
6047 static char_u *
6048compile_if(char_u *arg, cctx_T *cctx)
6049{
6050 char_u *p = arg;
6051 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006052 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006054 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006055 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056
Bram Moolenaara5565e42020-05-09 15:44:01 +02006057 CLEAR_FIELD(ppconst);
6058 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006059 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006060 clear_ppconst(&ppconst);
6061 return NULL;
6062 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006063 if (cctx->ctx_skip == SKIP_YES)
6064 clear_ppconst(&ppconst);
6065 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006066 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006067 int error = FALSE;
6068 int v;
6069
Bram Moolenaara5565e42020-05-09 15:44:01 +02006070 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006071 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006072 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006073 if (error)
6074 return NULL;
6075 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006076 }
6077 else
6078 {
6079 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006080 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006081 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006082 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006083 if (bool_on_stack(cctx) == FAIL)
6084 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086
6087 scope = new_scope(cctx, IF_SCOPE);
6088 if (scope == NULL)
6089 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006090 scope->se_skip_save = skip_save;
6091 // "is_had_return" will be reset if any block does not end in :return
6092 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006094 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006095 {
6096 // "where" is set when ":elseif", "else" or ":endif" is found
6097 scope->se_u.se_if.is_if_label = instr->ga_len;
6098 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6099 }
6100 else
6101 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102
6103 return p;
6104}
6105
6106 static char_u *
6107compile_elseif(char_u *arg, cctx_T *cctx)
6108{
6109 char_u *p = arg;
6110 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006111 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 isn_T *isn;
6113 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006114 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006115 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
6117 if (scope == NULL || scope->se_type != IF_SCOPE)
6118 {
6119 emsg(_(e_elseif_without_if));
6120 return NULL;
6121 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006122 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006123 if (!cctx->ctx_had_return)
6124 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006126 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006127 {
6128 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006130 return NULL;
6131 // previous "if" or "elseif" jumps here
6132 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6133 isn->isn_arg.jump.jump_where = instr->ga_len;
6134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006136 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006137 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006138 if (cctx->ctx_skip == SKIP_YES)
6139 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006140 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006141 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006142 clear_ppconst(&ppconst);
6143 return NULL;
6144 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006145 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006146 if (scope->se_skip_save == SKIP_YES)
6147 clear_ppconst(&ppconst);
6148 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006149 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006150 int error = FALSE;
6151 int v;
6152
Bram Moolenaar7f141552020-05-09 17:35:53 +02006153 // The expression results in a constant.
6154 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006155 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6156 if (error)
6157 return NULL;
6158 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006159 clear_ppconst(&ppconst);
6160 scope->se_u.se_if.is_if_label = -1;
6161 }
6162 else
6163 {
6164 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006165 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006166 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006167 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006168 if (bool_on_stack(cctx) == FAIL)
6169 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006171 // "where" is set when ":elseif", "else" or ":endif" is found
6172 scope->se_u.se_if.is_if_label = instr->ga_len;
6173 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175
6176 return p;
6177}
6178
6179 static char_u *
6180compile_else(char_u *arg, cctx_T *cctx)
6181{
6182 char_u *p = arg;
6183 garray_T *instr = &cctx->ctx_instr;
6184 isn_T *isn;
6185 scope_T *scope = cctx->ctx_scope;
6186
6187 if (scope == NULL || scope->se_type != IF_SCOPE)
6188 {
6189 emsg(_(e_else_without_if));
6190 return NULL;
6191 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006192 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006193 if (!cctx->ctx_had_return)
6194 scope->se_u.se_if.is_had_return = FALSE;
6195 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196
Bram Moolenaarefd88552020-06-18 20:50:10 +02006197 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006198 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006199 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006200 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006201 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006202 if (!cctx->ctx_had_return
6203 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6204 JUMP_ALWAYS, cctx) == FAIL)
6205 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006206 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006207
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006208 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006209 {
6210 if (scope->se_u.se_if.is_if_label >= 0)
6211 {
6212 // previous "if" or "elseif" jumps here
6213 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6214 isn->isn_arg.jump.jump_where = instr->ga_len;
6215 scope->se_u.se_if.is_if_label = -1;
6216 }
6217 }
6218
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006219 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006220 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222
6223 return p;
6224}
6225
6226 static char_u *
6227compile_endif(char_u *arg, cctx_T *cctx)
6228{
6229 scope_T *scope = cctx->ctx_scope;
6230 ifscope_T *ifscope;
6231 garray_T *instr = &cctx->ctx_instr;
6232 isn_T *isn;
6233
6234 if (scope == NULL || scope->se_type != IF_SCOPE)
6235 {
6236 emsg(_(e_endif_without_if));
6237 return NULL;
6238 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006239 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006240 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006241 if (!cctx->ctx_had_return)
6242 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006244 if (scope->se_u.se_if.is_if_label >= 0)
6245 {
6246 // previous "if" or "elseif" jumps here
6247 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6248 isn->isn_arg.jump.jump_where = instr->ga_len;
6249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 // Fill in the "end" label in jumps at the end of the blocks.
6251 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006252 cctx->ctx_skip = scope->se_skip_save;
6253
6254 // If all the blocks end in :return and there is an :else then the
6255 // had_return flag is set.
6256 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006258 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 return arg;
6260}
6261
6262/*
6263 * compile "for var in expr"
6264 *
6265 * Produces instructions:
6266 * PUSHNR -1
6267 * STORE loop-idx Set index to -1
6268 * EVAL expr Push result of "expr"
6269 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6270 * - if beyond end, jump to "end"
6271 * - otherwise get item from list and push it
6272 * STORE var Store item in "var"
6273 * ... body ...
6274 * JUMP top Jump back to repeat
6275 * end: DROP Drop the result of "expr"
6276 *
6277 */
6278 static char_u *
6279compile_for(char_u *arg, cctx_T *cctx)
6280{
6281 char_u *p;
6282 size_t varlen;
6283 garray_T *instr = &cctx->ctx_instr;
6284 garray_T *stack = &cctx->ctx_type_stack;
6285 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006286 lvar_T *loop_lvar; // loop iteration variable
6287 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 type_T *vartype;
6289
6290 // TODO: list of variables: "for [key, value] in dict"
6291 // parse "var"
6292 for (p = arg; eval_isnamec1(*p); ++p)
6293 ;
6294 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006295 var_lvar = lookup_local(arg, varlen, cctx);
6296 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006298 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 return NULL;
6300 }
6301
6302 // consume "in"
6303 p = skipwhite(p);
6304 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6305 {
6306 emsg(_(e_missing_in));
6307 return NULL;
6308 }
6309 p = skipwhite(p + 2);
6310
6311
6312 scope = new_scope(cctx, FOR_SCOPE);
6313 if (scope == NULL)
6314 return NULL;
6315
6316 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006317 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6318 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006319 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006320 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006321 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324
6325 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006326 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6327 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006328 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006329 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006330 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006334 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335
6336 // compile "expr", it remains on the stack until "endfor"
6337 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006338 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006339 {
6340 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006342 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006344 // Now that we know the type of "var", check that it is a list, now or at
6345 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006347 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006349 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 return NULL;
6351 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006352 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006353 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006356 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006358 generate_FOR(cctx, loop_lvar->lv_idx);
6359 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
6361 return arg;
6362}
6363
6364/*
6365 * compile "endfor"
6366 */
6367 static char_u *
6368compile_endfor(char_u *arg, cctx_T *cctx)
6369{
6370 garray_T *instr = &cctx->ctx_instr;
6371 scope_T *scope = cctx->ctx_scope;
6372 forscope_T *forscope;
6373 isn_T *isn;
6374
6375 if (scope == NULL || scope->se_type != FOR_SCOPE)
6376 {
6377 emsg(_(e_for));
6378 return NULL;
6379 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006380 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006382 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383
6384 // At end of ":for" scope jump back to the FOR instruction.
6385 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6386
6387 // Fill in the "end" label in the FOR statement so it can jump here
6388 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6389 isn->isn_arg.forloop.for_end = instr->ga_len;
6390
6391 // Fill in the "end" label any BREAK statements
6392 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6393
6394 // Below the ":for" scope drop the "expr" list from the stack.
6395 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6396 return NULL;
6397
6398 vim_free(scope);
6399
6400 return arg;
6401}
6402
6403/*
6404 * compile "while expr"
6405 *
6406 * Produces instructions:
6407 * top: EVAL expr Push result of "expr"
6408 * JUMP_IF_FALSE end jump if false
6409 * ... body ...
6410 * JUMP top Jump back to repeat
6411 * end:
6412 *
6413 */
6414 static char_u *
6415compile_while(char_u *arg, cctx_T *cctx)
6416{
6417 char_u *p = arg;
6418 garray_T *instr = &cctx->ctx_instr;
6419 scope_T *scope;
6420
6421 scope = new_scope(cctx, WHILE_SCOPE);
6422 if (scope == NULL)
6423 return NULL;
6424
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006425 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426
6427 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006428 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 return NULL;
6430
Bram Moolenaar13106602020-10-04 16:06:05 +02006431 if (bool_on_stack(cctx) == FAIL)
6432 return FAIL;
6433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006435 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 JUMP_IF_FALSE, cctx) == FAIL)
6437 return FAIL;
6438
6439 return p;
6440}
6441
6442/*
6443 * compile "endwhile"
6444 */
6445 static char_u *
6446compile_endwhile(char_u *arg, cctx_T *cctx)
6447{
6448 scope_T *scope = cctx->ctx_scope;
6449
6450 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6451 {
6452 emsg(_(e_while));
6453 return NULL;
6454 }
6455 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006456 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457
6458 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006459 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460
6461 // Fill in the "end" label in the WHILE statement so it can jump here.
6462 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006463 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464
6465 vim_free(scope);
6466
6467 return arg;
6468}
6469
6470/*
6471 * compile "continue"
6472 */
6473 static char_u *
6474compile_continue(char_u *arg, cctx_T *cctx)
6475{
6476 scope_T *scope = cctx->ctx_scope;
6477
6478 for (;;)
6479 {
6480 if (scope == NULL)
6481 {
6482 emsg(_(e_continue));
6483 return NULL;
6484 }
6485 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6486 break;
6487 scope = scope->se_outer;
6488 }
6489
6490 // Jump back to the FOR or WHILE instruction.
6491 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006492 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6493 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 return arg;
6495}
6496
6497/*
6498 * compile "break"
6499 */
6500 static char_u *
6501compile_break(char_u *arg, cctx_T *cctx)
6502{
6503 scope_T *scope = cctx->ctx_scope;
6504 endlabel_T **el;
6505
6506 for (;;)
6507 {
6508 if (scope == NULL)
6509 {
6510 emsg(_(e_break));
6511 return NULL;
6512 }
6513 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6514 break;
6515 scope = scope->se_outer;
6516 }
6517
6518 // Jump to the end of the FOR or WHILE loop.
6519 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006520 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006522 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6524 return FAIL;
6525
6526 return arg;
6527}
6528
6529/*
6530 * compile "{" start of block
6531 */
6532 static char_u *
6533compile_block(char_u *arg, cctx_T *cctx)
6534{
6535 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6536 return NULL;
6537 return skipwhite(arg + 1);
6538}
6539
6540/*
6541 * compile end of block: drop one scope
6542 */
6543 static void
6544compile_endblock(cctx_T *cctx)
6545{
6546 scope_T *scope = cctx->ctx_scope;
6547
6548 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006549 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 vim_free(scope);
6551}
6552
6553/*
6554 * compile "try"
6555 * Creates a new scope for the try-endtry, pointing to the first catch and
6556 * finally.
6557 * Creates another scope for the "try" block itself.
6558 * TRY instruction sets up exception handling at runtime.
6559 *
6560 * "try"
6561 * TRY -> catch1, -> finally push trystack entry
6562 * ... try block
6563 * "throw {exception}"
6564 * EVAL {exception}
6565 * THROW create exception
6566 * ... try block
6567 * " catch {expr}"
6568 * JUMP -> finally
6569 * catch1: PUSH exeception
6570 * EVAL {expr}
6571 * MATCH
6572 * JUMP nomatch -> catch2
6573 * CATCH remove exception
6574 * ... catch block
6575 * " catch"
6576 * JUMP -> finally
6577 * catch2: CATCH remove exception
6578 * ... catch block
6579 * " finally"
6580 * finally:
6581 * ... finally block
6582 * " endtry"
6583 * ENDTRY pop trystack entry, may rethrow
6584 */
6585 static char_u *
6586compile_try(char_u *arg, cctx_T *cctx)
6587{
6588 garray_T *instr = &cctx->ctx_instr;
6589 scope_T *try_scope;
6590 scope_T *scope;
6591
6592 // scope that holds the jumps that go to catch/finally/endtry
6593 try_scope = new_scope(cctx, TRY_SCOPE);
6594 if (try_scope == NULL)
6595 return NULL;
6596
6597 // "catch" is set when the first ":catch" is found.
6598 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006599 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 if (generate_instr(cctx, ISN_TRY) == NULL)
6601 return NULL;
6602
6603 // scope for the try block itself
6604 scope = new_scope(cctx, BLOCK_SCOPE);
6605 if (scope == NULL)
6606 return NULL;
6607
6608 return arg;
6609}
6610
6611/*
6612 * compile "catch {expr}"
6613 */
6614 static char_u *
6615compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6616{
6617 scope_T *scope = cctx->ctx_scope;
6618 garray_T *instr = &cctx->ctx_instr;
6619 char_u *p;
6620 isn_T *isn;
6621
6622 // end block scope from :try or :catch
6623 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6624 compile_endblock(cctx);
6625 scope = cctx->ctx_scope;
6626
6627 // Error if not in a :try scope
6628 if (scope == NULL || scope->se_type != TRY_SCOPE)
6629 {
6630 emsg(_(e_catch));
6631 return NULL;
6632 }
6633
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006634 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006636 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 return NULL;
6638 }
6639
6640 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006641 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 JUMP_ALWAYS, cctx) == FAIL)
6643 return NULL;
6644
6645 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006646 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 if (isn->isn_arg.try.try_catch == 0)
6648 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006649 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 {
6651 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006652 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653 isn->isn_arg.jump.jump_where = instr->ga_len;
6654 }
6655
6656 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006657 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006659 scope->se_u.se_try.ts_caught_all = TRUE;
6660 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 }
6662 else
6663 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006664 char_u *end;
6665 char_u *pat;
6666 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006667 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006668 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 // Push v:exception, push {expr} and MATCH
6671 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6672
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006673 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006674 if (*end != *p)
6675 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006676 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006677 vim_free(tofree);
6678 return FAIL;
6679 }
6680 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006681 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006682 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006683 len = (int)(end - tofree);
6684 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006685 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006686 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006687 if (pat == NULL)
6688 return FAIL;
6689 if (generate_PUSHS(cctx, pat) == FAIL)
6690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6693 return NULL;
6694
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006695 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6697 return NULL;
6698 }
6699
6700 if (generate_instr(cctx, ISN_CATCH) == NULL)
6701 return NULL;
6702
6703 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6704 return NULL;
6705 return p;
6706}
6707
6708 static char_u *
6709compile_finally(char_u *arg, cctx_T *cctx)
6710{
6711 scope_T *scope = cctx->ctx_scope;
6712 garray_T *instr = &cctx->ctx_instr;
6713 isn_T *isn;
6714
6715 // end block scope from :try or :catch
6716 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6717 compile_endblock(cctx);
6718 scope = cctx->ctx_scope;
6719
6720 // Error if not in a :try scope
6721 if (scope == NULL || scope->se_type != TRY_SCOPE)
6722 {
6723 emsg(_(e_finally));
6724 return NULL;
6725 }
6726
6727 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006728 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 if (isn->isn_arg.try.try_finally != 0)
6730 {
6731 emsg(_(e_finally_dup));
6732 return NULL;
6733 }
6734
6735 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006736 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737
Bram Moolenaar585fea72020-04-02 22:33:21 +02006738 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006739 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740 {
6741 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006742 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006744 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 }
6746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 // TODO: set index in ts_finally_label jumps
6748
6749 return arg;
6750}
6751
6752 static char_u *
6753compile_endtry(char_u *arg, cctx_T *cctx)
6754{
6755 scope_T *scope = cctx->ctx_scope;
6756 garray_T *instr = &cctx->ctx_instr;
6757 isn_T *isn;
6758
6759 // end block scope from :catch or :finally
6760 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6761 compile_endblock(cctx);
6762 scope = cctx->ctx_scope;
6763
6764 // Error if not in a :try scope
6765 if (scope == NULL || scope->se_type != TRY_SCOPE)
6766 {
6767 if (scope == NULL)
6768 emsg(_(e_no_endtry));
6769 else if (scope->se_type == WHILE_SCOPE)
6770 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006771 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 emsg(_(e_endfor));
6773 else
6774 emsg(_(e_endif));
6775 return NULL;
6776 }
6777
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006778 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6780 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006781 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 return NULL;
6783 }
6784
6785 // Fill in the "end" label in jumps at the end of the blocks, if not done
6786 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006787 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788
6789 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006790 if (isn->isn_arg.try.try_catch == 0)
6791 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 if (isn->isn_arg.try.try_finally == 0)
6793 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006794
6795 if (scope->se_u.se_try.ts_catch_label != 0)
6796 {
6797 // Last catch without match jumps here
6798 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6799 isn->isn_arg.jump.jump_where = instr->ga_len;
6800 }
6801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 compile_endblock(cctx);
6803
6804 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6805 return NULL;
6806 return arg;
6807}
6808
6809/*
6810 * compile "throw {expr}"
6811 */
6812 static char_u *
6813compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6814{
6815 char_u *p = skipwhite(arg);
6816
Bram Moolenaara5565e42020-05-09 15:44:01 +02006817 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 return NULL;
6819 if (may_generate_2STRING(-1, cctx) == FAIL)
6820 return NULL;
6821 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6822 return NULL;
6823
6824 return p;
6825}
6826
6827/*
6828 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006829 * compile "echomsg expr"
6830 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006831 * compile "execute expr"
6832 */
6833 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006834compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006835{
6836 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006837 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006838 int count = 0;
6839
6840 for (;;)
6841 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006842 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006843 return NULL;
6844 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006845 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006846 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006847 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006848 break;
6849 }
6850
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6852 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6853 else if (cmdidx == CMD_execute)
6854 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6855 else if (cmdidx == CMD_echomsg)
6856 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6857 else
6858 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859 return p;
6860}
6861
6862/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006863 * :put r
6864 * :put ={expr}
6865 */
6866 static char_u *
6867compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6868{
6869 char_u *line = arg;
6870 linenr_T lnum;
6871 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006872 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006873
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006874 eap->regname = *line;
6875
6876 if (eap->regname == '=')
6877 {
6878 char_u *p = line + 1;
6879
6880 if (compile_expr0(&p, cctx) == FAIL)
6881 return NULL;
6882 line = p;
6883 }
6884 else if (eap->regname != NUL)
6885 ++line;
6886
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006887 // "errormsg" will not be set because the range is ADDR_LINES.
6888 // TODO: if the range contains something like "$" or "." need to evaluate
6889 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006890 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6891 return NULL;
6892 if (eap->addr_count == 0)
6893 lnum = -1;
6894 else
6895 lnum = eap->line2;
6896 if (above)
6897 --lnum;
6898
6899 generate_PUT(cctx, eap->regname, lnum);
6900 return line;
6901}
6902
6903/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006904 * A command that is not compiled, execute with legacy code.
6905 */
6906 static char_u *
6907compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6908{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006909 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006910 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006911 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006912
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006913 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006914 goto theend;
6915
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006916 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006917 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006918 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006919 int usefilter = FALSE;
6920
6921 has_expr = argt & (EX_XFILE | EX_EXPAND);
6922
6923 // If the command can be followed by a bar, find the bar and truncate
6924 // it, so that the following command can be compiled.
6925 // The '|' is overwritten with a NUL, it is put back below.
6926 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6927 && *eap->arg == '!')
6928 // :w !filter or :r !filter or :r! filter
6929 usefilter = TRUE;
6930 if ((argt & EX_TRLBAR) && !usefilter)
6931 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006932 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006933 separate_nextcmd(eap);
6934 if (eap->nextcmd != NULL)
6935 nextcmd = eap->nextcmd;
6936 }
6937 }
6938
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006939 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6940 {
6941 // expand filename in "syntax include [@group] filename"
6942 has_expr = TRUE;
6943 eap->arg = skipwhite(eap->arg + 7);
6944 if (*eap->arg == '@')
6945 eap->arg = skiptowhite(eap->arg);
6946 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006947
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006948 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006949 {
6950 int count = 0;
6951 char_u *start = skipwhite(line);
6952
6953 // :cmd xxx`=expr1`yyy`=expr2`zzz
6954 // PUSHS ":cmd xxx"
6955 // eval expr1
6956 // PUSHS "yyy"
6957 // eval expr2
6958 // PUSHS "zzz"
6959 // EXECCONCAT 5
6960 for (;;)
6961 {
6962 if (p > start)
6963 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006964 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006965 ++count;
6966 }
6967 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006968 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006969 return NULL;
6970 may_generate_2STRING(-1, cctx);
6971 ++count;
6972 p = skipwhite(p);
6973 if (*p != '`')
6974 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006975 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006976 return NULL;
6977 }
6978 start = p + 1;
6979
6980 p = (char_u *)strstr((char *)start, "`=");
6981 if (p == NULL)
6982 {
6983 if (*skipwhite(start) != NUL)
6984 {
6985 generate_PUSHS(cctx, vim_strsave(start));
6986 ++count;
6987 }
6988 break;
6989 }
6990 }
6991 generate_EXECCONCAT(cctx, count);
6992 }
6993 else
6994 generate_EXEC(cctx, line);
6995
6996theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006997 if (*nextcmd != NUL)
6998 {
6999 // the parser expects a pointer to the bar, put it back
7000 --nextcmd;
7001 *nextcmd = '|';
7002 }
7003
7004 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007005}
7006
7007/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007008 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007009 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007010 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007011 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007012add_def_function(ufunc_T *ufunc)
7013{
7014 dfunc_T *dfunc;
7015
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007016 if (def_functions.ga_len == 0)
7017 {
7018 // The first position is not used, so that a zero uf_dfunc_idx means it
7019 // wasn't set.
7020 if (ga_grow(&def_functions, 1) == FAIL)
7021 return FAIL;
7022 ++def_functions.ga_len;
7023 }
7024
Bram Moolenaar09689a02020-05-09 22:50:08 +02007025 // Add the function to "def_functions".
7026 if (ga_grow(&def_functions, 1) == FAIL)
7027 return FAIL;
7028 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7029 CLEAR_POINTER(dfunc);
7030 dfunc->df_idx = def_functions.ga_len;
7031 ufunc->uf_dfunc_idx = dfunc->df_idx;
7032 dfunc->df_ufunc = ufunc;
7033 ++def_functions.ga_len;
7034 return OK;
7035}
7036
7037/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 * After ex_function() has collected all the function lines: parse and compile
7039 * the lines into instructions.
7040 * Adds the function to "def_functions".
7041 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7042 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007043 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007044 * This can be used recursively through compile_lambda(), which may reallocate
7045 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007046 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007048 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007049compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 char_u *line = NULL;
7052 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 cctx_T cctx;
7055 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007056 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 int ret = FAIL;
7058 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007059 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007060 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007061 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007063 // When using a function that was compiled before: Free old instructions.
7064 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007065 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007067 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7068 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007069 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007071 else
7072 {
7073 if (add_def_function(ufunc) == FAIL)
7074 return FAIL;
7075 new_def_function = TRUE;
7076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077
Bram Moolenaar985116a2020-07-12 17:31:09 +02007078 ufunc->uf_def_status = UF_COMPILING;
7079
Bram Moolenaara80faa82020-04-12 19:37:17 +02007080 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 cctx.ctx_ufunc = ufunc;
7082 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007083 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7085 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7086 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7087 cctx.ctx_type_list = &ufunc->uf_type_list;
7088 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7089 instr = &cctx.ctx_instr;
7090
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007091 // Set the context to the function, it may be compiled when called from
7092 // another script. Set the script version to the most modern one.
7093 // The line number will be set in next_line_from_context().
7094 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7096
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007097 // Make sure error messages are OK.
7098 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7099 if (do_estack_push)
7100 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007101 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007102
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007103 if (ufunc->uf_def_args.ga_len > 0)
7104 {
7105 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007106 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007107 int i;
7108 char_u *arg;
7109 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007110 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007111
7112 // Produce instructions for the default values of optional arguments.
7113 // Store the instruction index in uf_def_arg_idx[] so that we know
7114 // where to start when the function is called, depending on the number
7115 // of arguments.
7116 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7117 if (ufunc->uf_def_arg_idx == NULL)
7118 goto erret;
7119 for (i = 0; i < count; ++i)
7120 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007121 garray_T *stack = &cctx.ctx_type_stack;
7122 type_T *val_type;
7123 int arg_idx = first_def_arg + i;
7124
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007125 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7126 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007127 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007128 goto erret;
7129
7130 // If no type specified use the type of the default value.
7131 // Otherwise check that the default value type matches the
7132 // specified type.
7133 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7134 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007135 {
7136 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007137 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007138 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007139 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7140 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007141 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007142
7143 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007144 goto erret;
7145 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007146 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007147
7148 if (did_set_arg_type)
7149 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007150 }
7151
7152 /*
7153 * Loop over all the lines of the function and generate instructions.
7154 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 for (;;)
7156 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007157 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007158 int starts_with_colon = FALSE;
7159 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007160 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007161
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007162 // Bail out on the first error to avoid a flood of errors and report
7163 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007164 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007165 goto erret;
7166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 if (line != NULL && *line == '|')
7168 // the line continues after a '|'
7169 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007170 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007171 && !(*line == '#' && (line == cctx.ctx_line_start
7172 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007174 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 goto erret;
7176 }
7177 else
7178 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007179 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007180 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007181 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 }
7184
Bram Moolenaara80faa82020-04-12 19:37:17 +02007185 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 ea.cmdlinep = &line;
7187 ea.cmd = skipwhite(line);
7188
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007189 // Some things can be recognized by the first character.
7190 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007192 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007193 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007194 if (ea.cmd[1] != '{')
7195 {
7196 line = (char_u *)"";
7197 continue;
7198 }
7199 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007201 case '}':
7202 {
7203 // "}" ends a block scope
7204 scopetype_T stype = cctx.ctx_scope == NULL
7205 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007207 if (stype == BLOCK_SCOPE)
7208 {
7209 compile_endblock(&cctx);
7210 line = ea.cmd;
7211 }
7212 else
7213 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007214 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007215 goto erret;
7216 }
7217 if (line != NULL)
7218 line = skipwhite(ea.cmd + 1);
7219 continue;
7220 }
7221
7222 case '{':
7223 // "{" starts a block scope
7224 // "{'a': 1}->func() is something else
7225 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7226 {
7227 line = compile_block(ea.cmd, &cctx);
7228 continue;
7229 }
7230 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 }
7232
7233 /*
7234 * COMMAND MODIFIERS
7235 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007236 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007237 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7238 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 {
7240 if (errormsg != NULL)
7241 goto erret;
7242 // empty line or comment
7243 line = (char_u *)"";
7244 continue;
7245 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007246 generate_cmdmods(&cctx, &local_cmdmod);
7247 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007249 // Check if there was a colon after the last command modifier or before
7250 // the current position.
7251 for (p = ea.cmd; p >= line; --p)
7252 {
7253 if (*p == ':')
7254 starts_with_colon = TRUE;
7255 if (p < ea.cmd && !VIM_ISWHITE(*p))
7256 break;
7257 }
7258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007260 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007262 {
7263 if (*ea.cmd == '(')
7264 // not for "call()"
7265 ea.cmd = p;
7266 else
7267 ea.cmd = skipwhite(ea.cmd);
7268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007270 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007272 char_u *pskip;
7273
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007274 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007275 // find what follows.
7276 // Skip over "var.member", "var[idx]" and the like.
7277 // Also "&opt = val", "$ENV = val" and "@r = val".
7278 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007279 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007280 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007281 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007283 char_u *var_end;
7284 int oplen;
7285 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286
Bram Moolenaar65821722020-08-02 18:58:54 +02007287 if (ea.cmd[0] == '@')
7288 var_end = ea.cmd + 2;
7289 else
7290 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007291 FNE_CHECK_START | FNE_INCL_BR);
7292 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007293 if (oplen > 0)
7294 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007295 size_t len = p - ea.cmd;
7296
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007297 // Recognize an assignment if we recognize the variable
7298 // name:
7299 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007300 // "local = expr" where "local" is a local var.
7301 // "script = expr" where "script" is a script-local var.
7302 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007303 // "&opt = expr"
7304 // "$ENV = expr"
7305 // "@r = expr"
7306 if (*ea.cmd == '&'
7307 || *ea.cmd == '$'
7308 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007309 || ((len) > 2 && ea.cmd[1] == ':')
7310 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007311 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007312 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007313 || script_var_exists(ea.cmd, len,
7314 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007315 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007316 {
7317 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007318 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007319 goto erret;
7320 continue;
7321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 }
7323 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007324
7325 if (*ea.cmd == '[')
7326 {
7327 // [var, var] = expr
7328 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7329 if (line == NULL)
7330 goto erret;
7331 if (line != ea.cmd)
7332 continue;
7333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 }
7335
7336 /*
7337 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007338 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007340 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007341 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007342 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007343 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007344 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007345 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007346 if (!starts_with_colon)
7347 {
7348 emsg(_(e_colon_required_before_a_range));
7349 goto erret;
7350 }
7351 if (ends_excmd2(line, ea.cmd))
7352 {
7353 // A range without a command: jump to the line.
7354 // TODO: compile to a more efficient command, possibly
7355 // calling parse_cmd_address().
7356 ea.cmdidx = CMD_SIZE;
7357 line = compile_exec(line, &ea, &cctx);
7358 goto nextline;
7359 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007360 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007361 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007362 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007363 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007364 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365
7366 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7367 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007368 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007369 {
7370 line += STRLEN(line);
7371 continue;
7372 }
7373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007375 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007377 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007378 iemsg("Command from find_ex_command() not handled");
7379 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 }
7382
Bram Moolenaar3988f642020-08-27 22:43:03 +02007383 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007384 && ea.cmdidx != CMD_elseif
7385 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007386 && ea.cmdidx != CMD_endif
7387 && ea.cmdidx != CMD_endfor
7388 && ea.cmdidx != CMD_endwhile
7389 && ea.cmdidx != CMD_catch
7390 && ea.cmdidx != CMD_finally
7391 && ea.cmdidx != CMD_endtry)
7392 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007393 emsg(_(e_unreachable_code_after_return));
7394 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007395 }
7396
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007397 p = skipwhite(p);
7398 if (ea.cmdidx != CMD_SIZE
7399 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7400 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007401 if (ea.cmdidx >= 0)
7402 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007403 if ((ea.argt & EX_BANG) && *p == '!')
7404 {
7405 ea.forceit = TRUE;
7406 p = skipwhite(p + 1);
7407 }
7408 }
7409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 switch (ea.cmdidx)
7411 {
7412 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007413 ea.arg = p;
7414 line = compile_nested_function(&ea, &cctx);
7415 break;
7416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007418 // TODO: should we allow this, e.g. to declare a global
7419 // function?
7420 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 goto erret;
7422
7423 case CMD_return:
7424 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 break;
7427
7428 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007429 emsg(_(e_cannot_use_let_in_vim9_script));
7430 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007431 case CMD_var:
7432 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 case CMD_const:
7434 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007435 if (line == p)
7436 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 break;
7438
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007439 case CMD_unlet:
7440 case CMD_unlockvar:
7441 case CMD_lockvar:
7442 line = compile_unletlock(p, &ea, &cctx);
7443 break;
7444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 case CMD_import:
7446 line = compile_import(p, &cctx);
7447 break;
7448
7449 case CMD_if:
7450 line = compile_if(p, &cctx);
7451 break;
7452 case CMD_elseif:
7453 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007454 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 break;
7456 case CMD_else:
7457 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007458 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 break;
7460 case CMD_endif:
7461 line = compile_endif(p, &cctx);
7462 break;
7463
7464 case CMD_while:
7465 line = compile_while(p, &cctx);
7466 break;
7467 case CMD_endwhile:
7468 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007469 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470 break;
7471
7472 case CMD_for:
7473 line = compile_for(p, &cctx);
7474 break;
7475 case CMD_endfor:
7476 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007477 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 break;
7479 case CMD_continue:
7480 line = compile_continue(p, &cctx);
7481 break;
7482 case CMD_break:
7483 line = compile_break(p, &cctx);
7484 break;
7485
7486 case CMD_try:
7487 line = compile_try(p, &cctx);
7488 break;
7489 case CMD_catch:
7490 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007491 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 break;
7493 case CMD_finally:
7494 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007495 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 break;
7497 case CMD_endtry:
7498 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007499 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500 break;
7501 case CMD_throw:
7502 line = compile_throw(p, &cctx);
7503 break;
7504
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007505 case CMD_eval:
7506 if (compile_expr0(&p, &cctx) == FAIL)
7507 goto erret;
7508
Bram Moolenaar3988f642020-08-27 22:43:03 +02007509 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007510 generate_instr_drop(&cctx, ISN_DROP, 1);
7511
7512 line = skipwhite(p);
7513 break;
7514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007517 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007518 case CMD_echomsg:
7519 case CMD_echoerr:
7520 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007521 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007523 case CMD_put:
7524 ea.cmd = cmd;
7525 line = compile_put(p, &ea, &cctx);
7526 break;
7527
Bram Moolenaar3988f642020-08-27 22:43:03 +02007528 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007529
Bram Moolenaarae616492020-07-28 20:07:27 +02007530 case CMD_append:
7531 case CMD_change:
7532 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007533 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007534 case CMD_xit:
7535 not_in_vim9(&ea);
7536 goto erret;
7537
Bram Moolenaar002262f2020-07-08 17:47:57 +02007538 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007539 if (cctx.ctx_skip != SKIP_YES)
7540 {
7541 semsg(_(e_invalid_command_str), ea.cmd);
7542 goto erret;
7543 }
7544 // We don't check for a next command here.
7545 line = (char_u *)"";
7546 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007549 if (cctx.ctx_skip == SKIP_YES)
7550 {
7551 // We don't check for a next command here.
7552 line = (char_u *)"";
7553 }
7554 else
7555 {
7556 // Not recognized, execute with do_cmdline_cmd().
7557 ea.arg = p;
7558 line = compile_exec(line, &ea, &cctx);
7559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560 break;
7561 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007562nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 if (line == NULL)
7564 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007565 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007567 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007568 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 if (cctx.ctx_type_stack.ga_len < 0)
7571 {
7572 iemsg("Type stack underflow");
7573 goto erret;
7574 }
7575 }
7576
7577 if (cctx.ctx_scope != NULL)
7578 {
7579 if (cctx.ctx_scope->se_type == IF_SCOPE)
7580 emsg(_(e_endif));
7581 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7582 emsg(_(e_endwhile));
7583 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7584 emsg(_(e_endfor));
7585 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007586 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587 goto erret;
7588 }
7589
Bram Moolenaarefd88552020-06-18 20:50:10 +02007590 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 {
7592 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7593 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007594 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 goto erret;
7596 }
7597
7598 // Return zero if there is no return at the end.
7599 generate_PUSHNR(&cctx, 0);
7600 generate_instr(&cctx, ISN_RETURN);
7601 }
7602
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007603 {
7604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7605 + ufunc->uf_dfunc_idx;
7606 dfunc->df_deleted = FALSE;
7607 dfunc->df_instr = instr->ga_data;
7608 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007609 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007610 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007611 if (cctx.ctx_outer_used)
7612 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007613 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615
7616 ret = OK;
7617
7618erret:
7619 if (ret == FAIL)
7620 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007621 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007622 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7623 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007624
7625 for (idx = 0; idx < instr->ga_len; ++idx)
7626 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007628
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007629 // If using the last entry in the table and it was added above, we
7630 // might as well remove it.
7631 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007632 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007633 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007634 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007635 ufunc->uf_dfunc_idx = 0;
7636 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007637 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007638
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007639 while (cctx.ctx_scope != NULL)
7640 drop_scope(&cctx);
7641
Bram Moolenaar20431c92020-03-20 18:39:46 +01007642 // Don't execute this function body.
7643 ga_clear_strings(&ufunc->uf_lines);
7644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 if (errormsg != NULL)
7646 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007647 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007648 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 }
7650
7651 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007652 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007653 if (do_estack_push)
7654 estack_pop();
7655
Bram Moolenaar20431c92020-03-20 18:39:46 +01007656 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007657 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007659 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660}
7661
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007662 void
7663set_function_type(ufunc_T *ufunc)
7664{
7665 int varargs = ufunc->uf_va_name != NULL;
7666 int argcount = ufunc->uf_args.ga_len;
7667
7668 // Create a type for the function, with the return type and any
7669 // argument types.
7670 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7671 // The type is included in "tt_args".
7672 if (argcount > 0 || varargs)
7673 {
7674 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7675 argcount, &ufunc->uf_type_list);
7676 // Add argument types to the function type.
7677 if (func_type_add_arg_types(ufunc->uf_func_type,
7678 argcount + varargs,
7679 &ufunc->uf_type_list) == FAIL)
7680 return;
7681 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7682 ufunc->uf_func_type->tt_min_argcount =
7683 argcount - ufunc->uf_def_args.ga_len;
7684 if (ufunc->uf_arg_types == NULL)
7685 {
7686 int i;
7687
7688 // lambda does not have argument types.
7689 for (i = 0; i < argcount; ++i)
7690 ufunc->uf_func_type->tt_args[i] = &t_any;
7691 }
7692 else
7693 mch_memmove(ufunc->uf_func_type->tt_args,
7694 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7695 if (varargs)
7696 {
7697 ufunc->uf_func_type->tt_args[argcount] =
7698 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7699 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7700 }
7701 }
7702 else
7703 // No arguments, can use a predefined type.
7704 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7705 argcount, &ufunc->uf_type_list);
7706}
7707
7708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709/*
7710 * Delete an instruction, free what it contains.
7711 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007712 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713delete_instr(isn_T *isn)
7714{
7715 switch (isn->isn_type)
7716 {
7717 case ISN_EXEC:
7718 case ISN_LOADENV:
7719 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007720 case ISN_LOADB:
7721 case ISN_LOADW:
7722 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007724 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 case ISN_PUSHEXC:
7726 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007727 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007729 case ISN_STOREB:
7730 case ISN_STOREW:
7731 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007732 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 vim_free(isn->isn_arg.string);
7734 break;
7735
7736 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007737 case ISN_STORES:
7738 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 break;
7740
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007741 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007742 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007743 vim_free(isn->isn_arg.unlet.ul_name);
7744 break;
7745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 case ISN_STOREOPT:
7747 vim_free(isn->isn_arg.storeopt.so_name);
7748 break;
7749
7750 case ISN_PUSHBLOB: // push blob isn_arg.blob
7751 blob_unref(isn->isn_arg.blob);
7752 break;
7753
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007754 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007755#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007756 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007757#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007758 break;
7759
7760 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007761#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007762 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007763#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007764 break;
7765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 case ISN_UCALL:
7767 vim_free(isn->isn_arg.ufunc.cuf_name);
7768 break;
7769
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007770 case ISN_FUNCREF:
7771 {
7772 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7773 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007774
7775 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7776 func_ptr_unref(dfunc->df_ufunc);
7777 }
7778 break;
7779
7780 case ISN_DCALL:
7781 {
7782 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7783 + isn->isn_arg.dfunc.cdf_idx;
7784
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007785 if (dfunc->df_ufunc != NULL
7786 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007787 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007788 }
7789 break;
7790
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007791 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007792 {
7793 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7794 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7795
7796 if (ufunc != NULL)
7797 {
7798 // Clear uf_dfunc_idx so that the function is deleted.
7799 clear_def_function(ufunc);
7800 ufunc->uf_dfunc_idx = 0;
7801 func_ptr_unref(ufunc);
7802 }
7803
7804 vim_free(lambda);
7805 vim_free(isn->isn_arg.newfunc.nf_global);
7806 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007807 break;
7808
Bram Moolenaar5e654232020-09-16 15:22:00 +02007809 case ISN_CHECKTYPE:
7810 free_type(isn->isn_arg.type.ct_type);
7811 break;
7812
Bram Moolenaar02194d22020-10-24 23:08:38 +02007813 case ISN_CMDMOD:
7814 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7815 ->cmod_filter_regmatch.regprog);
7816 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7817 break;
7818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 case ISN_2BOOL:
7820 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007821 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 case ISN_ADDBLOB:
7823 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007824 case ISN_ANYINDEX:
7825 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007827 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007829 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007831 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 case ISN_COMPAREANY:
7833 case ISN_COMPAREBLOB:
7834 case ISN_COMPAREBOOL:
7835 case ISN_COMPAREDICT:
7836 case ISN_COMPAREFLOAT:
7837 case ISN_COMPAREFUNC:
7838 case ISN_COMPARELIST:
7839 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 case ISN_COMPARESPECIAL:
7841 case ISN_COMPARESTRING:
7842 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007843 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 case ISN_DROP:
7845 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007846 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007847 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007849 case ISN_EXECCONCAT:
7850 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007852 case ISN_GETITEM:
7853 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007854 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007855 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007856 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007858 case ISN_LOADBDICT:
7859 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007860 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007862 case ISN_LOADSCRIPT:
7863 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007865 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007866 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007867 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 case ISN_NEGATENR:
7869 case ISN_NEWDICT:
7870 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007872 case ISN_OPFLOAT:
7873 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007875 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007876 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 case ISN_PUSHF:
7878 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007880 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007882 case ISN_SHUFFLE:
7883 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007885 case ISN_STOREDICT:
7886 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007887 case ISN_STORENR:
7888 case ISN_STOREOUTER:
7889 case ISN_STOREREG:
7890 case ISN_STORESCRIPT:
7891 case ISN_STOREV:
7892 case ISN_STRINDEX:
7893 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 case ISN_THROW:
7895 case ISN_TRY:
7896 // nothing allocated
7897 break;
7898 }
7899}
7900
7901/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007902 * Free all instructions for "dfunc".
7903 */
7904 static void
7905delete_def_function_contents(dfunc_T *dfunc)
7906{
7907 int idx;
7908
7909 ga_clear(&dfunc->df_def_args_isn);
7910
7911 if (dfunc->df_instr != NULL)
7912 {
7913 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7914 delete_instr(dfunc->df_instr + idx);
7915 VIM_CLEAR(dfunc->df_instr);
7916 }
7917
7918 dfunc->df_deleted = TRUE;
7919}
7920
7921/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007922 * When a user function is deleted, clear the contents of any associated def
7923 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 */
7925 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007926clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007928 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 {
7930 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7931 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932
Bram Moolenaar20431c92020-03-20 18:39:46 +01007933 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007934 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 }
7936}
7937
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007938/*
7939 * Used when a user function is about to be deleted: remove the pointer to it.
7940 * The entry in def_functions is then unused.
7941 */
7942 void
7943unlink_def_function(ufunc_T *ufunc)
7944{
7945 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7946
7947 dfunc->df_ufunc = NULL;
7948}
7949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007951/*
7952 * Free all functions defined with ":def".
7953 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 void
7955free_def_functions(void)
7956{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007957 int idx;
7958
7959 for (idx = 0; idx < def_functions.ga_len; ++idx)
7960 {
7961 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7962
7963 delete_def_function_contents(dfunc);
7964 }
7965
7966 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967}
7968#endif
7969
7970
7971#endif // FEAT_EVAL