blob: 3057fb1a66730cba87a41bb69d84e3a71002b106 [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 Moolenaarb3a01942020-11-17 19:56:09 +01002629 ufunc_T *ufunc = NULL;
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 Moolenaar2bede172020-11-19 18:53:18 +01002774 char_u *
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002775to_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 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01002991 char_u *key = NULL;
2992 char_u *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993
Bram Moolenaar23c55272020-06-21 16:58:13 +02002994 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002995 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002996 *arg = NULL;
2997 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002998 }
2999
3000 if (**arg == '}')
3001 break;
3002
Bram Moolenaar2bede172020-11-19 18:53:18 +01003003 // Eventually {name: value} will use "name" as a literal key and
3004 // {[expr]: value} for an evaluated key.
3005 // Temporarily: if "name" is indeed a valid key, or "[expr]" is
3006 // used, use the new method, like JavaScript. Otherwise fall back
3007 // to the old method.
3008 end = to_name_end(*arg, FALSE);
3009 if (literal || *end == ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003011 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003013 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 return FAIL;
3015 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003016 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 if (generate_PUSHS(cctx, key) == FAIL)
3018 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003019 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 }
3021 else
3022 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003023 isn_T *isn;
3024 int has_bracket = **arg == '[';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025
Bram Moolenaar2bede172020-11-19 18:53:18 +01003026 if (has_bracket)
3027 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003028 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3031 if (isn->isn_type == ISN_PUSHS)
3032 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003033 else
3034 {
3035 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003036 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003037 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003038 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003039 return FAIL;
3040 }
Bram Moolenaar2bede172020-11-19 18:53:18 +01003041 if (has_bracket)
3042 {
3043 *arg = skipwhite(*arg);
3044 if (**arg != ']')
3045 {
3046 emsg(_(e_missing_matching_bracket_after_dict_key));
3047 return FAIL;
3048 }
3049 ++*arg;
3050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 }
3052
3053 // Check for duplicate keys, if using string keys.
3054 if (key != NULL)
3055 {
3056 item = dict_find(d, key, -1);
3057 if (item != NULL)
3058 {
3059 semsg(_(e_duplicate_key), key);
3060 goto failret;
3061 }
3062 item = dictitem_alloc(key);
3063 if (item != NULL)
3064 {
3065 item->di_tv.v_type = VAR_UNKNOWN;
3066 item->di_tv.v_lock = 0;
3067 if (dict_add(d, item) == FAIL)
3068 dictitem_free(item);
3069 }
3070 }
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (**arg != ':')
3073 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003074 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003075 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003076 else
3077 semsg(_(e_missing_dict_colon), *arg);
3078 return FAIL;
3079 }
3080 whitep = *arg + 1;
3081 if (!IS_WHITE_OR_NUL(*whitep))
3082 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003083 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 return FAIL;
3085 }
3086
3087 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003088 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003089 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003090 *arg = NULL;
3091 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003092 }
3093
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003094 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003096 if (!is_const)
3097 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 ++count;
3099
Bram Moolenaar2c330432020-04-13 14:41:35 +02003100 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003101 *arg = skipwhite(*arg);
3102 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003103 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003104 *arg = NULL;
3105 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (**arg == '}')
3108 break;
3109 if (**arg != ',')
3110 {
3111 semsg(_(e_missing_dict_comma), *arg);
3112 goto failret;
3113 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003114 if (IS_WHITE_OR_NUL(*whitep))
3115 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003116 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003117 return FAIL;
3118 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003119 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003120 if (!IS_WHITE_OR_NUL(*whitep))
3121 {
3122 semsg(_(e_white_space_required_after_str), ",");
3123 return FAIL;
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 *arg = skipwhite(*arg + 1);
3126 }
3127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 *arg = *arg + 1;
3129
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003130 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003131 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003132 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003133 *arg += STRLEN(*arg);
3134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003136 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 return generate_NEWDICT(cctx, count);
3138
3139failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003140 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003141 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003142 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003143 *arg = (char_u *)"";
3144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 dict_unref(d);
3146 return FAIL;
3147}
3148
3149/*
3150 * Compile "&option".
3151 */
3152 static int
3153compile_get_option(char_u **arg, cctx_T *cctx)
3154{
3155 typval_T rettv;
3156 char_u *start = *arg;
3157 int ret;
3158
3159 // parse the option and get the current value to get the type.
3160 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003161 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 if (ret == OK)
3163 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003164 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 char_u *name = vim_strnsave(start, *arg - start);
3166 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3167
3168 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3169 vim_free(name);
3170 }
3171 clear_tv(&rettv);
3172
3173 return ret;
3174}
3175
3176/*
3177 * Compile "$VAR".
3178 */
3179 static int
3180compile_get_env(char_u **arg, cctx_T *cctx)
3181{
3182 char_u *start = *arg;
3183 int len;
3184 int ret;
3185 char_u *name;
3186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 ++*arg;
3188 len = get_env_len(arg);
3189 if (len == 0)
3190 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003191 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 return FAIL;
3193 }
3194
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003195 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 name = vim_strnsave(start, len + 1);
3197 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3198 vim_free(name);
3199 return ret;
3200}
3201
3202/*
3203 * Compile "@r".
3204 */
3205 static int
3206compile_get_register(char_u **arg, cctx_T *cctx)
3207{
3208 int ret;
3209
3210 ++*arg;
3211 if (**arg == NUL)
3212 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003213 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return FAIL;
3215 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003216 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 {
3218 emsg_invreg(**arg);
3219 return FAIL;
3220 }
3221 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3222 ++*arg;
3223 return ret;
3224}
3225
3226/*
3227 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003228 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 */
3230 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003231apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003233 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234
3235 // this works from end to start
3236 while (p > start)
3237 {
3238 --p;
3239 if (*p == '-' || *p == '+')
3240 {
3241 // only '-' has an effect, for '+' we only check the type
3242#ifdef FEAT_FLOAT
3243 if (rettv->v_type == VAR_FLOAT)
3244 {
3245 if (*p == '-')
3246 rettv->vval.v_float = -rettv->vval.v_float;
3247 }
3248 else
3249#endif
3250 {
3251 varnumber_T val;
3252 int error = FALSE;
3253
3254 // tv_get_number_chk() accepts a string, but we don't want that
3255 // here
3256 if (check_not_string(rettv) == FAIL)
3257 return FAIL;
3258 val = tv_get_number_chk(rettv, &error);
3259 clear_tv(rettv);
3260 if (error)
3261 return FAIL;
3262 if (*p == '-')
3263 val = -val;
3264 rettv->v_type = VAR_NUMBER;
3265 rettv->vval.v_number = val;
3266 }
3267 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003268 else if (numeric_only)
3269 {
3270 ++p;
3271 break;
3272 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003273 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 {
3275 int v = tv2bool(rettv);
3276
3277 // '!' is permissive in the type.
3278 clear_tv(rettv);
3279 rettv->v_type = VAR_BOOL;
3280 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3281 }
3282 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003283 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 return OK;
3285}
3286
3287/*
3288 * Recognize v: variables that are constants and set "rettv".
3289 */
3290 static void
3291get_vim_constant(char_u **arg, typval_T *rettv)
3292{
3293 if (STRNCMP(*arg, "v:true", 6) == 0)
3294 {
3295 rettv->v_type = VAR_BOOL;
3296 rettv->vval.v_number = VVAL_TRUE;
3297 *arg += 6;
3298 }
3299 else if (STRNCMP(*arg, "v:false", 7) == 0)
3300 {
3301 rettv->v_type = VAR_BOOL;
3302 rettv->vval.v_number = VVAL_FALSE;
3303 *arg += 7;
3304 }
3305 else if (STRNCMP(*arg, "v:null", 6) == 0)
3306 {
3307 rettv->v_type = VAR_SPECIAL;
3308 rettv->vval.v_number = VVAL_NULL;
3309 *arg += 6;
3310 }
3311 else if (STRNCMP(*arg, "v:none", 6) == 0)
3312 {
3313 rettv->v_type = VAR_SPECIAL;
3314 rettv->vval.v_number = VVAL_NONE;
3315 *arg += 6;
3316 }
3317}
3318
Bram Moolenaar696ba232020-07-29 21:20:41 +02003319 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003320get_compare_type(char_u *p, int *len, int *type_is)
3321{
3322 exptype_T type = EXPR_UNKNOWN;
3323 int i;
3324
3325 switch (p[0])
3326 {
3327 case '=': if (p[1] == '=')
3328 type = EXPR_EQUAL;
3329 else if (p[1] == '~')
3330 type = EXPR_MATCH;
3331 break;
3332 case '!': if (p[1] == '=')
3333 type = EXPR_NEQUAL;
3334 else if (p[1] == '~')
3335 type = EXPR_NOMATCH;
3336 break;
3337 case '>': if (p[1] != '=')
3338 {
3339 type = EXPR_GREATER;
3340 *len = 1;
3341 }
3342 else
3343 type = EXPR_GEQUAL;
3344 break;
3345 case '<': if (p[1] != '=')
3346 {
3347 type = EXPR_SMALLER;
3348 *len = 1;
3349 }
3350 else
3351 type = EXPR_SEQUAL;
3352 break;
3353 case 'i': if (p[1] == 's')
3354 {
3355 // "is" and "isnot"; but not a prefix of a name
3356 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3357 *len = 5;
3358 i = p[*len];
3359 if (!isalnum(i) && i != '_')
3360 {
3361 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3362 *type_is = TRUE;
3363 }
3364 }
3365 break;
3366 }
3367 return type;
3368}
3369
3370/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003372 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 */
3374 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003375compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003377 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378
3379 // this works from end to start
3380 while (p > start)
3381 {
3382 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003383 while (VIM_ISWHITE(*p))
3384 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 if (*p == '-' || *p == '+')
3386 {
3387 int negate = *p == '-';
3388 isn_T *isn;
3389
3390 // TODO: check type
3391 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3392 {
3393 --p;
3394 if (*p == '-')
3395 negate = !negate;
3396 }
3397 // only '-' has an effect, for '+' we only check the type
3398 if (negate)
3399 isn = generate_instr(cctx, ISN_NEGATENR);
3400 else
3401 isn = generate_instr(cctx, ISN_CHECKNR);
3402 if (isn == NULL)
3403 return FAIL;
3404 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003405 else if (numeric_only)
3406 {
3407 ++p;
3408 break;
3409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 else
3411 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003412 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003414 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003416 if (p[-1] == '!')
3417 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 }
3420 if (generate_2BOOL(cctx, invert) == FAIL)
3421 return FAIL;
3422 }
3423 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003424 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 return OK;
3426}
3427
3428/*
3429 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003430 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 */
3432 static int
3433compile_subscript(
3434 char_u **arg,
3435 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003436 char_u *start_leader,
3437 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003438 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003440 char_u *name_start = *end_leader;
3441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 for (;;)
3443 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003444 char_u *p = skipwhite(*arg);
3445
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003446 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003447 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003448 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003449
3450 // If a following line starts with "->{" or "->X" advance to that
3451 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003452 // Also if a following line starts with ".x".
3453 if (next != NULL &&
3454 ((next[0] == '-' && next[1] == '>'
3455 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003456 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003457 {
3458 next = next_line_from_context(cctx, TRUE);
3459 if (next == NULL)
3460 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003461 *arg = next;
3462 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003463 }
3464 }
3465
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003466 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3467 // not a function call.
3468 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003470 garray_T *stack = &cctx->ctx_type_stack;
3471 type_T *type;
3472 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003474 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003475 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003476 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003479 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3480
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003481 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3483 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003484 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 return FAIL;
3486 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003487 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003489 char_u *pstart = p;
3490
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003491 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003492 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003493 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 // something->method()
3496 // Apply the '!', '-' and '+' first:
3497 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003498 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003501 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003502 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003503 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (**arg == '{')
3505 {
3506 // lambda call: list->{lambda}
3507 if (compile_lambda_call(arg, cctx) == FAIL)
3508 return FAIL;
3509 }
3510 else
3511 {
3512 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003513 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003514 if (!eval_isnamec1(*p))
3515 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003516 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003517 return FAIL;
3518 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003519 if (ASCII_ISALPHA(*p) && p[1] == ':')
3520 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003521 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 ;
3523 if (*p != '(')
3524 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003525 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 return FAIL;
3527 }
3528 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003529 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 return FAIL;
3531 }
3532 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003533 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003535 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003536 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003537 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003538 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003539 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003540
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003541 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003542 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003543 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003544 // TODO: blob index
3545 // TODO: more arguments
3546 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003547 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003548 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003549 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003550
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003551 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003552 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003553 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003554 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003555 if (**arg == ':')
3556 // missing first index is equal to zero
3557 generate_PUSHNR(cctx, 0);
3558 else
3559 {
3560 if (compile_expr0(arg, cctx) == FAIL)
3561 return FAIL;
3562 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3563 return FAIL;
3564 *arg = skipwhite(*arg);
3565 }
3566 if (**arg == ':')
3567 {
3568 *arg = skipwhite(*arg + 1);
3569 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3570 return FAIL;
3571 if (**arg == ']')
3572 // missing second index is equal to end of string
3573 generate_PUSHNR(cctx, -1);
3574 else
3575 {
3576 if (compile_expr0(arg, cctx) == FAIL)
3577 return FAIL;
3578 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3579 return FAIL;
3580 *arg = skipwhite(*arg);
3581 }
3582 is_slice = TRUE;
3583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
3585 if (**arg != ']')
3586 {
3587 emsg(_(e_missbrac));
3588 return FAIL;
3589 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003590 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003592 // We can index a list and a dict. If we don't know the type
3593 // we can use the index value type.
3594 // TODO: If we don't know use an instruction to figure it out at
3595 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003596 typep = ((type_T **)stack->ga_data) + stack->ga_len
3597 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003598 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003599 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3600 // If the index is a string, the variable must be a Dict.
3601 if (*typep == &t_any && valtype == &t_string)
3602 vtype = VAR_DICT;
3603 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003604 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003605 if (need_type(valtype, &t_number, -1, cctx,
3606 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003607 return FAIL;
3608 if (is_slice)
3609 {
3610 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003611 if (need_type(valtype, &t_number, -2, cctx,
3612 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003613 return FAIL;
3614 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003615 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003616
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003617 if (vtype == VAR_DICT)
3618 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003619 if (is_slice)
3620 {
3621 emsg(_(e_cannot_slice_dictionary));
3622 return FAIL;
3623 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003624 if ((*typep)->tt_type == VAR_DICT)
3625 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003626 else
3627 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003628 if (need_type(*typep, &t_dict_any, -2, cctx,
3629 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003630 return FAIL;
3631 *typep = &t_any;
3632 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003633 if (may_generate_2STRING(-1, cctx) == FAIL)
3634 return FAIL;
3635 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3636 return FAIL;
3637 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003638 else if (vtype == VAR_STRING)
3639 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003640 *typep = &t_string;
3641 if ((is_slice
3642 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3643 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003644 return FAIL;
3645 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003646 else if (vtype == VAR_BLOB)
3647 {
3648 emsg("Sorry, blob index and slice not implemented yet");
3649 return FAIL;
3650 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003651 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003652 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003653 if (is_slice)
3654 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003655 if (generate_instr_drop(cctx,
3656 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3657 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003658 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003659 }
Bram Moolenaared591872020-08-15 22:14:53 +02003660 else
3661 {
3662 if ((*typep)->tt_type == VAR_LIST)
3663 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003664 if (generate_instr_drop(cctx,
3665 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3666 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003667 return FAIL;
3668 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003669 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003670 else
3671 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003672 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003673 return FAIL;
3674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003676 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003678 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003679 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003680 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003681 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003682
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003683 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003684 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003685 {
3686 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003687 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003688 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003689 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003690 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 while (eval_isnamec(*p))
3692 MB_PTR_ADV(p);
3693 if (p == *arg)
3694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003695 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 return FAIL;
3697 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003698 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 return FAIL;
3700 *arg = p;
3701 }
3702 else
3703 break;
3704 }
3705
3706 // TODO - see handle_subscript():
3707 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3708 // Don't do this when "Func" is already a partial that was bound
3709 // explicitly (pt_auto is FALSE).
3710
3711 return OK;
3712}
3713
3714/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3716 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003718 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003719 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003720 *
3721 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 */
3723
3724/*
3725 * number number constant
3726 * 0zFFFFFFFF Blob constant
3727 * "string" string constant
3728 * 'string' literal string constant
3729 * &option-name option value
3730 * @r register contents
3731 * identifier variable value
3732 * function() function call
3733 * $VAR environment variable
3734 * (expression) nested expression
3735 * [expr, expr] List
3736 * {key: val, key: val} Dictionary
3737 * #{key: val, key: val} Dictionary with literal keys
3738 *
3739 * Also handle:
3740 * ! in front logical NOT
3741 * - in front unary minus
3742 * + in front unary plus (ignored)
3743 * trailing (arg) funcref/partial call
3744 * trailing [] subscript in String or List
3745 * trailing .name entry in Dictionary
3746 * trailing ->name() method call
3747 */
3748 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003749compile_expr7(
3750 char_u **arg,
3751 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003752 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 char_u *start_leader, *end_leader;
3755 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003756 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003757 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003759 ppconst->pp_is_const = FALSE;
3760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 /*
3762 * Skip '!', '-' and '+' characters. They are handled later.
3763 */
3764 start_leader = *arg;
3765 while (**arg == '!' || **arg == '-' || **arg == '+')
3766 *arg = skipwhite(*arg + 1);
3767 end_leader = *arg;
3768
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003769 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 switch (**arg)
3771 {
3772 /*
3773 * Number constant.
3774 */
3775 case '0': // also for blob starting with 0z
3776 case '1':
3777 case '2':
3778 case '3':
3779 case '4':
3780 case '5':
3781 case '6':
3782 case '7':
3783 case '8':
3784 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003785 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003787 // Apply "-" and "+" just before the number now, right to
3788 // left. Matters especially when "->" follows. Stops at
3789 // '!'.
3790 if (apply_leader(rettv, TRUE,
3791 start_leader, &end_leader) == FAIL)
3792 {
3793 clear_tv(rettv);
3794 return FAIL;
3795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 break;
3797
3798 /*
3799 * String constant: "string".
3800 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003801 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 return FAIL;
3803 break;
3804
3805 /*
3806 * Literal string constant: 'str''ing'.
3807 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003808 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 return FAIL;
3810 break;
3811
3812 /*
3813 * Constant Vim variable.
3814 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003815 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 ret = NOTDONE;
3817 break;
3818
3819 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003820 * "true" constant
3821 */
3822 case 't': if (STRNCMP(*arg, "true", 4) == 0
3823 && !eval_isnamec((*arg)[4]))
3824 {
3825 *arg += 4;
3826 rettv->v_type = VAR_BOOL;
3827 rettv->vval.v_number = VVAL_TRUE;
3828 }
3829 else
3830 ret = NOTDONE;
3831 break;
3832
3833 /*
3834 * "false" constant
3835 */
3836 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3837 && !eval_isnamec((*arg)[5]))
3838 {
3839 *arg += 5;
3840 rettv->v_type = VAR_BOOL;
3841 rettv->vval.v_number = VVAL_FALSE;
3842 }
3843 else
3844 ret = NOTDONE;
3845 break;
3846
3847 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 * List: [expr, expr]
3849 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003850 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 break;
3852
3853 /*
3854 * Dictionary: #{key: val, key: val}
3855 */
3856 case '#': if ((*arg)[1] == '{')
3857 {
3858 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003859 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 }
3861 else
3862 ret = NOTDONE;
3863 break;
3864
3865 /*
3866 * Lambda: {arg, arg -> expr}
3867 * Dictionary: {'key': val, 'key': val}
3868 */
3869 case '{': {
3870 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003871 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
3873 // Find out what comes after the arguments.
3874 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003875 &ga_arg, TRUE, NULL, NULL,
3876 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 if (ret != FAIL && *start == '>')
3878 ret = compile_lambda(arg, cctx);
3879 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003880 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 }
3882 break;
3883
3884 /*
3885 * Option value: &name
3886 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003887 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3888 return FAIL;
3889 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 break;
3891
3892 /*
3893 * Environment variable: $VAR.
3894 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003895 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3896 return FAIL;
3897 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 break;
3899
3900 /*
3901 * Register contents: @r.
3902 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003903 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3904 return FAIL;
3905 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 break;
3907 /*
3908 * nested expression: (expression).
3909 */
3910 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003911
3912 // recursive!
3913 if (ppconst->pp_used <= PPSIZE - 10)
3914 {
3915 ret = compile_expr1(arg, cctx, ppconst);
3916 }
3917 else
3918 {
3919 // Not enough space in ppconst, flush constants.
3920 if (generate_ppconst(cctx, ppconst) == FAIL)
3921 return FAIL;
3922 ret = compile_expr0(arg, cctx);
3923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 *arg = skipwhite(*arg);
3925 if (**arg == ')')
3926 ++*arg;
3927 else if (ret == OK)
3928 {
3929 emsg(_(e_missing_close));
3930 ret = FAIL;
3931 }
3932 break;
3933
3934 default: ret = NOTDONE;
3935 break;
3936 }
3937 if (ret == FAIL)
3938 return FAIL;
3939
Bram Moolenaar1c747212020-05-09 18:28:34 +02003940 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003942 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003943 clear_tv(rettv);
3944 else
3945 // A constant expression can possibly be handled compile time,
3946 // return the value instead of generating code.
3947 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949 else if (ret == NOTDONE)
3950 {
3951 char_u *p;
3952 int r;
3953
3954 if (!eval_isnamec1(**arg))
3955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003956 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 return FAIL;
3958 }
3959
3960 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003961 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003963 {
3964 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003967 {
3968 if (generate_ppconst(cctx, ppconst) == FAIL)
3969 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003970 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 if (r == FAIL)
3973 return FAIL;
3974 }
3975
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003976 // Handle following "[]", ".member", etc.
3977 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003978 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979 ppconst) == FAIL)
3980 return FAIL;
3981 if (ppconst->pp_used > 0)
3982 {
3983 // apply the '!', '-' and '+' before the constant
3984 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003985 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003986 return FAIL;
3987 return OK;
3988 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003989 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003991 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992}
3993
3994/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003995 * Give the "white on both sides" error, taking the operator from "p[len]".
3996 */
3997 void
3998error_white_both(char_u *op, int len)
3999{
4000 char_u buf[10];
4001
4002 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004003 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004004}
4005
4006/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004007 * <type>expr7: runtime type check / conversion
4008 */
4009 static int
4010compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4011{
4012 type_T *want_type = NULL;
4013
4014 // Recognize <type>
4015 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4016 {
4017 int called_emsg_before = called_emsg;
4018
4019 ++*arg;
4020 want_type = parse_type(arg, cctx->ctx_type_list);
4021 if (called_emsg != called_emsg_before)
4022 return FAIL;
4023
4024 if (**arg != '>')
4025 {
4026 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004027 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004028 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004029 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004030 return FAIL;
4031 }
4032 ++*arg;
4033 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4034 return FAIL;
4035 }
4036
4037 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4038 return FAIL;
4039
4040 if (want_type != NULL)
4041 {
4042 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004043 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004044
Bram Moolenaard1103582020-08-14 22:44:25 +02004045 generate_ppconst(cctx, ppconst);
4046 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004047 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004048 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004049 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004050 return FAIL;
4051 }
4052 }
4053
4054 return OK;
4055}
4056
4057/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 * * number multiplication
4059 * / number division
4060 * % number modulo
4061 */
4062 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
4065 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004066 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004069 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004070 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 return FAIL;
4072
4073 /*
4074 * Repeat computing, until no "*", "/" or "%" is following.
4075 */
4076 for (;;)
4077 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004078 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 if (*op != '*' && *op != '/' && *op != '%')
4080 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004081 if (next != NULL)
4082 {
4083 *arg = next_line_from_context(cctx, TRUE);
4084 op = skipwhite(*arg);
4085 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004087 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004089 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004090 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 }
4092 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004093 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004094 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004096 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004097 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004099
4100 if (ppconst->pp_used == ppconst_used + 2
4101 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4102 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004103 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4105 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004108 // both are numbers: compute the result
4109 switch (*op)
4110 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004111 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004113 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004114 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004115 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 break;
4117 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004118 tv1->vval.v_number = res;
4119 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004120 }
4121 else
4122 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004123 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004124 generate_two_op(cctx, op);
4125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
4127
4128 return OK;
4129}
4130
4131/*
4132 * + number addition
4133 * - number subtraction
4134 * .. string concatenation
4135 */
4136 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138{
4139 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004140 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143
4144 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 return FAIL;
4147
4148 /*
4149 * Repeat computing, until no "+", "-" or ".." is following.
4150 */
4151 for (;;)
4152 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004153 op = may_peek_next_line(cctx, *arg, &next);
4154 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 break;
4156 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004157 if (next != NULL)
4158 {
4159 *arg = next_line_from_context(cctx, TRUE);
4160 op = skipwhite(*arg);
4161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004163 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004165 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 }
4168
4169 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004170 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004171 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
4176
Bram Moolenaara5565e42020-05-09 15:44:01 +02004177 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004178 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004179 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4180 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4181 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4182 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4185 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004186
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187 // concat/subtract/add constant numbers
4188 if (*op == '+')
4189 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4190 else if (*op == '-')
4191 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4192 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 // concatenate constant strings
4195 char_u *s1 = tv1->vval.v_string;
4196 char_u *s2 = tv2->vval.v_string;
4197 size_t len1 = STRLEN(s1);
4198
4199 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4200 if (tv1->vval.v_string == NULL)
4201 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004203 return FAIL;
4204 }
4205 mch_memmove(tv1->vval.v_string, s1, len1);
4206 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207 vim_free(s1);
4208 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 }
4212 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004214 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 if (*op == '.')
4216 {
4217 if (may_generate_2STRING(-2, cctx) == FAIL
4218 || may_generate_2STRING(-1, cctx) == FAIL)
4219 return FAIL;
4220 generate_instr_drop(cctx, ISN_CONCAT, 1);
4221 }
4222 else
4223 generate_two_op(cctx, op);
4224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 }
4226
4227 return OK;
4228}
4229
4230/*
4231 * expr5a == expr5b
4232 * expr5a =~ expr5b
4233 * expr5a != expr5b
4234 * expr5a !~ expr5b
4235 * expr5a > expr5b
4236 * expr5a >= expr5b
4237 * expr5a < expr5b
4238 * expr5a <= expr5b
4239 * expr5a is expr5b
4240 * expr5a isnot expr5b
4241 *
4242 * Produces instructions:
4243 * EVAL expr5a Push result of "expr5a"
4244 * EVAL expr5b Push result of "expr5b"
4245 * COMPARE one of the compare instructions
4246 */
4247 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249{
4250 exptype_T type = EXPR_UNKNOWN;
4251 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004252 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
4257 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 return FAIL;
4260
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004261 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004262 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263
4264 /*
4265 * If there is a comparative operator, use it.
4266 */
4267 if (type != EXPR_UNKNOWN)
4268 {
4269 int ic = FALSE; // Default: do not ignore case
4270
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004271 if (next != NULL)
4272 {
4273 *arg = next_line_from_context(cctx, TRUE);
4274 p = skipwhite(*arg);
4275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 if (type_is && (p[len] == '?' || p[len] == '#'))
4277 {
4278 semsg(_(e_invexpr2), *arg);
4279 return FAIL;
4280 }
4281 // extra question mark appended: ignore case
4282 if (p[len] == '?')
4283 {
4284 ic = TRUE;
4285 ++len;
4286 }
4287 // extra '#' appended: match case (ignored)
4288 else if (p[len] == '#')
4289 ++len;
4290 // nothing appended: match case
4291
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004292 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004294 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004295 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
4297
4298 // get the second variable
4299 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004300 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004301 return FAIL;
4302
Bram Moolenaara5565e42020-05-09 15:44:01 +02004303 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 return FAIL;
4305
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306 if (ppconst->pp_used == ppconst_used + 2)
4307 {
4308 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4309 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4310 int ret;
4311
4312 // Both sides are a constant, compute the result now.
4313 // First check for a valid combination of types, this is more
4314 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004315 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 ret = FAIL;
4317 else
4318 {
4319 ret = typval_compare(tv1, tv2, type, ic);
4320 tv1->v_type = VAR_BOOL;
4321 tv1->vval.v_number = tv1->vval.v_number
4322 ? VVAL_TRUE : VVAL_FALSE;
4323 clear_tv(tv2);
4324 --ppconst->pp_used;
4325 }
4326 return ret;
4327 }
4328
4329 generate_ppconst(cctx, ppconst);
4330 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
4332
4333 return OK;
4334}
4335
Bram Moolenaar7f141552020-05-09 17:35:53 +02004336static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338/*
4339 * Compile || or &&.
4340 */
4341 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342compile_and_or(
4343 char_u **arg,
4344 cctx_T *cctx,
4345 char *op,
4346 ppconst_T *ppconst,
4347 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004349 char_u *next;
4350 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 int opchar = *op;
4352
4353 if (p[0] == opchar && p[1] == opchar)
4354 {
4355 garray_T *instr = &cctx->ctx_instr;
4356 garray_T end_ga;
4357
4358 /*
4359 * Repeat until there is no following "||" or "&&"
4360 */
4361 ga_init2(&end_ga, sizeof(int), 10);
4362 while (p[0] == opchar && p[1] == opchar)
4363 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004364 if (next != NULL)
4365 {
4366 *arg = next_line_from_context(cctx, TRUE);
4367 p = skipwhite(*arg);
4368 }
4369
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004370 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4371 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004372 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004373 return FAIL;
4374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004376 // TODO: use ppconst if the value is a constant and check
4377 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 generate_ppconst(cctx, ppconst);
4379
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004380 // Every part must evaluate to a bool.
4381 if (bool_on_stack(cctx) == FAIL)
4382 {
4383 ga_clear(&end_ga);
4384 return FAIL;
4385 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 if (ga_grow(&end_ga, 1) == FAIL)
4388 {
4389 ga_clear(&end_ga);
4390 return FAIL;
4391 }
4392 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4393 ++end_ga.ga_len;
4394 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004395 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
4397 // eval the next expression
4398 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004399 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004400 return FAIL;
4401
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4403 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 {
4405 ga_clear(&end_ga);
4406 return FAIL;
4407 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004408
4409 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004413 // Every part must evaluate to a bool.
4414 if (bool_on_stack(cctx) == FAIL)
4415 {
4416 ga_clear(&end_ga);
4417 return FAIL;
4418 }
4419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 // Fill in the end label in all jumps.
4421 while (end_ga.ga_len > 0)
4422 {
4423 isn_T *isn;
4424
4425 --end_ga.ga_len;
4426 isn = ((isn_T *)instr->ga_data)
4427 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4428 isn->isn_arg.jump.jump_where = instr->ga_len;
4429 }
4430 ga_clear(&end_ga);
4431 }
4432
4433 return OK;
4434}
4435
4436/*
4437 * expr4a && expr4a && expr4a logical AND
4438 *
4439 * Produces instructions:
4440 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004441 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004442 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004444 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 * EVAL expr4c Push result of "expr4c"
4446 * end:
4447 */
4448 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 int ppconst_used = ppconst->pp_used;
4452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 return FAIL;
4456
4457 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459}
4460
4461/*
4462 * expr3a || expr3b || expr3c logical OR
4463 *
4464 * Produces instructions:
4465 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004466 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004467 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004469 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 * EVAL expr3c Push result of "expr3c"
4471 * end:
4472 */
4473 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 int ppconst_used = ppconst->pp_used;
4477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004479 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 return FAIL;
4481
4482 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484}
4485
4486/*
4487 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004489 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 * JUMP_IF_FALSE alt jump if false
4491 * EVAL expr1a
4492 * JUMP_ALWAYS end
4493 * alt: EVAL expr1b
4494 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004495 *
4496 * Toplevel expression: expr2 ?? expr1
4497 * Produces instructions:
4498 * EVAL expr2 Push result of "expr2"
4499 * JUMP_AND_KEEP_IF_TRUE end jump if true
4500 * EVAL expr1
4501 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 */
4503 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505{
4506 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004508 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
Bram Moolenaar3988f642020-08-27 22:43:03 +02004510 // Ignore all kinds of errors when not producing code.
4511 if (cctx->ctx_skip == SKIP_YES)
4512 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004513 evalarg_T evalarg;
4514
4515 CLEAR_FIELD(evalarg);
4516 evalarg.eval_cctx = cctx;
4517 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004518 return OK;
4519 }
4520
Bram Moolenaar61a89812020-05-07 16:58:17 +02004521 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 return FAIL;
4524
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004525 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 if (*p == '?')
4527 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004528 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 garray_T *instr = &cctx->ctx_instr;
4530 garray_T *stack = &cctx->ctx_type_stack;
4531 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004532 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004534 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 int has_const_expr = FALSE;
4536 int const_value = FALSE;
4537 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004539 if (next != NULL)
4540 {
4541 *arg = next_line_from_context(cctx, TRUE);
4542 p = skipwhite(*arg);
4543 }
4544
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004545 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004546 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004547 semsg(_(e_white_space_required_before_and_after_str),
4548 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004549 return FAIL;
4550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551
Bram Moolenaara5565e42020-05-09 15:44:01 +02004552 if (ppconst->pp_used == ppconst_used + 1)
4553 {
4554 // the condition is a constant, we know whether the ? or the :
4555 // expression is to be evaluated.
4556 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004557 if (op_falsy)
4558 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4559 else
4560 {
4561 int error = FALSE;
4562
4563 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4564 &error);
4565 if (error)
4566 return FAIL;
4567 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004568 cctx->ctx_skip = save_skip == SKIP_YES ||
4569 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4570
4571 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4572 // "left ?? right" and "left" is truthy: produce "left"
4573 generate_ppconst(cctx, ppconst);
4574 else
4575 {
4576 clear_tv(&ppconst->pp_tv[ppconst_used]);
4577 --ppconst->pp_used;
4578 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579 }
4580 else
4581 {
4582 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004583 if (op_falsy)
4584 end_idx = instr->ga_len;
4585 generate_JUMP(cctx, op_falsy
4586 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4587 if (op_falsy)
4588 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
4591 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004592 *arg = skipwhite(p + 1 + op_falsy);
4593 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004594 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004596 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 if (!has_const_expr)
4599 {
4600 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004602 if (!op_falsy)
4603 {
4604 // remember the type and drop it
4605 --stack->ga_len;
4606 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004608 end_idx = instr->ga_len;
4609 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004610
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004611 // jump here from JUMP_IF_FALSE
4612 isn = ((isn_T *)instr->ga_data) + alt_idx;
4613 isn->isn_arg.jump.jump_where = instr->ga_len;
4614 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004617 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004619 // Check for the ":".
4620 p = may_peek_next_line(cctx, *arg, &next);
4621 if (*p != ':')
4622 {
4623 emsg(_(e_missing_colon));
4624 return FAIL;
4625 }
4626 if (next != NULL)
4627 {
4628 *arg = next_line_from_context(cctx, TRUE);
4629 p = skipwhite(*arg);
4630 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004631
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004632 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4633 {
4634 semsg(_(e_white_space_required_before_and_after_str), ":");
4635 return FAIL;
4636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004638 // evaluate the third expression
4639 if (has_const_expr)
4640 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004641 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004642 *arg = skipwhite(p + 1);
4643 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4644 return FAIL;
4645 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4646 return FAIL;
4647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649 if (!has_const_expr)
4650 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004651 type_T **typep;
4652
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004656 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4657 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004659 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 isn = ((isn_T *)instr->ga_data) + end_idx;
4661 isn->isn_arg.jump.jump_where = instr->ga_len;
4662 }
4663
4664 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 }
4666 return OK;
4667}
4668
4669/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004671 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4672 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 */
4674 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004675compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676{
4677 ppconst_T ppconst;
4678
4679 CLEAR_FIELD(ppconst);
4680 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4681 {
4682 clear_ppconst(&ppconst);
4683 return FAIL;
4684 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004685 if (is_const != NULL)
4686 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 if (generate_ppconst(cctx, &ppconst) == FAIL)
4688 return FAIL;
4689 return OK;
4690}
4691
4692/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004693 * Toplevel expression.
4694 */
4695 static int
4696compile_expr0(char_u **arg, cctx_T *cctx)
4697{
4698 return compile_expr0_ext(arg, cctx, NULL);
4699}
4700
4701/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 * compile "return [expr]"
4703 */
4704 static char_u *
4705compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4706{
4707 char_u *p = arg;
4708 garray_T *stack = &cctx->ctx_type_stack;
4709 type_T *stack_type;
4710
4711 if (*p != NUL && *p != '|' && *p != '\n')
4712 {
4713 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 return NULL;
4716
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004717 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004718 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004719 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4720 if (set_return_type)
4721 cctx->ctx_ufunc->uf_ret_type = stack_type;
4722 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004723 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004724 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4725 && stack_type->tt_type != VAR_VOID
4726 && stack_type->tt_type != VAR_UNKNOWN)
4727 {
4728 emsg(_(e_returning_value_in_function_without_return_type));
4729 return NULL;
4730 }
4731 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004732 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004733 return NULL;
4734 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 }
4737 else
4738 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004739 // "set_return_type" cannot be TRUE, only used for a lambda which
4740 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004741 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4742 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004744 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 return NULL;
4746 }
4747
4748 // No argument, return zero.
4749 generate_PUSHNR(cctx, 0);
4750 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004751 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 return NULL;
4753
4754 // "return val | endif" is possible
4755 return skipwhite(p);
4756}
4757
4758/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004759 * Get a line from the compilation context, compatible with exarg_T getline().
4760 * Return a pointer to the line in allocated memory.
4761 * Return NULL for end-of-file or some error.
4762 */
4763 static char_u *
4764exarg_getline(
4765 int c UNUSED,
4766 void *cookie,
4767 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004768 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004769{
4770 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004771 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004772
Bram Moolenaar66250c92020-08-20 15:02:42 +02004773 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004774 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004775 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004776 return NULL;
4777 ++cctx->ctx_lnum;
4778 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4779 // Comment lines result in NULL pointers, skip them.
4780 if (p != NULL)
4781 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004782 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004783}
4784
4785/*
4786 * Compile a nested :def command.
4787 */
4788 static char_u *
4789compile_nested_function(exarg_T *eap, cctx_T *cctx)
4790{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004791 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004793 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004794 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004796 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004797
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004798 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004799 {
4800 emsg(_(e_cannot_use_bang_with_nested_def));
4801 return NULL;
4802 }
4803
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004804 // Only g:Func() can use a namespace.
4805 if (name_start[1] == ':' && !is_global)
4806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004807 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004808 return NULL;
4809 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004810 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4811 return NULL;
4812
Bram Moolenaar04b12692020-05-04 23:24:44 +02004813 eap->arg = name_end;
4814 eap->getline = exarg_getline;
4815 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004816 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004817 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004818 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004819 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004820
Bram Moolenaar822ba242020-05-24 23:00:18 +02004821 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004822 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004823 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004824 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004825 {
4826 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004827 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004828 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004829
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004830 if (is_global)
4831 {
4832 char_u *func_name = vim_strnsave(name_start + 2,
4833 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004834
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004835 if (func_name == NULL)
4836 r = FAIL;
4837 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004838 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004839 }
4840 else
4841 {
4842 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004843 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004844 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004845 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004846
Bram Moolenaareef21022020-08-01 22:16:43 +02004847 if (lvar == NULL)
4848 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004849 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004850 return NULL;
4851 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004852
4853 // copy over the block scope IDs
4854 if (block_depth > 0)
4855 {
4856 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4857 if (ufunc->uf_block_ids != NULL)
4858 {
4859 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4860 sizeof(int) * block_depth);
4861 ufunc->uf_block_depth = block_depth;
4862 }
4863 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004864 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004865
Bram Moolenaar61a89812020-05-07 16:58:17 +02004866 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004867 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004868}
4869
4870/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 * Return the length of an assignment operator, or zero if there isn't one.
4872 */
4873 int
4874assignment_len(char_u *p, int *heredoc)
4875{
4876 if (*p == '=')
4877 {
4878 if (p[1] == '<' && p[2] == '<')
4879 {
4880 *heredoc = TRUE;
4881 return 3;
4882 }
4883 return 1;
4884 }
4885 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4886 return 2;
4887 if (STRNCMP(p, "..=", 3) == 0)
4888 return 3;
4889 return 0;
4890}
4891
4892// words that cannot be used as a variable
4893static char *reserved[] = {
4894 "true",
4895 "false",
4896 NULL
4897};
4898
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004899typedef enum {
4900 dest_local,
4901 dest_option,
4902 dest_env,
4903 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004904 dest_buffer,
4905 dest_window,
4906 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004907 dest_vimvar,
4908 dest_script,
4909 dest_reg,
4910} assign_dest_T;
4911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004913 * Generate the load instruction for "name".
4914 */
4915 static void
4916generate_loadvar(
4917 cctx_T *cctx,
4918 assign_dest_T dest,
4919 char_u *name,
4920 lvar_T *lvar,
4921 type_T *type)
4922{
4923 switch (dest)
4924 {
4925 case dest_option:
4926 // TODO: check the option exists
4927 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4928 break;
4929 case dest_global:
4930 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4931 break;
4932 case dest_buffer:
4933 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4934 break;
4935 case dest_window:
4936 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4937 break;
4938 case dest_tab:
4939 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4940 break;
4941 case dest_script:
4942 compile_load_scriptvar(cctx,
4943 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4944 break;
4945 case dest_env:
4946 // Include $ in the name here
4947 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4948 break;
4949 case dest_reg:
4950 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4951 break;
4952 case dest_vimvar:
4953 generate_LOADV(cctx, name + 2, TRUE);
4954 break;
4955 case dest_local:
4956 if (lvar->lv_from_outer)
4957 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4958 NULL, type);
4959 else
4960 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4961 break;
4962 }
4963}
4964
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004965 void
4966vim9_declare_error(char_u *name)
4967{
4968 char *scope = "";
4969
4970 switch (*name)
4971 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004972 case 'g': scope = _("global"); break;
4973 case 'b': scope = _("buffer"); break;
4974 case 'w': scope = _("window"); break;
4975 case 't': scope = _("tab"); break;
4976 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004977 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004978 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004979 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004980 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004981 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004982 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004983 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004984 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004985 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004986}
4987
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004988/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004990 * "let name"
4991 * "var name = expr"
4992 * "final name = expr"
4993 * "const name = expr"
4994 * "name = expr"
4995 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 * Return NULL for an error.
4997 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 */
4999 static char_u *
5000compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5001{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005004 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 char_u *ret = NULL;
5006 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005008 int scriptvar_sid = 0;
5009 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005012 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 int oplen = 0;
5015 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005016 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005020 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5021 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 // Skip over the "var" or "[var, var]" to get to any "=".
5024 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5025 if (p == NULL)
5026 return *arg == '[' ? arg : NULL;
5027
5028 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005030 // TODO: should we allow this, and figure out type inference from list
5031 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005032 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 return NULL;
5034 }
5035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 sp = p;
5037 p = skipwhite(p);
5038 op = p;
5039 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040
5041 if (var_count > 0 && oplen == 0)
5042 // can be something like "[1, 2]->func()"
5043 return arg;
5044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5046 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005047 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005049 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 if (heredoc)
5052 {
5053 list_T *l;
5054 listitem_T *li;
5055
5056 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005057 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005059 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005060 if (l == NULL)
5061 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062
Bram Moolenaar078269b2020-09-21 20:35:55 +02005063 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005065 // Push each line and the create the list.
5066 FOR_ALL_LIST_ITEMS(l, li)
5067 {
5068 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5069 li->li_tv.vval.v_string = NULL;
5070 }
5071 generate_NEWLIST(cctx, l->lv_len);
5072 type = &t_list_string;
5073 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 list_free(l);
5076 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 // for "[var, var] = expr" evaluate the expression here, loop over the
5082 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005083
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 p = skipwhite(op + oplen);
5085 if (compile_expr0(&p, cctx) == FAIL)
5086 return NULL;
5087 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005089 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005091 type_T *stacktype;
5092
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005093 stacktype = stack->ga_len == 0 ? &t_void
5094 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005095 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005097 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 goto theend;
5099 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005100 if (need_type(stacktype, &t_list_any, -1, cctx,
5101 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005103 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005104 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5105 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 }
5107 }
5108
5109 /*
5110 * Loop over variables in "[var, var] = expr".
5111 * For "var = expr" and "let var: type" this is done only once.
5112 */
5113 if (var_count > 0)
5114 var_start = skipwhite(arg + 1); // skip over the "["
5115 else
5116 var_start = arg;
5117 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5118 {
5119 char_u *var_end = skip_var_one(var_start, FALSE);
5120 size_t varlen;
5121 int new_local = FALSE;
5122 int opt_type;
5123 int opt_flags = 0;
5124 assign_dest_T dest = dest_local;
5125 int vimvaridx = -1;
5126 lvar_T *lvar = NULL;
5127 lvar_T arg_lvar;
5128 int has_type = FALSE;
5129 int has_index = FALSE;
5130 int instr_count = -1;
5131
Bram Moolenaar65821722020-08-02 18:58:54 +02005132 if (*var_start == '@')
5133 p = var_start + 2;
5134 else
5135 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005136 // skip over the leading "&", "&l:", "&g:" and "$"
5137 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005138 p = to_name_end(p, TRUE);
5139 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140
5141 // "a: type" is declaring variable "a" with a type, not "a:".
5142 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5143 --var_end;
5144 if (is_decl && p == var_start + 2 && p[-1] == ':')
5145 --p;
5146
5147 varlen = p - var_start;
5148 vim_free(name);
5149 name = vim_strnsave(var_start, varlen);
5150 if (name == NULL)
5151 return NULL;
5152 if (!heredoc)
5153 type = &t_any;
5154
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005155 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005157 int declare_error = FALSE;
5158
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 if (*var_start == '&')
5160 {
5161 int cc;
5162 long numval;
5163
5164 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005165 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 emsg(_(e_const_option));
5168 goto theend;
5169 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005170 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 p = var_start;
5172 p = find_option_end(&p, &opt_flags);
5173 if (p == NULL)
5174 {
5175 // cannot happen?
5176 emsg(_(e_letunexp));
5177 goto theend;
5178 }
5179 cc = *p;
5180 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005181 opt_type = get_option_value(skip_option_env_lead(var_start),
5182 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 *p = cc;
5184 if (opt_type == -3)
5185 {
5186 semsg(_(e_unknown_option), var_start);
5187 goto theend;
5188 }
5189 if (opt_type == -2 || opt_type == 0)
5190 type = &t_string;
5191 else
5192 type = &t_number; // both number and boolean option
5193 }
5194 else if (*var_start == '$')
5195 {
5196 dest = dest_env;
5197 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005198 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 }
5200 else if (*var_start == '@')
5201 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005202 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 {
5204 emsg_invreg(var_start[1]);
5205 goto theend;
5206 }
5207 dest = dest_reg;
5208 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005209 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005211 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 {
5213 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005214 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005215 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005216 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 {
5218 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005219 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005221 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 {
5223 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005224 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005226 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 {
5228 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005229 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005231 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 {
5233 typval_T *vtv;
5234 int di_flags;
5235
5236 vimvaridx = find_vim_var(name + 2, &di_flags);
5237 if (vimvaridx < 0)
5238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005239 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 goto theend;
5241 }
5242 // We use the current value of "sandbox" here, is that OK?
5243 if (var_check_ro(di_flags, name, FALSE))
5244 goto theend;
5245 dest = dest_vimvar;
5246 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005247 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005248 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 }
5250 else
5251 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005252 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253
5254 for (idx = 0; reserved[idx] != NULL; ++idx)
5255 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005257 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005258 goto theend;
5259 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260
5261 lvar = lookup_local(var_start, varlen, cctx);
5262 if (lvar == NULL)
5263 {
5264 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005265 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5267 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005268 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 if (is_decl)
5270 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005271 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 goto theend;
5273 }
5274 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005275 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005276 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 if (lvar != NULL)
5278 {
5279 if (is_decl)
5280 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005281 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282 goto theend;
5283 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005285 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005286 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005287 int script_namespace = varlen > 1
5288 && STRNCMP(var_start, "s:", 2) == 0;
5289 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005290 ? script_var_exists(var_start + 2, varlen - 2,
5291 FALSE, cctx)
5292 : script_var_exists(var_start, varlen,
5293 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005294 imported_T *import =
5295 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005296
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005297 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005299 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5300
5301 if (is_decl)
5302 {
5303 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005304 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005305 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005306 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005307 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005308 name);
5309 goto theend;
5310 }
5311 else if (cctx->ctx_ufunc->uf_script_ctx_version
5312 == SCRIPT_VERSION_VIM9
5313 && script_namespace
5314 && !script_var && import == NULL)
5315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005316 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005317 goto theend;
5318 }
5319
5320 dest = dest_script;
5321
5322 // existing script-local variables should have a type
5323 scriptvar_sid = current_sctx.sc_sid;
5324 if (import != NULL)
5325 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005326 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005327 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005328 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005329 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005330 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005331 {
5332 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5333 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005334 ((svar_T *)si->sn_var_vals.ga_data)
5335 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005336 type = sv->sv_type;
5337 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005338 }
5339 }
5340 else if (name[1] == ':' && name[2] != NUL)
5341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005342 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 goto theend;
5344 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005345 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005346 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005347 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005348 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005349 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005350 else if (check_defined(var_start, varlen, cctx) == FAIL)
5351 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005354
5355 if (declare_error)
5356 {
5357 vim9_declare_error(name);
5358 goto theend;
5359 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005360 }
5361
5362 // handle "a:name" as a name, not index "name" on "a"
5363 if (varlen > 1 || var_start[varlen] != ':')
5364 p = var_end;
5365
5366 if (dest != dest_option)
5367 {
5368 if (is_decl && *p == ':')
5369 {
5370 // parse optional type: "let var: type = expr"
5371 if (!VIM_ISWHITE(p[1]))
5372 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005373 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 goto theend;
5375 }
5376 p = skipwhite(p + 1);
5377 type = parse_type(&p, cctx->ctx_type_list);
5378 has_type = TRUE;
5379 }
5380 else if (lvar != NULL)
5381 type = lvar->lv_type;
5382 }
5383
5384 if (oplen == 3 && !heredoc && dest != dest_global
5385 && type->tt_type != VAR_STRING
5386 && type->tt_type != VAR_ANY)
5387 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005388 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389 goto theend;
5390 }
5391
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005392 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 {
5394 if (oplen > 1 && !heredoc)
5395 {
5396 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005397 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 goto theend;
5399 }
5400
5401 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005402 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5403 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 goto theend;
5405 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005406 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 if (lvar == NULL)
5408 goto theend;
5409 new_local = TRUE;
5410 }
5411
5412 member_type = type;
5413 if (var_end > var_start + varlen)
5414 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005415 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 if (is_decl)
5417 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005418 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 goto theend;
5420 }
5421
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005422 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 {
5424 has_index = TRUE;
5425 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005426 member_type = &t_any;
5427 else
5428 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 }
5430 else
5431 {
5432 semsg("Not supported yet: %s", var_start);
5433 goto theend;
5434 }
5435 }
5436 else if (lvar == &arg_lvar)
5437 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005438 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005439 goto theend;
5440 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005441 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5442 {
5443 semsg(_(e_cannot_assign_to_constant), name);
5444 goto theend;
5445 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005446
5447 if (!heredoc)
5448 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005449 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005450 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005451 if (oplen > 0 && var_count == 0)
5452 {
5453 // skip over the "=" and the expression
5454 p = skipwhite(op + oplen);
5455 compile_expr0(&p, cctx);
5456 }
5457 }
5458 else if (oplen > 0)
5459 {
5460 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005461 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005462
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 // For "var = expr" evaluate the expression.
5464 if (var_count == 0)
5465 {
5466 int r;
5467
5468 // for "+=", "*=", "..=" etc. first load the current value
5469 if (*op != '=')
5470 {
5471 generate_loadvar(cctx, dest, name, lvar, type);
5472
5473 if (has_index)
5474 {
5475 // TODO: get member from list or dict
5476 emsg("Index with operation not supported yet");
5477 goto theend;
5478 }
5479 }
5480
5481 // Compile the expression. Temporarily hide the new local
5482 // variable here, it is not available to this expression.
5483 if (new_local)
5484 --cctx->ctx_locals.ga_len;
5485 instr_count = instr->ga_len;
5486 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005487 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488 if (new_local)
5489 ++cctx->ctx_locals.ga_len;
5490 if (r == FAIL)
5491 goto theend;
5492 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005493 else if (semicolon && var_idx == var_count - 1)
5494 {
5495 // For "[var; var] = expr" get the rest of the list
5496 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5497 goto theend;
5498 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005499 else
5500 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 // For "[var, var] = expr" get the "var_idx" item from the
5502 // list.
5503 if (generate_GETITEM(cctx, var_idx) == FAIL)
5504 return FAIL;
5505 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005506
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005507 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005508 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005511 if ((stacktype->tt_type == VAR_FUNC
5512 || stacktype->tt_type == VAR_PARTIAL)
5513 && var_wrong_func_name(name, TRUE))
5514 goto theend;
5515
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005516 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005517 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005518 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005519 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005520 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005521 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522 }
5523 else
5524 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005525 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005526 // for a variable that implies &t_any.
5527 if (stacktype == &t_list_empty)
5528 lvar->lv_type = &t_list_any;
5529 else if (stacktype == &t_dict_empty)
5530 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005531 else if (stacktype == &t_unknown)
5532 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005533 else
5534 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005536 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005537 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005538 {
5539 type_T *use_type = lvar->lv_type;
5540
Bram Moolenaar71abe482020-09-14 22:28:30 +02005541 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005542 if (has_index)
5543 {
5544 use_type = use_type->tt_member;
5545 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005546 // could be indexing "any"
5547 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005548 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005549 if (need_type(stacktype, use_type, -1, cctx,
5550 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005551 goto theend;
5552 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005553 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005554 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005555 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005556 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005558 else if (cmdidx == CMD_final)
5559 {
5560 emsg(_(e_final_requires_a_value));
5561 goto theend;
5562 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005565 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566 goto theend;
5567 }
5568 else if (!has_type || dest == dest_option)
5569 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005570 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 goto theend;
5572 }
5573 else
5574 {
5575 // variables are always initialized
5576 if (ga_grow(instr, 1) == FAIL)
5577 goto theend;
5578 switch (member_type->tt_type)
5579 {
5580 case VAR_BOOL:
5581 generate_PUSHBOOL(cctx, VVAL_FALSE);
5582 break;
5583 case VAR_FLOAT:
5584#ifdef FEAT_FLOAT
5585 generate_PUSHF(cctx, 0.0);
5586#endif
5587 break;
5588 case VAR_STRING:
5589 generate_PUSHS(cctx, NULL);
5590 break;
5591 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005592 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005593 break;
5594 case VAR_FUNC:
5595 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5596 break;
5597 case VAR_LIST:
5598 generate_NEWLIST(cctx, 0);
5599 break;
5600 case VAR_DICT:
5601 generate_NEWDICT(cctx, 0);
5602 break;
5603 case VAR_JOB:
5604 generate_PUSHJOB(cctx, NULL);
5605 break;
5606 case VAR_CHANNEL:
5607 generate_PUSHCHANNEL(cctx, NULL);
5608 break;
5609 case VAR_NUMBER:
5610 case VAR_UNKNOWN:
5611 case VAR_ANY:
5612 case VAR_PARTIAL:
5613 case VAR_VOID:
5614 case VAR_SPECIAL: // cannot happen
5615 generate_PUSHNR(cctx, 0);
5616 break;
5617 }
5618 }
5619 if (var_count == 0)
5620 end = p;
5621 }
5622
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005623 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005624 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005625 break;
5626
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005627 if (oplen > 0 && *op != '=')
5628 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005629 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005630 type_T *stacktype;
5631
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005632 if (*op == '.')
5633 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005634 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005635 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005637 if (
5638#ifdef FEAT_FLOAT
5639 // If variable is float operation with number is OK.
5640 !(expected == &t_float && stacktype == &t_number) &&
5641#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005642 need_type(stacktype, expected, -1, cctx,
5643 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 goto theend;
5645
5646 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005647 {
5648 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5649 goto theend;
5650 }
5651 else if (*op == '+')
5652 {
5653 if (generate_add_instr(cctx,
5654 operator_type(member_type, stacktype),
5655 member_type, stacktype) == FAIL)
5656 goto theend;
5657 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005658 else if (generate_two_op(cctx, op) == FAIL)
5659 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005662 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005663 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005664 int r;
5665
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005666 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005667 if (new_local)
5668 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005669 p = var_start + varlen;
5670 if (*p == '[')
5671 {
5672 p = skipwhite(p + 1);
5673 r = compile_expr0(&p, cctx);
5674 if (r == OK && *skipwhite(p) != ']')
5675 {
5676 // this should not happen
5677 emsg(_(e_missbrac));
5678 r = FAIL;
5679 }
5680 }
5681 else // if (*p == '.')
5682 {
5683 char_u *key_end = to_name_end(p + 1, TRUE);
5684 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5685
5686 r = generate_PUSHS(cctx, key);
5687 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688 if (new_local)
5689 ++cctx->ctx_locals.ga_len;
5690 if (r == FAIL)
5691 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005692
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005693 if (type == &t_any)
5694 {
5695 type_T *idx_type = ((type_T **)stack->ga_data)[
5696 stack->ga_len - 1];
5697 // Index on variable of unknown type: guess the type from the
5698 // index type: number is dict, otherwise dict.
5699 // TODO: should do the assignment at runtime
5700 if (idx_type->tt_type == VAR_NUMBER)
5701 type = &t_list_any;
5702 else
5703 type = &t_dict_any;
5704 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005705 if (type->tt_type == VAR_DICT
5706 && may_generate_2STRING(-1, cctx) == FAIL)
5707 goto theend;
5708 if (type->tt_type == VAR_LIST
5709 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005710 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005711 {
5712 emsg(_(e_number_exp));
5713 goto theend;
5714 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005715
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005716 // Load the dict or list. On the stack we then have:
5717 // - value
5718 // - index
5719 // - variable
5720 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005721
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005722 if (type->tt_type == VAR_LIST)
5723 {
5724 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5725 return FAIL;
5726 }
5727 else if (type->tt_type == VAR_DICT)
5728 {
5729 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5730 return FAIL;
5731 }
5732 else
5733 {
5734 emsg(_(e_listreq));
5735 goto theend;
5736 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005737 }
5738 else
5739 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005740 if (is_decl && cmdidx == CMD_const
5741 && (dest == dest_script || dest == dest_local))
5742 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005743 generate_LOCKCONST(cctx);
5744
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005745 switch (dest)
5746 {
5747 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005748 generate_STOREOPT(cctx, skip_option_env_lead(name),
5749 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005750 break;
5751 case dest_global:
5752 // include g: with the name, easier to execute that way
5753 generate_STORE(cctx, ISN_STOREG, 0, name);
5754 break;
5755 case dest_buffer:
5756 // include b: with the name, easier to execute that way
5757 generate_STORE(cctx, ISN_STOREB, 0, name);
5758 break;
5759 case dest_window:
5760 // include w: with the name, easier to execute that way
5761 generate_STORE(cctx, ISN_STOREW, 0, name);
5762 break;
5763 case dest_tab:
5764 // include t: with the name, easier to execute that way
5765 generate_STORE(cctx, ISN_STORET, 0, name);
5766 break;
5767 case dest_env:
5768 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5769 break;
5770 case dest_reg:
5771 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5772 break;
5773 case dest_vimvar:
5774 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5775 break;
5776 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005777 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005778 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005779 {
5780 char_u *name_s = name;
5781
5782 // Include s: in the name for store_var()
5783 if (name[1] != ':')
5784 {
5785 int len = (int)STRLEN(name) + 3;
5786
5787 name_s = alloc(len);
5788 if (name_s == NULL)
5789 name_s = name;
5790 else
5791 vim_snprintf((char *)name_s, len,
5792 "s:%s", name);
5793 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005794 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5795 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005796 if (name_s != name)
5797 vim_free(name_s);
5798 }
5799 else
5800 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005801 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005802 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005803 break;
5804 case dest_local:
5805 if (lvar != NULL)
5806 {
5807 isn_T *isn = ((isn_T *)instr->ga_data)
5808 + instr->ga_len - 1;
5809
5810 // optimization: turn "var = 123" from ISN_PUSHNR +
5811 // ISN_STORE into ISN_STORENR
5812 if (!lvar->lv_from_outer
5813 && instr->ga_len == instr_count + 1
5814 && isn->isn_type == ISN_PUSHNR)
5815 {
5816 varnumber_T val = isn->isn_arg.number;
5817
5818 isn->isn_type = ISN_STORENR;
5819 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5820 isn->isn_arg.storenr.stnr_val = val;
5821 if (stack->ga_len > 0)
5822 --stack->ga_len;
5823 }
5824 else if (lvar->lv_from_outer)
5825 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005826 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005827 else
5828 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5829 }
5830 break;
5831 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005832 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005833
5834 if (var_idx + 1 < var_count)
5835 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005837
5838 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005839 if (var_count > 0 && !semicolon)
5840 {
5841 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5842 goto theend;
5843 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005844
Bram Moolenaarb2097502020-07-19 17:17:02 +02005845 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846
5847theend:
5848 vim_free(name);
5849 return ret;
5850}
5851
5852/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005853 * Check if "name" can be "unlet".
5854 */
5855 int
5856check_vim9_unlet(char_u *name)
5857{
5858 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5859 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005860 // "unlet s:var" is allowed in legacy script.
5861 if (*name == 's' && !script_is_vim9())
5862 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005863 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005864 return FAIL;
5865 }
5866 return OK;
5867}
5868
5869/*
5870 * Callback passed to ex_unletlock().
5871 */
5872 static int
5873compile_unlet(
5874 lval_T *lvp,
5875 char_u *name_end,
5876 exarg_T *eap,
5877 int deep UNUSED,
5878 void *coookie)
5879{
5880 cctx_T *cctx = coookie;
5881
5882 if (lvp->ll_tv == NULL)
5883 {
5884 char_u *p = lvp->ll_name;
5885 int cc = *name_end;
5886 int ret = OK;
5887
5888 // Normal name. Only supports g:, w:, t: and b: namespaces.
5889 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005890 if (*p == '$')
5891 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5892 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005893 ret = FAIL;
5894 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005895 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005896
5897 *name_end = cc;
5898 return ret;
5899 }
5900
5901 // TODO: unlet {list}[idx]
5902 // TODO: unlet {dict}[key]
5903 emsg("Sorry, :unlet not fully implemented yet");
5904 return FAIL;
5905}
5906
5907/*
5908 * compile "unlet var", "lock var" and "unlock var"
5909 * "arg" points to "var".
5910 */
5911 static char_u *
5912compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5913{
5914 char_u *p = arg;
5915
5916 if (eap->cmdidx != CMD_unlet)
5917 {
5918 emsg("Sorry, :lock and unlock not implemented yet");
5919 return NULL;
5920 }
5921
5922 if (*p == '!')
5923 {
5924 p = skipwhite(p + 1);
5925 eap->forceit = TRUE;
5926 }
5927
5928 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5929 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5930}
5931
5932/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 * Compile an :import command.
5934 */
5935 static char_u *
5936compile_import(char_u *arg, cctx_T *cctx)
5937{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005938 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939}
5940
5941/*
5942 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5943 */
5944 static int
5945compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5946{
5947 garray_T *instr = &cctx->ctx_instr;
5948 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5949
5950 if (endlabel == NULL)
5951 return FAIL;
5952 endlabel->el_next = *el;
5953 *el = endlabel;
5954 endlabel->el_end_label = instr->ga_len;
5955
5956 generate_JUMP(cctx, when, 0);
5957 return OK;
5958}
5959
5960 static void
5961compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5962{
5963 garray_T *instr = &cctx->ctx_instr;
5964
5965 while (*el != NULL)
5966 {
5967 endlabel_T *cur = (*el);
5968 isn_T *isn;
5969
5970 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5971 isn->isn_arg.jump.jump_where = instr->ga_len;
5972 *el = cur->el_next;
5973 vim_free(cur);
5974 }
5975}
5976
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005977 static void
5978compile_free_jump_to_end(endlabel_T **el)
5979{
5980 while (*el != NULL)
5981 {
5982 endlabel_T *cur = (*el);
5983
5984 *el = cur->el_next;
5985 vim_free(cur);
5986 }
5987}
5988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989/*
5990 * Create a new scope and set up the generic items.
5991 */
5992 static scope_T *
5993new_scope(cctx_T *cctx, scopetype_T type)
5994{
5995 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5996
5997 if (scope == NULL)
5998 return NULL;
5999 scope->se_outer = cctx->ctx_scope;
6000 cctx->ctx_scope = scope;
6001 scope->se_type = type;
6002 scope->se_local_count = cctx->ctx_locals.ga_len;
6003 return scope;
6004}
6005
6006/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006007 * Free the current scope and go back to the outer scope.
6008 */
6009 static void
6010drop_scope(cctx_T *cctx)
6011{
6012 scope_T *scope = cctx->ctx_scope;
6013
6014 if (scope == NULL)
6015 {
6016 iemsg("calling drop_scope() without a scope");
6017 return;
6018 }
6019 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006020 switch (scope->se_type)
6021 {
6022 case IF_SCOPE:
6023 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6024 case FOR_SCOPE:
6025 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6026 case WHILE_SCOPE:
6027 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6028 case TRY_SCOPE:
6029 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6030 case NO_SCOPE:
6031 case BLOCK_SCOPE:
6032 break;
6033 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006034 vim_free(scope);
6035}
6036
6037/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 * compile "if expr"
6039 *
6040 * "if expr" Produces instructions:
6041 * EVAL expr Push result of "expr"
6042 * JUMP_IF_FALSE end
6043 * ... body ...
6044 * end:
6045 *
6046 * "if expr | else" Produces instructions:
6047 * EVAL expr Push result of "expr"
6048 * JUMP_IF_FALSE else
6049 * ... body ...
6050 * JUMP_ALWAYS end
6051 * else:
6052 * ... body ...
6053 * end:
6054 *
6055 * "if expr1 | elseif expr2 | else" Produces instructions:
6056 * EVAL expr Push result of "expr"
6057 * JUMP_IF_FALSE elseif
6058 * ... body ...
6059 * JUMP_ALWAYS end
6060 * elseif:
6061 * EVAL expr Push result of "expr"
6062 * JUMP_IF_FALSE else
6063 * ... body ...
6064 * JUMP_ALWAYS end
6065 * else:
6066 * ... body ...
6067 * end:
6068 */
6069 static char_u *
6070compile_if(char_u *arg, cctx_T *cctx)
6071{
6072 char_u *p = arg;
6073 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006074 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006076 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006077 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078
Bram Moolenaara5565e42020-05-09 15:44:01 +02006079 CLEAR_FIELD(ppconst);
6080 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006081 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006082 clear_ppconst(&ppconst);
6083 return NULL;
6084 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006085 if (cctx->ctx_skip == SKIP_YES)
6086 clear_ppconst(&ppconst);
6087 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006088 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006089 int error = FALSE;
6090 int v;
6091
Bram Moolenaara5565e42020-05-09 15:44:01 +02006092 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006093 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006094 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006095 if (error)
6096 return NULL;
6097 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006098 }
6099 else
6100 {
6101 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006102 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006103 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006104 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006105 if (bool_on_stack(cctx) == FAIL)
6106 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108
6109 scope = new_scope(cctx, IF_SCOPE);
6110 if (scope == NULL)
6111 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006112 scope->se_skip_save = skip_save;
6113 // "is_had_return" will be reset if any block does not end in :return
6114 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006116 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006117 {
6118 // "where" is set when ":elseif", "else" or ":endif" is found
6119 scope->se_u.se_if.is_if_label = instr->ga_len;
6120 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6121 }
6122 else
6123 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124
6125 return p;
6126}
6127
6128 static char_u *
6129compile_elseif(char_u *arg, cctx_T *cctx)
6130{
6131 char_u *p = arg;
6132 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006133 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 isn_T *isn;
6135 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006136 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006137 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138
6139 if (scope == NULL || scope->se_type != IF_SCOPE)
6140 {
6141 emsg(_(e_elseif_without_if));
6142 return NULL;
6143 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006144 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006145 if (!cctx->ctx_had_return)
6146 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006148 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006149 {
6150 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006152 return NULL;
6153 // previous "if" or "elseif" jumps here
6154 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6155 isn->isn_arg.jump.jump_where = instr->ga_len;
6156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006158 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006159 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006160 if (cctx->ctx_skip == SKIP_YES)
6161 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006162 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006163 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006164 clear_ppconst(&ppconst);
6165 return NULL;
6166 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006167 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006168 if (scope->se_skip_save == SKIP_YES)
6169 clear_ppconst(&ppconst);
6170 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006171 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006172 int error = FALSE;
6173 int v;
6174
Bram Moolenaar7f141552020-05-09 17:35:53 +02006175 // The expression results in a constant.
6176 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006177 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6178 if (error)
6179 return NULL;
6180 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006181 clear_ppconst(&ppconst);
6182 scope->se_u.se_if.is_if_label = -1;
6183 }
6184 else
6185 {
6186 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006187 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006188 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006189 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006190 if (bool_on_stack(cctx) == FAIL)
6191 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006193 // "where" is set when ":elseif", "else" or ":endif" is found
6194 scope->se_u.se_if.is_if_label = instr->ga_len;
6195 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197
6198 return p;
6199}
6200
6201 static char_u *
6202compile_else(char_u *arg, cctx_T *cctx)
6203{
6204 char_u *p = arg;
6205 garray_T *instr = &cctx->ctx_instr;
6206 isn_T *isn;
6207 scope_T *scope = cctx->ctx_scope;
6208
6209 if (scope == NULL || scope->se_type != IF_SCOPE)
6210 {
6211 emsg(_(e_else_without_if));
6212 return NULL;
6213 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006214 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006215 if (!cctx->ctx_had_return)
6216 scope->se_u.se_if.is_had_return = FALSE;
6217 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
Bram Moolenaarefd88552020-06-18 20:50:10 +02006219 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006220 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006221 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006222 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006223 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006224 if (!cctx->ctx_had_return
6225 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6226 JUMP_ALWAYS, cctx) == FAIL)
6227 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006228 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006229
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006230 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006231 {
6232 if (scope->se_u.se_if.is_if_label >= 0)
6233 {
6234 // previous "if" or "elseif" jumps here
6235 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6236 isn->isn_arg.jump.jump_where = instr->ga_len;
6237 scope->se_u.se_if.is_if_label = -1;
6238 }
6239 }
6240
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006241 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006242 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244
6245 return p;
6246}
6247
6248 static char_u *
6249compile_endif(char_u *arg, cctx_T *cctx)
6250{
6251 scope_T *scope = cctx->ctx_scope;
6252 ifscope_T *ifscope;
6253 garray_T *instr = &cctx->ctx_instr;
6254 isn_T *isn;
6255
6256 if (scope == NULL || scope->se_type != IF_SCOPE)
6257 {
6258 emsg(_(e_endif_without_if));
6259 return NULL;
6260 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006261 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006262 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006263 if (!cctx->ctx_had_return)
6264 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006266 if (scope->se_u.se_if.is_if_label >= 0)
6267 {
6268 // previous "if" or "elseif" jumps here
6269 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6270 isn->isn_arg.jump.jump_where = instr->ga_len;
6271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 // Fill in the "end" label in jumps at the end of the blocks.
6273 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006274 cctx->ctx_skip = scope->se_skip_save;
6275
6276 // If all the blocks end in :return and there is an :else then the
6277 // had_return flag is set.
6278 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006280 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 return arg;
6282}
6283
6284/*
6285 * compile "for var in expr"
6286 *
6287 * Produces instructions:
6288 * PUSHNR -1
6289 * STORE loop-idx Set index to -1
6290 * EVAL expr Push result of "expr"
6291 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6292 * - if beyond end, jump to "end"
6293 * - otherwise get item from list and push it
6294 * STORE var Store item in "var"
6295 * ... body ...
6296 * JUMP top Jump back to repeat
6297 * end: DROP Drop the result of "expr"
6298 *
6299 */
6300 static char_u *
6301compile_for(char_u *arg, cctx_T *cctx)
6302{
6303 char_u *p;
6304 size_t varlen;
6305 garray_T *instr = &cctx->ctx_instr;
6306 garray_T *stack = &cctx->ctx_type_stack;
6307 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006308 lvar_T *loop_lvar; // loop iteration variable
6309 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 type_T *vartype;
6311
6312 // TODO: list of variables: "for [key, value] in dict"
6313 // parse "var"
6314 for (p = arg; eval_isnamec1(*p); ++p)
6315 ;
6316 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006317 var_lvar = lookup_local(arg, varlen, cctx);
6318 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006320 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 return NULL;
6322 }
6323
6324 // consume "in"
6325 p = skipwhite(p);
6326 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6327 {
6328 emsg(_(e_missing_in));
6329 return NULL;
6330 }
6331 p = skipwhite(p + 2);
6332
6333
6334 scope = new_scope(cctx, FOR_SCOPE);
6335 if (scope == NULL)
6336 return NULL;
6337
6338 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006339 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6340 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006341 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006342 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006343 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
6347 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006348 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6349 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006350 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006351 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006352 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006356 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
6358 // compile "expr", it remains on the stack until "endfor"
6359 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006360 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006361 {
6362 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006366 // Now that we know the type of "var", check that it is a list, now or at
6367 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006369 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006371 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372 return NULL;
6373 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006374 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006375 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376
6377 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006378 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006380 generate_FOR(cctx, loop_lvar->lv_idx);
6381 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382
6383 return arg;
6384}
6385
6386/*
6387 * compile "endfor"
6388 */
6389 static char_u *
6390compile_endfor(char_u *arg, cctx_T *cctx)
6391{
6392 garray_T *instr = &cctx->ctx_instr;
6393 scope_T *scope = cctx->ctx_scope;
6394 forscope_T *forscope;
6395 isn_T *isn;
6396
6397 if (scope == NULL || scope->se_type != FOR_SCOPE)
6398 {
6399 emsg(_(e_for));
6400 return NULL;
6401 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006402 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006404 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405
6406 // At end of ":for" scope jump back to the FOR instruction.
6407 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6408
6409 // Fill in the "end" label in the FOR statement so it can jump here
6410 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6411 isn->isn_arg.forloop.for_end = instr->ga_len;
6412
6413 // Fill in the "end" label any BREAK statements
6414 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6415
6416 // Below the ":for" scope drop the "expr" list from the stack.
6417 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6418 return NULL;
6419
6420 vim_free(scope);
6421
6422 return arg;
6423}
6424
6425/*
6426 * compile "while expr"
6427 *
6428 * Produces instructions:
6429 * top: EVAL expr Push result of "expr"
6430 * JUMP_IF_FALSE end jump if false
6431 * ... body ...
6432 * JUMP top Jump back to repeat
6433 * end:
6434 *
6435 */
6436 static char_u *
6437compile_while(char_u *arg, cctx_T *cctx)
6438{
6439 char_u *p = arg;
6440 garray_T *instr = &cctx->ctx_instr;
6441 scope_T *scope;
6442
6443 scope = new_scope(cctx, WHILE_SCOPE);
6444 if (scope == NULL)
6445 return NULL;
6446
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006447 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
6449 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006450 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451 return NULL;
6452
Bram Moolenaar13106602020-10-04 16:06:05 +02006453 if (bool_on_stack(cctx) == FAIL)
6454 return FAIL;
6455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006457 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 JUMP_IF_FALSE, cctx) == FAIL)
6459 return FAIL;
6460
6461 return p;
6462}
6463
6464/*
6465 * compile "endwhile"
6466 */
6467 static char_u *
6468compile_endwhile(char_u *arg, cctx_T *cctx)
6469{
6470 scope_T *scope = cctx->ctx_scope;
6471
6472 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6473 {
6474 emsg(_(e_while));
6475 return NULL;
6476 }
6477 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006478 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479
6480 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006481 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483 // Fill in the "end" label in the WHILE statement so it can jump here.
6484 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
6487 vim_free(scope);
6488
6489 return arg;
6490}
6491
6492/*
6493 * compile "continue"
6494 */
6495 static char_u *
6496compile_continue(char_u *arg, cctx_T *cctx)
6497{
6498 scope_T *scope = cctx->ctx_scope;
6499
6500 for (;;)
6501 {
6502 if (scope == NULL)
6503 {
6504 emsg(_(e_continue));
6505 return NULL;
6506 }
6507 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6508 break;
6509 scope = scope->se_outer;
6510 }
6511
6512 // Jump back to the FOR or WHILE instruction.
6513 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006514 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6515 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 return arg;
6517}
6518
6519/*
6520 * compile "break"
6521 */
6522 static char_u *
6523compile_break(char_u *arg, cctx_T *cctx)
6524{
6525 scope_T *scope = cctx->ctx_scope;
6526 endlabel_T **el;
6527
6528 for (;;)
6529 {
6530 if (scope == NULL)
6531 {
6532 emsg(_(e_break));
6533 return NULL;
6534 }
6535 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6536 break;
6537 scope = scope->se_outer;
6538 }
6539
6540 // Jump to the end of the FOR or WHILE loop.
6541 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006542 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006544 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6546 return FAIL;
6547
6548 return arg;
6549}
6550
6551/*
6552 * compile "{" start of block
6553 */
6554 static char_u *
6555compile_block(char_u *arg, cctx_T *cctx)
6556{
6557 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6558 return NULL;
6559 return skipwhite(arg + 1);
6560}
6561
6562/*
6563 * compile end of block: drop one scope
6564 */
6565 static void
6566compile_endblock(cctx_T *cctx)
6567{
6568 scope_T *scope = cctx->ctx_scope;
6569
6570 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006571 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 vim_free(scope);
6573}
6574
6575/*
6576 * compile "try"
6577 * Creates a new scope for the try-endtry, pointing to the first catch and
6578 * finally.
6579 * Creates another scope for the "try" block itself.
6580 * TRY instruction sets up exception handling at runtime.
6581 *
6582 * "try"
6583 * TRY -> catch1, -> finally push trystack entry
6584 * ... try block
6585 * "throw {exception}"
6586 * EVAL {exception}
6587 * THROW create exception
6588 * ... try block
6589 * " catch {expr}"
6590 * JUMP -> finally
6591 * catch1: PUSH exeception
6592 * EVAL {expr}
6593 * MATCH
6594 * JUMP nomatch -> catch2
6595 * CATCH remove exception
6596 * ... catch block
6597 * " catch"
6598 * JUMP -> finally
6599 * catch2: CATCH remove exception
6600 * ... catch block
6601 * " finally"
6602 * finally:
6603 * ... finally block
6604 * " endtry"
6605 * ENDTRY pop trystack entry, may rethrow
6606 */
6607 static char_u *
6608compile_try(char_u *arg, cctx_T *cctx)
6609{
6610 garray_T *instr = &cctx->ctx_instr;
6611 scope_T *try_scope;
6612 scope_T *scope;
6613
6614 // scope that holds the jumps that go to catch/finally/endtry
6615 try_scope = new_scope(cctx, TRY_SCOPE);
6616 if (try_scope == NULL)
6617 return NULL;
6618
6619 // "catch" is set when the first ":catch" is found.
6620 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006621 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 if (generate_instr(cctx, ISN_TRY) == NULL)
6623 return NULL;
6624
6625 // scope for the try block itself
6626 scope = new_scope(cctx, BLOCK_SCOPE);
6627 if (scope == NULL)
6628 return NULL;
6629
6630 return arg;
6631}
6632
6633/*
6634 * compile "catch {expr}"
6635 */
6636 static char_u *
6637compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6638{
6639 scope_T *scope = cctx->ctx_scope;
6640 garray_T *instr = &cctx->ctx_instr;
6641 char_u *p;
6642 isn_T *isn;
6643
6644 // end block scope from :try or :catch
6645 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6646 compile_endblock(cctx);
6647 scope = cctx->ctx_scope;
6648
6649 // Error if not in a :try scope
6650 if (scope == NULL || scope->se_type != TRY_SCOPE)
6651 {
6652 emsg(_(e_catch));
6653 return NULL;
6654 }
6655
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006656 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006658 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 return NULL;
6660 }
6661
6662 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 JUMP_ALWAYS, cctx) == FAIL)
6665 return NULL;
6666
6667 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006668 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 if (isn->isn_arg.try.try_catch == 0)
6670 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006671 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 {
6673 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006674 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 isn->isn_arg.jump.jump_where = instr->ga_len;
6676 }
6677
6678 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006679 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006681 scope->se_u.se_try.ts_caught_all = TRUE;
6682 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 }
6684 else
6685 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006686 char_u *end;
6687 char_u *pat;
6688 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006689 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006690 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 // Push v:exception, push {expr} and MATCH
6693 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6694
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006695 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006696 if (*end != *p)
6697 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006698 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006699 vim_free(tofree);
6700 return FAIL;
6701 }
6702 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006703 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006704 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006705 len = (int)(end - tofree);
6706 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006707 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006708 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006709 if (pat == NULL)
6710 return FAIL;
6711 if (generate_PUSHS(cctx, pat) == FAIL)
6712 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6715 return NULL;
6716
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006717 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6719 return NULL;
6720 }
6721
6722 if (generate_instr(cctx, ISN_CATCH) == NULL)
6723 return NULL;
6724
6725 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6726 return NULL;
6727 return p;
6728}
6729
6730 static char_u *
6731compile_finally(char_u *arg, cctx_T *cctx)
6732{
6733 scope_T *scope = cctx->ctx_scope;
6734 garray_T *instr = &cctx->ctx_instr;
6735 isn_T *isn;
6736
6737 // end block scope from :try or :catch
6738 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6739 compile_endblock(cctx);
6740 scope = cctx->ctx_scope;
6741
6742 // Error if not in a :try scope
6743 if (scope == NULL || scope->se_type != TRY_SCOPE)
6744 {
6745 emsg(_(e_finally));
6746 return NULL;
6747 }
6748
6749 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006750 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 if (isn->isn_arg.try.try_finally != 0)
6752 {
6753 emsg(_(e_finally_dup));
6754 return NULL;
6755 }
6756
6757 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006758 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
Bram Moolenaar585fea72020-04-02 22:33:21 +02006760 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006761 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 {
6763 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006764 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006766 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 }
6768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 // TODO: set index in ts_finally_label jumps
6770
6771 return arg;
6772}
6773
6774 static char_u *
6775compile_endtry(char_u *arg, cctx_T *cctx)
6776{
6777 scope_T *scope = cctx->ctx_scope;
6778 garray_T *instr = &cctx->ctx_instr;
6779 isn_T *isn;
6780
6781 // end block scope from :catch or :finally
6782 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6783 compile_endblock(cctx);
6784 scope = cctx->ctx_scope;
6785
6786 // Error if not in a :try scope
6787 if (scope == NULL || scope->se_type != TRY_SCOPE)
6788 {
6789 if (scope == NULL)
6790 emsg(_(e_no_endtry));
6791 else if (scope->se_type == WHILE_SCOPE)
6792 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006793 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 emsg(_(e_endfor));
6795 else
6796 emsg(_(e_endif));
6797 return NULL;
6798 }
6799
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006800 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6802 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006803 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 return NULL;
6805 }
6806
6807 // Fill in the "end" label in jumps at the end of the blocks, if not done
6808 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006809 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006812 if (isn->isn_arg.try.try_catch == 0)
6813 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 if (isn->isn_arg.try.try_finally == 0)
6815 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006816
6817 if (scope->se_u.se_try.ts_catch_label != 0)
6818 {
6819 // Last catch without match jumps here
6820 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6821 isn->isn_arg.jump.jump_where = instr->ga_len;
6822 }
6823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 compile_endblock(cctx);
6825
6826 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6827 return NULL;
6828 return arg;
6829}
6830
6831/*
6832 * compile "throw {expr}"
6833 */
6834 static char_u *
6835compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6836{
6837 char_u *p = skipwhite(arg);
6838
Bram Moolenaara5565e42020-05-09 15:44:01 +02006839 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 return NULL;
6841 if (may_generate_2STRING(-1, cctx) == FAIL)
6842 return NULL;
6843 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6844 return NULL;
6845
6846 return p;
6847}
6848
6849/*
6850 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851 * compile "echomsg expr"
6852 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006853 * compile "execute expr"
6854 */
6855 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006856compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006857{
6858 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006859 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006860 int count = 0;
6861
6862 for (;;)
6863 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006864 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006865 return NULL;
6866 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006867 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006868 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006869 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006870 break;
6871 }
6872
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006873 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6874 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6875 else if (cmdidx == CMD_execute)
6876 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6877 else if (cmdidx == CMD_echomsg)
6878 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6879 else
6880 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 return p;
6882}
6883
6884/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006885 * :put r
6886 * :put ={expr}
6887 */
6888 static char_u *
6889compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6890{
6891 char_u *line = arg;
6892 linenr_T lnum;
6893 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006894 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006895
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006896 eap->regname = *line;
6897
6898 if (eap->regname == '=')
6899 {
6900 char_u *p = line + 1;
6901
6902 if (compile_expr0(&p, cctx) == FAIL)
6903 return NULL;
6904 line = p;
6905 }
6906 else if (eap->regname != NUL)
6907 ++line;
6908
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006909 // "errormsg" will not be set because the range is ADDR_LINES.
6910 // TODO: if the range contains something like "$" or "." need to evaluate
6911 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006912 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6913 return NULL;
6914 if (eap->addr_count == 0)
6915 lnum = -1;
6916 else
6917 lnum = eap->line2;
6918 if (above)
6919 --lnum;
6920
6921 generate_PUT(cctx, eap->regname, lnum);
6922 return line;
6923}
6924
6925/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006926 * A command that is not compiled, execute with legacy code.
6927 */
6928 static char_u *
6929compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6930{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006931 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006932 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006933 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006934
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006935 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006936 goto theend;
6937
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006938 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006939 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006940 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006941 int usefilter = FALSE;
6942
6943 has_expr = argt & (EX_XFILE | EX_EXPAND);
6944
6945 // If the command can be followed by a bar, find the bar and truncate
6946 // it, so that the following command can be compiled.
6947 // The '|' is overwritten with a NUL, it is put back below.
6948 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6949 && *eap->arg == '!')
6950 // :w !filter or :r !filter or :r! filter
6951 usefilter = TRUE;
6952 if ((argt & EX_TRLBAR) && !usefilter)
6953 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006954 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006955 separate_nextcmd(eap);
6956 if (eap->nextcmd != NULL)
6957 nextcmd = eap->nextcmd;
6958 }
6959 }
6960
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006961 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6962 {
6963 // expand filename in "syntax include [@group] filename"
6964 has_expr = TRUE;
6965 eap->arg = skipwhite(eap->arg + 7);
6966 if (*eap->arg == '@')
6967 eap->arg = skiptowhite(eap->arg);
6968 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006969
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006970 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006971 {
6972 int count = 0;
6973 char_u *start = skipwhite(line);
6974
6975 // :cmd xxx`=expr1`yyy`=expr2`zzz
6976 // PUSHS ":cmd xxx"
6977 // eval expr1
6978 // PUSHS "yyy"
6979 // eval expr2
6980 // PUSHS "zzz"
6981 // EXECCONCAT 5
6982 for (;;)
6983 {
6984 if (p > start)
6985 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006986 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006987 ++count;
6988 }
6989 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006990 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006991 return NULL;
6992 may_generate_2STRING(-1, cctx);
6993 ++count;
6994 p = skipwhite(p);
6995 if (*p != '`')
6996 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006997 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006998 return NULL;
6999 }
7000 start = p + 1;
7001
7002 p = (char_u *)strstr((char *)start, "`=");
7003 if (p == NULL)
7004 {
7005 if (*skipwhite(start) != NUL)
7006 {
7007 generate_PUSHS(cctx, vim_strsave(start));
7008 ++count;
7009 }
7010 break;
7011 }
7012 }
7013 generate_EXECCONCAT(cctx, count);
7014 }
7015 else
7016 generate_EXEC(cctx, line);
7017
7018theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007019 if (*nextcmd != NUL)
7020 {
7021 // the parser expects a pointer to the bar, put it back
7022 --nextcmd;
7023 *nextcmd = '|';
7024 }
7025
7026 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007027}
7028
7029/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007030 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007031 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007032 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007033 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007034add_def_function(ufunc_T *ufunc)
7035{
7036 dfunc_T *dfunc;
7037
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007038 if (def_functions.ga_len == 0)
7039 {
7040 // The first position is not used, so that a zero uf_dfunc_idx means it
7041 // wasn't set.
7042 if (ga_grow(&def_functions, 1) == FAIL)
7043 return FAIL;
7044 ++def_functions.ga_len;
7045 }
7046
Bram Moolenaar09689a02020-05-09 22:50:08 +02007047 // Add the function to "def_functions".
7048 if (ga_grow(&def_functions, 1) == FAIL)
7049 return FAIL;
7050 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7051 CLEAR_POINTER(dfunc);
7052 dfunc->df_idx = def_functions.ga_len;
7053 ufunc->uf_dfunc_idx = dfunc->df_idx;
7054 dfunc->df_ufunc = ufunc;
7055 ++def_functions.ga_len;
7056 return OK;
7057}
7058
7059/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 * After ex_function() has collected all the function lines: parse and compile
7061 * the lines into instructions.
7062 * Adds the function to "def_functions".
7063 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7064 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007065 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007066 * This can be used recursively through compile_lambda(), which may reallocate
7067 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007068 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007070 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007071compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 char_u *line = NULL;
7074 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 cctx_T cctx;
7077 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007078 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 int ret = FAIL;
7080 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007081 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007082 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007083 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007085 // When using a function that was compiled before: Free old instructions.
7086 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007087 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7090 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007091 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007093 else
7094 {
7095 if (add_def_function(ufunc) == FAIL)
7096 return FAIL;
7097 new_def_function = TRUE;
7098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099
Bram Moolenaar985116a2020-07-12 17:31:09 +02007100 ufunc->uf_def_status = UF_COMPILING;
7101
Bram Moolenaara80faa82020-04-12 19:37:17 +02007102 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 cctx.ctx_ufunc = ufunc;
7104 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007105 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7107 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7108 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7109 cctx.ctx_type_list = &ufunc->uf_type_list;
7110 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7111 instr = &cctx.ctx_instr;
7112
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007113 // Set the context to the function, it may be compiled when called from
7114 // another script. Set the script version to the most modern one.
7115 // The line number will be set in next_line_from_context().
7116 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7118
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007119 // Make sure error messages are OK.
7120 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7121 if (do_estack_push)
7122 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007123 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007124
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007125 if (ufunc->uf_def_args.ga_len > 0)
7126 {
7127 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007128 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007129 int i;
7130 char_u *arg;
7131 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007132 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007133
7134 // Produce instructions for the default values of optional arguments.
7135 // Store the instruction index in uf_def_arg_idx[] so that we know
7136 // where to start when the function is called, depending on the number
7137 // of arguments.
7138 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7139 if (ufunc->uf_def_arg_idx == NULL)
7140 goto erret;
7141 for (i = 0; i < count; ++i)
7142 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007143 garray_T *stack = &cctx.ctx_type_stack;
7144 type_T *val_type;
7145 int arg_idx = first_def_arg + i;
7146
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007147 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7148 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007149 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007150 goto erret;
7151
7152 // If no type specified use the type of the default value.
7153 // Otherwise check that the default value type matches the
7154 // specified type.
7155 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7156 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007157 {
7158 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007159 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007160 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007161 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7162 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007163 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007164
7165 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007166 goto erret;
7167 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007168 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007169
7170 if (did_set_arg_type)
7171 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007172 }
7173
7174 /*
7175 * Loop over all the lines of the function and generate instructions.
7176 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 for (;;)
7178 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007179 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007180 int starts_with_colon = FALSE;
7181 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007182 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007183
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007184 // Bail out on the first error to avoid a flood of errors and report
7185 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007186 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007187 goto erret;
7188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 if (line != NULL && *line == '|')
7190 // the line continues after a '|'
7191 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007192 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007193 && !(*line == '#' && (line == cctx.ctx_line_start
7194 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007196 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 goto erret;
7198 }
7199 else
7200 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007201 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007202 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007203 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 }
7206
Bram Moolenaara80faa82020-04-12 19:37:17 +02007207 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 ea.cmdlinep = &line;
7209 ea.cmd = skipwhite(line);
7210
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007211 // Some things can be recognized by the first character.
7212 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007214 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007215 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007216 if (ea.cmd[1] != '{')
7217 {
7218 line = (char_u *)"";
7219 continue;
7220 }
7221 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007223 case '}':
7224 {
7225 // "}" ends a block scope
7226 scopetype_T stype = cctx.ctx_scope == NULL
7227 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007229 if (stype == BLOCK_SCOPE)
7230 {
7231 compile_endblock(&cctx);
7232 line = ea.cmd;
7233 }
7234 else
7235 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007236 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007237 goto erret;
7238 }
7239 if (line != NULL)
7240 line = skipwhite(ea.cmd + 1);
7241 continue;
7242 }
7243
7244 case '{':
7245 // "{" starts a block scope
7246 // "{'a': 1}->func() is something else
7247 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7248 {
7249 line = compile_block(ea.cmd, &cctx);
7250 continue;
7251 }
7252 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 }
7254
7255 /*
7256 * COMMAND MODIFIERS
7257 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007258 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007259 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7260 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 {
7262 if (errormsg != NULL)
7263 goto erret;
7264 // empty line or comment
7265 line = (char_u *)"";
7266 continue;
7267 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007268 generate_cmdmods(&cctx, &local_cmdmod);
7269 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007271 // Check if there was a colon after the last command modifier or before
7272 // the current position.
7273 for (p = ea.cmd; p >= line; --p)
7274 {
7275 if (*p == ':')
7276 starts_with_colon = TRUE;
7277 if (p < ea.cmd && !VIM_ISWHITE(*p))
7278 break;
7279 }
7280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007282 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007284 {
7285 if (*ea.cmd == '(')
7286 // not for "call()"
7287 ea.cmd = p;
7288 else
7289 ea.cmd = skipwhite(ea.cmd);
7290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007292 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007294 char_u *pskip;
7295
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007296 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007297 // find what follows.
7298 // Skip over "var.member", "var[idx]" and the like.
7299 // Also "&opt = val", "$ENV = val" and "@r = val".
7300 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007301 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007302 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007303 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007305 char_u *var_end;
7306 int oplen;
7307 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308
Bram Moolenaar65821722020-08-02 18:58:54 +02007309 if (ea.cmd[0] == '@')
7310 var_end = ea.cmd + 2;
7311 else
7312 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007313 FNE_CHECK_START | FNE_INCL_BR);
7314 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007315 if (oplen > 0)
7316 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007317 size_t len = p - ea.cmd;
7318
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007319 // Recognize an assignment if we recognize the variable
7320 // name:
7321 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007322 // "local = expr" where "local" is a local var.
7323 // "script = expr" where "script" is a script-local var.
7324 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007325 // "&opt = expr"
7326 // "$ENV = expr"
7327 // "@r = expr"
7328 if (*ea.cmd == '&'
7329 || *ea.cmd == '$'
7330 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007331 || ((len) > 2 && ea.cmd[1] == ':')
7332 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007333 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007334 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007335 || script_var_exists(ea.cmd, len,
7336 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007337 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007338 {
7339 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007340 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007341 goto erret;
7342 continue;
7343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 }
7345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007346
7347 if (*ea.cmd == '[')
7348 {
7349 // [var, var] = expr
7350 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7351 if (line == NULL)
7352 goto erret;
7353 if (line != ea.cmd)
7354 continue;
7355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 }
7357
7358 /*
7359 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007360 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007362 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007363 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007364 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007365 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007366 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007367 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007368 if (!starts_with_colon)
7369 {
7370 emsg(_(e_colon_required_before_a_range));
7371 goto erret;
7372 }
7373 if (ends_excmd2(line, ea.cmd))
7374 {
7375 // A range without a command: jump to the line.
7376 // TODO: compile to a more efficient command, possibly
7377 // calling parse_cmd_address().
7378 ea.cmdidx = CMD_SIZE;
7379 line = compile_exec(line, &ea, &cctx);
7380 goto nextline;
7381 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007382 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007383 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007384 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007385 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007386 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
7388 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7389 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007390 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007391 {
7392 line += STRLEN(line);
7393 continue;
7394 }
7395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007397 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007399 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007400 iemsg("Command from find_ex_command() not handled");
7401 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 }
7404
Bram Moolenaar3988f642020-08-27 22:43:03 +02007405 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007406 && ea.cmdidx != CMD_elseif
7407 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007408 && ea.cmdidx != CMD_endif
7409 && ea.cmdidx != CMD_endfor
7410 && ea.cmdidx != CMD_endwhile
7411 && ea.cmdidx != CMD_catch
7412 && ea.cmdidx != CMD_finally
7413 && ea.cmdidx != CMD_endtry)
7414 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007415 emsg(_(e_unreachable_code_after_return));
7416 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007417 }
7418
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007419 p = skipwhite(p);
7420 if (ea.cmdidx != CMD_SIZE
7421 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7422 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007423 if (ea.cmdidx >= 0)
7424 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007425 if ((ea.argt & EX_BANG) && *p == '!')
7426 {
7427 ea.forceit = TRUE;
7428 p = skipwhite(p + 1);
7429 }
7430 }
7431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 switch (ea.cmdidx)
7433 {
7434 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007435 ea.arg = p;
7436 line = compile_nested_function(&ea, &cctx);
7437 break;
7438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007440 // TODO: should we allow this, e.g. to declare a global
7441 // function?
7442 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 goto erret;
7444
7445 case CMD_return:
7446 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007447 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 break;
7449
7450 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007451 emsg(_(e_cannot_use_let_in_vim9_script));
7452 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007453 case CMD_var:
7454 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 case CMD_const:
7456 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007457 if (line == p)
7458 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 break;
7460
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007461 case CMD_unlet:
7462 case CMD_unlockvar:
7463 case CMD_lockvar:
7464 line = compile_unletlock(p, &ea, &cctx);
7465 break;
7466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 case CMD_import:
7468 line = compile_import(p, &cctx);
7469 break;
7470
7471 case CMD_if:
7472 line = compile_if(p, &cctx);
7473 break;
7474 case CMD_elseif:
7475 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007476 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 break;
7478 case CMD_else:
7479 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007480 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 break;
7482 case CMD_endif:
7483 line = compile_endif(p, &cctx);
7484 break;
7485
7486 case CMD_while:
7487 line = compile_while(p, &cctx);
7488 break;
7489 case CMD_endwhile:
7490 line = compile_endwhile(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
7494 case CMD_for:
7495 line = compile_for(p, &cctx);
7496 break;
7497 case CMD_endfor:
7498 line = compile_endfor(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_continue:
7502 line = compile_continue(p, &cctx);
7503 break;
7504 case CMD_break:
7505 line = compile_break(p, &cctx);
7506 break;
7507
7508 case CMD_try:
7509 line = compile_try(p, &cctx);
7510 break;
7511 case CMD_catch:
7512 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007513 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 break;
7515 case CMD_finally:
7516 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007517 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 break;
7519 case CMD_endtry:
7520 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007521 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 break;
7523 case CMD_throw:
7524 line = compile_throw(p, &cctx);
7525 break;
7526
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007527 case CMD_eval:
7528 if (compile_expr0(&p, &cctx) == FAIL)
7529 goto erret;
7530
Bram Moolenaar3988f642020-08-27 22:43:03 +02007531 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007532 generate_instr_drop(&cctx, ISN_DROP, 1);
7533
7534 line = skipwhite(p);
7535 break;
7536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007539 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007540 case CMD_echomsg:
7541 case CMD_echoerr:
7542 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007543 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007545 case CMD_put:
7546 ea.cmd = cmd;
7547 line = compile_put(p, &ea, &cctx);
7548 break;
7549
Bram Moolenaar3988f642020-08-27 22:43:03 +02007550 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007551
Bram Moolenaarae616492020-07-28 20:07:27 +02007552 case CMD_append:
7553 case CMD_change:
7554 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007555 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007556 case CMD_xit:
7557 not_in_vim9(&ea);
7558 goto erret;
7559
Bram Moolenaar002262f2020-07-08 17:47:57 +02007560 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007561 if (cctx.ctx_skip != SKIP_YES)
7562 {
7563 semsg(_(e_invalid_command_str), ea.cmd);
7564 goto erret;
7565 }
7566 // We don't check for a next command here.
7567 line = (char_u *)"";
7568 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007571 if (cctx.ctx_skip == SKIP_YES)
7572 {
7573 // We don't check for a next command here.
7574 line = (char_u *)"";
7575 }
7576 else
7577 {
7578 // Not recognized, execute with do_cmdline_cmd().
7579 ea.arg = p;
7580 line = compile_exec(line, &ea, &cctx);
7581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 break;
7583 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007584nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 if (line == NULL)
7586 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007587 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007589 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007590 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 if (cctx.ctx_type_stack.ga_len < 0)
7593 {
7594 iemsg("Type stack underflow");
7595 goto erret;
7596 }
7597 }
7598
7599 if (cctx.ctx_scope != NULL)
7600 {
7601 if (cctx.ctx_scope->se_type == IF_SCOPE)
7602 emsg(_(e_endif));
7603 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7604 emsg(_(e_endwhile));
7605 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7606 emsg(_(e_endfor));
7607 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007608 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 goto erret;
7610 }
7611
Bram Moolenaarefd88552020-06-18 20:50:10 +02007612 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 {
7614 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7615 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007616 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 goto erret;
7618 }
7619
7620 // Return zero if there is no return at the end.
7621 generate_PUSHNR(&cctx, 0);
7622 generate_instr(&cctx, ISN_RETURN);
7623 }
7624
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007625 {
7626 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7627 + ufunc->uf_dfunc_idx;
7628 dfunc->df_deleted = FALSE;
7629 dfunc->df_instr = instr->ga_data;
7630 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007631 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007632 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007633 if (cctx.ctx_outer_used)
7634 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007635 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637
7638 ret = OK;
7639
7640erret:
7641 if (ret == FAIL)
7642 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007643 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007644 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7645 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007646
7647 for (idx = 0; idx < instr->ga_len; ++idx)
7648 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007650
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007651 // If using the last entry in the table and it was added above, we
7652 // might as well remove it.
7653 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007654 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007655 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007656 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007657 ufunc->uf_dfunc_idx = 0;
7658 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007659 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007660
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007661 while (cctx.ctx_scope != NULL)
7662 drop_scope(&cctx);
7663
Bram Moolenaar20431c92020-03-20 18:39:46 +01007664 // Don't execute this function body.
7665 ga_clear_strings(&ufunc->uf_lines);
7666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 if (errormsg != NULL)
7668 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007669 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007670 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 }
7672
7673 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007674 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007675 if (do_estack_push)
7676 estack_pop();
7677
Bram Moolenaar20431c92020-03-20 18:39:46 +01007678 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007679 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007681 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682}
7683
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007684 void
7685set_function_type(ufunc_T *ufunc)
7686{
7687 int varargs = ufunc->uf_va_name != NULL;
7688 int argcount = ufunc->uf_args.ga_len;
7689
7690 // Create a type for the function, with the return type and any
7691 // argument types.
7692 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7693 // The type is included in "tt_args".
7694 if (argcount > 0 || varargs)
7695 {
7696 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7697 argcount, &ufunc->uf_type_list);
7698 // Add argument types to the function type.
7699 if (func_type_add_arg_types(ufunc->uf_func_type,
7700 argcount + varargs,
7701 &ufunc->uf_type_list) == FAIL)
7702 return;
7703 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7704 ufunc->uf_func_type->tt_min_argcount =
7705 argcount - ufunc->uf_def_args.ga_len;
7706 if (ufunc->uf_arg_types == NULL)
7707 {
7708 int i;
7709
7710 // lambda does not have argument types.
7711 for (i = 0; i < argcount; ++i)
7712 ufunc->uf_func_type->tt_args[i] = &t_any;
7713 }
7714 else
7715 mch_memmove(ufunc->uf_func_type->tt_args,
7716 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7717 if (varargs)
7718 {
7719 ufunc->uf_func_type->tt_args[argcount] =
7720 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7721 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7722 }
7723 }
7724 else
7725 // No arguments, can use a predefined type.
7726 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7727 argcount, &ufunc->uf_type_list);
7728}
7729
7730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731/*
7732 * Delete an instruction, free what it contains.
7733 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007734 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735delete_instr(isn_T *isn)
7736{
7737 switch (isn->isn_type)
7738 {
7739 case ISN_EXEC:
7740 case ISN_LOADENV:
7741 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007742 case ISN_LOADB:
7743 case ISN_LOADW:
7744 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007746 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 case ISN_PUSHEXC:
7748 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007749 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007751 case ISN_STOREB:
7752 case ISN_STOREW:
7753 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007754 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 vim_free(isn->isn_arg.string);
7756 break;
7757
7758 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007759 case ISN_STORES:
7760 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 break;
7762
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007763 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007764 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007765 vim_free(isn->isn_arg.unlet.ul_name);
7766 break;
7767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 case ISN_STOREOPT:
7769 vim_free(isn->isn_arg.storeopt.so_name);
7770 break;
7771
7772 case ISN_PUSHBLOB: // push blob isn_arg.blob
7773 blob_unref(isn->isn_arg.blob);
7774 break;
7775
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007776 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007778 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007779#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007780 break;
7781
7782 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007783#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007784 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007785#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007786 break;
7787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788 case ISN_UCALL:
7789 vim_free(isn->isn_arg.ufunc.cuf_name);
7790 break;
7791
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007792 case ISN_FUNCREF:
7793 {
7794 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7795 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007796
7797 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7798 func_ptr_unref(dfunc->df_ufunc);
7799 }
7800 break;
7801
7802 case ISN_DCALL:
7803 {
7804 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7805 + isn->isn_arg.dfunc.cdf_idx;
7806
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007807 if (dfunc->df_ufunc != NULL
7808 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007809 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007810 }
7811 break;
7812
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007813 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007814 {
7815 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7816 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7817
7818 if (ufunc != NULL)
7819 {
7820 // Clear uf_dfunc_idx so that the function is deleted.
7821 clear_def_function(ufunc);
7822 ufunc->uf_dfunc_idx = 0;
7823 func_ptr_unref(ufunc);
7824 }
7825
7826 vim_free(lambda);
7827 vim_free(isn->isn_arg.newfunc.nf_global);
7828 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007829 break;
7830
Bram Moolenaar5e654232020-09-16 15:22:00 +02007831 case ISN_CHECKTYPE:
7832 free_type(isn->isn_arg.type.ct_type);
7833 break;
7834
Bram Moolenaar02194d22020-10-24 23:08:38 +02007835 case ISN_CMDMOD:
7836 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7837 ->cmod_filter_regmatch.regprog);
7838 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7839 break;
7840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 case ISN_2BOOL:
7842 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007843 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 case ISN_ADDBLOB:
7845 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007846 case ISN_ANYINDEX:
7847 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007849 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007851 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007853 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854 case ISN_COMPAREANY:
7855 case ISN_COMPAREBLOB:
7856 case ISN_COMPAREBOOL:
7857 case ISN_COMPAREDICT:
7858 case ISN_COMPAREFLOAT:
7859 case ISN_COMPAREFUNC:
7860 case ISN_COMPARELIST:
7861 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 case ISN_COMPARESPECIAL:
7863 case ISN_COMPARESTRING:
7864 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007865 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 case ISN_DROP:
7867 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007868 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007869 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007871 case ISN_EXECCONCAT:
7872 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007874 case ISN_GETITEM:
7875 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007876 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007877 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007878 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007880 case ISN_LOADBDICT:
7881 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007882 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007884 case ISN_LOADSCRIPT:
7885 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007887 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007888 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007889 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 case ISN_NEGATENR:
7891 case ISN_NEWDICT:
7892 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007894 case ISN_OPFLOAT:
7895 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007897 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007898 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 case ISN_PUSHF:
7900 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007902 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007904 case ISN_SHUFFLE:
7905 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007907 case ISN_STOREDICT:
7908 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007909 case ISN_STORENR:
7910 case ISN_STOREOUTER:
7911 case ISN_STOREREG:
7912 case ISN_STORESCRIPT:
7913 case ISN_STOREV:
7914 case ISN_STRINDEX:
7915 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916 case ISN_THROW:
7917 case ISN_TRY:
7918 // nothing allocated
7919 break;
7920 }
7921}
7922
7923/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007924 * Free all instructions for "dfunc".
7925 */
7926 static void
7927delete_def_function_contents(dfunc_T *dfunc)
7928{
7929 int idx;
7930
7931 ga_clear(&dfunc->df_def_args_isn);
7932
7933 if (dfunc->df_instr != NULL)
7934 {
7935 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7936 delete_instr(dfunc->df_instr + idx);
7937 VIM_CLEAR(dfunc->df_instr);
7938 }
7939
7940 dfunc->df_deleted = TRUE;
7941}
7942
7943/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007944 * When a user function is deleted, clear the contents of any associated def
7945 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 */
7947 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007948clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007950 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951 {
7952 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7953 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954
Bram Moolenaar20431c92020-03-20 18:39:46 +01007955 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007956 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 }
7958}
7959
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007960/*
7961 * Used when a user function is about to be deleted: remove the pointer to it.
7962 * The entry in def_functions is then unused.
7963 */
7964 void
7965unlink_def_function(ufunc_T *ufunc)
7966{
7967 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7968
7969 dfunc->df_ufunc = NULL;
7970}
7971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007973/*
7974 * Free all functions defined with ":def".
7975 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976 void
7977free_def_functions(void)
7978{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007979 int idx;
7980
7981 for (idx = 0; idx < def_functions.ga_len; ++idx)
7982 {
7983 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7984
7985 delete_def_function_contents(dfunc);
7986 }
7987
7988 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989}
7990#endif
7991
7992
7993#endif // FEAT_EVAL