blob: ee3d89e517309c6e74ef8d7ab0a94b3aac369052 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264 * Lookup a script-local variable in the current script, possibly defined in a
265 * block that contains the function "cctx->ctx_ufunc".
266 * "cctx" is NULL at the script level.
267 * if "len" is <= 0 "name" must be NUL terminated.
268 * Return NULL when not found.
269 */
270 static sallvar_T *
271find_script_var(char_u *name, size_t len, cctx_T *cctx)
272{
273 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
274 hashitem_T *hi;
275 int cc;
276 sallvar_T *sav;
277 ufunc_T *ufunc;
278
279 // Find the list of all script variables with the right name.
280 if (len > 0)
281 {
282 cc = name[len];
283 name[len] = NUL;
284 }
285 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
286 if (len > 0)
287 name[len] = cc;
288 if (HASHITEM_EMPTY(hi))
289 return NULL;
290
291 sav = HI2SAV(hi);
292 if (sav->sav_block_id == 0 || cctx == NULL)
293 // variable defined in the script scope or not in a function.
294 return sav;
295
296 // Go over the variables with this name and find one that was visible
297 // from the function.
298 ufunc = cctx->ctx_ufunc;
299 while (sav != NULL)
300 {
301 int idx;
302
303 // Go over the blocks that this function was defined in. If the
304 // variable block ID matches it was visible to the function.
305 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
306 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
307 return sav;
308 sav = sav->sav_next;
309 }
310
311 return NULL;
312}
313
314/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200315 * Returnd TRUE if the script context is Vim9 script.
316 */
317 static int
318script_is_vim9()
319{
320 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
321}
322
323/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200324 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200325 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
326 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 * Returns OK or FAIL.
329 */
330 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200335 if (current_sctx.sc_sid <= 0)
336 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 is_vim9_script = script_is_vim9();
338 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200339 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340 if (is_vim9_script)
341 {
342 // Check script variables that were visible where the function was
343 // defined.
344 if (find_script_var(name, len, cctx) != NULL)
345 return OK;
346 }
347 else
348 {
349 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
350 dictitem_T *di;
351 int cc;
352
353 // Check script variables that are currently visible
354 cc = name[len];
355 name[len] = NUL;
356 di = find_var_in_ht(ht, 0, name, TRUE);
357 name[len] = cc;
358 if (di != NULL)
359 return OK;
360 }
361
362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363}
364
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365/*
366 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200367 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200368 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 * Return FAIL and give an error if it defined.
370 */
371 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 int c = p[len];
375 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200376
377 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200378 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200380 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100384 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200385 // A local or script-local function can shadow a global function.
386 if (ufunc == NULL || !func_is_global(ufunc)
387 || (p[0] == 'g' && p[1] == ':'))
388 {
389 p[len] = c;
390 semsg(_(e_name_already_defined_str), p);
391 return FAIL;
392 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200394 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100395 return OK;
396}
397
Bram Moolenaar65b95452020-07-19 14:03:09 +0200398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200455 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200463 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 */
465 static int
466may_generate_2STRING(int offset, cctx_T *cctx)
467{
468 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 switch ((*type)->tt_type)
474 {
475 // nothing to be done
476 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 // conversion possible
479 case VAR_SPECIAL:
480 case VAR_BOOL:
481 case VAR_NUMBER:
482 case VAR_FLOAT:
483 break;
484
485 // conversion possible (with runtime check)
486 case VAR_ANY:
487 case VAR_UNKNOWN:
488 isntype = ISN_2STRING_ANY;
489 break;
490
491 // conversion not possible
492 case VAR_VOID:
493 case VAR_BLOB:
494 case VAR_FUNC:
495 case VAR_PARTIAL:
496 case VAR_LIST:
497 case VAR_DICT:
498 case VAR_JOB:
499 case VAR_CHANNEL:
500 to_string_error((*type)->tt_type);
501 return FAIL;
502 }
503
504 *type = &t_string;
505 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507 isn->isn_arg.number = offset;
508
509 return OK;
510}
511
512 static int
513check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
514{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200517 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 {
519 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return FAIL;
524 }
525 return OK;
526}
527
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200528 static int
529generate_add_instr(
530 cctx_T *cctx,
531 vartype_T vartype,
532 type_T *type1,
533 type_T *type2)
534{
535 isn_T *isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR
537 : vartype == VAR_LIST ? ISN_ADDLIST
538 : vartype == VAR_BLOB ? ISN_ADDBLOB
539#ifdef FEAT_FLOAT
540 : vartype == VAR_FLOAT ? ISN_OPFLOAT
541#endif
542 : ISN_OPANY, 1);
543
544 if (vartype != VAR_LIST && vartype != VAR_BLOB
545 && type1->tt_type != VAR_ANY
546 && type2->tt_type != VAR_ANY
547 && check_number_or_float(
548 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
549 return FAIL;
550
551 if (isn != NULL)
552 isn->isn_arg.op.op_type = EXPR_ADD;
553 return isn == NULL ? FAIL : OK;
554}
555
556/*
557 * Get the type to use for an instruction for an operation on "type1" and
558 * "type2". If they are matching use a type-specific instruction. Otherwise
559 * fall back to runtime type checking.
560 */
561 static vartype_T
562operator_type(type_T *type1, type_T *type2)
563{
564 if (type1->tt_type == type2->tt_type
565 && (type1->tt_type == VAR_NUMBER
566 || type1->tt_type == VAR_LIST
567#ifdef FEAT_FLOAT
568 || type1->tt_type == VAR_FLOAT
569#endif
570 || type1->tt_type == VAR_BLOB))
571 return type1->tt_type;
572 return VAR_ANY;
573}
574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575/*
576 * Generate an instruction with two arguments. The instruction depends on the
577 * type of the arguments.
578 */
579 static int
580generate_two_op(cctx_T *cctx, char_u *op)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583 type_T *type1;
584 type_T *type2;
585 vartype_T vartype;
586 isn_T *isn;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_OK_IF_SKIP(cctx);
589
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200590 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
592 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 switch (*op)
596 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200597 case '+':
598 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 break;
601
602 case '-':
603 case '*':
604 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
605 op) == FAIL)
606 return FAIL;
607 if (vartype == VAR_NUMBER)
608 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
609#ifdef FEAT_FLOAT
610 else if (vartype == VAR_FLOAT)
611 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
612#endif
613 else
614 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
615 if (isn != NULL)
616 isn->isn_arg.op.op_type = *op == '*'
617 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
618 break;
619
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200622 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 && type2->tt_type != VAR_NUMBER))
624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200625 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 }
628 isn = generate_instr_drop(cctx,
629 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = EXPR_REM;
632 break;
633 }
634
635 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 type_T *type = &t_any;
639
640#ifdef FEAT_FLOAT
641 // float+number and number+float results in float
642 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
643 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
644 type = &t_float;
645#endif
646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
647 }
648
649 return OK;
650}
651
652/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200653 * Get the instruction to use for comparing "type1" with "type2"
654 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200656 static isntype_T
657get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658{
659 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 if (type1 == VAR_UNKNOWN)
662 type1 = VAR_ANY;
663 if (type2 == VAR_UNKNOWN)
664 type2 = VAR_ANY;
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (type1 == type2)
667 {
668 switch (type1)
669 {
670 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
671 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
672 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
673 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
674 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
675 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
676 case VAR_LIST: isntype = ISN_COMPARELIST; break;
677 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
678 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 default: isntype = ISN_COMPAREANY; break;
680 }
681 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
684 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
685 isntype = ISN_COMPAREANY;
686
687 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
688 && (isntype == ISN_COMPAREBOOL
689 || isntype == ISN_COMPARESPECIAL
690 || isntype == ISN_COMPARENR
691 || isntype == ISN_COMPAREFLOAT))
692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200693 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200695 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 }
697 if (isntype == ISN_DROP
698 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
699 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
700 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
701 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
702 && exptype != EXPR_IS && exptype != EXPR_ISNOT
703 && (type1 == VAR_BLOB || type2 == VAR_BLOB
704 || type1 == VAR_LIST || type2 == VAR_LIST))))
705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200706 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return isntype;
711}
712
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200713 int
714check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
715{
716 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
717 return FAIL;
718 return OK;
719}
720
Bram Moolenaara5565e42020-05-09 15:44:01 +0200721/*
722 * Generate an ISN_COMPARE* instruction with a boolean result.
723 */
724 static int
725generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
726{
727 isntype_T isntype;
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730 vartype_T type1;
731 vartype_T type2;
732
733 RETURN_OK_IF_SKIP(cctx);
734
735 // Get the known type of the two items on the stack. If they are matching
736 // use a type-specific instruction. Otherwise fall back to runtime type
737 // checking.
738 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
739 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
740 isntype = get_compare_isn(exptype, type1, type2);
741 if (isntype == ISN_DROP)
742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if ((isn = generate_instr(cctx, isntype)) == NULL)
745 return FAIL;
746 isn->isn_arg.op.op_type = exptype;
747 isn->isn_arg.op.op_ic = ic;
748
749 // takes two arguments, puts one bool back
750 if (stack->ga_len >= 2)
751 {
752 --stack->ga_len;
753 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
754 }
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_2BOOL instruction.
761 */
762 static int
763generate_2BOOL(cctx_T *cctx, int invert)
764{
765 isn_T *isn;
766 garray_T *stack = &cctx->ctx_type_stack;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
770 return FAIL;
771 isn->isn_arg.number = invert;
772
773 // type becomes bool
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775
776 return OK;
777}
778
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200779/*
780 * Generate an ISN_COND2BOOL instruction.
781 */
782 static int
783generate_COND2BOOL(cctx_T *cctx)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
788 RETURN_OK_IF_SKIP(cctx);
789 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
790 return FAIL;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200799generate_TYPECHECK(
800 cctx_T *cctx,
801 type_T *expected,
802 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
Bram Moolenaar080457c2020-03-03 21:53:32 +0100807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
809 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200810 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 isn->isn_arg.type.ct_off = offset;
812
Bram Moolenaar5e654232020-09-16 15:22:00 +0200813 // type becomes expected
814 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 return OK;
817}
818
819/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200820 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821 * used. Return FALSE if the types will never match.
822 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100823 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200824use_typecheck(type_T *actual, type_T *expected)
825{
826 if (actual->tt_type == VAR_ANY
827 || actual->tt_type == VAR_UNKNOWN
828 || (actual->tt_type == VAR_FUNC
829 && (expected->tt_type == VAR_FUNC
830 || expected->tt_type == VAR_PARTIAL)
831 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
832 return TRUE;
833 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
834 && actual->tt_type == expected->tt_type)
835 // This takes care of a nested list or dict.
836 return use_typecheck(actual->tt_member, expected->tt_member);
837 return FALSE;
838}
839
840/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200843 * - "actual" is a type that can be "expected" type: add a runtime check; or
844 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200845 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
846 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 */
848 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200849need_type(
850 type_T *actual,
851 type_T *expected,
852 int offset,
853 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200854 int silent,
855 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200857 if (expected == &t_bool && actual != &t_bool
858 && (actual->tt_flags & TTFLAG_BOOL_OK))
859 {
860 // Using "0", "1" or the result of an expression with "&&" or "||" as a
861 // boolean is OK but requires a conversion.
862 generate_2BOOL(cctx, FALSE);
863 return OK;
864 }
865
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200866 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200868
869 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 // If it's a constant a runtime check makes no sense.
871 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 generate_TYPECHECK(cctx, expected, offset);
874 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200875 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876
877 if (!silent)
878 type_mismatch(expected, actual);
879 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880}
881
882/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100883 * Check that the top of the type stack has a type that can be used as a
884 * condition. Give an error and return FAIL if not.
885 */
886 static int
887bool_on_stack(cctx_T *cctx)
888{
889 garray_T *stack = &cctx->ctx_type_stack;
890 type_T *type;
891
892 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
893 if (type == &t_bool)
894 return OK;
895
896 if (type == &t_any || type == &t_number)
897 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
898 // This requires a runtime type check.
899 return generate_COND2BOOL(cctx);
900
901 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
902}
903
904/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 * Generate an ISN_PUSHNR instruction.
906 */
907 static int
908generate_PUSHNR(cctx_T *cctx, varnumber_T number)
909{
910 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200911 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200918 if (number == 0 || number == 1)
919 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200920 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200921
922 // A 0 or 1 number can also be used as a bool.
923 if (type != NULL)
924 {
925 type->tt_type = VAR_NUMBER;
926 type->tt_flags = TTFLAG_BOOL_OK;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
928 }
929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 return OK;
931}
932
933/*
934 * Generate an ISN_PUSHBOOL instruction.
935 */
936 static int
937generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
940
Bram Moolenaar080457c2020-03-03 21:53:32 +0100941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
943 return FAIL;
944 isn->isn_arg.number = number;
945
946 return OK;
947}
948
949/*
950 * Generate an ISN_PUSHSPEC instruction.
951 */
952 static int
953generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
954{
955 isn_T *isn;
956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
959 return FAIL;
960 isn->isn_arg.number = number;
961
962 return OK;
963}
964
965#ifdef FEAT_FLOAT
966/*
967 * Generate an ISN_PUSHF instruction.
968 */
969 static int
970generate_PUSHF(cctx_T *cctx, float_T fnumber)
971{
972 isn_T *isn;
973
Bram Moolenaar080457c2020-03-03 21:53:32 +0100974 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
976 return FAIL;
977 isn->isn_arg.fnumber = fnumber;
978
979 return OK;
980}
981#endif
982
983/*
984 * Generate an ISN_PUSHS instruction.
985 * Consumes "str".
986 */
987 static int
988generate_PUSHS(cctx_T *cctx, char_u *str)
989{
990 isn_T *isn;
991
Bram Moolenaar080457c2020-03-03 21:53:32 +0100992 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
994 return FAIL;
995 isn->isn_arg.string = str;
996
997 return OK;
998}
999
1000/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001001 * Generate an ISN_PUSHCHANNEL instruction.
1002 * Consumes "channel".
1003 */
1004 static int
1005generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1006{
1007 isn_T *isn;
1008
Bram Moolenaar080457c2020-03-03 21:53:32 +01001009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001010 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1011 return FAIL;
1012 isn->isn_arg.channel = channel;
1013
1014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHJOB instruction.
1019 * Consumes "job".
1020 */
1021 static int
1022generate_PUSHJOB(cctx_T *cctx, job_T *job)
1023{
1024 isn_T *isn;
1025
Bram Moolenaar080457c2020-03-03 21:53:32 +01001026 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001027 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 return FAIL;
1029 isn->isn_arg.job = job;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 * Generate an ISN_PUSHBLOB instruction.
1036 * Consumes "blob".
1037 */
1038 static int
1039generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.blob = blob;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001052 * Generate an ISN_PUSHFUNC instruction with name "name".
1053 * Consumes "name".
1054 */
1055 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001056generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001057{
1058 isn_T *isn;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001061 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001063 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001064
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 * Generate an ISN_GETITEM instruction with "index".
1070 */
1071 static int
1072generate_GETITEM(cctx_T *cctx, int index)
1073{
1074 isn_T *isn;
1075 garray_T *stack = &cctx->ctx_type_stack;
1076 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1077 type_T *item_type = &t_any;
1078
1079 RETURN_OK_IF_SKIP(cctx);
1080
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001081 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001083 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 emsg(_(e_listreq));
1085 return FAIL;
1086 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = index;
1091
1092 // add the item type to the type stack
1093 if (ga_grow(stack, 1) == FAIL)
1094 return FAIL;
1095 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1096 ++stack->ga_len;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001101 * Generate an ISN_SLICE instruction with "count".
1102 */
1103 static int
1104generate_SLICE(cctx_T *cctx, int count)
1105{
1106 isn_T *isn;
1107
1108 RETURN_OK_IF_SKIP(cctx);
1109 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_CHECKLEN instruction with "min_len".
1117 */
1118 static int
1119generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124
1125 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.checklen.cl_min_len = min_len;
1128 isn->isn_arg.checklen.cl_more_OK = more_OK;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 * Generate an ISN_STORE instruction.
1135 */
1136 static int
1137generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1138{
1139 isn_T *isn;
1140
Bram Moolenaar080457c2020-03-03 21:53:32 +01001141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1143 return FAIL;
1144 if (name != NULL)
1145 isn->isn_arg.string = vim_strsave(name);
1146 else
1147 isn->isn_arg.number = idx;
1148
1149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1154 */
1155 static int
1156generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1157{
1158 isn_T *isn;
1159
Bram Moolenaar080457c2020-03-03 21:53:32 +01001160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1162 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001163 isn->isn_arg.storenr.stnr_idx = idx;
1164 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_STOREOPT instruction
1171 */
1172 static int
1173generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001178 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 return FAIL;
1180 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1181 isn->isn_arg.storeopt.so_flags = opt_flags;
1182
1183 return OK;
1184}
1185
1186/*
1187 * Generate an ISN_LOAD or similar instruction.
1188 */
1189 static int
1190generate_LOAD(
1191 cctx_T *cctx,
1192 isntype_T isn_type,
1193 int idx,
1194 char_u *name,
1195 type_T *type)
1196{
1197 isn_T *isn;
1198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1201 return FAIL;
1202 if (name != NULL)
1203 isn->isn_arg.string = vim_strsave(name);
1204 else
1205 isn->isn_arg.number = idx;
1206
1207 return OK;
1208}
1209
1210/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001211 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001212 */
1213 static int
1214generate_LOADV(
1215 cctx_T *cctx,
1216 char_u *name,
1217 int error)
1218{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001219 int di_flags;
1220 int vidx = find_vim_var(name, &di_flags);
1221 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 if (vidx < 0)
1225 {
1226 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001227 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228 return FAIL;
1229 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001230 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001232 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001233}
1234
1235/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001236 * Generate an ISN_UNLET instruction.
1237 */
1238 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001239generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001240{
1241 isn_T *isn;
1242
1243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001244 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001245 return FAIL;
1246 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1247 isn->isn_arg.unlet.ul_forceit = forceit;
1248
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001253 * Generate an ISN_LOCKCONST instruction.
1254 */
1255 static int
1256generate_LOCKCONST(cctx_T *cctx)
1257{
1258 isn_T *isn;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1262 return FAIL;
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_LOADS instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 int sid,
1275 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 if (isn_type == ISN_LOADS)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1287 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1294 */
1295 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int sid,
1300 int idx,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (isn_type == ISN_LOADSCRIPT)
1307 isn = generate_instr_type(cctx, isn_type, type);
1308 else
1309 isn = generate_instr_drop(cctx, isn_type, 1);
1310 if (isn == NULL)
1311 return FAIL;
1312 isn->isn_arg.script.script_sid = sid;
1313 isn->isn_arg.script.script_idx = idx;
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWLIST instruction.
1319 */
1320 static int
1321generate_NEWLIST(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
Bram Moolenaar127542b2020-08-09 17:22:04 +02001333 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001334 if (count == 0)
1335 member = &t_void;
1336 else
1337 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001338 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1339 cctx->ctx_type_list);
1340 type = get_list_type(member, cctx->ctx_type_list);
1341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 // drop the value types
1343 stack->ga_len -= count;
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // add the list type to the type stack
1346 if (ga_grow(stack, 1) == FAIL)
1347 return FAIL;
1348 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_NEWDICT instruction.
1356 */
1357 static int
1358generate_NEWDICT(cctx_T *cctx, int count)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 type_T *type;
1363 type_T *member;
1364
Bram Moolenaar080457c2020-03-03 21:53:32 +01001365 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.number = count;
1369
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001370 if (count == 0)
1371 member = &t_void;
1372 else
1373 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001374 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1375 cctx->ctx_type_list);
1376 type = get_dict_type(member, cctx->ctx_type_list);
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 // drop the key and value types
1379 stack->ga_len -= 2 * count;
1380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 // add the dict type to the type stack
1382 if (ga_grow(stack, 1) == FAIL)
1383 return FAIL;
1384 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
1391 * Generate an ISN_FUNCREF instruction.
1392 */
1393 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001394generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395{
1396 isn_T *isn;
1397 garray_T *stack = &cctx->ctx_type_stack;
1398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1401 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001402 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001403 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 if (ga_grow(stack, 1) == FAIL)
1406 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001407 ((type_T **)stack->ga_data)[stack->ga_len] =
1408 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 ++stack->ga_len;
1410
1411 return OK;
1412}
1413
1414/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001415 * Generate an ISN_NEWFUNC instruction.
1416 */
1417 static int
1418generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1419{
1420 isn_T *isn;
1421 char_u *name;
1422
1423 RETURN_OK_IF_SKIP(cctx);
1424 name = vim_strsave(lambda_name);
1425 if (name == NULL)
1426 return FAIL;
1427 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1428 return FAIL;
1429 isn->isn_arg.newfunc.nf_lambda = name;
1430 isn->isn_arg.newfunc.nf_global = func_name;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001436 * Generate an ISN_DEF instruction: list functions
1437 */
1438 static int
1439generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1440{
1441 isn_T *isn;
1442
1443 RETURN_OK_IF_SKIP(cctx);
1444 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1445 return FAIL;
1446 if (len > 0)
1447 {
1448 isn->isn_arg.string = vim_strnsave(name, len);
1449 if (isn->isn_arg.string == NULL)
1450 return FAIL;
1451 }
1452 return OK;
1453}
1454
1455/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 * Generate an ISN_JUMP instruction.
1457 */
1458 static int
1459generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1460{
1461 isn_T *isn;
1462 garray_T *stack = &cctx->ctx_type_stack;
1463
Bram Moolenaar080457c2020-03-03 21:53:32 +01001464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1466 return FAIL;
1467 isn->isn_arg.jump.jump_when = when;
1468 isn->isn_arg.jump.jump_where = where;
1469
1470 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1471 --stack->ga_len;
1472
1473 return OK;
1474}
1475
1476 static int
1477generate_FOR(cctx_T *cctx, int loop_idx)
1478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1484 return FAIL;
1485 isn->isn_arg.forloop.for_idx = loop_idx;
1486
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 // type doesn't matter, will be stored next
1490 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1491 ++stack->ga_len;
1492
1493 return OK;
1494}
1495
1496/*
1497 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001498 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 * Return FAIL if the number of arguments is wrong.
1500 */
1501 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001502generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001506 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001507 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001510 argoff = check_internal_func(func_idx, argcount);
1511 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 return FAIL;
1513
Bram Moolenaar389df252020-07-09 21:20:47 +02001514 if (method_call && argoff > 1)
1515 {
1516 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1517 return FAIL;
1518 isn->isn_arg.shuffle.shfl_item = argcount;
1519 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1520 }
1521
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001522 if (argcount > 0)
1523 {
1524 // Check the types of the arguments.
1525 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1526 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001527 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001528 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1531 return FAIL;
1532 isn->isn_arg.bfunc.cbf_idx = func_idx;
1533 isn->isn_arg.bfunc.cbf_argcount = argcount;
1534
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535 // Drop the argument types and push the return type.
1536 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (ga_grow(stack, 1) == FAIL)
1538 return FAIL;
1539 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001540 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001547 * Generate an ISN_LISTAPPEND instruction. Works like add().
1548 * Argument count is already checked.
1549 */
1550 static int
1551generate_LISTAPPEND(cctx_T *cctx)
1552{
1553 garray_T *stack = &cctx->ctx_type_stack;
1554 type_T *list_type;
1555 type_T *item_type;
1556 type_T *expected;
1557
1558 // Caller already checked that list_type is a list.
1559 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1560 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1561 expected = list_type->tt_member;
1562 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1563 return FAIL;
1564
1565 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1566 return FAIL;
1567
1568 --stack->ga_len; // drop the argument
1569 return OK;
1570}
1571
1572/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001573 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1574 * Argument count is already checked.
1575 */
1576 static int
1577generate_BLOBAPPEND(cctx_T *cctx)
1578{
1579 garray_T *stack = &cctx->ctx_type_stack;
1580 type_T *item_type;
1581
1582 // Caller already checked that blob_type is a blob.
1583 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1584 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1585 return FAIL;
1586
1587 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1588 return FAIL;
1589
1590 --stack->ga_len; // drop the argument
1591 return OK;
1592}
1593
1594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 * Generate an ISN_DCALL or ISN_UCALL instruction.
1596 * Return FAIL if the number of arguments is wrong.
1597 */
1598 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001599generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600{
1601 isn_T *isn;
1602 garray_T *stack = &cctx->ctx_type_stack;
1603 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001604 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if (argcount > regular_args && !has_varargs(ufunc))
1608 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001609 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return FAIL;
1611 }
1612 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1613 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001614 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 return FAIL;
1616 }
1617
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001618 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001619 {
1620 int i;
1621
1622 for (i = 0; i < argcount; ++i)
1623 {
1624 type_T *expected;
1625 type_T *actual;
1626
1627 if (i < regular_args)
1628 {
1629 if (ufunc->uf_arg_types == NULL)
1630 continue;
1631 expected = ufunc->uf_arg_types[i];
1632 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001633 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1634 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001635 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001636 else
1637 expected = ufunc->uf_va_type->tt_member;
1638 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001639 if (need_type(actual, expected, -argcount + i, cctx,
1640 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001641 {
1642 arg_type_mismatch(expected, actual, i + 1);
1643 return FAIL;
1644 }
1645 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001646 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001647 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1648 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001649 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001650 }
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001653 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001654 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001656 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 {
1658 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1659 isn->isn_arg.dfunc.cdf_argcount = argcount;
1660 }
1661 else
1662 {
1663 // A user function may be deleted and redefined later, can't use the
1664 // ufunc pointer, need to look it up again at runtime.
1665 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1666 isn->isn_arg.ufunc.cuf_argcount = argcount;
1667 }
1668
1669 stack->ga_len -= argcount; // drop the arguments
1670 if (ga_grow(stack, 1) == FAIL)
1671 return FAIL;
1672 // add return value
1673 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1674 ++stack->ga_len;
1675
1676 return OK;
1677}
1678
1679/*
1680 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1681 */
1682 static int
1683generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1684{
1685 isn_T *isn;
1686 garray_T *stack = &cctx->ctx_type_stack;
1687
Bram Moolenaar080457c2020-03-03 21:53:32 +01001688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1690 return FAIL;
1691 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1692 isn->isn_arg.ufunc.cuf_argcount = argcount;
1693
1694 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001695 if (ga_grow(stack, 1) == FAIL)
1696 return FAIL;
1697 // add return value
1698 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1699 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700
1701 return OK;
1702}
1703
1704/*
1705 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001706 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 */
1708 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001709generate_PCALL(
1710 cctx_T *cctx,
1711 int argcount,
1712 char_u *name,
1713 type_T *type,
1714 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715{
1716 isn_T *isn;
1717 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001718 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar080457c2020-03-03 21:53:32 +01001720 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001721
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001722 if (type->tt_type == VAR_ANY)
1723 ret_type = &t_any;
1724 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001725 {
1726 if (type->tt_argcount != -1)
1727 {
1728 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1729
1730 if (argcount < type->tt_min_argcount - varargs)
1731 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001732 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001733 return FAIL;
1734 }
1735 if (!varargs && argcount > type->tt_argcount)
1736 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001737 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001738 return FAIL;
1739 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001740 if (type->tt_args != NULL)
1741 {
1742 int i;
1743
1744 for (i = 0; i < argcount; ++i)
1745 {
1746 int offset = -argcount + i - 1;
1747 type_T *actual = ((type_T **)stack->ga_data)[
1748 stack->ga_len + offset];
1749 type_T *expected;
1750
1751 if (varargs && i >= type->tt_min_argcount - 1)
1752 expected = type->tt_args[
1753 type->tt_min_argcount - 1]->tt_member;
1754 else
1755 expected = type->tt_args[i];
1756 if (need_type(actual, expected, offset,
1757 cctx, TRUE, FALSE) == FAIL)
1758 {
1759 arg_type_mismatch(expected, actual, i + 1);
1760 return FAIL;
1761 }
1762 }
1763 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001764 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001765 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001766 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001767 else
1768 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001769 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001770 return FAIL;
1771 }
1772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.pfunc.cpf_top = at_top;
1776 isn->isn_arg.pfunc.cpf_argcount = argcount;
1777
1778 stack->ga_len -= argcount; // drop the arguments
1779
1780 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001781 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001783 // If partial is above the arguments it must be cleared and replaced with
1784 // the return value.
1785 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1786 return FAIL;
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return OK;
1789}
1790
1791/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001792 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 */
1794 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001795generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796{
1797 isn_T *isn;
1798 garray_T *stack = &cctx->ctx_type_stack;
1799 type_T *type;
1800
Bram Moolenaar080457c2020-03-03 21:53:32 +01001801 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001802 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001804 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001806 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001808 if (type->tt_type != VAR_DICT && type != &t_any)
1809 {
1810 emsg(_(e_dictreq));
1811 return FAIL;
1812 }
1813 // change dict type to dict member type
1814 if (type->tt_type == VAR_DICT)
1815 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
1817 return OK;
1818}
1819
1820/*
1821 * Generate an ISN_ECHO instruction.
1822 */
1823 static int
1824generate_ECHO(cctx_T *cctx, int with_white, int count)
1825{
1826 isn_T *isn;
1827
Bram Moolenaar080457c2020-03-03 21:53:32 +01001828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1830 return FAIL;
1831 isn->isn_arg.echo.echo_with_white = with_white;
1832 isn->isn_arg.echo.echo_count = count;
1833
1834 return OK;
1835}
1836
Bram Moolenaarad39c092020-02-26 18:23:43 +01001837/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001838 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001839 */
1840 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001841generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001842{
1843 isn_T *isn;
1844
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001845 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846 return FAIL;
1847 isn->isn_arg.number = count;
1848
1849 return OK;
1850}
1851
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001852/*
1853 * Generate an ISN_PUT instruction.
1854 */
1855 static int
1856generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1857{
1858 isn_T *isn;
1859
1860 RETURN_OK_IF_SKIP(cctx);
1861 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1862 return FAIL;
1863 isn->isn_arg.put.put_regname = regname;
1864 isn->isn_arg.put.put_lnum = lnum;
1865 return OK;
1866}
1867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 static int
1869generate_EXEC(cctx_T *cctx, char_u *line)
1870{
1871 isn_T *isn;
1872
Bram Moolenaar080457c2020-03-03 21:53:32 +01001873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1875 return FAIL;
1876 isn->isn_arg.string = vim_strsave(line);
1877 return OK;
1878}
1879
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001880 static int
1881generate_EXECCONCAT(cctx_T *cctx, int count)
1882{
1883 isn_T *isn;
1884
1885 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.number = count;
1888 return OK;
1889}
1890
Bram Moolenaar792f7862020-11-23 08:31:18 +01001891 static int
1892generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1893{
1894 isn_T *isn;
1895
1896 RETURN_OK_IF_SKIP(cctx);
1897 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1898 return FAIL;
1899 isn->isn_arg.unpack.unp_count = var_count;
1900 isn->isn_arg.unpack.unp_semicolon = semicolon;
1901 return OK;
1902}
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001906 */
1907 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001908generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001909{
1910 isn_T *isn;
1911
Bram Moolenaar02194d22020-10-24 23:08:38 +02001912 if (cmod->cmod_flags != 0
1913 || cmod->cmod_split != 0
1914 || cmod->cmod_verbose != 0
1915 || cmod->cmod_tab != 0
1916 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001917 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001918 cctx->ctx_has_cmdmod = TRUE;
1919
1920 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001921 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001922 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1923 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1924 return FAIL;
1925 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1926 // filter progam now belongs to the instruction
1927 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001928 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001929
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001930 return OK;
1931}
1932
1933 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001934generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001935{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001936 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1937 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001938 return OK;
1939}
1940
1941/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001943 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001945 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001946reserve_local(
1947 cctx_T *cctx,
1948 char_u *name,
1949 size_t len,
1950 int isConst,
1951 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 lvar_T *lvar;
1954
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001955 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001957 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001958 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 }
1960
1961 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001962 return NULL;
1963 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001964 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001966 // Every local variable uses the next entry on the stack. We could re-use
1967 // the last ones when leaving a scope, but then variables used in a closure
1968 // might get overwritten. To keep things simple do not re-use stack
1969 // entries. This is less efficient, but memory is cheap these days.
1970 lvar->lv_idx = cctx->ctx_locals_count++;
1971
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001972 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 lvar->lv_const = isConst;
1974 lvar->lv_type = type;
1975
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001976 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977}
1978
1979/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001980 * Remove local variables above "new_top".
1981 */
1982 static void
1983unwind_locals(cctx_T *cctx, int new_top)
1984{
1985 if (cctx->ctx_locals.ga_len > new_top)
1986 {
1987 int idx;
1988 lvar_T *lvar;
1989
1990 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1991 {
1992 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1993 vim_free(lvar->lv_name);
1994 }
1995 }
1996 cctx->ctx_locals.ga_len = new_top;
1997}
1998
1999/*
2000 * Free all local variables.
2001 */
2002 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002003free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002004{
2005 unwind_locals(cctx, 0);
2006 ga_clear(&cctx->ctx_locals);
2007}
2008
2009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 * Find "name" in script-local items of script "sid".
2011 * Returns the index in "sn_var_vals" if found.
2012 * If found but not in "sn_var_vals" returns -1.
2013 * If not found returns -2.
2014 */
2015 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002016get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017{
2018 hashtab_T *ht;
2019 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002020 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002021 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 int idx;
2023
Bram Moolenaare3d46852020-08-29 13:39:17 +02002024 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002026 if (sid == current_sctx.sc_sid)
2027 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002028 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002029
2030 if (sav == NULL)
2031 return -2;
2032 idx = sav->sav_var_vals_idx;
2033 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2034 if (check_writable && sv->sv_const)
2035 semsg(_(e_readonlyvar), name);
2036 return idx;
2037 }
2038
2039 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ht = &SCRIPT_VARS(sid);
2041 di = find_var_in_ht(ht, 0, name, TRUE);
2042 if (di == NULL)
2043 return -2;
2044
2045 // Now find the svar_T index in sn_var_vals.
2046 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2047 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002048 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 if (sv->sv_tv == &di->di_tv)
2050 {
2051 if (check_writable && sv->sv_const)
2052 semsg(_(e_readonlyvar), name);
2053 return idx;
2054 }
2055 }
2056 return -1;
2057}
2058
2059/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002060 * Find "name" in imported items of the current script or in "cctx" if not
2061 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 */
2063 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002064find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 int idx;
2067
Bram Moolenaare3d46852020-08-29 13:39:17 +02002068 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002069 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 if (cctx != NULL)
2071 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2072 {
2073 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2074 + idx;
2075
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002076 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2077 : STRLEN(import->imp_name) == len
2078 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 return import;
2080 }
2081
Bram Moolenaarefa94442020-08-08 22:16:00 +02002082 return find_imported_in_script(name, len, current_sctx.sc_sid);
2083}
2084
2085 imported_T *
2086find_imported_in_script(char_u *name, size_t len, int sid)
2087{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002088 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002089 int idx;
2090
Bram Moolenaare3d46852020-08-29 13:39:17 +02002091 if (!SCRIPT_ID_VALID(sid))
2092 return NULL;
2093 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2095 {
2096 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2097
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002098 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2099 : STRLEN(import->imp_name) == len
2100 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 return import;
2102 }
2103 return NULL;
2104}
2105
2106/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002107 * Free all imported variables.
2108 */
2109 static void
2110free_imported(cctx_T *cctx)
2111{
2112 int idx;
2113
2114 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2115 {
2116 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2117
2118 vim_free(import->imp_name);
2119 }
2120 ga_clear(&cctx->ctx_imports);
2121}
2122
2123/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002124 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002125 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002126 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002127vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002128{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002129 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002130}
2131
2132/*
2133 * Return a pointer to the next line that isn't empty or only contains a
2134 * comment. Skips over white space.
2135 * Returns NULL if there is none.
2136 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002137 char_u *
2138peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002139{
2140 int lnum = cctx->ctx_lnum;
2141
2142 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2143 {
2144 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002145 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002146
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002147 // ignore NULLs inserted for continuation lines
2148 if (line != NULL)
2149 {
2150 p = skipwhite(line);
2151 if (*p != NUL && !vim9_comment_start(p))
2152 return p;
2153 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154 }
2155 return NULL;
2156}
2157
2158/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002159 * Called when checking for a following operator at "arg". When the rest of
2160 * the line is empty or only a comment, peek the next line. If there is a next
2161 * line return a pointer to it and set "nextp".
2162 * Otherwise skip over white space.
2163 */
2164 static char_u *
2165may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2166{
2167 char_u *p = skipwhite(arg);
2168
2169 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002170 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002171 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002172 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002173 if (*nextp != NULL)
2174 return *nextp;
2175 }
2176 return p;
2177}
2178
2179/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002180 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002181 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002182 * Returns NULL when at the end.
2183 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002184 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002185next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002186{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002187 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002188
2189 do
2190 {
2191 ++cctx->ctx_lnum;
2192 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002193 {
2194 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002195 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002196 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002197 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002198 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002199 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002200 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002201 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002202 return line;
2203}
2204
2205/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002206 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002207 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002208 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2209 */
2210 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002211may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002212{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002213 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002214 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002215 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002216
2217 if (next == NULL)
2218 return FAIL;
2219 *arg = skipwhite(next);
2220 }
2221 return OK;
2222}
2223
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002224/*
2225 * Idem, and give an error when failed.
2226 */
2227 static int
2228may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2229{
2230 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2231 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002232 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002233 return FAIL;
2234 }
2235 return OK;
2236}
2237
2238
Bram Moolenaara5565e42020-05-09 15:44:01 +02002239// Structure passed between the compile_expr* functions to keep track of
2240// constants that have been parsed but for which no code was produced yet. If
2241// possible expressions on these constants are applied at compile time. If
2242// that is not possible, the code to push the constants needs to be generated
2243// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002244// Using 50 should be more than enough of 5 levels of ().
2245#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002246typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002247 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002248 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002249 int pp_is_const; // all generated code was constants, used for a
2250 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002251} ppconst_T;
2252
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002253static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002254static int compile_expr0(char_u **arg, cctx_T *cctx);
2255static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2256
Bram Moolenaara5565e42020-05-09 15:44:01 +02002257/*
2258 * Generate a PUSH instruction for "tv".
2259 * "tv" will be consumed or cleared.
2260 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2261 */
2262 static int
2263generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2264{
2265 if (tv != NULL)
2266 {
2267 switch (tv->v_type)
2268 {
2269 case VAR_UNKNOWN:
2270 break;
2271 case VAR_BOOL:
2272 generate_PUSHBOOL(cctx, tv->vval.v_number);
2273 break;
2274 case VAR_SPECIAL:
2275 generate_PUSHSPEC(cctx, tv->vval.v_number);
2276 break;
2277 case VAR_NUMBER:
2278 generate_PUSHNR(cctx, tv->vval.v_number);
2279 break;
2280#ifdef FEAT_FLOAT
2281 case VAR_FLOAT:
2282 generate_PUSHF(cctx, tv->vval.v_float);
2283 break;
2284#endif
2285 case VAR_BLOB:
2286 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2287 tv->vval.v_blob = NULL;
2288 break;
2289 case VAR_STRING:
2290 generate_PUSHS(cctx, tv->vval.v_string);
2291 tv->vval.v_string = NULL;
2292 break;
2293 default:
2294 iemsg("constant type not supported");
2295 clear_tv(tv);
2296 return FAIL;
2297 }
2298 tv->v_type = VAR_UNKNOWN;
2299 }
2300 return OK;
2301}
2302
2303/*
2304 * Generate code for any ppconst entries.
2305 */
2306 static int
2307generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2308{
2309 int i;
2310 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002311 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002312
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002313 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002314 for (i = 0; i < ppconst->pp_used; ++i)
2315 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2316 ret = FAIL;
2317 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002318 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002319 return ret;
2320}
2321
2322/*
2323 * Clear ppconst constants. Used when failing.
2324 */
2325 static void
2326clear_ppconst(ppconst_T *ppconst)
2327{
2328 int i;
2329
2330 for (i = 0; i < ppconst->pp_used; ++i)
2331 clear_tv(&ppconst->pp_tv[i]);
2332 ppconst->pp_used = 0;
2333}
2334
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002335/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002336 * Generate an instruction to load script-local variable "name", without the
2337 * leading "s:".
2338 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 */
2340 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002341compile_load_scriptvar(
2342 cctx_T *cctx,
2343 char_u *name, // variable NUL terminated
2344 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002345 char_u **end, // end of variable
2346 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002348 scriptitem_T *si;
2349 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 imported_T *import;
2351
Bram Moolenaare3d46852020-08-29 13:39:17 +02002352 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2353 return FAIL;
2354 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002355 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002356 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002358 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002359 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2360 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 }
2362 if (idx >= 0)
2363 {
2364 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2365
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002366 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 current_sctx.sc_sid, idx, sv->sv_type);
2368 return OK;
2369 }
2370
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002371 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (import != NULL)
2373 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002374 if (import->imp_all)
2375 {
2376 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002377 char_u *exp_name;
2378 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 ufunc_T *ufunc;
2380 type_T *type;
2381
2382 // Used "import * as Name", need to lookup the member.
2383 if (*p != '.')
2384 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002385 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002386 return FAIL;
2387 }
2388 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002389 if (VIM_ISWHITE(*p))
2390 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002391 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002392 return FAIL;
2393 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002394
Bram Moolenaar1c991142020-07-04 13:15:31 +02002395 // isolate one name
2396 exp_name = p;
2397 while (eval_isnamec(*p))
2398 ++p;
2399 cc = *p;
2400 *p = NUL;
2401
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002402 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002403 *p = cc;
2404 p = skipwhite(p);
2405
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002406 // TODO: what if it is a function?
2407 if (idx < 0)
2408 return FAIL;
2409 *end = p;
2410
2411 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2412 import->imp_sid,
2413 idx,
2414 type);
2415 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002416 else if (import->imp_funcname != NULL)
2417 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002418 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002419 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2420 import->imp_sid,
2421 import->imp_var_vals_idx,
2422 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 return OK;
2424 }
2425
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002426 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002427 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 return FAIL;
2429}
2430
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002431 static int
2432generate_funcref(cctx_T *cctx, char_u *name)
2433{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002434 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002435
2436 if (ufunc == NULL)
2437 return FAIL;
2438
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002439 // Need to compile any default values to get the argument types.
2440 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2441 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2442 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002443 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002444}
2445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446/*
2447 * Compile a variable name into a load instruction.
2448 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002449 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 * When "error" is FALSE do not give an error when not found.
2451 */
2452 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002453compile_load(
2454 char_u **arg,
2455 char_u *end_arg,
2456 cctx_T *cctx,
2457 int is_expr,
2458 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459{
2460 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002461 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002462 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002464 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465
2466 if (*(*arg + 1) == ':')
2467 {
2468 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002469 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002470 {
2471 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002473 switch (**arg)
2474 {
2475 case 'g': isn_type = ISN_LOADGDICT; break;
2476 case 'w': isn_type = ISN_LOADWDICT; break;
2477 case 't': isn_type = ISN_LOADTDICT; break;
2478 case 'b': isn_type = ISN_LOADBDICT; break;
2479 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002480 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002481 goto theend;
2482 }
2483 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2484 goto theend;
2485 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 else
2488 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002489 isntype_T isn_type = ISN_DROP;
2490
2491 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2492 if (name == NULL)
2493 return FAIL;
2494
2495 switch (**arg)
2496 {
2497 case 'v': res = generate_LOADV(cctx, name, error);
2498 break;
2499 case 's': res = compile_load_scriptvar(cctx, name,
2500 NULL, NULL, error);
2501 break;
2502 case 'g': isn_type = ISN_LOADG; break;
2503 case 'w': isn_type = ISN_LOADW; break;
2504 case 't': isn_type = ISN_LOADT; break;
2505 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002506 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002507 goto theend;
2508 }
2509 if (isn_type != ISN_DROP)
2510 {
2511 // Global, Buffer-local, Window-local and Tabpage-local
2512 // variables can be defined later, thus we don't check if it
2513 // exists, give error at runtime.
2514 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 }
2517 }
2518 else
2519 {
2520 size_t len = end - *arg;
2521 int idx;
2522 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002523 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
2525 name = vim_strnsave(*arg, end - *arg);
2526 if (name == NULL)
2527 return FAIL;
2528
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002529 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002531 if (!gen_load_outer)
2532 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534 else
2535 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002536 lvar_T *lvar = lookup_local(*arg, len, cctx);
2537
2538 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002540 type = lvar->lv_type;
2541 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002542 if (lvar->lv_from_outer)
2543 gen_load_outer = TRUE;
2544 else
2545 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 }
2547 else
2548 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002549 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002550 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002551 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002552 || find_imported(name, 0, cctx) != NULL)
2553 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002554
Bram Moolenaar0f769812020-09-12 18:32:34 +02002555 // When evaluating an expression and the name starts with an
2556 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002557 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002558 if (res == FAIL && is_expr
2559 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562 }
2563 if (gen_load)
2564 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002565 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002566 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002567 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002568 cctx->ctx_outer_used = TRUE;
2569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 }
2571
2572 *arg = end;
2573
2574theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002575 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002576 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 vim_free(name);
2578 return res;
2579}
2580
2581/*
2582 * Compile the argument expressions.
2583 * "arg" points to just after the "(" and is advanced to after the ")"
2584 */
2585 static int
2586compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2587{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002588 char_u *p = *arg;
2589 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002590 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591
Bram Moolenaare6085c52020-04-12 20:19:16 +02002592 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002594 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2595 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002596 if (*p == ')')
2597 {
2598 *arg = p + 1;
2599 return OK;
2600 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002601 if (must_end)
2602 {
2603 semsg(_(e_missing_comma_before_argument_str), p);
2604 return FAIL;
2605 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002606
Bram Moolenaara5565e42020-05-09 15:44:01 +02002607 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 return FAIL;
2609 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002610
2611 if (*p != ',' && *skipwhite(p) == ',')
2612 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002613 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002614 p = skipwhite(p);
2615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002617 {
2618 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002619 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002620 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002621 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002622 else
2623 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002624 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002625 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002627failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002628 emsg(_(e_missing_close));
2629 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630}
2631
2632/*
2633 * Compile a function call: name(arg1, arg2)
2634 * "arg" points to "name", "arg + varlen" to the "(".
2635 * "argcount_init" is 1 for "value->method()"
2636 * Instructions:
2637 * EVAL arg1
2638 * EVAL arg2
2639 * BCALL / DCALL / UCALL
2640 */
2641 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002642compile_call(
2643 char_u **arg,
2644 size_t varlen,
2645 cctx_T *cctx,
2646 ppconst_T *ppconst,
2647 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648{
2649 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002650 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 int argcount = argcount_init;
2652 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002653 char_u fname_buf[FLEN_FIXED + 1];
2654 char_u *tofree = NULL;
2655 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002656 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002657 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002658 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 // we can evaluate "has('name')" at compile time
2661 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2662 {
2663 char_u *s = skipwhite(*arg + varlen + 1);
2664 typval_T argvars[2];
2665
2666 argvars[0].v_type = VAR_UNKNOWN;
2667 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002668 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002669 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002670 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002671 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002672 if (*s == ')' && argvars[0].v_type == VAR_STRING
2673 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002674 {
2675 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2676
2677 *arg = s + 1;
2678 argvars[1].v_type = VAR_UNKNOWN;
2679 tv->v_type = VAR_NUMBER;
2680 tv->vval.v_number = 0;
2681 f_has(argvars, tv);
2682 clear_tv(&argvars[0]);
2683 ++ppconst->pp_used;
2684 return OK;
2685 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002686 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002687 }
2688
2689 if (generate_ppconst(cctx, ppconst) == FAIL)
2690 return FAIL;
2691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 if (varlen >= sizeof(namebuf))
2693 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002694 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 return FAIL;
2696 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002697 vim_strncpy(namebuf, *arg, varlen);
2698 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699
2700 *arg = skipwhite(*arg + varlen + 1);
2701 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002702 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703
Bram Moolenaara1773442020-08-12 15:21:22 +02002704 is_autoload = vim_strchr(name, '#') != NULL;
2705 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 {
2707 int idx;
2708
2709 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002710 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002712 {
2713 if (STRCMP(name, "add") == 0 && argcount == 2)
2714 {
2715 garray_T *stack = &cctx->ctx_type_stack;
2716 type_T *type = ((type_T **)stack->ga_data)[
2717 stack->ga_len - 2];
2718
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002719 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002720 if (type->tt_type == VAR_LIST)
2721 {
2722 // inline "add(list, item)" so that the type can be checked
2723 res = generate_LISTAPPEND(cctx);
2724 idx = -1;
2725 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002726 else if (type->tt_type == VAR_BLOB)
2727 {
2728 // inline "add(blob, nr)" so that the type can be checked
2729 res = generate_BLOBAPPEND(cctx);
2730 idx = -1;
2731 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002732 }
2733
2734 if (idx >= 0)
2735 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2736 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002737 else
2738 semsg(_(e_unknownfunc), namebuf);
2739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 }
2741
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002742 // An argument or local variable can be a function reference, this
2743 // overrules a function name.
2744 if (lookup_local(namebuf, varlen, cctx) == NULL
2745 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002746 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002747 // If we can find the function by name generate the right call.
2748 // Skip global functions here, a local funcref takes precedence.
2749 ufunc = find_func(name, FALSE, cctx);
2750 if (ufunc != NULL && !func_is_global(ufunc))
2751 {
2752 res = generate_CALL(cctx, ufunc, argcount);
2753 goto theend;
2754 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756
2757 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002758 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002759 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002761 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002762 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002763 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002764 garray_T *stack = &cctx->ctx_type_stack;
2765 type_T *type;
2766
2767 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2768 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002769 goto theend;
2770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771
Bram Moolenaar0f769812020-09-12 18:32:34 +02002772 // If we can find a global function by name generate the right call.
2773 if (ufunc != NULL)
2774 {
2775 res = generate_CALL(cctx, ufunc, argcount);
2776 goto theend;
2777 }
2778
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002779 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002780 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002781 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002782 res = generate_UCALL(cctx, name, argcount);
2783 else
2784 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002785
2786theend:
2787 vim_free(tofree);
2788 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789}
2790
2791// like NAMESPACE_CHAR but with 'a' and 'l'.
2792#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2793
2794/*
2795 * Find the end of a variable or function name. Unlike find_name_end() this
2796 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002797 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 * Return a pointer to just after the name. Equal to "arg" if there is no
2799 * valid name.
2800 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002801 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002802to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803{
2804 char_u *p;
2805
2806 // Quick check for valid starting character.
2807 if (!eval_isnamec1(*arg))
2808 return arg;
2809
2810 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2811 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2812 // and can be used in slice "[n:]".
2813 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002814 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2816 break;
2817 return p;
2818}
2819
2820/*
2821 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002822 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 */
2824 char_u *
2825to_name_const_end(char_u *arg)
2826{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002827 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 typval_T rettv;
2829
2830 if (p == arg && *arg == '[')
2831 {
2832
2833 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002834 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 p = arg;
2836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 return p;
2838}
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840/*
2841 * parse a list: [expr, expr]
2842 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002843 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 */
2845 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002846compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847{
2848 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002849 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002851 int is_const;
2852 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002854 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002856 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002857 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002858 semsg(_(e_list_end), *arg);
2859 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002860 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002861 if (*p == ',')
2862 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002863 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002864 return FAIL;
2865 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002866 if (*p == ']')
2867 {
2868 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002869 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002870 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002872 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002873 if (!is_const)
2874 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 ++count;
2876 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002877 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002879 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2880 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002881 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002882 return FAIL;
2883 }
2884 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002885 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 p = skipwhite(p);
2887 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002888 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002890 ppconst->pp_is_const = is_all_const;
2891 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892}
2893
2894/*
2895 * parse a lambda: {arg, arg -> expr}
2896 * "*arg" points to the '{'.
2897 */
2898 static int
2899compile_lambda(char_u **arg, cctx_T *cctx)
2900{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 typval_T rettv;
2902 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002903 evalarg_T evalarg;
2904
2905 CLEAR_FIELD(evalarg);
2906 evalarg.eval_flags = EVAL_EVALUATE;
2907 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
2909 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002910 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002911 {
2912 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002914 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002917 ++ufunc->uf_refcount;
2918 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919
2920 // The function will have one line: "return {expr}".
2921 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002922 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002924 clear_evalarg(&evalarg, NULL);
2925
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002926 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002927 {
2928 // The return type will now be known.
2929 set_function_type(ufunc);
2930
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002931 // The function reference count will be 1. When the ISN_FUNCREF
2932 // instruction is deleted the reference count is decremented and the
2933 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002934 return generate_FUNCREF(cctx, ufunc);
2935 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002936
2937 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 return FAIL;
2939}
2940
2941/*
2942 * Compile a lamda call: expr->{lambda}(args)
2943 * "arg" points to the "{".
2944 */
2945 static int
2946compile_lambda_call(char_u **arg, cctx_T *cctx)
2947{
2948 ufunc_T *ufunc;
2949 typval_T rettv;
2950 int argcount = 1;
2951 int ret = FAIL;
2952
2953 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002954 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 return FAIL;
2956
2957 if (**arg != '(')
2958 {
2959 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002960 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 else
2962 semsg(_(e_missing_paren), "lambda");
2963 clear_tv(&rettv);
2964 return FAIL;
2965 }
2966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 ufunc = rettv.vval.v_partial->pt_func;
2968 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002969 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002970 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002971
Bram Moolenaara05e5242020-09-19 18:19:19 +02002972 // The function will have one line: "return {expr}". Compile it into
2973 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002974 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975
2976 // compile the arguments
2977 *arg = skipwhite(*arg + 1);
2978 if (compile_arguments(arg, cctx, &argcount) == OK)
2979 // call the compiled function
2980 ret = generate_CALL(cctx, ufunc, argcount);
2981
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002982 if (ret == FAIL)
2983 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 return ret;
2985}
2986
2987/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002988 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002990 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 */
2992 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01002993compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994{
2995 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002996 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 int count = 0;
2998 dict_T *d = dict_alloc();
2999 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003000 char_u *whitep = *arg;
3001 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003002 int is_const;
3003 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004
3005 if (d == NULL)
3006 return FAIL;
3007 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003008 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003010 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011
Bram Moolenaar23c55272020-06-21 16:58:13 +02003012 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003013 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003014 *arg = NULL;
3015 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003016 }
3017
3018 if (**arg == '}')
3019 break;
3020
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003021 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003023 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003025 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003026 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003027 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3030 if (isn->isn_type == ISN_PUSHS)
3031 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003032 else
3033 {
3034 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003035 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003036 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003037 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003038 return FAIL;
3039 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003040 *arg = skipwhite(*arg);
3041 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003042 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003043 emsg(_(e_missing_matching_bracket_after_dict_key));
3044 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003045 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003046 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003048 else
3049 {
3050 // {"name": value},
3051 // {'name': value},
3052 // {name: value} use "name" as a literal key
3053 key = get_literal_key(arg);
3054 if (key == NULL)
3055 return FAIL;
3056 if (generate_PUSHS(cctx, key) == FAIL)
3057 return FAIL;
3058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
3060 // Check for duplicate keys, if using string keys.
3061 if (key != NULL)
3062 {
3063 item = dict_find(d, key, -1);
3064 if (item != NULL)
3065 {
3066 semsg(_(e_duplicate_key), key);
3067 goto failret;
3068 }
3069 item = dictitem_alloc(key);
3070 if (item != NULL)
3071 {
3072 item->di_tv.v_type = VAR_UNKNOWN;
3073 item->di_tv.v_lock = 0;
3074 if (dict_add(d, item) == FAIL)
3075 dictitem_free(item);
3076 }
3077 }
3078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 if (**arg != ':')
3080 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003081 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003082 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003083 else
3084 semsg(_(e_missing_dict_colon), *arg);
3085 return FAIL;
3086 }
3087 whitep = *arg + 1;
3088 if (!IS_WHITE_OR_NUL(*whitep))
3089 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003090 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 return FAIL;
3092 }
3093
3094 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003095 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003096 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003097 *arg = NULL;
3098 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003099 }
3100
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003101 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003103 if (!is_const)
3104 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 ++count;
3106
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003108 *arg = skipwhite(*arg);
3109 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003110 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003111 *arg = NULL;
3112 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 if (**arg == '}')
3115 break;
3116 if (**arg != ',')
3117 {
3118 semsg(_(e_missing_dict_comma), *arg);
3119 goto failret;
3120 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003121 if (IS_WHITE_OR_NUL(*whitep))
3122 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003123 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003124 return FAIL;
3125 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003126 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003127 if (!IS_WHITE_OR_NUL(*whitep))
3128 {
3129 semsg(_(e_white_space_required_after_str), ",");
3130 return FAIL;
3131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 *arg = skipwhite(*arg + 1);
3133 }
3134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 *arg = *arg + 1;
3136
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003137 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003138 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003139 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003140 *arg += STRLEN(*arg);
3141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003143 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 return generate_NEWDICT(cctx, count);
3145
3146failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003147 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003148 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003149 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003150 *arg = (char_u *)"";
3151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 dict_unref(d);
3153 return FAIL;
3154}
3155
3156/*
3157 * Compile "&option".
3158 */
3159 static int
3160compile_get_option(char_u **arg, cctx_T *cctx)
3161{
3162 typval_T rettv;
3163 char_u *start = *arg;
3164 int ret;
3165
3166 // parse the option and get the current value to get the type.
3167 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003168 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 if (ret == OK)
3170 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003171 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 char_u *name = vim_strnsave(start, *arg - start);
3173 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3174
3175 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3176 vim_free(name);
3177 }
3178 clear_tv(&rettv);
3179
3180 return ret;
3181}
3182
3183/*
3184 * Compile "$VAR".
3185 */
3186 static int
3187compile_get_env(char_u **arg, cctx_T *cctx)
3188{
3189 char_u *start = *arg;
3190 int len;
3191 int ret;
3192 char_u *name;
3193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 ++*arg;
3195 len = get_env_len(arg);
3196 if (len == 0)
3197 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003198 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return FAIL;
3200 }
3201
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003202 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 name = vim_strnsave(start, len + 1);
3204 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3205 vim_free(name);
3206 return ret;
3207}
3208
3209/*
3210 * Compile "@r".
3211 */
3212 static int
3213compile_get_register(char_u **arg, cctx_T *cctx)
3214{
3215 int ret;
3216
3217 ++*arg;
3218 if (**arg == NUL)
3219 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003220 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 return FAIL;
3222 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003223 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
3225 emsg_invreg(**arg);
3226 return FAIL;
3227 }
3228 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3229 ++*arg;
3230 return ret;
3231}
3232
3233/*
3234 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003235 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 */
3237 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003238apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003240 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
3242 // this works from end to start
3243 while (p > start)
3244 {
3245 --p;
3246 if (*p == '-' || *p == '+')
3247 {
3248 // only '-' has an effect, for '+' we only check the type
3249#ifdef FEAT_FLOAT
3250 if (rettv->v_type == VAR_FLOAT)
3251 {
3252 if (*p == '-')
3253 rettv->vval.v_float = -rettv->vval.v_float;
3254 }
3255 else
3256#endif
3257 {
3258 varnumber_T val;
3259 int error = FALSE;
3260
3261 // tv_get_number_chk() accepts a string, but we don't want that
3262 // here
3263 if (check_not_string(rettv) == FAIL)
3264 return FAIL;
3265 val = tv_get_number_chk(rettv, &error);
3266 clear_tv(rettv);
3267 if (error)
3268 return FAIL;
3269 if (*p == '-')
3270 val = -val;
3271 rettv->v_type = VAR_NUMBER;
3272 rettv->vval.v_number = val;
3273 }
3274 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003275 else if (numeric_only)
3276 {
3277 ++p;
3278 break;
3279 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003280 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 {
3282 int v = tv2bool(rettv);
3283
3284 // '!' is permissive in the type.
3285 clear_tv(rettv);
3286 rettv->v_type = VAR_BOOL;
3287 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3288 }
3289 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003290 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 return OK;
3292}
3293
3294/*
3295 * Recognize v: variables that are constants and set "rettv".
3296 */
3297 static void
3298get_vim_constant(char_u **arg, typval_T *rettv)
3299{
3300 if (STRNCMP(*arg, "v:true", 6) == 0)
3301 {
3302 rettv->v_type = VAR_BOOL;
3303 rettv->vval.v_number = VVAL_TRUE;
3304 *arg += 6;
3305 }
3306 else if (STRNCMP(*arg, "v:false", 7) == 0)
3307 {
3308 rettv->v_type = VAR_BOOL;
3309 rettv->vval.v_number = VVAL_FALSE;
3310 *arg += 7;
3311 }
3312 else if (STRNCMP(*arg, "v:null", 6) == 0)
3313 {
3314 rettv->v_type = VAR_SPECIAL;
3315 rettv->vval.v_number = VVAL_NULL;
3316 *arg += 6;
3317 }
3318 else if (STRNCMP(*arg, "v:none", 6) == 0)
3319 {
3320 rettv->v_type = VAR_SPECIAL;
3321 rettv->vval.v_number = VVAL_NONE;
3322 *arg += 6;
3323 }
3324}
3325
Bram Moolenaar696ba232020-07-29 21:20:41 +02003326 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003327get_compare_type(char_u *p, int *len, int *type_is)
3328{
3329 exptype_T type = EXPR_UNKNOWN;
3330 int i;
3331
3332 switch (p[0])
3333 {
3334 case '=': if (p[1] == '=')
3335 type = EXPR_EQUAL;
3336 else if (p[1] == '~')
3337 type = EXPR_MATCH;
3338 break;
3339 case '!': if (p[1] == '=')
3340 type = EXPR_NEQUAL;
3341 else if (p[1] == '~')
3342 type = EXPR_NOMATCH;
3343 break;
3344 case '>': if (p[1] != '=')
3345 {
3346 type = EXPR_GREATER;
3347 *len = 1;
3348 }
3349 else
3350 type = EXPR_GEQUAL;
3351 break;
3352 case '<': if (p[1] != '=')
3353 {
3354 type = EXPR_SMALLER;
3355 *len = 1;
3356 }
3357 else
3358 type = EXPR_SEQUAL;
3359 break;
3360 case 'i': if (p[1] == 's')
3361 {
3362 // "is" and "isnot"; but not a prefix of a name
3363 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3364 *len = 5;
3365 i = p[*len];
3366 if (!isalnum(i) && i != '_')
3367 {
3368 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3369 *type_is = TRUE;
3370 }
3371 }
3372 break;
3373 }
3374 return type;
3375}
3376
3377/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003379 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 */
3381 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003382compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003384 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385
3386 // this works from end to start
3387 while (p > start)
3388 {
3389 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003390 while (VIM_ISWHITE(*p))
3391 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 if (*p == '-' || *p == '+')
3393 {
3394 int negate = *p == '-';
3395 isn_T *isn;
3396
3397 // TODO: check type
3398 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3399 {
3400 --p;
3401 if (*p == '-')
3402 negate = !negate;
3403 }
3404 // only '-' has an effect, for '+' we only check the type
3405 if (negate)
3406 isn = generate_instr(cctx, ISN_NEGATENR);
3407 else
3408 isn = generate_instr(cctx, ISN_CHECKNR);
3409 if (isn == NULL)
3410 return FAIL;
3411 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003412 else if (numeric_only)
3413 {
3414 ++p;
3415 break;
3416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 else
3418 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003419 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003421 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003423 if (p[-1] == '!')
3424 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
3427 if (generate_2BOOL(cctx, invert) == FAIL)
3428 return FAIL;
3429 }
3430 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003431 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 return OK;
3433}
3434
3435/*
3436 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003437 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 */
3439 static int
3440compile_subscript(
3441 char_u **arg,
3442 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003443 char_u *start_leader,
3444 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003445 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003447 char_u *name_start = *end_leader;
3448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 for (;;)
3450 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003451 char_u *p = skipwhite(*arg);
3452
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003453 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003454 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003455 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003456
3457 // If a following line starts with "->{" or "->X" advance to that
3458 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003459 // Also if a following line starts with ".x".
3460 if (next != NULL &&
3461 ((next[0] == '-' && next[1] == '>'
3462 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003463 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003464 {
3465 next = next_line_from_context(cctx, TRUE);
3466 if (next == NULL)
3467 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003468 *arg = next;
3469 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003470 }
3471 }
3472
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003473 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3474 // not a function call.
3475 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003477 garray_T *stack = &cctx->ctx_type_stack;
3478 type_T *type;
3479 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003481 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003482 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003483 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003486 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3487
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003488 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3490 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003491 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 return FAIL;
3493 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003494 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003496 char_u *pstart = p;
3497
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003498 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003499 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003500 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 // something->method()
3503 // Apply the '!', '-' and '+' first:
3504 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003505 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003508 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003509 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003510 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 if (**arg == '{')
3512 {
3513 // lambda call: list->{lambda}
3514 if (compile_lambda_call(arg, cctx) == FAIL)
3515 return FAIL;
3516 }
3517 else
3518 {
3519 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003520 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003521 if (!eval_isnamec1(*p))
3522 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003523 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003524 return FAIL;
3525 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003526 if (ASCII_ISALPHA(*p) && p[1] == ':')
3527 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003528 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 ;
3530 if (*p != '(')
3531 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003532 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 return FAIL;
3534 }
3535 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003536 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 return FAIL;
3538 }
3539 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003540 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003542 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003543 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003544 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003545 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003546 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003547
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003548 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003549 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003550 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003551 // TODO: blob index
3552 // TODO: more arguments
3553 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003554 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003555 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003556 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003557
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003558 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003559 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003560 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003561 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003562 if (**arg == ':')
3563 // missing first index is equal to zero
3564 generate_PUSHNR(cctx, 0);
3565 else
3566 {
3567 if (compile_expr0(arg, cctx) == FAIL)
3568 return FAIL;
3569 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3570 return FAIL;
3571 *arg = skipwhite(*arg);
3572 }
3573 if (**arg == ':')
3574 {
3575 *arg = skipwhite(*arg + 1);
3576 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3577 return FAIL;
3578 if (**arg == ']')
3579 // missing second index is equal to end of string
3580 generate_PUSHNR(cctx, -1);
3581 else
3582 {
3583 if (compile_expr0(arg, cctx) == FAIL)
3584 return FAIL;
3585 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3586 return FAIL;
3587 *arg = skipwhite(*arg);
3588 }
3589 is_slice = TRUE;
3590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
3592 if (**arg != ']')
3593 {
3594 emsg(_(e_missbrac));
3595 return FAIL;
3596 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003597 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003599 // We can index a list and a dict. If we don't know the type
3600 // we can use the index value type.
3601 // TODO: If we don't know use an instruction to figure it out at
3602 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003603 typep = ((type_T **)stack->ga_data) + stack->ga_len
3604 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003605 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003606 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3607 // If the index is a string, the variable must be a Dict.
3608 if (*typep == &t_any && valtype == &t_string)
3609 vtype = VAR_DICT;
3610 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003611 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003612 if (need_type(valtype, &t_number, -1, cctx,
3613 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003614 return FAIL;
3615 if (is_slice)
3616 {
3617 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003618 if (need_type(valtype, &t_number, -2, cctx,
3619 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003620 return FAIL;
3621 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003622 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003623
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003624 if (vtype == VAR_DICT)
3625 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 if (is_slice)
3627 {
3628 emsg(_(e_cannot_slice_dictionary));
3629 return FAIL;
3630 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003631 if ((*typep)->tt_type == VAR_DICT)
3632 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003633 else
3634 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003635 if (need_type(*typep, &t_dict_any, -2, cctx,
3636 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003637 return FAIL;
3638 *typep = &t_any;
3639 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003640 if (may_generate_2STRING(-1, cctx) == FAIL)
3641 return FAIL;
3642 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3643 return FAIL;
3644 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003645 else if (vtype == VAR_STRING)
3646 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003647 *typep = &t_string;
3648 if ((is_slice
3649 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3650 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003651 return FAIL;
3652 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003653 else if (vtype == VAR_BLOB)
3654 {
3655 emsg("Sorry, blob index and slice not implemented yet");
3656 return FAIL;
3657 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003658 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003659 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003660 if (is_slice)
3661 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003662 if (generate_instr_drop(cctx,
3663 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3664 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003665 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003666 }
Bram Moolenaared591872020-08-15 22:14:53 +02003667 else
3668 {
3669 if ((*typep)->tt_type == VAR_LIST)
3670 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003671 if (generate_instr_drop(cctx,
3672 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3673 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003674 return FAIL;
3675 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003676 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003677 else
3678 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003679 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003680 return FAIL;
3681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003683 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003685 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003686 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003687 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003688 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003690 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003691 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003692 {
3693 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003694 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003695 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003696 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003697 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 while (eval_isnamec(*p))
3699 MB_PTR_ADV(p);
3700 if (p == *arg)
3701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003702 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 return FAIL;
3704 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003705 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 return FAIL;
3707 *arg = p;
3708 }
3709 else
3710 break;
3711 }
3712
3713 // TODO - see handle_subscript():
3714 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3715 // Don't do this when "Func" is already a partial that was bound
3716 // explicitly (pt_auto is FALSE).
3717
3718 return OK;
3719}
3720
3721/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003722 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3723 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003725 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003726 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003727 *
3728 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 */
3730
3731/*
3732 * number number constant
3733 * 0zFFFFFFFF Blob constant
3734 * "string" string constant
3735 * 'string' literal string constant
3736 * &option-name option value
3737 * @r register contents
3738 * identifier variable value
3739 * function() function call
3740 * $VAR environment variable
3741 * (expression) nested expression
3742 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003743 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 *
3745 * Also handle:
3746 * ! in front logical NOT
3747 * - in front unary minus
3748 * + in front unary plus (ignored)
3749 * trailing (arg) funcref/partial call
3750 * trailing [] subscript in String or List
3751 * trailing .name entry in Dictionary
3752 * trailing ->name() method call
3753 */
3754 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003755compile_expr7(
3756 char_u **arg,
3757 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003758 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 char_u *start_leader, *end_leader;
3761 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003762 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003763 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003765 ppconst->pp_is_const = FALSE;
3766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 /*
3768 * Skip '!', '-' and '+' characters. They are handled later.
3769 */
3770 start_leader = *arg;
3771 while (**arg == '!' || **arg == '-' || **arg == '+')
3772 *arg = skipwhite(*arg + 1);
3773 end_leader = *arg;
3774
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003775 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 switch (**arg)
3777 {
3778 /*
3779 * Number constant.
3780 */
3781 case '0': // also for blob starting with 0z
3782 case '1':
3783 case '2':
3784 case '3':
3785 case '4':
3786 case '5':
3787 case '6':
3788 case '7':
3789 case '8':
3790 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003791 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003793 // Apply "-" and "+" just before the number now, right to
3794 // left. Matters especially when "->" follows. Stops at
3795 // '!'.
3796 if (apply_leader(rettv, TRUE,
3797 start_leader, &end_leader) == FAIL)
3798 {
3799 clear_tv(rettv);
3800 return FAIL;
3801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 break;
3803
3804 /*
3805 * String constant: "string".
3806 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003807 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 return FAIL;
3809 break;
3810
3811 /*
3812 * Literal string constant: 'str''ing'.
3813 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003814 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 return FAIL;
3816 break;
3817
3818 /*
3819 * Constant Vim variable.
3820 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003821 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 ret = NOTDONE;
3823 break;
3824
3825 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003826 * "true" constant
3827 */
3828 case 't': if (STRNCMP(*arg, "true", 4) == 0
3829 && !eval_isnamec((*arg)[4]))
3830 {
3831 *arg += 4;
3832 rettv->v_type = VAR_BOOL;
3833 rettv->vval.v_number = VVAL_TRUE;
3834 }
3835 else
3836 ret = NOTDONE;
3837 break;
3838
3839 /*
3840 * "false" constant
3841 */
3842 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3843 && !eval_isnamec((*arg)[5]))
3844 {
3845 *arg += 5;
3846 rettv->v_type = VAR_BOOL;
3847 rettv->vval.v_number = VVAL_FALSE;
3848 }
3849 else
3850 ret = NOTDONE;
3851 break;
3852
3853 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 * List: [expr, expr]
3855 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003856 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 break;
3858
3859 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 * Lambda: {arg, arg -> expr}
3861 * Dictionary: {'key': val, 'key': val}
3862 */
3863 case '{': {
3864 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003865 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
3867 // Find out what comes after the arguments.
3868 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003869 &ga_arg, TRUE, NULL, NULL,
3870 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 if (ret != FAIL && *start == '>')
3872 ret = compile_lambda(arg, cctx);
3873 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003874 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876 break;
3877
3878 /*
3879 * Option value: &name
3880 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003881 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3882 return FAIL;
3883 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 break;
3885
3886 /*
3887 * Environment variable: $VAR.
3888 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003889 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3890 return FAIL;
3891 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 break;
3893
3894 /*
3895 * Register contents: @r.
3896 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003897 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3898 return FAIL;
3899 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 break;
3901 /*
3902 * nested expression: (expression).
3903 */
3904 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003905
3906 // recursive!
3907 if (ppconst->pp_used <= PPSIZE - 10)
3908 {
3909 ret = compile_expr1(arg, cctx, ppconst);
3910 }
3911 else
3912 {
3913 // Not enough space in ppconst, flush constants.
3914 if (generate_ppconst(cctx, ppconst) == FAIL)
3915 return FAIL;
3916 ret = compile_expr0(arg, cctx);
3917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 *arg = skipwhite(*arg);
3919 if (**arg == ')')
3920 ++*arg;
3921 else if (ret == OK)
3922 {
3923 emsg(_(e_missing_close));
3924 ret = FAIL;
3925 }
3926 break;
3927
3928 default: ret = NOTDONE;
3929 break;
3930 }
3931 if (ret == FAIL)
3932 return FAIL;
3933
Bram Moolenaar1c747212020-05-09 18:28:34 +02003934 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003936 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 clear_tv(rettv);
3938 else
3939 // A constant expression can possibly be handled compile time,
3940 // return the value instead of generating code.
3941 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 }
3943 else if (ret == NOTDONE)
3944 {
3945 char_u *p;
3946 int r;
3947
3948 if (!eval_isnamec1(**arg))
3949 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003950 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 return FAIL;
3952 }
3953
3954 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003955 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003957 {
3958 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 {
3962 if (generate_ppconst(cctx, ppconst) == FAIL)
3963 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003964 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 if (r == FAIL)
3967 return FAIL;
3968 }
3969
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003970 // Handle following "[]", ".member", etc.
3971 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003972 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973 ppconst) == FAIL)
3974 return FAIL;
3975 if (ppconst->pp_used > 0)
3976 {
3977 // apply the '!', '-' and '+' before the constant
3978 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003979 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003980 return FAIL;
3981 return OK;
3982 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003983 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003985 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986}
3987
3988/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003989 * Give the "white on both sides" error, taking the operator from "p[len]".
3990 */
3991 void
3992error_white_both(char_u *op, int len)
3993{
3994 char_u buf[10];
3995
3996 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003997 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003998}
3999
4000/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004001 * <type>expr7: runtime type check / conversion
4002 */
4003 static int
4004compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4005{
4006 type_T *want_type = NULL;
4007
4008 // Recognize <type>
4009 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4010 {
4011 int called_emsg_before = called_emsg;
4012
4013 ++*arg;
4014 want_type = parse_type(arg, cctx->ctx_type_list);
4015 if (called_emsg != called_emsg_before)
4016 return FAIL;
4017
4018 if (**arg != '>')
4019 {
4020 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004021 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004022 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004023 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004024 return FAIL;
4025 }
4026 ++*arg;
4027 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4028 return FAIL;
4029 }
4030
4031 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4032 return FAIL;
4033
4034 if (want_type != NULL)
4035 {
4036 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004037 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004038
Bram Moolenaard1103582020-08-14 22:44:25 +02004039 generate_ppconst(cctx, ppconst);
4040 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004041 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004042 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004043 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004044 return FAIL;
4045 }
4046 }
4047
4048 return OK;
4049}
4050
4051/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 * * number multiplication
4053 * / number division
4054 * % number modulo
4055 */
4056 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058{
4059 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004060 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004061 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004063 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004064 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 return FAIL;
4066
4067 /*
4068 * Repeat computing, until no "*", "/" or "%" is following.
4069 */
4070 for (;;)
4071 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004072 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 if (*op != '*' && *op != '/' && *op != '%')
4074 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004075 if (next != NULL)
4076 {
4077 *arg = next_line_from_context(cctx, TRUE);
4078 op = skipwhite(*arg);
4079 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004080
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004081 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004083 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004084 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 }
4086 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004087 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004088 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004090 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004091 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093
4094 if (ppconst->pp_used == ppconst_used + 2
4095 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4096 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004097 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004098 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4099 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 // both are numbers: compute the result
4103 switch (*op)
4104 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004107 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004108 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004109 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004110 break;
4111 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004112 tv1->vval.v_number = res;
4113 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004114 }
4115 else
4116 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004117 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004118 generate_two_op(cctx, op);
4119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * + number addition
4127 * - number subtraction
4128 * .. string concatenation
4129 */
4130 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132{
4133 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004134 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137
4138 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 return FAIL;
4141
4142 /*
4143 * Repeat computing, until no "+", "-" or ".." is following.
4144 */
4145 for (;;)
4146 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004147 op = may_peek_next_line(cctx, *arg, &next);
4148 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 break;
4150 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004151 if (next != NULL)
4152 {
4153 *arg = next_line_from_context(cctx, TRUE);
4154 op = skipwhite(*arg);
4155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004157 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004159 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004160 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 }
4162
4163 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004164 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004165 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004168 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 return FAIL;
4170
Bram Moolenaara5565e42020-05-09 15:44:01 +02004171 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004172 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4174 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4175 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4176 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004178 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4179 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004180
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004181 // concat/subtract/add constant numbers
4182 if (*op == '+')
4183 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4184 else if (*op == '-')
4185 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4186 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004188 // concatenate constant strings
4189 char_u *s1 = tv1->vval.v_string;
4190 char_u *s2 = tv2->vval.v_string;
4191 size_t len1 = STRLEN(s1);
4192
4193 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4194 if (tv1->vval.v_string == NULL)
4195 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004196 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004197 return FAIL;
4198 }
4199 mch_memmove(tv1->vval.v_string, s1, len1);
4200 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201 vim_free(s1);
4202 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004203 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 if (*op == '.')
4210 {
4211 if (may_generate_2STRING(-2, cctx) == FAIL
4212 || may_generate_2STRING(-1, cctx) == FAIL)
4213 return FAIL;
4214 generate_instr_drop(cctx, ISN_CONCAT, 1);
4215 }
4216 else
4217 generate_two_op(cctx, op);
4218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * expr5a == expr5b
4226 * expr5a =~ expr5b
4227 * expr5a != expr5b
4228 * expr5a !~ expr5b
4229 * expr5a > expr5b
4230 * expr5a >= expr5b
4231 * expr5a < expr5b
4232 * expr5a <= expr5b
4233 * expr5a is expr5b
4234 * expr5a isnot expr5b
4235 *
4236 * Produces instructions:
4237 * EVAL expr5a Push result of "expr5a"
4238 * EVAL expr5b Push result of "expr5b"
4239 * COMPARE one of the compare instructions
4240 */
4241 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004242compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243{
4244 exptype_T type = EXPR_UNKNOWN;
4245 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004246 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004249 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
4251 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 return FAIL;
4254
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004255 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004256 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
4258 /*
4259 * If there is a comparative operator, use it.
4260 */
4261 if (type != EXPR_UNKNOWN)
4262 {
4263 int ic = FALSE; // Default: do not ignore case
4264
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004265 if (next != NULL)
4266 {
4267 *arg = next_line_from_context(cctx, TRUE);
4268 p = skipwhite(*arg);
4269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 if (type_is && (p[len] == '?' || p[len] == '#'))
4271 {
4272 semsg(_(e_invexpr2), *arg);
4273 return FAIL;
4274 }
4275 // extra question mark appended: ignore case
4276 if (p[len] == '?')
4277 {
4278 ic = TRUE;
4279 ++len;
4280 }
4281 // extra '#' appended: match case (ignored)
4282 else if (p[len] == '#')
4283 ++len;
4284 // nothing appended: match case
4285
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004286 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004288 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004289 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291
4292 // get the second variable
4293 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004294 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004295 return FAIL;
4296
Bram Moolenaara5565e42020-05-09 15:44:01 +02004297 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 return FAIL;
4299
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300 if (ppconst->pp_used == ppconst_used + 2)
4301 {
4302 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4303 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4304 int ret;
4305
4306 // Both sides are a constant, compute the result now.
4307 // First check for a valid combination of types, this is more
4308 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004309 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004310 ret = FAIL;
4311 else
4312 {
4313 ret = typval_compare(tv1, tv2, type, ic);
4314 tv1->v_type = VAR_BOOL;
4315 tv1->vval.v_number = tv1->vval.v_number
4316 ? VVAL_TRUE : VVAL_FALSE;
4317 clear_tv(tv2);
4318 --ppconst->pp_used;
4319 }
4320 return ret;
4321 }
4322
4323 generate_ppconst(cctx, ppconst);
4324 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
4326
4327 return OK;
4328}
4329
Bram Moolenaar7f141552020-05-09 17:35:53 +02004330static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332/*
4333 * Compile || or &&.
4334 */
4335 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336compile_and_or(
4337 char_u **arg,
4338 cctx_T *cctx,
4339 char *op,
4340 ppconst_T *ppconst,
4341 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004343 char_u *next;
4344 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 int opchar = *op;
4346
4347 if (p[0] == opchar && p[1] == opchar)
4348 {
4349 garray_T *instr = &cctx->ctx_instr;
4350 garray_T end_ga;
4351
4352 /*
4353 * Repeat until there is no following "||" or "&&"
4354 */
4355 ga_init2(&end_ga, sizeof(int), 10);
4356 while (p[0] == opchar && p[1] == opchar)
4357 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358 if (next != NULL)
4359 {
4360 *arg = next_line_from_context(cctx, TRUE);
4361 p = skipwhite(*arg);
4362 }
4363
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004364 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4365 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004366 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004367 return FAIL;
4368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004370 // TODO: use ppconst if the value is a constant and check
4371 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 generate_ppconst(cctx, ppconst);
4373
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004374 // Every part must evaluate to a bool.
4375 if (bool_on_stack(cctx) == FAIL)
4376 {
4377 ga_clear(&end_ga);
4378 return FAIL;
4379 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 if (ga_grow(&end_ga, 1) == FAIL)
4382 {
4383 ga_clear(&end_ga);
4384 return FAIL;
4385 }
4386 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4387 ++end_ga.ga_len;
4388 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004389 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
4391 // eval the next expression
4392 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004393 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004394 return FAIL;
4395
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4397 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
4399 ga_clear(&end_ga);
4400 return FAIL;
4401 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004402
4403 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004407 // Every part must evaluate to a bool.
4408 if (bool_on_stack(cctx) == FAIL)
4409 {
4410 ga_clear(&end_ga);
4411 return FAIL;
4412 }
4413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 // Fill in the end label in all jumps.
4415 while (end_ga.ga_len > 0)
4416 {
4417 isn_T *isn;
4418
4419 --end_ga.ga_len;
4420 isn = ((isn_T *)instr->ga_data)
4421 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4422 isn->isn_arg.jump.jump_where = instr->ga_len;
4423 }
4424 ga_clear(&end_ga);
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * expr4a && expr4a && expr4a logical AND
4432 *
4433 * Produces instructions:
4434 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004435 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004436 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004438 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 * EVAL expr4c Push result of "expr4c"
4440 * end:
4441 */
4442 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 int ppconst_used = ppconst->pp_used;
4446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 return FAIL;
4450
4451 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453}
4454
4455/*
4456 * expr3a || expr3b || expr3c logical OR
4457 *
4458 * Produces instructions:
4459 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004460 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004461 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004463 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 * EVAL expr3c Push result of "expr3c"
4465 * end:
4466 */
4467 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 int ppconst_used = ppconst->pp_used;
4471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 return FAIL;
4475
4476 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478}
4479
4480/*
4481 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004483 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 * JUMP_IF_FALSE alt jump if false
4485 * EVAL expr1a
4486 * JUMP_ALWAYS end
4487 * alt: EVAL expr1b
4488 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004489 *
4490 * Toplevel expression: expr2 ?? expr1
4491 * Produces instructions:
4492 * EVAL expr2 Push result of "expr2"
4493 * JUMP_AND_KEEP_IF_TRUE end jump if true
4494 * EVAL expr1
4495 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 */
4497 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499{
4500 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004502 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
Bram Moolenaar3988f642020-08-27 22:43:03 +02004504 // Ignore all kinds of errors when not producing code.
4505 if (cctx->ctx_skip == SKIP_YES)
4506 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004507 evalarg_T evalarg;
4508
4509 CLEAR_FIELD(evalarg);
4510 evalarg.eval_cctx = cctx;
4511 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004512 return OK;
4513 }
4514
Bram Moolenaar61a89812020-05-07 16:58:17 +02004515 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 return FAIL;
4518
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 if (*p == '?')
4521 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004522 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 garray_T *instr = &cctx->ctx_instr;
4524 garray_T *stack = &cctx->ctx_type_stack;
4525 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004526 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004528 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 int has_const_expr = FALSE;
4530 int const_value = FALSE;
4531 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004533 if (next != NULL)
4534 {
4535 *arg = next_line_from_context(cctx, TRUE);
4536 p = skipwhite(*arg);
4537 }
4538
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004539 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004540 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004541 semsg(_(e_white_space_required_before_and_after_str),
4542 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004543 return FAIL;
4544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
Bram Moolenaara5565e42020-05-09 15:44:01 +02004546 if (ppconst->pp_used == ppconst_used + 1)
4547 {
4548 // the condition is a constant, we know whether the ? or the :
4549 // expression is to be evaluated.
4550 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004551 if (op_falsy)
4552 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4553 else
4554 {
4555 int error = FALSE;
4556
4557 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4558 &error);
4559 if (error)
4560 return FAIL;
4561 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004562 cctx->ctx_skip = save_skip == SKIP_YES ||
4563 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4564
4565 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4566 // "left ?? right" and "left" is truthy: produce "left"
4567 generate_ppconst(cctx, ppconst);
4568 else
4569 {
4570 clear_tv(&ppconst->pp_tv[ppconst_used]);
4571 --ppconst->pp_used;
4572 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 }
4574 else
4575 {
4576 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004577 if (op_falsy)
4578 end_idx = instr->ga_len;
4579 generate_JUMP(cctx, op_falsy
4580 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4581 if (op_falsy)
4582 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584
4585 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004586 *arg = skipwhite(p + 1 + op_falsy);
4587 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004588 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004590 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 if (!has_const_expr)
4593 {
4594 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004596 if (!op_falsy)
4597 {
4598 // remember the type and drop it
4599 --stack->ga_len;
4600 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004602 end_idx = instr->ga_len;
4603 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004605 // jump here from JUMP_IF_FALSE
4606 isn = ((isn_T *)instr->ga_data) + alt_idx;
4607 isn->isn_arg.jump.jump_where = instr->ga_len;
4608 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004611 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004613 // Check for the ":".
4614 p = may_peek_next_line(cctx, *arg, &next);
4615 if (*p != ':')
4616 {
4617 emsg(_(e_missing_colon));
4618 return FAIL;
4619 }
4620 if (next != NULL)
4621 {
4622 *arg = next_line_from_context(cctx, TRUE);
4623 p = skipwhite(*arg);
4624 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004626 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4627 {
4628 semsg(_(e_white_space_required_before_and_after_str), ":");
4629 return FAIL;
4630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004632 // evaluate the third expression
4633 if (has_const_expr)
4634 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004635 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004636 *arg = skipwhite(p + 1);
4637 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4638 return FAIL;
4639 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4640 return FAIL;
4641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 if (!has_const_expr)
4644 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004645 type_T **typep;
4646
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004650 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4651 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004653 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 isn = ((isn_T *)instr->ga_data) + end_idx;
4655 isn->isn_arg.jump.jump_where = instr->ga_len;
4656 }
4657
4658 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 }
4660 return OK;
4661}
4662
4663/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004664 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004665 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4666 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 */
4668 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004669compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670{
4671 ppconst_T ppconst;
4672
4673 CLEAR_FIELD(ppconst);
4674 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4675 {
4676 clear_ppconst(&ppconst);
4677 return FAIL;
4678 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004679 if (is_const != NULL)
4680 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 if (generate_ppconst(cctx, &ppconst) == FAIL)
4682 return FAIL;
4683 return OK;
4684}
4685
4686/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004687 * Toplevel expression.
4688 */
4689 static int
4690compile_expr0(char_u **arg, cctx_T *cctx)
4691{
4692 return compile_expr0_ext(arg, cctx, NULL);
4693}
4694
4695/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 * compile "return [expr]"
4697 */
4698 static char_u *
4699compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4700{
4701 char_u *p = arg;
4702 garray_T *stack = &cctx->ctx_type_stack;
4703 type_T *stack_type;
4704
4705 if (*p != NUL && *p != '|' && *p != '\n')
4706 {
4707 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004708 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 return NULL;
4710
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004711 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004712 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004713 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4714 if (set_return_type)
4715 cctx->ctx_ufunc->uf_ret_type = stack_type;
4716 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004717 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004718 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4719 && stack_type->tt_type != VAR_VOID
4720 && stack_type->tt_type != VAR_UNKNOWN)
4721 {
4722 emsg(_(e_returning_value_in_function_without_return_type));
4723 return NULL;
4724 }
4725 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004726 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004727 return NULL;
4728 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 }
4731 else
4732 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004733 // "set_return_type" cannot be TRUE, only used for a lambda which
4734 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004735 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4736 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004738 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 return NULL;
4740 }
4741
4742 // No argument, return zero.
4743 generate_PUSHNR(cctx, 0);
4744 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004745 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 return NULL;
4747
4748 // "return val | endif" is possible
4749 return skipwhite(p);
4750}
4751
4752/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004753 * Get a line from the compilation context, compatible with exarg_T getline().
4754 * Return a pointer to the line in allocated memory.
4755 * Return NULL for end-of-file or some error.
4756 */
4757 static char_u *
4758exarg_getline(
4759 int c UNUSED,
4760 void *cookie,
4761 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004762 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004763{
4764 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004765 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004766
Bram Moolenaar66250c92020-08-20 15:02:42 +02004767 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004768 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004769 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004770 return NULL;
4771 ++cctx->ctx_lnum;
4772 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4773 // Comment lines result in NULL pointers, skip them.
4774 if (p != NULL)
4775 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004776 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777}
4778
4779/*
4780 * Compile a nested :def command.
4781 */
4782 static char_u *
4783compile_nested_function(exarg_T *eap, cctx_T *cctx)
4784{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004785 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004786 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004787 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004788 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004789 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004790 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004791
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004792 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004793 {
4794 emsg(_(e_cannot_use_bang_with_nested_def));
4795 return NULL;
4796 }
4797
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004798 if (*name_start == '/')
4799 {
4800 name_end = skip_regexp(name_start + 1, '/', TRUE);
4801 if (*name_end == '/')
4802 ++name_end;
4803 eap->nextcmd = check_nextcmd(name_end);
4804 }
4805 if (name_end == name_start || *skipwhite(name_end) != '(')
4806 {
4807 if (!ends_excmd2(name_start, name_end))
4808 {
4809 semsg(_(e_invalid_command_str), eap->cmd);
4810 return NULL;
4811 }
4812
4813 // "def" or "def Name": list functions
4814 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4815 return NULL;
4816 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4817 }
4818
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004819 // Only g:Func() can use a namespace.
4820 if (name_start[1] == ':' && !is_global)
4821 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004822 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004823 return NULL;
4824 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004825 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4826 return NULL;
4827
Bram Moolenaar04b12692020-05-04 23:24:44 +02004828 eap->arg = name_end;
4829 eap->getline = exarg_getline;
4830 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004831 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004832 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004833 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004834 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004835
Bram Moolenaar822ba242020-05-24 23:00:18 +02004836 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004837 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004838 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004839 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004840 {
4841 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004842 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004843 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004844
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004845 if (is_global)
4846 {
4847 char_u *func_name = vim_strnsave(name_start + 2,
4848 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004849
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004850 if (func_name == NULL)
4851 r = FAIL;
4852 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004853 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004854 }
4855 else
4856 {
4857 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004858 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004859 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004860 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004861
Bram Moolenaareef21022020-08-01 22:16:43 +02004862 if (lvar == NULL)
4863 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004864 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004865 return NULL;
4866 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004867
4868 // copy over the block scope IDs
4869 if (block_depth > 0)
4870 {
4871 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4872 if (ufunc->uf_block_ids != NULL)
4873 {
4874 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4875 sizeof(int) * block_depth);
4876 ufunc->uf_block_depth = block_depth;
4877 }
4878 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004879 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880
Bram Moolenaar61a89812020-05-07 16:58:17 +02004881 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004882 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004883}
4884
4885/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 * Return the length of an assignment operator, or zero if there isn't one.
4887 */
4888 int
4889assignment_len(char_u *p, int *heredoc)
4890{
4891 if (*p == '=')
4892 {
4893 if (p[1] == '<' && p[2] == '<')
4894 {
4895 *heredoc = TRUE;
4896 return 3;
4897 }
4898 return 1;
4899 }
4900 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4901 return 2;
4902 if (STRNCMP(p, "..=", 3) == 0)
4903 return 3;
4904 return 0;
4905}
4906
4907// words that cannot be used as a variable
4908static char *reserved[] = {
4909 "true",
4910 "false",
4911 NULL
4912};
4913
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004914typedef enum {
4915 dest_local,
4916 dest_option,
4917 dest_env,
4918 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004919 dest_buffer,
4920 dest_window,
4921 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004922 dest_vimvar,
4923 dest_script,
4924 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004925 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004926} assign_dest_T;
4927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004929 * Generate the load instruction for "name".
4930 */
4931 static void
4932generate_loadvar(
4933 cctx_T *cctx,
4934 assign_dest_T dest,
4935 char_u *name,
4936 lvar_T *lvar,
4937 type_T *type)
4938{
4939 switch (dest)
4940 {
4941 case dest_option:
4942 // TODO: check the option exists
4943 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4944 break;
4945 case dest_global:
4946 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4947 break;
4948 case dest_buffer:
4949 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4950 break;
4951 case dest_window:
4952 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4953 break;
4954 case dest_tab:
4955 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4956 break;
4957 case dest_script:
4958 compile_load_scriptvar(cctx,
4959 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4960 break;
4961 case dest_env:
4962 // Include $ in the name here
4963 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4964 break;
4965 case dest_reg:
4966 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4967 break;
4968 case dest_vimvar:
4969 generate_LOADV(cctx, name + 2, TRUE);
4970 break;
4971 case dest_local:
4972 if (lvar->lv_from_outer)
4973 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4974 NULL, type);
4975 else
4976 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4977 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004978 case dest_expr:
4979 // list or dict value should already be on the stack.
4980 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004981 }
4982}
4983
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004984/*
4985 * Skip over "[expr]" or ".member".
4986 * Does not check for any errors.
4987 */
4988 static char_u *
4989skip_index(char_u *start)
4990{
4991 char_u *p = start;
4992
4993 if (*p == '[')
4994 {
4995 p = skipwhite(p + 1);
4996 (void)skip_expr(&p, NULL);
4997 p = skipwhite(p);
4998 if (*p == ']')
4999 return p + 1;
5000 return p;
5001 }
5002 // if (*p == '.')
5003 return to_name_end(p + 1, TRUE);
5004}
5005
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005006 void
5007vim9_declare_error(char_u *name)
5008{
5009 char *scope = "";
5010
5011 switch (*name)
5012 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005013 case 'g': scope = _("global"); break;
5014 case 'b': scope = _("buffer"); break;
5015 case 'w': scope = _("window"); break;
5016 case 't': scope = _("tab"); break;
5017 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005018 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005019 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005020 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005021 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005022 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005023 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005024 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005025 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005026 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005027}
5028
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005029/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005030 * For one assignment figure out the type of destination. Return it in "dest".
5031 * When not recognized "dest" is not set.
5032 * For an option "opt_flags" is set.
5033 * For a v:var "vimvaridx" is set.
5034 * "type" is set to the destination type if known, unchanted otherwise.
5035 * Return FAIL if an error message was given.
5036 */
5037 static int
5038get_var_dest(
5039 char_u *name,
5040 assign_dest_T *dest,
5041 int cmdidx,
5042 int *opt_flags,
5043 int *vimvaridx,
5044 type_T **type,
5045 cctx_T *cctx)
5046{
5047 char_u *p;
5048
5049 if (*name == '&')
5050 {
5051 int cc;
5052 long numval;
5053 int opt_type;
5054
5055 *dest = dest_option;
5056 if (cmdidx == CMD_final || cmdidx == CMD_const)
5057 {
5058 emsg(_(e_const_option));
5059 return FAIL;
5060 }
5061 p = name;
5062 p = find_option_end(&p, opt_flags);
5063 if (p == NULL)
5064 {
5065 // cannot happen?
5066 emsg(_(e_letunexp));
5067 return FAIL;
5068 }
5069 cc = *p;
5070 *p = NUL;
5071 opt_type = get_option_value(skip_option_env_lead(name),
5072 &numval, NULL, *opt_flags);
5073 *p = cc;
5074 if (opt_type == -3)
5075 {
5076 semsg(_(e_unknown_option), name);
5077 return FAIL;
5078 }
5079 if (opt_type == -2 || opt_type == 0)
5080 *type = &t_string;
5081 else
5082 *type = &t_number; // both number and boolean option
5083 }
5084 else if (*name == '$')
5085 {
5086 *dest = dest_env;
5087 *type = &t_string;
5088 }
5089 else if (*name == '@')
5090 {
5091 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5092 {
5093 emsg_invreg(name[1]);
5094 return FAIL;
5095 }
5096 *dest = dest_reg;
5097 *type = &t_string;
5098 }
5099 else if (STRNCMP(name, "g:", 2) == 0)
5100 {
5101 *dest = dest_global;
5102 }
5103 else if (STRNCMP(name, "b:", 2) == 0)
5104 {
5105 *dest = dest_buffer;
5106 }
5107 else if (STRNCMP(name, "w:", 2) == 0)
5108 {
5109 *dest = dest_window;
5110 }
5111 else if (STRNCMP(name, "t:", 2) == 0)
5112 {
5113 *dest = dest_tab;
5114 }
5115 else if (STRNCMP(name, "v:", 2) == 0)
5116 {
5117 typval_T *vtv;
5118 int di_flags;
5119
5120 *vimvaridx = find_vim_var(name + 2, &di_flags);
5121 if (*vimvaridx < 0)
5122 {
5123 semsg(_(e_variable_not_found_str), name);
5124 return FAIL;
5125 }
5126 // We use the current value of "sandbox" here, is that OK?
5127 if (var_check_ro(di_flags, name, FALSE))
5128 return FAIL;
5129 *dest = dest_vimvar;
5130 vtv = get_vim_var_tv(*vimvaridx);
5131 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5132 }
5133 return OK;
5134}
5135
5136/*
5137 * Generate a STORE instruction for "dest", not being "dest_local".
5138 * Return FAIL when out of memory.
5139 */
5140 static int
5141generate_store_var(
5142 cctx_T *cctx,
5143 assign_dest_T dest,
5144 int opt_flags,
5145 int vimvaridx,
5146 int scriptvar_idx,
5147 int scriptvar_sid,
5148 type_T *type,
5149 char_u *name)
5150{
5151 switch (dest)
5152 {
5153 case dest_option:
5154 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5155 opt_flags);
5156 case dest_global:
5157 // include g: with the name, easier to execute that way
5158 return generate_STORE(cctx, ISN_STOREG, 0, name);
5159 case dest_buffer:
5160 // include b: with the name, easier to execute that way
5161 return generate_STORE(cctx, ISN_STOREB, 0, name);
5162 case dest_window:
5163 // include w: with the name, easier to execute that way
5164 return generate_STORE(cctx, ISN_STOREW, 0, name);
5165 case dest_tab:
5166 // include t: with the name, easier to execute that way
5167 return generate_STORE(cctx, ISN_STORET, 0, name);
5168 case dest_env:
5169 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5170 case dest_reg:
5171 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5172 case dest_vimvar:
5173 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5174 case dest_script:
5175 if (scriptvar_idx < 0)
5176 {
5177 char_u *name_s = name;
5178 int r;
5179
5180 // Include s: in the name for store_var()
5181 if (name[1] != ':')
5182 {
5183 int len = (int)STRLEN(name) + 3;
5184
5185 name_s = alloc(len);
5186 if (name_s == NULL)
5187 name_s = name;
5188 else
5189 vim_snprintf((char *)name_s, len, "s:%s", name);
5190 }
5191 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5192 scriptvar_sid, type);
5193 if (name_s != name)
5194 vim_free(name_s);
5195 return r;
5196 }
5197 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5198 scriptvar_sid, scriptvar_idx, type);
5199 case dest_local:
5200 case dest_expr:
5201 // cannot happen
5202 break;
5203 }
5204 return FAIL;
5205}
5206
5207/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005208 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005209 * "let name"
5210 * "var name = expr"
5211 * "final name = expr"
5212 * "const name = expr"
5213 * "name = expr"
5214 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005215 * Return NULL for an error.
5216 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 */
5218 static char_u *
5219compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5220{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005223 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 char_u *ret = NULL;
5225 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005227 int scriptvar_sid = 0;
5228 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 int oplen = 0;
5234 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005235 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005236 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005237 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005240 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5241 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 // Skip over the "var" or "[var, var]" to get to any "=".
5244 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5245 if (p == NULL)
5246 return *arg == '[' ? arg : NULL;
5247
5248 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005250 // TODO: should we allow this, and figure out type inference from list
5251 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005252 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 return NULL;
5254 }
5255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 sp = p;
5257 p = skipwhite(p);
5258 op = p;
5259 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260
5261 if (var_count > 0 && oplen == 0)
5262 // can be something like "[1, 2]->func()"
5263 return arg;
5264
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005265 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005267 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005268 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005269 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 if (heredoc)
5272 {
5273 list_T *l;
5274 listitem_T *li;
5275
5276 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005277 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005279 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005280 if (l == NULL)
5281 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282
Bram Moolenaar078269b2020-09-21 20:35:55 +02005283 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005285 // Push each line and the create the list.
5286 FOR_ALL_LIST_ITEMS(l, li)
5287 {
5288 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5289 li->li_tv.vval.v_string = NULL;
5290 }
5291 generate_NEWLIST(cctx, l->lv_len);
5292 type = &t_list_string;
5293 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 list_free(l);
5296 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005299 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005301 char_u *wp;
5302
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 // for "[var, var] = expr" evaluate the expression here, loop over the
5304 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005305 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005306
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005307 wp = op + oplen;
5308 p = skipwhite(wp);
5309 if (may_get_next_line(wp, &p, cctx) == FAIL)
5310 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005311 if (compile_expr0(&p, cctx) == FAIL)
5312 return NULL;
5313 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005315 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005317 type_T *stacktype;
5318
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005319 stacktype = stack->ga_len == 0 ? &t_void
5320 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005323 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 goto theend;
5325 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005326 if (need_type(stacktype, &t_list_any, -1, cctx,
5327 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005329 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005330 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5331 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005332 if (stacktype->tt_member != NULL)
5333 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 }
5335 }
5336
5337 /*
5338 * Loop over variables in "[var, var] = expr".
5339 * For "var = expr" and "let var: type" this is done only once.
5340 */
5341 if (var_count > 0)
5342 var_start = skipwhite(arg + 1); // skip over the "["
5343 else
5344 var_start = arg;
5345 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5346 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005347 char_u *var_end;
5348 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005349 size_t varlen;
5350 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005352 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353 int vimvaridx = -1;
5354 lvar_T *lvar = NULL;
5355 lvar_T arg_lvar;
5356 int has_type = FALSE;
5357 int has_index = FALSE;
5358 int instr_count = -1;
5359
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005360 // "dest_end" is the end of the destination, including "[expr]" or
5361 // ".name".
5362 // "var_end" is the end of the variable/option/etc. name.
5363 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005364 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005365 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005366 else
5367 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005368 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005369 var_end = skip_option_env_lead(var_start);
5370 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005371 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005372
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005373 // "a: type" is declaring variable "a" with a type, not dict "a:".
5374 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5375 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5377 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005379 // compute the length of the destination without "[expr]" or ".name"
5380 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 vim_free(name);
5382 name = vim_strnsave(var_start, varlen);
5383 if (name == NULL)
5384 return NULL;
5385 if (!heredoc)
5386 type = &t_any;
5387
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005388 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005390 int declare_error = FALSE;
5391
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005392 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5393 &vimvaridx, &type, cctx) == FAIL)
5394 goto theend;
5395 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005396 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005397 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005398 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 }
5400 else
5401 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005402 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005403
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005404 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405 for (idx = 0; reserved[idx] != NULL; ++idx)
5406 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005407 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005408 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005409 goto theend;
5410 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411
5412 lvar = lookup_local(var_start, varlen, cctx);
5413 if (lvar == NULL)
5414 {
5415 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005416 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005417 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5418 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005419 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 if (is_decl)
5421 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005422 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 goto theend;
5424 }
5425 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005426 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005427 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 if (lvar != NULL)
5429 {
5430 if (is_decl)
5431 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005432 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 goto theend;
5434 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005436 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005437 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005438 int script_namespace = varlen > 1
5439 && STRNCMP(var_start, "s:", 2) == 0;
5440 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005441 ? script_var_exists(var_start + 2, varlen - 2,
5442 FALSE, cctx)
5443 : script_var_exists(var_start, varlen,
5444 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005445 imported_T *import =
5446 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005447
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005448 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005450 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5451
5452 if (is_decl)
5453 {
5454 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005455 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005456 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005457 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005458 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005459 name);
5460 goto theend;
5461 }
5462 else if (cctx->ctx_ufunc->uf_script_ctx_version
5463 == SCRIPT_VERSION_VIM9
5464 && script_namespace
5465 && !script_var && import == NULL)
5466 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005467 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005468 goto theend;
5469 }
5470
5471 dest = dest_script;
5472
5473 // existing script-local variables should have a type
5474 scriptvar_sid = current_sctx.sc_sid;
5475 if (import != NULL)
5476 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005477 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005478 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005479 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005480 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005481 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005482 {
5483 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5484 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005485 ((svar_T *)si->sn_var_vals.ga_data)
5486 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005487 type = sv->sv_type;
5488 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005489 }
5490 }
5491 else if (name[1] == ':' && name[2] != NUL)
5492 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005493 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 goto theend;
5495 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005496 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005497 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005498 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005499 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005500 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005501 else if (check_defined(var_start, varlen, cctx) == FAIL)
5502 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005503 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005504 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005505
5506 if (declare_error)
5507 {
5508 vim9_declare_error(name);
5509 goto theend;
5510 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 }
5512
5513 // handle "a:name" as a name, not index "name" on "a"
5514 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005515 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005516
5517 if (dest != dest_option)
5518 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005519 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 {
5521 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005522 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005523 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005524 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 goto theend;
5526 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005527 p = skipwhite(var_end + 1);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 type = parse_type(&p, cctx->ctx_type_list);
5529 has_type = TRUE;
5530 }
5531 else if (lvar != NULL)
5532 type = lvar->lv_type;
5533 }
5534
5535 if (oplen == 3 && !heredoc && dest != dest_global
5536 && type->tt_type != VAR_STRING
5537 && type->tt_type != VAR_ANY)
5538 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005539 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 goto theend;
5541 }
5542
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005543 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 {
5545 if (oplen > 1 && !heredoc)
5546 {
5547 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005548 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 goto theend;
5550 }
5551
5552 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005553 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5554 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 goto theend;
5556 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005557 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 if (lvar == NULL)
5559 goto theend;
5560 new_local = TRUE;
5561 }
5562
5563 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005564 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005566 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005567 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005568 if (is_decl)
5569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005570 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 goto theend;
5572 }
5573
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005574 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005575 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005576 char_u *after = var_start + varlen;
5577
5578 // Only the last index is used below, if there are others
5579 // before it generate code for the expression. Thus for
5580 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5581 for (;;)
5582 {
5583 p = skip_index(after);
5584 if (*p != '[' && *p != '.')
5585 break;
5586 after = p;
5587 }
5588 if (after > var_start + varlen)
5589 {
5590 varlen = after - var_start;
5591 dest = dest_expr;
5592 // We don't know the type before evaluating the expression,
5593 // use "any" until then.
5594 type = &t_any;
5595 }
5596
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 has_index = TRUE;
5598 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005599 member_type = &t_any;
5600 else
5601 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 }
5603 else
5604 {
5605 semsg("Not supported yet: %s", var_start);
5606 goto theend;
5607 }
5608 }
5609 else if (lvar == &arg_lvar)
5610 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005611 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 goto theend;
5613 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005614 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5615 {
5616 semsg(_(e_cannot_assign_to_constant), name);
5617 goto theend;
5618 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005619
5620 if (!heredoc)
5621 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005622 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005624 if (oplen > 0 && var_count == 0)
5625 {
5626 // skip over the "=" and the expression
5627 p = skipwhite(op + oplen);
5628 compile_expr0(&p, cctx);
5629 }
5630 }
5631 else if (oplen > 0)
5632 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005633 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005634 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005635
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636 // For "var = expr" evaluate the expression.
5637 if (var_count == 0)
5638 {
5639 int r;
5640
5641 // for "+=", "*=", "..=" etc. first load the current value
5642 if (*op != '=')
5643 {
5644 generate_loadvar(cctx, dest, name, lvar, type);
5645
5646 if (has_index)
5647 {
5648 // TODO: get member from list or dict
5649 emsg("Index with operation not supported yet");
5650 goto theend;
5651 }
5652 }
5653
5654 // Compile the expression. Temporarily hide the new local
5655 // variable here, it is not available to this expression.
5656 if (new_local)
5657 --cctx->ctx_locals.ga_len;
5658 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005659 wp = op + oplen;
5660 p = skipwhite(wp);
5661 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005662 {
5663 if (new_local)
5664 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005665 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005666 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005667 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005668 if (new_local)
5669 ++cctx->ctx_locals.ga_len;
5670 if (r == FAIL)
5671 goto theend;
5672 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005673 else if (semicolon && var_idx == var_count - 1)
5674 {
5675 // For "[var; var] = expr" get the rest of the list
5676 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5677 goto theend;
5678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005679 else
5680 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005681 // For "[var, var] = expr" get the "var_idx" item from the
5682 // list.
5683 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005684 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005686
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005687 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005688 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005689 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005690 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005691 if ((rhs_type->tt_type == VAR_FUNC
5692 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005693 && var_wrong_func_name(name, TRUE))
5694 goto theend;
5695
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005696 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005697 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005698 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005699 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005700 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005701 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 }
5703 else
5704 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005705 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005706 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005707 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005708 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005709 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005710 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005711 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005712 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005713 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005714 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005715 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005716 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005717 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005718 {
5719 type_T *use_type = lvar->lv_type;
5720
Bram Moolenaar71abe482020-09-14 22:28:30 +02005721 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005722 if (has_index)
5723 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005724 use_type = member_type;
5725 if (member_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005726 // could be indexing "any"
5727 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005728 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005729 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005730 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005731 goto theend;
5732 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005734 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005735 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005736 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005738 else if (cmdidx == CMD_final)
5739 {
5740 emsg(_(e_final_requires_a_value));
5741 goto theend;
5742 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005743 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005745 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005746 goto theend;
5747 }
5748 else if (!has_type || dest == dest_option)
5749 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005750 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005751 goto theend;
5752 }
5753 else
5754 {
5755 // variables are always initialized
5756 if (ga_grow(instr, 1) == FAIL)
5757 goto theend;
5758 switch (member_type->tt_type)
5759 {
5760 case VAR_BOOL:
5761 generate_PUSHBOOL(cctx, VVAL_FALSE);
5762 break;
5763 case VAR_FLOAT:
5764#ifdef FEAT_FLOAT
5765 generate_PUSHF(cctx, 0.0);
5766#endif
5767 break;
5768 case VAR_STRING:
5769 generate_PUSHS(cctx, NULL);
5770 break;
5771 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005772 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005773 break;
5774 case VAR_FUNC:
5775 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5776 break;
5777 case VAR_LIST:
5778 generate_NEWLIST(cctx, 0);
5779 break;
5780 case VAR_DICT:
5781 generate_NEWDICT(cctx, 0);
5782 break;
5783 case VAR_JOB:
5784 generate_PUSHJOB(cctx, NULL);
5785 break;
5786 case VAR_CHANNEL:
5787 generate_PUSHCHANNEL(cctx, NULL);
5788 break;
5789 case VAR_NUMBER:
5790 case VAR_UNKNOWN:
5791 case VAR_ANY:
5792 case VAR_PARTIAL:
5793 case VAR_VOID:
5794 case VAR_SPECIAL: // cannot happen
5795 generate_PUSHNR(cctx, 0);
5796 break;
5797 }
5798 }
5799 if (var_count == 0)
5800 end = p;
5801 }
5802
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005803 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005804 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005805 break;
5806
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005807 if (oplen > 0 && *op != '=')
5808 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005809 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005810 type_T *stacktype;
5811
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005812 if (*op == '.')
5813 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005814 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005815 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005816 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005817 if (
5818#ifdef FEAT_FLOAT
5819 // If variable is float operation with number is OK.
5820 !(expected == &t_float && stacktype == &t_number) &&
5821#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005822 need_type(stacktype, expected, -1, cctx,
5823 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 goto theend;
5825
5826 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005827 {
5828 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5829 goto theend;
5830 }
5831 else if (*op == '+')
5832 {
5833 if (generate_add_instr(cctx,
5834 operator_type(member_type, stacktype),
5835 member_type, stacktype) == FAIL)
5836 goto theend;
5837 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005838 else if (generate_two_op(cctx, op) == FAIL)
5839 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005842 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005843 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005844 int r;
5845
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005846 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005847 if (new_local)
5848 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005849 p = var_start + varlen;
5850 if (*p == '[')
5851 {
5852 p = skipwhite(p + 1);
5853 r = compile_expr0(&p, cctx);
5854 if (r == OK && *skipwhite(p) != ']')
5855 {
5856 // this should not happen
5857 emsg(_(e_missbrac));
5858 r = FAIL;
5859 }
5860 }
5861 else // if (*p == '.')
5862 {
5863 char_u *key_end = to_name_end(p + 1, TRUE);
5864 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5865
5866 r = generate_PUSHS(cctx, key);
5867 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005868 if (new_local)
5869 ++cctx->ctx_locals.ga_len;
5870 if (r == FAIL)
5871 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005872
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005873 if (type == &t_any)
5874 {
5875 type_T *idx_type = ((type_T **)stack->ga_data)[
5876 stack->ga_len - 1];
5877 // Index on variable of unknown type: guess the type from the
5878 // index type: number is dict, otherwise dict.
5879 // TODO: should do the assignment at runtime
5880 if (idx_type->tt_type == VAR_NUMBER)
5881 type = &t_list_any;
5882 else
5883 type = &t_dict_any;
5884 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005885 if (type->tt_type == VAR_DICT
5886 && may_generate_2STRING(-1, cctx) == FAIL)
5887 goto theend;
5888 if (type->tt_type == VAR_LIST
5889 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005890 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005891 {
5892 emsg(_(e_number_exp));
5893 goto theend;
5894 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005895
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005896 // Load the dict or list. On the stack we then have:
5897 // - value
5898 // - index
5899 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005900 if (dest == dest_expr)
5901 {
5902 int c = var_start[varlen];
5903
5904 // Evaluate "ll[expr]" of "ll[expr][idx]"
5905 p = var_start;
5906 var_start[varlen] = NUL;
5907 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5908 {
5909 // this should not happen
5910 emsg(_(e_missbrac));
5911 goto theend;
5912 }
5913 var_start[varlen] = c;
5914
5915 type = stack->ga_len == 0 ? &t_void
5916 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5917 // now we can properly check the type
5918 if (type->tt_member != NULL
5919 && need_type(rhs_type, type->tt_member, -2, cctx,
5920 FALSE, FALSE) == FAIL)
5921 goto theend;
5922 }
5923 else
5924 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005925
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005926 if (type->tt_type == VAR_LIST)
5927 {
5928 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005929 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005930 }
5931 else if (type->tt_type == VAR_DICT)
5932 {
5933 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005934 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005935 }
5936 else
5937 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005938 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005939 goto theend;
5940 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005941 }
5942 else
5943 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005944 if (is_decl && cmdidx == CMD_const
5945 && (dest == dest_script || dest == dest_local))
5946 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005947 generate_LOCKCONST(cctx);
5948
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005949 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005950 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005951 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
5952 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
5953 goto theend;
5954 }
5955 else if (lvar != NULL)
5956 {
5957 isn_T *isn = ((isn_T *)instr->ga_data)
5958 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005959
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005960 // optimization: turn "var = 123" from ISN_PUSHNR +
5961 // ISN_STORE into ISN_STORENR
5962 if (!lvar->lv_from_outer
5963 && instr->ga_len == instr_count + 1
5964 && isn->isn_type == ISN_PUSHNR)
5965 {
5966 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005967
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005968 isn->isn_type = ISN_STORENR;
5969 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5970 isn->isn_arg.storenr.stnr_val = val;
5971 if (stack->ga_len > 0)
5972 --stack->ga_len;
5973 }
5974 else if (lvar->lv_from_outer)
5975 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
5976 else
5977 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005979 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005980
5981 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005982 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005984
5985 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005986 if (var_count > 0 && !semicolon)
5987 {
5988 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5989 goto theend;
5990 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005991
Bram Moolenaarb2097502020-07-19 17:17:02 +02005992 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
5994theend:
5995 vim_free(name);
5996 return ret;
5997}
5998
5999/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006000 * Check if "name" can be "unlet".
6001 */
6002 int
6003check_vim9_unlet(char_u *name)
6004{
6005 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6006 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006007 // "unlet s:var" is allowed in legacy script.
6008 if (*name == 's' && !script_is_vim9())
6009 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006010 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006011 return FAIL;
6012 }
6013 return OK;
6014}
6015
6016/*
6017 * Callback passed to ex_unletlock().
6018 */
6019 static int
6020compile_unlet(
6021 lval_T *lvp,
6022 char_u *name_end,
6023 exarg_T *eap,
6024 int deep UNUSED,
6025 void *coookie)
6026{
6027 cctx_T *cctx = coookie;
6028
6029 if (lvp->ll_tv == NULL)
6030 {
6031 char_u *p = lvp->ll_name;
6032 int cc = *name_end;
6033 int ret = OK;
6034
6035 // Normal name. Only supports g:, w:, t: and b: namespaces.
6036 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006037 if (*p == '$')
6038 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6039 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006040 ret = FAIL;
6041 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006042 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006043
6044 *name_end = cc;
6045 return ret;
6046 }
6047
6048 // TODO: unlet {list}[idx]
6049 // TODO: unlet {dict}[key]
6050 emsg("Sorry, :unlet not fully implemented yet");
6051 return FAIL;
6052}
6053
6054/*
6055 * compile "unlet var", "lock var" and "unlock var"
6056 * "arg" points to "var".
6057 */
6058 static char_u *
6059compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6060{
6061 char_u *p = arg;
6062
6063 if (eap->cmdidx != CMD_unlet)
6064 {
6065 emsg("Sorry, :lock and unlock not implemented yet");
6066 return NULL;
6067 }
6068
6069 if (*p == '!')
6070 {
6071 p = skipwhite(p + 1);
6072 eap->forceit = TRUE;
6073 }
6074
6075 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6076 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6077}
6078
6079/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 * Compile an :import command.
6081 */
6082 static char_u *
6083compile_import(char_u *arg, cctx_T *cctx)
6084{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006085 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086}
6087
6088/*
6089 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6090 */
6091 static int
6092compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6093{
6094 garray_T *instr = &cctx->ctx_instr;
6095 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6096
6097 if (endlabel == NULL)
6098 return FAIL;
6099 endlabel->el_next = *el;
6100 *el = endlabel;
6101 endlabel->el_end_label = instr->ga_len;
6102
6103 generate_JUMP(cctx, when, 0);
6104 return OK;
6105}
6106
6107 static void
6108compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6109{
6110 garray_T *instr = &cctx->ctx_instr;
6111
6112 while (*el != NULL)
6113 {
6114 endlabel_T *cur = (*el);
6115 isn_T *isn;
6116
6117 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6118 isn->isn_arg.jump.jump_where = instr->ga_len;
6119 *el = cur->el_next;
6120 vim_free(cur);
6121 }
6122}
6123
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006124 static void
6125compile_free_jump_to_end(endlabel_T **el)
6126{
6127 while (*el != NULL)
6128 {
6129 endlabel_T *cur = (*el);
6130
6131 *el = cur->el_next;
6132 vim_free(cur);
6133 }
6134}
6135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136/*
6137 * Create a new scope and set up the generic items.
6138 */
6139 static scope_T *
6140new_scope(cctx_T *cctx, scopetype_T type)
6141{
6142 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6143
6144 if (scope == NULL)
6145 return NULL;
6146 scope->se_outer = cctx->ctx_scope;
6147 cctx->ctx_scope = scope;
6148 scope->se_type = type;
6149 scope->se_local_count = cctx->ctx_locals.ga_len;
6150 return scope;
6151}
6152
6153/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006154 * Free the current scope and go back to the outer scope.
6155 */
6156 static void
6157drop_scope(cctx_T *cctx)
6158{
6159 scope_T *scope = cctx->ctx_scope;
6160
6161 if (scope == NULL)
6162 {
6163 iemsg("calling drop_scope() without a scope");
6164 return;
6165 }
6166 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006167 switch (scope->se_type)
6168 {
6169 case IF_SCOPE:
6170 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6171 case FOR_SCOPE:
6172 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6173 case WHILE_SCOPE:
6174 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6175 case TRY_SCOPE:
6176 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6177 case NO_SCOPE:
6178 case BLOCK_SCOPE:
6179 break;
6180 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006181 vim_free(scope);
6182}
6183
6184/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 * compile "if expr"
6186 *
6187 * "if expr" Produces instructions:
6188 * EVAL expr Push result of "expr"
6189 * JUMP_IF_FALSE end
6190 * ... body ...
6191 * end:
6192 *
6193 * "if expr | else" Produces instructions:
6194 * EVAL expr Push result of "expr"
6195 * JUMP_IF_FALSE else
6196 * ... body ...
6197 * JUMP_ALWAYS end
6198 * else:
6199 * ... body ...
6200 * end:
6201 *
6202 * "if expr1 | elseif expr2 | else" Produces instructions:
6203 * EVAL expr Push result of "expr"
6204 * JUMP_IF_FALSE elseif
6205 * ... body ...
6206 * JUMP_ALWAYS end
6207 * elseif:
6208 * EVAL expr Push result of "expr"
6209 * JUMP_IF_FALSE else
6210 * ... body ...
6211 * JUMP_ALWAYS end
6212 * else:
6213 * ... body ...
6214 * end:
6215 */
6216 static char_u *
6217compile_if(char_u *arg, cctx_T *cctx)
6218{
6219 char_u *p = arg;
6220 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006221 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006223 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006224 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225
Bram Moolenaara5565e42020-05-09 15:44:01 +02006226 CLEAR_FIELD(ppconst);
6227 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006228 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006229 clear_ppconst(&ppconst);
6230 return NULL;
6231 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006232 if (cctx->ctx_skip == SKIP_YES)
6233 clear_ppconst(&ppconst);
6234 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006235 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006236 int error = FALSE;
6237 int v;
6238
Bram Moolenaara5565e42020-05-09 15:44:01 +02006239 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006240 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006241 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006242 if (error)
6243 return NULL;
6244 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006245 }
6246 else
6247 {
6248 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006249 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006250 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006251 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006252 if (bool_on_stack(cctx) == FAIL)
6253 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255
6256 scope = new_scope(cctx, IF_SCOPE);
6257 if (scope == NULL)
6258 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006259 scope->se_skip_save = skip_save;
6260 // "is_had_return" will be reset if any block does not end in :return
6261 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006263 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006264 {
6265 // "where" is set when ":elseif", "else" or ":endif" is found
6266 scope->se_u.se_if.is_if_label = instr->ga_len;
6267 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6268 }
6269 else
6270 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271
6272 return p;
6273}
6274
6275 static char_u *
6276compile_elseif(char_u *arg, cctx_T *cctx)
6277{
6278 char_u *p = arg;
6279 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006280 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 isn_T *isn;
6282 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006283 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006284 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285
6286 if (scope == NULL || scope->se_type != IF_SCOPE)
6287 {
6288 emsg(_(e_elseif_without_if));
6289 return NULL;
6290 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006291 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006292 if (!cctx->ctx_had_return)
6293 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006295 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006296 {
6297 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006299 return NULL;
6300 // previous "if" or "elseif" jumps here
6301 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6302 isn->isn_arg.jump.jump_where = instr->ga_len;
6303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006305 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006306 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006307 if (cctx->ctx_skip == SKIP_YES)
6308 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006309 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006310 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006311 clear_ppconst(&ppconst);
6312 return NULL;
6313 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006314 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006315 if (scope->se_skip_save == SKIP_YES)
6316 clear_ppconst(&ppconst);
6317 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006318 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006319 int error = FALSE;
6320 int v;
6321
Bram Moolenaar7f141552020-05-09 17:35:53 +02006322 // The expression results in a constant.
6323 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006324 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6325 if (error)
6326 return NULL;
6327 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006328 clear_ppconst(&ppconst);
6329 scope->se_u.se_if.is_if_label = -1;
6330 }
6331 else
6332 {
6333 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006334 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006335 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006336 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006337 if (bool_on_stack(cctx) == FAIL)
6338 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006340 // "where" is set when ":elseif", "else" or ":endif" is found
6341 scope->se_u.se_if.is_if_label = instr->ga_len;
6342 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344
6345 return p;
6346}
6347
6348 static char_u *
6349compile_else(char_u *arg, cctx_T *cctx)
6350{
6351 char_u *p = arg;
6352 garray_T *instr = &cctx->ctx_instr;
6353 isn_T *isn;
6354 scope_T *scope = cctx->ctx_scope;
6355
6356 if (scope == NULL || scope->se_type != IF_SCOPE)
6357 {
6358 emsg(_(e_else_without_if));
6359 return NULL;
6360 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006361 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006362 if (!cctx->ctx_had_return)
6363 scope->se_u.se_if.is_had_return = FALSE;
6364 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365
Bram Moolenaarefd88552020-06-18 20:50:10 +02006366 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006367 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006368 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006369 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006370 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006371 if (!cctx->ctx_had_return
6372 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6373 JUMP_ALWAYS, cctx) == FAIL)
6374 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006375 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006376
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006377 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006378 {
6379 if (scope->se_u.se_if.is_if_label >= 0)
6380 {
6381 // previous "if" or "elseif" jumps here
6382 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6383 isn->isn_arg.jump.jump_where = instr->ga_len;
6384 scope->se_u.se_if.is_if_label = -1;
6385 }
6386 }
6387
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006388 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006389 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391
6392 return p;
6393}
6394
6395 static char_u *
6396compile_endif(char_u *arg, cctx_T *cctx)
6397{
6398 scope_T *scope = cctx->ctx_scope;
6399 ifscope_T *ifscope;
6400 garray_T *instr = &cctx->ctx_instr;
6401 isn_T *isn;
6402
6403 if (scope == NULL || scope->se_type != IF_SCOPE)
6404 {
6405 emsg(_(e_endif_without_if));
6406 return NULL;
6407 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006408 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006409 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006410 if (!cctx->ctx_had_return)
6411 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006413 if (scope->se_u.se_if.is_if_label >= 0)
6414 {
6415 // previous "if" or "elseif" jumps here
6416 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6417 isn->isn_arg.jump.jump_where = instr->ga_len;
6418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 // Fill in the "end" label in jumps at the end of the blocks.
6420 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006421 cctx->ctx_skip = scope->se_skip_save;
6422
6423 // If all the blocks end in :return and there is an :else then the
6424 // had_return flag is set.
6425 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006427 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 return arg;
6429}
6430
6431/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006432 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 *
6434 * Produces instructions:
6435 * PUSHNR -1
6436 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006437 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6439 * - if beyond end, jump to "end"
6440 * - otherwise get item from list and push it
6441 * STORE var Store item in "var"
6442 * ... body ...
6443 * JUMP top Jump back to repeat
6444 * end: DROP Drop the result of "expr"
6445 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006446 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6447 * UNPACK 2 Split item in 2
6448 * STORE var1 Store item in "var1"
6449 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 */
6451 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006452compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006454 char_u *arg;
6455 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006456 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006458 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006459 int var_count = 0;
6460 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461 size_t varlen;
6462 garray_T *instr = &cctx->ctx_instr;
6463 garray_T *stack = &cctx->ctx_type_stack;
6464 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006465 lvar_T *loop_lvar; // loop iteration variable
6466 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006468 type_T *item_type = &t_any;
6469 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470
Bram Moolenaar792f7862020-11-23 08:31:18 +01006471 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6472 if (var_count == 0)
6473 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474
6475 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006476 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006478 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6479 return NULL;
6480 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 {
6482 emsg(_(e_missing_in));
6483 return NULL;
6484 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006485 wp = p + 2;
6486 p = skipwhite(wp);
6487 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6488 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 scope = new_scope(cctx, FOR_SCOPE);
6491 if (scope == NULL)
6492 return NULL;
6493
Bram Moolenaar792f7862020-11-23 08:31:18 +01006494 // Reserve a variable to store the loop iteration counter and initialize it
6495 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006496 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6497 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006498 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006499 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006500 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006502 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006503 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504
6505 // compile "expr", it remains on the stack until "endfor"
6506 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006507 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006508 {
6509 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006511 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006512 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006514 // Now that we know the type of "var", check that it is a list, now or at
6515 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006517 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006519 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 return NULL;
6521 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006522
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006523 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006524 {
6525 if (var_count == 1)
6526 item_type = vartype->tt_member;
6527 else if (vartype->tt_member->tt_type == VAR_LIST
6528 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6529 item_type = vartype->tt_member->tt_member;
6530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531
6532 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006533 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006534 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535
Bram Moolenaar792f7862020-11-23 08:31:18 +01006536 arg = arg_start;
6537 if (var_count > 1)
6538 {
6539 generate_UNPACK(cctx, var_count, semicolon);
6540 arg = skipwhite(arg + 1); // skip white after '['
6541
6542 // the list item is replaced by a number of items
6543 if (ga_grow(stack, var_count - 1) == FAIL)
6544 {
6545 drop_scope(cctx);
6546 return NULL;
6547 }
6548 --stack->ga_len;
6549 for (idx = 0; idx < var_count; ++idx)
6550 {
6551 ((type_T **)stack->ga_data)[stack->ga_len] =
6552 (semicolon && idx == 0) ? vartype : item_type;
6553 ++stack->ga_len;
6554 }
6555 }
6556
6557 for (idx = 0; idx < var_count; ++idx)
6558 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006559 assign_dest_T dest = dest_local;
6560 int opt_flags = 0;
6561 int vimvaridx = -1;
6562 type_T *type = &t_any;
6563
6564 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006565 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006566 name = vim_strnsave(arg, varlen);
6567 if (name == NULL)
6568 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006569
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006570 // TODO: script var not supported?
6571 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6572 &vimvaridx, &type, cctx) == FAIL)
6573 goto failed;
6574 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006575 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006576 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6577 0, 0, type, name) == FAIL)
6578 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006579 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006580 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006581 {
6582 var_lvar = lookup_local(arg, varlen, cctx);
6583 if (var_lvar != NULL)
6584 {
6585 semsg(_(e_variable_already_declared), arg);
6586 goto failed;
6587 }
6588
Bram Moolenaarea870692020-12-02 14:24:30 +01006589 if (STRNCMP(name, "s:", 2) == 0)
6590 {
6591 semsg(_(e_cannot_declare_script_variable_in_function), name);
6592 goto failed;
6593 }
6594
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006595 // Reserve a variable to store "var".
6596 // TODO: check for type
6597 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6598 if (var_lvar == NULL)
6599 // out of memory or used as an argument
6600 goto failed;
6601
6602 if (semicolon && idx == var_count - 1)
6603 var_lvar->lv_type = vartype;
6604 else
6605 var_lvar->lv_type = item_type;
6606 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6607 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006608
6609 if (*p == ',' || *p == ';')
6610 ++p;
6611 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006612 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006613 }
6614
6615 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006616
6617failed:
6618 vim_free(name);
6619 drop_scope(cctx);
6620 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621}
6622
6623/*
6624 * compile "endfor"
6625 */
6626 static char_u *
6627compile_endfor(char_u *arg, cctx_T *cctx)
6628{
6629 garray_T *instr = &cctx->ctx_instr;
6630 scope_T *scope = cctx->ctx_scope;
6631 forscope_T *forscope;
6632 isn_T *isn;
6633
6634 if (scope == NULL || scope->se_type != FOR_SCOPE)
6635 {
6636 emsg(_(e_for));
6637 return NULL;
6638 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006639 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006641 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642
6643 // At end of ":for" scope jump back to the FOR instruction.
6644 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6645
6646 // Fill in the "end" label in the FOR statement so it can jump here
6647 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6648 isn->isn_arg.forloop.for_end = instr->ga_len;
6649
6650 // Fill in the "end" label any BREAK statements
6651 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6652
6653 // Below the ":for" scope drop the "expr" list from the stack.
6654 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6655 return NULL;
6656
6657 vim_free(scope);
6658
6659 return arg;
6660}
6661
6662/*
6663 * compile "while expr"
6664 *
6665 * Produces instructions:
6666 * top: EVAL expr Push result of "expr"
6667 * JUMP_IF_FALSE end jump if false
6668 * ... body ...
6669 * JUMP top Jump back to repeat
6670 * end:
6671 *
6672 */
6673 static char_u *
6674compile_while(char_u *arg, cctx_T *cctx)
6675{
6676 char_u *p = arg;
6677 garray_T *instr = &cctx->ctx_instr;
6678 scope_T *scope;
6679
6680 scope = new_scope(cctx, WHILE_SCOPE);
6681 if (scope == NULL)
6682 return NULL;
6683
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006684 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
6686 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006687 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 return NULL;
6689
Bram Moolenaar13106602020-10-04 16:06:05 +02006690 if (bool_on_stack(cctx) == FAIL)
6691 return FAIL;
6692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006694 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 JUMP_IF_FALSE, cctx) == FAIL)
6696 return FAIL;
6697
6698 return p;
6699}
6700
6701/*
6702 * compile "endwhile"
6703 */
6704 static char_u *
6705compile_endwhile(char_u *arg, cctx_T *cctx)
6706{
6707 scope_T *scope = cctx->ctx_scope;
6708
6709 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6710 {
6711 emsg(_(e_while));
6712 return NULL;
6713 }
6714 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006715 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716
6717 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006718 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719
6720 // Fill in the "end" label in the WHILE statement so it can jump here.
6721 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006722 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
6724 vim_free(scope);
6725
6726 return arg;
6727}
6728
6729/*
6730 * compile "continue"
6731 */
6732 static char_u *
6733compile_continue(char_u *arg, cctx_T *cctx)
6734{
6735 scope_T *scope = cctx->ctx_scope;
6736
6737 for (;;)
6738 {
6739 if (scope == NULL)
6740 {
6741 emsg(_(e_continue));
6742 return NULL;
6743 }
6744 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6745 break;
6746 scope = scope->se_outer;
6747 }
6748
6749 // Jump back to the FOR or WHILE instruction.
6750 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006751 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6752 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 return arg;
6754}
6755
6756/*
6757 * compile "break"
6758 */
6759 static char_u *
6760compile_break(char_u *arg, cctx_T *cctx)
6761{
6762 scope_T *scope = cctx->ctx_scope;
6763 endlabel_T **el;
6764
6765 for (;;)
6766 {
6767 if (scope == NULL)
6768 {
6769 emsg(_(e_break));
6770 return NULL;
6771 }
6772 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6773 break;
6774 scope = scope->se_outer;
6775 }
6776
6777 // Jump to the end of the FOR or WHILE loop.
6778 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006779 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006781 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6783 return FAIL;
6784
6785 return arg;
6786}
6787
6788/*
6789 * compile "{" start of block
6790 */
6791 static char_u *
6792compile_block(char_u *arg, cctx_T *cctx)
6793{
6794 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6795 return NULL;
6796 return skipwhite(arg + 1);
6797}
6798
6799/*
6800 * compile end of block: drop one scope
6801 */
6802 static void
6803compile_endblock(cctx_T *cctx)
6804{
6805 scope_T *scope = cctx->ctx_scope;
6806
6807 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006808 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 vim_free(scope);
6810}
6811
6812/*
6813 * compile "try"
6814 * Creates a new scope for the try-endtry, pointing to the first catch and
6815 * finally.
6816 * Creates another scope for the "try" block itself.
6817 * TRY instruction sets up exception handling at runtime.
6818 *
6819 * "try"
6820 * TRY -> catch1, -> finally push trystack entry
6821 * ... try block
6822 * "throw {exception}"
6823 * EVAL {exception}
6824 * THROW create exception
6825 * ... try block
6826 * " catch {expr}"
6827 * JUMP -> finally
6828 * catch1: PUSH exeception
6829 * EVAL {expr}
6830 * MATCH
6831 * JUMP nomatch -> catch2
6832 * CATCH remove exception
6833 * ... catch block
6834 * " catch"
6835 * JUMP -> finally
6836 * catch2: CATCH remove exception
6837 * ... catch block
6838 * " finally"
6839 * finally:
6840 * ... finally block
6841 * " endtry"
6842 * ENDTRY pop trystack entry, may rethrow
6843 */
6844 static char_u *
6845compile_try(char_u *arg, cctx_T *cctx)
6846{
6847 garray_T *instr = &cctx->ctx_instr;
6848 scope_T *try_scope;
6849 scope_T *scope;
6850
6851 // scope that holds the jumps that go to catch/finally/endtry
6852 try_scope = new_scope(cctx, TRY_SCOPE);
6853 if (try_scope == NULL)
6854 return NULL;
6855
6856 // "catch" is set when the first ":catch" is found.
6857 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006858 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859 if (generate_instr(cctx, ISN_TRY) == NULL)
6860 return NULL;
6861
6862 // scope for the try block itself
6863 scope = new_scope(cctx, BLOCK_SCOPE);
6864 if (scope == NULL)
6865 return NULL;
6866
6867 return arg;
6868}
6869
6870/*
6871 * compile "catch {expr}"
6872 */
6873 static char_u *
6874compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6875{
6876 scope_T *scope = cctx->ctx_scope;
6877 garray_T *instr = &cctx->ctx_instr;
6878 char_u *p;
6879 isn_T *isn;
6880
6881 // end block scope from :try or :catch
6882 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6883 compile_endblock(cctx);
6884 scope = cctx->ctx_scope;
6885
6886 // Error if not in a :try scope
6887 if (scope == NULL || scope->se_type != TRY_SCOPE)
6888 {
6889 emsg(_(e_catch));
6890 return NULL;
6891 }
6892
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006893 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006895 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 return NULL;
6897 }
6898
6899 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006900 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 JUMP_ALWAYS, cctx) == FAIL)
6902 return NULL;
6903
6904 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006905 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 if (isn->isn_arg.try.try_catch == 0)
6907 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006908 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 {
6910 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006911 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 isn->isn_arg.jump.jump_where = instr->ga_len;
6913 }
6914
6915 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006916 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006918 scope->se_u.se_try.ts_caught_all = TRUE;
6919 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 }
6921 else
6922 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006923 char_u *end;
6924 char_u *pat;
6925 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006926 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006927 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 // Push v:exception, push {expr} and MATCH
6930 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6931
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006932 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006933 if (*end != *p)
6934 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006935 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006936 vim_free(tofree);
6937 return FAIL;
6938 }
6939 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006940 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006941 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006942 len = (int)(end - tofree);
6943 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006944 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006945 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006946 if (pat == NULL)
6947 return FAIL;
6948 if (generate_PUSHS(cctx, pat) == FAIL)
6949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6952 return NULL;
6953
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006954 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6956 return NULL;
6957 }
6958
6959 if (generate_instr(cctx, ISN_CATCH) == NULL)
6960 return NULL;
6961
6962 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6963 return NULL;
6964 return p;
6965}
6966
6967 static char_u *
6968compile_finally(char_u *arg, cctx_T *cctx)
6969{
6970 scope_T *scope = cctx->ctx_scope;
6971 garray_T *instr = &cctx->ctx_instr;
6972 isn_T *isn;
6973
6974 // end block scope from :try or :catch
6975 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6976 compile_endblock(cctx);
6977 scope = cctx->ctx_scope;
6978
6979 // Error if not in a :try scope
6980 if (scope == NULL || scope->se_type != TRY_SCOPE)
6981 {
6982 emsg(_(e_finally));
6983 return NULL;
6984 }
6985
6986 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006987 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 if (isn->isn_arg.try.try_finally != 0)
6989 {
6990 emsg(_(e_finally_dup));
6991 return NULL;
6992 }
6993
6994 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006995 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996
Bram Moolenaar585fea72020-04-02 22:33:21 +02006997 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006998 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 {
7000 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007001 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007003 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 }
7005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 // TODO: set index in ts_finally_label jumps
7007
7008 return arg;
7009}
7010
7011 static char_u *
7012compile_endtry(char_u *arg, cctx_T *cctx)
7013{
7014 scope_T *scope = cctx->ctx_scope;
7015 garray_T *instr = &cctx->ctx_instr;
7016 isn_T *isn;
7017
7018 // end block scope from :catch or :finally
7019 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7020 compile_endblock(cctx);
7021 scope = cctx->ctx_scope;
7022
7023 // Error if not in a :try scope
7024 if (scope == NULL || scope->se_type != TRY_SCOPE)
7025 {
7026 if (scope == NULL)
7027 emsg(_(e_no_endtry));
7028 else if (scope->se_type == WHILE_SCOPE)
7029 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007030 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 emsg(_(e_endfor));
7032 else
7033 emsg(_(e_endif));
7034 return NULL;
7035 }
7036
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007037 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7039 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007040 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 return NULL;
7042 }
7043
7044 // Fill in the "end" label in jumps at the end of the blocks, if not done
7045 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007046 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047
7048 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007049 if (isn->isn_arg.try.try_catch == 0)
7050 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 if (isn->isn_arg.try.try_finally == 0)
7052 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007053
7054 if (scope->se_u.se_try.ts_catch_label != 0)
7055 {
7056 // Last catch without match jumps here
7057 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7058 isn->isn_arg.jump.jump_where = instr->ga_len;
7059 }
7060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 compile_endblock(cctx);
7062
7063 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7064 return NULL;
7065 return arg;
7066}
7067
7068/*
7069 * compile "throw {expr}"
7070 */
7071 static char_u *
7072compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7073{
7074 char_u *p = skipwhite(arg);
7075
Bram Moolenaara5565e42020-05-09 15:44:01 +02007076 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 return NULL;
7078 if (may_generate_2STRING(-1, cctx) == FAIL)
7079 return NULL;
7080 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7081 return NULL;
7082
7083 return p;
7084}
7085
7086/*
7087 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007088 * compile "echomsg expr"
7089 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007090 * compile "execute expr"
7091 */
7092 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007093compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007094{
7095 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007096 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007097 int count = 0;
7098
7099 for (;;)
7100 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007101 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007102 return NULL;
7103 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007104 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007105 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007106 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01007107 break;
7108 }
7109
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007110 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7111 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7112 else if (cmdidx == CMD_execute)
7113 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7114 else if (cmdidx == CMD_echomsg)
7115 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7116 else
7117 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 return p;
7119}
7120
7121/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007122 * :put r
7123 * :put ={expr}
7124 */
7125 static char_u *
7126compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7127{
7128 char_u *line = arg;
7129 linenr_T lnum;
7130 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007131 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007132
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007133 eap->regname = *line;
7134
7135 if (eap->regname == '=')
7136 {
7137 char_u *p = line + 1;
7138
7139 if (compile_expr0(&p, cctx) == FAIL)
7140 return NULL;
7141 line = p;
7142 }
7143 else if (eap->regname != NUL)
7144 ++line;
7145
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007146 // "errormsg" will not be set because the range is ADDR_LINES.
7147 // TODO: if the range contains something like "$" or "." need to evaluate
7148 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007149 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
7150 return NULL;
7151 if (eap->addr_count == 0)
7152 lnum = -1;
7153 else
7154 lnum = eap->line2;
7155 if (above)
7156 --lnum;
7157
7158 generate_PUT(cctx, eap->regname, lnum);
7159 return line;
7160}
7161
7162/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007163 * A command that is not compiled, execute with legacy code.
7164 */
7165 static char_u *
7166compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7167{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007168 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007169 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007170 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007171
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007172 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007173 goto theend;
7174
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007175 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007176 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007177 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007178 int usefilter = FALSE;
7179
7180 has_expr = argt & (EX_XFILE | EX_EXPAND);
7181
7182 // If the command can be followed by a bar, find the bar and truncate
7183 // it, so that the following command can be compiled.
7184 // The '|' is overwritten with a NUL, it is put back below.
7185 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7186 && *eap->arg == '!')
7187 // :w !filter or :r !filter or :r! filter
7188 usefilter = TRUE;
7189 if ((argt & EX_TRLBAR) && !usefilter)
7190 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007191 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007192 separate_nextcmd(eap);
7193 if (eap->nextcmd != NULL)
7194 nextcmd = eap->nextcmd;
7195 }
7196 }
7197
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007198 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7199 {
7200 // expand filename in "syntax include [@group] filename"
7201 has_expr = TRUE;
7202 eap->arg = skipwhite(eap->arg + 7);
7203 if (*eap->arg == '@')
7204 eap->arg = skiptowhite(eap->arg);
7205 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007206
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007207 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007208 {
7209 int count = 0;
7210 char_u *start = skipwhite(line);
7211
7212 // :cmd xxx`=expr1`yyy`=expr2`zzz
7213 // PUSHS ":cmd xxx"
7214 // eval expr1
7215 // PUSHS "yyy"
7216 // eval expr2
7217 // PUSHS "zzz"
7218 // EXECCONCAT 5
7219 for (;;)
7220 {
7221 if (p > start)
7222 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007223 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007224 ++count;
7225 }
7226 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007227 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007228 return NULL;
7229 may_generate_2STRING(-1, cctx);
7230 ++count;
7231 p = skipwhite(p);
7232 if (*p != '`')
7233 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007234 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007235 return NULL;
7236 }
7237 start = p + 1;
7238
7239 p = (char_u *)strstr((char *)start, "`=");
7240 if (p == NULL)
7241 {
7242 if (*skipwhite(start) != NUL)
7243 {
7244 generate_PUSHS(cctx, vim_strsave(start));
7245 ++count;
7246 }
7247 break;
7248 }
7249 }
7250 generate_EXECCONCAT(cctx, count);
7251 }
7252 else
7253 generate_EXEC(cctx, line);
7254
7255theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007256 if (*nextcmd != NUL)
7257 {
7258 // the parser expects a pointer to the bar, put it back
7259 --nextcmd;
7260 *nextcmd = '|';
7261 }
7262
7263 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007264}
7265
7266/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007267 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007268 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007269 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007270 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007271add_def_function(ufunc_T *ufunc)
7272{
7273 dfunc_T *dfunc;
7274
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007275 if (def_functions.ga_len == 0)
7276 {
7277 // The first position is not used, so that a zero uf_dfunc_idx means it
7278 // wasn't set.
7279 if (ga_grow(&def_functions, 1) == FAIL)
7280 return FAIL;
7281 ++def_functions.ga_len;
7282 }
7283
Bram Moolenaar09689a02020-05-09 22:50:08 +02007284 // Add the function to "def_functions".
7285 if (ga_grow(&def_functions, 1) == FAIL)
7286 return FAIL;
7287 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7288 CLEAR_POINTER(dfunc);
7289 dfunc->df_idx = def_functions.ga_len;
7290 ufunc->uf_dfunc_idx = dfunc->df_idx;
7291 dfunc->df_ufunc = ufunc;
7292 ++def_functions.ga_len;
7293 return OK;
7294}
7295
7296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 * After ex_function() has collected all the function lines: parse and compile
7298 * the lines into instructions.
7299 * Adds the function to "def_functions".
7300 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7301 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007302 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007303 * This can be used recursively through compile_lambda(), which may reallocate
7304 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007305 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007307 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007308compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 char_u *line = NULL;
7311 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313 cctx_T cctx;
7314 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007315 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 int ret = FAIL;
7317 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007318 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007319 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007320 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007322 // When using a function that was compiled before: Free old instructions.
7323 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007324 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007326 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7327 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007328 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007330 else
7331 {
7332 if (add_def_function(ufunc) == FAIL)
7333 return FAIL;
7334 new_def_function = TRUE;
7335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336
Bram Moolenaar985116a2020-07-12 17:31:09 +02007337 ufunc->uf_def_status = UF_COMPILING;
7338
Bram Moolenaara80faa82020-04-12 19:37:17 +02007339 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 cctx.ctx_ufunc = ufunc;
7341 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007342 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7344 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7345 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7346 cctx.ctx_type_list = &ufunc->uf_type_list;
7347 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7348 instr = &cctx.ctx_instr;
7349
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007350 // Set the context to the function, it may be compiled when called from
7351 // another script. Set the script version to the most modern one.
7352 // The line number will be set in next_line_from_context().
7353 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7355
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007356 // Make sure error messages are OK.
7357 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7358 if (do_estack_push)
7359 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007360 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007361
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007362 if (ufunc->uf_def_args.ga_len > 0)
7363 {
7364 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007365 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007366 int i;
7367 char_u *arg;
7368 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007369 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007370
7371 // Produce instructions for the default values of optional arguments.
7372 // Store the instruction index in uf_def_arg_idx[] so that we know
7373 // where to start when the function is called, depending on the number
7374 // of arguments.
7375 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7376 if (ufunc->uf_def_arg_idx == NULL)
7377 goto erret;
7378 for (i = 0; i < count; ++i)
7379 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007380 garray_T *stack = &cctx.ctx_type_stack;
7381 type_T *val_type;
7382 int arg_idx = first_def_arg + i;
7383
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007384 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7385 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007386 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007387 goto erret;
7388
7389 // If no type specified use the type of the default value.
7390 // Otherwise check that the default value type matches the
7391 // specified type.
7392 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7393 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007394 {
7395 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007396 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007397 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007398 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7399 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007400 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007401
7402 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007403 goto erret;
7404 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007405 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007406
7407 if (did_set_arg_type)
7408 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007409 }
7410
7411 /*
7412 * Loop over all the lines of the function and generate instructions.
7413 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 for (;;)
7415 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007416 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007417 int starts_with_colon = FALSE;
7418 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007419 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007420
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007421 // Bail out on the first error to avoid a flood of errors and report
7422 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007423 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007424 goto erret;
7425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 if (line != NULL && *line == '|')
7427 // the line continues after a '|'
7428 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007429 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007430 && !(*line == '#' && (line == cctx.ctx_line_start
7431 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007433 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 goto erret;
7435 }
7436 else
7437 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007438 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007439 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007440 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 }
7443
Bram Moolenaara80faa82020-04-12 19:37:17 +02007444 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 ea.cmdlinep = &line;
7446 ea.cmd = skipwhite(line);
7447
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007448 // Some things can be recognized by the first character.
7449 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007451 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007452 // "#" starts a comment
7453 line = (char_u *)"";
7454 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007456 case '}':
7457 {
7458 // "}" ends a block scope
7459 scopetype_T stype = cctx.ctx_scope == NULL
7460 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007462 if (stype == BLOCK_SCOPE)
7463 {
7464 compile_endblock(&cctx);
7465 line = ea.cmd;
7466 }
7467 else
7468 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007469 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007470 goto erret;
7471 }
7472 if (line != NULL)
7473 line = skipwhite(ea.cmd + 1);
7474 continue;
7475 }
7476
7477 case '{':
7478 // "{" starts a block scope
7479 // "{'a': 1}->func() is something else
7480 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7481 {
7482 line = compile_block(ea.cmd, &cctx);
7483 continue;
7484 }
7485 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 }
7487
7488 /*
7489 * COMMAND MODIFIERS
7490 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007491 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007492 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7493 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 {
7495 if (errormsg != NULL)
7496 goto erret;
7497 // empty line or comment
7498 line = (char_u *)"";
7499 continue;
7500 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007501 generate_cmdmods(&cctx, &local_cmdmod);
7502 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007504 // Check if there was a colon after the last command modifier or before
7505 // the current position.
7506 for (p = ea.cmd; p >= line; --p)
7507 {
7508 if (*p == ':')
7509 starts_with_colon = TRUE;
7510 if (p < ea.cmd && !VIM_ISWHITE(*p))
7511 break;
7512 }
7513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007515 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007517 {
7518 if (*ea.cmd == '(')
7519 // not for "call()"
7520 ea.cmd = p;
7521 else
7522 ea.cmd = skipwhite(ea.cmd);
7523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007525 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007527 char_u *pskip;
7528
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007529 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007530 // find what follows.
7531 // Skip over "var.member", "var[idx]" and the like.
7532 // Also "&opt = val", "$ENV = val" and "@r = val".
7533 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007534 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007535 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007536 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007538 char_u *var_end;
7539 int oplen;
7540 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541
Bram Moolenaar65821722020-08-02 18:58:54 +02007542 if (ea.cmd[0] == '@')
7543 var_end = ea.cmd + 2;
7544 else
7545 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007546 FNE_CHECK_START | FNE_INCL_BR);
7547 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007548 if (oplen > 0)
7549 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007550 size_t len = p - ea.cmd;
7551
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007552 // Recognize an assignment if we recognize the variable
7553 // name:
7554 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007555 // "local = expr" where "local" is a local var.
7556 // "script = expr" where "script" is a script-local var.
7557 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007558 // "&opt = expr"
7559 // "$ENV = expr"
7560 // "@r = expr"
7561 if (*ea.cmd == '&'
7562 || *ea.cmd == '$'
7563 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007564 || ((len) > 2 && ea.cmd[1] == ':')
7565 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007566 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007567 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007568 || script_var_exists(ea.cmd, len,
7569 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007570 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007571 {
7572 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007573 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007574 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007575 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 }
7578 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007579
7580 if (*ea.cmd == '[')
7581 {
7582 // [var, var] = expr
7583 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7584 if (line == NULL)
7585 goto erret;
7586 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007587 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 }
7590
7591 /*
7592 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007593 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007595 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007596 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007597 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007598 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007599 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007600 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007601 if (!starts_with_colon)
7602 {
7603 emsg(_(e_colon_required_before_a_range));
7604 goto erret;
7605 }
7606 if (ends_excmd2(line, ea.cmd))
7607 {
7608 // A range without a command: jump to the line.
7609 // TODO: compile to a more efficient command, possibly
7610 // calling parse_cmd_address().
7611 ea.cmdidx = CMD_SIZE;
7612 line = compile_exec(line, &ea, &cctx);
7613 goto nextline;
7614 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007615 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007616 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007617 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007618 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007619 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620
7621 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7622 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007623 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007624 {
7625 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007626 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007627 }
7628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007630 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007632 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007633 iemsg("Command from find_ex_command() not handled");
7634 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 }
7637
Bram Moolenaar3988f642020-08-27 22:43:03 +02007638 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007639 && ea.cmdidx != CMD_elseif
7640 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007641 && ea.cmdidx != CMD_endif
7642 && ea.cmdidx != CMD_endfor
7643 && ea.cmdidx != CMD_endwhile
7644 && ea.cmdidx != CMD_catch
7645 && ea.cmdidx != CMD_finally
7646 && ea.cmdidx != CMD_endtry)
7647 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007648 emsg(_(e_unreachable_code_after_return));
7649 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007650 }
7651
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007652 p = skipwhite(p);
7653 if (ea.cmdidx != CMD_SIZE
7654 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7655 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007656 if (ea.cmdidx >= 0)
7657 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007658 if ((ea.argt & EX_BANG) && *p == '!')
7659 {
7660 ea.forceit = TRUE;
7661 p = skipwhite(p + 1);
7662 }
7663 }
7664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 switch (ea.cmdidx)
7666 {
7667 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007668 ea.arg = p;
7669 line = compile_nested_function(&ea, &cctx);
7670 break;
7671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007673 // TODO: should we allow this, e.g. to declare a global
7674 // function?
7675 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 goto erret;
7677
7678 case CMD_return:
7679 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007680 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 break;
7682
7683 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007684 emsg(_(e_cannot_use_let_in_vim9_script));
7685 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007686 case CMD_var:
7687 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 case CMD_const:
7689 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007690 if (line == p)
7691 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 break;
7693
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007694 case CMD_unlet:
7695 case CMD_unlockvar:
7696 case CMD_lockvar:
7697 line = compile_unletlock(p, &ea, &cctx);
7698 break;
7699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 case CMD_import:
7701 line = compile_import(p, &cctx);
7702 break;
7703
7704 case CMD_if:
7705 line = compile_if(p, &cctx);
7706 break;
7707 case CMD_elseif:
7708 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007709 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 break;
7711 case CMD_else:
7712 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007713 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 break;
7715 case CMD_endif:
7716 line = compile_endif(p, &cctx);
7717 break;
7718
7719 case CMD_while:
7720 line = compile_while(p, &cctx);
7721 break;
7722 case CMD_endwhile:
7723 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007724 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 break;
7726
7727 case CMD_for:
7728 line = compile_for(p, &cctx);
7729 break;
7730 case CMD_endfor:
7731 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007732 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 break;
7734 case CMD_continue:
7735 line = compile_continue(p, &cctx);
7736 break;
7737 case CMD_break:
7738 line = compile_break(p, &cctx);
7739 break;
7740
7741 case CMD_try:
7742 line = compile_try(p, &cctx);
7743 break;
7744 case CMD_catch:
7745 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007746 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 break;
7748 case CMD_finally:
7749 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007750 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 break;
7752 case CMD_endtry:
7753 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007754 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 break;
7756 case CMD_throw:
7757 line = compile_throw(p, &cctx);
7758 break;
7759
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007760 case CMD_eval:
7761 if (compile_expr0(&p, &cctx) == FAIL)
7762 goto erret;
7763
Bram Moolenaar3988f642020-08-27 22:43:03 +02007764 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007765 generate_instr_drop(&cctx, ISN_DROP, 1);
7766
7767 line = skipwhite(p);
7768 break;
7769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007772 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007773 case CMD_echomsg:
7774 case CMD_echoerr:
7775 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007776 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007778 case CMD_put:
7779 ea.cmd = cmd;
7780 line = compile_put(p, &ea, &cctx);
7781 break;
7782
Bram Moolenaar3988f642020-08-27 22:43:03 +02007783 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007784
Bram Moolenaarae616492020-07-28 20:07:27 +02007785 case CMD_append:
7786 case CMD_change:
7787 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007788 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007789 case CMD_xit:
7790 not_in_vim9(&ea);
7791 goto erret;
7792
Bram Moolenaar002262f2020-07-08 17:47:57 +02007793 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007794 if (cctx.ctx_skip != SKIP_YES)
7795 {
7796 semsg(_(e_invalid_command_str), ea.cmd);
7797 goto erret;
7798 }
7799 // We don't check for a next command here.
7800 line = (char_u *)"";
7801 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007804 if (cctx.ctx_skip == SKIP_YES)
7805 {
7806 // We don't check for a next command here.
7807 line = (char_u *)"";
7808 }
7809 else
7810 {
7811 // Not recognized, execute with do_cmdline_cmd().
7812 ea.arg = p;
7813 line = compile_exec(line, &ea, &cctx);
7814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 break;
7816 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007817nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 if (line == NULL)
7819 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007820 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007822 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007823 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 if (cctx.ctx_type_stack.ga_len < 0)
7826 {
7827 iemsg("Type stack underflow");
7828 goto erret;
7829 }
7830 }
7831
7832 if (cctx.ctx_scope != NULL)
7833 {
7834 if (cctx.ctx_scope->se_type == IF_SCOPE)
7835 emsg(_(e_endif));
7836 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7837 emsg(_(e_endwhile));
7838 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7839 emsg(_(e_endfor));
7840 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007841 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 goto erret;
7843 }
7844
Bram Moolenaarefd88552020-06-18 20:50:10 +02007845 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 {
7847 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7848 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007849 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 goto erret;
7851 }
7852
7853 // Return zero if there is no return at the end.
7854 generate_PUSHNR(&cctx, 0);
7855 generate_instr(&cctx, ISN_RETURN);
7856 }
7857
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007858 {
7859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7860 + ufunc->uf_dfunc_idx;
7861 dfunc->df_deleted = FALSE;
7862 dfunc->df_instr = instr->ga_data;
7863 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007864 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007865 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007866 if (cctx.ctx_outer_used)
7867 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007868 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870
7871 ret = OK;
7872
7873erret:
7874 if (ret == FAIL)
7875 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007876 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7878 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007879
7880 for (idx = 0; idx < instr->ga_len; ++idx)
7881 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007883
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007884 // If using the last entry in the table and it was added above, we
7885 // might as well remove it.
7886 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007887 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007888 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007889 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007890 ufunc->uf_dfunc_idx = 0;
7891 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007892 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007893
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007894 while (cctx.ctx_scope != NULL)
7895 drop_scope(&cctx);
7896
Bram Moolenaar20431c92020-03-20 18:39:46 +01007897 // Don't execute this function body.
7898 ga_clear_strings(&ufunc->uf_lines);
7899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 if (errormsg != NULL)
7901 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007902 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007903 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 }
7905
7906 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007907 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007908 if (do_estack_push)
7909 estack_pop();
7910
Bram Moolenaar20431c92020-03-20 18:39:46 +01007911 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007912 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007914 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915}
7916
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007917 void
7918set_function_type(ufunc_T *ufunc)
7919{
7920 int varargs = ufunc->uf_va_name != NULL;
7921 int argcount = ufunc->uf_args.ga_len;
7922
7923 // Create a type for the function, with the return type and any
7924 // argument types.
7925 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7926 // The type is included in "tt_args".
7927 if (argcount > 0 || varargs)
7928 {
7929 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7930 argcount, &ufunc->uf_type_list);
7931 // Add argument types to the function type.
7932 if (func_type_add_arg_types(ufunc->uf_func_type,
7933 argcount + varargs,
7934 &ufunc->uf_type_list) == FAIL)
7935 return;
7936 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7937 ufunc->uf_func_type->tt_min_argcount =
7938 argcount - ufunc->uf_def_args.ga_len;
7939 if (ufunc->uf_arg_types == NULL)
7940 {
7941 int i;
7942
7943 // lambda does not have argument types.
7944 for (i = 0; i < argcount; ++i)
7945 ufunc->uf_func_type->tt_args[i] = &t_any;
7946 }
7947 else
7948 mch_memmove(ufunc->uf_func_type->tt_args,
7949 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7950 if (varargs)
7951 {
7952 ufunc->uf_func_type->tt_args[argcount] =
7953 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7954 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7955 }
7956 }
7957 else
7958 // No arguments, can use a predefined type.
7959 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7960 argcount, &ufunc->uf_type_list);
7961}
7962
7963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964/*
7965 * Delete an instruction, free what it contains.
7966 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007967 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007968delete_instr(isn_T *isn)
7969{
7970 switch (isn->isn_type)
7971 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007972 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973 case ISN_EXEC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007974 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975 case ISN_LOADENV:
7976 case ISN_LOADG:
7977 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007978 case ISN_LOADT:
7979 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007980 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007981 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 case ISN_PUSHS:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007983 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007984 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007986 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007987 case ISN_STOREW:
7988 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 vim_free(isn->isn_arg.string);
7990 break;
7991
7992 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007993 case ISN_STORES:
7994 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 break;
7996
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007997 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007998 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007999 vim_free(isn->isn_arg.unlet.ul_name);
8000 break;
8001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002 case ISN_STOREOPT:
8003 vim_free(isn->isn_arg.storeopt.so_name);
8004 break;
8005
8006 case ISN_PUSHBLOB: // push blob isn_arg.blob
8007 blob_unref(isn->isn_arg.blob);
8008 break;
8009
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008010 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008012 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008013#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008014 break;
8015
8016 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008017#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008018 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008019#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008020 break;
8021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022 case ISN_UCALL:
8023 vim_free(isn->isn_arg.ufunc.cuf_name);
8024 break;
8025
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008026 case ISN_FUNCREF:
8027 {
8028 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8029 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008030
8031 if (func_name_refcount(dfunc->df_ufunc->uf_name))
8032 func_ptr_unref(dfunc->df_ufunc);
8033 }
8034 break;
8035
8036 case ISN_DCALL:
8037 {
8038 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8039 + isn->isn_arg.dfunc.cdf_idx;
8040
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008041 if (dfunc->df_ufunc != NULL
8042 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008043 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008044 }
8045 break;
8046
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008047 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008048 {
8049 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8050 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8051
8052 if (ufunc != NULL)
8053 {
8054 // Clear uf_dfunc_idx so that the function is deleted.
8055 clear_def_function(ufunc);
8056 ufunc->uf_dfunc_idx = 0;
8057 func_ptr_unref(ufunc);
8058 }
8059
8060 vim_free(lambda);
8061 vim_free(isn->isn_arg.newfunc.nf_global);
8062 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008063 break;
8064
Bram Moolenaar5e654232020-09-16 15:22:00 +02008065 case ISN_CHECKTYPE:
8066 free_type(isn->isn_arg.type.ct_type);
8067 break;
8068
Bram Moolenaar02194d22020-10-24 23:08:38 +02008069 case ISN_CMDMOD:
8070 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8071 ->cmod_filter_regmatch.regprog);
8072 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8073 break;
8074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 case ISN_2BOOL:
8076 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008077 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008078 case ISN_ADDBLOB:
8079 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008080 case ISN_ANYINDEX:
8081 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008083 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008085 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008087 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 case ISN_COMPAREANY:
8089 case ISN_COMPAREBLOB:
8090 case ISN_COMPAREBOOL:
8091 case ISN_COMPAREDICT:
8092 case ISN_COMPAREFLOAT:
8093 case ISN_COMPAREFUNC:
8094 case ISN_COMPARELIST:
8095 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 case ISN_COMPARESPECIAL:
8097 case ISN_COMPARESTRING:
8098 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008099 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 case ISN_DROP:
8101 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008102 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008103 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008105 case ISN_EXECCONCAT:
8106 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008108 case ISN_GETITEM:
8109 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008110 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008111 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008112 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008114 case ISN_LOADBDICT:
8115 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008116 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008118 case ISN_LOADSCRIPT:
8119 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008121 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008122 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008123 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124 case ISN_NEGATENR:
8125 case ISN_NEWDICT:
8126 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008128 case ISN_OPFLOAT:
8129 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008130 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008131 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008132 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 case ISN_PUSHF:
8134 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008136 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008138 case ISN_SHUFFLE:
8139 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008141 case ISN_STOREDICT:
8142 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008143 case ISN_STORENR:
8144 case ISN_STOREOUTER:
8145 case ISN_STOREREG:
8146 case ISN_STORESCRIPT:
8147 case ISN_STOREV:
8148 case ISN_STRINDEX:
8149 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 case ISN_THROW:
8151 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008152 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 // nothing allocated
8154 break;
8155 }
8156}
8157
8158/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01008159 * Free all instructions for "dfunc".
8160 */
8161 static void
8162delete_def_function_contents(dfunc_T *dfunc)
8163{
8164 int idx;
8165
8166 ga_clear(&dfunc->df_def_args_isn);
8167
8168 if (dfunc->df_instr != NULL)
8169 {
8170 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8171 delete_instr(dfunc->df_instr + idx);
8172 VIM_CLEAR(dfunc->df_instr);
8173 }
8174
8175 dfunc->df_deleted = TRUE;
8176}
8177
8178/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008179 * When a user function is deleted, clear the contents of any associated def
8180 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008181 */
8182 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008183clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008185 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 {
8187 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8188 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
Bram Moolenaar20431c92020-03-20 18:39:46 +01008190 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008191 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 }
8193}
8194
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008195/*
8196 * Used when a user function is about to be deleted: remove the pointer to it.
8197 * The entry in def_functions is then unused.
8198 */
8199 void
8200unlink_def_function(ufunc_T *ufunc)
8201{
8202 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
8203
8204 dfunc->df_ufunc = NULL;
8205}
8206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008208/*
8209 * Free all functions defined with ":def".
8210 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008211 void
8212free_def_functions(void)
8213{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008214 int idx;
8215
8216 for (idx = 0; idx < def_functions.ga_len; ++idx)
8217 {
8218 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8219
8220 delete_def_function_contents(dfunc);
8221 }
8222
8223 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224}
8225#endif
8226
8227
8228#endif // FEAT_EVAL