blob: 39c36dfb24fdd55a44dd8ca26e82f9a8387ed2dd [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;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaarb2049902021-01-24 12:53:53 +0100177 int ctx_profiling; // when TRUE generate ISN_PROF_START
178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200180 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200182 int ctx_has_closure; // set to one if a closures was created in
183 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185 garray_T ctx_imports; // imported items
186
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200187 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200189 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200191 cctx_T *ctx_outer; // outer scope for lambda or nested
192 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200193 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200196 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200197
Bram Moolenaar02194d22020-10-24 23:08:38 +0200198 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200199
200 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
201 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202};
203
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100204static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205
206/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100207 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100208 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * If "lvar" is NULL only check if the variable can be found.
210 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 static int
213lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214{
215 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100218 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200220
221 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
223 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100224 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
225 if (STRNCMP(name, lvp->lv_name, len) == 0
226 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 if (lvar != NULL)
229 {
230 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100231 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 }
233 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236
237 // Find local in outer function scope.
238 if (cctx->ctx_outer != NULL)
239 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100240 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lvar != NULL)
243 {
244 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100245 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 }
247 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200248 }
249 }
250
Bram Moolenaar709664c2020-12-12 14:33:41 +0100251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252}
253
254/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 * Lookup an argument in the current function and an enclosing function.
256 * Returns the argument index in "idxp"
257 * Returns the argument type in "type"
258 * Sets "gen_load_outer" to TRUE if found in outer scope.
259 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 */
261 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200263 char_u *name,
264 size_t len,
265 int *idxp,
266 type_T **type,
267 int *gen_load_outer,
268 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269{
270 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200271 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100273 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
276 {
277 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
278
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200279 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
280 {
281 if (idxp != NULL)
282 {
283 // Arguments are located above the frame pointer. One further
284 // if there is a vararg argument
285 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
286 + STACK_FRAME_SIZE)
287 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
288
289 if (cctx->ctx_ufunc->uf_arg_types != NULL)
290 *type = cctx->ctx_ufunc->uf_arg_types[idx];
291 else
292 *type = &t_any;
293 }
294 return OK;
295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200298 va_name = cctx->ctx_ufunc->uf_va_name;
299 if (va_name != NULL
300 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
301 {
302 if (idxp != NULL)
303 {
304 // varargs is always the last argument
305 *idxp = -STACK_FRAME_SIZE - 1;
306 *type = cctx->ctx_ufunc->uf_va_type;
307 }
308 return OK;
309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200311 if (cctx->ctx_outer != NULL)
312 {
313 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 == OK)
316 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100317 if (gen_load_outer != NULL)
318 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 return OK;
320 }
321 }
322
323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324}
325
326/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * Lookup a script-local variable in the current script, possibly defined in a
328 * block that contains the function "cctx->ctx_ufunc".
329 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100330 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Return NULL when not found.
332 */
333 static sallvar_T *
334find_script_var(char_u *name, size_t len, cctx_T *cctx)
335{
336 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
337 hashitem_T *hi;
338 int cc;
339 sallvar_T *sav;
340 ufunc_T *ufunc;
341
342 // Find the list of all script variables with the right name.
343 if (len > 0)
344 {
345 cc = name[len];
346 name[len] = NUL;
347 }
348 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
349 if (len > 0)
350 name[len] = cc;
351 if (HASHITEM_EMPTY(hi))
352 return NULL;
353
354 sav = HI2SAV(hi);
355 if (sav->sav_block_id == 0 || cctx == NULL)
356 // variable defined in the script scope or not in a function.
357 return sav;
358
359 // Go over the variables with this name and find one that was visible
360 // from the function.
361 ufunc = cctx->ctx_ufunc;
362 while (sav != NULL)
363 {
364 int idx;
365
366 // Go over the blocks that this function was defined in. If the
367 // variable block ID matches it was visible to the function.
368 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
369 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
370 return sav;
371 sav = sav->sav_next;
372 }
373
374 return NULL;
375}
376
377/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100378 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200379 */
380 static int
381script_is_vim9()
382{
383 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
384}
385
386/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200388 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 * Returns OK or FAIL.
390 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200391 static int
392script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200394 if (current_sctx.sc_sid <= 0)
395 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200396 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200397 {
398 // Check script variables that were visible where the function was
399 // defined.
400 if (find_script_var(name, len, cctx) != NULL)
401 return OK;
402 }
403 else
404 {
405 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
406 dictitem_T *di;
407 int cc;
408
409 // Check script variables that are currently visible
410 cc = name[len];
411 name[len] = NUL;
412 di = find_var_in_ht(ht, 0, name, TRUE);
413 name[len] = cc;
414 if (di != NULL)
415 return OK;
416 }
417
418 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100422 * Return TRUE if "name" is a local variable, argument, script variable or
423 * imported.
424 */
425 static int
426variable_exists(char_u *name, size_t len, cctx_T *cctx)
427{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100428 return (cctx != NULL
429 && (lookup_local(name, len, NULL, cctx) == OK
430 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200431 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100432 || find_imported(name, len, cctx) != NULL;
433}
434
435/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436 * Return TRUE if "name" is a local variable, argument, script variable,
437 * imported or function.
438 */
439 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100440item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100441{
442 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100443 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100444
445 if (variable_exists(name, len, cctx))
446 return TRUE;
447
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100448 // This is similar to what is in lookup_scriptitem():
449 // Find a function, so that a following "->" works.
450 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
451 // a function call.
452 p = skipwhite(name + len);
453
454 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
455 {
456 // Do not check for an internal function, since it might also be a
457 // valid command, such as ":split" versuse "split()".
458 // Skip "g:" before a function name.
459 is_global = (name[0] == 'g' && name[1] == ':');
460 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
461 }
462 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463}
464
465/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100466 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200467 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200468 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100469 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100470 * Return FAIL and give an error if it defined.
471 */
472 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100473check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100474{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200475 int c = p[len];
476 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200477
Bram Moolenaarda479c72021-04-10 21:01:38 +0200478 // underscore argument is OK
479 if (len == 1 && *p == '_')
480 return OK;
481
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200482 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100483 {
484 if (is_arg)
485 semsg(_(e_argument_already_declared_in_script_str), p);
486 else
487 semsg(_(e_variable_already_declared_in_script_str), p);
488 return FAIL;
489 }
490
Bram Moolenaarad486a02020-08-01 23:22:18 +0200491 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100493 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200494 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200496 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100497 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 // A local or script-local function can shadow a global function.
499 if (ufunc == NULL || !func_is_global(ufunc)
500 || (p[0] == 'g' && p[1] == ':'))
501 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 if (is_arg)
503 semsg(_(e_argument_name_shadows_existing_variable_str), p);
504 else
505 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200506 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200507 return FAIL;
508 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100509 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 return OK;
512}
513
Bram Moolenaar65b95452020-07-19 14:03:09 +0200514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515/////////////////////////////////////////////////////////////////////
516// Following generate_ functions expect the caller to call ga_grow().
517
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200518#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
519#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521/*
522 * Generate an instruction without arguments.
523 * Returns a pointer to the new instruction, NULL if failed.
524 */
525 static isn_T *
526generate_instr(cctx_T *cctx, isntype_T isn_type)
527{
528 garray_T *instr = &cctx->ctx_instr;
529 isn_T *isn;
530
Bram Moolenaar080457c2020-03-03 21:53:32 +0100531 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 if (ga_grow(instr, 1) == FAIL)
533 return NULL;
534 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
535 isn->isn_type = isn_type;
536 isn->isn_lnum = cctx->ctx_lnum + 1;
537 ++instr->ga_len;
538
539 return isn;
540}
541
542/*
543 * Generate an instruction without arguments.
544 * "drop" will be removed from the stack.
545 * Returns a pointer to the new instruction, NULL if failed.
546 */
547 static isn_T *
548generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
549{
550 garray_T *stack = &cctx->ctx_type_stack;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 stack->ga_len -= drop;
554 return generate_instr(cctx, isn_type);
555}
556
557/*
558 * Generate instruction "isn_type" and put "type" on the type stack.
559 */
560 static isn_T *
561generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
562{
563 isn_T *isn;
564 garray_T *stack = &cctx->ctx_type_stack;
565
566 if ((isn = generate_instr(cctx, isn_type)) == NULL)
567 return NULL;
568
569 if (ga_grow(stack, 1) == FAIL)
570 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200571 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 ++stack->ga_len;
573
574 return isn;
575}
576
577/*
578 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200579 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 */
581 static int
582may_generate_2STRING(int offset, cctx_T *cctx)
583{
584 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200585 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100587 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100589 RETURN_OK_IF_SKIP(cctx);
590 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200591 switch ((*type)->tt_type)
592 {
593 // nothing to be done
594 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200596 // conversion possible
597 case VAR_SPECIAL:
598 case VAR_BOOL:
599 case VAR_NUMBER:
600 case VAR_FLOAT:
601 break;
602
603 // conversion possible (with runtime check)
604 case VAR_ANY:
605 case VAR_UNKNOWN:
606 isntype = ISN_2STRING_ANY;
607 break;
608
609 // conversion not possible
610 case VAR_VOID:
611 case VAR_BLOB:
612 case VAR_FUNC:
613 case VAR_PARTIAL:
614 case VAR_LIST:
615 case VAR_DICT:
616 case VAR_JOB:
617 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200618 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200619 to_string_error((*type)->tt_type);
620 return FAIL;
621 }
622
623 *type = &t_string;
624 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 return FAIL;
626 isn->isn_arg.number = offset;
627
628 return OK;
629}
630
631 static int
632check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
633{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200639 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200641 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 return FAIL;
643 }
644 return OK;
645}
646
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200647 static int
648generate_add_instr(
649 cctx_T *cctx,
650 vartype_T vartype,
651 type_T *type1,
652 type_T *type2)
653{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100654 garray_T *stack = &cctx->ctx_type_stack;
655 isn_T *isn = generate_instr_drop(cctx,
656 vartype == VAR_NUMBER ? ISN_OPNR
657 : vartype == VAR_LIST ? ISN_ADDLIST
658 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200659#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100660 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200661#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100662 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663
664 if (vartype != VAR_LIST && vartype != VAR_BLOB
665 && type1->tt_type != VAR_ANY
666 && type2->tt_type != VAR_ANY
667 && check_number_or_float(
668 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
669 return FAIL;
670
671 if (isn != NULL)
672 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100673
674 // When concatenating two lists with different member types the member type
675 // becomes "any".
676 if (vartype == VAR_LIST
677 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
678 && type1->tt_member != type2->tt_member)
679 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
680
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200681 return isn == NULL ? FAIL : OK;
682}
683
684/*
685 * Get the type to use for an instruction for an operation on "type1" and
686 * "type2". If they are matching use a type-specific instruction. Otherwise
687 * fall back to runtime type checking.
688 */
689 static vartype_T
690operator_type(type_T *type1, type_T *type2)
691{
692 if (type1->tt_type == type2->tt_type
693 && (type1->tt_type == VAR_NUMBER
694 || type1->tt_type == VAR_LIST
695#ifdef FEAT_FLOAT
696 || type1->tt_type == VAR_FLOAT
697#endif
698 || type1->tt_type == VAR_BLOB))
699 return type1->tt_type;
700 return VAR_ANY;
701}
702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703/*
704 * Generate an instruction with two arguments. The instruction depends on the
705 * type of the arguments.
706 */
707 static int
708generate_two_op(cctx_T *cctx, char_u *op)
709{
710 garray_T *stack = &cctx->ctx_type_stack;
711 type_T *type1;
712 type_T *type2;
713 vartype_T vartype;
714 isn_T *isn;
715
Bram Moolenaar080457c2020-03-03 21:53:32 +0100716 RETURN_OK_IF_SKIP(cctx);
717
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200718 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
720 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200721 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722
723 switch (*op)
724 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200725 case '+':
726 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 break;
729
730 case '-':
731 case '*':
732 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
733 op) == FAIL)
734 return FAIL;
735 if (vartype == VAR_NUMBER)
736 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
737#ifdef FEAT_FLOAT
738 else if (vartype == VAR_FLOAT)
739 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
740#endif
741 else
742 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
743 if (isn != NULL)
744 isn->isn_arg.op.op_type = *op == '*'
745 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
746 break;
747
Bram Moolenaar4c683752020-04-05 21:38:23 +0200748 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200750 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 && type2->tt_type != VAR_NUMBER))
752 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200753 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 return FAIL;
755 }
756 isn = generate_instr_drop(cctx,
757 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
758 if (isn != NULL)
759 isn->isn_arg.op.op_type = EXPR_REM;
760 break;
761 }
762
763 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200764 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 {
766 type_T *type = &t_any;
767
768#ifdef FEAT_FLOAT
769 // float+number and number+float results in float
770 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
771 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
772 type = &t_float;
773#endif
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
775 }
776
777 return OK;
778}
779
780/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200781 * Get the instruction to use for comparing "type1" with "type2"
782 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200784 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100785get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786{
787 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaar4c683752020-04-05 21:38:23 +0200789 if (type1 == VAR_UNKNOWN)
790 type1 = VAR_ANY;
791 if (type2 == VAR_UNKNOWN)
792 type2 = VAR_ANY;
793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if (type1 == type2)
795 {
796 switch (type1)
797 {
798 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
799 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
800 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
801 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
802 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
803 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
804 case VAR_LIST: isntype = ISN_COMPARELIST; break;
805 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
806 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 default: isntype = ISN_COMPAREANY; break;
808 }
809 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100812 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 isntype = ISN_COMPAREANY;
814
Bram Moolenaar657137c2021-01-09 15:45:23 +0100815 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 && (isntype == ISN_COMPAREBOOL
817 || isntype == ISN_COMPARESPECIAL
818 || isntype == ISN_COMPARENR
819 || isntype == ISN_COMPAREFLOAT))
820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200821 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100822 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200823 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 }
825 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100826 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
828 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100829 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
830 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 && (type1 == VAR_BLOB || type2 == VAR_BLOB
832 || type1 == VAR_LIST || type2 == VAR_LIST))))
833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200834 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200836 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200838 return isntype;
839}
840
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200841 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100842check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200843{
844 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
845 return FAIL;
846 return OK;
847}
848
Bram Moolenaara5565e42020-05-09 15:44:01 +0200849/*
850 * Generate an ISN_COMPARE* instruction with a boolean result.
851 */
852 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100853generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200854{
855 isntype_T isntype;
856 isn_T *isn;
857 garray_T *stack = &cctx->ctx_type_stack;
858 vartype_T type1;
859 vartype_T type2;
860
861 RETURN_OK_IF_SKIP(cctx);
862
863 // Get the known type of the two items on the stack. If they are matching
864 // use a type-specific instruction. Otherwise fall back to runtime type
865 // checking.
866 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
867 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 if (isntype == ISN_DROP)
870 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871
872 if ((isn = generate_instr(cctx, isntype)) == NULL)
873 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100874 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 isn->isn_arg.op.op_ic = ic;
876
877 // takes two arguments, puts one bool back
878 if (stack->ga_len >= 2)
879 {
880 --stack->ga_len;
881 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
882 }
883
884 return OK;
885}
886
887/*
888 * Generate an ISN_2BOOL instruction.
889 */
890 static int
891generate_2BOOL(cctx_T *cctx, int invert)
892{
893 isn_T *isn;
894 garray_T *stack = &cctx->ctx_type_stack;
895
Bram Moolenaar080457c2020-03-03 21:53:32 +0100896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
898 return FAIL;
899 isn->isn_arg.number = invert;
900
901 // type becomes bool
902 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
903
904 return OK;
905}
906
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200907/*
908 * Generate an ISN_COND2BOOL instruction.
909 */
910 static int
911generate_COND2BOOL(cctx_T *cctx)
912{
913 isn_T *isn;
914 garray_T *stack = &cctx->ctx_type_stack;
915
916 RETURN_OK_IF_SKIP(cctx);
917 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
918 return FAIL;
919
920 // type becomes bool
921 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
922
923 return OK;
924}
925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200927generate_TYPECHECK(
928 cctx_T *cctx,
929 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100930 int offset,
931 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932{
933 isn_T *isn;
934 garray_T *stack = &cctx->ctx_type_stack;
935
Bram Moolenaar080457c2020-03-03 21:53:32 +0100936 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
938 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200939 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100940 isn->isn_arg.type.ct_off = (int8_T)offset;
941 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
Bram Moolenaar5e654232020-09-16 15:22:00 +0200943 // type becomes expected
944 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945
946 return OK;
947}
948
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100949 static int
950generate_SETTYPE(
951 cctx_T *cctx,
952 type_T *expected)
953{
954 isn_T *isn;
955
956 RETURN_OK_IF_SKIP(cctx);
957 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
958 return FAIL;
959 isn->isn_arg.type.ct_type = alloc_type(expected);
960 return OK;
961}
962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200964 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
965 * used. Return FALSE if the types will never match.
966 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100967 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200968use_typecheck(type_T *actual, type_T *expected)
969{
970 if (actual->tt_type == VAR_ANY
971 || actual->tt_type == VAR_UNKNOWN
972 || (actual->tt_type == VAR_FUNC
973 && (expected->tt_type == VAR_FUNC
974 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100975 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
976 && ((actual->tt_member == &t_void)
977 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200978 return TRUE;
979 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
980 && actual->tt_type == expected->tt_type)
981 // This takes care of a nested list or dict.
982 return use_typecheck(actual->tt_member, expected->tt_member);
983 return FALSE;
984}
985
986/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200987 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200988 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200989 * - "actual" is a type that can be "expected" type: add a runtime check; or
990 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200991 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
992 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200993 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200995need_type(
996 type_T *actual,
997 type_T *expected,
998 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100999 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001000 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001001 int silent,
1002 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001003{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001004 where_T where;
1005
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001006 if (expected == &t_bool && actual != &t_bool
1007 && (actual->tt_flags & TTFLAG_BOOL_OK))
1008 {
1009 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1010 // boolean is OK but requires a conversion.
1011 generate_2BOOL(cctx, FALSE);
1012 return OK;
1013 }
1014
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001015 where.wt_index = arg_idx;
1016 where.wt_variable = FALSE;
1017 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001018 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001019
1020 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001021 // If it's a constant a runtime check makes no sense.
1022 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001023 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001024 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001025 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001026 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001027
1028 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001029 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001030 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001031}
1032
1033/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001034 * Check that the top of the type stack has a type that can be used as a
1035 * condition. Give an error and return FAIL if not.
1036 */
1037 static int
1038bool_on_stack(cctx_T *cctx)
1039{
1040 garray_T *stack = &cctx->ctx_type_stack;
1041 type_T *type;
1042
1043 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1044 if (type == &t_bool)
1045 return OK;
1046
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001047 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001048 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1049 // This requires a runtime type check.
1050 return generate_COND2BOOL(cctx);
1051
Bram Moolenaar351ead02021-01-16 16:07:01 +01001052 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001053}
1054
1055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 * Generate an ISN_PUSHNR instruction.
1057 */
1058 static int
1059generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1060{
1061 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001062 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063
Bram Moolenaar080457c2020-03-03 21:53:32 +01001064 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1066 return FAIL;
1067 isn->isn_arg.number = number;
1068
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001069 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001070 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001071 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_PUSHBOOL instruction.
1077 */
1078 static int
1079generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1085 return FAIL;
1086 isn->isn_arg.number = number;
1087
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_PUSHSPEC instruction.
1093 */
1094 static int
1095generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1096{
1097 isn_T *isn;
1098
Bram Moolenaar080457c2020-03-03 21:53:32 +01001099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1101 return FAIL;
1102 isn->isn_arg.number = number;
1103
1104 return OK;
1105}
1106
1107#ifdef FEAT_FLOAT
1108/*
1109 * Generate an ISN_PUSHF instruction.
1110 */
1111 static int
1112generate_PUSHF(cctx_T *cctx, float_T fnumber)
1113{
1114 isn_T *isn;
1115
Bram Moolenaar080457c2020-03-03 21:53:32 +01001116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.fnumber = fnumber;
1120
1121 return OK;
1122}
1123#endif
1124
1125/*
1126 * Generate an ISN_PUSHS instruction.
1127 * Consumes "str".
1128 */
1129 static int
1130generate_PUSHS(cctx_T *cctx, char_u *str)
1131{
1132 isn_T *isn;
1133
Bram Moolenaar508b5612021-01-02 18:17:26 +01001134 if (cctx->ctx_skip == SKIP_YES)
1135 {
1136 vim_free(str);
1137 return OK;
1138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.string = str;
1142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 * Generate an ISN_PUSHCHANNEL instruction.
1148 * Consumes "channel".
1149 */
1150 static int
1151generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001156 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.channel = channel;
1159
1160 return OK;
1161}
1162
1163/*
1164 * Generate an ISN_PUSHJOB instruction.
1165 * Consumes "job".
1166 */
1167 static int
1168generate_PUSHJOB(cctx_T *cctx, job_T *job)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001173 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001174 return FAIL;
1175 isn->isn_arg.job = job;
1176
1177 return OK;
1178}
1179
1180/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 * Generate an ISN_PUSHBLOB instruction.
1182 * Consumes "blob".
1183 */
1184 static int
1185generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1186{
1187 isn_T *isn;
1188
Bram Moolenaar080457c2020-03-03 21:53:32 +01001189 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.blob = blob;
1193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001198 * Generate an ISN_PUSHFUNC instruction with name "name".
1199 * Consumes "name".
1200 */
1201 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001202generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001207 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001208 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001209 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001210
1211 return OK;
1212}
1213
1214/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001215 * Generate an ISN_GETITEM instruction with "index".
1216 */
1217 static int
1218generate_GETITEM(cctx_T *cctx, int index)
1219{
1220 isn_T *isn;
1221 garray_T *stack = &cctx->ctx_type_stack;
1222 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1223 type_T *item_type = &t_any;
1224
1225 RETURN_OK_IF_SKIP(cctx);
1226
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001227 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001228 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001229 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001230 emsg(_(e_listreq));
1231 return FAIL;
1232 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001233 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001234 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.number = index;
1237
1238 // add the item type to the type stack
1239 if (ga_grow(stack, 1) == FAIL)
1240 return FAIL;
1241 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1242 ++stack->ga_len;
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001247 * Generate an ISN_SLICE instruction with "count".
1248 */
1249 static int
1250generate_SLICE(cctx_T *cctx, int count)
1251{
1252 isn_T *isn;
1253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.number = count;
1258 return OK;
1259}
1260
1261/*
1262 * Generate an ISN_CHECKLEN instruction with "min_len".
1263 */
1264 static int
1265generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1266{
1267 isn_T *isn;
1268
1269 RETURN_OK_IF_SKIP(cctx);
1270
1271 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.checklen.cl_min_len = min_len;
1274 isn->isn_arg.checklen.cl_more_OK = more_OK;
1275
1276 return OK;
1277}
1278
1279/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 * Generate an ISN_STORE instruction.
1281 */
1282 static int
1283generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1284{
1285 isn_T *isn;
1286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1289 return FAIL;
1290 if (name != NULL)
1291 isn->isn_arg.string = vim_strsave(name);
1292 else
1293 isn->isn_arg.number = idx;
1294
1295 return OK;
1296}
1297
1298/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001299 * Generate an ISN_STOREOUTER instruction.
1300 */
1301 static int
1302generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.outer.outer_idx = idx;
1310 isn->isn_arg.outer.outer_depth = level;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1317 */
1318 static int
1319generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1320{
1321 isn_T *isn;
1322
Bram Moolenaar080457c2020-03-03 21:53:32 +01001323 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1325 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001326 isn->isn_arg.storenr.stnr_idx = idx;
1327 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_STOREOPT instruction
1334 */
1335 static int
1336generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1337{
1338 isn_T *isn;
1339
Bram Moolenaar080457c2020-03-03 21:53:32 +01001340 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001341 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 return FAIL;
1343 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1344 isn->isn_arg.storeopt.so_flags = opt_flags;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_LOAD or similar instruction.
1351 */
1352 static int
1353generate_LOAD(
1354 cctx_T *cctx,
1355 isntype_T isn_type,
1356 int idx,
1357 char_u *name,
1358 type_T *type)
1359{
1360 isn_T *isn;
1361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1364 return FAIL;
1365 if (name != NULL)
1366 isn->isn_arg.string = vim_strsave(name);
1367 else
1368 isn->isn_arg.number = idx;
1369
1370 return OK;
1371}
1372
1373/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001374 * Generate an ISN_LOADOUTER instruction
1375 */
1376 static int
1377generate_LOADOUTER(
1378 cctx_T *cctx,
1379 int idx,
1380 int nesting,
1381 type_T *type)
1382{
1383 isn_T *isn;
1384
1385 RETURN_OK_IF_SKIP(cctx);
1386 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1387 return FAIL;
1388 isn->isn_arg.outer.outer_idx = idx;
1389 isn->isn_arg.outer.outer_depth = nesting;
1390
1391 return OK;
1392}
1393
1394/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001395 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396 */
1397 static int
1398generate_LOADV(
1399 cctx_T *cctx,
1400 char_u *name,
1401 int error)
1402{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001403 int di_flags;
1404 int vidx = find_vim_var(name, &di_flags);
1405 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406
Bram Moolenaar080457c2020-03-03 21:53:32 +01001407 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408 if (vidx < 0)
1409 {
1410 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001411 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 return FAIL;
1413 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001414 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001415
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001416 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001417}
1418
1419/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001420 * Generate an ISN_UNLET instruction.
1421 */
1422 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001423generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001424{
1425 isn_T *isn;
1426
1427 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001428 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001429 return FAIL;
1430 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1431 isn->isn_arg.unlet.ul_forceit = forceit;
1432
1433 return OK;
1434}
1435
1436/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001437 * Generate an ISN_LOCKCONST instruction.
1438 */
1439 static int
1440generate_LOCKCONST(cctx_T *cctx)
1441{
1442 isn_T *isn;
1443
1444 RETURN_OK_IF_SKIP(cctx);
1445 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1446 return FAIL;
1447 return OK;
1448}
1449
1450/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 * Generate an ISN_LOADS instruction.
1452 */
1453 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001456 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001458 int sid,
1459 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460{
1461 isn_T *isn;
1462
Bram Moolenaar080457c2020-03-03 21:53:32 +01001463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 if (isn_type == ISN_LOADS)
1465 isn = generate_instr_type(cctx, isn_type, type);
1466 else
1467 isn = generate_instr_drop(cctx, isn_type, 1);
1468 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1471 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1478 */
1479 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001480generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 cctx_T *cctx,
1482 isntype_T isn_type,
1483 int sid,
1484 int idx,
1485 type_T *type)
1486{
1487 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001488 scriptref_T *sref;
1489 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490
Bram Moolenaar080457c2020-03-03 21:53:32 +01001491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if (isn_type == ISN_LOADSCRIPT)
1493 isn = generate_instr_type(cctx, isn_type, type);
1494 else
1495 isn = generate_instr_drop(cctx, isn_type, 1);
1496 if (isn == NULL)
1497 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001498
1499 // This requires three arguments, which doesn't fit in an instruction, thus
1500 // we need to allocate a struct for this.
1501 sref = ALLOC_ONE(scriptref_T);
1502 if (sref == NULL)
1503 return FAIL;
1504 isn->isn_arg.script.scriptref = sref;
1505 sref->sref_sid = sid;
1506 sref->sref_idx = idx;
1507 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001508 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_NEWLIST instruction.
1514 */
1515 static int
1516generate_NEWLIST(cctx_T *cctx, int count)
1517{
1518 isn_T *isn;
1519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 type_T *type;
1521 type_T *member;
1522
Bram Moolenaar080457c2020-03-03 21:53:32 +01001523 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1525 return FAIL;
1526 isn->isn_arg.number = count;
1527
Bram Moolenaar127542b2020-08-09 17:22:04 +02001528 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001529 if (count == 0)
1530 member = &t_void;
1531 else
1532 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001533 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1534 cctx->ctx_type_list);
1535 type = get_list_type(member, cctx->ctx_type_list);
1536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 // drop the value types
1538 stack->ga_len -= count;
1539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 // add the list type to the type stack
1541 if (ga_grow(stack, 1) == FAIL)
1542 return FAIL;
1543 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1544 ++stack->ga_len;
1545
1546 return OK;
1547}
1548
1549/*
1550 * Generate an ISN_NEWDICT instruction.
1551 */
1552 static int
1553generate_NEWDICT(cctx_T *cctx, int count)
1554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 type_T *type;
1558 type_T *member;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.number = count;
1564
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001565 if (count == 0)
1566 member = &t_void;
1567 else
1568 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001569 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1570 cctx->ctx_type_list);
1571 type = get_dict_type(member, cctx->ctx_type_list);
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 // drop the key and value types
1574 stack->ga_len -= 2 * count;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // add the dict type to the type stack
1577 if (ga_grow(stack, 1) == FAIL)
1578 return FAIL;
1579 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_FUNCREF instruction.
1587 */
1588 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001589generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1596 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001597 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001598 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001600 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001601 // the nested context, including this one.
1602 if (ufunc->uf_flags & FC_CLOSURE)
1603 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if (ga_grow(stack, 1) == FAIL)
1606 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001607 ((type_T **)stack->ga_data)[stack->ga_len] =
1608 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 ++stack->ga_len;
1610
1611 return OK;
1612}
1613
1614/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001615 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001616 * "lambda_name" and "func_name" must be in allocated memory and will be
1617 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001618 */
1619 static int
1620generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1621{
1622 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001623
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001624 if (cctx->ctx_skip == SKIP_YES)
1625 {
1626 vim_free(lambda_name);
1627 vim_free(func_name);
1628 return OK;
1629 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001630 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001631 {
1632 vim_free(lambda_name);
1633 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001634 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001635 }
1636 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001637 isn->isn_arg.newfunc.nf_global = func_name;
1638
1639 return OK;
1640}
1641
1642/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001643 * Generate an ISN_DEF instruction: list functions
1644 */
1645 static int
1646generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1647{
1648 isn_T *isn;
1649
1650 RETURN_OK_IF_SKIP(cctx);
1651 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1652 return FAIL;
1653 if (len > 0)
1654 {
1655 isn->isn_arg.string = vim_strnsave(name, len);
1656 if (isn->isn_arg.string == NULL)
1657 return FAIL;
1658 }
1659 return OK;
1660}
1661
1662/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 * Generate an ISN_JUMP instruction.
1664 */
1665 static int
1666generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1667{
1668 isn_T *isn;
1669 garray_T *stack = &cctx->ctx_type_stack;
1670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1673 return FAIL;
1674 isn->isn_arg.jump.jump_when = when;
1675 isn->isn_arg.jump.jump_where = where;
1676
1677 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1678 --stack->ga_len;
1679
1680 return OK;
1681}
1682
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001683/*
1684 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1685 */
1686 static int
1687generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1688{
1689 isn_T *isn;
1690
1691 RETURN_OK_IF_SKIP(cctx);
1692 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1693 return FAIL;
1694 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1695 // jump_where is set later
1696 return OK;
1697}
1698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 static int
1700generate_FOR(cctx_T *cctx, int loop_idx)
1701{
1702 isn_T *isn;
1703 garray_T *stack = &cctx->ctx_type_stack;
1704
Bram Moolenaar080457c2020-03-03 21:53:32 +01001705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1707 return FAIL;
1708 isn->isn_arg.forloop.for_idx = loop_idx;
1709
1710 if (ga_grow(stack, 1) == FAIL)
1711 return FAIL;
1712 // type doesn't matter, will be stored next
1713 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1714 ++stack->ga_len;
1715
1716 return OK;
1717}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001718/*
1719 * Generate an ISN_TRYCONT instruction.
1720 */
1721 static int
1722generate_TRYCONT(cctx_T *cctx, int levels, int where)
1723{
1724 isn_T *isn;
1725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.trycont.tct_levels = levels;
1730 isn->isn_arg.trycont.tct_where = where;
1731
1732 return OK;
1733}
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
1736/*
1737 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001738 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 * Return FAIL if the number of arguments is wrong.
1740 */
1741 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001742generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743{
1744 isn_T *isn;
1745 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001746 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001747 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001748 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749
Bram Moolenaar080457c2020-03-03 21:53:32 +01001750 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001751 argoff = check_internal_func(func_idx, argcount);
1752 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 return FAIL;
1754
Bram Moolenaar389df252020-07-09 21:20:47 +02001755 if (method_call && argoff > 1)
1756 {
1757 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.shuffle.shfl_item = argcount;
1760 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1761 }
1762
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001763 if (argcount > 0)
1764 {
1765 // Check the types of the arguments.
1766 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001767 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1768 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001769 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001770 if (internal_func_is_map(func_idx))
1771 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001772 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1775 return FAIL;
1776 isn->isn_arg.bfunc.cbf_idx = func_idx;
1777 isn->isn_arg.bfunc.cbf_argcount = argcount;
1778
Bram Moolenaar94738d82020-10-21 14:25:07 +02001779 // Drop the argument types and push the return type.
1780 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 if (ga_grow(stack, 1) == FAIL)
1782 return FAIL;
1783 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001784 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001785 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001787 if (maptype != NULL && maptype->tt_member != NULL
1788 && maptype->tt_member != &t_any)
1789 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001790 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 return OK;
1793}
1794
1795/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001796 * Generate an ISN_LISTAPPEND instruction. Works like add().
1797 * Argument count is already checked.
1798 */
1799 static int
1800generate_LISTAPPEND(cctx_T *cctx)
1801{
1802 garray_T *stack = &cctx->ctx_type_stack;
1803 type_T *list_type;
1804 type_T *item_type;
1805 type_T *expected;
1806
1807 // Caller already checked that list_type is a list.
1808 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1809 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1810 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001811 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001812 return FAIL;
1813
1814 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1815 return FAIL;
1816
1817 --stack->ga_len; // drop the argument
1818 return OK;
1819}
1820
1821/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001822 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1823 * Argument count is already checked.
1824 */
1825 static int
1826generate_BLOBAPPEND(cctx_T *cctx)
1827{
1828 garray_T *stack = &cctx->ctx_type_stack;
1829 type_T *item_type;
1830
1831 // Caller already checked that blob_type is a blob.
1832 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001833 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001834 return FAIL;
1835
1836 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1837 return FAIL;
1838
1839 --stack->ga_len; // drop the argument
1840 return OK;
1841}
1842
1843/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001844 * Return TRUE if "ufunc" should be compiled, taking into account whether
1845 * "profile" indicates profiling is to be done.
1846 */
1847 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001848func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001849{
1850 switch (ufunc->uf_def_status)
1851 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001852 case UF_TO_BE_COMPILED:
1853 return TRUE;
1854
Bram Moolenaarb2049902021-01-24 12:53:53 +01001855 case UF_COMPILED:
1856 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001857#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001858 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1859 + ufunc->uf_dfunc_idx;
1860
1861 return profile ? dfunc->df_instr_prof == NULL
1862 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001863#else
1864 break;
1865#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001866 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001867
1868 case UF_NOT_COMPILED:
1869 case UF_COMPILE_ERROR:
1870 case UF_COMPILING:
1871 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001872 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001873 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001874}
1875
1876/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 * Generate an ISN_DCALL or ISN_UCALL instruction.
1878 * Return FAIL if the number of arguments is wrong.
1879 */
1880 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001881generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882{
1883 isn_T *isn;
1884 garray_T *stack = &cctx->ctx_type_stack;
1885 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001886 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
Bram Moolenaar080457c2020-03-03 21:53:32 +01001888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 if (argcount > regular_args && !has_varargs(ufunc))
1890 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001891 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 return FAIL;
1893 }
1894 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1895 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001896 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 return FAIL;
1898 }
1899
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001900 if (ufunc->uf_def_status != UF_NOT_COMPILED
1901 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001902 {
1903 int i;
1904
1905 for (i = 0; i < argcount; ++i)
1906 {
1907 type_T *expected;
1908 type_T *actual;
1909
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001910 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1911 if (actual == &t_special
1912 && i >= regular_args - ufunc->uf_def_args.ga_len)
1913 {
1914 // assume v:none used for default argument value
1915 continue;
1916 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001917 if (i < regular_args)
1918 {
1919 if (ufunc->uf_arg_types == NULL)
1920 continue;
1921 expected = ufunc->uf_arg_types[i];
1922 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001923 else if (ufunc->uf_va_type == NULL
1924 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001925 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001926 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001927 else
1928 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001929 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001930 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001931 {
1932 arg_type_mismatch(expected, actual, i + 1);
1933 return FAIL;
1934 }
1935 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001936 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001937 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001938 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001939 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001940 }
1941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001943 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001944 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001946 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 {
1948 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1949 isn->isn_arg.dfunc.cdf_argcount = argcount;
1950 }
1951 else
1952 {
1953 // A user function may be deleted and redefined later, can't use the
1954 // ufunc pointer, need to look it up again at runtime.
1955 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1956 isn->isn_arg.ufunc.cuf_argcount = argcount;
1957 }
1958
1959 stack->ga_len -= argcount; // drop the arguments
1960 if (ga_grow(stack, 1) == FAIL)
1961 return FAIL;
1962 // add return value
1963 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1964 ++stack->ga_len;
1965
1966 return OK;
1967}
1968
1969/*
1970 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1971 */
1972 static int
1973generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1974{
1975 isn_T *isn;
1976 garray_T *stack = &cctx->ctx_type_stack;
1977
Bram Moolenaar080457c2020-03-03 21:53:32 +01001978 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1980 return FAIL;
1981 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1982 isn->isn_arg.ufunc.cuf_argcount = argcount;
1983
1984 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001985 if (ga_grow(stack, 1) == FAIL)
1986 return FAIL;
1987 // add return value
1988 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1989 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990
1991 return OK;
1992}
1993
1994/*
1995 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001996 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 */
1998 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001999generate_PCALL(
2000 cctx_T *cctx,
2001 int argcount,
2002 char_u *name,
2003 type_T *type,
2004 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005{
2006 isn_T *isn;
2007 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002008 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009
Bram Moolenaar080457c2020-03-03 21:53:32 +01002010 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002011
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002012 if (type->tt_type == VAR_ANY)
2013 ret_type = &t_any;
2014 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002015 {
2016 if (type->tt_argcount != -1)
2017 {
2018 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2019
2020 if (argcount < type->tt_min_argcount - varargs)
2021 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002022 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002023 return FAIL;
2024 }
2025 if (!varargs && argcount > type->tt_argcount)
2026 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002027 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002028 return FAIL;
2029 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002030 if (type->tt_args != NULL)
2031 {
2032 int i;
2033
2034 for (i = 0; i < argcount; ++i)
2035 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002036 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002037 type_T *actual = ((type_T **)stack->ga_data)[
2038 stack->ga_len + offset];
2039 type_T *expected;
2040
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002041 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002042 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002043 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002044 else if (i >= type->tt_min_argcount
2045 && actual == &t_special)
2046 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002047 else
2048 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002049 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002050 cctx, TRUE, FALSE) == FAIL)
2051 {
2052 arg_type_mismatch(expected, actual, i + 1);
2053 return FAIL;
2054 }
2055 }
2056 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002057 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002058 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002059 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002060 else
2061 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002062 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002063 return FAIL;
2064 }
2065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2067 return FAIL;
2068 isn->isn_arg.pfunc.cpf_top = at_top;
2069 isn->isn_arg.pfunc.cpf_argcount = argcount;
2070
2071 stack->ga_len -= argcount; // drop the arguments
2072
2073 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002074 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002076 // If partial is above the arguments it must be cleared and replaced with
2077 // the return value.
2078 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2079 return FAIL;
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 return OK;
2082}
2083
2084/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002085 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 */
2087 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002088generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089{
2090 isn_T *isn;
2091 garray_T *stack = &cctx->ctx_type_stack;
2092 type_T *type;
2093
Bram Moolenaar080457c2020-03-03 21:53:32 +01002094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002095 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002097 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002099 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002101 if (type->tt_type != VAR_DICT && type != &t_any)
2102 {
2103 emsg(_(e_dictreq));
2104 return FAIL;
2105 }
2106 // change dict type to dict member type
2107 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002108 {
2109 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2110 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112
2113 return OK;
2114}
2115
2116/*
2117 * Generate an ISN_ECHO instruction.
2118 */
2119 static int
2120generate_ECHO(cctx_T *cctx, int with_white, int count)
2121{
2122 isn_T *isn;
2123
Bram Moolenaar080457c2020-03-03 21:53:32 +01002124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2126 return FAIL;
2127 isn->isn_arg.echo.echo_with_white = with_white;
2128 isn->isn_arg.echo.echo_count = count;
2129
2130 return OK;
2131}
2132
Bram Moolenaarad39c092020-02-26 18:23:43 +01002133/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002134 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002135 */
2136 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002137generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002138{
2139 isn_T *isn;
2140
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002141 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002142 return FAIL;
2143 isn->isn_arg.number = count;
2144
2145 return OK;
2146}
2147
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002148/*
2149 * Generate an ISN_PUT instruction.
2150 */
2151 static int
2152generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2153{
2154 isn_T *isn;
2155
2156 RETURN_OK_IF_SKIP(cctx);
2157 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2158 return FAIL;
2159 isn->isn_arg.put.put_regname = regname;
2160 isn->isn_arg.put.put_lnum = lnum;
2161 return OK;
2162}
2163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 static int
2165generate_EXEC(cctx_T *cctx, char_u *line)
2166{
2167 isn_T *isn;
2168
Bram Moolenaar080457c2020-03-03 21:53:32 +01002169 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2171 return FAIL;
2172 isn->isn_arg.string = vim_strsave(line);
2173 return OK;
2174}
2175
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002176 static int
2177generate_EXECCONCAT(cctx_T *cctx, int count)
2178{
2179 isn_T *isn;
2180
2181 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2182 return FAIL;
2183 isn->isn_arg.number = count;
2184 return OK;
2185}
2186
Bram Moolenaar08597872020-12-10 19:43:40 +01002187/*
2188 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2189 */
2190 static int
2191generate_RANGE(cctx_T *cctx, char_u *range)
2192{
2193 isn_T *isn;
2194 garray_T *stack = &cctx->ctx_type_stack;
2195
2196 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2197 return FAIL;
2198 isn->isn_arg.string = range;
2199
2200 if (ga_grow(stack, 1) == FAIL)
2201 return FAIL;
2202 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2203 ++stack->ga_len;
2204 return OK;
2205}
2206
Bram Moolenaar792f7862020-11-23 08:31:18 +01002207 static int
2208generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2209{
2210 isn_T *isn;
2211
2212 RETURN_OK_IF_SKIP(cctx);
2213 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2214 return FAIL;
2215 isn->isn_arg.unpack.unp_count = var_count;
2216 isn->isn_arg.unpack.unp_semicolon = semicolon;
2217 return OK;
2218}
2219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002221 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002222 */
2223 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002224generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002225{
2226 isn_T *isn;
2227
Bram Moolenaarfa984412021-03-25 22:15:28 +01002228 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002229 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002230 cctx->ctx_has_cmdmod = TRUE;
2231
2232 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002233 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002234 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2235 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2236 return FAIL;
2237 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002238 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002239 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002240 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002241
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002242 return OK;
2243}
2244
2245 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002246generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002247{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002248 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2249 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002250 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002251 return OK;
2252}
2253
Bram Moolenaarfa984412021-03-25 22:15:28 +01002254 static int
2255misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002256{
2257 garray_T *instr = &cctx->ctx_instr;
2258
Bram Moolenaara91a7132021-03-25 21:12:15 +01002259 if (cctx->ctx_has_cmdmod
2260 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2261 == ISN_CMDMOD)
2262 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002263 emsg(_(e_misplaced_command_modifier));
2264 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002265 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002266 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002267}
2268
2269/*
2270 * Get the index of the current instruction.
2271 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2272 */
2273 static int
2274current_instr_idx(cctx_T *cctx)
2275{
2276 garray_T *instr = &cctx->ctx_instr;
2277 int idx = instr->ga_len;
2278
2279 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2280 .isn_type == ISN_CMDMOD)
2281 --idx;
2282#ifdef FEAT_PROFILE
2283 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2284 .isn_type == ISN_PROF_START)
2285 --idx;
2286#endif
2287 return idx;
2288}
2289
Bram Moolenaarf002a412021-01-24 13:34:18 +01002290#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002291 static void
2292may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2293{
2294 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002295 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002296}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002297#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002298
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002299/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002301 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002303 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002304reserve_local(
2305 cctx_T *cctx,
2306 char_u *name,
2307 size_t len,
2308 int isConst,
2309 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 lvar_T *lvar;
2312
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002313 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002315 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002316 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 }
2318
2319 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002320 return NULL;
2321 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002322 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002324 // Every local variable uses the next entry on the stack. We could re-use
2325 // the last ones when leaving a scope, but then variables used in a closure
2326 // might get overwritten. To keep things simple do not re-use stack
2327 // entries. This is less efficient, but memory is cheap these days.
2328 lvar->lv_idx = cctx->ctx_locals_count++;
2329
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002330 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 lvar->lv_const = isConst;
2332 lvar->lv_type = type;
2333
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002334 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335}
2336
2337/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002338 * Remove local variables above "new_top".
2339 */
2340 static void
2341unwind_locals(cctx_T *cctx, int new_top)
2342{
2343 if (cctx->ctx_locals.ga_len > new_top)
2344 {
2345 int idx;
2346 lvar_T *lvar;
2347
2348 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2349 {
2350 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2351 vim_free(lvar->lv_name);
2352 }
2353 }
2354 cctx->ctx_locals.ga_len = new_top;
2355}
2356
2357/*
2358 * Free all local variables.
2359 */
2360 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002361free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002362{
2363 unwind_locals(cctx, 0);
2364 ga_clear(&cctx->ctx_locals);
2365}
2366
2367/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002368 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2369 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2370 * error if the variable was defined with :const.
2371 */
2372 static int
2373check_item_writable(svar_T *sv, int check_writable, char_u *name)
2374{
2375 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2376 || (check_writable == ASSIGN_FINAL
2377 && sv->sv_const == ASSIGN_CONST))
2378 {
2379 semsg(_(e_readonlyvar), name);
2380 return FAIL;
2381 }
2382 return OK;
2383}
2384
2385/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002387 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 * Returns the index in "sn_var_vals" if found.
2389 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002390 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 */
2392 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002393get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394{
2395 hashtab_T *ht;
2396 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002397 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002398 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 int idx;
2400
Bram Moolenaare3d46852020-08-29 13:39:17 +02002401 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002403 if (sid == current_sctx.sc_sid)
2404 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002405 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002406
2407 if (sav == NULL)
2408 return -2;
2409 idx = sav->sav_var_vals_idx;
2410 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002411 if (check_item_writable(sv, check_writable, name) == FAIL)
2412 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002413 return idx;
2414 }
2415
2416 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 ht = &SCRIPT_VARS(sid);
2418 di = find_var_in_ht(ht, 0, name, TRUE);
2419 if (di == NULL)
2420 return -2;
2421
2422 // Now find the svar_T index in sn_var_vals.
2423 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2424 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002425 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 if (sv->sv_tv == &di->di_tv)
2427 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002428 if (check_item_writable(sv, check_writable, name) == FAIL)
2429 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 return idx;
2431 }
2432 }
2433 return -1;
2434}
2435
2436/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002437 * Find "name" in imported items of the current script or in "cctx" if not
2438 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 */
2440 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002441find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 int idx;
2444
Bram Moolenaare3d46852020-08-29 13:39:17 +02002445 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002446 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 if (cctx != NULL)
2448 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2449 {
2450 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2451 + idx;
2452
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002453 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2454 : STRLEN(import->imp_name) == len
2455 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 return import;
2457 }
2458
Bram Moolenaarefa94442020-08-08 22:16:00 +02002459 return find_imported_in_script(name, len, current_sctx.sc_sid);
2460}
2461
2462 imported_T *
2463find_imported_in_script(char_u *name, size_t len, int sid)
2464{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002465 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002466 int idx;
2467
Bram Moolenaare3d46852020-08-29 13:39:17 +02002468 if (!SCRIPT_ID_VALID(sid))
2469 return NULL;
2470 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2472 {
2473 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2474
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002475 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2476 : STRLEN(import->imp_name) == len
2477 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 return import;
2479 }
2480 return NULL;
2481}
2482
2483/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002484 * Free all imported variables.
2485 */
2486 static void
2487free_imported(cctx_T *cctx)
2488{
2489 int idx;
2490
2491 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2492 {
2493 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2494
2495 vim_free(import->imp_name);
2496 }
2497 ga_clear(&cctx->ctx_imports);
2498}
2499
2500/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002501 * Return a pointer to the next line that isn't empty or only contains a
2502 * comment. Skips over white space.
2503 * Returns NULL if there is none.
2504 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002505 char_u *
2506peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002507{
2508 int lnum = cctx->ctx_lnum;
2509
2510 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2511 {
2512 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002513 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002514
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002515 // ignore NULLs inserted for continuation lines
2516 if (line != NULL)
2517 {
2518 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002519 if (vim9_bad_comment(p))
2520 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002521 if (*p != NUL && !vim9_comment_start(p))
2522 return p;
2523 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524 }
2525 return NULL;
2526}
2527
2528/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002529 * Called when checking for a following operator at "arg". When the rest of
2530 * the line is empty or only a comment, peek the next line. If there is a next
2531 * line return a pointer to it and set "nextp".
2532 * Otherwise skip over white space.
2533 */
2534 static char_u *
2535may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2536{
2537 char_u *p = skipwhite(arg);
2538
2539 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002540 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002541 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002542 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002543 if (*nextp != NULL)
2544 return *nextp;
2545 }
2546 return p;
2547}
2548
2549/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002550 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002551 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002552 * Returns NULL when at the end.
2553 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002554 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002556{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002557 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002558
2559 do
2560 {
2561 ++cctx->ctx_lnum;
2562 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002563 {
2564 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002566 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002567 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002568 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002569 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002571 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002572 return line;
2573}
2574
2575/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002576 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002577 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002578 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002579 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2580 */
2581 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002582may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002583{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002584 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002585 if (vim9_bad_comment(*arg))
2586 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002587 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002588 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002590
2591 if (next == NULL)
2592 return FAIL;
2593 *arg = skipwhite(next);
2594 }
2595 return OK;
2596}
2597
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002598/*
2599 * Idem, and give an error when failed.
2600 */
2601 static int
2602may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2603{
2604 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2605 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002606 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002607 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002608 return FAIL;
2609 }
2610 return OK;
2611}
2612
2613
Bram Moolenaara5565e42020-05-09 15:44:01 +02002614// Structure passed between the compile_expr* functions to keep track of
2615// constants that have been parsed but for which no code was produced yet. If
2616// possible expressions on these constants are applied at compile time. If
2617// that is not possible, the code to push the constants needs to be generated
2618// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002619// Using 50 should be more than enough of 5 levels of ().
2620#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002621typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002622 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002623 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002624 int pp_is_const; // all generated code was constants, used for a
2625 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002626} ppconst_T;
2627
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002628static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002629static int compile_expr0(char_u **arg, cctx_T *cctx);
2630static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2631
Bram Moolenaara5565e42020-05-09 15:44:01 +02002632/*
2633 * Generate a PUSH instruction for "tv".
2634 * "tv" will be consumed or cleared.
2635 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2636 */
2637 static int
2638generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2639{
2640 if (tv != NULL)
2641 {
2642 switch (tv->v_type)
2643 {
2644 case VAR_UNKNOWN:
2645 break;
2646 case VAR_BOOL:
2647 generate_PUSHBOOL(cctx, tv->vval.v_number);
2648 break;
2649 case VAR_SPECIAL:
2650 generate_PUSHSPEC(cctx, tv->vval.v_number);
2651 break;
2652 case VAR_NUMBER:
2653 generate_PUSHNR(cctx, tv->vval.v_number);
2654 break;
2655#ifdef FEAT_FLOAT
2656 case VAR_FLOAT:
2657 generate_PUSHF(cctx, tv->vval.v_float);
2658 break;
2659#endif
2660 case VAR_BLOB:
2661 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2662 tv->vval.v_blob = NULL;
2663 break;
2664 case VAR_STRING:
2665 generate_PUSHS(cctx, tv->vval.v_string);
2666 tv->vval.v_string = NULL;
2667 break;
2668 default:
2669 iemsg("constant type not supported");
2670 clear_tv(tv);
2671 return FAIL;
2672 }
2673 tv->v_type = VAR_UNKNOWN;
2674 }
2675 return OK;
2676}
2677
2678/*
2679 * Generate code for any ppconst entries.
2680 */
2681 static int
2682generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2683{
2684 int i;
2685 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002686 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002687
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002688 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002689 for (i = 0; i < ppconst->pp_used; ++i)
2690 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2691 ret = FAIL;
2692 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002693 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002694 return ret;
2695}
2696
2697/*
2698 * Clear ppconst constants. Used when failing.
2699 */
2700 static void
2701clear_ppconst(ppconst_T *ppconst)
2702{
2703 int i;
2704
2705 for (i = 0; i < ppconst->pp_used; ++i)
2706 clear_tv(&ppconst->pp_tv[i]);
2707 ppconst->pp_used = 0;
2708}
2709
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002710/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002711 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002712 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002713 */
2714 static int
2715compile_member(int is_slice, cctx_T *cctx)
2716{
2717 type_T **typep;
2718 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002719 vartype_T vartype;
2720 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002721
Bram Moolenaar261417b2021-05-06 21:04:55 +02002722 // We can index a list, dict and blob. If we don't know the type
2723 // we can use the index value type. If we still don't know use an "ANY"
2724 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002725 typep = ((type_T **)stack->ga_data) + stack->ga_len
2726 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002727 vartype = (*typep)->tt_type;
2728 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002729 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002730 if (*typep == &t_any && idxtype == &t_string)
2731 vartype = VAR_DICT;
2732 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002733 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002734 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002735 return FAIL;
2736 if (is_slice)
2737 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002738 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2739 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002740 FALSE, FALSE) == FAIL)
2741 return FAIL;
2742 }
2743 }
2744
Bram Moolenaar261417b2021-05-06 21:04:55 +02002745 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002746 {
2747 if (is_slice)
2748 {
2749 emsg(_(e_cannot_slice_dictionary));
2750 return FAIL;
2751 }
2752 if ((*typep)->tt_type == VAR_DICT)
2753 {
2754 *typep = (*typep)->tt_member;
2755 if (*typep == &t_unknown)
2756 // empty dict was used
2757 *typep = &t_any;
2758 }
2759 else
2760 {
2761 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2762 FALSE, FALSE) == FAIL)
2763 return FAIL;
2764 *typep = &t_any;
2765 }
2766 if (may_generate_2STRING(-1, cctx) == FAIL)
2767 return FAIL;
2768 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2769 return FAIL;
2770 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002771 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002772 {
2773 *typep = &t_string;
2774 if ((is_slice
2775 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2776 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2777 return FAIL;
2778 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002779 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002780 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002781 if (is_slice)
2782 {
2783 *typep = &t_blob;
2784 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2785 return FAIL;
2786 }
2787 else
2788 {
2789 *typep = &t_number;
2790 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2791 return FAIL;
2792 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002793 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002794 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002795 {
2796 if (is_slice)
2797 {
2798 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002799 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002800 2) == FAIL)
2801 return FAIL;
2802 }
2803 else
2804 {
2805 if ((*typep)->tt_type == VAR_LIST)
2806 {
2807 *typep = (*typep)->tt_member;
2808 if (*typep == &t_unknown)
2809 // empty list was used
2810 *typep = &t_any;
2811 }
2812 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002813 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2814 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002815 return FAIL;
2816 }
2817 }
2818 else
2819 {
2820 emsg(_(e_string_list_dict_or_blob_required));
2821 return FAIL;
2822 }
2823 return OK;
2824}
2825
2826/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002827 * Generate an instruction to load script-local variable "name", without the
2828 * leading "s:".
2829 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 */
2831 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002832compile_load_scriptvar(
2833 cctx_T *cctx,
2834 char_u *name, // variable NUL terminated
2835 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002836 char_u **end, // end of variable
2837 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002839 scriptitem_T *si;
2840 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 imported_T *import;
2842
Bram Moolenaare3d46852020-08-29 13:39:17 +02002843 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2844 return FAIL;
2845 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002846 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002847 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002849 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002850 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2851 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853 if (idx >= 0)
2854 {
2855 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2856
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002857 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 current_sctx.sc_sid, idx, sv->sv_type);
2859 return OK;
2860 }
2861
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002862 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 if (import != NULL)
2864 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002865 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002866 {
2867 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002868 char_u *exp_name;
2869 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002870 ufunc_T *ufunc;
2871 type_T *type;
2872
2873 // Used "import * as Name", need to lookup the member.
2874 if (*p != '.')
2875 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002876 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002877 return FAIL;
2878 }
2879 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002880 if (VIM_ISWHITE(*p))
2881 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002882 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002883 return FAIL;
2884 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002885
Bram Moolenaar1c991142020-07-04 13:15:31 +02002886 // isolate one name
2887 exp_name = p;
2888 while (eval_isnamec(*p))
2889 ++p;
2890 cc = *p;
2891 *p = NUL;
2892
Bram Moolenaaredba7072021-03-13 21:14:18 +01002893 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2894 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002895 *p = cc;
2896 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002897 *end = p;
2898
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002899 if (idx < 0)
2900 {
2901 if (*p == '(' && ufunc != NULL)
2902 {
2903 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2904 return OK;
2905 }
2906 return FAIL;
2907 }
2908
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002909 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2910 import->imp_sid,
2911 idx,
2912 type);
2913 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002914 else if (import->imp_funcname != NULL)
2915 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002917 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2918 import->imp_sid,
2919 import->imp_var_vals_idx,
2920 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 return OK;
2922 }
2923
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002924 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002925 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 return FAIL;
2927}
2928
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002929 static int
2930generate_funcref(cctx_T *cctx, char_u *name)
2931{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002932 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002933
2934 if (ufunc == NULL)
2935 return FAIL;
2936
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002937 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002938 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2939 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002940 == FAIL)
2941 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002942 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002943}
2944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945/*
2946 * Compile a variable name into a load instruction.
2947 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002948 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 * When "error" is FALSE do not give an error when not found.
2950 */
2951 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002952compile_load(
2953 char_u **arg,
2954 char_u *end_arg,
2955 cctx_T *cctx,
2956 int is_expr,
2957 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958{
2959 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002961 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002963 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964
2965 if (*(*arg + 1) == ':')
2966 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002967 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002968 {
2969 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
Bram Moolenaarfa596382021-04-07 21:58:16 +02002971 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002972 switch (**arg)
2973 {
2974 case 'g': isn_type = ISN_LOADGDICT; break;
2975 case 'w': isn_type = ISN_LOADWDICT; break;
2976 case 't': isn_type = ISN_LOADTDICT; break;
2977 case 'b': isn_type = ISN_LOADBDICT; break;
2978 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002979 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002980 goto theend;
2981 }
2982 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2983 goto theend;
2984 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 else
2987 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002988 isntype_T isn_type = ISN_DROP;
2989
Bram Moolenaarfa596382021-04-07 21:58:16 +02002990 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002991 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2992 if (name == NULL)
2993 return FAIL;
2994
2995 switch (**arg)
2996 {
2997 case 'v': res = generate_LOADV(cctx, name, error);
2998 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002999 case 's': if (is_expr && ASCII_ISUPPER(*name)
3000 && find_func(name, FALSE, cctx) != NULL)
3001 res = generate_funcref(cctx, name);
3002 else
3003 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003004 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003005 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003006 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003007 {
3008 if (is_expr && ASCII_ISUPPER(*name)
3009 && find_func(name, FALSE, cctx) != NULL)
3010 res = generate_funcref(cctx, name);
3011 else
3012 isn_type = ISN_LOADG;
3013 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003014 else
3015 {
3016 isn_type = ISN_LOADAUTO;
3017 vim_free(name);
3018 name = vim_strnsave(*arg, end - *arg);
3019 if (name == NULL)
3020 return FAIL;
3021 }
3022 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003023 case 'w': isn_type = ISN_LOADW; break;
3024 case 't': isn_type = ISN_LOADT; break;
3025 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003026 default: // cannot happen, just in case
3027 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003028 goto theend;
3029 }
3030 if (isn_type != ISN_DROP)
3031 {
3032 // Global, Buffer-local, Window-local and Tabpage-local
3033 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003034 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003035 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 }
3038 }
3039 else
3040 {
3041 size_t len = end - *arg;
3042 int idx;
3043 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003044 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045
3046 name = vim_strnsave(*arg, end - *arg);
3047 if (name == NULL)
3048 return FAIL;
3049
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003050 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003052 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003053 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 else
3056 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003057 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003058
Bram Moolenaar709664c2020-12-12 14:33:41 +01003059 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003061 type = lvar.lv_type;
3062 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003063 if (lvar.lv_from_outer != 0)
3064 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003065 else
3066 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 }
3068 else
3069 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003070 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003071 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003072 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003073 || find_imported(name, 0, cctx) != NULL)
3074 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003075
Bram Moolenaar0f769812020-09-12 18:32:34 +02003076 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003077 // uppercase letter it can be a user defined function.
3078 // generate_funcref() will fail if the function can't be found.
3079 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003080 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 }
3082 }
3083 if (gen_load)
3084 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003085 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003086 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003087 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003088 cctx->ctx_outer_used = TRUE;
3089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 }
3091
3092 *arg = end;
3093
3094theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003095 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003096 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 vim_free(name);
3098 return res;
3099}
3100
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003101 static void
3102clear_instr_ga(garray_T *gap)
3103{
3104 int idx;
3105
3106 for (idx = 0; idx < gap->ga_len; ++idx)
3107 delete_instr(((isn_T *)gap->ga_data) + idx);
3108 ga_clear(gap);
3109}
3110
3111/*
3112 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3113 * Returns FAIL if compilation fails.
3114 */
3115 static int
3116compile_string(isn_T *isn, cctx_T *cctx)
3117{
3118 char_u *s = isn->isn_arg.string;
3119 garray_T save_ga = cctx->ctx_instr;
3120 int expr_res;
3121 int trailing_error;
3122 int instr_count;
3123 isn_T *instr = NULL;
3124
3125 // Temporarily reset the list of instructions so that the jump labels are
3126 // correct.
3127 cctx->ctx_instr.ga_len = 0;
3128 cctx->ctx_instr.ga_maxlen = 0;
3129 cctx->ctx_instr.ga_data = NULL;
3130 expr_res = compile_expr0(&s, cctx);
3131 s = skipwhite(s);
3132 trailing_error = *s != NUL;
3133
3134 if (expr_res == FAIL || trailing_error)
3135 {
3136 if (trailing_error)
3137 semsg(_(e_trailing_arg), s);
3138 clear_instr_ga(&cctx->ctx_instr);
3139 cctx->ctx_instr = save_ga;
3140 return FAIL;
3141 }
3142
3143 // Move the generated instructions into the ISN_INSTR instruction, then
3144 // restore the list of instructions.
3145 instr_count = cctx->ctx_instr.ga_len;
3146 instr = cctx->ctx_instr.ga_data;
3147 instr[instr_count].isn_type = ISN_FINISH;
3148
3149 cctx->ctx_instr = save_ga;
3150 vim_free(isn->isn_arg.string);
3151 isn->isn_type = ISN_INSTR;
3152 isn->isn_arg.instr = instr;
3153 return OK;
3154}
3155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156/*
3157 * Compile the argument expressions.
3158 * "arg" points to just after the "(" and is advanced to after the ")"
3159 */
3160 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003161compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003163 char_u *p = *arg;
3164 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003165 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003166 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
Bram Moolenaare6085c52020-04-12 20:19:16 +02003168 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003170 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3171 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003172 if (*p == ')')
3173 {
3174 *arg = p + 1;
3175 return OK;
3176 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003177 if (must_end)
3178 {
3179 semsg(_(e_missing_comma_before_argument_str), p);
3180 return FAIL;
3181 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003182
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003183 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003184 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 return FAIL;
3186 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003187
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003188 if (is_searchpair && *argcount == 5
3189 && cctx->ctx_instr.ga_len == instr_count + 1)
3190 {
3191 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3192
3193 // {skip} argument of searchpair() can be compiled if not empty
3194 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3195 compile_string(isn, cctx);
3196 }
3197
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003198 if (*p != ',' && *skipwhite(p) == ',')
3199 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003200 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003201 p = skipwhite(p);
3202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003204 {
3205 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003206 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003207 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003208 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003209 else
3210 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003211 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003212 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003214failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003215 emsg(_(e_missing_close));
3216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217}
3218
3219/*
3220 * Compile a function call: name(arg1, arg2)
3221 * "arg" points to "name", "arg + varlen" to the "(".
3222 * "argcount_init" is 1 for "value->method()"
3223 * Instructions:
3224 * EVAL arg1
3225 * EVAL arg2
3226 * BCALL / DCALL / UCALL
3227 */
3228 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003229compile_call(
3230 char_u **arg,
3231 size_t varlen,
3232 cctx_T *cctx,
3233 ppconst_T *ppconst,
3234 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235{
3236 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003237 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 int argcount = argcount_init;
3239 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003240 char_u fname_buf[FLEN_FIXED + 1];
3241 char_u *tofree = NULL;
3242 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003243 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003244 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003245 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003246 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
Bram Moolenaara5565e42020-05-09 15:44:01 +02003248 // we can evaluate "has('name')" at compile time
3249 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3250 {
3251 char_u *s = skipwhite(*arg + varlen + 1);
3252 typval_T argvars[2];
3253
3254 argvars[0].v_type = VAR_UNKNOWN;
3255 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003256 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003257 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003258 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003259 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003260 if (*s == ')' && argvars[0].v_type == VAR_STRING
3261 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003262 {
3263 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3264
3265 *arg = s + 1;
3266 argvars[1].v_type = VAR_UNKNOWN;
3267 tv->v_type = VAR_NUMBER;
3268 tv->vval.v_number = 0;
3269 f_has(argvars, tv);
3270 clear_tv(&argvars[0]);
3271 ++ppconst->pp_used;
3272 return OK;
3273 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003274 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003275 }
3276
3277 if (generate_ppconst(cctx, ppconst) == FAIL)
3278 return FAIL;
3279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 if (varlen >= sizeof(namebuf))
3281 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003282 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 return FAIL;
3284 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003285 vim_strncpy(namebuf, *arg, varlen);
3286 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003288 // We handle the "skip" argument of searchpair() and searchpairpos()
3289 // differently.
3290 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3291 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3292 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3293 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003296 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003297 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
Bram Moolenaar03290b82020-12-19 16:30:44 +01003299 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003300 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 {
3302 int idx;
3303
3304 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003305 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003307 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003308 if (STRCMP(name, "flatten") == 0)
3309 {
3310 emsg(_(e_cannot_use_flatten_in_vim9_script));
3311 goto theend;
3312 }
3313
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003314 if (STRCMP(name, "add") == 0 && argcount == 2)
3315 {
3316 garray_T *stack = &cctx->ctx_type_stack;
3317 type_T *type = ((type_T **)stack->ga_data)[
3318 stack->ga_len - 2];
3319
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003320 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003321 if (type->tt_type == VAR_LIST)
3322 {
3323 // inline "add(list, item)" so that the type can be checked
3324 res = generate_LISTAPPEND(cctx);
3325 idx = -1;
3326 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003327 else if (type->tt_type == VAR_BLOB)
3328 {
3329 // inline "add(blob, nr)" so that the type can be checked
3330 res = generate_BLOBAPPEND(cctx);
3331 idx = -1;
3332 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003333 }
3334
3335 if (idx >= 0)
3336 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3337 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003338 else
3339 semsg(_(e_unknownfunc), namebuf);
3340 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 }
3342
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003343 // An argument or local variable can be a function reference, this
3344 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003345 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003346 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003347 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003348 // If we can find the function by name generate the right call.
3349 // Skip global functions here, a local funcref takes precedence.
3350 ufunc = find_func(name, FALSE, cctx);
3351 if (ufunc != NULL && !func_is_global(ufunc))
3352 {
3353 res = generate_CALL(cctx, ufunc, argcount);
3354 goto theend;
3355 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357
3358 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003359 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003360 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003362 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003363 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003364 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003365 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003366 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003367
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003368 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003369 goto theend;
3370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371
Bram Moolenaar0f769812020-09-12 18:32:34 +02003372 // If we can find a global function by name generate the right call.
3373 if (ufunc != NULL)
3374 {
3375 res = generate_CALL(cctx, ufunc, argcount);
3376 goto theend;
3377 }
3378
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003379 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003380 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003381 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003382 res = generate_UCALL(cctx, name, argcount);
3383 else
3384 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003385
3386theend:
3387 vim_free(tofree);
3388 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389}
3390
3391// like NAMESPACE_CHAR but with 'a' and 'l'.
3392#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3393
3394/*
3395 * Find the end of a variable or function name. Unlike find_name_end() this
3396 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003397 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 * Return a pointer to just after the name. Equal to "arg" if there is no
3399 * valid name.
3400 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003401 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003402to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403{
3404 char_u *p;
3405
3406 // Quick check for valid starting character.
3407 if (!eval_isnamec1(*arg))
3408 return arg;
3409
3410 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3411 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3412 // and can be used in slice "[n:]".
3413 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003414 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3416 break;
3417 return p;
3418}
3419
3420/*
3421 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003422 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 */
3424 char_u *
3425to_name_const_end(char_u *arg)
3426{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003427 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 typval_T rettv;
3429
3430 if (p == arg && *arg == '[')
3431 {
3432
3433 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 p = arg;
3436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 return p;
3438}
3439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440/*
3441 * parse a list: [expr, expr]
3442 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003443 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 */
3445 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003446compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447{
3448 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003449 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003451 int is_const;
3452 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003454 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003456 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003457 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003458 semsg(_(e_list_end), *arg);
3459 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003460 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003461 if (*p == ',')
3462 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003463 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003464 return FAIL;
3465 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003466 if (*p == ']')
3467 {
3468 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003469 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003470 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003471 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003472 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003473 if (!is_const)
3474 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 ++count;
3476 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003477 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003479 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3480 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003481 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003482 return FAIL;
3483 }
3484 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003485 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 p = skipwhite(p);
3487 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003488 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003490 ppconst->pp_is_const = is_all_const;
3491 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492}
3493
3494/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003495 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003496 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003497 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 */
3499 static int
3500compile_lambda(char_u **arg, cctx_T *cctx)
3501{
Bram Moolenaare462f522020-12-27 14:43:30 +01003502 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 typval_T rettv;
3504 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003505 evalarg_T evalarg;
3506
3507 CLEAR_FIELD(evalarg);
3508 evalarg.eval_flags = EVAL_EVALUATE;
3509 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510
3511 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003512 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3513 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003514 {
3515 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003516 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003517 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003518
Bram Moolenaar65c44152020-12-24 15:14:01 +01003519 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003521 ++ufunc->uf_refcount;
3522 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523
Bram Moolenaar65c44152020-12-24 15:14:01 +01003524 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003525 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003527 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3528 // points into it. Point to the original line to avoid a dangling pointer.
3529 if (evalarg.eval_tofree_cmdline != NULL)
3530 {
3531 size_t off = *arg - evalarg.eval_tofree_cmdline;
3532
3533 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3534 + off;
3535 }
3536
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003537 clear_evalarg(&evalarg, NULL);
3538
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003539 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003540 {
3541 // The return type will now be known.
3542 set_function_type(ufunc);
3543
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003544 // The function reference count will be 1. When the ISN_FUNCREF
3545 // instruction is deleted the reference count is decremented and the
3546 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003547 return generate_FUNCREF(cctx, ufunc);
3548 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003549
3550 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 return FAIL;
3552}
3553
3554/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003555 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003557 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 */
3559 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003560compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561{
3562 garray_T *instr = &cctx->ctx_instr;
3563 int count = 0;
3564 dict_T *d = dict_alloc();
3565 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003566 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003567 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 int is_const;
3569 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570
3571 if (d == NULL)
3572 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003573 if (generate_ppconst(cctx, ppconst) == FAIL)
3574 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003575 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003577 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578
Bram Moolenaar23c55272020-06-21 16:58:13 +02003579 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003580 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003581 *arg = NULL;
3582 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003583 }
3584
3585 if (**arg == '}')
3586 break;
3587
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003588 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003590 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003592 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003593 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003594 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003597 if (isn->isn_type == ISN_PUSHNR)
3598 {
3599 char buf[NUMBUFLEN];
3600
3601 // Convert to string at compile time.
3602 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3603 isn->isn_type = ISN_PUSHS;
3604 isn->isn_arg.string = vim_strsave((char_u *)buf);
3605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 if (isn->isn_type == ISN_PUSHS)
3607 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003608 else if (may_generate_2STRING(-1, cctx) == FAIL)
3609 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003610 *arg = skipwhite(*arg);
3611 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003612 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003613 emsg(_(e_missing_matching_bracket_after_dict_key));
3614 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003615 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003616 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003618 else
3619 {
3620 // {"name": value},
3621 // {'name': value},
3622 // {name: value} use "name" as a literal key
3623 key = get_literal_key(arg);
3624 if (key == NULL)
3625 return FAIL;
3626 if (generate_PUSHS(cctx, key) == FAIL)
3627 return FAIL;
3628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
3630 // Check for duplicate keys, if using string keys.
3631 if (key != NULL)
3632 {
3633 item = dict_find(d, key, -1);
3634 if (item != NULL)
3635 {
3636 semsg(_(e_duplicate_key), key);
3637 goto failret;
3638 }
3639 item = dictitem_alloc(key);
3640 if (item != NULL)
3641 {
3642 item->di_tv.v_type = VAR_UNKNOWN;
3643 item->di_tv.v_lock = 0;
3644 if (dict_add(d, item) == FAIL)
3645 dictitem_free(item);
3646 }
3647 }
3648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 if (**arg != ':')
3650 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003651 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003652 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003653 else
3654 semsg(_(e_missing_dict_colon), *arg);
3655 return FAIL;
3656 }
3657 whitep = *arg + 1;
3658 if (!IS_WHITE_OR_NUL(*whitep))
3659 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003660 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 return FAIL;
3662 }
3663
Bram Moolenaar23c55272020-06-21 16:58:13 +02003664 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003665 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003666 *arg = NULL;
3667 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003668 }
3669
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003670 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003672 if (!is_const)
3673 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 ++count;
3675
Bram Moolenaar2c330432020-04-13 14:41:35 +02003676 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003677 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003678 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003679 *arg = NULL;
3680 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 if (**arg == '}')
3683 break;
3684 if (**arg != ',')
3685 {
3686 semsg(_(e_missing_dict_comma), *arg);
3687 goto failret;
3688 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003689 if (IS_WHITE_OR_NUL(*whitep))
3690 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003691 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003692 return FAIL;
3693 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003694 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003695 if (!IS_WHITE_OR_NUL(*whitep))
3696 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003697 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003698 return FAIL;
3699 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003700 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 }
3702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 *arg = *arg + 1;
3704
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003705 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003706 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003707 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003708 *arg += STRLEN(*arg);
3709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003711 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 return generate_NEWDICT(cctx, count);
3713
3714failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003715 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003716 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003717 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003718 *arg = (char_u *)"";
3719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 dict_unref(d);
3721 return FAIL;
3722}
3723
3724/*
3725 * Compile "&option".
3726 */
3727 static int
3728compile_get_option(char_u **arg, cctx_T *cctx)
3729{
3730 typval_T rettv;
3731 char_u *start = *arg;
3732 int ret;
3733
3734 // parse the option and get the current value to get the type.
3735 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003736 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 if (ret == OK)
3738 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003739 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003740 char_u *name = vim_strnsave(start, *arg - start);
3741 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3742 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743
3744 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3745 vim_free(name);
3746 }
3747 clear_tv(&rettv);
3748
3749 return ret;
3750}
3751
3752/*
3753 * Compile "$VAR".
3754 */
3755 static int
3756compile_get_env(char_u **arg, cctx_T *cctx)
3757{
3758 char_u *start = *arg;
3759 int len;
3760 int ret;
3761 char_u *name;
3762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 ++*arg;
3764 len = get_env_len(arg);
3765 if (len == 0)
3766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003767 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 return FAIL;
3769 }
3770
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003771 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 name = vim_strnsave(start, len + 1);
3773 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3774 vim_free(name);
3775 return ret;
3776}
3777
3778/*
3779 * Compile "@r".
3780 */
3781 static int
3782compile_get_register(char_u **arg, cctx_T *cctx)
3783{
3784 int ret;
3785
3786 ++*arg;
3787 if (**arg == NUL)
3788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003789 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 return FAIL;
3791 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003792 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 {
3794 emsg_invreg(**arg);
3795 return FAIL;
3796 }
3797 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3798 ++*arg;
3799 return ret;
3800}
3801
3802/*
3803 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003804 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 */
3806 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003807apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003809 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
3811 // this works from end to start
3812 while (p > start)
3813 {
3814 --p;
3815 if (*p == '-' || *p == '+')
3816 {
3817 // only '-' has an effect, for '+' we only check the type
3818#ifdef FEAT_FLOAT
3819 if (rettv->v_type == VAR_FLOAT)
3820 {
3821 if (*p == '-')
3822 rettv->vval.v_float = -rettv->vval.v_float;
3823 }
3824 else
3825#endif
3826 {
3827 varnumber_T val;
3828 int error = FALSE;
3829
3830 // tv_get_number_chk() accepts a string, but we don't want that
3831 // here
3832 if (check_not_string(rettv) == FAIL)
3833 return FAIL;
3834 val = tv_get_number_chk(rettv, &error);
3835 clear_tv(rettv);
3836 if (error)
3837 return FAIL;
3838 if (*p == '-')
3839 val = -val;
3840 rettv->v_type = VAR_NUMBER;
3841 rettv->vval.v_number = val;
3842 }
3843 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003844 else if (numeric_only)
3845 {
3846 ++p;
3847 break;
3848 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003849 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 {
3851 int v = tv2bool(rettv);
3852
3853 // '!' is permissive in the type.
3854 clear_tv(rettv);
3855 rettv->v_type = VAR_BOOL;
3856 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3857 }
3858 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003859 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 return OK;
3861}
3862
3863/*
3864 * Recognize v: variables that are constants and set "rettv".
3865 */
3866 static void
3867get_vim_constant(char_u **arg, typval_T *rettv)
3868{
3869 if (STRNCMP(*arg, "v:true", 6) == 0)
3870 {
3871 rettv->v_type = VAR_BOOL;
3872 rettv->vval.v_number = VVAL_TRUE;
3873 *arg += 6;
3874 }
3875 else if (STRNCMP(*arg, "v:false", 7) == 0)
3876 {
3877 rettv->v_type = VAR_BOOL;
3878 rettv->vval.v_number = VVAL_FALSE;
3879 *arg += 7;
3880 }
3881 else if (STRNCMP(*arg, "v:null", 6) == 0)
3882 {
3883 rettv->v_type = VAR_SPECIAL;
3884 rettv->vval.v_number = VVAL_NULL;
3885 *arg += 6;
3886 }
3887 else if (STRNCMP(*arg, "v:none", 6) == 0)
3888 {
3889 rettv->v_type = VAR_SPECIAL;
3890 rettv->vval.v_number = VVAL_NONE;
3891 *arg += 6;
3892 }
3893}
3894
Bram Moolenaar657137c2021-01-09 15:45:23 +01003895 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003896get_compare_type(char_u *p, int *len, int *type_is)
3897{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003898 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003899 int i;
3900
3901 switch (p[0])
3902 {
3903 case '=': if (p[1] == '=')
3904 type = EXPR_EQUAL;
3905 else if (p[1] == '~')
3906 type = EXPR_MATCH;
3907 break;
3908 case '!': if (p[1] == '=')
3909 type = EXPR_NEQUAL;
3910 else if (p[1] == '~')
3911 type = EXPR_NOMATCH;
3912 break;
3913 case '>': if (p[1] != '=')
3914 {
3915 type = EXPR_GREATER;
3916 *len = 1;
3917 }
3918 else
3919 type = EXPR_GEQUAL;
3920 break;
3921 case '<': if (p[1] != '=')
3922 {
3923 type = EXPR_SMALLER;
3924 *len = 1;
3925 }
3926 else
3927 type = EXPR_SEQUAL;
3928 break;
3929 case 'i': if (p[1] == 's')
3930 {
3931 // "is" and "isnot"; but not a prefix of a name
3932 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3933 *len = 5;
3934 i = p[*len];
3935 if (!isalnum(i) && i != '_')
3936 {
3937 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3938 *type_is = TRUE;
3939 }
3940 }
3941 break;
3942 }
3943 return type;
3944}
3945
3946/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003947 * Skip over an expression, ignoring most errors.
3948 */
3949 static void
3950skip_expr_cctx(char_u **arg, cctx_T *cctx)
3951{
3952 evalarg_T evalarg;
3953
3954 CLEAR_FIELD(evalarg);
3955 evalarg.eval_cctx = cctx;
3956 skip_expr(arg, &evalarg);
3957}
3958
3959/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003961 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 */
3963 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003964compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003966 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967
3968 // this works from end to start
3969 while (p > start)
3970 {
3971 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003972 while (VIM_ISWHITE(*p))
3973 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 if (*p == '-' || *p == '+')
3975 {
3976 int negate = *p == '-';
3977 isn_T *isn;
3978
3979 // TODO: check type
3980 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3981 {
3982 --p;
3983 if (*p == '-')
3984 negate = !negate;
3985 }
3986 // only '-' has an effect, for '+' we only check the type
3987 if (negate)
3988 isn = generate_instr(cctx, ISN_NEGATENR);
3989 else
3990 isn = generate_instr(cctx, ISN_CHECKNR);
3991 if (isn == NULL)
3992 return FAIL;
3993 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003994 else if (numeric_only)
3995 {
3996 ++p;
3997 break;
3998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 else
4000 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004001 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004003 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004005 if (p[-1] == '!')
4006 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 }
4009 if (generate_2BOOL(cctx, invert) == FAIL)
4010 return FAIL;
4011 }
4012 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004013 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 return OK;
4015}
4016
4017/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004018 * Compile "(expression)": recursive!
4019 * Return FAIL/OK.
4020 */
4021 static int
4022compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4023{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004024 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004025 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004026
Bram Moolenaar24156692021-01-14 20:35:49 +01004027 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4028 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004029 if (ppconst->pp_used <= PPSIZE - 10)
4030 {
4031 ret = compile_expr1(arg, cctx, ppconst);
4032 }
4033 else
4034 {
4035 // Not enough space in ppconst, flush constants.
4036 if (generate_ppconst(cctx, ppconst) == FAIL)
4037 return FAIL;
4038 ret = compile_expr0(arg, cctx);
4039 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004040 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4041 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004042 if (**arg == ')')
4043 ++*arg;
4044 else if (ret == OK)
4045 {
4046 emsg(_(e_missing_close));
4047 ret = FAIL;
4048 }
4049 return ret;
4050}
4051
4052/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004054 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 */
4056 static int
4057compile_subscript(
4058 char_u **arg,
4059 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004060 char_u *start_leader,
4061 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004062 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004064 char_u *name_start = *end_leader;
4065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 for (;;)
4067 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004068 char_u *p = skipwhite(*arg);
4069
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004070 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004071 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004072 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004073
4074 // If a following line starts with "->{" or "->X" advance to that
4075 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004076 // Also if a following line starts with ".x".
4077 if (next != NULL &&
4078 ((next[0] == '-' && next[1] == '>'
4079 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004080 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004081 {
4082 next = next_line_from_context(cctx, TRUE);
4083 if (next == NULL)
4084 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004085 *arg = next;
4086 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004087 }
4088 }
4089
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004090 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004091 // not a function call.
4092 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004094 garray_T *stack = &cctx->ctx_type_stack;
4095 type_T *type;
4096 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004098 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004100 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004103 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4104
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004105 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004106 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004108 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 return FAIL;
4110 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004111 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004113 char_u *pstart = p;
4114
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004115 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004117 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 // something->method()
4120 // Apply the '!', '-' and '+' first:
4121 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004122 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004125 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004126 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004127 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004128 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004129 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004130 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004131 garray_T *stack = &cctx->ctx_type_stack;
4132 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004133 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004134 int expr_isn_start = cctx->ctx_instr.ga_len;
4135 int expr_isn_end;
4136 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004137
4138 // Funcref call: list->(Refs[2])(arg)
4139 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004140 //
4141 // Fist compile the function expression.
4142 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004143 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004144
4145 // Remember the next instruction index, where the instructions
4146 // for arguments are being written.
4147 expr_isn_end = cctx->ctx_instr.ga_len;
4148
4149 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004150 if (**arg != '(')
4151 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004152 if (*skipwhite(*arg) == '(')
4153 emsg(_(e_nowhitespace));
4154 else
4155 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004156 return FAIL;
4157 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004158 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004159 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004160 return FAIL;
4161
Bram Moolenaar2927c072021-04-05 19:41:21 +02004162 // Move the instructions for the arguments to before the
4163 // instructions of the expression and move the type of the
4164 // expression after the argument types. This is what ISN_PCALL
4165 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004166 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004167 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4168 if (arg_isn_count > 0)
4169 {
4170 int expr_isn_count = expr_isn_end - expr_isn_start;
4171 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4172
4173 if (isn == NULL)
4174 return FAIL;
4175 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4176 + expr_isn_start,
4177 sizeof(isn_T) * expr_isn_count);
4178 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4179 + expr_isn_start,
4180 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4181 sizeof(isn_T) * arg_isn_count);
4182 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4183 + expr_isn_start + arg_isn_count,
4184 isn, sizeof(isn_T) * expr_isn_count);
4185 vim_free(isn);
4186
4187 type = ((type_T **)stack->ga_data)[type_idx_start];
4188 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4189 ((type_T **)stack->ga_data) + type_idx_start + 1,
4190 sizeof(type_T *)
4191 * (stack->ga_len - type_idx_start - 1));
4192 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4193 }
4194
Bram Moolenaar7e368202020-12-25 21:56:57 +01004195 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4196 if (generate_PCALL(cctx, argcount,
4197 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 return FAIL;
4199 }
4200 else
4201 {
4202 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004203 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004204 if (!eval_isnamec1(*p))
4205 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004206 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004207 return FAIL;
4208 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004209 if (ASCII_ISALPHA(*p) && p[1] == ':')
4210 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004211 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 ;
4213 if (*p != '(')
4214 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004215 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 return FAIL;
4217 }
4218 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 return FAIL;
4221 }
4222 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004223 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004225 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004226
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004227 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004228 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004229 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004230 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004231 // TODO: more arguments
4232 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004233 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004235 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004237 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004238 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004239 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004240 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004241 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004242 // missing first index is equal to zero
4243 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004244 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004245 else
4246 {
4247 if (compile_expr0(arg, cctx) == FAIL)
4248 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004249 if (**arg == ':')
4250 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004251 semsg(_(e_white_space_required_before_and_after_str_at_str),
4252 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004253 return FAIL;
4254 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004255 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004256 return FAIL;
4257 *arg = skipwhite(*arg);
4258 }
4259 if (**arg == ':')
4260 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004261 is_slice = TRUE;
4262 ++*arg;
4263 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4264 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004265 semsg(_(e_white_space_required_before_and_after_str_at_str),
4266 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004267 return FAIL;
4268 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004269 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004270 return FAIL;
4271 if (**arg == ']')
4272 // missing second index is equal to end of string
4273 generate_PUSHNR(cctx, -1);
4274 else
4275 {
4276 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004277 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004278 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004279 return FAIL;
4280 *arg = skipwhite(*arg);
4281 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283
4284 if (**arg != ']')
4285 {
4286 emsg(_(e_missbrac));
4287 return FAIL;
4288 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004289 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290
Bram Moolenaare42939a2021-04-05 17:11:17 +02004291 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004292 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004294 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004296 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004297 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004299 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004300
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004301 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004302 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004303 {
4304 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004305 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004306 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004307 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004308 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 while (eval_isnamec(*p))
4310 MB_PTR_ADV(p);
4311 if (p == *arg)
4312 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004313 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 return FAIL;
4315 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004316 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 return FAIL;
4318 *arg = p;
4319 }
4320 else
4321 break;
4322 }
4323
4324 // TODO - see handle_subscript():
4325 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4326 // Don't do this when "Func" is already a partial that was bound
4327 // explicitly (pt_auto is FALSE).
4328
4329 return OK;
4330}
4331
4332/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004333 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4334 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004336 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004337 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004338 *
4339 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 */
4341
4342/*
4343 * number number constant
4344 * 0zFFFFFFFF Blob constant
4345 * "string" string constant
4346 * 'string' literal string constant
4347 * &option-name option value
4348 * @r register contents
4349 * identifier variable value
4350 * function() function call
4351 * $VAR environment variable
4352 * (expression) nested expression
4353 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004354 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 *
4356 * Also handle:
4357 * ! in front logical NOT
4358 * - in front unary minus
4359 * + in front unary plus (ignored)
4360 * trailing (arg) funcref/partial call
4361 * trailing [] subscript in String or List
4362 * trailing .name entry in Dictionary
4363 * trailing ->name() method call
4364 */
4365 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004366compile_expr7(
4367 char_u **arg,
4368 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004369 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 char_u *start_leader, *end_leader;
4372 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004373 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004374 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004376 ppconst->pp_is_const = FALSE;
4377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 /*
4379 * Skip '!', '-' and '+' characters. They are handled later.
4380 */
4381 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004382 if (eval_leader(arg, TRUE) == FAIL)
4383 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 end_leader = *arg;
4385
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004386 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 switch (**arg)
4388 {
4389 /*
4390 * Number constant.
4391 */
4392 case '0': // also for blob starting with 0z
4393 case '1':
4394 case '2':
4395 case '3':
4396 case '4':
4397 case '5':
4398 case '6':
4399 case '7':
4400 case '8':
4401 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004402 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004404 // Apply "-" and "+" just before the number now, right to
4405 // left. Matters especially when "->" follows. Stops at
4406 // '!'.
4407 if (apply_leader(rettv, TRUE,
4408 start_leader, &end_leader) == FAIL)
4409 {
4410 clear_tv(rettv);
4411 return FAIL;
4412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 break;
4414
4415 /*
4416 * String constant: "string".
4417 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004418 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 return FAIL;
4420 break;
4421
4422 /*
4423 * Literal string constant: 'str''ing'.
4424 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004425 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 return FAIL;
4427 break;
4428
4429 /*
4430 * Constant Vim variable.
4431 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004432 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 ret = NOTDONE;
4434 break;
4435
4436 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 * "true" constant
4438 */
4439 case 't': if (STRNCMP(*arg, "true", 4) == 0
4440 && !eval_isnamec((*arg)[4]))
4441 {
4442 *arg += 4;
4443 rettv->v_type = VAR_BOOL;
4444 rettv->vval.v_number = VVAL_TRUE;
4445 }
4446 else
4447 ret = NOTDONE;
4448 break;
4449
4450 /*
4451 * "false" constant
4452 */
4453 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4454 && !eval_isnamec((*arg)[5]))
4455 {
4456 *arg += 5;
4457 rettv->v_type = VAR_BOOL;
4458 rettv->vval.v_number = VVAL_FALSE;
4459 }
4460 else
4461 ret = NOTDONE;
4462 break;
4463
4464 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004465 * "null" constant
4466 */
4467 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004468 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004469 {
4470 *arg += 4;
4471 rettv->v_type = VAR_SPECIAL;
4472 rettv->vval.v_number = VVAL_NULL;
4473 }
4474 else
4475 ret = NOTDONE;
4476 break;
4477
4478 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 * List: [expr, expr]
4480 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004481 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4482 return FAIL;
4483 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 break;
4485
4486 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 * Dictionary: {'key': val, 'key': val}
4488 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004489 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4490 return FAIL;
4491 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 break;
4493
4494 /*
4495 * Option value: &name
4496 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004497 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4498 return FAIL;
4499 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 break;
4501
4502 /*
4503 * Environment variable: $VAR.
4504 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004505 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4506 return FAIL;
4507 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 break;
4509
4510 /*
4511 * Register contents: @r.
4512 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004513 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4514 return FAIL;
4515 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 break;
4517 /*
4518 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004519 * lambda: (arg, arg) => expr
4520 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004522 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4523 ret = compile_lambda(arg, cctx);
4524 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004525 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 break;
4527
4528 default: ret = NOTDONE;
4529 break;
4530 }
4531 if (ret == FAIL)
4532 return FAIL;
4533
Bram Moolenaar1c747212020-05-09 18:28:34 +02004534 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004536 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 clear_tv(rettv);
4538 else
4539 // A constant expression can possibly be handled compile time,
4540 // return the value instead of generating code.
4541 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 }
4543 else if (ret == NOTDONE)
4544 {
4545 char_u *p;
4546 int r;
4547
4548 if (!eval_isnamec1(**arg))
4549 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004550 if (!vim9_bad_comment(*arg))
4551 {
4552 if (ends_excmd(*skipwhite(*arg)))
4553 semsg(_(e_empty_expression_str), *arg);
4554 else
4555 semsg(_(e_name_expected_str), *arg);
4556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 return FAIL;
4558 }
4559
4560 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004561 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004562 if (p - *arg == (size_t)1 && **arg == '_')
4563 {
4564 emsg(_(e_cannot_use_underscore_here));
4565 return FAIL;
4566 }
4567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 {
4570 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 {
4574 if (generate_ppconst(cctx, ppconst) == FAIL)
4575 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004576 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 if (r == FAIL)
4579 return FAIL;
4580 }
4581
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004582 // Handle following "[]", ".member", etc.
4583 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004584 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004585 ppconst) == FAIL)
4586 return FAIL;
4587 if (ppconst->pp_used > 0)
4588 {
4589 // apply the '!', '-' and '+' before the constant
4590 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004591 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 return FAIL;
4593 return OK;
4594 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004595 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004597 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598}
4599
4600/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004601 * Give the "white on both sides" error, taking the operator from "p[len]".
4602 */
4603 void
4604error_white_both(char_u *op, int len)
4605{
4606 char_u buf[10];
4607
4608 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004609 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004610}
4611
4612/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004613 * <type>expr7: runtime type check / conversion
4614 */
4615 static int
4616compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4617{
4618 type_T *want_type = NULL;
4619
4620 // Recognize <type>
4621 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4622 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004623 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004624 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4625 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004626 return FAIL;
4627
4628 if (**arg != '>')
4629 {
4630 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004631 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004632 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004633 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004634 return FAIL;
4635 }
4636 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004637 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004638 return FAIL;
4639 }
4640
4641 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4642 return FAIL;
4643
4644 if (want_type != NULL)
4645 {
4646 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004647 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004648 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004649
Bram Moolenaard1103582020-08-14 22:44:25 +02004650 generate_ppconst(cctx, ppconst);
4651 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004652 where.wt_index = 0;
4653 where.wt_variable = FALSE;
4654 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004655 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004656 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004657 return FAIL;
4658 }
4659 }
4660
4661 return OK;
4662}
4663
4664/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 * * number multiplication
4666 * / number division
4667 * % number modulo
4668 */
4669 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671{
4672 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004673 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004674 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004676 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004677 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 return FAIL;
4679
4680 /*
4681 * Repeat computing, until no "*", "/" or "%" is following.
4682 */
4683 for (;;)
4684 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004685 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 if (*op != '*' && *op != '/' && *op != '%')
4687 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004688 if (next != NULL)
4689 {
4690 *arg = next_line_from_context(cctx, TRUE);
4691 op = skipwhite(*arg);
4692 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004693
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004694 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004696 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004699 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004700 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004702 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004703 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004705
4706 if (ppconst->pp_used == ppconst_used + 2
4707 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4708 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004709 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004710 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4711 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4712 varnumber_T res = 0;
4713 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004715 // both are numbers: compute the result
4716 switch (*op)
4717 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004718 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004719 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004720 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004721 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004722 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004723 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004724 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004725 break;
4726 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004727 if (failed)
4728 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004729 tv1->vval.v_number = res;
4730 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004731 }
4732 else
4733 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004734 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004735 generate_two_op(cctx, op);
4736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 }
4738
4739 return OK;
4740}
4741
4742/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004743 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 * - number subtraction
4745 * .. string concatenation
4746 */
4747 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004748compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749{
4750 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004751 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004753 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754
4755 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004756 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no "+", "-" or ".." is following.
4761 */
4762 for (;;)
4763 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004764 op = may_peek_next_line(cctx, *arg, &next);
4765 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004767 if (op[0] == op[1] && *op != '.' && next)
4768 // Finding "++" or "--" on the next line is a separate command.
4769 // But ".." is concatenation.
4770 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004772 if (next != NULL)
4773 {
4774 *arg = next_line_from_context(cctx, TRUE);
4775 op = skipwhite(*arg);
4776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004778 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004780 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004781 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 }
4783
Bram Moolenaare0de1712020-12-02 17:36:54 +01004784 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004785 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004787 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 return FAIL;
4790
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004792 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4794 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4795 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4796 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4799 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004800
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004801 // concat/subtract/add constant numbers
4802 if (*op == '+')
4803 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4804 else if (*op == '-')
4805 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4806 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004807 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004808 // concatenate constant strings
4809 char_u *s1 = tv1->vval.v_string;
4810 char_u *s2 = tv2->vval.v_string;
4811 size_t len1 = STRLEN(s1);
4812
4813 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4814 if (tv1->vval.v_string == NULL)
4815 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004816 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004817 return FAIL;
4818 }
4819 mch_memmove(tv1->vval.v_string, s1, len1);
4820 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004821 vim_free(s1);
4822 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004823 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004824 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 }
4826 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004827 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004828 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004829 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004830 if (*op == '.')
4831 {
4832 if (may_generate_2STRING(-2, cctx) == FAIL
4833 || may_generate_2STRING(-1, cctx) == FAIL)
4834 return FAIL;
4835 generate_instr_drop(cctx, ISN_CONCAT, 1);
4836 }
4837 else
4838 generate_two_op(cctx, op);
4839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 }
4841
4842 return OK;
4843}
4844
4845/*
4846 * expr5a == expr5b
4847 * expr5a =~ expr5b
4848 * expr5a != expr5b
4849 * expr5a !~ expr5b
4850 * expr5a > expr5b
4851 * expr5a >= expr5b
4852 * expr5a < expr5b
4853 * expr5a <= expr5b
4854 * expr5a is expr5b
4855 * expr5a isnot expr5b
4856 *
4857 * Produces instructions:
4858 * EVAL expr5a Push result of "expr5a"
4859 * EVAL expr5b Push result of "expr5b"
4860 * COMPARE one of the compare instructions
4861 */
4862 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004863compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004865 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004867 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004870 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871
4872 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 return FAIL;
4875
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004876 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004877 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
4879 /*
4880 * If there is a comparative operator, use it.
4881 */
4882 if (type != EXPR_UNKNOWN)
4883 {
4884 int ic = FALSE; // Default: do not ignore case
4885
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004886 if (next != NULL)
4887 {
4888 *arg = next_line_from_context(cctx, TRUE);
4889 p = skipwhite(*arg);
4890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 if (type_is && (p[len] == '?' || p[len] == '#'))
4892 {
4893 semsg(_(e_invexpr2), *arg);
4894 return FAIL;
4895 }
4896 // extra question mark appended: ignore case
4897 if (p[len] == '?')
4898 {
4899 ic = TRUE;
4900 ++len;
4901 }
4902 // extra '#' appended: match case (ignored)
4903 else if (p[len] == '#')
4904 ++len;
4905 // nothing appended: match case
4906
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004907 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004909 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004910 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 }
4912
4913 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004914 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004915 return FAIL;
4916
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 return FAIL;
4919
Bram Moolenaara5565e42020-05-09 15:44:01 +02004920 if (ppconst->pp_used == ppconst_used + 2)
4921 {
4922 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4923 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4924 int ret;
4925
4926 // Both sides are a constant, compute the result now.
4927 // First check for a valid combination of types, this is more
4928 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004929 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 ret = FAIL;
4931 else
4932 {
4933 ret = typval_compare(tv1, tv2, type, ic);
4934 tv1->v_type = VAR_BOOL;
4935 tv1->vval.v_number = tv1->vval.v_number
4936 ? VVAL_TRUE : VVAL_FALSE;
4937 clear_tv(tv2);
4938 --ppconst->pp_used;
4939 }
4940 return ret;
4941 }
4942
4943 generate_ppconst(cctx, ppconst);
4944 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
4946
4947 return OK;
4948}
4949
Bram Moolenaar7f141552020-05-09 17:35:53 +02004950static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952/*
4953 * Compile || or &&.
4954 */
4955 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004956compile_and_or(
4957 char_u **arg,
4958 cctx_T *cctx,
4959 char *op,
4960 ppconst_T *ppconst,
4961 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004963 char_u *next;
4964 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 int opchar = *op;
4966
4967 if (p[0] == opchar && p[1] == opchar)
4968 {
4969 garray_T *instr = &cctx->ctx_instr;
4970 garray_T end_ga;
4971
4972 /*
4973 * Repeat until there is no following "||" or "&&"
4974 */
4975 ga_init2(&end_ga, sizeof(int), 10);
4976 while (p[0] == opchar && p[1] == opchar)
4977 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004978 long start_lnum = SOURCING_LNUM;
4979 int start_ctx_lnum = cctx->ctx_lnum;
4980 int save_lnum;
4981
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004982 if (next != NULL)
4983 {
4984 *arg = next_line_from_context(cctx, TRUE);
4985 p = skipwhite(*arg);
4986 }
4987
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004988 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4989 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004990 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004991 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004992 return FAIL;
4993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004995 // TODO: use ppconst if the value is a constant and check
4996 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004997 generate_ppconst(cctx, ppconst);
4998
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004999 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005000 SOURCING_LNUM = start_lnum;
5001 save_lnum = cctx->ctx_lnum;
5002 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005003 if (bool_on_stack(cctx) == FAIL)
5004 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005005 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005006 ga_clear(&end_ga);
5007 return FAIL;
5008 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005009 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 if (ga_grow(&end_ga, 1) == FAIL)
5012 {
5013 ga_clear(&end_ga);
5014 return FAIL;
5015 }
5016 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5017 ++end_ga.ga_len;
5018 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005019 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020
5021 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005022 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005023 {
5024 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005025 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005026 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005027
Bram Moolenaara5565e42020-05-09 15:44:01 +02005028 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5029 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 {
5031 ga_clear(&end_ga);
5032 return FAIL;
5033 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005034
5035 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005037 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005039 // Every part must evaluate to a bool.
5040 if (bool_on_stack(cctx) == FAIL)
5041 {
5042 ga_clear(&end_ga);
5043 return FAIL;
5044 }
5045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 // Fill in the end label in all jumps.
5047 while (end_ga.ga_len > 0)
5048 {
5049 isn_T *isn;
5050
5051 --end_ga.ga_len;
5052 isn = ((isn_T *)instr->ga_data)
5053 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5054 isn->isn_arg.jump.jump_where = instr->ga_len;
5055 }
5056 ga_clear(&end_ga);
5057 }
5058
5059 return OK;
5060}
5061
5062/*
5063 * expr4a && expr4a && expr4a logical AND
5064 *
5065 * Produces instructions:
5066 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005067 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005068 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005070 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 * EVAL expr4c Push result of "expr4c"
5072 * end:
5073 */
5074 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005075compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077 int ppconst_used = ppconst->pp_used;
5078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005080 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 return FAIL;
5082
5083 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005084 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085}
5086
5087/*
5088 * expr3a || expr3b || expr3c logical OR
5089 *
5090 * Produces instructions:
5091 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005092 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005093 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005095 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 * EVAL expr3c Push result of "expr3c"
5097 * end:
5098 */
5099 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005102 int ppconst_used = ppconst->pp_used;
5103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 return FAIL;
5107
5108 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110}
5111
5112/*
5113 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005115 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 * JUMP_IF_FALSE alt jump if false
5117 * EVAL expr1a
5118 * JUMP_ALWAYS end
5119 * alt: EVAL expr1b
5120 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005121 *
5122 * Toplevel expression: expr2 ?? expr1
5123 * Produces instructions:
5124 * EVAL expr2 Push result of "expr2"
5125 * JUMP_AND_KEEP_IF_TRUE end jump if true
5126 * EVAL expr1
5127 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 */
5129 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005130compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131{
5132 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005134 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135
Bram Moolenaar3988f642020-08-27 22:43:03 +02005136 // Ignore all kinds of errors when not producing code.
5137 if (cctx->ctx_skip == SKIP_YES)
5138 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005139 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005140 return OK;
5141 }
5142
Bram Moolenaar61a89812020-05-07 16:58:17 +02005143 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005144 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 return FAIL;
5146
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005147 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 if (*p == '?')
5149 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005150 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 garray_T *instr = &cctx->ctx_instr;
5152 garray_T *stack = &cctx->ctx_type_stack;
5153 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005154 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005156 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005157 int has_const_expr = FALSE;
5158 int const_value = FALSE;
5159 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005161 if (next != NULL)
5162 {
5163 *arg = next_line_from_context(cctx, TRUE);
5164 p = skipwhite(*arg);
5165 }
5166
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005167 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005168 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005169 semsg(_(e_white_space_required_before_and_after_str_at_str),
5170 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005171 return FAIL;
5172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173
Bram Moolenaara5565e42020-05-09 15:44:01 +02005174 if (ppconst->pp_used == ppconst_used + 1)
5175 {
5176 // the condition is a constant, we know whether the ? or the :
5177 // expression is to be evaluated.
5178 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005179 if (op_falsy)
5180 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5181 else
5182 {
5183 int error = FALSE;
5184
5185 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5186 &error);
5187 if (error)
5188 return FAIL;
5189 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005190 cctx->ctx_skip = save_skip == SKIP_YES ||
5191 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5192
5193 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5194 // "left ?? right" and "left" is truthy: produce "left"
5195 generate_ppconst(cctx, ppconst);
5196 else
5197 {
5198 clear_tv(&ppconst->pp_tv[ppconst_used]);
5199 --ppconst->pp_used;
5200 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 }
5202 else
5203 {
5204 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005205 if (op_falsy)
5206 end_idx = instr->ga_len;
5207 generate_JUMP(cctx, op_falsy
5208 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5209 if (op_falsy)
5210 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212
5213 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005214 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005215 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005216 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218
Bram Moolenaara5565e42020-05-09 15:44:01 +02005219 if (!has_const_expr)
5220 {
5221 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005223 if (!op_falsy)
5224 {
5225 // remember the type and drop it
5226 --stack->ga_len;
5227 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005229 end_idx = instr->ga_len;
5230 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005231
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005232 // jump here from JUMP_IF_FALSE
5233 isn = ((isn_T *)instr->ga_data) + alt_idx;
5234 isn->isn_arg.jump.jump_where = instr->ga_len;
5235 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005238 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005240 // Check for the ":".
5241 p = may_peek_next_line(cctx, *arg, &next);
5242 if (*p != ':')
5243 {
5244 emsg(_(e_missing_colon));
5245 return FAIL;
5246 }
5247 if (next != NULL)
5248 {
5249 *arg = next_line_from_context(cctx, TRUE);
5250 p = skipwhite(*arg);
5251 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005252
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005253 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5254 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005255 semsg(_(e_white_space_required_before_and_after_str_at_str),
5256 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005257 return FAIL;
5258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005260 // evaluate the third expression
5261 if (has_const_expr)
5262 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005263 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005264 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005265 return FAIL;
5266 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5267 return FAIL;
5268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269
Bram Moolenaara5565e42020-05-09 15:44:01 +02005270 if (!has_const_expr)
5271 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005272 type_T **typep;
5273
Bram Moolenaara5565e42020-05-09 15:44:01 +02005274 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275
Bram Moolenaara5565e42020-05-09 15:44:01 +02005276 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005277 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5278 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005279
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005280 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005281 isn = ((isn_T *)instr->ga_data) + end_idx;
5282 isn->isn_arg.jump.jump_where = instr->ga_len;
5283 }
5284
5285 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286 }
5287 return OK;
5288}
5289
5290/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005291 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005292 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5293 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005294 */
5295 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005296compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297{
5298 ppconst_T ppconst;
5299
5300 CLEAR_FIELD(ppconst);
5301 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5302 {
5303 clear_ppconst(&ppconst);
5304 return FAIL;
5305 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005306 if (is_const != NULL)
5307 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005308 if (generate_ppconst(cctx, &ppconst) == FAIL)
5309 return FAIL;
5310 return OK;
5311}
5312
5313/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005314 * Toplevel expression.
5315 */
5316 static int
5317compile_expr0(char_u **arg, cctx_T *cctx)
5318{
5319 return compile_expr0_ext(arg, cctx, NULL);
5320}
5321
5322/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 * compile "return [expr]"
5324 */
5325 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005326compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327{
5328 char_u *p = arg;
5329 garray_T *stack = &cctx->ctx_type_stack;
5330 type_T *stack_type;
5331
5332 if (*p != NUL && *p != '|' && *p != '\n')
5333 {
5334 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005335 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 return NULL;
5337
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005338 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005339 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005340 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005341 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005342 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5343 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005344 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005345 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005346 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005347 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005348 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005349 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5350 && stack_type->tt_type != VAR_VOID
5351 && stack_type->tt_type != VAR_UNKNOWN)
5352 {
5353 emsg(_(e_returning_value_in_function_without_return_type));
5354 return NULL;
5355 }
5356 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005357 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005358 return NULL;
5359 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 }
5362 else
5363 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005364 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005365 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005366 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5367 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005369 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 return NULL;
5371 }
5372
5373 // No argument, return zero.
5374 generate_PUSHNR(cctx, 0);
5375 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005376
5377 // Undo any command modifiers.
5378 generate_undo_cmdmods(cctx);
5379
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005380 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381 return NULL;
5382
5383 // "return val | endif" is possible
5384 return skipwhite(p);
5385}
5386
5387/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005388 * Get a line from the compilation context, compatible with exarg_T getline().
5389 * Return a pointer to the line in allocated memory.
5390 * Return NULL for end-of-file or some error.
5391 */
5392 static char_u *
5393exarg_getline(
5394 int c UNUSED,
5395 void *cookie,
5396 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005397 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005398{
5399 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005400 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005401
Bram Moolenaar66250c92020-08-20 15:02:42 +02005402 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005403 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005404 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005405 return NULL;
5406 ++cctx->ctx_lnum;
5407 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5408 // Comment lines result in NULL pointers, skip them.
5409 if (p != NULL)
5410 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005411 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005412}
5413
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005414 void
5415fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5416{
5417 eap->getline = exarg_getline;
5418 eap->cookie = cctx;
5419}
5420
Bram Moolenaar04b12692020-05-04 23:24:44 +02005421/*
5422 * Compile a nested :def command.
5423 */
5424 static char_u *
5425compile_nested_function(exarg_T *eap, cctx_T *cctx)
5426{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005427 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005428 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005429 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005430 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005431 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005432 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005433
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005434 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005435 {
5436 emsg(_(e_cannot_use_bang_with_nested_def));
5437 return NULL;
5438 }
5439
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005440 if (*name_start == '/')
5441 {
5442 name_end = skip_regexp(name_start + 1, '/', TRUE);
5443 if (*name_end == '/')
5444 ++name_end;
5445 eap->nextcmd = check_nextcmd(name_end);
5446 }
5447 if (name_end == name_start || *skipwhite(name_end) != '(')
5448 {
5449 if (!ends_excmd2(name_start, name_end))
5450 {
5451 semsg(_(e_invalid_command_str), eap->cmd);
5452 return NULL;
5453 }
5454
5455 // "def" or "def Name": list functions
5456 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5457 return NULL;
5458 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5459 }
5460
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005461 // Only g:Func() can use a namespace.
5462 if (name_start[1] == ':' && !is_global)
5463 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005464 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005465 return NULL;
5466 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005467 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005468 return NULL;
5469
Bram Moolenaar04b12692020-05-04 23:24:44 +02005470 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005471 fill_exarg_from_cctx(eap, cctx);
5472
Bram Moolenaar04b12692020-05-04 23:24:44 +02005473 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005474 lambda_name = vim_strsave(get_lambda_name());
5475 if (lambda_name == NULL)
5476 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005477 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005478
Bram Moolenaar822ba242020-05-24 23:00:18 +02005479 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005480 {
5481 r = eap->skip ? OK : FAIL;
5482 goto theend;
5483 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005484
5485 // copy over the block scope IDs before compiling
5486 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5487 {
5488 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5489
5490 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5491 if (ufunc->uf_block_ids != NULL)
5492 {
5493 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5494 sizeof(int) * block_depth);
5495 ufunc->uf_block_depth = block_depth;
5496 }
5497 }
5498
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005499 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5500 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005501 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005502 {
5503 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005504 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005505 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005506
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005507 if (is_global)
5508 {
5509 char_u *func_name = vim_strnsave(name_start + 2,
5510 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005511
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005512 if (func_name == NULL)
5513 r = FAIL;
5514 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005515 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005516 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005517 lambda_name = NULL;
5518 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005519 }
5520 else
5521 {
5522 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005523 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005524 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005525
Bram Moolenaareef21022020-08-01 22:16:43 +02005526 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005527 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005528 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005529 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005530 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5531 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005532 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005533
5534theend:
5535 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005536 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005537}
5538
5539/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 * Return the length of an assignment operator, or zero if there isn't one.
5541 */
5542 int
5543assignment_len(char_u *p, int *heredoc)
5544{
5545 if (*p == '=')
5546 {
5547 if (p[1] == '<' && p[2] == '<')
5548 {
5549 *heredoc = TRUE;
5550 return 3;
5551 }
5552 return 1;
5553 }
5554 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5555 return 2;
5556 if (STRNCMP(p, "..=", 3) == 0)
5557 return 3;
5558 return 0;
5559}
5560
5561// words that cannot be used as a variable
5562static char *reserved[] = {
5563 "true",
5564 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005565 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566 NULL
5567};
5568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570 * Generate the load instruction for "name".
5571 */
5572 static void
5573generate_loadvar(
5574 cctx_T *cctx,
5575 assign_dest_T dest,
5576 char_u *name,
5577 lvar_T *lvar,
5578 type_T *type)
5579{
5580 switch (dest)
5581 {
5582 case dest_option:
5583 // TODO: check the option exists
5584 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5585 break;
5586 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005587 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5588 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5589 else
5590 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005591 break;
5592 case dest_buffer:
5593 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5594 break;
5595 case dest_window:
5596 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5597 break;
5598 case dest_tab:
5599 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5600 break;
5601 case dest_script:
5602 compile_load_scriptvar(cctx,
5603 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5604 break;
5605 case dest_env:
5606 // Include $ in the name here
5607 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5608 break;
5609 case dest_reg:
5610 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5611 break;
5612 case dest_vimvar:
5613 generate_LOADV(cctx, name + 2, TRUE);
5614 break;
5615 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005616 if (lvar->lv_from_outer > 0)
5617 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5618 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005619 else
5620 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5621 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005622 case dest_expr:
5623 // list or dict value should already be on the stack.
5624 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005625 }
5626}
5627
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005628/*
5629 * Skip over "[expr]" or ".member".
5630 * Does not check for any errors.
5631 */
5632 static char_u *
5633skip_index(char_u *start)
5634{
5635 char_u *p = start;
5636
5637 if (*p == '[')
5638 {
5639 p = skipwhite(p + 1);
5640 (void)skip_expr(&p, NULL);
5641 p = skipwhite(p);
5642 if (*p == ']')
5643 return p + 1;
5644 return p;
5645 }
5646 // if (*p == '.')
5647 return to_name_end(p + 1, TRUE);
5648}
5649
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005650 void
5651vim9_declare_error(char_u *name)
5652{
5653 char *scope = "";
5654
5655 switch (*name)
5656 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005657 case 'g': scope = _("global"); break;
5658 case 'b': scope = _("buffer"); break;
5659 case 'w': scope = _("window"); break;
5660 case 't': scope = _("tab"); break;
5661 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005662 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005663 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005664 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005665 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005666 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005667 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005668 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005669 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005670 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005671}
5672
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005673/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005674 * For one assignment figure out the type of destination. Return it in "dest".
5675 * When not recognized "dest" is not set.
5676 * For an option "opt_flags" is set.
5677 * For a v:var "vimvaridx" is set.
5678 * "type" is set to the destination type if known, unchanted otherwise.
5679 * Return FAIL if an error message was given.
5680 */
5681 static int
5682get_var_dest(
5683 char_u *name,
5684 assign_dest_T *dest,
5685 int cmdidx,
5686 int *opt_flags,
5687 int *vimvaridx,
5688 type_T **type,
5689 cctx_T *cctx)
5690{
5691 char_u *p;
5692
5693 if (*name == '&')
5694 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005695 int cc;
5696 long numval;
5697 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005698
5699 *dest = dest_option;
5700 if (cmdidx == CMD_final || cmdidx == CMD_const)
5701 {
5702 emsg(_(e_const_option));
5703 return FAIL;
5704 }
5705 p = name;
5706 p = find_option_end(&p, opt_flags);
5707 if (p == NULL)
5708 {
5709 // cannot happen?
5710 emsg(_(e_letunexp));
5711 return FAIL;
5712 }
5713 cc = *p;
5714 *p = NUL;
5715 opt_type = get_option_value(skip_option_env_lead(name),
5716 &numval, NULL, *opt_flags);
5717 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005718 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005719 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005720 case gov_unknown:
5721 semsg(_(e_unknown_option), name);
5722 return FAIL;
5723 case gov_string:
5724 case gov_hidden_string:
5725 *type = &t_string;
5726 break;
5727 case gov_bool:
5728 case gov_hidden_bool:
5729 *type = &t_bool;
5730 break;
5731 case gov_number:
5732 case gov_hidden_number:
5733 *type = &t_number;
5734 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005735 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005736 }
5737 else if (*name == '$')
5738 {
5739 *dest = dest_env;
5740 *type = &t_string;
5741 }
5742 else if (*name == '@')
5743 {
5744 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5745 {
5746 emsg_invreg(name[1]);
5747 return FAIL;
5748 }
5749 *dest = dest_reg;
5750 *type = &t_string;
5751 }
5752 else if (STRNCMP(name, "g:", 2) == 0)
5753 {
5754 *dest = dest_global;
5755 }
5756 else if (STRNCMP(name, "b:", 2) == 0)
5757 {
5758 *dest = dest_buffer;
5759 }
5760 else if (STRNCMP(name, "w:", 2) == 0)
5761 {
5762 *dest = dest_window;
5763 }
5764 else if (STRNCMP(name, "t:", 2) == 0)
5765 {
5766 *dest = dest_tab;
5767 }
5768 else if (STRNCMP(name, "v:", 2) == 0)
5769 {
5770 typval_T *vtv;
5771 int di_flags;
5772
5773 *vimvaridx = find_vim_var(name + 2, &di_flags);
5774 if (*vimvaridx < 0)
5775 {
5776 semsg(_(e_variable_not_found_str), name);
5777 return FAIL;
5778 }
5779 // We use the current value of "sandbox" here, is that OK?
5780 if (var_check_ro(di_flags, name, FALSE))
5781 return FAIL;
5782 *dest = dest_vimvar;
5783 vtv = get_vim_var_tv(*vimvaridx);
5784 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5785 }
5786 return OK;
5787}
5788
5789/*
5790 * Generate a STORE instruction for "dest", not being "dest_local".
5791 * Return FAIL when out of memory.
5792 */
5793 static int
5794generate_store_var(
5795 cctx_T *cctx,
5796 assign_dest_T dest,
5797 int opt_flags,
5798 int vimvaridx,
5799 int scriptvar_idx,
5800 int scriptvar_sid,
5801 type_T *type,
5802 char_u *name)
5803{
5804 switch (dest)
5805 {
5806 case dest_option:
5807 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5808 opt_flags);
5809 case dest_global:
5810 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005811 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5812 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005813 case dest_buffer:
5814 // include b: with the name, easier to execute that way
5815 return generate_STORE(cctx, ISN_STOREB, 0, name);
5816 case dest_window:
5817 // include w: with the name, easier to execute that way
5818 return generate_STORE(cctx, ISN_STOREW, 0, name);
5819 case dest_tab:
5820 // include t: with the name, easier to execute that way
5821 return generate_STORE(cctx, ISN_STORET, 0, name);
5822 case dest_env:
5823 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5824 case dest_reg:
5825 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5826 case dest_vimvar:
5827 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5828 case dest_script:
5829 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005830 // "s:" may be included in the name.
5831 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005832 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005833 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5834 scriptvar_sid, scriptvar_idx, type);
5835 case dest_local:
5836 case dest_expr:
5837 // cannot happen
5838 break;
5839 }
5840 return FAIL;
5841}
5842
Bram Moolenaar752fc692021-01-04 21:57:11 +01005843 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005844generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5845{
5846 if (lhs->lhs_dest != dest_local)
5847 return generate_store_var(cctx, lhs->lhs_dest,
5848 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5849 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5850 lhs->lhs_type, lhs->lhs_name);
5851
5852 if (lhs->lhs_lvar != NULL)
5853 {
5854 garray_T *instr = &cctx->ctx_instr;
5855 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5856
5857 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5858 // ISN_STORENR
5859 if (lhs->lhs_lvar->lv_from_outer == 0
5860 && instr->ga_len == instr_count + 1
5861 && isn->isn_type == ISN_PUSHNR)
5862 {
5863 varnumber_T val = isn->isn_arg.number;
5864 garray_T *stack = &cctx->ctx_type_stack;
5865
5866 isn->isn_type = ISN_STORENR;
5867 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5868 isn->isn_arg.storenr.stnr_val = val;
5869 if (stack->ga_len > 0)
5870 --stack->ga_len;
5871 }
5872 else if (lhs->lhs_lvar->lv_from_outer > 0)
5873 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5874 lhs->lhs_lvar->lv_from_outer);
5875 else
5876 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5877 }
5878 return OK;
5879}
5880
5881 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005882is_decl_command(int cmdidx)
5883{
5884 return cmdidx == CMD_let || cmdidx == CMD_var
5885 || cmdidx == CMD_final || cmdidx == CMD_const;
5886}
5887
5888/*
5889 * Figure out the LHS type and other properties for an assignment or one item
5890 * of ":unlet" with an index.
5891 * Returns OK or FAIL.
5892 */
5893 static int
5894compile_lhs(
5895 char_u *var_start,
5896 lhs_T *lhs,
5897 int cmdidx,
5898 int heredoc,
5899 int oplen,
5900 cctx_T *cctx)
5901{
5902 char_u *var_end;
5903 int is_decl = is_decl_command(cmdidx);
5904
5905 CLEAR_POINTER(lhs);
5906 lhs->lhs_dest = dest_local;
5907 lhs->lhs_vimvaridx = -1;
5908 lhs->lhs_scriptvar_idx = -1;
5909
5910 // "dest_end" is the end of the destination, including "[expr]" or
5911 // ".name".
5912 // "var_end" is the end of the variable/option/etc. name.
5913 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5914 if (*var_start == '@')
5915 var_end = var_start + 2;
5916 else
5917 {
5918 // skip over the leading "&", "&l:", "&g:" and "$"
5919 var_end = skip_option_env_lead(var_start);
5920 var_end = to_name_end(var_end, TRUE);
5921 }
5922
5923 // "a: type" is declaring variable "a" with a type, not dict "a:".
5924 if (is_decl && lhs->lhs_dest_end == var_start + 2
5925 && lhs->lhs_dest_end[-1] == ':')
5926 --lhs->lhs_dest_end;
5927 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5928 --var_end;
5929
5930 // compute the length of the destination without "[expr]" or ".name"
5931 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005932 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005933 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5934 if (lhs->lhs_name == NULL)
5935 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005936
5937 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5938 // Something follows after the variable: "var[idx]" or "var.key".
5939 lhs->lhs_has_index = TRUE;
5940
Bram Moolenaar752fc692021-01-04 21:57:11 +01005941 if (heredoc)
5942 lhs->lhs_type = &t_list_string;
5943 else
5944 lhs->lhs_type = &t_any;
5945
5946 if (cctx->ctx_skip != SKIP_YES)
5947 {
5948 int declare_error = FALSE;
5949
5950 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5951 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5952 &lhs->lhs_type, cctx) == FAIL)
5953 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005954 if (lhs->lhs_dest != dest_local
5955 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005956 {
5957 // Specific kind of variable recognized.
5958 declare_error = is_decl;
5959 }
5960 else
5961 {
5962 int idx;
5963
5964 // No specific kind of variable recognized, just a name.
5965 for (idx = 0; reserved[idx] != NULL; ++idx)
5966 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5967 {
5968 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5969 return FAIL;
5970 }
5971
5972
5973 if (lookup_local(var_start, lhs->lhs_varlen,
5974 &lhs->lhs_local_lvar, cctx) == OK)
5975 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5976 else
5977 {
5978 CLEAR_FIELD(lhs->lhs_arg_lvar);
5979 if (arg_exists(var_start, lhs->lhs_varlen,
5980 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5981 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5982 {
5983 if (is_decl)
5984 {
5985 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5986 return FAIL;
5987 }
5988 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5989 }
5990 }
5991 if (lhs->lhs_lvar != NULL)
5992 {
5993 if (is_decl)
5994 {
5995 semsg(_(e_variable_already_declared), lhs->lhs_name);
5996 return FAIL;
5997 }
5998 }
5999 else
6000 {
6001 int script_namespace = lhs->lhs_varlen > 1
6002 && STRNCMP(var_start, "s:", 2) == 0;
6003 int script_var = (script_namespace
6004 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006005 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006006 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006007 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006008 imported_T *import =
6009 find_imported(var_start, lhs->lhs_varlen, cctx);
6010
6011 if (script_namespace || script_var || import != NULL)
6012 {
6013 char_u *rawname = lhs->lhs_name
6014 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6015
6016 if (is_decl)
6017 {
6018 if (script_namespace)
6019 semsg(_(e_cannot_declare_script_variable_in_function),
6020 lhs->lhs_name);
6021 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006022 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006023 lhs->lhs_name);
6024 return FAIL;
6025 }
6026 else if (cctx->ctx_ufunc->uf_script_ctx_version
6027 == SCRIPT_VERSION_VIM9
6028 && script_namespace
6029 && !script_var && import == NULL)
6030 {
6031 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6032 return FAIL;
6033 }
6034
6035 lhs->lhs_dest = dest_script;
6036
6037 // existing script-local variables should have a type
6038 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6039 if (import != NULL)
6040 lhs->lhs_scriptvar_sid = import->imp_sid;
6041 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6042 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006043 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006045 lhs->lhs_scriptvar_sid, rawname,
6046 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6047 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 if (lhs->lhs_scriptvar_idx >= 0)
6049 {
6050 scriptitem_T *si = SCRIPT_ITEM(
6051 lhs->lhs_scriptvar_sid);
6052 svar_T *sv =
6053 ((svar_T *)si->sn_var_vals.ga_data)
6054 + lhs->lhs_scriptvar_idx;
6055 lhs->lhs_type = sv->sv_type;
6056 }
6057 }
6058 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006059 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006060 == FAIL)
6061 return FAIL;
6062 }
6063 }
6064
6065 if (declare_error)
6066 {
6067 vim9_declare_error(lhs->lhs_name);
6068 return FAIL;
6069 }
6070 }
6071
6072 // handle "a:name" as a name, not index "name" on "a"
6073 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6074 var_end = lhs->lhs_dest_end;
6075
6076 if (lhs->lhs_dest != dest_option)
6077 {
6078 if (is_decl && *var_end == ':')
6079 {
6080 char_u *p;
6081
6082 // parse optional type: "let var: type = expr"
6083 if (!VIM_ISWHITE(var_end[1]))
6084 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006085 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006086 return FAIL;
6087 }
6088 p = skipwhite(var_end + 1);
6089 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6090 if (lhs->lhs_type == NULL)
6091 return FAIL;
6092 lhs->lhs_has_type = TRUE;
6093 }
6094 else if (lhs->lhs_lvar != NULL)
6095 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6096 }
6097
Bram Moolenaare42939a2021-04-05 17:11:17 +02006098 if (oplen == 3 && !heredoc
6099 && lhs->lhs_dest != dest_global
6100 && !lhs->lhs_has_index
6101 && lhs->lhs_type->tt_type != VAR_STRING
6102 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006103 {
6104 emsg(_(e_can_only_concatenate_to_string));
6105 return FAIL;
6106 }
6107
6108 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6109 && cctx->ctx_skip != SKIP_YES)
6110 {
6111 if (oplen > 1 && !heredoc)
6112 {
6113 // +=, /=, etc. require an existing variable
6114 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6115 return FAIL;
6116 }
6117 if (!is_decl)
6118 {
6119 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6120 return FAIL;
6121 }
6122
Bram Moolenaar3f327882021-03-17 20:56:38 +01006123 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006124 if ((lhs->lhs_type->tt_type == VAR_FUNC
6125 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006126 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006127 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006128
6129 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6131 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6132 if (lhs->lhs_lvar == NULL)
6133 return FAIL;
6134 lhs->lhs_new_local = TRUE;
6135 }
6136
6137 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006138 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006139 {
6140 // Something follows after the variable: "var[idx]" or "var.key".
6141 // TODO: should we also handle "->func()" here?
6142 if (is_decl)
6143 {
6144 emsg(_(e_cannot_use_index_when_declaring_variable));
6145 return FAIL;
6146 }
6147
6148 if (var_start[lhs->lhs_varlen] == '['
6149 || var_start[lhs->lhs_varlen] == '.')
6150 {
6151 char_u *after = var_start + lhs->lhs_varlen;
6152 char_u *p;
6153
6154 // Only the last index is used below, if there are others
6155 // before it generate code for the expression. Thus for
6156 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6157 for (;;)
6158 {
6159 p = skip_index(after);
6160 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006161 {
6162 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006164 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006165 after = p;
6166 }
6167 if (after > var_start + lhs->lhs_varlen)
6168 {
6169 lhs->lhs_varlen = after - var_start;
6170 lhs->lhs_dest = dest_expr;
6171 // We don't know the type before evaluating the expression,
6172 // use "any" until then.
6173 lhs->lhs_type = &t_any;
6174 }
6175
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 if (lhs->lhs_type->tt_member == NULL)
6177 lhs->lhs_member_type = &t_any;
6178 else
6179 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6180 }
6181 else
6182 {
6183 semsg("Not supported yet: %s", var_start);
6184 return FAIL;
6185 }
6186 }
6187 return OK;
6188}
6189
6190/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006191 * Figure out the LHS and check a few errors.
6192 */
6193 static int
6194compile_assign_lhs(
6195 char_u *var_start,
6196 lhs_T *lhs,
6197 int cmdidx,
6198 int is_decl,
6199 int heredoc,
6200 int oplen,
6201 cctx_T *cctx)
6202{
6203 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6204 return FAIL;
6205
6206 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6207 {
6208 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6209 return FAIL;
6210 }
6211 if (!is_decl && lhs->lhs_lvar != NULL
6212 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6213 {
6214 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6215 return FAIL;
6216 }
6217 return OK;
6218}
6219
6220/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006221 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6222 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006223 */
6224 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006225compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006226 char_u *var_start,
6227 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006228 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006229 cctx_T *cctx)
6230{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006232 char_u *p;
6233 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006234 int need_white_before = TRUE;
6235 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006236
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 p = var_start + varlen;
6238 if (*p == '[')
6239 {
6240 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006241 if (*p == ':')
6242 {
6243 // empty first index, push zero
6244 r = generate_PUSHNR(cctx, 0);
6245 need_white_before = FALSE;
6246 }
6247 else
6248 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006249
6250 if (r == OK && *skipwhite(p) == ':')
6251 {
6252 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006253 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006254 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006255 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006256 empty_second = *skipwhite(p + 1) == ']';
6257 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6258 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006259 {
6260 semsg(_(e_white_space_required_before_and_after_str_at_str),
6261 ":", p);
6262 return FAIL;
6263 }
6264 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006265 if (*p == ']')
6266 // empty second index, push "none"
6267 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6268 else
6269 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006270 }
6271
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 if (r == OK && *skipwhite(p) != ']')
6273 {
6274 // this should not happen
6275 emsg(_(e_missbrac));
6276 r = FAIL;
6277 }
6278 }
6279 else // if (*p == '.')
6280 {
6281 char_u *key_end = to_name_end(p + 1, TRUE);
6282 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6283
6284 r = generate_PUSHS(cctx, key);
6285 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006286 return r;
6287}
6288
6289/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006290 * For a LHS with an index, load the variable to be indexed.
6291 */
6292 static int
6293compile_load_lhs(
6294 lhs_T *lhs,
6295 char_u *var_start,
6296 type_T *rhs_type,
6297 cctx_T *cctx)
6298{
6299 if (lhs->lhs_dest == dest_expr)
6300 {
6301 size_t varlen = lhs->lhs_varlen;
6302 int c = var_start[varlen];
6303 char_u *p = var_start;
6304 garray_T *stack = &cctx->ctx_type_stack;
6305
6306 // Evaluate "ll[expr]" of "ll[expr][idx]"
6307 var_start[varlen] = NUL;
6308 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6309 {
6310 // this should not happen
6311 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006312 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006313 return FAIL;
6314 }
6315 var_start[varlen] = c;
6316
6317 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6318 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6319 // now we can properly check the type
6320 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6321 && rhs_type != &t_void
6322 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6323 FALSE, FALSE) == FAIL)
6324 return FAIL;
6325 }
6326 else
6327 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6328 lhs->lhs_lvar, lhs->lhs_type);
6329 return OK;
6330}
6331
6332/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006333 * Produce code for loading "lhs" and also take care of an index.
6334 * Return OK/FAIL.
6335 */
6336 static int
6337compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6338{
6339 compile_load_lhs(lhs, var_start, NULL, cctx);
6340
6341 if (lhs->lhs_has_index)
6342 {
6343 int range = FALSE;
6344
6345 // Get member from list or dict. First compile the
6346 // index value.
6347 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6348 return FAIL;
6349 if (range)
6350 {
6351 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6352 var_start);
6353 return FAIL;
6354 }
6355
6356 // Get the member.
6357 if (compile_member(FALSE, cctx) == FAIL)
6358 return FAIL;
6359 }
6360 return OK;
6361}
6362
6363/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006364 * Assignment to a list or dict member, or ":unlet" for the item, using the
6365 * information in "lhs".
6366 * Returns OK or FAIL.
6367 */
6368 static int
6369compile_assign_unlet(
6370 char_u *var_start,
6371 lhs_T *lhs,
6372 int is_assign,
6373 type_T *rhs_type,
6374 cctx_T *cctx)
6375{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006376 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006377 garray_T *stack = &cctx->ctx_type_stack;
6378 int range = FALSE;
6379
Bram Moolenaar68452172021-04-12 21:21:02 +02006380 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006382 if (is_assign && range && lhs->lhs_type != &t_blob
6383 && lhs->lhs_type != &t_any)
6384 {
6385 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6386 return FAIL;
6387 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006388
6389 if (lhs->lhs_type == &t_any)
6390 {
6391 // Index on variable of unknown type: check at runtime.
6392 dest_type = VAR_ANY;
6393 }
6394 else
6395 {
6396 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006397 if (dest_type == VAR_DICT && range)
6398 {
6399 emsg(e_cannot_use_range_with_dictionary);
6400 return FAIL;
6401 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6403 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006404 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006405 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006406 type_T *type;
6407
6408 if (range)
6409 {
6410 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6411 if (need_type(type, &t_number,
6412 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006413 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006414 }
6415 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6416 if ((dest_type != VAR_BLOB || type != &t_special)
6417 && need_type(type, &t_number,
6418 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006419 return FAIL;
6420 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006421 }
6422
6423 // Load the dict or list. On the stack we then have:
6424 // - value (for assignment, not for :unlet)
6425 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006426 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006427 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006428 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6429 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006430
Bram Moolenaar68452172021-04-12 21:21:02 +02006431 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6432 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 {
6434 if (is_assign)
6435 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006436 if (range)
6437 {
6438 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6439 return FAIL;
6440 }
6441 else
6442 {
6443 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006444
Bram Moolenaar68452172021-04-12 21:21:02 +02006445 if (isn == NULL)
6446 return FAIL;
6447 isn->isn_arg.vartype = dest_type;
6448 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006449 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006450 else if (range)
6451 {
6452 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6453 return FAIL;
6454 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 else
6456 {
6457 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6458 return FAIL;
6459 }
6460 }
6461 else
6462 {
6463 emsg(_(e_indexable_type_required));
6464 return FAIL;
6465 }
6466
6467 return OK;
6468}
6469
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006470/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006471 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006472 * "let name"
6473 * "var name = expr"
6474 * "final name = expr"
6475 * "const name = expr"
6476 * "name = expr"
6477 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006478 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006479 * Return NULL for an error.
6480 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 */
6482 static char_u *
6483compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6484{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006485 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006487 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 char_u *ret = NULL;
6489 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006490 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006493 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 int oplen = 0;
6496 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006497 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006498 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 int is_decl = is_decl_command(cmdidx);
6501 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006502 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006504 // Skip over the "var" or "[var, var]" to get to any "=".
6505 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6506 if (p == NULL)
6507 return *arg == '[' ? arg : NULL;
6508
6509 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006511 // TODO: should we allow this, and figure out type inference from list
6512 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006513 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 return NULL;
6515 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006516 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 sp = p;
6519 p = skipwhite(p);
6520 op = p;
6521 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006522
6523 if (var_count > 0 && oplen == 0)
6524 // can be something like "[1, 2]->func()"
6525 return arg;
6526
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006527 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006529 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006530 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006531 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006532 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6533 {
6534 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6535 oplen = 2;
6536 incdec = TRUE;
6537 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 if (heredoc)
6540 {
6541 list_T *l;
6542 listitem_T *li;
6543
6544 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006545 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006547 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006548 if (l == NULL)
6549 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaar078269b2020-09-21 20:35:55 +02006551 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006553 // Push each line and the create the list.
6554 FOR_ALL_LIST_ITEMS(l, li)
6555 {
6556 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6557 li->li_tv.vval.v_string = NULL;
6558 }
6559 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 list_free(l);
6562 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006563 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006565 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006567 char_u *wp;
6568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006569 // for "[var, var] = expr" evaluate the expression here, loop over the
6570 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006571 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006572
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006573 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006574 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006575 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006576 if (compile_expr0(&p, cctx) == FAIL)
6577 return NULL;
6578 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006580 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006582 type_T *stacktype;
6583
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006584 stacktype = stack->ga_len == 0 ? &t_void
6585 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006586 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006588 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006589 goto theend;
6590 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006591 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006592 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006593 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006594 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006595 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6596 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006597 if (stacktype->tt_member != NULL)
6598 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006599 }
6600 }
6601
6602 /*
6603 * Loop over variables in "[var, var] = expr".
6604 * For "var = expr" and "let var: type" this is done only once.
6605 */
6606 if (var_count > 0)
6607 var_start = skipwhite(arg + 1); // skip over the "["
6608 else
6609 var_start = arg;
6610 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6611 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 int instr_count = -1;
6613
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006614 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6615 {
6616 // Ignore underscore in "[a, _, b] = list".
6617 if (var_count > 0)
6618 {
6619 var_start = skipwhite(var_start + 2);
6620 continue;
6621 }
6622 emsg(_(e_cannot_use_underscore_here));
6623 goto theend;
6624 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006625 vim_free(lhs.lhs_name);
6626
6627 /*
6628 * Figure out the LHS type and other properties.
6629 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006630 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6631 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006633 if (!heredoc)
6634 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006635 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006637 if (oplen > 0 && var_count == 0)
6638 {
6639 // skip over the "=" and the expression
6640 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006641 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006642 }
6643 }
6644 else if (oplen > 0)
6645 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006646 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006647 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006648
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006649 // For "var = expr" evaluate the expression.
6650 if (var_count == 0)
6651 {
6652 int r;
6653
6654 // for "+=", "*=", "..=" etc. first load the current value
6655 if (*op != '=')
6656 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006657 if (compile_load_lhs_with_index(&lhs, var_start,
6658 cctx) == FAIL)
6659 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006660 }
6661
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006662 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006663 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006664 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006665 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006666 r = generate_PUSHNR(cctx, 1);
6667 }
6668 else
6669 {
6670 // Temporarily hide the new local variable here, it is
6671 // not available to this expression.
6672 if (lhs.lhs_new_local)
6673 --cctx->ctx_locals.ga_len;
6674 wp = op + oplen;
6675 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6676 {
6677 if (lhs.lhs_new_local)
6678 ++cctx->ctx_locals.ga_len;
6679 goto theend;
6680 }
6681 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006683 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006684 if (r == FAIL)
6685 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006687 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006688 else if (semicolon && var_idx == var_count - 1)
6689 {
6690 // For "[var; var] = expr" get the rest of the list
6691 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6692 goto theend;
6693 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006694 else
6695 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006696 // For "[var, var] = expr" get the "var_idx" item from the
6697 // list.
6698 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006699 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006700 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006701
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006702 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006703 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006704 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006705 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006706 if ((rhs_type->tt_type == VAR_FUNC
6707 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006708 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006709 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006710 goto theend;
6711
Bram Moolenaar752fc692021-01-04 21:57:11 +01006712 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006713 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006714 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006715 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006716 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006717 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006718 }
6719 else
6720 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006721 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006722 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006723 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006724 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006725 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006726 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006727 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006728 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006729 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006730 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006731 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006732 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006733 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006734 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006735 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006736
Bram Moolenaar77709b12021-04-03 21:01:01 +02006737 // Without operator check type here, otherwise below.
6738 // Use the line number of the assignment.
6739 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006740 if (lhs.lhs_has_index)
6741 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006742 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006743 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006744 goto theend;
6745 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006746 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006747 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006748 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006749 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006751 else if (cmdidx == CMD_final)
6752 {
6753 emsg(_(e_final_requires_a_value));
6754 goto theend;
6755 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006758 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006759 goto theend;
6760 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006761 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006763 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006764 goto theend;
6765 }
6766 else
6767 {
6768 // variables are always initialized
6769 if (ga_grow(instr, 1) == FAIL)
6770 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006771 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006772 {
6773 case VAR_BOOL:
6774 generate_PUSHBOOL(cctx, VVAL_FALSE);
6775 break;
6776 case VAR_FLOAT:
6777#ifdef FEAT_FLOAT
6778 generate_PUSHF(cctx, 0.0);
6779#endif
6780 break;
6781 case VAR_STRING:
6782 generate_PUSHS(cctx, NULL);
6783 break;
6784 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006785 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006786 break;
6787 case VAR_FUNC:
6788 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6789 break;
6790 case VAR_LIST:
6791 generate_NEWLIST(cctx, 0);
6792 break;
6793 case VAR_DICT:
6794 generate_NEWDICT(cctx, 0);
6795 break;
6796 case VAR_JOB:
6797 generate_PUSHJOB(cctx, NULL);
6798 break;
6799 case VAR_CHANNEL:
6800 generate_PUSHCHANNEL(cctx, NULL);
6801 break;
6802 case VAR_NUMBER:
6803 case VAR_UNKNOWN:
6804 case VAR_ANY:
6805 case VAR_PARTIAL:
6806 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006807 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006808 case VAR_SPECIAL: // cannot happen
6809 generate_PUSHNR(cctx, 0);
6810 break;
6811 }
6812 }
6813 if (var_count == 0)
6814 end = p;
6815 }
6816
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006817 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006818 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006819 break;
6820
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006821 if (oplen > 0 && *op != '=')
6822 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006823 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006824 type_T *stacktype;
6825
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006826 if (*op == '.')
6827 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006828 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006829 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006830 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006831 if (
6832#ifdef FEAT_FLOAT
6833 // If variable is float operation with number is OK.
6834 !(expected == &t_float && stacktype == &t_number) &&
6835#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006836 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006837 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006838 goto theend;
6839
6840 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006841 {
6842 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6843 goto theend;
6844 }
6845 else if (*op == '+')
6846 {
6847 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006848 operator_type(lhs.lhs_member_type, stacktype),
6849 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006850 goto theend;
6851 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006852 else if (generate_two_op(cctx, op) == FAIL)
6853 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855
Bram Moolenaar752fc692021-01-04 21:57:11 +01006856 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006857 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006858 // Use the info in "lhs" to store the value at the index in the
6859 // list or dict.
6860 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6861 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006862 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006863 }
6864 else
6865 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006866 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006867 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006868 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006869 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006870 generate_LOCKCONST(cctx);
6871
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006872 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006873 && (lhs.lhs_type->tt_type == VAR_DICT
6874 || lhs.lhs_type->tt_type == VAR_LIST)
6875 && lhs.lhs_type->tt_member != NULL
6876 && lhs.lhs_type->tt_member != &t_any
6877 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006878 // Set the type in the list or dict, so that it can be checked,
6879 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006880 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006881
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006882 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6883 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006885
6886 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006889
6890 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006891 if (var_count > 0 && !semicolon)
6892 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006893 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006894 goto theend;
6895 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006896
Bram Moolenaarb2097502020-07-19 17:17:02 +02006897 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898
6899theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006900 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 return ret;
6902}
6903
6904/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006905 * Check for an assignment at "eap->cmd", compile it if found.
6906 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6907 */
6908 static int
6909may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6910{
6911 char_u *pskip;
6912 char_u *p;
6913
6914 // Assuming the command starts with a variable or function name,
6915 // find what follows.
6916 // Skip over "var.member", "var[idx]" and the like.
6917 // Also "&opt = val", "$ENV = val" and "@r = val".
6918 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6919 ? eap->cmd + 1 : eap->cmd;
6920 p = to_name_end(pskip, TRUE);
6921 if (p > eap->cmd && *p != NUL)
6922 {
6923 char_u *var_end;
6924 int oplen;
6925 int heredoc;
6926
6927 if (eap->cmd[0] == '@')
6928 var_end = eap->cmd + 2;
6929 else
6930 var_end = find_name_end(pskip, NULL, NULL,
6931 FNE_CHECK_START | FNE_INCL_BR);
6932 oplen = assignment_len(skipwhite(var_end), &heredoc);
6933 if (oplen > 0)
6934 {
6935 size_t len = p - eap->cmd;
6936
6937 // Recognize an assignment if we recognize the variable
6938 // name:
6939 // "g:var = expr"
6940 // "local = expr" where "local" is a local var.
6941 // "script = expr" where "script" is a script-local var.
6942 // "import = expr" where "import" is an imported var
6943 // "&opt = expr"
6944 // "$ENV = expr"
6945 // "@r = expr"
6946 if (*eap->cmd == '&'
6947 || *eap->cmd == '$'
6948 || *eap->cmd == '@'
6949 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006950 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006951 {
6952 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6953 if (*line == NULL || *line == eap->cmd)
6954 return FAIL;
6955 return OK;
6956 }
6957 }
6958 }
6959
6960 if (*eap->cmd == '[')
6961 {
6962 // [var, var] = expr
6963 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6964 if (*line == NULL)
6965 return FAIL;
6966 if (*line != eap->cmd)
6967 return OK;
6968 }
6969 return NOTDONE;
6970}
6971
6972/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006973 * Check if "name" can be "unlet".
6974 */
6975 int
6976check_vim9_unlet(char_u *name)
6977{
6978 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6979 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006980 // "unlet s:var" is allowed in legacy script.
6981 if (*name == 's' && !script_is_vim9())
6982 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006983 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006984 return FAIL;
6985 }
6986 return OK;
6987}
6988
6989/*
6990 * Callback passed to ex_unletlock().
6991 */
6992 static int
6993compile_unlet(
6994 lval_T *lvp,
6995 char_u *name_end,
6996 exarg_T *eap,
6997 int deep UNUSED,
6998 void *coookie)
6999{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007000 cctx_T *cctx = coookie;
7001 char_u *p = lvp->ll_name;
7002 int cc = *name_end;
7003 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007004
Bram Moolenaar752fc692021-01-04 21:57:11 +01007005 if (cctx->ctx_skip == SKIP_YES)
7006 return OK;
7007
7008 *name_end = NUL;
7009 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007010 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007011 // :unlet $ENV_VAR
7012 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7013 }
7014 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7015 {
7016 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007017
Bram Moolenaar752fc692021-01-04 21:57:11 +01007018 // This is similar to assigning: lookup the list/dict, compile the
7019 // idx/key. Then instead of storing the value unlet the item.
7020 // unlet {list}[idx]
7021 // unlet {dict}[key] dict.key
7022 //
7023 // Figure out the LHS type and other properties.
7024 //
7025 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7026
7027 // : unlet an indexed item
7028 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007029 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007030 iemsg("called compile_lhs() without an index");
7031 ret = FAIL;
7032 }
7033 else
7034 {
7035 // Use the info in "lhs" to unlet the item at the index in the
7036 // list or dict.
7037 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007038 }
7039
Bram Moolenaar752fc692021-01-04 21:57:11 +01007040 vim_free(lhs.lhs_name);
7041 }
7042 else if (check_vim9_unlet(p) == FAIL)
7043 {
7044 ret = FAIL;
7045 }
7046 else
7047 {
7048 // Normal name. Only supports g:, w:, t: and b: namespaces.
7049 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007050 }
7051
Bram Moolenaar752fc692021-01-04 21:57:11 +01007052 *name_end = cc;
7053 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007054}
7055
7056/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007057 * Callback passed to ex_unletlock().
7058 */
7059 static int
7060compile_lock_unlock(
7061 lval_T *lvp,
7062 char_u *name_end,
7063 exarg_T *eap,
7064 int deep UNUSED,
7065 void *coookie)
7066{
7067 cctx_T *cctx = coookie;
7068 int cc = *name_end;
7069 char_u *p = lvp->ll_name;
7070 int ret = OK;
7071 size_t len;
7072 char_u *buf;
7073
7074 if (cctx->ctx_skip == SKIP_YES)
7075 return OK;
7076
7077 // Cannot use :lockvar and :unlockvar on local variables.
7078 if (p[1] != ':')
7079 {
7080 char_u *end = skip_var_one(p, FALSE);
7081
7082 if (lookup_local(p, end - p, NULL, cctx) == OK)
7083 {
7084 emsg(_(e_cannot_lock_unlock_local_variable));
7085 return FAIL;
7086 }
7087 }
7088
7089 // Checking is done at runtime.
7090 *name_end = NUL;
7091 len = name_end - p + 20;
7092 buf = alloc(len);
7093 if (buf == NULL)
7094 ret = FAIL;
7095 else
7096 {
7097 vim_snprintf((char *)buf, len, "%s %s",
7098 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7099 p);
7100 ret = generate_EXEC(cctx, buf);
7101
7102 vim_free(buf);
7103 *name_end = cc;
7104 }
7105 return ret;
7106}
7107
7108/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007109 * compile "unlet var", "lock var" and "unlock var"
7110 * "arg" points to "var".
7111 */
7112 static char_u *
7113compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7114{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007115 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7116 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7117 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007118 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7119}
7120
7121/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 * Compile an :import command.
7123 */
7124 static char_u *
7125compile_import(char_u *arg, cctx_T *cctx)
7126{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007127 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128}
7129
7130/*
7131 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7132 */
7133 static int
7134compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7135{
7136 garray_T *instr = &cctx->ctx_instr;
7137 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7138
7139 if (endlabel == NULL)
7140 return FAIL;
7141 endlabel->el_next = *el;
7142 *el = endlabel;
7143 endlabel->el_end_label = instr->ga_len;
7144
7145 generate_JUMP(cctx, when, 0);
7146 return OK;
7147}
7148
7149 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007150compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151{
7152 garray_T *instr = &cctx->ctx_instr;
7153
7154 while (*el != NULL)
7155 {
7156 endlabel_T *cur = (*el);
7157 isn_T *isn;
7158
7159 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007160 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 *el = cur->el_next;
7162 vim_free(cur);
7163 }
7164}
7165
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007166 static void
7167compile_free_jump_to_end(endlabel_T **el)
7168{
7169 while (*el != NULL)
7170 {
7171 endlabel_T *cur = (*el);
7172
7173 *el = cur->el_next;
7174 vim_free(cur);
7175 }
7176}
7177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178/*
7179 * Create a new scope and set up the generic items.
7180 */
7181 static scope_T *
7182new_scope(cctx_T *cctx, scopetype_T type)
7183{
7184 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7185
7186 if (scope == NULL)
7187 return NULL;
7188 scope->se_outer = cctx->ctx_scope;
7189 cctx->ctx_scope = scope;
7190 scope->se_type = type;
7191 scope->se_local_count = cctx->ctx_locals.ga_len;
7192 return scope;
7193}
7194
7195/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007196 * Free the current scope and go back to the outer scope.
7197 */
7198 static void
7199drop_scope(cctx_T *cctx)
7200{
7201 scope_T *scope = cctx->ctx_scope;
7202
7203 if (scope == NULL)
7204 {
7205 iemsg("calling drop_scope() without a scope");
7206 return;
7207 }
7208 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007209 switch (scope->se_type)
7210 {
7211 case IF_SCOPE:
7212 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7213 case FOR_SCOPE:
7214 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7215 case WHILE_SCOPE:
7216 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7217 case TRY_SCOPE:
7218 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7219 case NO_SCOPE:
7220 case BLOCK_SCOPE:
7221 break;
7222 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007223 vim_free(scope);
7224}
7225
7226/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 * compile "if expr"
7228 *
7229 * "if expr" Produces instructions:
7230 * EVAL expr Push result of "expr"
7231 * JUMP_IF_FALSE end
7232 * ... body ...
7233 * end:
7234 *
7235 * "if expr | else" Produces instructions:
7236 * EVAL expr Push result of "expr"
7237 * JUMP_IF_FALSE else
7238 * ... body ...
7239 * JUMP_ALWAYS end
7240 * else:
7241 * ... body ...
7242 * end:
7243 *
7244 * "if expr1 | elseif expr2 | else" Produces instructions:
7245 * EVAL expr Push result of "expr"
7246 * JUMP_IF_FALSE elseif
7247 * ... body ...
7248 * JUMP_ALWAYS end
7249 * elseif:
7250 * EVAL expr Push result of "expr"
7251 * JUMP_IF_FALSE else
7252 * ... body ...
7253 * JUMP_ALWAYS end
7254 * else:
7255 * ... body ...
7256 * end:
7257 */
7258 static char_u *
7259compile_if(char_u *arg, cctx_T *cctx)
7260{
7261 char_u *p = arg;
7262 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007263 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007265 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007266 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267
Bram Moolenaara5565e42020-05-09 15:44:01 +02007268 CLEAR_FIELD(ppconst);
7269 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007270 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007271 clear_ppconst(&ppconst);
7272 return NULL;
7273 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007274 if (!ends_excmd2(arg, skipwhite(p)))
7275 {
7276 semsg(_(e_trailing_arg), p);
7277 return NULL;
7278 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007279 if (cctx->ctx_skip == SKIP_YES)
7280 clear_ppconst(&ppconst);
7281 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007282 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007283 int error = FALSE;
7284 int v;
7285
Bram Moolenaara5565e42020-05-09 15:44:01 +02007286 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007287 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007288 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007289 if (error)
7290 return NULL;
7291 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007292 }
7293 else
7294 {
7295 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007296 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007297 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007298 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007299 if (bool_on_stack(cctx) == FAIL)
7300 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302
Bram Moolenaara91a7132021-03-25 21:12:15 +01007303 // CMDMOD_REV must come before the jump
7304 generate_undo_cmdmods(cctx);
7305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 scope = new_scope(cctx, IF_SCOPE);
7307 if (scope == NULL)
7308 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007309 scope->se_skip_save = skip_save;
7310 // "is_had_return" will be reset if any block does not end in :return
7311 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007313 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007314 {
7315 // "where" is set when ":elseif", "else" or ":endif" is found
7316 scope->se_u.se_if.is_if_label = instr->ga_len;
7317 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7318 }
7319 else
7320 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321
Bram Moolenaarced68a02021-01-24 17:53:47 +01007322#ifdef FEAT_PROFILE
7323 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7324 && skip_save != SKIP_YES)
7325 {
7326 // generated a profile start, need to generate a profile end, since it
7327 // won't be done after returning
7328 cctx->ctx_skip = SKIP_NOT;
7329 generate_instr(cctx, ISN_PROF_END);
7330 cctx->ctx_skip = SKIP_YES;
7331 }
7332#endif
7333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 return p;
7335}
7336
7337 static char_u *
7338compile_elseif(char_u *arg, cctx_T *cctx)
7339{
7340 char_u *p = arg;
7341 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007342 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 isn_T *isn;
7344 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007345 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007346 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347
7348 if (scope == NULL || scope->se_type != IF_SCOPE)
7349 {
7350 emsg(_(e_elseif_without_if));
7351 return NULL;
7352 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007353 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007354 if (!cctx->ctx_had_return)
7355 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356
Bram Moolenaarced68a02021-01-24 17:53:47 +01007357 if (cctx->ctx_skip == SKIP_NOT)
7358 {
7359 // previous block was executed, this one and following will not
7360 cctx->ctx_skip = SKIP_YES;
7361 scope->se_u.se_if.is_seen_skip_not = TRUE;
7362 }
7363 if (scope->se_u.se_if.is_seen_skip_not)
7364 {
7365 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007366 // Do not count the "elseif" for profiling and cmdmod
7367 instr->ga_len = current_instr_idx(cctx);
7368
Bram Moolenaarced68a02021-01-24 17:53:47 +01007369 skip_expr_cctx(&p, cctx);
7370 return p;
7371 }
7372
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007373 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007374 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007375 int moved_cmdmod = FALSE;
7376
7377 // Move any CMDMOD instruction to after the jump
7378 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7379 {
7380 if (ga_grow(instr, 1) == FAIL)
7381 return NULL;
7382 ((isn_T *)instr->ga_data)[instr->ga_len] =
7383 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7384 --instr->ga_len;
7385 moved_cmdmod = TRUE;
7386 }
7387
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007388 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007390 return NULL;
7391 // previous "if" or "elseif" jumps here
7392 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7393 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007394 if (moved_cmdmod)
7395 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007398 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007399 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007400 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007401 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007402 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007403#ifdef FEAT_PROFILE
7404 if (cctx->ctx_profiling)
7405 {
7406 // the previous block was skipped, need to profile this line
7407 generate_instr(cctx, ISN_PROF_START);
7408 instr_count = instr->ga_len;
7409 }
7410#endif
7411 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007412 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007413 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007414 clear_ppconst(&ppconst);
7415 return NULL;
7416 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007417 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007418 if (!ends_excmd2(arg, skipwhite(p)))
7419 {
7420 semsg(_(e_trailing_arg), p);
7421 return NULL;
7422 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007423 if (scope->se_skip_save == SKIP_YES)
7424 clear_ppconst(&ppconst);
7425 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007426 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007427 int error = FALSE;
7428 int v;
7429
Bram Moolenaar7f141552020-05-09 17:35:53 +02007430 // The expression results in a constant.
7431 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007432 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7433 if (error)
7434 return NULL;
7435 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007436 clear_ppconst(&ppconst);
7437 scope->se_u.se_if.is_if_label = -1;
7438 }
7439 else
7440 {
7441 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007442 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007443 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007444 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007445 if (bool_on_stack(cctx) == FAIL)
7446 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447
Bram Moolenaara91a7132021-03-25 21:12:15 +01007448 // CMDMOD_REV must come before the jump
7449 generate_undo_cmdmods(cctx);
7450
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007451 // "where" is set when ":elseif", "else" or ":endif" is found
7452 scope->se_u.se_if.is_if_label = instr->ga_len;
7453 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455
7456 return p;
7457}
7458
7459 static char_u *
7460compile_else(char_u *arg, cctx_T *cctx)
7461{
7462 char_u *p = arg;
7463 garray_T *instr = &cctx->ctx_instr;
7464 isn_T *isn;
7465 scope_T *scope = cctx->ctx_scope;
7466
7467 if (scope == NULL || scope->se_type != IF_SCOPE)
7468 {
7469 emsg(_(e_else_without_if));
7470 return NULL;
7471 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007472 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007473 if (!cctx->ctx_had_return)
7474 scope->se_u.se_if.is_had_return = FALSE;
7475 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476
Bram Moolenaarced68a02021-01-24 17:53:47 +01007477#ifdef FEAT_PROFILE
7478 if (cctx->ctx_profiling)
7479 {
7480 if (cctx->ctx_skip == SKIP_NOT
7481 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7482 .isn_type == ISN_PROF_START)
7483 // the previous block was executed, do not count "else" for profiling
7484 --instr->ga_len;
7485 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7486 {
7487 // the previous block was not executed, this one will, do count the
7488 // "else" for profiling
7489 cctx->ctx_skip = SKIP_NOT;
7490 generate_instr(cctx, ISN_PROF_END);
7491 generate_instr(cctx, ISN_PROF_START);
7492 cctx->ctx_skip = SKIP_YES;
7493 }
7494 }
7495#endif
7496
7497 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007498 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007499 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007500 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007501 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007502 if (!cctx->ctx_had_return
7503 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7504 JUMP_ALWAYS, cctx) == FAIL)
7505 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007506 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007507
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007508 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007509 {
7510 if (scope->se_u.se_if.is_if_label >= 0)
7511 {
7512 // previous "if" or "elseif" jumps here
7513 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7514 isn->isn_arg.jump.jump_where = instr->ga_len;
7515 scope->se_u.se_if.is_if_label = -1;
7516 }
7517 }
7518
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007519 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007520 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522
7523 return p;
7524}
7525
7526 static char_u *
7527compile_endif(char_u *arg, cctx_T *cctx)
7528{
7529 scope_T *scope = cctx->ctx_scope;
7530 ifscope_T *ifscope;
7531 garray_T *instr = &cctx->ctx_instr;
7532 isn_T *isn;
7533
Bram Moolenaarfa984412021-03-25 22:15:28 +01007534 if (misplaced_cmdmod(cctx))
7535 return NULL;
7536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 if (scope == NULL || scope->se_type != IF_SCOPE)
7538 {
7539 emsg(_(e_endif_without_if));
7540 return NULL;
7541 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007542 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007543 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007544 if (!cctx->ctx_had_return)
7545 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007547 if (scope->se_u.se_if.is_if_label >= 0)
7548 {
7549 // previous "if" or "elseif" jumps here
7550 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7551 isn->isn_arg.jump.jump_where = instr->ga_len;
7552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007554 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007555
7556#ifdef FEAT_PROFILE
7557 // even when skipping we count the endif as executed, unless the block it's
7558 // in is skipped
7559 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7560 && scope->se_skip_save != SKIP_YES)
7561 {
7562 cctx->ctx_skip = SKIP_NOT;
7563 generate_instr(cctx, ISN_PROF_START);
7564 }
7565#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007566 cctx->ctx_skip = scope->se_skip_save;
7567
7568 // If all the blocks end in :return and there is an :else then the
7569 // had_return flag is set.
7570 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007572 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 return arg;
7574}
7575
7576/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007577 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 *
7579 * Produces instructions:
7580 * PUSHNR -1
7581 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007582 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7584 * - if beyond end, jump to "end"
7585 * - otherwise get item from list and push it
7586 * STORE var Store item in "var"
7587 * ... body ...
7588 * JUMP top Jump back to repeat
7589 * end: DROP Drop the result of "expr"
7590 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007591 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7592 * UNPACK 2 Split item in 2
7593 * STORE var1 Store item in "var1"
7594 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 */
7596 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007597compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007599 char_u *arg;
7600 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007601 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007603 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007604 int var_count = 0;
7605 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 garray_T *stack = &cctx->ctx_type_stack;
7608 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007609 lvar_T *loop_lvar; // loop iteration variable
7610 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007612 type_T *item_type = &t_any;
7613 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614
Bram Moolenaar792f7862020-11-23 08:31:18 +01007615 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007616 if (p == NULL)
7617 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007618 if (var_count == 0)
7619 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620
7621 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007622 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007623 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7624 return NULL;
7625 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 {
7627 emsg(_(e_missing_in));
7628 return NULL;
7629 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007630 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007631 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7632 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 scope = new_scope(cctx, FOR_SCOPE);
7635 if (scope == NULL)
7636 return NULL;
7637
Bram Moolenaar792f7862020-11-23 08:31:18 +01007638 // Reserve a variable to store the loop iteration counter and initialize it
7639 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007640 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7641 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007642 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007643 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007644 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007646 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007647 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648
7649 // compile "expr", it remains on the stack until "endfor"
7650 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007651 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007652 {
7653 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007655 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007656 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007658 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007659 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007661 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007662 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007664 semsg(_(e_for_loop_on_str_not_supported),
7665 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007666 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 return NULL;
7668 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007669
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007670 if (vartype->tt_type == VAR_STRING)
7671 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007672 else if (vartype->tt_type == VAR_BLOB)
7673 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007674 else if (vartype->tt_type == VAR_LIST
7675 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007676 {
7677 if (var_count == 1)
7678 item_type = vartype->tt_member;
7679 else if (vartype->tt_member->tt_type == VAR_LIST
7680 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007681 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007682 item_type = vartype->tt_member->tt_member;
7683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684
Bram Moolenaara91a7132021-03-25 21:12:15 +01007685 // CMDMOD_REV must come before the FOR instruction
7686 generate_undo_cmdmods(cctx);
7687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007689 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007690 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691
Bram Moolenaar792f7862020-11-23 08:31:18 +01007692 arg = arg_start;
7693 if (var_count > 1)
7694 {
7695 generate_UNPACK(cctx, var_count, semicolon);
7696 arg = skipwhite(arg + 1); // skip white after '['
7697
7698 // the list item is replaced by a number of items
7699 if (ga_grow(stack, var_count - 1) == FAIL)
7700 {
7701 drop_scope(cctx);
7702 return NULL;
7703 }
7704 --stack->ga_len;
7705 for (idx = 0; idx < var_count; ++idx)
7706 {
7707 ((type_T **)stack->ga_data)[stack->ga_len] =
7708 (semicolon && idx == 0) ? vartype : item_type;
7709 ++stack->ga_len;
7710 }
7711 }
7712
7713 for (idx = 0; idx < var_count; ++idx)
7714 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007715 assign_dest_T dest = dest_local;
7716 int opt_flags = 0;
7717 int vimvaridx = -1;
7718 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007719 type_T *lhs_type = &t_any;
7720 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007721
7722 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007723 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007724 name = vim_strnsave(arg, varlen);
7725 if (name == NULL)
7726 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007727 if (*p == ':')
7728 {
7729 p = skipwhite(p + 1);
7730 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7731 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007732
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007733 // TODO: script var not supported?
7734 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7735 &vimvaridx, &type, cctx) == FAIL)
7736 goto failed;
7737 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007738 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007739 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7740 0, 0, type, name) == FAIL)
7741 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007742 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007743 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007744 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007745 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007746 {
7747 semsg(_(e_variable_already_declared), arg);
7748 goto failed;
7749 }
7750
Bram Moolenaarea870692020-12-02 14:24:30 +01007751 if (STRNCMP(name, "s:", 2) == 0)
7752 {
7753 semsg(_(e_cannot_declare_script_variable_in_function), name);
7754 goto failed;
7755 }
7756
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007757 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007758 where.wt_index = var_count > 1 ? idx + 1 : 0;
7759 where.wt_variable = TRUE;
7760 if (lhs_type == &t_any)
7761 lhs_type = item_type;
7762 else if (item_type != &t_unknown
7763 && !(var_count > 1 && item_type == &t_any)
7764 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7765 goto failed;
7766 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007767 if (var_lvar == NULL)
7768 // out of memory or used as an argument
7769 goto failed;
7770
7771 if (semicolon && idx == var_count - 1)
7772 var_lvar->lv_type = vartype;
7773 else
7774 var_lvar->lv_type = item_type;
7775 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7776 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007777
7778 if (*p == ',' || *p == ';')
7779 ++p;
7780 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007781 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007782 }
7783
7784 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007785
7786failed:
7787 vim_free(name);
7788 drop_scope(cctx);
7789 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790}
7791
7792/*
7793 * compile "endfor"
7794 */
7795 static char_u *
7796compile_endfor(char_u *arg, cctx_T *cctx)
7797{
7798 garray_T *instr = &cctx->ctx_instr;
7799 scope_T *scope = cctx->ctx_scope;
7800 forscope_T *forscope;
7801 isn_T *isn;
7802
Bram Moolenaarfa984412021-03-25 22:15:28 +01007803 if (misplaced_cmdmod(cctx))
7804 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 if (scope == NULL || scope->se_type != FOR_SCOPE)
7807 {
7808 emsg(_(e_for));
7809 return NULL;
7810 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007811 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007813 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814
7815 // At end of ":for" scope jump back to the FOR instruction.
7816 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7817
7818 // Fill in the "end" label in the FOR statement so it can jump here
7819 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7820 isn->isn_arg.forloop.for_end = instr->ga_len;
7821
7822 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007823 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824
7825 // Below the ":for" scope drop the "expr" list from the stack.
7826 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7827 return NULL;
7828
7829 vim_free(scope);
7830
7831 return arg;
7832}
7833
7834/*
7835 * compile "while expr"
7836 *
7837 * Produces instructions:
7838 * top: EVAL expr Push result of "expr"
7839 * JUMP_IF_FALSE end jump if false
7840 * ... body ...
7841 * JUMP top Jump back to repeat
7842 * end:
7843 *
7844 */
7845 static char_u *
7846compile_while(char_u *arg, cctx_T *cctx)
7847{
7848 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 scope_T *scope;
7850
7851 scope = new_scope(cctx, WHILE_SCOPE);
7852 if (scope == NULL)
7853 return NULL;
7854
Bram Moolenaara91a7132021-03-25 21:12:15 +01007855 // "endwhile" jumps back here, one before when profiling or using cmdmods
7856 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857
7858 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007859 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007861 if (!ends_excmd2(arg, skipwhite(p)))
7862 {
7863 semsg(_(e_trailing_arg), p);
7864 return NULL;
7865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866
Bram Moolenaar13106602020-10-04 16:06:05 +02007867 if (bool_on_stack(cctx) == FAIL)
7868 return FAIL;
7869
Bram Moolenaara91a7132021-03-25 21:12:15 +01007870 // CMDMOD_REV must come before the jump
7871 generate_undo_cmdmods(cctx);
7872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007874 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 JUMP_IF_FALSE, cctx) == FAIL)
7876 return FAIL;
7877
7878 return p;
7879}
7880
7881/*
7882 * compile "endwhile"
7883 */
7884 static char_u *
7885compile_endwhile(char_u *arg, cctx_T *cctx)
7886{
7887 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007888 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889
Bram Moolenaarfa984412021-03-25 22:15:28 +01007890 if (misplaced_cmdmod(cctx))
7891 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007892 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7893 {
7894 emsg(_(e_while));
7895 return NULL;
7896 }
7897 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007898 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899
Bram Moolenaarf002a412021-01-24 13:34:18 +01007900#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007901 // count the endwhile before jumping
7902 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007903#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007906 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007907
7908 // Fill in the "end" label in the WHILE statement so it can jump here.
7909 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007910 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7911 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912
7913 vim_free(scope);
7914
7915 return arg;
7916}
7917
7918/*
7919 * compile "continue"
7920 */
7921 static char_u *
7922compile_continue(char_u *arg, cctx_T *cctx)
7923{
7924 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007925 int try_scopes = 0;
7926 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927
7928 for (;;)
7929 {
7930 if (scope == NULL)
7931 {
7932 emsg(_(e_continue));
7933 return NULL;
7934 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007935 if (scope->se_type == FOR_SCOPE)
7936 {
7937 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007939 }
7940 if (scope->se_type == WHILE_SCOPE)
7941 {
7942 loop_label = scope->se_u.se_while.ws_top_label;
7943 break;
7944 }
7945 if (scope->se_type == TRY_SCOPE)
7946 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 scope = scope->se_outer;
7948 }
7949
Bram Moolenaarc150c092021-02-13 15:02:46 +01007950 if (try_scopes > 0)
7951 // Inside one or more try/catch blocks we first need to jump to the
7952 // "finally" or "endtry" to cleanup.
7953 generate_TRYCONT(cctx, try_scopes, loop_label);
7954 else
7955 // Jump back to the FOR or WHILE instruction.
7956 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958 return arg;
7959}
7960
7961/*
7962 * compile "break"
7963 */
7964 static char_u *
7965compile_break(char_u *arg, cctx_T *cctx)
7966{
7967 scope_T *scope = cctx->ctx_scope;
7968 endlabel_T **el;
7969
7970 for (;;)
7971 {
7972 if (scope == NULL)
7973 {
7974 emsg(_(e_break));
7975 return NULL;
7976 }
7977 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7978 break;
7979 scope = scope->se_outer;
7980 }
7981
7982 // Jump to the end of the FOR or WHILE loop.
7983 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007984 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007986 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7988 return FAIL;
7989
7990 return arg;
7991}
7992
7993/*
7994 * compile "{" start of block
7995 */
7996 static char_u *
7997compile_block(char_u *arg, cctx_T *cctx)
7998{
7999 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8000 return NULL;
8001 return skipwhite(arg + 1);
8002}
8003
8004/*
8005 * compile end of block: drop one scope
8006 */
8007 static void
8008compile_endblock(cctx_T *cctx)
8009{
8010 scope_T *scope = cctx->ctx_scope;
8011
8012 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008013 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 vim_free(scope);
8015}
8016
8017/*
8018 * compile "try"
8019 * Creates a new scope for the try-endtry, pointing to the first catch and
8020 * finally.
8021 * Creates another scope for the "try" block itself.
8022 * TRY instruction sets up exception handling at runtime.
8023 *
8024 * "try"
8025 * TRY -> catch1, -> finally push trystack entry
8026 * ... try block
8027 * "throw {exception}"
8028 * EVAL {exception}
8029 * THROW create exception
8030 * ... try block
8031 * " catch {expr}"
8032 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008033 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034 * EVAL {expr}
8035 * MATCH
8036 * JUMP nomatch -> catch2
8037 * CATCH remove exception
8038 * ... catch block
8039 * " catch"
8040 * JUMP -> finally
8041 * catch2: CATCH remove exception
8042 * ... catch block
8043 * " finally"
8044 * finally:
8045 * ... finally block
8046 * " endtry"
8047 * ENDTRY pop trystack entry, may rethrow
8048 */
8049 static char_u *
8050compile_try(char_u *arg, cctx_T *cctx)
8051{
8052 garray_T *instr = &cctx->ctx_instr;
8053 scope_T *try_scope;
8054 scope_T *scope;
8055
Bram Moolenaarfa984412021-03-25 22:15:28 +01008056 if (misplaced_cmdmod(cctx))
8057 return NULL;
8058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 // scope that holds the jumps that go to catch/finally/endtry
8060 try_scope = new_scope(cctx, TRY_SCOPE);
8061 if (try_scope == NULL)
8062 return NULL;
8063
Bram Moolenaar69f70502021-01-01 16:10:46 +01008064 if (cctx->ctx_skip != SKIP_YES)
8065 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008066 isn_T *isn;
8067
8068 // "try_catch" is set when the first ":catch" is found or when no catch
8069 // is found and ":finally" is found.
8070 // "try_finally" is set when ":finally" is found
8071 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008072 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008073 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8074 return NULL;
8075 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8076 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008077 return NULL;
8078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079
8080 // scope for the try block itself
8081 scope = new_scope(cctx, BLOCK_SCOPE);
8082 if (scope == NULL)
8083 return NULL;
8084
8085 return arg;
8086}
8087
8088/*
8089 * compile "catch {expr}"
8090 */
8091 static char_u *
8092compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8093{
8094 scope_T *scope = cctx->ctx_scope;
8095 garray_T *instr = &cctx->ctx_instr;
8096 char_u *p;
8097 isn_T *isn;
8098
Bram Moolenaarfa984412021-03-25 22:15:28 +01008099 if (misplaced_cmdmod(cctx))
8100 return NULL;
8101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102 // end block scope from :try or :catch
8103 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8104 compile_endblock(cctx);
8105 scope = cctx->ctx_scope;
8106
8107 // Error if not in a :try scope
8108 if (scope == NULL || scope->se_type != TRY_SCOPE)
8109 {
8110 emsg(_(e_catch));
8111 return NULL;
8112 }
8113
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008114 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008116 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 return NULL;
8118 }
8119
Bram Moolenaar69f70502021-01-01 16:10:46 +01008120 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008122#ifdef FEAT_PROFILE
8123 // the profile-start should be after the jump
8124 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8125 .isn_type == ISN_PROF_START)
8126 --instr->ga_len;
8127#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008128 // Jump from end of previous block to :finally or :endtry
8129 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8130 JUMP_ALWAYS, cctx) == FAIL)
8131 return NULL;
8132
8133 // End :try or :catch scope: set value in ISN_TRY instruction
8134 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008135 if (isn->isn_arg.try.try_ref->try_catch == 0)
8136 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008137 if (scope->se_u.se_try.ts_catch_label != 0)
8138 {
8139 // Previous catch without match jumps here
8140 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8141 isn->isn_arg.jump.jump_where = instr->ga_len;
8142 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008143#ifdef FEAT_PROFILE
8144 if (cctx->ctx_profiling)
8145 {
8146 // a "throw" that jumps here needs to be counted
8147 generate_instr(cctx, ISN_PROF_END);
8148 // the "catch" is also counted
8149 generate_instr(cctx, ISN_PROF_START);
8150 }
8151#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 }
8153
8154 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008155 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008157 scope->se_u.se_try.ts_caught_all = TRUE;
8158 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 }
8160 else
8161 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008162 char_u *end;
8163 char_u *pat;
8164 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008165 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008166 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 // Push v:exception, push {expr} and MATCH
8169 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8170
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008171 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008172 if (*end != *p)
8173 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008174 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008175 vim_free(tofree);
8176 return FAIL;
8177 }
8178 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008179 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008180 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008181 len = (int)(end - tofree);
8182 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008183 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008184 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008185 if (pat == NULL)
8186 return FAIL;
8187 if (generate_PUSHS(cctx, pat) == FAIL)
8188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8191 return NULL;
8192
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008193 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8195 return NULL;
8196 }
8197
Bram Moolenaar69f70502021-01-01 16:10:46 +01008198 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 return NULL;
8200
8201 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8202 return NULL;
8203 return p;
8204}
8205
8206 static char_u *
8207compile_finally(char_u *arg, cctx_T *cctx)
8208{
8209 scope_T *scope = cctx->ctx_scope;
8210 garray_T *instr = &cctx->ctx_instr;
8211 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008212 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008213
Bram Moolenaarfa984412021-03-25 22:15:28 +01008214 if (misplaced_cmdmod(cctx))
8215 return NULL;
8216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 // end block scope from :try or :catch
8218 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8219 compile_endblock(cctx);
8220 scope = cctx->ctx_scope;
8221
8222 // Error if not in a :try scope
8223 if (scope == NULL || scope->se_type != TRY_SCOPE)
8224 {
8225 emsg(_(e_finally));
8226 return NULL;
8227 }
8228
8229 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008230 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008231 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008232 {
8233 emsg(_(e_finally_dup));
8234 return NULL;
8235 }
8236
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008237 this_instr = instr->ga_len;
8238#ifdef FEAT_PROFILE
8239 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8240 .isn_type == ISN_PROF_START)
8241 // jump to the profile start of the "finally"
8242 --this_instr;
8243#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008245 // Fill in the "end" label in jumps at the end of the blocks.
8246 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8247 this_instr, cctx);
8248
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008249 // If there is no :catch then an exception jumps to :finally.
8250 if (isn->isn_arg.try.try_ref->try_catch == 0)
8251 isn->isn_arg.try.try_ref->try_catch = this_instr;
8252 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008253 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254 {
8255 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008256 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008257 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008258 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008260 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8261 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 // TODO: set index in ts_finally_label jumps
8264
8265 return arg;
8266}
8267
8268 static char_u *
8269compile_endtry(char_u *arg, cctx_T *cctx)
8270{
8271 scope_T *scope = cctx->ctx_scope;
8272 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008273 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274
Bram Moolenaarfa984412021-03-25 22:15:28 +01008275 if (misplaced_cmdmod(cctx))
8276 return NULL;
8277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278 // end block scope from :catch or :finally
8279 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8280 compile_endblock(cctx);
8281 scope = cctx->ctx_scope;
8282
8283 // Error if not in a :try scope
8284 if (scope == NULL || scope->se_type != TRY_SCOPE)
8285 {
8286 if (scope == NULL)
8287 emsg(_(e_no_endtry));
8288 else if (scope->se_type == WHILE_SCOPE)
8289 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008290 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 emsg(_(e_endfor));
8292 else
8293 emsg(_(e_endif));
8294 return NULL;
8295 }
8296
Bram Moolenaarc150c092021-02-13 15:02:46 +01008297 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008298 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008300 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8301 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008302 {
8303 emsg(_(e_missing_catch_or_finally));
8304 return NULL;
8305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008307#ifdef FEAT_PROFILE
8308 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8309 .isn_type == ISN_PROF_START)
8310 // move the profile start after "endtry" so that it's not counted when
8311 // the exception is rethrown.
8312 --instr->ga_len;
8313#endif
8314
Bram Moolenaar69f70502021-01-01 16:10:46 +01008315 // Fill in the "end" label in jumps at the end of the blocks, if not
8316 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008317 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8318 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008319
Bram Moolenaar69f70502021-01-01 16:10:46 +01008320 if (scope->se_u.se_try.ts_catch_label != 0)
8321 {
8322 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008323 isn_T *isn = ((isn_T *)instr->ga_data)
8324 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008325 isn->isn_arg.jump.jump_where = instr->ga_len;
8326 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008327 }
8328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008329 compile_endblock(cctx);
8330
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008331 if (cctx->ctx_skip != SKIP_YES)
8332 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008333 // End :catch or :finally scope: set instruction index in ISN_TRY
8334 // instruction
8335 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008336 if (cctx->ctx_skip != SKIP_YES
8337 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8338 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008339#ifdef FEAT_PROFILE
8340 if (cctx->ctx_profiling)
8341 generate_instr(cctx, ISN_PROF_START);
8342#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 return arg;
8345}
8346
8347/*
8348 * compile "throw {expr}"
8349 */
8350 static char_u *
8351compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8352{
8353 char_u *p = skipwhite(arg);
8354
Bram Moolenaara5565e42020-05-09 15:44:01 +02008355 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008357 if (cctx->ctx_skip == SKIP_YES)
8358 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 if (may_generate_2STRING(-1, cctx) == FAIL)
8360 return NULL;
8361 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8362 return NULL;
8363
8364 return p;
8365}
8366
8367/*
8368 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008369 * compile "echomsg expr"
8370 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008371 * compile "execute expr"
8372 */
8373 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008374compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008375{
8376 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008377 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008378 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008379 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008380 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008381 garray_T *stack = &cctx->ctx_type_stack;
8382 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008383
8384 for (;;)
8385 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008386 if (ends_excmd2(prev, p))
8387 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008388 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008389 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008390 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008391
8392 if (cctx->ctx_skip != SKIP_YES)
8393 {
8394 // check for non-void type
8395 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8396 if (type->tt_type == VAR_VOID)
8397 {
8398 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8399 return NULL;
8400 }
8401 }
8402
Bram Moolenaarad39c092020-02-26 18:23:43 +01008403 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008404 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008405 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008406 }
8407
Bram Moolenaare4984292020-12-13 14:19:25 +01008408 if (count > 0)
8409 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008410 long save_lnum = cctx->ctx_lnum;
8411
8412 // Use the line number where the command started.
8413 cctx->ctx_lnum = start_ctx_lnum;
8414
Bram Moolenaare4984292020-12-13 14:19:25 +01008415 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8416 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8417 else if (cmdidx == CMD_execute)
8418 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8419 else if (cmdidx == CMD_echomsg)
8420 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8421 else
8422 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008423
8424 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 return p;
8427}
8428
8429/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008430 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008431 * instruction to compute it and return OK.
8432 * Otherwise return FAIL, the caller must deal with any range.
8433 */
8434 static int
8435compile_variable_range(exarg_T *eap, cctx_T *cctx)
8436{
8437 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8438 char_u *p = skipdigits(eap->cmd);
8439
8440 if (p == range_end)
8441 return FAIL;
8442 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8443}
8444
8445/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008446 * :put r
8447 * :put ={expr}
8448 */
8449 static char_u *
8450compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8451{
8452 char_u *line = arg;
8453 linenr_T lnum;
8454 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008455 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008456
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008457 eap->regname = *line;
8458
8459 if (eap->regname == '=')
8460 {
8461 char_u *p = line + 1;
8462
8463 if (compile_expr0(&p, cctx) == FAIL)
8464 return NULL;
8465 line = p;
8466 }
8467 else if (eap->regname != NUL)
8468 ++line;
8469
Bram Moolenaar08597872020-12-10 19:43:40 +01008470 if (compile_variable_range(eap, cctx) == OK)
8471 {
8472 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8473 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008474 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008475 {
8476 // Either no range or a number.
8477 // "errormsg" will not be set because the range is ADDR_LINES.
8478 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008479 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008480 return NULL;
8481 if (eap->addr_count == 0)
8482 lnum = -1;
8483 else
8484 lnum = eap->line2;
8485 if (above)
8486 --lnum;
8487 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008488
8489 generate_PUT(cctx, eap->regname, lnum);
8490 return line;
8491}
8492
8493/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008494 * A command that is not compiled, execute with legacy code.
8495 */
8496 static char_u *
8497compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8498{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008499 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008500 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008501 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008502
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008503 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008504 goto theend;
8505
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008506 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008507 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008508 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008509 int usefilter = FALSE;
8510
8511 has_expr = argt & (EX_XFILE | EX_EXPAND);
8512
8513 // If the command can be followed by a bar, find the bar and truncate
8514 // it, so that the following command can be compiled.
8515 // The '|' is overwritten with a NUL, it is put back below.
8516 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8517 && *eap->arg == '!')
8518 // :w !filter or :r !filter or :r! filter
8519 usefilter = TRUE;
8520 if ((argt & EX_TRLBAR) && !usefilter)
8521 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008522 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008523 separate_nextcmd(eap);
8524 if (eap->nextcmd != NULL)
8525 nextcmd = eap->nextcmd;
8526 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008527 else if (eap->cmdidx == CMD_wincmd)
8528 {
8529 p = eap->arg;
8530 if (*p != NUL)
8531 ++p;
8532 if (*p == 'g' || *p == Ctrl_G)
8533 ++p;
8534 p = skipwhite(p);
8535 if (*p == '|')
8536 {
8537 *p = NUL;
8538 nextcmd = p + 1;
8539 }
8540 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008541 }
8542
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008543 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8544 {
8545 // expand filename in "syntax include [@group] filename"
8546 has_expr = TRUE;
8547 eap->arg = skipwhite(eap->arg + 7);
8548 if (*eap->arg == '@')
8549 eap->arg = skiptowhite(eap->arg);
8550 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008551
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008552 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8553 && STRLEN(eap->arg) > 4)
8554 {
8555 int delim = *eap->arg;
8556
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008557 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008558 if (*p == delim)
8559 {
8560 eap->arg = p + 1;
8561 has_expr = TRUE;
8562 }
8563 }
8564
Bram Moolenaarecac5912021-01-05 19:23:28 +01008565 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8566 {
8567 // TODO: should only expand when appropriate for the command
8568 eap->arg = skiptowhite(eap->arg);
8569 has_expr = TRUE;
8570 }
8571
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008572 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008573 {
8574 int count = 0;
8575 char_u *start = skipwhite(line);
8576
8577 // :cmd xxx`=expr1`yyy`=expr2`zzz
8578 // PUSHS ":cmd xxx"
8579 // eval expr1
8580 // PUSHS "yyy"
8581 // eval expr2
8582 // PUSHS "zzz"
8583 // EXECCONCAT 5
8584 for (;;)
8585 {
8586 if (p > start)
8587 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008588 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008589 ++count;
8590 }
8591 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008592 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008593 return NULL;
8594 may_generate_2STRING(-1, cctx);
8595 ++count;
8596 p = skipwhite(p);
8597 if (*p != '`')
8598 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008599 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008600 return NULL;
8601 }
8602 start = p + 1;
8603
8604 p = (char_u *)strstr((char *)start, "`=");
8605 if (p == NULL)
8606 {
8607 if (*skipwhite(start) != NUL)
8608 {
8609 generate_PUSHS(cctx, vim_strsave(start));
8610 ++count;
8611 }
8612 break;
8613 }
8614 }
8615 generate_EXECCONCAT(cctx, count);
8616 }
8617 else
8618 generate_EXEC(cctx, line);
8619
8620theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008621 if (*nextcmd != NUL)
8622 {
8623 // the parser expects a pointer to the bar, put it back
8624 --nextcmd;
8625 *nextcmd = '|';
8626 }
8627
8628 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008629}
8630
Bram Moolenaar8238f082021-04-20 21:10:48 +02008631
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008632/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008633 * :s/pat/repl/
8634 */
8635 static char_u *
8636compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8637{
8638 char_u *cmd = eap->arg;
8639 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8640
8641 if (expr != NULL)
8642 {
8643 int delimiter = *cmd++;
8644
8645 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008646 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008647 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8648 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008649 garray_T save_ga = cctx->ctx_instr;
8650 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008651 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008652 int trailing_error;
8653 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008654 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008655 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008656
8657 cmd += 3;
8658 end = skip_substitute(cmd, delimiter);
8659
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008660 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008661 // labels are correct.
8662 cctx->ctx_instr.ga_len = 0;
8663 cctx->ctx_instr.ga_maxlen = 0;
8664 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008665 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008666 if (end[-1] == NUL)
8667 end[-1] = delimiter;
8668 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008669 trailing_error = *cmd != delimiter && *cmd != NUL;
8670
Bram Moolenaar169502f2021-04-21 12:19:35 +02008671 if (expr_res == FAIL || trailing_error
8672 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008673 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008674 if (trailing_error)
8675 semsg(_(e_trailing_arg), cmd);
8676 clear_instr_ga(&cctx->ctx_instr);
8677 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008678 return NULL;
8679 }
8680
Bram Moolenaar8238f082021-04-20 21:10:48 +02008681 // Move the generated instructions into the ISN_SUBSTITUTE
8682 // instructions, then restore the list of instructions before
8683 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008684 instr_count = cctx->ctx_instr.ga_len;
8685 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008686 instr[instr_count].isn_type = ISN_FINISH;
8687
8688 cctx->ctx_instr = save_ga;
8689 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8690 {
8691 int idx;
8692
8693 for (idx = 0; idx < instr_count; ++idx)
8694 delete_instr(instr + idx);
8695 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008696 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008697 }
8698 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8699 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008700
8701 // skip over flags
8702 if (*end == '&')
8703 ++end;
8704 while (ASCII_ISALPHA(*end) || *end == '#')
8705 ++end;
8706 return end;
8707 }
8708 }
8709
8710 return compile_exec(arg, eap, cctx);
8711}
8712
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008713 static char_u *
8714compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8715{
8716 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008717 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008718
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008719 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008720 {
8721 if (STRNCMP(arg, "END", 3) == 0)
8722 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008723 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008724 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008725 // First load the current variable value.
8726 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8727 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008728 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008729 }
8730
8731 // Gets the redirected text and put it on the stack, then store it
8732 // in the variable.
8733 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8734
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008735 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008736 generate_instr_drop(cctx, ISN_CONCAT, 1);
8737
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008738 if (lhs->lhs_has_index)
8739 {
8740 // Use the info in "lhs" to store the value at the index in the
8741 // list or dict.
8742 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8743 &t_string, cctx) == FAIL)
8744 return NULL;
8745 }
8746 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008747 return NULL;
8748
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008749 VIM_CLEAR(lhs->lhs_name);
8750 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008751 return arg + 3;
8752 }
8753 emsg(_(e_cannot_nest_redir));
8754 return NULL;
8755 }
8756
8757 if (arg[0] == '=' && arg[1] == '>')
8758 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008759 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008760
8761 // redirect to a variable is compiled
8762 arg += 2;
8763 if (*arg == '>')
8764 {
8765 ++arg;
8766 append = TRUE;
8767 }
8768 arg = skipwhite(arg);
8769
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008770 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008771 FALSE, FALSE, 1, cctx) == FAIL)
8772 return NULL;
8773 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008774 lhs->lhs_append = append;
8775 if (lhs->lhs_has_index)
8776 {
8777 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8778 if (lhs->lhs_whole == NULL)
8779 return NULL;
8780 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008781
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008782 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008783 }
8784
8785 // other redirects are handled like at script level
8786 return compile_exec(line, eap, cctx);
8787}
8788
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008789#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008790 static char_u *
8791compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8792{
8793 isn_T *isn;
8794 char_u *p;
8795
8796 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8797 if (isn == NULL)
8798 return NULL;
8799 isn->isn_arg.number = eap->cmdidx;
8800
8801 p = eap->arg;
8802 if (compile_expr0(&p, cctx) == FAIL)
8803 return NULL;
8804
8805 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8806 if (isn == NULL)
8807 return NULL;
8808 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8809 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8810 return NULL;
8811 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8812 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8813 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8814
8815 return p;
8816}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008817#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008818
Bram Moolenaar4c137212021-04-19 16:48:48 +02008819/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008820 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008821 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008822 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008823 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008824add_def_function(ufunc_T *ufunc)
8825{
8826 dfunc_T *dfunc;
8827
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008828 if (def_functions.ga_len == 0)
8829 {
8830 // The first position is not used, so that a zero uf_dfunc_idx means it
8831 // wasn't set.
8832 if (ga_grow(&def_functions, 1) == FAIL)
8833 return FAIL;
8834 ++def_functions.ga_len;
8835 }
8836
Bram Moolenaar09689a02020-05-09 22:50:08 +02008837 // Add the function to "def_functions".
8838 if (ga_grow(&def_functions, 1) == FAIL)
8839 return FAIL;
8840 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8841 CLEAR_POINTER(dfunc);
8842 dfunc->df_idx = def_functions.ga_len;
8843 ufunc->uf_dfunc_idx = dfunc->df_idx;
8844 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008845 dfunc->df_name = vim_strsave(ufunc->uf_name);
8846 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008847 ++def_functions.ga_len;
8848 return OK;
8849}
8850
8851/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852 * After ex_function() has collected all the function lines: parse and compile
8853 * the lines into instructions.
8854 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008855 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8856 * the return statement (used for lambda). When uf_ret_type is already set
8857 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008858 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008859 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008860 * This can be used recursively through compile_lambda(), which may reallocate
8861 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008862 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008863 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008864 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008865compile_def_function(
8866 ufunc_T *ufunc,
8867 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008868 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008869 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008871 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008872 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008873 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875 cctx_T cctx;
8876 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008877 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008878 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 int ret = FAIL;
8880 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008881 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008882 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008883 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008884 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008885#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008886 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008887#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008888
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008889 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008890 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008891 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008892 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8894 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008895 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008896 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008897 else
8898 {
8899 if (add_def_function(ufunc) == FAIL)
8900 return FAIL;
8901 new_def_function = TRUE;
8902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008903
Bram Moolenaar985116a2020-07-12 17:31:09 +02008904 ufunc->uf_def_status = UF_COMPILING;
8905
Bram Moolenaara80faa82020-04-12 19:37:17 +02008906 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008907
Bram Moolenaarf002a412021-01-24 13:34:18 +01008908#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008909 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008910#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911 cctx.ctx_ufunc = ufunc;
8912 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008913 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008914 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8915 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8916 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8917 cctx.ctx_type_list = &ufunc->uf_type_list;
8918 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8919 instr = &cctx.ctx_instr;
8920
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008921 // Set the context to the function, it may be compiled when called from
8922 // another script. Set the script version to the most modern one.
8923 // The line number will be set in next_line_from_context().
8924 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008925 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8926
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008927 // Don't use the flag from ":legacy" here.
8928 cmdmod.cmod_flags &= ~CMOD_LEGACY;
8929
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008930 // Make sure error messages are OK.
8931 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8932 if (do_estack_push)
8933 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008934 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008935
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008936 if (ufunc->uf_def_args.ga_len > 0)
8937 {
8938 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008939 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008940 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008941 int i;
8942 char_u *arg;
8943 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008944 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008945
8946 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008947 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008948 for (i = 0; i < count; ++i)
8949 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008950 garray_T *stack = &cctx.ctx_type_stack;
8951 type_T *val_type;
8952 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008953 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008954 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008955 int jump_instr_idx = instr->ga_len;
8956 isn_T *isn;
8957
8958 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8959 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8960 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008961
8962 // Make sure later arguments are not found.
8963 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008964
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008965 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008966 r = compile_expr0(&arg, &cctx);
8967
8968 ufunc->uf_args.ga_len = uf_args_len;
8969 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008970 goto erret;
8971
8972 // If no type specified use the type of the default value.
8973 // Otherwise check that the default value type matches the
8974 // specified type.
8975 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008976 where.wt_index = arg_idx + 1;
8977 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008978 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008979 {
8980 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008981 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008982 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008983 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008984 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008985 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008986
8987 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008988 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008989
8990 // set instruction index in JUMP_IF_ARG_SET to here
8991 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8992 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008993 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008994
8995 if (did_set_arg_type)
8996 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008997 }
8998
8999 /*
9000 * Loop over all the lines of the function and generate instructions.
9001 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009002 for (;;)
9003 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009004 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009005 int starts_with_colon = FALSE;
9006 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009007 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009008
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009009 // Bail out on the first error to avoid a flood of errors and report
9010 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009011 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009012 goto erret;
9013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009014 if (line != NULL && *line == '|')
9015 // the line continues after a '|'
9016 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009017 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009018 && !(*line == '#' && (line == cctx.ctx_line_start
9019 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009020 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009021 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022 goto erret;
9023 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009024 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9025 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 else
9027 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009028 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009029 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009030 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009031 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009032#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009033 if (cctx.ctx_skip != SKIP_YES)
9034 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009035#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009036 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009037 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009038 // Make a copy, splitting off nextcmd and removing trailing spaces
9039 // may change it.
9040 if (line != NULL)
9041 {
9042 line = vim_strsave(line);
9043 vim_free(line_to_free);
9044 line_to_free = line;
9045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009046 }
9047
Bram Moolenaara80faa82020-04-12 19:37:17 +02009048 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009049 ea.cmdlinep = &line;
9050 ea.cmd = skipwhite(line);
9051
Bram Moolenaarb2049902021-01-24 12:53:53 +01009052 if (*ea.cmd == '#')
9053 {
9054 // "#" starts a comment
9055 line = (char_u *)"";
9056 continue;
9057 }
9058
Bram Moolenaarf002a412021-01-24 13:34:18 +01009059#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009060 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
9061 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009062 {
9063 may_generate_prof_end(&cctx, prof_lnum);
9064
9065 prof_lnum = cctx.ctx_lnum;
9066 generate_instr(&cctx, ISN_PROF_START);
9067 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009068#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009069
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009070 // Some things can be recognized by the first character.
9071 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009072 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009073 case '}':
9074 {
9075 // "}" ends a block scope
9076 scopetype_T stype = cctx.ctx_scope == NULL
9077 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009078
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009079 if (stype == BLOCK_SCOPE)
9080 {
9081 compile_endblock(&cctx);
9082 line = ea.cmd;
9083 }
9084 else
9085 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009086 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009087 goto erret;
9088 }
9089 if (line != NULL)
9090 line = skipwhite(ea.cmd + 1);
9091 continue;
9092 }
9093
9094 case '{':
9095 // "{" starts a block scope
9096 // "{'a': 1}->func() is something else
9097 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9098 {
9099 line = compile_block(ea.cmd, &cctx);
9100 continue;
9101 }
9102 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009103 }
9104
9105 /*
9106 * COMMAND MODIFIERS
9107 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009108 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009109 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9110 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009111 {
9112 if (errormsg != NULL)
9113 goto erret;
9114 // empty line or comment
9115 line = (char_u *)"";
9116 continue;
9117 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009118 generate_cmdmods(&cctx, &local_cmdmod);
9119 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009120
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009121 // Check if there was a colon after the last command modifier or before
9122 // the current position.
9123 for (p = ea.cmd; p >= line; --p)
9124 {
9125 if (*p == ':')
9126 starts_with_colon = TRUE;
9127 if (p < ea.cmd && !VIM_ISWHITE(*p))
9128 break;
9129 }
9130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009131 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009132 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009133 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009134 {
9135 if (*ea.cmd == '(')
9136 // not for "call()"
9137 ea.cmd = p;
9138 else
9139 ea.cmd = skipwhite(ea.cmd);
9140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009141
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009142 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009143 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009144 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009145
Bram Moolenaar17126b12021-01-07 22:03:02 +01009146 // Check for assignment after command modifiers.
9147 assign = may_compile_assignment(&ea, &line, &cctx);
9148 if (assign == OK)
9149 goto nextline;
9150 if (assign == FAIL)
9151 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009152 }
9153
9154 /*
9155 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009156 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009157 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009158 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009159 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009160 if (starts_with_colon || !(*cmd == '\''
9161 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009162 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009163 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009164 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009165 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009166 if (!starts_with_colon)
9167 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009168 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009169 goto erret;
9170 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009171 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009172 if (ends_excmd2(line, ea.cmd))
9173 {
9174 // A range without a command: jump to the line.
9175 // TODO: compile to a more efficient command, possibly
9176 // calling parse_cmd_address().
9177 ea.cmdidx = CMD_SIZE;
9178 line = compile_exec(line, &ea, &cctx);
9179 goto nextline;
9180 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009181 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009182 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009183 p = find_ex_command(&ea, NULL, starts_with_colon
9184 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009185
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009186 if (p == NULL)
9187 {
9188 if (cctx.ctx_skip != SKIP_YES)
9189 emsg(_(e_ambiguous_use_of_user_defined_command));
9190 goto erret;
9191 }
9192
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009193 // When using ":legacy cmd" always use compile_exec().
9194 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
9195 ea.cmdidx = CMD_legacy;
9196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009197 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9198 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009199 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009200 {
9201 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009202 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009203 }
9204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009206 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009208 // CMD_var cannot happen, compile_assignment() above would be
9209 // used. Most likely an assignment to a non-existing variable.
9210 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009211 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 }
9214
Bram Moolenaar3988f642020-08-27 22:43:03 +02009215 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009216 && ea.cmdidx != CMD_elseif
9217 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009218 && ea.cmdidx != CMD_endif
9219 && ea.cmdidx != CMD_endfor
9220 && ea.cmdidx != CMD_endwhile
9221 && ea.cmdidx != CMD_catch
9222 && ea.cmdidx != CMD_finally
9223 && ea.cmdidx != CMD_endtry)
9224 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009225 emsg(_(e_unreachable_code_after_return));
9226 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009227 }
9228
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009229 p = skipwhite(p);
9230 if (ea.cmdidx != CMD_SIZE
9231 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9232 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009233 if (ea.cmdidx >= 0)
9234 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009235 if ((ea.argt & EX_BANG) && *p == '!')
9236 {
9237 ea.forceit = TRUE;
9238 p = skipwhite(p + 1);
9239 }
9240 }
9241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 switch (ea.cmdidx)
9243 {
9244 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009245 ea.arg = p;
9246 line = compile_nested_function(&ea, &cctx);
9247 break;
9248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009249 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009250 // TODO: should we allow this, e.g. to declare a global
9251 // function?
9252 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009253 goto erret;
9254
9255 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009256 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009257 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 break;
9259
9260 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009261 emsg(_(e_cannot_use_let_in_vim9_script));
9262 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009263 case CMD_var:
9264 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009266 case CMD_increment:
9267 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009268 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009269 if (line == p)
9270 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 break;
9272
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009273 case CMD_unlet:
9274 case CMD_unlockvar:
9275 case CMD_lockvar:
9276 line = compile_unletlock(p, &ea, &cctx);
9277 break;
9278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case CMD_import:
9280 line = compile_import(p, &cctx);
9281 break;
9282
9283 case CMD_if:
9284 line = compile_if(p, &cctx);
9285 break;
9286 case CMD_elseif:
9287 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009288 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 break;
9290 case CMD_else:
9291 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009292 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009293 break;
9294 case CMD_endif:
9295 line = compile_endif(p, &cctx);
9296 break;
9297
9298 case CMD_while:
9299 line = compile_while(p, &cctx);
9300 break;
9301 case CMD_endwhile:
9302 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009303 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009304 break;
9305
9306 case CMD_for:
9307 line = compile_for(p, &cctx);
9308 break;
9309 case CMD_endfor:
9310 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009311 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009312 break;
9313 case CMD_continue:
9314 line = compile_continue(p, &cctx);
9315 break;
9316 case CMD_break:
9317 line = compile_break(p, &cctx);
9318 break;
9319
9320 case CMD_try:
9321 line = compile_try(p, &cctx);
9322 break;
9323 case CMD_catch:
9324 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009325 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009326 break;
9327 case CMD_finally:
9328 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009329 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009330 break;
9331 case CMD_endtry:
9332 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009333 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 break;
9335 case CMD_throw:
9336 line = compile_throw(p, &cctx);
9337 break;
9338
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009339 case CMD_eval:
9340 if (compile_expr0(&p, &cctx) == FAIL)
9341 goto erret;
9342
Bram Moolenaar3988f642020-08-27 22:43:03 +02009343 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009344 generate_instr_drop(&cctx, ISN_DROP, 1);
9345
9346 line = skipwhite(p);
9347 break;
9348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009349 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009350 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009351 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009352 case CMD_echomsg:
9353 case CMD_echoerr:
9354 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009355 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009356
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009357 case CMD_put:
9358 ea.cmd = cmd;
9359 line = compile_put(p, &ea, &cctx);
9360 break;
9361
Bram Moolenaar4c137212021-04-19 16:48:48 +02009362 case CMD_substitute:
9363 if (cctx.ctx_skip == SKIP_YES)
9364 line = (char_u *)"";
9365 else
9366 {
9367 ea.arg = p;
9368 line = compile_substitute(line, &ea, &cctx);
9369 }
9370 break;
9371
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009372 case CMD_redir:
9373 ea.arg = p;
9374 line = compile_redir(line, &ea, &cctx);
9375 break;
9376
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009377 case CMD_cexpr:
9378 case CMD_lexpr:
9379 case CMD_caddexpr:
9380 case CMD_laddexpr:
9381 case CMD_cgetexpr:
9382 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009383#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009384 ea.arg = p;
9385 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009386#else
9387 ex_ni(&ea);
9388 line = NULL;
9389#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009390 break;
9391
Bram Moolenaar3988f642020-08-27 22:43:03 +02009392 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009393
Bram Moolenaarae616492020-07-28 20:07:27 +02009394 case CMD_append:
9395 case CMD_change:
9396 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009397 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009398 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009399 case CMD_xit:
9400 not_in_vim9(&ea);
9401 goto erret;
9402
Bram Moolenaar002262f2020-07-08 17:47:57 +02009403 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009404 if (cctx.ctx_skip != SKIP_YES)
9405 {
9406 semsg(_(e_invalid_command_str), ea.cmd);
9407 goto erret;
9408 }
9409 // We don't check for a next command here.
9410 line = (char_u *)"";
9411 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009413 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009414 if (cctx.ctx_skip == SKIP_YES)
9415 {
9416 // We don't check for a next command here.
9417 line = (char_u *)"";
9418 }
9419 else
9420 {
9421 // Not recognized, execute with do_cmdline_cmd().
9422 ea.arg = p;
9423 line = compile_exec(line, &ea, &cctx);
9424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425 break;
9426 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009427nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009428 if (line == NULL)
9429 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009430 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009431
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009432 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009433 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009435 if (cctx.ctx_type_stack.ga_len < 0)
9436 {
9437 iemsg("Type stack underflow");
9438 goto erret;
9439 }
9440 }
9441
9442 if (cctx.ctx_scope != NULL)
9443 {
9444 if (cctx.ctx_scope->se_type == IF_SCOPE)
9445 emsg(_(e_endif));
9446 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9447 emsg(_(e_endwhile));
9448 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9449 emsg(_(e_endfor));
9450 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009451 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009452 goto erret;
9453 }
9454
Bram Moolenaarefd88552020-06-18 20:50:10 +02009455 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009456 {
9457 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009459 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009460 goto erret;
9461 }
9462
9463 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009464 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009465 }
9466
Bram Moolenaar599410c2021-04-10 14:03:43 +02009467 // When compiled with ":silent!" and there was an error don't consider the
9468 // function compiled.
9469 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009470 {
9471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9472 + ufunc->uf_dfunc_idx;
9473 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009474 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009475#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009476 if (cctx.ctx_profiling)
9477 {
9478 dfunc->df_instr_prof = instr->ga_data;
9479 dfunc->df_instr_prof_count = instr->ga_len;
9480 }
9481 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009482#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009483 {
9484 dfunc->df_instr = instr->ga_data;
9485 dfunc->df_instr_count = instr->ga_len;
9486 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009487 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009488 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009489 if (cctx.ctx_outer_used)
9490 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009491 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009493
9494 ret = OK;
9495
9496erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009497 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009498 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009499 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9500 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009501
Bram Moolenaar8238f082021-04-20 21:10:48 +02009502 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009503 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009504
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009505 // If using the last entry in the table and it was added above, we
9506 // might as well remove it.
9507 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009508 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009509 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009510 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009511 ufunc->uf_dfunc_idx = 0;
9512 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009513 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009514
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009515 while (cctx.ctx_scope != NULL)
9516 drop_scope(&cctx);
9517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009518 if (errormsg != NULL)
9519 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009520 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009521 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009522 }
9523
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009524 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9525 {
9526 if (ret == OK)
9527 {
9528 emsg(_(e_missing_redir_end));
9529 ret = FAIL;
9530 }
9531 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009532 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009533 }
9534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009536 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009537 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009538 if (do_estack_push)
9539 estack_pop();
9540
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009541 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009542 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009543 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009544 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009545 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009546}
9547
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009548 void
9549set_function_type(ufunc_T *ufunc)
9550{
9551 int varargs = ufunc->uf_va_name != NULL;
9552 int argcount = ufunc->uf_args.ga_len;
9553
9554 // Create a type for the function, with the return type and any
9555 // argument types.
9556 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9557 // The type is included in "tt_args".
9558 if (argcount > 0 || varargs)
9559 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009560 if (ufunc->uf_type_list.ga_itemsize == 0)
9561 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009562 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9563 argcount, &ufunc->uf_type_list);
9564 // Add argument types to the function type.
9565 if (func_type_add_arg_types(ufunc->uf_func_type,
9566 argcount + varargs,
9567 &ufunc->uf_type_list) == FAIL)
9568 return;
9569 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9570 ufunc->uf_func_type->tt_min_argcount =
9571 argcount - ufunc->uf_def_args.ga_len;
9572 if (ufunc->uf_arg_types == NULL)
9573 {
9574 int i;
9575
9576 // lambda does not have argument types.
9577 for (i = 0; i < argcount; ++i)
9578 ufunc->uf_func_type->tt_args[i] = &t_any;
9579 }
9580 else
9581 mch_memmove(ufunc->uf_func_type->tt_args,
9582 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9583 if (varargs)
9584 {
9585 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009586 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009587 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9588 }
9589 }
9590 else
9591 // No arguments, can use a predefined type.
9592 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9593 argcount, &ufunc->uf_type_list);
9594}
9595
9596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597/*
9598 * Delete an instruction, free what it contains.
9599 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009600 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601delete_instr(isn_T *isn)
9602{
9603 switch (isn->isn_type)
9604 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009605 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009606 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009607 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009608 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009609 case ISN_LOADENV:
9610 case ISN_LOADG:
9611 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009612 case ISN_LOADT:
9613 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009614 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009615 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009616 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009617 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009618 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009619 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009620 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009621 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009622 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009623 case ISN_STOREW:
9624 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009625 vim_free(isn->isn_arg.string);
9626 break;
9627
Bram Moolenaar4c137212021-04-19 16:48:48 +02009628 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009629 {
9630 int idx;
9631 isn_T *list = isn->isn_arg.subs.subs_instr;
9632
9633 vim_free(isn->isn_arg.subs.subs_cmd);
9634 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9635 delete_instr(list + idx);
9636 vim_free(list);
9637 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009638 break;
9639
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009640 case ISN_INSTR:
9641 {
9642 int idx;
9643 isn_T *list = isn->isn_arg.instr;
9644
9645 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9646 delete_instr(list + idx);
9647 vim_free(list);
9648 }
9649 break;
9650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009652 case ISN_STORES:
9653 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009654 break;
9655
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009656 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009657 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009658 vim_free(isn->isn_arg.unlet.ul_name);
9659 break;
9660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009661 case ISN_STOREOPT:
9662 vim_free(isn->isn_arg.storeopt.so_name);
9663 break;
9664
9665 case ISN_PUSHBLOB: // push blob isn_arg.blob
9666 blob_unref(isn->isn_arg.blob);
9667 break;
9668
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009669 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009670#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009671 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009672#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009673 break;
9674
9675 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009676#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009677 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009678#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009679 break;
9680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009681 case ISN_UCALL:
9682 vim_free(isn->isn_arg.ufunc.cuf_name);
9683 break;
9684
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009685 case ISN_FUNCREF:
9686 {
9687 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9688 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009689 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009690
Bram Moolenaar077a4232020-12-22 18:33:27 +01009691 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9692 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009693 }
9694 break;
9695
9696 case ISN_DCALL:
9697 {
9698 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9699 + isn->isn_arg.dfunc.cdf_idx;
9700
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009701 if (dfunc->df_ufunc != NULL
9702 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009703 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009704 }
9705 break;
9706
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009707 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009708 {
9709 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9710 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9711
9712 if (ufunc != NULL)
9713 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009714 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009715 func_ptr_unref(ufunc);
9716 }
9717
9718 vim_free(lambda);
9719 vim_free(isn->isn_arg.newfunc.nf_global);
9720 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009721 break;
9722
Bram Moolenaar5e654232020-09-16 15:22:00 +02009723 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009724 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009725 free_type(isn->isn_arg.type.ct_type);
9726 break;
9727
Bram Moolenaar02194d22020-10-24 23:08:38 +02009728 case ISN_CMDMOD:
9729 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9730 ->cmod_filter_regmatch.regprog);
9731 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9732 break;
9733
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009734 case ISN_LOADSCRIPT:
9735 case ISN_STORESCRIPT:
9736 vim_free(isn->isn_arg.script.scriptref);
9737 break;
9738
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009739 case ISN_TRY:
9740 vim_free(isn->isn_arg.try.try_ref);
9741 break;
9742
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009743 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009744 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009745 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9746 break;
9747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009748 case ISN_2BOOL:
9749 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009750 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009751 case ISN_ADDBLOB:
9752 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009753 case ISN_ANYINDEX:
9754 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009755 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009756 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009757 case ISN_BLOBINDEX:
9758 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009760 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009761 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009763 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009764 case ISN_COMPAREANY:
9765 case ISN_COMPAREBLOB:
9766 case ISN_COMPAREBOOL:
9767 case ISN_COMPAREDICT:
9768 case ISN_COMPAREFLOAT:
9769 case ISN_COMPAREFUNC:
9770 case ISN_COMPARELIST:
9771 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009772 case ISN_COMPARESPECIAL:
9773 case ISN_COMPARESTRING:
9774 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009775 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009776 case ISN_DROP:
9777 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009778 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009779 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009780 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009781 case ISN_EXECCONCAT:
9782 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009783 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009784 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009785 case ISN_GETITEM:
9786 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009787 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009788 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009789 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009790 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009791 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009792 case ISN_LOADBDICT:
9793 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009794 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009796 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009798 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009799 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009800 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009801 case ISN_NEGATENR:
9802 case ISN_NEWDICT:
9803 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009804 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009805 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009806 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009807 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009808 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009809 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009810 case ISN_PROF_END:
9811 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009812 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 case ISN_PUSHF:
9814 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009815 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009816 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009817 case ISN_REDIREND:
9818 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009819 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009820 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009821 case ISN_SHUFFLE:
9822 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009823 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009824 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009825 case ISN_STORENR:
9826 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009827 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009828 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009829 case ISN_STOREV:
9830 case ISN_STRINDEX:
9831 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009832 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009833 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009834 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009835 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009836 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009837 // nothing allocated
9838 break;
9839 }
9840}
9841
9842/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009843 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009844 */
9845 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009846delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009847{
9848 int idx;
9849
9850 ga_clear(&dfunc->df_def_args_isn);
9851
9852 if (dfunc->df_instr != NULL)
9853 {
9854 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9855 delete_instr(dfunc->df_instr + idx);
9856 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009857 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009858 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009859#ifdef FEAT_PROFILE
9860 if (dfunc->df_instr_prof != NULL)
9861 {
9862 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9863 delete_instr(dfunc->df_instr_prof + idx);
9864 VIM_CLEAR(dfunc->df_instr_prof);
9865 dfunc->df_instr_prof = NULL;
9866 }
9867#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009868
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009869 if (mark_deleted)
9870 dfunc->df_deleted = TRUE;
9871 if (dfunc->df_ufunc != NULL)
9872 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009873}
9874
9875/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009876 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009877 * function, unless another user function still uses it.
9878 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009879 */
9880 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009881unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009883 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884 {
9885 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9886 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009888 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009889 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009890 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009891 ufunc->uf_dfunc_idx = 0;
9892 if (dfunc->df_ufunc == ufunc)
9893 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009894 }
9895}
9896
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009897/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009898 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009899 */
9900 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009901link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009902{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009903 if (ufunc->uf_dfunc_idx > 0)
9904 {
9905 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9906 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009907
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009908 ++dfunc->df_refcount;
9909 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009910}
9911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009912#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009913/*
9914 * Free all functions defined with ":def".
9915 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009916 void
9917free_def_functions(void)
9918{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009919 int idx;
9920
9921 for (idx = 0; idx < def_functions.ga_len; ++idx)
9922 {
9923 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9924
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009925 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009926 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009927 }
9928
9929 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009930}
9931#endif
9932
9933
9934#endif // FEAT_EVAL