blob: 2c522809d992b37acf66ac4d99805d7777e9c690 [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 Moolenaar5381c7a2020-03-02 22:53:32 +01002774 static char_u *
2775to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776{
2777 char_u *p;
2778
2779 // Quick check for valid starting character.
2780 if (!eval_isnamec1(*arg))
2781 return arg;
2782
2783 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2784 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2785 // and can be used in slice "[n:]".
2786 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002787 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2789 break;
2790 return p;
2791}
2792
2793/*
2794 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002795 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 */
2797 char_u *
2798to_name_const_end(char_u *arg)
2799{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002800 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 typval_T rettv;
2802
2803 if (p == arg && *arg == '[')
2804 {
2805
2806 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002807 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 p = arg;
2809 }
2810 else if (p == arg && *arg == '#' && arg[1] == '{')
2811 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002812 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002814 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 p = arg;
2816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
2818 return p;
2819}
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821/*
2822 * parse a list: [expr, expr]
2823 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002824 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 */
2826 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002827compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828{
2829 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002830 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002832 int is_const;
2833 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002835 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002837 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002838 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002839 semsg(_(e_list_end), *arg);
2840 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002841 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002842 if (*p == ',')
2843 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002844 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002845 return FAIL;
2846 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002847 if (*p == ']')
2848 {
2849 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002850 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002851 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002852 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002853 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002854 if (!is_const)
2855 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 ++count;
2857 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002860 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2861 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002862 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002863 return FAIL;
2864 }
2865 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002866 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 p = skipwhite(p);
2868 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002869 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 ppconst->pp_is_const = is_all_const;
2872 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873}
2874
2875/*
2876 * parse a lambda: {arg, arg -> expr}
2877 * "*arg" points to the '{'.
2878 */
2879 static int
2880compile_lambda(char_u **arg, cctx_T *cctx)
2881{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 typval_T rettv;
2883 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002884 evalarg_T evalarg;
2885
2886 CLEAR_FIELD(evalarg);
2887 evalarg.eval_flags = EVAL_EVALUATE;
2888 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002891 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002892 {
2893 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002895 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002898 ++ufunc->uf_refcount;
2899 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
2901 // The function will have one line: "return {expr}".
2902 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002903 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002905 clear_evalarg(&evalarg, NULL);
2906
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002907 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002908 {
2909 // The return type will now be known.
2910 set_function_type(ufunc);
2911
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002912 // The function reference count will be 1. When the ISN_FUNCREF
2913 // instruction is deleted the reference count is decremented and the
2914 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002915 return generate_FUNCREF(cctx, ufunc);
2916 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002917
2918 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 return FAIL;
2920}
2921
2922/*
2923 * Compile a lamda call: expr->{lambda}(args)
2924 * "arg" points to the "{".
2925 */
2926 static int
2927compile_lambda_call(char_u **arg, cctx_T *cctx)
2928{
2929 ufunc_T *ufunc;
2930 typval_T rettv;
2931 int argcount = 1;
2932 int ret = FAIL;
2933
2934 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002935 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return FAIL;
2937
2938 if (**arg != '(')
2939 {
2940 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002941 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 else
2943 semsg(_(e_missing_paren), "lambda");
2944 clear_tv(&rettv);
2945 return FAIL;
2946 }
2947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 ufunc = rettv.vval.v_partial->pt_func;
2949 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002950 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002951 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002952
Bram Moolenaara05e5242020-09-19 18:19:19 +02002953 // The function will have one line: "return {expr}". Compile it into
2954 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002955 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 // compile the arguments
2958 *arg = skipwhite(*arg + 1);
2959 if (compile_arguments(arg, cctx, &argcount) == OK)
2960 // call the compiled function
2961 ret = generate_CALL(cctx, ufunc, argcount);
2962
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002963 if (ret == FAIL)
2964 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 return ret;
2966}
2967
2968/*
2969 * parse a dict: {'key': val} or #{key: val}
2970 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002971 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 */
2973 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002974compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975{
2976 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002977 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 int count = 0;
2979 dict_T *d = dict_alloc();
2980 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002981 char_u *whitep = *arg;
2982 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002983 int is_const;
2984 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985
2986 if (d == NULL)
2987 return FAIL;
2988 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
2991 char_u *key = NULL;
2992
Bram Moolenaar23c55272020-06-21 16:58:13 +02002993 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002994 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002995 *arg = NULL;
2996 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002997 }
2998
2999 if (**arg == '}')
3000 break;
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (literal)
3003 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003004 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
Bram Moolenaar2c330432020-04-13 14:41:35 +02003006 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003008 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003011 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 if (generate_PUSHS(cctx, key) == FAIL)
3013 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003014 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016 else
3017 {
3018 isn_T *isn;
3019
Bram Moolenaara5565e42020-05-09 15:44:01 +02003020 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3023 if (isn->isn_type == ISN_PUSHS)
3024 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003025 else
3026 {
3027 type_T *keytype = ((type_T **)stack->ga_data)
3028 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003029 if (need_type(keytype, &t_string, -1, cctx,
3030 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003031 return FAIL;
3032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034
3035 // Check for duplicate keys, if using string keys.
3036 if (key != NULL)
3037 {
3038 item = dict_find(d, key, -1);
3039 if (item != NULL)
3040 {
3041 semsg(_(e_duplicate_key), key);
3042 goto failret;
3043 }
3044 item = dictitem_alloc(key);
3045 if (item != NULL)
3046 {
3047 item->di_tv.v_type = VAR_UNKNOWN;
3048 item->di_tv.v_lock = 0;
3049 if (dict_add(d, item) == FAIL)
3050 dictitem_free(item);
3051 }
3052 }
3053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 if (**arg != ':')
3055 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003056 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003057 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003058 else
3059 semsg(_(e_missing_dict_colon), *arg);
3060 return FAIL;
3061 }
3062 whitep = *arg + 1;
3063 if (!IS_WHITE_OR_NUL(*whitep))
3064 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003065 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068
3069 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003070 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003072 *arg = NULL;
3073 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003074 }
3075
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003076 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003078 if (!is_const)
3079 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 ++count;
3081
Bram Moolenaar2c330432020-04-13 14:41:35 +02003082 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003083 *arg = skipwhite(*arg);
3084 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003085 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003086 *arg = NULL;
3087 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (**arg == '}')
3090 break;
3091 if (**arg != ',')
3092 {
3093 semsg(_(e_missing_dict_comma), *arg);
3094 goto failret;
3095 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003096 if (IS_WHITE_OR_NUL(*whitep))
3097 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003098 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003099 return FAIL;
3100 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003101 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003102 if (!IS_WHITE_OR_NUL(*whitep))
3103 {
3104 semsg(_(e_white_space_required_after_str), ",");
3105 return FAIL;
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 *arg = skipwhite(*arg + 1);
3108 }
3109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 *arg = *arg + 1;
3111
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003113 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003114 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003115 *arg += STRLEN(*arg);
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003118 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return generate_NEWDICT(cctx, count);
3120
3121failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003123 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003125 *arg = (char_u *)"";
3126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 dict_unref(d);
3128 return FAIL;
3129}
3130
3131/*
3132 * Compile "&option".
3133 */
3134 static int
3135compile_get_option(char_u **arg, cctx_T *cctx)
3136{
3137 typval_T rettv;
3138 char_u *start = *arg;
3139 int ret;
3140
3141 // parse the option and get the current value to get the type.
3142 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003143 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (ret == OK)
3145 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003146 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 char_u *name = vim_strnsave(start, *arg - start);
3148 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3149
3150 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3151 vim_free(name);
3152 }
3153 clear_tv(&rettv);
3154
3155 return ret;
3156}
3157
3158/*
3159 * Compile "$VAR".
3160 */
3161 static int
3162compile_get_env(char_u **arg, cctx_T *cctx)
3163{
3164 char_u *start = *arg;
3165 int len;
3166 int ret;
3167 char_u *name;
3168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 ++*arg;
3170 len = get_env_len(arg);
3171 if (len == 0)
3172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003173 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 return FAIL;
3175 }
3176
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003177 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 name = vim_strnsave(start, len + 1);
3179 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3180 vim_free(name);
3181 return ret;
3182}
3183
3184/*
3185 * Compile "@r".
3186 */
3187 static int
3188compile_get_register(char_u **arg, cctx_T *cctx)
3189{
3190 int ret;
3191
3192 ++*arg;
3193 if (**arg == NUL)
3194 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003195 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 return FAIL;
3197 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003198 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 {
3200 emsg_invreg(**arg);
3201 return FAIL;
3202 }
3203 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3204 ++*arg;
3205 return ret;
3206}
3207
3208/*
3209 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003210 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 */
3212 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003213apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003215 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 // this works from end to start
3218 while (p > start)
3219 {
3220 --p;
3221 if (*p == '-' || *p == '+')
3222 {
3223 // only '-' has an effect, for '+' we only check the type
3224#ifdef FEAT_FLOAT
3225 if (rettv->v_type == VAR_FLOAT)
3226 {
3227 if (*p == '-')
3228 rettv->vval.v_float = -rettv->vval.v_float;
3229 }
3230 else
3231#endif
3232 {
3233 varnumber_T val;
3234 int error = FALSE;
3235
3236 // tv_get_number_chk() accepts a string, but we don't want that
3237 // here
3238 if (check_not_string(rettv) == FAIL)
3239 return FAIL;
3240 val = tv_get_number_chk(rettv, &error);
3241 clear_tv(rettv);
3242 if (error)
3243 return FAIL;
3244 if (*p == '-')
3245 val = -val;
3246 rettv->v_type = VAR_NUMBER;
3247 rettv->vval.v_number = val;
3248 }
3249 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003250 else if (numeric_only)
3251 {
3252 ++p;
3253 break;
3254 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003255 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
3257 int v = tv2bool(rettv);
3258
3259 // '!' is permissive in the type.
3260 clear_tv(rettv);
3261 rettv->v_type = VAR_BOOL;
3262 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3263 }
3264 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003265 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 return OK;
3267}
3268
3269/*
3270 * Recognize v: variables that are constants and set "rettv".
3271 */
3272 static void
3273get_vim_constant(char_u **arg, typval_T *rettv)
3274{
3275 if (STRNCMP(*arg, "v:true", 6) == 0)
3276 {
3277 rettv->v_type = VAR_BOOL;
3278 rettv->vval.v_number = VVAL_TRUE;
3279 *arg += 6;
3280 }
3281 else if (STRNCMP(*arg, "v:false", 7) == 0)
3282 {
3283 rettv->v_type = VAR_BOOL;
3284 rettv->vval.v_number = VVAL_FALSE;
3285 *arg += 7;
3286 }
3287 else if (STRNCMP(*arg, "v:null", 6) == 0)
3288 {
3289 rettv->v_type = VAR_SPECIAL;
3290 rettv->vval.v_number = VVAL_NULL;
3291 *arg += 6;
3292 }
3293 else if (STRNCMP(*arg, "v:none", 6) == 0)
3294 {
3295 rettv->v_type = VAR_SPECIAL;
3296 rettv->vval.v_number = VVAL_NONE;
3297 *arg += 6;
3298 }
3299}
3300
Bram Moolenaar696ba232020-07-29 21:20:41 +02003301 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003302get_compare_type(char_u *p, int *len, int *type_is)
3303{
3304 exptype_T type = EXPR_UNKNOWN;
3305 int i;
3306
3307 switch (p[0])
3308 {
3309 case '=': if (p[1] == '=')
3310 type = EXPR_EQUAL;
3311 else if (p[1] == '~')
3312 type = EXPR_MATCH;
3313 break;
3314 case '!': if (p[1] == '=')
3315 type = EXPR_NEQUAL;
3316 else if (p[1] == '~')
3317 type = EXPR_NOMATCH;
3318 break;
3319 case '>': if (p[1] != '=')
3320 {
3321 type = EXPR_GREATER;
3322 *len = 1;
3323 }
3324 else
3325 type = EXPR_GEQUAL;
3326 break;
3327 case '<': if (p[1] != '=')
3328 {
3329 type = EXPR_SMALLER;
3330 *len = 1;
3331 }
3332 else
3333 type = EXPR_SEQUAL;
3334 break;
3335 case 'i': if (p[1] == 's')
3336 {
3337 // "is" and "isnot"; but not a prefix of a name
3338 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3339 *len = 5;
3340 i = p[*len];
3341 if (!isalnum(i) && i != '_')
3342 {
3343 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3344 *type_is = TRUE;
3345 }
3346 }
3347 break;
3348 }
3349 return type;
3350}
3351
3352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 */
3356 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003357compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003359 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361 // this works from end to start
3362 while (p > start)
3363 {
3364 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003365 while (VIM_ISWHITE(*p))
3366 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 if (*p == '-' || *p == '+')
3368 {
3369 int negate = *p == '-';
3370 isn_T *isn;
3371
3372 // TODO: check type
3373 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3374 {
3375 --p;
3376 if (*p == '-')
3377 negate = !negate;
3378 }
3379 // only '-' has an effect, for '+' we only check the type
3380 if (negate)
3381 isn = generate_instr(cctx, ISN_NEGATENR);
3382 else
3383 isn = generate_instr(cctx, ISN_CHECKNR);
3384 if (isn == NULL)
3385 return FAIL;
3386 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003387 else if (numeric_only)
3388 {
3389 ++p;
3390 break;
3391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 else
3393 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003394 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003396 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003398 if (p[-1] == '!')
3399 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 }
3402 if (generate_2BOOL(cctx, invert) == FAIL)
3403 return FAIL;
3404 }
3405 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003406 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 return OK;
3408}
3409
3410/*
3411 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003412 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 */
3414 static int
3415compile_subscript(
3416 char_u **arg,
3417 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003418 char_u *start_leader,
3419 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003420 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003422 char_u *name_start = *end_leader;
3423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 for (;;)
3425 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003426 char_u *p = skipwhite(*arg);
3427
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003428 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003429 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003430 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003431
3432 // If a following line starts with "->{" or "->X" advance to that
3433 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003434 // Also if a following line starts with ".x".
3435 if (next != NULL &&
3436 ((next[0] == '-' && next[1] == '>'
3437 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003438 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003439 {
3440 next = next_line_from_context(cctx, TRUE);
3441 if (next == NULL)
3442 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003443 *arg = next;
3444 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003445 }
3446 }
3447
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003448 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3449 // not a function call.
3450 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003452 garray_T *stack = &cctx->ctx_type_stack;
3453 type_T *type;
3454 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003456 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003457 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003458 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003461 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3462
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003463 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3465 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003466 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 return FAIL;
3468 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003469 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003471 char_u *pstart = p;
3472
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003473 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003474 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003475 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 // something->method()
3478 // Apply the '!', '-' and '+' first:
3479 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003480 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003483 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003484 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003485 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 if (**arg == '{')
3487 {
3488 // lambda call: list->{lambda}
3489 if (compile_lambda_call(arg, cctx) == FAIL)
3490 return FAIL;
3491 }
3492 else
3493 {
3494 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003495 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003496 if (!eval_isnamec1(*p))
3497 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003498 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003499 return FAIL;
3500 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003501 if (ASCII_ISALPHA(*p) && p[1] == ':')
3502 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003503 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 ;
3505 if (*p != '(')
3506 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003507 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 return FAIL;
3509 }
3510 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003511 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 return FAIL;
3513 }
3514 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003515 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003517 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003518 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003519 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003520 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003521 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003522
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003523 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003524 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003525 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003526 // TODO: blob index
3527 // TODO: more arguments
3528 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003529 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003530 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003531 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003532
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003533 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003534 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003535 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003536 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003537 if (**arg == ':')
3538 // missing first index is equal to zero
3539 generate_PUSHNR(cctx, 0);
3540 else
3541 {
3542 if (compile_expr0(arg, cctx) == FAIL)
3543 return FAIL;
3544 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3545 return FAIL;
3546 *arg = skipwhite(*arg);
3547 }
3548 if (**arg == ':')
3549 {
3550 *arg = skipwhite(*arg + 1);
3551 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3552 return FAIL;
3553 if (**arg == ']')
3554 // missing second index is equal to end of string
3555 generate_PUSHNR(cctx, -1);
3556 else
3557 {
3558 if (compile_expr0(arg, cctx) == FAIL)
3559 return FAIL;
3560 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3561 return FAIL;
3562 *arg = skipwhite(*arg);
3563 }
3564 is_slice = TRUE;
3565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566
3567 if (**arg != ']')
3568 {
3569 emsg(_(e_missbrac));
3570 return FAIL;
3571 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003572 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003574 // We can index a list and a dict. If we don't know the type
3575 // we can use the index value type.
3576 // TODO: If we don't know use an instruction to figure it out at
3577 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003578 typep = ((type_T **)stack->ga_data) + stack->ga_len
3579 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003580 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003581 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3582 // If the index is a string, the variable must be a Dict.
3583 if (*typep == &t_any && valtype == &t_string)
3584 vtype = VAR_DICT;
3585 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003586 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003587 if (need_type(valtype, &t_number, -1, cctx,
3588 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003589 return FAIL;
3590 if (is_slice)
3591 {
3592 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003593 if (need_type(valtype, &t_number, -2, cctx,
3594 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003595 return FAIL;
3596 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003597 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003598
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003599 if (vtype == VAR_DICT)
3600 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003601 if (is_slice)
3602 {
3603 emsg(_(e_cannot_slice_dictionary));
3604 return FAIL;
3605 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003606 if ((*typep)->tt_type == VAR_DICT)
3607 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003608 else
3609 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003610 if (need_type(*typep, &t_dict_any, -2, cctx,
3611 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003612 return FAIL;
3613 *typep = &t_any;
3614 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003615 if (may_generate_2STRING(-1, cctx) == FAIL)
3616 return FAIL;
3617 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3618 return FAIL;
3619 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003620 else if (vtype == VAR_STRING)
3621 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003622 *typep = &t_string;
3623 if ((is_slice
3624 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3625 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003626 return FAIL;
3627 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003628 else if (vtype == VAR_BLOB)
3629 {
3630 emsg("Sorry, blob index and slice not implemented yet");
3631 return FAIL;
3632 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003633 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003634 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003635 if (is_slice)
3636 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003637 if (generate_instr_drop(cctx,
3638 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3639 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003640 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003641 }
Bram Moolenaared591872020-08-15 22:14:53 +02003642 else
3643 {
3644 if ((*typep)->tt_type == VAR_LIST)
3645 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003646 if (generate_instr_drop(cctx,
3647 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3648 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003649 return FAIL;
3650 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003651 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003652 else
3653 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003654 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003655 return FAIL;
3656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003658 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003660 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003661 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003662 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003663 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003664
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003665 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003666 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003667 {
3668 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003669 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003670 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003671 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003672 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 while (eval_isnamec(*p))
3674 MB_PTR_ADV(p);
3675 if (p == *arg)
3676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003677 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 return FAIL;
3679 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003680 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 return FAIL;
3682 *arg = p;
3683 }
3684 else
3685 break;
3686 }
3687
3688 // TODO - see handle_subscript():
3689 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3690 // Don't do this when "Func" is already a partial that was bound
3691 // explicitly (pt_auto is FALSE).
3692
3693 return OK;
3694}
3695
3696/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003697 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3698 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003700 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003701 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003702 *
3703 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 */
3705
3706/*
3707 * number number constant
3708 * 0zFFFFFFFF Blob constant
3709 * "string" string constant
3710 * 'string' literal string constant
3711 * &option-name option value
3712 * @r register contents
3713 * identifier variable value
3714 * function() function call
3715 * $VAR environment variable
3716 * (expression) nested expression
3717 * [expr, expr] List
3718 * {key: val, key: val} Dictionary
3719 * #{key: val, key: val} Dictionary with literal keys
3720 *
3721 * Also handle:
3722 * ! in front logical NOT
3723 * - in front unary minus
3724 * + in front unary plus (ignored)
3725 * trailing (arg) funcref/partial call
3726 * trailing [] subscript in String or List
3727 * trailing .name entry in Dictionary
3728 * trailing ->name() method call
3729 */
3730 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731compile_expr7(
3732 char_u **arg,
3733 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003734 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 char_u *start_leader, *end_leader;
3737 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003738 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003739 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003741 ppconst->pp_is_const = FALSE;
3742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 /*
3744 * Skip '!', '-' and '+' characters. They are handled later.
3745 */
3746 start_leader = *arg;
3747 while (**arg == '!' || **arg == '-' || **arg == '+')
3748 *arg = skipwhite(*arg + 1);
3749 end_leader = *arg;
3750
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003751 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 switch (**arg)
3753 {
3754 /*
3755 * Number constant.
3756 */
3757 case '0': // also for blob starting with 0z
3758 case '1':
3759 case '2':
3760 case '3':
3761 case '4':
3762 case '5':
3763 case '6':
3764 case '7':
3765 case '8':
3766 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003767 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003769 // Apply "-" and "+" just before the number now, right to
3770 // left. Matters especially when "->" follows. Stops at
3771 // '!'.
3772 if (apply_leader(rettv, TRUE,
3773 start_leader, &end_leader) == FAIL)
3774 {
3775 clear_tv(rettv);
3776 return FAIL;
3777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 break;
3779
3780 /*
3781 * String constant: "string".
3782 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003783 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 return FAIL;
3785 break;
3786
3787 /*
3788 * Literal string constant: 'str''ing'.
3789 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003790 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792 break;
3793
3794 /*
3795 * Constant Vim variable.
3796 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003797 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 ret = NOTDONE;
3799 break;
3800
3801 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003802 * "true" constant
3803 */
3804 case 't': if (STRNCMP(*arg, "true", 4) == 0
3805 && !eval_isnamec((*arg)[4]))
3806 {
3807 *arg += 4;
3808 rettv->v_type = VAR_BOOL;
3809 rettv->vval.v_number = VVAL_TRUE;
3810 }
3811 else
3812 ret = NOTDONE;
3813 break;
3814
3815 /*
3816 * "false" constant
3817 */
3818 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3819 && !eval_isnamec((*arg)[5]))
3820 {
3821 *arg += 5;
3822 rettv->v_type = VAR_BOOL;
3823 rettv->vval.v_number = VVAL_FALSE;
3824 }
3825 else
3826 ret = NOTDONE;
3827 break;
3828
3829 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 * List: [expr, expr]
3831 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003832 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 break;
3834
3835 /*
3836 * Dictionary: #{key: val, key: val}
3837 */
3838 case '#': if ((*arg)[1] == '{')
3839 {
3840 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003841 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 }
3843 else
3844 ret = NOTDONE;
3845 break;
3846
3847 /*
3848 * Lambda: {arg, arg -> expr}
3849 * Dictionary: {'key': val, 'key': val}
3850 */
3851 case '{': {
3852 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003853 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
3855 // Find out what comes after the arguments.
3856 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003857 &ga_arg, TRUE, NULL, NULL,
3858 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 if (ret != FAIL && *start == '>')
3860 ret = compile_lambda(arg, cctx);
3861 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003862 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 }
3864 break;
3865
3866 /*
3867 * Option value: &name
3868 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003869 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3870 return FAIL;
3871 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 break;
3873
3874 /*
3875 * Environment variable: $VAR.
3876 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003877 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3878 return FAIL;
3879 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 break;
3881
3882 /*
3883 * Register contents: @r.
3884 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003885 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3886 return FAIL;
3887 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 break;
3889 /*
3890 * nested expression: (expression).
3891 */
3892 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003893
3894 // recursive!
3895 if (ppconst->pp_used <= PPSIZE - 10)
3896 {
3897 ret = compile_expr1(arg, cctx, ppconst);
3898 }
3899 else
3900 {
3901 // Not enough space in ppconst, flush constants.
3902 if (generate_ppconst(cctx, ppconst) == FAIL)
3903 return FAIL;
3904 ret = compile_expr0(arg, cctx);
3905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 *arg = skipwhite(*arg);
3907 if (**arg == ')')
3908 ++*arg;
3909 else if (ret == OK)
3910 {
3911 emsg(_(e_missing_close));
3912 ret = FAIL;
3913 }
3914 break;
3915
3916 default: ret = NOTDONE;
3917 break;
3918 }
3919 if (ret == FAIL)
3920 return FAIL;
3921
Bram Moolenaar1c747212020-05-09 18:28:34 +02003922 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003924 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 clear_tv(rettv);
3926 else
3927 // A constant expression can possibly be handled compile time,
3928 // return the value instead of generating code.
3929 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 }
3931 else if (ret == NOTDONE)
3932 {
3933 char_u *p;
3934 int r;
3935
3936 if (!eval_isnamec1(**arg))
3937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003938 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 return FAIL;
3940 }
3941
3942 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003943 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 {
3946 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003949 {
3950 if (generate_ppconst(cctx, ppconst) == FAIL)
3951 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003952 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 if (r == FAIL)
3955 return FAIL;
3956 }
3957
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003958 // Handle following "[]", ".member", etc.
3959 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003960 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 ppconst) == FAIL)
3962 return FAIL;
3963 if (ppconst->pp_used > 0)
3964 {
3965 // apply the '!', '-' and '+' before the constant
3966 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003967 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003968 return FAIL;
3969 return OK;
3970 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003971 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003973 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974}
3975
3976/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003977 * Give the "white on both sides" error, taking the operator from "p[len]".
3978 */
3979 void
3980error_white_both(char_u *op, int len)
3981{
3982 char_u buf[10];
3983
3984 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003985 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003986}
3987
3988/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003989 * <type>expr7: runtime type check / conversion
3990 */
3991 static int
3992compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3993{
3994 type_T *want_type = NULL;
3995
3996 // Recognize <type>
3997 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3998 {
3999 int called_emsg_before = called_emsg;
4000
4001 ++*arg;
4002 want_type = parse_type(arg, cctx->ctx_type_list);
4003 if (called_emsg != called_emsg_before)
4004 return FAIL;
4005
4006 if (**arg != '>')
4007 {
4008 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004009 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004010 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004011 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004012 return FAIL;
4013 }
4014 ++*arg;
4015 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4016 return FAIL;
4017 }
4018
4019 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4020 return FAIL;
4021
4022 if (want_type != NULL)
4023 {
4024 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004025 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004026
Bram Moolenaard1103582020-08-14 22:44:25 +02004027 generate_ppconst(cctx, ppconst);
4028 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004029 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004030 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004031 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004032 return FAIL;
4033 }
4034 }
4035
4036 return OK;
4037}
4038
4039/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 * * number multiplication
4041 * / number division
4042 * % number modulo
4043 */
4044 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004045compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046{
4047 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004048 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004049 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004051 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004052 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 return FAIL;
4054
4055 /*
4056 * Repeat computing, until no "*", "/" or "%" is following.
4057 */
4058 for (;;)
4059 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004060 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 if (*op != '*' && *op != '/' && *op != '%')
4062 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004063 if (next != NULL)
4064 {
4065 *arg = next_line_from_context(cctx, TRUE);
4066 op = skipwhite(*arg);
4067 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004068
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004069 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004071 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004072 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
4074 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004075 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004078 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004079 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004081
4082 if (ppconst->pp_used == ppconst_used + 2
4083 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4084 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004086 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4087 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004090 // both are numbers: compute the result
4091 switch (*op)
4092 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004095 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004096 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004097 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 break;
4099 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004100 tv1->vval.v_number = res;
4101 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 }
4103 else
4104 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 generate_two_op(cctx, op);
4107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
4109
4110 return OK;
4111}
4112
4113/*
4114 * + number addition
4115 * - number subtraction
4116 * .. string concatenation
4117 */
4118 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004119compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120{
4121 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004122 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125
4126 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 return FAIL;
4129
4130 /*
4131 * Repeat computing, until no "+", "-" or ".." is following.
4132 */
4133 for (;;)
4134 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004135 op = may_peek_next_line(cctx, *arg, &next);
4136 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 break;
4138 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 if (next != NULL)
4140 {
4141 *arg = next_line_from_context(cctx, TRUE);
4142 op = skipwhite(*arg);
4143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004145 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004147 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004148 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 }
4150
4151 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004152 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004153 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004155 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 return FAIL;
4158
Bram Moolenaara5565e42020-05-09 15:44:01 +02004159 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004160 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004161 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4162 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4163 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4164 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4167 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004168
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004169 // concat/subtract/add constant numbers
4170 if (*op == '+')
4171 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4172 else if (*op == '-')
4173 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4174 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004175 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004176 // concatenate constant strings
4177 char_u *s1 = tv1->vval.v_string;
4178 char_u *s2 = tv2->vval.v_string;
4179 size_t len1 = STRLEN(s1);
4180
4181 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4182 if (tv1->vval.v_string == NULL)
4183 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004185 return FAIL;
4186 }
4187 mch_memmove(tv1->vval.v_string, s1, len1);
4188 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 vim_free(s1);
4190 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004191 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004196 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004197 if (*op == '.')
4198 {
4199 if (may_generate_2STRING(-2, cctx) == FAIL
4200 || may_generate_2STRING(-1, cctx) == FAIL)
4201 return FAIL;
4202 generate_instr_drop(cctx, ISN_CONCAT, 1);
4203 }
4204 else
4205 generate_two_op(cctx, op);
4206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * expr5a == expr5b
4214 * expr5a =~ expr5b
4215 * expr5a != expr5b
4216 * expr5a !~ expr5b
4217 * expr5a > expr5b
4218 * expr5a >= expr5b
4219 * expr5a < expr5b
4220 * expr5a <= expr5b
4221 * expr5a is expr5b
4222 * expr5a isnot expr5b
4223 *
4224 * Produces instructions:
4225 * EVAL expr5a Push result of "expr5a"
4226 * EVAL expr5b Push result of "expr5b"
4227 * COMPARE one of the compare instructions
4228 */
4229 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
4232 exptype_T type = EXPR_UNKNOWN;
4233 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004234 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238
4239 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return FAIL;
4242
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004243 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004244 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
4246 /*
4247 * If there is a comparative operator, use it.
4248 */
4249 if (type != EXPR_UNKNOWN)
4250 {
4251 int ic = FALSE; // Default: do not ignore case
4252
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004253 if (next != NULL)
4254 {
4255 *arg = next_line_from_context(cctx, TRUE);
4256 p = skipwhite(*arg);
4257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 if (type_is && (p[len] == '?' || p[len] == '#'))
4259 {
4260 semsg(_(e_invexpr2), *arg);
4261 return FAIL;
4262 }
4263 // extra question mark appended: ignore case
4264 if (p[len] == '?')
4265 {
4266 ic = TRUE;
4267 ++len;
4268 }
4269 // extra '#' appended: match case (ignored)
4270 else if (p[len] == '#')
4271 ++len;
4272 // nothing appended: match case
4273
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004274 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004276 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004277 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
4279
4280 // get the second variable
4281 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004282 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004283 return FAIL;
4284
Bram Moolenaara5565e42020-05-09 15:44:01 +02004285 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 return FAIL;
4287
Bram Moolenaara5565e42020-05-09 15:44:01 +02004288 if (ppconst->pp_used == ppconst_used + 2)
4289 {
4290 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4291 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4292 int ret;
4293
4294 // Both sides are a constant, compute the result now.
4295 // First check for a valid combination of types, this is more
4296 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004297 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298 ret = FAIL;
4299 else
4300 {
4301 ret = typval_compare(tv1, tv2, type, ic);
4302 tv1->v_type = VAR_BOOL;
4303 tv1->vval.v_number = tv1->vval.v_number
4304 ? VVAL_TRUE : VVAL_FALSE;
4305 clear_tv(tv2);
4306 --ppconst->pp_used;
4307 }
4308 return ret;
4309 }
4310
4311 generate_ppconst(cctx, ppconst);
4312 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
4314
4315 return OK;
4316}
4317
Bram Moolenaar7f141552020-05-09 17:35:53 +02004318static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320/*
4321 * Compile || or &&.
4322 */
4323 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004324compile_and_or(
4325 char_u **arg,
4326 cctx_T *cctx,
4327 char *op,
4328 ppconst_T *ppconst,
4329 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004331 char_u *next;
4332 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 int opchar = *op;
4334
4335 if (p[0] == opchar && p[1] == opchar)
4336 {
4337 garray_T *instr = &cctx->ctx_instr;
4338 garray_T end_ga;
4339
4340 /*
4341 * Repeat until there is no following "||" or "&&"
4342 */
4343 ga_init2(&end_ga, sizeof(int), 10);
4344 while (p[0] == opchar && p[1] == opchar)
4345 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004346 if (next != NULL)
4347 {
4348 *arg = next_line_from_context(cctx, TRUE);
4349 p = skipwhite(*arg);
4350 }
4351
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004352 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4353 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004354 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 return FAIL;
4356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004358 // TODO: use ppconst if the value is a constant and check
4359 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360 generate_ppconst(cctx, ppconst);
4361
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004362 // Every part must evaluate to a bool.
4363 if (bool_on_stack(cctx) == FAIL)
4364 {
4365 ga_clear(&end_ga);
4366 return FAIL;
4367 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 if (ga_grow(&end_ga, 1) == FAIL)
4370 {
4371 ga_clear(&end_ga);
4372 return FAIL;
4373 }
4374 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4375 ++end_ga.ga_len;
4376 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004377 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378
4379 // eval the next expression
4380 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004381 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004382 return FAIL;
4383
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4385 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 {
4387 ga_clear(&end_ga);
4388 return FAIL;
4389 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004390
4391 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004395 // Every part must evaluate to a bool.
4396 if (bool_on_stack(cctx) == FAIL)
4397 {
4398 ga_clear(&end_ga);
4399 return FAIL;
4400 }
4401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 // Fill in the end label in all jumps.
4403 while (end_ga.ga_len > 0)
4404 {
4405 isn_T *isn;
4406
4407 --end_ga.ga_len;
4408 isn = ((isn_T *)instr->ga_data)
4409 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4410 isn->isn_arg.jump.jump_where = instr->ga_len;
4411 }
4412 ga_clear(&end_ga);
4413 }
4414
4415 return OK;
4416}
4417
4418/*
4419 * expr4a && expr4a && expr4a logical AND
4420 *
4421 * Produces instructions:
4422 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004423 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004426 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 * EVAL expr4c Push result of "expr4c"
4428 * end:
4429 */
4430 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 int ppconst_used = ppconst->pp_used;
4434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 return FAIL;
4438
4439 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441}
4442
4443/*
4444 * expr3a || expr3b || expr3c logical OR
4445 *
4446 * Produces instructions:
4447 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004448 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004449 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004451 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 * EVAL expr3c Push result of "expr3c"
4453 * end:
4454 */
4455 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 int ppconst_used = ppconst->pp_used;
4459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 return FAIL;
4463
4464 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004465 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466}
4467
4468/*
4469 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004471 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 * JUMP_IF_FALSE alt jump if false
4473 * EVAL expr1a
4474 * JUMP_ALWAYS end
4475 * alt: EVAL expr1b
4476 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004477 *
4478 * Toplevel expression: expr2 ?? expr1
4479 * Produces instructions:
4480 * EVAL expr2 Push result of "expr2"
4481 * JUMP_AND_KEEP_IF_TRUE end jump if true
4482 * EVAL expr1
4483 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 */
4485 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487{
4488 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004489 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004490 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaar3988f642020-08-27 22:43:03 +02004492 // Ignore all kinds of errors when not producing code.
4493 if (cctx->ctx_skip == SKIP_YES)
4494 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004495 evalarg_T evalarg;
4496
4497 CLEAR_FIELD(evalarg);
4498 evalarg.eval_cctx = cctx;
4499 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004500 return OK;
4501 }
4502
Bram Moolenaar61a89812020-05-07 16:58:17 +02004503 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 return FAIL;
4506
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004507 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 if (*p == '?')
4509 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004510 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 garray_T *instr = &cctx->ctx_instr;
4512 garray_T *stack = &cctx->ctx_type_stack;
4513 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004514 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004516 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004517 int has_const_expr = FALSE;
4518 int const_value = FALSE;
4519 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004521 if (next != NULL)
4522 {
4523 *arg = next_line_from_context(cctx, TRUE);
4524 p = skipwhite(*arg);
4525 }
4526
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004527 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004528 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004529 semsg(_(e_white_space_required_before_and_after_str),
4530 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004531 return FAIL;
4532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 if (ppconst->pp_used == ppconst_used + 1)
4535 {
4536 // the condition is a constant, we know whether the ? or the :
4537 // expression is to be evaluated.
4538 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004539 if (op_falsy)
4540 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4541 else
4542 {
4543 int error = FALSE;
4544
4545 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4546 &error);
4547 if (error)
4548 return FAIL;
4549 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004550 cctx->ctx_skip = save_skip == SKIP_YES ||
4551 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4552
4553 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4554 // "left ?? right" and "left" is truthy: produce "left"
4555 generate_ppconst(cctx, ppconst);
4556 else
4557 {
4558 clear_tv(&ppconst->pp_tv[ppconst_used]);
4559 --ppconst->pp_used;
4560 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 }
4562 else
4563 {
4564 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004565 if (op_falsy)
4566 end_idx = instr->ga_len;
4567 generate_JUMP(cctx, op_falsy
4568 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4569 if (op_falsy)
4570 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572
4573 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004574 *arg = skipwhite(p + 1 + op_falsy);
4575 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004576 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004578 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 if (!has_const_expr)
4581 {
4582 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004584 if (!op_falsy)
4585 {
4586 // remember the type and drop it
4587 --stack->ga_len;
4588 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004590 end_idx = instr->ga_len;
4591 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004593 // jump here from JUMP_IF_FALSE
4594 isn = ((isn_T *)instr->ga_data) + alt_idx;
4595 isn->isn_arg.jump.jump_where = instr->ga_len;
4596 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004599 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004601 // Check for the ":".
4602 p = may_peek_next_line(cctx, *arg, &next);
4603 if (*p != ':')
4604 {
4605 emsg(_(e_missing_colon));
4606 return FAIL;
4607 }
4608 if (next != NULL)
4609 {
4610 *arg = next_line_from_context(cctx, TRUE);
4611 p = skipwhite(*arg);
4612 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004613
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004614 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4615 {
4616 semsg(_(e_white_space_required_before_and_after_str), ":");
4617 return FAIL;
4618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004620 // evaluate the third expression
4621 if (has_const_expr)
4622 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004623 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004624 *arg = skipwhite(p + 1);
4625 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4626 return FAIL;
4627 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4628 return FAIL;
4629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 if (!has_const_expr)
4632 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004633 type_T **typep;
4634
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004638 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4639 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004641 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642 isn = ((isn_T *)instr->ga_data) + end_idx;
4643 isn->isn_arg.jump.jump_where = instr->ga_len;
4644 }
4645
4646 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
4648 return OK;
4649}
4650
4651/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004653 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4654 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 */
4656 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004657compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658{
4659 ppconst_T ppconst;
4660
4661 CLEAR_FIELD(ppconst);
4662 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4663 {
4664 clear_ppconst(&ppconst);
4665 return FAIL;
4666 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004667 if (is_const != NULL)
4668 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 if (generate_ppconst(cctx, &ppconst) == FAIL)
4670 return FAIL;
4671 return OK;
4672}
4673
4674/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004675 * Toplevel expression.
4676 */
4677 static int
4678compile_expr0(char_u **arg, cctx_T *cctx)
4679{
4680 return compile_expr0_ext(arg, cctx, NULL);
4681}
4682
4683/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 * compile "return [expr]"
4685 */
4686 static char_u *
4687compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4688{
4689 char_u *p = arg;
4690 garray_T *stack = &cctx->ctx_type_stack;
4691 type_T *stack_type;
4692
4693 if (*p != NUL && *p != '|' && *p != '\n')
4694 {
4695 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 return NULL;
4698
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004699 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004700 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004701 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4702 if (set_return_type)
4703 cctx->ctx_ufunc->uf_ret_type = stack_type;
4704 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004705 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004706 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4707 && stack_type->tt_type != VAR_VOID
4708 && stack_type->tt_type != VAR_UNKNOWN)
4709 {
4710 emsg(_(e_returning_value_in_function_without_return_type));
4711 return NULL;
4712 }
4713 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004714 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004715 return NULL;
4716 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 }
4719 else
4720 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004721 // "set_return_type" cannot be TRUE, only used for a lambda which
4722 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004723 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4724 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004726 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return NULL;
4728 }
4729
4730 // No argument, return zero.
4731 generate_PUSHNR(cctx, 0);
4732 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004733 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 return NULL;
4735
4736 // "return val | endif" is possible
4737 return skipwhite(p);
4738}
4739
4740/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004741 * Get a line from the compilation context, compatible with exarg_T getline().
4742 * Return a pointer to the line in allocated memory.
4743 * Return NULL for end-of-file or some error.
4744 */
4745 static char_u *
4746exarg_getline(
4747 int c UNUSED,
4748 void *cookie,
4749 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004750 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004751{
4752 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004753 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754
Bram Moolenaar66250c92020-08-20 15:02:42 +02004755 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004756 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004757 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004758 return NULL;
4759 ++cctx->ctx_lnum;
4760 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4761 // Comment lines result in NULL pointers, skip them.
4762 if (p != NULL)
4763 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004764 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004765}
4766
4767/*
4768 * Compile a nested :def command.
4769 */
4770 static char_u *
4771compile_nested_function(exarg_T *eap, cctx_T *cctx)
4772{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004773 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004774 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004775 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004776 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004778 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004779
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004780 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004781 {
4782 emsg(_(e_cannot_use_bang_with_nested_def));
4783 return NULL;
4784 }
4785
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004786 // Only g:Func() can use a namespace.
4787 if (name_start[1] == ':' && !is_global)
4788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004789 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004790 return NULL;
4791 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004792 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4793 return NULL;
4794
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 eap->arg = name_end;
4796 eap->getline = exarg_getline;
4797 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004798 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004799 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004800 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004801 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802
Bram Moolenaar822ba242020-05-24 23:00:18 +02004803 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004804 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004805 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004806 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004807 {
4808 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004809 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004810 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004811
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004812 if (is_global)
4813 {
4814 char_u *func_name = vim_strnsave(name_start + 2,
4815 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004816
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004817 if (func_name == NULL)
4818 r = FAIL;
4819 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004820 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004821 }
4822 else
4823 {
4824 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004825 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004826 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004827 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004828
Bram Moolenaareef21022020-08-01 22:16:43 +02004829 if (lvar == NULL)
4830 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004831 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004832 return NULL;
4833 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004834
4835 // copy over the block scope IDs
4836 if (block_depth > 0)
4837 {
4838 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4839 if (ufunc->uf_block_ids != NULL)
4840 {
4841 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4842 sizeof(int) * block_depth);
4843 ufunc->uf_block_depth = block_depth;
4844 }
4845 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004846 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004847
Bram Moolenaar61a89812020-05-07 16:58:17 +02004848 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004849 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004850}
4851
4852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 * Return the length of an assignment operator, or zero if there isn't one.
4854 */
4855 int
4856assignment_len(char_u *p, int *heredoc)
4857{
4858 if (*p == '=')
4859 {
4860 if (p[1] == '<' && p[2] == '<')
4861 {
4862 *heredoc = TRUE;
4863 return 3;
4864 }
4865 return 1;
4866 }
4867 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4868 return 2;
4869 if (STRNCMP(p, "..=", 3) == 0)
4870 return 3;
4871 return 0;
4872}
4873
4874// words that cannot be used as a variable
4875static char *reserved[] = {
4876 "true",
4877 "false",
4878 NULL
4879};
4880
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004881typedef enum {
4882 dest_local,
4883 dest_option,
4884 dest_env,
4885 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004886 dest_buffer,
4887 dest_window,
4888 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004889 dest_vimvar,
4890 dest_script,
4891 dest_reg,
4892} assign_dest_T;
4893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004895 * Generate the load instruction for "name".
4896 */
4897 static void
4898generate_loadvar(
4899 cctx_T *cctx,
4900 assign_dest_T dest,
4901 char_u *name,
4902 lvar_T *lvar,
4903 type_T *type)
4904{
4905 switch (dest)
4906 {
4907 case dest_option:
4908 // TODO: check the option exists
4909 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4910 break;
4911 case dest_global:
4912 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4913 break;
4914 case dest_buffer:
4915 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4916 break;
4917 case dest_window:
4918 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4919 break;
4920 case dest_tab:
4921 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4922 break;
4923 case dest_script:
4924 compile_load_scriptvar(cctx,
4925 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4926 break;
4927 case dest_env:
4928 // Include $ in the name here
4929 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4930 break;
4931 case dest_reg:
4932 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4933 break;
4934 case dest_vimvar:
4935 generate_LOADV(cctx, name + 2, TRUE);
4936 break;
4937 case dest_local:
4938 if (lvar->lv_from_outer)
4939 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4940 NULL, type);
4941 else
4942 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4943 break;
4944 }
4945}
4946
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004947 void
4948vim9_declare_error(char_u *name)
4949{
4950 char *scope = "";
4951
4952 switch (*name)
4953 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004954 case 'g': scope = _("global"); break;
4955 case 'b': scope = _("buffer"); break;
4956 case 'w': scope = _("window"); break;
4957 case 't': scope = _("tab"); break;
4958 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004959 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004960 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004961 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004962 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004963 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004964 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004965 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004966 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004967 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004968}
4969
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004970/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004972 * "let name"
4973 * "var name = expr"
4974 * "final name = expr"
4975 * "const name = expr"
4976 * "name = expr"
4977 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004978 * Return NULL for an error.
4979 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 */
4981 static char_u *
4982compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4983{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004986 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 char_u *ret = NULL;
4988 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004990 int scriptvar_sid = 0;
4991 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004994 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 int oplen = 0;
4997 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004998 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004999 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005002 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5003 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 // Skip over the "var" or "[var, var]" to get to any "=".
5006 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5007 if (p == NULL)
5008 return *arg == '[' ? arg : NULL;
5009
5010 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005012 // TODO: should we allow this, and figure out type inference from list
5013 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005014 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 return NULL;
5016 }
5017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 sp = p;
5019 p = skipwhite(p);
5020 op = p;
5021 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022
5023 if (var_count > 0 && oplen == 0)
5024 // can be something like "[1, 2]->func()"
5025 return arg;
5026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5028 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005029 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005031 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 if (heredoc)
5034 {
5035 list_T *l;
5036 listitem_T *li;
5037
5038 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005039 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005041 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005042 if (l == NULL)
5043 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044
Bram Moolenaar078269b2020-09-21 20:35:55 +02005045 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005047 // Push each line and the create the list.
5048 FOR_ALL_LIST_ITEMS(l, li)
5049 {
5050 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5051 li->li_tv.vval.v_string = NULL;
5052 }
5053 generate_NEWLIST(cctx, l->lv_len);
5054 type = &t_list_string;
5055 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 list_free(l);
5058 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005061 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 // for "[var, var] = expr" evaluate the expression here, loop over the
5064 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005065
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 p = skipwhite(op + oplen);
5067 if (compile_expr0(&p, cctx) == FAIL)
5068 return NULL;
5069 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005071 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005073 type_T *stacktype;
5074
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005075 stacktype = stack->ga_len == 0 ? &t_void
5076 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005079 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 goto theend;
5081 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005082 if (need_type(stacktype, &t_list_any, -1, cctx,
5083 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005085 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005086 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5087 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 }
5089 }
5090
5091 /*
5092 * Loop over variables in "[var, var] = expr".
5093 * For "var = expr" and "let var: type" this is done only once.
5094 */
5095 if (var_count > 0)
5096 var_start = skipwhite(arg + 1); // skip over the "["
5097 else
5098 var_start = arg;
5099 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5100 {
5101 char_u *var_end = skip_var_one(var_start, FALSE);
5102 size_t varlen;
5103 int new_local = FALSE;
5104 int opt_type;
5105 int opt_flags = 0;
5106 assign_dest_T dest = dest_local;
5107 int vimvaridx = -1;
5108 lvar_T *lvar = NULL;
5109 lvar_T arg_lvar;
5110 int has_type = FALSE;
5111 int has_index = FALSE;
5112 int instr_count = -1;
5113
Bram Moolenaar65821722020-08-02 18:58:54 +02005114 if (*var_start == '@')
5115 p = var_start + 2;
5116 else
5117 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005118 // skip over the leading "&", "&l:", "&g:" and "$"
5119 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005120 p = to_name_end(p, TRUE);
5121 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122
5123 // "a: type" is declaring variable "a" with a type, not "a:".
5124 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5125 --var_end;
5126 if (is_decl && p == var_start + 2 && p[-1] == ':')
5127 --p;
5128
5129 varlen = p - var_start;
5130 vim_free(name);
5131 name = vim_strnsave(var_start, varlen);
5132 if (name == NULL)
5133 return NULL;
5134 if (!heredoc)
5135 type = &t_any;
5136
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005137 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005139 int declare_error = FALSE;
5140
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 if (*var_start == '&')
5142 {
5143 int cc;
5144 long numval;
5145
5146 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005147 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 emsg(_(e_const_option));
5150 goto theend;
5151 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005152 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 p = var_start;
5154 p = find_option_end(&p, &opt_flags);
5155 if (p == NULL)
5156 {
5157 // cannot happen?
5158 emsg(_(e_letunexp));
5159 goto theend;
5160 }
5161 cc = *p;
5162 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005163 opt_type = get_option_value(skip_option_env_lead(var_start),
5164 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 *p = cc;
5166 if (opt_type == -3)
5167 {
5168 semsg(_(e_unknown_option), var_start);
5169 goto theend;
5170 }
5171 if (opt_type == -2 || opt_type == 0)
5172 type = &t_string;
5173 else
5174 type = &t_number; // both number and boolean option
5175 }
5176 else if (*var_start == '$')
5177 {
5178 dest = dest_env;
5179 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005180 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 }
5182 else if (*var_start == '@')
5183 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005184 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 {
5186 emsg_invreg(var_start[1]);
5187 goto theend;
5188 }
5189 dest = dest_reg;
5190 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005191 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005193 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 {
5195 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005196 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005198 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 {
5200 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005201 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005202 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005203 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 {
5205 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005206 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005208 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005209 {
5210 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005211 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005213 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 {
5215 typval_T *vtv;
5216 int di_flags;
5217
5218 vimvaridx = find_vim_var(name + 2, &di_flags);
5219 if (vimvaridx < 0)
5220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005221 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 goto theend;
5223 }
5224 // We use the current value of "sandbox" here, is that OK?
5225 if (var_check_ro(di_flags, name, FALSE))
5226 goto theend;
5227 dest = dest_vimvar;
5228 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005229 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005230 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231 }
5232 else
5233 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005234 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235
5236 for (idx = 0; reserved[idx] != NULL; ++idx)
5237 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005239 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005240 goto theend;
5241 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242
5243 lvar = lookup_local(var_start, varlen, cctx);
5244 if (lvar == NULL)
5245 {
5246 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005247 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5249 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005250 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 if (is_decl)
5252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005253 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 goto theend;
5255 }
5256 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005257 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005258 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 if (lvar != NULL)
5260 {
5261 if (is_decl)
5262 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005263 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 goto theend;
5265 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005267 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005268 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005269 int script_namespace = varlen > 1
5270 && STRNCMP(var_start, "s:", 2) == 0;
5271 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005272 ? script_var_exists(var_start + 2, varlen - 2,
5273 FALSE, cctx)
5274 : script_var_exists(var_start, varlen,
5275 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005276 imported_T *import =
5277 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005278
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005279 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005281 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5282
5283 if (is_decl)
5284 {
5285 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005286 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005287 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005288 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005289 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005290 name);
5291 goto theend;
5292 }
5293 else if (cctx->ctx_ufunc->uf_script_ctx_version
5294 == SCRIPT_VERSION_VIM9
5295 && script_namespace
5296 && !script_var && import == NULL)
5297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005298 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005299 goto theend;
5300 }
5301
5302 dest = dest_script;
5303
5304 // existing script-local variables should have a type
5305 scriptvar_sid = current_sctx.sc_sid;
5306 if (import != NULL)
5307 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005308 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005309 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005310 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005311 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005312 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005313 {
5314 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5315 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005316 ((svar_T *)si->sn_var_vals.ga_data)
5317 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005318 type = sv->sv_type;
5319 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005320 }
5321 }
5322 else if (name[1] == ':' && name[2] != NUL)
5323 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005324 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 goto theend;
5326 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005327 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005328 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005329 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005330 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005331 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005332 else if (check_defined(var_start, varlen, cctx) == FAIL)
5333 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005336
5337 if (declare_error)
5338 {
5339 vim9_declare_error(name);
5340 goto theend;
5341 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 }
5343
5344 // handle "a:name" as a name, not index "name" on "a"
5345 if (varlen > 1 || var_start[varlen] != ':')
5346 p = var_end;
5347
5348 if (dest != dest_option)
5349 {
5350 if (is_decl && *p == ':')
5351 {
5352 // parse optional type: "let var: type = expr"
5353 if (!VIM_ISWHITE(p[1]))
5354 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005355 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005356 goto theend;
5357 }
5358 p = skipwhite(p + 1);
5359 type = parse_type(&p, cctx->ctx_type_list);
5360 has_type = TRUE;
5361 }
5362 else if (lvar != NULL)
5363 type = lvar->lv_type;
5364 }
5365
5366 if (oplen == 3 && !heredoc && dest != dest_global
5367 && type->tt_type != VAR_STRING
5368 && type->tt_type != VAR_ANY)
5369 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005370 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 goto theend;
5372 }
5373
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005374 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375 {
5376 if (oplen > 1 && !heredoc)
5377 {
5378 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005379 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005380 goto theend;
5381 }
5382
5383 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005384 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5385 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 goto theend;
5387 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005388 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389 if (lvar == NULL)
5390 goto theend;
5391 new_local = TRUE;
5392 }
5393
5394 member_type = type;
5395 if (var_end > var_start + varlen)
5396 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005397 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 if (is_decl)
5399 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005400 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 goto theend;
5402 }
5403
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005404 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405 {
5406 has_index = TRUE;
5407 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005408 member_type = &t_any;
5409 else
5410 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411 }
5412 else
5413 {
5414 semsg("Not supported yet: %s", var_start);
5415 goto theend;
5416 }
5417 }
5418 else if (lvar == &arg_lvar)
5419 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005420 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421 goto theend;
5422 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005423 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5424 {
5425 semsg(_(e_cannot_assign_to_constant), name);
5426 goto theend;
5427 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428
5429 if (!heredoc)
5430 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005431 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005433 if (oplen > 0 && var_count == 0)
5434 {
5435 // skip over the "=" and the expression
5436 p = skipwhite(op + oplen);
5437 compile_expr0(&p, cctx);
5438 }
5439 }
5440 else if (oplen > 0)
5441 {
5442 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005443 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005444
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005445 // For "var = expr" evaluate the expression.
5446 if (var_count == 0)
5447 {
5448 int r;
5449
5450 // for "+=", "*=", "..=" etc. first load the current value
5451 if (*op != '=')
5452 {
5453 generate_loadvar(cctx, dest, name, lvar, type);
5454
5455 if (has_index)
5456 {
5457 // TODO: get member from list or dict
5458 emsg("Index with operation not supported yet");
5459 goto theend;
5460 }
5461 }
5462
5463 // Compile the expression. Temporarily hide the new local
5464 // variable here, it is not available to this expression.
5465 if (new_local)
5466 --cctx->ctx_locals.ga_len;
5467 instr_count = instr->ga_len;
5468 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005469 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 if (new_local)
5471 ++cctx->ctx_locals.ga_len;
5472 if (r == FAIL)
5473 goto theend;
5474 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005475 else if (semicolon && var_idx == var_count - 1)
5476 {
5477 // For "[var; var] = expr" get the rest of the list
5478 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5479 goto theend;
5480 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481 else
5482 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005483 // For "[var, var] = expr" get the "var_idx" item from the
5484 // list.
5485 if (generate_GETITEM(cctx, var_idx) == FAIL)
5486 return FAIL;
5487 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005488
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005489 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005490 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005491 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005492 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005493 if ((stacktype->tt_type == VAR_FUNC
5494 || stacktype->tt_type == VAR_PARTIAL)
5495 && var_wrong_func_name(name, TRUE))
5496 goto theend;
5497
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005499 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005500 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005502 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005503 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005504 }
5505 else
5506 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005507 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005508 // for a variable that implies &t_any.
5509 if (stacktype == &t_list_empty)
5510 lvar->lv_type = &t_list_any;
5511 else if (stacktype == &t_dict_empty)
5512 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005513 else if (stacktype == &t_unknown)
5514 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005515 else
5516 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005518 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005519 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005520 {
5521 type_T *use_type = lvar->lv_type;
5522
Bram Moolenaar71abe482020-09-14 22:28:30 +02005523 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005524 if (has_index)
5525 {
5526 use_type = use_type->tt_member;
5527 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005528 // could be indexing "any"
5529 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005530 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005531 if (need_type(stacktype, use_type, -1, cctx,
5532 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005533 goto theend;
5534 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005535 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005536 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005537 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005538 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005540 else if (cmdidx == CMD_final)
5541 {
5542 emsg(_(e_final_requires_a_value));
5543 goto theend;
5544 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005547 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 goto theend;
5549 }
5550 else if (!has_type || dest == dest_option)
5551 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005552 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 goto theend;
5554 }
5555 else
5556 {
5557 // variables are always initialized
5558 if (ga_grow(instr, 1) == FAIL)
5559 goto theend;
5560 switch (member_type->tt_type)
5561 {
5562 case VAR_BOOL:
5563 generate_PUSHBOOL(cctx, VVAL_FALSE);
5564 break;
5565 case VAR_FLOAT:
5566#ifdef FEAT_FLOAT
5567 generate_PUSHF(cctx, 0.0);
5568#endif
5569 break;
5570 case VAR_STRING:
5571 generate_PUSHS(cctx, NULL);
5572 break;
5573 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005574 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005575 break;
5576 case VAR_FUNC:
5577 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5578 break;
5579 case VAR_LIST:
5580 generate_NEWLIST(cctx, 0);
5581 break;
5582 case VAR_DICT:
5583 generate_NEWDICT(cctx, 0);
5584 break;
5585 case VAR_JOB:
5586 generate_PUSHJOB(cctx, NULL);
5587 break;
5588 case VAR_CHANNEL:
5589 generate_PUSHCHANNEL(cctx, NULL);
5590 break;
5591 case VAR_NUMBER:
5592 case VAR_UNKNOWN:
5593 case VAR_ANY:
5594 case VAR_PARTIAL:
5595 case VAR_VOID:
5596 case VAR_SPECIAL: // cannot happen
5597 generate_PUSHNR(cctx, 0);
5598 break;
5599 }
5600 }
5601 if (var_count == 0)
5602 end = p;
5603 }
5604
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005605 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005606 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005607 break;
5608
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005609 if (oplen > 0 && *op != '=')
5610 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005611 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 type_T *stacktype;
5613
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 if (*op == '.')
5615 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005616 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005617 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005619 if (
5620#ifdef FEAT_FLOAT
5621 // If variable is float operation with number is OK.
5622 !(expected == &t_float && stacktype == &t_number) &&
5623#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005624 need_type(stacktype, expected, -1, cctx,
5625 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626 goto theend;
5627
5628 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005629 {
5630 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5631 goto theend;
5632 }
5633 else if (*op == '+')
5634 {
5635 if (generate_add_instr(cctx,
5636 operator_type(member_type, stacktype),
5637 member_type, stacktype) == FAIL)
5638 goto theend;
5639 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005640 else if (generate_two_op(cctx, op) == FAIL)
5641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005645 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005646 int r;
5647
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005648 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005649 if (new_local)
5650 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005651 p = var_start + varlen;
5652 if (*p == '[')
5653 {
5654 p = skipwhite(p + 1);
5655 r = compile_expr0(&p, cctx);
5656 if (r == OK && *skipwhite(p) != ']')
5657 {
5658 // this should not happen
5659 emsg(_(e_missbrac));
5660 r = FAIL;
5661 }
5662 }
5663 else // if (*p == '.')
5664 {
5665 char_u *key_end = to_name_end(p + 1, TRUE);
5666 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5667
5668 r = generate_PUSHS(cctx, key);
5669 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670 if (new_local)
5671 ++cctx->ctx_locals.ga_len;
5672 if (r == FAIL)
5673 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005674
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005675 if (type == &t_any)
5676 {
5677 type_T *idx_type = ((type_T **)stack->ga_data)[
5678 stack->ga_len - 1];
5679 // Index on variable of unknown type: guess the type from the
5680 // index type: number is dict, otherwise dict.
5681 // TODO: should do the assignment at runtime
5682 if (idx_type->tt_type == VAR_NUMBER)
5683 type = &t_list_any;
5684 else
5685 type = &t_dict_any;
5686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005687 if (type->tt_type == VAR_DICT
5688 && may_generate_2STRING(-1, cctx) == FAIL)
5689 goto theend;
5690 if (type->tt_type == VAR_LIST
5691 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005692 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005693 {
5694 emsg(_(e_number_exp));
5695 goto theend;
5696 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005697
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005698 // Load the dict or list. On the stack we then have:
5699 // - value
5700 // - index
5701 // - variable
5702 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005703
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005704 if (type->tt_type == VAR_LIST)
5705 {
5706 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5707 return FAIL;
5708 }
5709 else if (type->tt_type == VAR_DICT)
5710 {
5711 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5712 return FAIL;
5713 }
5714 else
5715 {
5716 emsg(_(e_listreq));
5717 goto theend;
5718 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005719 }
5720 else
5721 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005722 if (is_decl && cmdidx == CMD_const
5723 && (dest == dest_script || dest == dest_local))
5724 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005725 generate_LOCKCONST(cctx);
5726
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005727 switch (dest)
5728 {
5729 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005730 generate_STOREOPT(cctx, skip_option_env_lead(name),
5731 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005732 break;
5733 case dest_global:
5734 // include g: with the name, easier to execute that way
5735 generate_STORE(cctx, ISN_STOREG, 0, name);
5736 break;
5737 case dest_buffer:
5738 // include b: with the name, easier to execute that way
5739 generate_STORE(cctx, ISN_STOREB, 0, name);
5740 break;
5741 case dest_window:
5742 // include w: with the name, easier to execute that way
5743 generate_STORE(cctx, ISN_STOREW, 0, name);
5744 break;
5745 case dest_tab:
5746 // include t: with the name, easier to execute that way
5747 generate_STORE(cctx, ISN_STORET, 0, name);
5748 break;
5749 case dest_env:
5750 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5751 break;
5752 case dest_reg:
5753 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5754 break;
5755 case dest_vimvar:
5756 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5757 break;
5758 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005759 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005760 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005761 {
5762 char_u *name_s = name;
5763
5764 // Include s: in the name for store_var()
5765 if (name[1] != ':')
5766 {
5767 int len = (int)STRLEN(name) + 3;
5768
5769 name_s = alloc(len);
5770 if (name_s == NULL)
5771 name_s = name;
5772 else
5773 vim_snprintf((char *)name_s, len,
5774 "s:%s", name);
5775 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005776 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5777 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005778 if (name_s != name)
5779 vim_free(name_s);
5780 }
5781 else
5782 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005783 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005784 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005785 break;
5786 case dest_local:
5787 if (lvar != NULL)
5788 {
5789 isn_T *isn = ((isn_T *)instr->ga_data)
5790 + instr->ga_len - 1;
5791
5792 // optimization: turn "var = 123" from ISN_PUSHNR +
5793 // ISN_STORE into ISN_STORENR
5794 if (!lvar->lv_from_outer
5795 && instr->ga_len == instr_count + 1
5796 && isn->isn_type == ISN_PUSHNR)
5797 {
5798 varnumber_T val = isn->isn_arg.number;
5799
5800 isn->isn_type = ISN_STORENR;
5801 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5802 isn->isn_arg.storenr.stnr_val = val;
5803 if (stack->ga_len > 0)
5804 --stack->ga_len;
5805 }
5806 else if (lvar->lv_from_outer)
5807 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005808 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005809 else
5810 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5811 }
5812 break;
5813 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005814 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005815
5816 if (var_idx + 1 < var_count)
5817 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005819
5820 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005821 if (var_count > 0 && !semicolon)
5822 {
5823 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5824 goto theend;
5825 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005826
Bram Moolenaarb2097502020-07-19 17:17:02 +02005827 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005828
5829theend:
5830 vim_free(name);
5831 return ret;
5832}
5833
5834/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005835 * Check if "name" can be "unlet".
5836 */
5837 int
5838check_vim9_unlet(char_u *name)
5839{
5840 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5841 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005842 // "unlet s:var" is allowed in legacy script.
5843 if (*name == 's' && !script_is_vim9())
5844 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005845 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005846 return FAIL;
5847 }
5848 return OK;
5849}
5850
5851/*
5852 * Callback passed to ex_unletlock().
5853 */
5854 static int
5855compile_unlet(
5856 lval_T *lvp,
5857 char_u *name_end,
5858 exarg_T *eap,
5859 int deep UNUSED,
5860 void *coookie)
5861{
5862 cctx_T *cctx = coookie;
5863
5864 if (lvp->ll_tv == NULL)
5865 {
5866 char_u *p = lvp->ll_name;
5867 int cc = *name_end;
5868 int ret = OK;
5869
5870 // Normal name. Only supports g:, w:, t: and b: namespaces.
5871 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005872 if (*p == '$')
5873 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5874 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005875 ret = FAIL;
5876 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005877 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005878
5879 *name_end = cc;
5880 return ret;
5881 }
5882
5883 // TODO: unlet {list}[idx]
5884 // TODO: unlet {dict}[key]
5885 emsg("Sorry, :unlet not fully implemented yet");
5886 return FAIL;
5887}
5888
5889/*
5890 * compile "unlet var", "lock var" and "unlock var"
5891 * "arg" points to "var".
5892 */
5893 static char_u *
5894compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5895{
5896 char_u *p = arg;
5897
5898 if (eap->cmdidx != CMD_unlet)
5899 {
5900 emsg("Sorry, :lock and unlock not implemented yet");
5901 return NULL;
5902 }
5903
5904 if (*p == '!')
5905 {
5906 p = skipwhite(p + 1);
5907 eap->forceit = TRUE;
5908 }
5909
5910 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5911 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5912}
5913
5914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 * Compile an :import command.
5916 */
5917 static char_u *
5918compile_import(char_u *arg, cctx_T *cctx)
5919{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005920 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921}
5922
5923/*
5924 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5925 */
5926 static int
5927compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5928{
5929 garray_T *instr = &cctx->ctx_instr;
5930 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5931
5932 if (endlabel == NULL)
5933 return FAIL;
5934 endlabel->el_next = *el;
5935 *el = endlabel;
5936 endlabel->el_end_label = instr->ga_len;
5937
5938 generate_JUMP(cctx, when, 0);
5939 return OK;
5940}
5941
5942 static void
5943compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5944{
5945 garray_T *instr = &cctx->ctx_instr;
5946
5947 while (*el != NULL)
5948 {
5949 endlabel_T *cur = (*el);
5950 isn_T *isn;
5951
5952 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5953 isn->isn_arg.jump.jump_where = instr->ga_len;
5954 *el = cur->el_next;
5955 vim_free(cur);
5956 }
5957}
5958
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005959 static void
5960compile_free_jump_to_end(endlabel_T **el)
5961{
5962 while (*el != NULL)
5963 {
5964 endlabel_T *cur = (*el);
5965
5966 *el = cur->el_next;
5967 vim_free(cur);
5968 }
5969}
5970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971/*
5972 * Create a new scope and set up the generic items.
5973 */
5974 static scope_T *
5975new_scope(cctx_T *cctx, scopetype_T type)
5976{
5977 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5978
5979 if (scope == NULL)
5980 return NULL;
5981 scope->se_outer = cctx->ctx_scope;
5982 cctx->ctx_scope = scope;
5983 scope->se_type = type;
5984 scope->se_local_count = cctx->ctx_locals.ga_len;
5985 return scope;
5986}
5987
5988/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005989 * Free the current scope and go back to the outer scope.
5990 */
5991 static void
5992drop_scope(cctx_T *cctx)
5993{
5994 scope_T *scope = cctx->ctx_scope;
5995
5996 if (scope == NULL)
5997 {
5998 iemsg("calling drop_scope() without a scope");
5999 return;
6000 }
6001 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006002 switch (scope->se_type)
6003 {
6004 case IF_SCOPE:
6005 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6006 case FOR_SCOPE:
6007 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6008 case WHILE_SCOPE:
6009 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6010 case TRY_SCOPE:
6011 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6012 case NO_SCOPE:
6013 case BLOCK_SCOPE:
6014 break;
6015 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006016 vim_free(scope);
6017}
6018
6019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 * compile "if expr"
6021 *
6022 * "if expr" Produces instructions:
6023 * EVAL expr Push result of "expr"
6024 * JUMP_IF_FALSE end
6025 * ... body ...
6026 * end:
6027 *
6028 * "if expr | else" Produces instructions:
6029 * EVAL expr Push result of "expr"
6030 * JUMP_IF_FALSE else
6031 * ... body ...
6032 * JUMP_ALWAYS end
6033 * else:
6034 * ... body ...
6035 * end:
6036 *
6037 * "if expr1 | elseif expr2 | else" Produces instructions:
6038 * EVAL expr Push result of "expr"
6039 * JUMP_IF_FALSE elseif
6040 * ... body ...
6041 * JUMP_ALWAYS end
6042 * elseif:
6043 * EVAL expr Push result of "expr"
6044 * JUMP_IF_FALSE else
6045 * ... body ...
6046 * JUMP_ALWAYS end
6047 * else:
6048 * ... body ...
6049 * end:
6050 */
6051 static char_u *
6052compile_if(char_u *arg, cctx_T *cctx)
6053{
6054 char_u *p = arg;
6055 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006056 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006058 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006059 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060
Bram Moolenaara5565e42020-05-09 15:44:01 +02006061 CLEAR_FIELD(ppconst);
6062 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006063 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006064 clear_ppconst(&ppconst);
6065 return NULL;
6066 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006067 if (cctx->ctx_skip == SKIP_YES)
6068 clear_ppconst(&ppconst);
6069 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006070 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006071 int error = FALSE;
6072 int v;
6073
Bram Moolenaara5565e42020-05-09 15:44:01 +02006074 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006075 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006076 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006077 if (error)
6078 return NULL;
6079 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006080 }
6081 else
6082 {
6083 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006084 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006085 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006086 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006087 if (bool_on_stack(cctx) == FAIL)
6088 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090
6091 scope = new_scope(cctx, IF_SCOPE);
6092 if (scope == NULL)
6093 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006094 scope->se_skip_save = skip_save;
6095 // "is_had_return" will be reset if any block does not end in :return
6096 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006098 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006099 {
6100 // "where" is set when ":elseif", "else" or ":endif" is found
6101 scope->se_u.se_if.is_if_label = instr->ga_len;
6102 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6103 }
6104 else
6105 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
6107 return p;
6108}
6109
6110 static char_u *
6111compile_elseif(char_u *arg, cctx_T *cctx)
6112{
6113 char_u *p = arg;
6114 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006115 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 isn_T *isn;
6117 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006118 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006119 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120
6121 if (scope == NULL || scope->se_type != IF_SCOPE)
6122 {
6123 emsg(_(e_elseif_without_if));
6124 return NULL;
6125 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006126 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 if (!cctx->ctx_had_return)
6128 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006130 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006131 {
6132 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006134 return NULL;
6135 // previous "if" or "elseif" jumps here
6136 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6137 isn->isn_arg.jump.jump_where = instr->ga_len;
6138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006140 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006141 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006142 if (cctx->ctx_skip == SKIP_YES)
6143 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006144 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006145 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006146 clear_ppconst(&ppconst);
6147 return NULL;
6148 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006149 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006150 if (scope->se_skip_save == SKIP_YES)
6151 clear_ppconst(&ppconst);
6152 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006153 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006154 int error = FALSE;
6155 int v;
6156
Bram Moolenaar7f141552020-05-09 17:35:53 +02006157 // The expression results in a constant.
6158 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006159 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6160 if (error)
6161 return NULL;
6162 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006163 clear_ppconst(&ppconst);
6164 scope->se_u.se_if.is_if_label = -1;
6165 }
6166 else
6167 {
6168 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006169 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006170 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006171 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006172 if (bool_on_stack(cctx) == FAIL)
6173 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006175 // "where" is set when ":elseif", "else" or ":endif" is found
6176 scope->se_u.se_if.is_if_label = instr->ga_len;
6177 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
6180 return p;
6181}
6182
6183 static char_u *
6184compile_else(char_u *arg, cctx_T *cctx)
6185{
6186 char_u *p = arg;
6187 garray_T *instr = &cctx->ctx_instr;
6188 isn_T *isn;
6189 scope_T *scope = cctx->ctx_scope;
6190
6191 if (scope == NULL || scope->se_type != IF_SCOPE)
6192 {
6193 emsg(_(e_else_without_if));
6194 return NULL;
6195 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006196 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006197 if (!cctx->ctx_had_return)
6198 scope->se_u.se_if.is_had_return = FALSE;
6199 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
Bram Moolenaarefd88552020-06-18 20:50:10 +02006201 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006202 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006203 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006204 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006205 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006206 if (!cctx->ctx_had_return
6207 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6208 JUMP_ALWAYS, cctx) == FAIL)
6209 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006210 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006211
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006212 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006213 {
6214 if (scope->se_u.se_if.is_if_label >= 0)
6215 {
6216 // previous "if" or "elseif" jumps here
6217 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6218 isn->isn_arg.jump.jump_where = instr->ga_len;
6219 scope->se_u.se_if.is_if_label = -1;
6220 }
6221 }
6222
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006223 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006224 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226
6227 return p;
6228}
6229
6230 static char_u *
6231compile_endif(char_u *arg, cctx_T *cctx)
6232{
6233 scope_T *scope = cctx->ctx_scope;
6234 ifscope_T *ifscope;
6235 garray_T *instr = &cctx->ctx_instr;
6236 isn_T *isn;
6237
6238 if (scope == NULL || scope->se_type != IF_SCOPE)
6239 {
6240 emsg(_(e_endif_without_if));
6241 return NULL;
6242 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006243 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006244 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006245 if (!cctx->ctx_had_return)
6246 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006248 if (scope->se_u.se_if.is_if_label >= 0)
6249 {
6250 // previous "if" or "elseif" jumps here
6251 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6252 isn->isn_arg.jump.jump_where = instr->ga_len;
6253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 // Fill in the "end" label in jumps at the end of the blocks.
6255 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006256 cctx->ctx_skip = scope->se_skip_save;
6257
6258 // If all the blocks end in :return and there is an :else then the
6259 // had_return flag is set.
6260 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006262 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 return arg;
6264}
6265
6266/*
6267 * compile "for var in expr"
6268 *
6269 * Produces instructions:
6270 * PUSHNR -1
6271 * STORE loop-idx Set index to -1
6272 * EVAL expr Push result of "expr"
6273 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6274 * - if beyond end, jump to "end"
6275 * - otherwise get item from list and push it
6276 * STORE var Store item in "var"
6277 * ... body ...
6278 * JUMP top Jump back to repeat
6279 * end: DROP Drop the result of "expr"
6280 *
6281 */
6282 static char_u *
6283compile_for(char_u *arg, cctx_T *cctx)
6284{
6285 char_u *p;
6286 size_t varlen;
6287 garray_T *instr = &cctx->ctx_instr;
6288 garray_T *stack = &cctx->ctx_type_stack;
6289 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006290 lvar_T *loop_lvar; // loop iteration variable
6291 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 type_T *vartype;
6293
6294 // TODO: list of variables: "for [key, value] in dict"
6295 // parse "var"
6296 for (p = arg; eval_isnamec1(*p); ++p)
6297 ;
6298 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006299 var_lvar = lookup_local(arg, varlen, cctx);
6300 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006302 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 return NULL;
6304 }
6305
6306 // consume "in"
6307 p = skipwhite(p);
6308 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6309 {
6310 emsg(_(e_missing_in));
6311 return NULL;
6312 }
6313 p = skipwhite(p + 2);
6314
6315
6316 scope = new_scope(cctx, FOR_SCOPE);
6317 if (scope == NULL)
6318 return NULL;
6319
6320 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006321 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6322 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006323 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006324 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006325 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
6329 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006330 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6331 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006332 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006333 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006334 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006338 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339
6340 // compile "expr", it remains on the stack until "endfor"
6341 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006342 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006343 {
6344 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006348 // Now that we know the type of "var", check that it is a list, now or at
6349 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006351 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006353 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 return NULL;
6355 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006356 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006357 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358
6359 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006360 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006362 generate_FOR(cctx, loop_lvar->lv_idx);
6363 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364
6365 return arg;
6366}
6367
6368/*
6369 * compile "endfor"
6370 */
6371 static char_u *
6372compile_endfor(char_u *arg, cctx_T *cctx)
6373{
6374 garray_T *instr = &cctx->ctx_instr;
6375 scope_T *scope = cctx->ctx_scope;
6376 forscope_T *forscope;
6377 isn_T *isn;
6378
6379 if (scope == NULL || scope->se_type != FOR_SCOPE)
6380 {
6381 emsg(_(e_for));
6382 return NULL;
6383 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006384 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006386 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
6388 // At end of ":for" scope jump back to the FOR instruction.
6389 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6390
6391 // Fill in the "end" label in the FOR statement so it can jump here
6392 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6393 isn->isn_arg.forloop.for_end = instr->ga_len;
6394
6395 // Fill in the "end" label any BREAK statements
6396 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6397
6398 // Below the ":for" scope drop the "expr" list from the stack.
6399 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6400 return NULL;
6401
6402 vim_free(scope);
6403
6404 return arg;
6405}
6406
6407/*
6408 * compile "while expr"
6409 *
6410 * Produces instructions:
6411 * top: EVAL expr Push result of "expr"
6412 * JUMP_IF_FALSE end jump if false
6413 * ... body ...
6414 * JUMP top Jump back to repeat
6415 * end:
6416 *
6417 */
6418 static char_u *
6419compile_while(char_u *arg, cctx_T *cctx)
6420{
6421 char_u *p = arg;
6422 garray_T *instr = &cctx->ctx_instr;
6423 scope_T *scope;
6424
6425 scope = new_scope(cctx, WHILE_SCOPE);
6426 if (scope == NULL)
6427 return NULL;
6428
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006429 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430
6431 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006432 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 return NULL;
6434
Bram Moolenaar13106602020-10-04 16:06:05 +02006435 if (bool_on_stack(cctx) == FAIL)
6436 return FAIL;
6437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006439 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 JUMP_IF_FALSE, cctx) == FAIL)
6441 return FAIL;
6442
6443 return p;
6444}
6445
6446/*
6447 * compile "endwhile"
6448 */
6449 static char_u *
6450compile_endwhile(char_u *arg, cctx_T *cctx)
6451{
6452 scope_T *scope = cctx->ctx_scope;
6453
6454 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6455 {
6456 emsg(_(e_while));
6457 return NULL;
6458 }
6459 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006460 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461
6462 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006463 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464
6465 // Fill in the "end" label in the WHILE statement so it can jump here.
6466 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006467 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468
6469 vim_free(scope);
6470
6471 return arg;
6472}
6473
6474/*
6475 * compile "continue"
6476 */
6477 static char_u *
6478compile_continue(char_u *arg, cctx_T *cctx)
6479{
6480 scope_T *scope = cctx->ctx_scope;
6481
6482 for (;;)
6483 {
6484 if (scope == NULL)
6485 {
6486 emsg(_(e_continue));
6487 return NULL;
6488 }
6489 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6490 break;
6491 scope = scope->se_outer;
6492 }
6493
6494 // Jump back to the FOR or WHILE instruction.
6495 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006496 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6497 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 return arg;
6499}
6500
6501/*
6502 * compile "break"
6503 */
6504 static char_u *
6505compile_break(char_u *arg, cctx_T *cctx)
6506{
6507 scope_T *scope = cctx->ctx_scope;
6508 endlabel_T **el;
6509
6510 for (;;)
6511 {
6512 if (scope == NULL)
6513 {
6514 emsg(_(e_break));
6515 return NULL;
6516 }
6517 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6518 break;
6519 scope = scope->se_outer;
6520 }
6521
6522 // Jump to the end of the FOR or WHILE loop.
6523 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006524 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6528 return FAIL;
6529
6530 return arg;
6531}
6532
6533/*
6534 * compile "{" start of block
6535 */
6536 static char_u *
6537compile_block(char_u *arg, cctx_T *cctx)
6538{
6539 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6540 return NULL;
6541 return skipwhite(arg + 1);
6542}
6543
6544/*
6545 * compile end of block: drop one scope
6546 */
6547 static void
6548compile_endblock(cctx_T *cctx)
6549{
6550 scope_T *scope = cctx->ctx_scope;
6551
6552 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006553 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 vim_free(scope);
6555}
6556
6557/*
6558 * compile "try"
6559 * Creates a new scope for the try-endtry, pointing to the first catch and
6560 * finally.
6561 * Creates another scope for the "try" block itself.
6562 * TRY instruction sets up exception handling at runtime.
6563 *
6564 * "try"
6565 * TRY -> catch1, -> finally push trystack entry
6566 * ... try block
6567 * "throw {exception}"
6568 * EVAL {exception}
6569 * THROW create exception
6570 * ... try block
6571 * " catch {expr}"
6572 * JUMP -> finally
6573 * catch1: PUSH exeception
6574 * EVAL {expr}
6575 * MATCH
6576 * JUMP nomatch -> catch2
6577 * CATCH remove exception
6578 * ... catch block
6579 * " catch"
6580 * JUMP -> finally
6581 * catch2: CATCH remove exception
6582 * ... catch block
6583 * " finally"
6584 * finally:
6585 * ... finally block
6586 * " endtry"
6587 * ENDTRY pop trystack entry, may rethrow
6588 */
6589 static char_u *
6590compile_try(char_u *arg, cctx_T *cctx)
6591{
6592 garray_T *instr = &cctx->ctx_instr;
6593 scope_T *try_scope;
6594 scope_T *scope;
6595
6596 // scope that holds the jumps that go to catch/finally/endtry
6597 try_scope = new_scope(cctx, TRY_SCOPE);
6598 if (try_scope == NULL)
6599 return NULL;
6600
6601 // "catch" is set when the first ":catch" is found.
6602 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006603 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 if (generate_instr(cctx, ISN_TRY) == NULL)
6605 return NULL;
6606
6607 // scope for the try block itself
6608 scope = new_scope(cctx, BLOCK_SCOPE);
6609 if (scope == NULL)
6610 return NULL;
6611
6612 return arg;
6613}
6614
6615/*
6616 * compile "catch {expr}"
6617 */
6618 static char_u *
6619compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6620{
6621 scope_T *scope = cctx->ctx_scope;
6622 garray_T *instr = &cctx->ctx_instr;
6623 char_u *p;
6624 isn_T *isn;
6625
6626 // end block scope from :try or :catch
6627 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6628 compile_endblock(cctx);
6629 scope = cctx->ctx_scope;
6630
6631 // Error if not in a :try scope
6632 if (scope == NULL || scope->se_type != TRY_SCOPE)
6633 {
6634 emsg(_(e_catch));
6635 return NULL;
6636 }
6637
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006638 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006640 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 return NULL;
6642 }
6643
6644 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006645 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 JUMP_ALWAYS, cctx) == FAIL)
6647 return NULL;
6648
6649 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006650 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 if (isn->isn_arg.try.try_catch == 0)
6652 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006653 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 {
6655 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 isn->isn_arg.jump.jump_where = instr->ga_len;
6658 }
6659
6660 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006661 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 scope->se_u.se_try.ts_caught_all = TRUE;
6664 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 }
6666 else
6667 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006668 char_u *end;
6669 char_u *pat;
6670 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006671 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006672 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 // Push v:exception, push {expr} and MATCH
6675 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6676
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006677 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006678 if (*end != *p)
6679 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006680 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006681 vim_free(tofree);
6682 return FAIL;
6683 }
6684 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006685 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006686 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006687 len = (int)(end - tofree);
6688 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006689 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006690 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006691 if (pat == NULL)
6692 return FAIL;
6693 if (generate_PUSHS(cctx, pat) == FAIL)
6694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6697 return NULL;
6698
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006699 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6701 return NULL;
6702 }
6703
6704 if (generate_instr(cctx, ISN_CATCH) == NULL)
6705 return NULL;
6706
6707 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6708 return NULL;
6709 return p;
6710}
6711
6712 static char_u *
6713compile_finally(char_u *arg, cctx_T *cctx)
6714{
6715 scope_T *scope = cctx->ctx_scope;
6716 garray_T *instr = &cctx->ctx_instr;
6717 isn_T *isn;
6718
6719 // end block scope from :try or :catch
6720 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6721 compile_endblock(cctx);
6722 scope = cctx->ctx_scope;
6723
6724 // Error if not in a :try scope
6725 if (scope == NULL || scope->se_type != TRY_SCOPE)
6726 {
6727 emsg(_(e_finally));
6728 return NULL;
6729 }
6730
6731 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006732 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 if (isn->isn_arg.try.try_finally != 0)
6734 {
6735 emsg(_(e_finally_dup));
6736 return NULL;
6737 }
6738
6739 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006740 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741
Bram Moolenaar585fea72020-04-02 22:33:21 +02006742 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006743 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 {
6745 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006746 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006748 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 }
6750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 // TODO: set index in ts_finally_label jumps
6752
6753 return arg;
6754}
6755
6756 static char_u *
6757compile_endtry(char_u *arg, cctx_T *cctx)
6758{
6759 scope_T *scope = cctx->ctx_scope;
6760 garray_T *instr = &cctx->ctx_instr;
6761 isn_T *isn;
6762
6763 // end block scope from :catch or :finally
6764 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6765 compile_endblock(cctx);
6766 scope = cctx->ctx_scope;
6767
6768 // Error if not in a :try scope
6769 if (scope == NULL || scope->se_type != TRY_SCOPE)
6770 {
6771 if (scope == NULL)
6772 emsg(_(e_no_endtry));
6773 else if (scope->se_type == WHILE_SCOPE)
6774 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006775 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 emsg(_(e_endfor));
6777 else
6778 emsg(_(e_endif));
6779 return NULL;
6780 }
6781
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006782 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6784 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006785 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 return NULL;
6787 }
6788
6789 // Fill in the "end" label in jumps at the end of the blocks, if not done
6790 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006791 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
6793 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006794 if (isn->isn_arg.try.try_catch == 0)
6795 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 if (isn->isn_arg.try.try_finally == 0)
6797 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006798
6799 if (scope->se_u.se_try.ts_catch_label != 0)
6800 {
6801 // Last catch without match jumps here
6802 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6803 isn->isn_arg.jump.jump_where = instr->ga_len;
6804 }
6805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 compile_endblock(cctx);
6807
6808 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6809 return NULL;
6810 return arg;
6811}
6812
6813/*
6814 * compile "throw {expr}"
6815 */
6816 static char_u *
6817compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6818{
6819 char_u *p = skipwhite(arg);
6820
Bram Moolenaara5565e42020-05-09 15:44:01 +02006821 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 return NULL;
6823 if (may_generate_2STRING(-1, cctx) == FAIL)
6824 return NULL;
6825 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6826 return NULL;
6827
6828 return p;
6829}
6830
6831/*
6832 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006833 * compile "echomsg expr"
6834 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006835 * compile "execute expr"
6836 */
6837 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006838compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006839{
6840 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006841 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006842 int count = 0;
6843
6844 for (;;)
6845 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006846 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006847 return NULL;
6848 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006849 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006850 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006851 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006852 break;
6853 }
6854
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006855 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6856 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6857 else if (cmdidx == CMD_execute)
6858 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6859 else if (cmdidx == CMD_echomsg)
6860 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6861 else
6862 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 return p;
6864}
6865
6866/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006867 * :put r
6868 * :put ={expr}
6869 */
6870 static char_u *
6871compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6872{
6873 char_u *line = arg;
6874 linenr_T lnum;
6875 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006876 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006877
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006878 eap->regname = *line;
6879
6880 if (eap->regname == '=')
6881 {
6882 char_u *p = line + 1;
6883
6884 if (compile_expr0(&p, cctx) == FAIL)
6885 return NULL;
6886 line = p;
6887 }
6888 else if (eap->regname != NUL)
6889 ++line;
6890
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006891 // "errormsg" will not be set because the range is ADDR_LINES.
6892 // TODO: if the range contains something like "$" or "." need to evaluate
6893 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006894 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6895 return NULL;
6896 if (eap->addr_count == 0)
6897 lnum = -1;
6898 else
6899 lnum = eap->line2;
6900 if (above)
6901 --lnum;
6902
6903 generate_PUT(cctx, eap->regname, lnum);
6904 return line;
6905}
6906
6907/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006908 * A command that is not compiled, execute with legacy code.
6909 */
6910 static char_u *
6911compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6912{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006913 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006914 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006915 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006916
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006917 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006918 goto theend;
6919
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006920 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006921 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006922 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006923 int usefilter = FALSE;
6924
6925 has_expr = argt & (EX_XFILE | EX_EXPAND);
6926
6927 // If the command can be followed by a bar, find the bar and truncate
6928 // it, so that the following command can be compiled.
6929 // The '|' is overwritten with a NUL, it is put back below.
6930 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6931 && *eap->arg == '!')
6932 // :w !filter or :r !filter or :r! filter
6933 usefilter = TRUE;
6934 if ((argt & EX_TRLBAR) && !usefilter)
6935 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006936 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006937 separate_nextcmd(eap);
6938 if (eap->nextcmd != NULL)
6939 nextcmd = eap->nextcmd;
6940 }
6941 }
6942
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006943 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6944 {
6945 // expand filename in "syntax include [@group] filename"
6946 has_expr = TRUE;
6947 eap->arg = skipwhite(eap->arg + 7);
6948 if (*eap->arg == '@')
6949 eap->arg = skiptowhite(eap->arg);
6950 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006951
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006952 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006953 {
6954 int count = 0;
6955 char_u *start = skipwhite(line);
6956
6957 // :cmd xxx`=expr1`yyy`=expr2`zzz
6958 // PUSHS ":cmd xxx"
6959 // eval expr1
6960 // PUSHS "yyy"
6961 // eval expr2
6962 // PUSHS "zzz"
6963 // EXECCONCAT 5
6964 for (;;)
6965 {
6966 if (p > start)
6967 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006968 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006969 ++count;
6970 }
6971 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006972 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006973 return NULL;
6974 may_generate_2STRING(-1, cctx);
6975 ++count;
6976 p = skipwhite(p);
6977 if (*p != '`')
6978 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006979 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006980 return NULL;
6981 }
6982 start = p + 1;
6983
6984 p = (char_u *)strstr((char *)start, "`=");
6985 if (p == NULL)
6986 {
6987 if (*skipwhite(start) != NUL)
6988 {
6989 generate_PUSHS(cctx, vim_strsave(start));
6990 ++count;
6991 }
6992 break;
6993 }
6994 }
6995 generate_EXECCONCAT(cctx, count);
6996 }
6997 else
6998 generate_EXEC(cctx, line);
6999
7000theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007001 if (*nextcmd != NUL)
7002 {
7003 // the parser expects a pointer to the bar, put it back
7004 --nextcmd;
7005 *nextcmd = '|';
7006 }
7007
7008 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007009}
7010
7011/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007012 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007013 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007014 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007015 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007016add_def_function(ufunc_T *ufunc)
7017{
7018 dfunc_T *dfunc;
7019
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007020 if (def_functions.ga_len == 0)
7021 {
7022 // The first position is not used, so that a zero uf_dfunc_idx means it
7023 // wasn't set.
7024 if (ga_grow(&def_functions, 1) == FAIL)
7025 return FAIL;
7026 ++def_functions.ga_len;
7027 }
7028
Bram Moolenaar09689a02020-05-09 22:50:08 +02007029 // Add the function to "def_functions".
7030 if (ga_grow(&def_functions, 1) == FAIL)
7031 return FAIL;
7032 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7033 CLEAR_POINTER(dfunc);
7034 dfunc->df_idx = def_functions.ga_len;
7035 ufunc->uf_dfunc_idx = dfunc->df_idx;
7036 dfunc->df_ufunc = ufunc;
7037 ++def_functions.ga_len;
7038 return OK;
7039}
7040
7041/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 * After ex_function() has collected all the function lines: parse and compile
7043 * the lines into instructions.
7044 * Adds the function to "def_functions".
7045 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7046 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007047 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007048 * This can be used recursively through compile_lambda(), which may reallocate
7049 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007050 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007052 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007053compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 char_u *line = NULL;
7056 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 cctx_T cctx;
7059 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007060 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 int ret = FAIL;
7062 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007063 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007064 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007065 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007067 // When using a function that was compiled before: Free old instructions.
7068 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007069 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007071 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7072 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007073 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007075 else
7076 {
7077 if (add_def_function(ufunc) == FAIL)
7078 return FAIL;
7079 new_def_function = TRUE;
7080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
Bram Moolenaar985116a2020-07-12 17:31:09 +02007082 ufunc->uf_def_status = UF_COMPILING;
7083
Bram Moolenaara80faa82020-04-12 19:37:17 +02007084 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 cctx.ctx_ufunc = ufunc;
7086 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007087 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7089 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7090 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7091 cctx.ctx_type_list = &ufunc->uf_type_list;
7092 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7093 instr = &cctx.ctx_instr;
7094
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007095 // Set the context to the function, it may be compiled when called from
7096 // another script. Set the script version to the most modern one.
7097 // The line number will be set in next_line_from_context().
7098 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7100
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007101 // Make sure error messages are OK.
7102 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7103 if (do_estack_push)
7104 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007105 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007106
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007107 if (ufunc->uf_def_args.ga_len > 0)
7108 {
7109 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007110 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007111 int i;
7112 char_u *arg;
7113 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007114 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007115
7116 // Produce instructions for the default values of optional arguments.
7117 // Store the instruction index in uf_def_arg_idx[] so that we know
7118 // where to start when the function is called, depending on the number
7119 // of arguments.
7120 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7121 if (ufunc->uf_def_arg_idx == NULL)
7122 goto erret;
7123 for (i = 0; i < count; ++i)
7124 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007125 garray_T *stack = &cctx.ctx_type_stack;
7126 type_T *val_type;
7127 int arg_idx = first_def_arg + i;
7128
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007129 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7130 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007131 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007132 goto erret;
7133
7134 // If no type specified use the type of the default value.
7135 // Otherwise check that the default value type matches the
7136 // specified type.
7137 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7138 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007139 {
7140 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007141 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007142 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007143 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7144 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007145 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007146
7147 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007148 goto erret;
7149 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007150 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007151
7152 if (did_set_arg_type)
7153 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007154 }
7155
7156 /*
7157 * Loop over all the lines of the function and generate instructions.
7158 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 for (;;)
7160 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007161 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007162 int starts_with_colon = FALSE;
7163 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007164 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007165
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007166 // Bail out on the first error to avoid a flood of errors and report
7167 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007168 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007169 goto erret;
7170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 if (line != NULL && *line == '|')
7172 // the line continues after a '|'
7173 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007174 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007175 && !(*line == '#' && (line == cctx.ctx_line_start
7176 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007178 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 goto erret;
7180 }
7181 else
7182 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007183 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007184 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007185 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187 }
7188
Bram Moolenaara80faa82020-04-12 19:37:17 +02007189 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 ea.cmdlinep = &line;
7191 ea.cmd = skipwhite(line);
7192
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007193 // Some things can be recognized by the first character.
7194 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007196 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007197 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007198 if (ea.cmd[1] != '{')
7199 {
7200 line = (char_u *)"";
7201 continue;
7202 }
7203 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007205 case '}':
7206 {
7207 // "}" ends a block scope
7208 scopetype_T stype = cctx.ctx_scope == NULL
7209 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007211 if (stype == BLOCK_SCOPE)
7212 {
7213 compile_endblock(&cctx);
7214 line = ea.cmd;
7215 }
7216 else
7217 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007218 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007219 goto erret;
7220 }
7221 if (line != NULL)
7222 line = skipwhite(ea.cmd + 1);
7223 continue;
7224 }
7225
7226 case '{':
7227 // "{" starts a block scope
7228 // "{'a': 1}->func() is something else
7229 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7230 {
7231 line = compile_block(ea.cmd, &cctx);
7232 continue;
7233 }
7234 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 }
7236
7237 /*
7238 * COMMAND MODIFIERS
7239 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007240 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007241 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7242 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 {
7244 if (errormsg != NULL)
7245 goto erret;
7246 // empty line or comment
7247 line = (char_u *)"";
7248 continue;
7249 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007250 generate_cmdmods(&cctx, &local_cmdmod);
7251 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007253 // Check if there was a colon after the last command modifier or before
7254 // the current position.
7255 for (p = ea.cmd; p >= line; --p)
7256 {
7257 if (*p == ':')
7258 starts_with_colon = TRUE;
7259 if (p < ea.cmd && !VIM_ISWHITE(*p))
7260 break;
7261 }
7262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007264 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007266 {
7267 if (*ea.cmd == '(')
7268 // not for "call()"
7269 ea.cmd = p;
7270 else
7271 ea.cmd = skipwhite(ea.cmd);
7272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007274 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007276 char_u *pskip;
7277
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007278 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007279 // find what follows.
7280 // Skip over "var.member", "var[idx]" and the like.
7281 // Also "&opt = val", "$ENV = val" and "@r = val".
7282 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007283 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007284 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007285 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007287 char_u *var_end;
7288 int oplen;
7289 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290
Bram Moolenaar65821722020-08-02 18:58:54 +02007291 if (ea.cmd[0] == '@')
7292 var_end = ea.cmd + 2;
7293 else
7294 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007295 FNE_CHECK_START | FNE_INCL_BR);
7296 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007297 if (oplen > 0)
7298 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007299 size_t len = p - ea.cmd;
7300
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007301 // Recognize an assignment if we recognize the variable
7302 // name:
7303 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007304 // "local = expr" where "local" is a local var.
7305 // "script = expr" where "script" is a script-local var.
7306 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007307 // "&opt = expr"
7308 // "$ENV = expr"
7309 // "@r = expr"
7310 if (*ea.cmd == '&'
7311 || *ea.cmd == '$'
7312 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007313 || ((len) > 2 && ea.cmd[1] == ':')
7314 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007315 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007316 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007317 || script_var_exists(ea.cmd, len,
7318 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007319 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007320 {
7321 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007322 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007323 goto erret;
7324 continue;
7325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 }
7327 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007328
7329 if (*ea.cmd == '[')
7330 {
7331 // [var, var] = expr
7332 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7333 if (line == NULL)
7334 goto erret;
7335 if (line != ea.cmd)
7336 continue;
7337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 }
7339
7340 /*
7341 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007342 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007344 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007345 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007346 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007347 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007348 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007349 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007350 if (!starts_with_colon)
7351 {
7352 emsg(_(e_colon_required_before_a_range));
7353 goto erret;
7354 }
7355 if (ends_excmd2(line, ea.cmd))
7356 {
7357 // A range without a command: jump to the line.
7358 // TODO: compile to a more efficient command, possibly
7359 // calling parse_cmd_address().
7360 ea.cmdidx = CMD_SIZE;
7361 line = compile_exec(line, &ea, &cctx);
7362 goto nextline;
7363 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007364 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007365 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007366 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007367 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007368 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369
7370 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7371 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007372 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007373 {
7374 line += STRLEN(line);
7375 continue;
7376 }
7377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007379 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007381 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007382 iemsg("Command from find_ex_command() not handled");
7383 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 }
7386
Bram Moolenaar3988f642020-08-27 22:43:03 +02007387 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007388 && ea.cmdidx != CMD_elseif
7389 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007390 && ea.cmdidx != CMD_endif
7391 && ea.cmdidx != CMD_endfor
7392 && ea.cmdidx != CMD_endwhile
7393 && ea.cmdidx != CMD_catch
7394 && ea.cmdidx != CMD_finally
7395 && ea.cmdidx != CMD_endtry)
7396 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007397 emsg(_(e_unreachable_code_after_return));
7398 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007399 }
7400
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007401 p = skipwhite(p);
7402 if (ea.cmdidx != CMD_SIZE
7403 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7404 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007405 if (ea.cmdidx >= 0)
7406 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007407 if ((ea.argt & EX_BANG) && *p == '!')
7408 {
7409 ea.forceit = TRUE;
7410 p = skipwhite(p + 1);
7411 }
7412 }
7413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 switch (ea.cmdidx)
7415 {
7416 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007417 ea.arg = p;
7418 line = compile_nested_function(&ea, &cctx);
7419 break;
7420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007422 // TODO: should we allow this, e.g. to declare a global
7423 // function?
7424 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 goto erret;
7426
7427 case CMD_return:
7428 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007429 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 break;
7431
7432 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007433 emsg(_(e_cannot_use_let_in_vim9_script));
7434 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007435 case CMD_var:
7436 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 case CMD_const:
7438 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007439 if (line == p)
7440 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 break;
7442
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007443 case CMD_unlet:
7444 case CMD_unlockvar:
7445 case CMD_lockvar:
7446 line = compile_unletlock(p, &ea, &cctx);
7447 break;
7448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 case CMD_import:
7450 line = compile_import(p, &cctx);
7451 break;
7452
7453 case CMD_if:
7454 line = compile_if(p, &cctx);
7455 break;
7456 case CMD_elseif:
7457 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007458 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 break;
7460 case CMD_else:
7461 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007462 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 break;
7464 case CMD_endif:
7465 line = compile_endif(p, &cctx);
7466 break;
7467
7468 case CMD_while:
7469 line = compile_while(p, &cctx);
7470 break;
7471 case CMD_endwhile:
7472 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007473 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 break;
7475
7476 case CMD_for:
7477 line = compile_for(p, &cctx);
7478 break;
7479 case CMD_endfor:
7480 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007481 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 break;
7483 case CMD_continue:
7484 line = compile_continue(p, &cctx);
7485 break;
7486 case CMD_break:
7487 line = compile_break(p, &cctx);
7488 break;
7489
7490 case CMD_try:
7491 line = compile_try(p, &cctx);
7492 break;
7493 case CMD_catch:
7494 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007495 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 break;
7497 case CMD_finally:
7498 line = compile_finally(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_endtry:
7502 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007503 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 break;
7505 case CMD_throw:
7506 line = compile_throw(p, &cctx);
7507 break;
7508
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007509 case CMD_eval:
7510 if (compile_expr0(&p, &cctx) == FAIL)
7511 goto erret;
7512
Bram Moolenaar3988f642020-08-27 22:43:03 +02007513 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007514 generate_instr_drop(&cctx, ISN_DROP, 1);
7515
7516 line = skipwhite(p);
7517 break;
7518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007521 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007522 case CMD_echomsg:
7523 case CMD_echoerr:
7524 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007525 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007527 case CMD_put:
7528 ea.cmd = cmd;
7529 line = compile_put(p, &ea, &cctx);
7530 break;
7531
Bram Moolenaar3988f642020-08-27 22:43:03 +02007532 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007533
Bram Moolenaarae616492020-07-28 20:07:27 +02007534 case CMD_append:
7535 case CMD_change:
7536 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007537 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007538 case CMD_xit:
7539 not_in_vim9(&ea);
7540 goto erret;
7541
Bram Moolenaar002262f2020-07-08 17:47:57 +02007542 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007543 if (cctx.ctx_skip != SKIP_YES)
7544 {
7545 semsg(_(e_invalid_command_str), ea.cmd);
7546 goto erret;
7547 }
7548 // We don't check for a next command here.
7549 line = (char_u *)"";
7550 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007553 if (cctx.ctx_skip == SKIP_YES)
7554 {
7555 // We don't check for a next command here.
7556 line = (char_u *)"";
7557 }
7558 else
7559 {
7560 // Not recognized, execute with do_cmdline_cmd().
7561 ea.arg = p;
7562 line = compile_exec(line, &ea, &cctx);
7563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 break;
7565 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007566nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 if (line == NULL)
7568 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007569 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007571 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007572 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 if (cctx.ctx_type_stack.ga_len < 0)
7575 {
7576 iemsg("Type stack underflow");
7577 goto erret;
7578 }
7579 }
7580
7581 if (cctx.ctx_scope != NULL)
7582 {
7583 if (cctx.ctx_scope->se_type == IF_SCOPE)
7584 emsg(_(e_endif));
7585 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7586 emsg(_(e_endwhile));
7587 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7588 emsg(_(e_endfor));
7589 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007590 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 goto erret;
7592 }
7593
Bram Moolenaarefd88552020-06-18 20:50:10 +02007594 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 {
7596 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007598 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 goto erret;
7600 }
7601
7602 // Return zero if there is no return at the end.
7603 generate_PUSHNR(&cctx, 0);
7604 generate_instr(&cctx, ISN_RETURN);
7605 }
7606
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007607 {
7608 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7609 + ufunc->uf_dfunc_idx;
7610 dfunc->df_deleted = FALSE;
7611 dfunc->df_instr = instr->ga_data;
7612 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007613 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007614 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007615 if (cctx.ctx_outer_used)
7616 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007617 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619
7620 ret = OK;
7621
7622erret:
7623 if (ret == FAIL)
7624 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007625 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007626 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7627 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007628
7629 for (idx = 0; idx < instr->ga_len; ++idx)
7630 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007632
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007633 // If using the last entry in the table and it was added above, we
7634 // might as well remove it.
7635 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007636 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007637 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007638 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007639 ufunc->uf_dfunc_idx = 0;
7640 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007641 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007642
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007643 while (cctx.ctx_scope != NULL)
7644 drop_scope(&cctx);
7645
Bram Moolenaar20431c92020-03-20 18:39:46 +01007646 // Don't execute this function body.
7647 ga_clear_strings(&ufunc->uf_lines);
7648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 if (errormsg != NULL)
7650 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007651 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007652 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 }
7654
7655 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007656 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007657 if (do_estack_push)
7658 estack_pop();
7659
Bram Moolenaar20431c92020-03-20 18:39:46 +01007660 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007661 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007663 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664}
7665
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007666 void
7667set_function_type(ufunc_T *ufunc)
7668{
7669 int varargs = ufunc->uf_va_name != NULL;
7670 int argcount = ufunc->uf_args.ga_len;
7671
7672 // Create a type for the function, with the return type and any
7673 // argument types.
7674 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7675 // The type is included in "tt_args".
7676 if (argcount > 0 || varargs)
7677 {
7678 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7679 argcount, &ufunc->uf_type_list);
7680 // Add argument types to the function type.
7681 if (func_type_add_arg_types(ufunc->uf_func_type,
7682 argcount + varargs,
7683 &ufunc->uf_type_list) == FAIL)
7684 return;
7685 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7686 ufunc->uf_func_type->tt_min_argcount =
7687 argcount - ufunc->uf_def_args.ga_len;
7688 if (ufunc->uf_arg_types == NULL)
7689 {
7690 int i;
7691
7692 // lambda does not have argument types.
7693 for (i = 0; i < argcount; ++i)
7694 ufunc->uf_func_type->tt_args[i] = &t_any;
7695 }
7696 else
7697 mch_memmove(ufunc->uf_func_type->tt_args,
7698 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7699 if (varargs)
7700 {
7701 ufunc->uf_func_type->tt_args[argcount] =
7702 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7703 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7704 }
7705 }
7706 else
7707 // No arguments, can use a predefined type.
7708 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7709 argcount, &ufunc->uf_type_list);
7710}
7711
7712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713/*
7714 * Delete an instruction, free what it contains.
7715 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007716 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717delete_instr(isn_T *isn)
7718{
7719 switch (isn->isn_type)
7720 {
7721 case ISN_EXEC:
7722 case ISN_LOADENV:
7723 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007724 case ISN_LOADB:
7725 case ISN_LOADW:
7726 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007728 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 case ISN_PUSHEXC:
7730 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007731 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007733 case ISN_STOREB:
7734 case ISN_STOREW:
7735 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007736 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 vim_free(isn->isn_arg.string);
7738 break;
7739
7740 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007741 case ISN_STORES:
7742 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 break;
7744
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007745 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007746 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007747 vim_free(isn->isn_arg.unlet.ul_name);
7748 break;
7749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 case ISN_STOREOPT:
7751 vim_free(isn->isn_arg.storeopt.so_name);
7752 break;
7753
7754 case ISN_PUSHBLOB: // push blob isn_arg.blob
7755 blob_unref(isn->isn_arg.blob);
7756 break;
7757
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007758 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007760 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007761#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007762 break;
7763
7764 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007765#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007766 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007767#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007768 break;
7769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 case ISN_UCALL:
7771 vim_free(isn->isn_arg.ufunc.cuf_name);
7772 break;
7773
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007774 case ISN_FUNCREF:
7775 {
7776 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7777 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007778
7779 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7780 func_ptr_unref(dfunc->df_ufunc);
7781 }
7782 break;
7783
7784 case ISN_DCALL:
7785 {
7786 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7787 + isn->isn_arg.dfunc.cdf_idx;
7788
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007789 if (dfunc->df_ufunc != NULL
7790 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007791 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007792 }
7793 break;
7794
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007795 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007796 {
7797 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7798 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7799
7800 if (ufunc != NULL)
7801 {
7802 // Clear uf_dfunc_idx so that the function is deleted.
7803 clear_def_function(ufunc);
7804 ufunc->uf_dfunc_idx = 0;
7805 func_ptr_unref(ufunc);
7806 }
7807
7808 vim_free(lambda);
7809 vim_free(isn->isn_arg.newfunc.nf_global);
7810 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007811 break;
7812
Bram Moolenaar5e654232020-09-16 15:22:00 +02007813 case ISN_CHECKTYPE:
7814 free_type(isn->isn_arg.type.ct_type);
7815 break;
7816
Bram Moolenaar02194d22020-10-24 23:08:38 +02007817 case ISN_CMDMOD:
7818 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7819 ->cmod_filter_regmatch.regprog);
7820 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7821 break;
7822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 case ISN_2BOOL:
7824 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007825 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 case ISN_ADDBLOB:
7827 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007828 case ISN_ANYINDEX:
7829 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007831 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007833 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007835 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 case ISN_COMPAREANY:
7837 case ISN_COMPAREBLOB:
7838 case ISN_COMPAREBOOL:
7839 case ISN_COMPAREDICT:
7840 case ISN_COMPAREFLOAT:
7841 case ISN_COMPAREFUNC:
7842 case ISN_COMPARELIST:
7843 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 case ISN_COMPARESPECIAL:
7845 case ISN_COMPARESTRING:
7846 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007847 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 case ISN_DROP:
7849 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007850 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007851 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007853 case ISN_EXECCONCAT:
7854 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007856 case ISN_GETITEM:
7857 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007858 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007859 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007860 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007862 case ISN_LOADBDICT:
7863 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007864 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007866 case ISN_LOADSCRIPT:
7867 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007869 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007870 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007871 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 case ISN_NEGATENR:
7873 case ISN_NEWDICT:
7874 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007876 case ISN_OPFLOAT:
7877 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007879 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007880 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 case ISN_PUSHF:
7882 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007884 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007886 case ISN_SHUFFLE:
7887 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007889 case ISN_STOREDICT:
7890 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007891 case ISN_STORENR:
7892 case ISN_STOREOUTER:
7893 case ISN_STOREREG:
7894 case ISN_STORESCRIPT:
7895 case ISN_STOREV:
7896 case ISN_STRINDEX:
7897 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 case ISN_THROW:
7899 case ISN_TRY:
7900 // nothing allocated
7901 break;
7902 }
7903}
7904
7905/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007906 * Free all instructions for "dfunc".
7907 */
7908 static void
7909delete_def_function_contents(dfunc_T *dfunc)
7910{
7911 int idx;
7912
7913 ga_clear(&dfunc->df_def_args_isn);
7914
7915 if (dfunc->df_instr != NULL)
7916 {
7917 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7918 delete_instr(dfunc->df_instr + idx);
7919 VIM_CLEAR(dfunc->df_instr);
7920 }
7921
7922 dfunc->df_deleted = TRUE;
7923}
7924
7925/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007926 * When a user function is deleted, clear the contents of any associated def
7927 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 */
7929 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007930clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007932 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007933 {
7934 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7935 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936
Bram Moolenaar20431c92020-03-20 18:39:46 +01007937 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007938 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 }
7940}
7941
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007942/*
7943 * Used when a user function is about to be deleted: remove the pointer to it.
7944 * The entry in def_functions is then unused.
7945 */
7946 void
7947unlink_def_function(ufunc_T *ufunc)
7948{
7949 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7950
7951 dfunc->df_ufunc = NULL;
7952}
7953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007955/*
7956 * Free all functions defined with ":def".
7957 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958 void
7959free_def_functions(void)
7960{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007961 int idx;
7962
7963 for (idx = 0; idx < def_functions.ga_len; ++idx)
7964 {
7965 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7966
7967 delete_def_function_contents(dfunc);
7968 }
7969
7970 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007971}
7972#endif
7973
7974
7975#endif // FEAT_EVAL