blob: 06098d84c509086e54b081485aae5c7b7687e646 [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 Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
342 ufunc_T *ufunc;
343
344 // Find the list of all script variables with the right name.
345 if (len > 0)
346 {
347 cc = name[len];
348 name[len] = NUL;
349 }
350 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
351 if (len > 0)
352 name[len] = cc;
353 if (HASHITEM_EMPTY(hi))
354 return NULL;
355
356 sav = HI2SAV(hi);
357 if (sav->sav_block_id == 0 || cctx == NULL)
358 // variable defined in the script scope or not in a function.
359 return sav;
360
361 // Go over the variables with this name and find one that was visible
362 // from the function.
363 ufunc = cctx->ctx_ufunc;
364 while (sav != NULL)
365 {
366 int idx;
367
368 // Go over the blocks that this function was defined in. If the
369 // variable block ID matches it was visible to the function.
370 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
371 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
372 return sav;
373 sav = sav->sav_next;
374 }
375
376 return NULL;
377}
378
379/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100380 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200381 */
382 static int
383script_is_vim9()
384{
385 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
386}
387
388/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 * Returns OK or FAIL.
392 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200393 static int
394script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200396 if (current_sctx.sc_sid <= 0)
397 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200398 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200399 {
400 // Check script variables that were visible where the function was
401 // defined.
402 if (find_script_var(name, len, cctx) != NULL)
403 return OK;
404 }
405 else
406 {
407 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
408 dictitem_T *di;
409 int cc;
410
411 // Check script variables that are currently visible
412 cc = name[len];
413 name[len] = NUL;
414 di = find_var_in_ht(ht, 0, name, TRUE);
415 name[len] = cc;
416 if (di != NULL)
417 return OK;
418 }
419
420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421}
422
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100423/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100424 * Return TRUE if "name" is a local variable, argument, script variable or
425 * imported.
426 */
427 static int
428variable_exists(char_u *name, size_t len, cctx_T *cctx)
429{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 return (cctx != NULL
431 && (lookup_local(name, len, NULL, cctx) == OK
432 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200433 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100434 || find_imported(name, len, cctx) != NULL;
435}
436
437/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438 * Return TRUE if "name" is a local variable, argument, script variable,
439 * imported or function.
440 */
441 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100442item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100443{
444 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100445 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100446
447 if (variable_exists(name, len, cctx))
448 return TRUE;
449
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100450 // This is similar to what is in lookup_scriptitem():
451 // Find a function, so that a following "->" works.
452 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
453 // a function call.
454 p = skipwhite(name + len);
455
456 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
457 {
458 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200459 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460 // Skip "g:" before a function name.
461 is_global = (name[0] == 'g' && name[1] == ':');
462 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
463 }
464 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100465}
466
467/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100468 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200469 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200470 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100471 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100472 * Return FAIL and give an error if it defined.
473 */
474 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100475check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100476{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200477 int c = p[len];
478 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200479
Bram Moolenaarda479c72021-04-10 21:01:38 +0200480 // underscore argument is OK
481 if (len == 1 && *p == '_')
482 return OK;
483
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200484 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100485 {
486 if (is_arg)
487 semsg(_(e_argument_already_declared_in_script_str), p);
488 else
489 semsg(_(e_variable_already_declared_in_script_str), p);
490 return FAIL;
491 }
492
Bram Moolenaarad486a02020-08-01 23:22:18 +0200493 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100494 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100495 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200496 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100499 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200500 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200501 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
502 && (!func_is_global(ufunc)
503 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200504 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100505 if (is_arg)
506 semsg(_(e_argument_name_shadows_existing_variable_str), p);
507 else
508 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200509 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200510 return FAIL;
511 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100512 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100514 return OK;
515}
516
Bram Moolenaar65b95452020-07-19 14:03:09 +0200517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518/////////////////////////////////////////////////////////////////////
519// Following generate_ functions expect the caller to call ga_grow().
520
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200521#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
522#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524/*
525 * Generate an instruction without arguments.
526 * Returns a pointer to the new instruction, NULL if failed.
527 */
528 static isn_T *
529generate_instr(cctx_T *cctx, isntype_T isn_type)
530{
531 garray_T *instr = &cctx->ctx_instr;
532 isn_T *isn;
533
Bram Moolenaar080457c2020-03-03 21:53:32 +0100534 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 if (ga_grow(instr, 1) == FAIL)
536 return NULL;
537 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
538 isn->isn_type = isn_type;
539 isn->isn_lnum = cctx->ctx_lnum + 1;
540 ++instr->ga_len;
541
542 return isn;
543}
544
545/*
546 * Generate an instruction without arguments.
547 * "drop" will be removed from the stack.
548 * Returns a pointer to the new instruction, NULL if failed.
549 */
550 static isn_T *
551generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
552{
553 garray_T *stack = &cctx->ctx_type_stack;
554
Bram Moolenaar080457c2020-03-03 21:53:32 +0100555 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 stack->ga_len -= drop;
557 return generate_instr(cctx, isn_type);
558}
559
560/*
561 * Generate instruction "isn_type" and put "type" on the type stack.
562 */
563 static isn_T *
564generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
565{
566 isn_T *isn;
567 garray_T *stack = &cctx->ctx_type_stack;
568
569 if ((isn = generate_instr(cctx, isn_type)) == NULL)
570 return NULL;
571
572 if (ga_grow(stack, 1) == FAIL)
573 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200574 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 ++stack->ga_len;
576
577 return isn;
578}
579
580/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200581 * Generate an ISN_DEBUG instruction.
582 */
583 static isn_T *
584generate_instr_debug(cctx_T *cctx)
585{
586 isn_T *isn;
587 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
588 + cctx->ctx_ufunc->uf_dfunc_idx;
589
590 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
591 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200592 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
593 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200594 return isn;
595}
596
597/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200599 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200600 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 */
602 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200603may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604{
605 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200606 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100608 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100610 RETURN_OK_IF_SKIP(cctx);
611 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200612 switch ((*type)->tt_type)
613 {
614 // nothing to be done
615 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200617 // conversion possible
618 case VAR_SPECIAL:
619 case VAR_BOOL:
620 case VAR_NUMBER:
621 case VAR_FLOAT:
622 break;
623
624 // conversion possible (with runtime check)
625 case VAR_ANY:
626 case VAR_UNKNOWN:
627 isntype = ISN_2STRING_ANY;
628 break;
629
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200630 // conversion possible when tolerant
631 case VAR_LIST:
632 if (tolerant)
633 {
634 isntype = ISN_2STRING_ANY;
635 break;
636 }
637 // FALLTHROUGH
638
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200639 // conversion not possible
640 case VAR_VOID:
641 case VAR_BLOB:
642 case VAR_FUNC:
643 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200644 case VAR_DICT:
645 case VAR_JOB:
646 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200647 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200648 to_string_error((*type)->tt_type);
649 return FAIL;
650 }
651
652 *type = &t_string;
653 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200655 isn->isn_arg.tostring.offset = offset;
656 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 return OK;
659}
660
661 static int
662check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
663{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200664 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200666 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 {
668 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200669 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200671 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
673 }
674 return OK;
675}
676
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200677 static int
678generate_add_instr(
679 cctx_T *cctx,
680 vartype_T vartype,
681 type_T *type1,
682 type_T *type2)
683{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100684 garray_T *stack = &cctx->ctx_type_stack;
685 isn_T *isn = generate_instr_drop(cctx,
686 vartype == VAR_NUMBER ? ISN_OPNR
687 : vartype == VAR_LIST ? ISN_ADDLIST
688 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200689#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100690 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200691#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100692 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693
694 if (vartype != VAR_LIST && vartype != VAR_BLOB
695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
699 return FAIL;
700
701 if (isn != NULL)
702 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100703
704 // When concatenating two lists with different member types the member type
705 // becomes "any".
706 if (vartype == VAR_LIST
707 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
708 && type1->tt_member != type2->tt_member)
709 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
710
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711 return isn == NULL ? FAIL : OK;
712}
713
714/*
715 * Get the type to use for an instruction for an operation on "type1" and
716 * "type2". If they are matching use a type-specific instruction. Otherwise
717 * fall back to runtime type checking.
718 */
719 static vartype_T
720operator_type(type_T *type1, type_T *type2)
721{
722 if (type1->tt_type == type2->tt_type
723 && (type1->tt_type == VAR_NUMBER
724 || type1->tt_type == VAR_LIST
725#ifdef FEAT_FLOAT
726 || type1->tt_type == VAR_FLOAT
727#endif
728 || type1->tt_type == VAR_BLOB))
729 return type1->tt_type;
730 return VAR_ANY;
731}
732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733/*
734 * Generate an instruction with two arguments. The instruction depends on the
735 * type of the arguments.
736 */
737 static int
738generate_two_op(cctx_T *cctx, char_u *op)
739{
740 garray_T *stack = &cctx->ctx_type_stack;
741 type_T *type1;
742 type_T *type2;
743 vartype_T vartype;
744 isn_T *isn;
745
Bram Moolenaar080457c2020-03-03 21:53:32 +0100746 RETURN_OK_IF_SKIP(cctx);
747
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200748 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
750 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200751 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
753 switch (*op)
754 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200755 case '+':
756 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 break;
759
760 case '-':
761 case '*':
762 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
763 op) == FAIL)
764 return FAIL;
765 if (vartype == VAR_NUMBER)
766 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
767#ifdef FEAT_FLOAT
768 else if (vartype == VAR_FLOAT)
769 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
770#endif
771 else
772 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
773 if (isn != NULL)
774 isn->isn_arg.op.op_type = *op == '*'
775 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
776 break;
777
Bram Moolenaar4c683752020-04-05 21:38:23 +0200778 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200780 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 && type2->tt_type != VAR_NUMBER))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 return FAIL;
785 }
786 isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
788 if (isn != NULL)
789 isn->isn_arg.op.op_type = EXPR_REM;
790 break;
791 }
792
793 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 {
796 type_T *type = &t_any;
797
798#ifdef FEAT_FLOAT
799 // float+number and number+float results in float
800 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
801 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
802 type = &t_float;
803#endif
804 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
805 }
806
807 return OK;
808}
809
810/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 * Get the instruction to use for comparing "type1" with "type2"
812 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200814 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100815get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816{
817 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818
Bram Moolenaar4c683752020-04-05 21:38:23 +0200819 if (type1 == VAR_UNKNOWN)
820 type1 = VAR_ANY;
821 if (type2 == VAR_UNKNOWN)
822 type2 = VAR_ANY;
823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 if (type1 == type2)
825 {
826 switch (type1)
827 {
828 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
829 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
830 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
831 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
832 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
833 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
834 case VAR_LIST: isntype = ISN_COMPARELIST; break;
835 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
836 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 default: isntype = ISN_COMPAREANY; break;
838 }
839 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200840 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100842 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 isntype = ISN_COMPAREANY;
844
Bram Moolenaar657137c2021-01-09 15:45:23 +0100845 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 && (isntype == ISN_COMPAREBOOL
847 || isntype == ISN_COMPARESPECIAL
848 || isntype == ISN_COMPARENR
849 || isntype == ISN_COMPAREFLOAT))
850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200851 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 }
855 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100856 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
858 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100859 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
860 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 && (type1 == VAR_BLOB || type2 == VAR_BLOB
862 || type1 == VAR_LIST || type2 == VAR_LIST))))
863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200864 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200866 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200868 return isntype;
869}
870
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200871 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200873{
874 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
875 return FAIL;
876 return OK;
877}
878
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879/*
880 * Generate an ISN_COMPARE* instruction with a boolean result.
881 */
882 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100883generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884{
885 isntype_T isntype;
886 isn_T *isn;
887 garray_T *stack = &cctx->ctx_type_stack;
888 vartype_T type1;
889 vartype_T type2;
890
891 RETURN_OK_IF_SKIP(cctx);
892
893 // Get the known type of the two items on the stack. If they are matching
894 // use a type-specific instruction. Otherwise fall back to runtime type
895 // checking.
896 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
897 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100898 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200899 if (isntype == ISN_DROP)
900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
902 if ((isn = generate_instr(cctx, isntype)) == NULL)
903 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100904 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 isn->isn_arg.op.op_ic = ic;
906
907 // takes two arguments, puts one bool back
908 if (stack->ga_len >= 2)
909 {
910 --stack->ga_len;
911 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
912 }
913
914 return OK;
915}
916
917/*
918 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200919 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 */
921 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200922generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923{
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926
Bram Moolenaar080457c2020-03-03 21:53:32 +0100927 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
929 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200930 isn->isn_arg.tobool.invert = invert;
931 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200934 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 return OK;
937}
938
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200939/*
940 * Generate an ISN_COND2BOOL instruction.
941 */
942 static int
943generate_COND2BOOL(cctx_T *cctx)
944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
950 return FAIL;
951
952 // type becomes bool
953 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
954
955 return OK;
956}
957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200959generate_TYPECHECK(
960 cctx_T *cctx,
961 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100962 int offset,
963 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964{
965 isn_T *isn;
966 garray_T *stack = &cctx->ctx_type_stack;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
970 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100972 isn->isn_arg.type.ct_off = (int8_T)offset;
973 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975 // type becomes expected
976 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977
978 return OK;
979}
980
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100981 static int
982generate_SETTYPE(
983 cctx_T *cctx,
984 type_T *expected)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
989 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
990 return FAIL;
991 isn->isn_arg.type.ct_type = alloc_type(expected);
992 return OK;
993}
994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200996 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
997 * used. Return FALSE if the types will never match.
998 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100999 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001000use_typecheck(type_T *actual, type_T *expected)
1001{
1002 if (actual->tt_type == VAR_ANY
1003 || actual->tt_type == VAR_UNKNOWN
1004 || (actual->tt_type == VAR_FUNC
1005 && (expected->tt_type == VAR_FUNC
1006 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001007 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1008 && ((actual->tt_member == &t_void)
1009 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001010 return TRUE;
1011 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1012 && actual->tt_type == expected->tt_type)
1013 // This takes care of a nested list or dict.
1014 return use_typecheck(actual->tt_member, expected->tt_member);
1015 return FALSE;
1016}
1017
1018/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001019 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001020 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001021 * - "actual" is a type that can be "expected" type: add a runtime check; or
1022 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001023 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1024 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001025 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001026 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001027need_type(
1028 type_T *actual,
1029 type_T *expected,
1030 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001031 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001032 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001033 int silent,
1034 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001036 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001037
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001038 if (expected == &t_bool && actual != &t_bool
1039 && (actual->tt_flags & TTFLAG_BOOL_OK))
1040 {
1041 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1042 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001043 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001044 return OK;
1045 }
1046
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001047 where.wt_index = arg_idx;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001048 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001049 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001050
1051 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001052 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001053 if ((!actual_is_const || actual == &t_any)
1054 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001055 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001056 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001057 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001058 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001059
1060 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001061 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001062 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001063}
1064
1065/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001066 * Check that the top of the type stack has a type that can be used as a
1067 * condition. Give an error and return FAIL if not.
1068 */
1069 static int
1070bool_on_stack(cctx_T *cctx)
1071{
1072 garray_T *stack = &cctx->ctx_type_stack;
1073 type_T *type;
1074
1075 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1076 if (type == &t_bool)
1077 return OK;
1078
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001079 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001080 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1081 // This requires a runtime type check.
1082 return generate_COND2BOOL(cctx);
1083
Bram Moolenaar351ead02021-01-16 16:07:01 +01001084 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001085}
1086
1087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Generate an ISN_PUSHNR instruction.
1089 */
1090 static int
1091generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1092{
1093 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001094 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.number = number;
1100
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001101 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001102 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001103 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_PUSHBOOL instruction.
1109 */
1110 static int
1111generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.number = number;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHSPEC instruction.
1125 */
1126 static int
1127generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139#ifdef FEAT_FLOAT
1140/*
1141 * Generate an ISN_PUSHF instruction.
1142 */
1143 static int
1144generate_PUSHF(cctx_T *cctx, float_T fnumber)
1145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1150 return FAIL;
1151 isn->isn_arg.fnumber = fnumber;
1152
1153 return OK;
1154}
1155#endif
1156
1157/*
1158 * Generate an ISN_PUSHS instruction.
1159 * Consumes "str".
1160 */
1161 static int
1162generate_PUSHS(cctx_T *cctx, char_u *str)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar508b5612021-01-02 18:17:26 +01001166 if (cctx->ctx_skip == SKIP_YES)
1167 {
1168 vim_free(str);
1169 return OK;
1170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1172 return FAIL;
1173 isn->isn_arg.string = str;
1174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001179 * Generate an ISN_PUSHCHANNEL instruction.
1180 * Consumes "channel".
1181 */
1182 static int
1183generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1184{
1185 isn_T *isn;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001188 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.channel = channel;
1191
1192 return OK;
1193}
1194
1195/*
1196 * Generate an ISN_PUSHJOB instruction.
1197 * Consumes "job".
1198 */
1199 static int
1200generate_PUSHJOB(cctx_T *cctx, job_T *job)
1201{
1202 isn_T *isn;
1203
Bram Moolenaar080457c2020-03-03 21:53:32 +01001204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001205 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001206 return FAIL;
1207 isn->isn_arg.job = job;
1208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 * Generate an ISN_PUSHBLOB instruction.
1214 * Consumes "blob".
1215 */
1216 static int
1217generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1218{
1219 isn_T *isn;
1220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1223 return FAIL;
1224 isn->isn_arg.blob = blob;
1225
1226 return OK;
1227}
1228
1229/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001230 * Generate an ISN_PUSHFUNC instruction with name "name".
1231 * Consumes "name".
1232 */
1233 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001234generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235{
1236 isn_T *isn;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001239 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001240 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001241 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001242
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001247 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001248 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1249 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001250 */
1251 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001252generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001253{
1254 isn_T *isn;
1255 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001256 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1257 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001258 type_T *item_type = &t_any;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001262 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001263 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001264 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001265 emsg(_(e_listreq));
1266 return FAIL;
1267 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001268 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001269 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1270 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001271 isn->isn_arg.getitem.gi_index = index;
1272 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273
1274 // add the item type to the type stack
1275 if (ga_grow(stack, 1) == FAIL)
1276 return FAIL;
1277 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1278 ++stack->ga_len;
1279 return OK;
1280}
1281
1282/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001283 * Generate an ISN_SLICE instruction with "count".
1284 */
1285 static int
1286generate_SLICE(cctx_T *cctx, int count)
1287{
1288 isn_T *isn;
1289
1290 RETURN_OK_IF_SKIP(cctx);
1291 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1292 return FAIL;
1293 isn->isn_arg.number = count;
1294 return OK;
1295}
1296
1297/*
1298 * Generate an ISN_CHECKLEN instruction with "min_len".
1299 */
1300 static int
1301generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306
1307 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.checklen.cl_min_len = min_len;
1310 isn->isn_arg.checklen.cl_more_OK = more_OK;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Generate an ISN_STORE instruction.
1317 */
1318 static int
1319generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
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_drop(cctx, isn_type, 1)) == NULL)
1325 return FAIL;
1326 if (name != NULL)
1327 isn->isn_arg.string = vim_strsave(name);
1328 else
1329 isn->isn_arg.number = idx;
1330
1331 return OK;
1332}
1333
1334/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001335 * Generate an ISN_STOREOUTER instruction.
1336 */
1337 static int
1338generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1339{
1340 isn_T *isn;
1341
1342 RETURN_OK_IF_SKIP(cctx);
1343 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.outer.outer_idx = idx;
1346 isn->isn_arg.outer.outer_depth = level;
1347
1348 return OK;
1349}
1350
1351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1353 */
1354 static int
1355generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1356{
1357 isn_T *isn;
1358
Bram Moolenaar080457c2020-03-03 21:53:32 +01001359 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1361 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001362 isn->isn_arg.storenr.stnr_idx = idx;
1363 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_STOREOPT instruction
1370 */
1371 static int
1372generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1373{
1374 isn_T *isn;
1375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001377 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 return FAIL;
1379 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1380 isn->isn_arg.storeopt.so_flags = opt_flags;
1381
1382 return OK;
1383}
1384
1385/*
1386 * Generate an ISN_LOAD or similar instruction.
1387 */
1388 static int
1389generate_LOAD(
1390 cctx_T *cctx,
1391 isntype_T isn_type,
1392 int idx,
1393 char_u *name,
1394 type_T *type)
1395{
1396 isn_T *isn;
1397
Bram Moolenaar080457c2020-03-03 21:53:32 +01001398 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1400 return FAIL;
1401 if (name != NULL)
1402 isn->isn_arg.string = vim_strsave(name);
1403 else
1404 isn->isn_arg.number = idx;
1405
1406 return OK;
1407}
1408
1409/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001410 * Generate an ISN_LOADOUTER instruction
1411 */
1412 static int
1413generate_LOADOUTER(
1414 cctx_T *cctx,
1415 int idx,
1416 int nesting,
1417 type_T *type)
1418{
1419 isn_T *isn;
1420
1421 RETURN_OK_IF_SKIP(cctx);
1422 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1423 return FAIL;
1424 isn->isn_arg.outer.outer_idx = idx;
1425 isn->isn_arg.outer.outer_depth = nesting;
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001431 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001432 */
1433 static int
1434generate_LOADV(
1435 cctx_T *cctx,
1436 char_u *name,
1437 int error)
1438{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001439 int di_flags;
1440 int vidx = find_vim_var(name, &di_flags);
1441 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444 if (vidx < 0)
1445 {
1446 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001447 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 return FAIL;
1449 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001450 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001452 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453}
1454
1455/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001456 * Generate an ISN_UNLET instruction.
1457 */
1458 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001459generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001460{
1461 isn_T *isn;
1462
1463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001464 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 return FAIL;
1466 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1467 isn->isn_arg.unlet.ul_forceit = forceit;
1468
1469 return OK;
1470}
1471
1472/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001473 * Generate an ISN_LOCKCONST instruction.
1474 */
1475 static int
1476generate_LOCKCONST(cctx_T *cctx)
1477{
1478 isn_T *isn;
1479
1480 RETURN_OK_IF_SKIP(cctx);
1481 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1482 return FAIL;
1483 return OK;
1484}
1485
1486/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 * Generate an ISN_LOADS instruction.
1488 */
1489 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001490generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001494 int sid,
1495 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498
Bram Moolenaar080457c2020-03-03 21:53:32 +01001499 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001500 if (isn_type == ISN_LOADS)
1501 isn = generate_instr_type(cctx, isn_type, type);
1502 else
1503 isn = generate_instr_drop(cctx, isn_type, 1);
1504 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1507 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
1509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1514 */
1515 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 cctx_T *cctx,
1518 isntype_T isn_type,
1519 int sid,
1520 int idx,
1521 type_T *type)
1522{
1523 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001524 scriptref_T *sref;
1525 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
Bram Moolenaar080457c2020-03-03 21:53:32 +01001527 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (isn_type == ISN_LOADSCRIPT)
1529 isn = generate_instr_type(cctx, isn_type, type);
1530 else
1531 isn = generate_instr_drop(cctx, isn_type, 1);
1532 if (isn == NULL)
1533 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001534
1535 // This requires three arguments, which doesn't fit in an instruction, thus
1536 // we need to allocate a struct for this.
1537 sref = ALLOC_ONE(scriptref_T);
1538 if (sref == NULL)
1539 return FAIL;
1540 isn->isn_arg.script.scriptref = sref;
1541 sref->sref_sid = sid;
1542 sref->sref_idx = idx;
1543 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001544 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_NEWLIST instruction.
1550 */
1551 static int
1552generate_NEWLIST(cctx_T *cctx, int count)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 type_T *type;
1557 type_T *member;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.number = count;
1563
Bram Moolenaar127542b2020-08-09 17:22:04 +02001564 // get the member type from all the items on the stack.
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, 1,
1570 cctx->ctx_type_list);
1571 type = get_list_type(member, cctx->ctx_type_list);
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 // drop the value types
1574 stack->ga_len -= count;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // add the list 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_NEWDICT instruction.
1587 */
1588 static int
1589generate_NEWDICT(cctx_T *cctx, int count)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 type_T *type;
1594 type_T *member;
1595
Bram Moolenaar080457c2020-03-03 21:53:32 +01001596 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1598 return FAIL;
1599 isn->isn_arg.number = count;
1600
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001601 if (count == 0)
1602 member = &t_void;
1603 else
1604 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001605 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1606 cctx->ctx_type_list);
1607 type = get_dict_type(member, cctx->ctx_type_list);
1608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 // drop the key and value types
1610 stack->ga_len -= 2 * count;
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 // add the dict type to the type stack
1613 if (ga_grow(stack, 1) == FAIL)
1614 return FAIL;
1615 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1616 ++stack->ga_len;
1617
1618 return OK;
1619}
1620
1621/*
1622 * Generate an ISN_FUNCREF instruction.
1623 */
1624 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001625generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626{
1627 isn_T *isn;
1628 garray_T *stack = &cctx->ctx_type_stack;
1629
Bram Moolenaar080457c2020-03-03 21:53:32 +01001630 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1632 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001633 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001634 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001636 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001637 // the nested context, including this one.
1638 if (ufunc->uf_flags & FC_CLOSURE)
1639 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if (ga_grow(stack, 1) == FAIL)
1642 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001643 ((type_T **)stack->ga_data)[stack->ga_len] =
1644 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 ++stack->ga_len;
1646
1647 return OK;
1648}
1649
1650/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001651 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001652 * "lambda_name" and "func_name" must be in allocated memory and will be
1653 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001654 */
1655 static int
1656generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1657{
1658 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001659
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001660 if (cctx->ctx_skip == SKIP_YES)
1661 {
1662 vim_free(lambda_name);
1663 vim_free(func_name);
1664 return OK;
1665 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001666 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001667 {
1668 vim_free(lambda_name);
1669 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001670 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001671 }
1672 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001673 isn->isn_arg.newfunc.nf_global = func_name;
1674
1675 return OK;
1676}
1677
1678/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001679 * Generate an ISN_DEF instruction: list functions
1680 */
1681 static int
1682generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1683{
1684 isn_T *isn;
1685
1686 RETURN_OK_IF_SKIP(cctx);
1687 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1688 return FAIL;
1689 if (len > 0)
1690 {
1691 isn->isn_arg.string = vim_strnsave(name, len);
1692 if (isn->isn_arg.string == NULL)
1693 return FAIL;
1694 }
1695 return OK;
1696}
1697
1698/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 * Generate an ISN_JUMP instruction.
1700 */
1701 static int
1702generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1703{
1704 isn_T *isn;
1705 garray_T *stack = &cctx->ctx_type_stack;
1706
Bram Moolenaar080457c2020-03-03 21:53:32 +01001707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.jump.jump_when = when;
1711 isn->isn_arg.jump.jump_where = where;
1712
1713 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1714 --stack->ga_len;
1715
1716 return OK;
1717}
1718
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001719/*
1720 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1721 */
1722 static int
1723generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1724{
1725 isn_T *isn;
1726
1727 RETURN_OK_IF_SKIP(cctx);
1728 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1729 return FAIL;
1730 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1731 // jump_where is set later
1732 return OK;
1733}
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 static int
1736generate_FOR(cctx_T *cctx, int loop_idx)
1737{
1738 isn_T *isn;
1739 garray_T *stack = &cctx->ctx_type_stack;
1740
Bram Moolenaar080457c2020-03-03 21:53:32 +01001741 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1743 return FAIL;
1744 isn->isn_arg.forloop.for_idx = loop_idx;
1745
1746 if (ga_grow(stack, 1) == FAIL)
1747 return FAIL;
1748 // type doesn't matter, will be stored next
1749 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1750 ++stack->ga_len;
1751
1752 return OK;
1753}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001754/*
1755 * Generate an ISN_TRYCONT instruction.
1756 */
1757 static int
1758generate_TRYCONT(cctx_T *cctx, int levels, int where)
1759{
1760 isn_T *isn;
1761
1762 RETURN_OK_IF_SKIP(cctx);
1763 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.trycont.tct_levels = levels;
1766 isn->isn_arg.trycont.tct_where = where;
1767
1768 return OK;
1769}
1770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772/*
1773 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001774 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 * Return FAIL if the number of arguments is wrong.
1776 */
1777 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001778generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779{
1780 isn_T *isn;
1781 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001782 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001783 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001784 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001785 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786
Bram Moolenaar080457c2020-03-03 21:53:32 +01001787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001788 argoff = check_internal_func(func_idx, argcount);
1789 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 return FAIL;
1791
Bram Moolenaar389df252020-07-09 21:20:47 +02001792 if (method_call && argoff > 1)
1793 {
1794 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1795 return FAIL;
1796 isn->isn_arg.shuffle.shfl_item = argcount;
1797 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1798 }
1799
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001800 if (argcount > 0)
1801 {
1802 // Check the types of the arguments.
1803 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001804 if (method_call && argoff > 1)
1805 {
1806 int i;
1807
1808 for (i = 0; i < argcount; ++i)
1809 shuffled_argtypes[i] = (i < argoff - 1)
1810 ? argtypes[i + 1]
1811 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1812 argtypes = shuffled_argtypes;
1813 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001814 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1815 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001816 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001817 if (internal_func_is_map(func_idx))
1818 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001819 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1822 return FAIL;
1823 isn->isn_arg.bfunc.cbf_idx = func_idx;
1824 isn->isn_arg.bfunc.cbf_argcount = argcount;
1825
Bram Moolenaar94738d82020-10-21 14:25:07 +02001826 // Drop the argument types and push the return type.
1827 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 if (ga_grow(stack, 1) == FAIL)
1829 return FAIL;
1830 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001831 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001832 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001834 if (maptype != NULL && maptype->tt_member != NULL
1835 && maptype->tt_member != &t_any)
1836 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001837 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 return OK;
1840}
1841
1842/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001843 * Generate an ISN_LISTAPPEND instruction. Works like add().
1844 * Argument count is already checked.
1845 */
1846 static int
1847generate_LISTAPPEND(cctx_T *cctx)
1848{
1849 garray_T *stack = &cctx->ctx_type_stack;
1850 type_T *list_type;
1851 type_T *item_type;
1852 type_T *expected;
1853
1854 // Caller already checked that list_type is a list.
1855 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1856 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1857 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001858 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001859 return FAIL;
1860
1861 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1862 return FAIL;
1863
1864 --stack->ga_len; // drop the argument
1865 return OK;
1866}
1867
1868/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001869 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1870 * Argument count is already checked.
1871 */
1872 static int
1873generate_BLOBAPPEND(cctx_T *cctx)
1874{
1875 garray_T *stack = &cctx->ctx_type_stack;
1876 type_T *item_type;
1877
1878 // Caller already checked that blob_type is a blob.
1879 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001880 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001881 return FAIL;
1882
1883 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1884 return FAIL;
1885
1886 --stack->ga_len; // drop the argument
1887 return OK;
1888}
1889
1890/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001891 * Return TRUE if "ufunc" should be compiled, taking into account whether
1892 * "profile" indicates profiling is to be done.
1893 */
1894 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001895func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001896{
1897 switch (ufunc->uf_def_status)
1898 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001899 case UF_TO_BE_COMPILED:
1900 return TRUE;
1901
Bram Moolenaarb2049902021-01-24 12:53:53 +01001902 case UF_COMPILED:
1903 {
1904 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1905 + ufunc->uf_dfunc_idx;
1906
Bram Moolenaare99d4222021-06-13 14:01:26 +02001907 switch (compile_type)
1908 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001909 case CT_PROFILE:
1910#ifdef FEAT_PROFILE
1911 return dfunc->df_instr_prof == NULL;
1912#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001913 case CT_NONE:
1914 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001915 case CT_DEBUG:
1916 return dfunc->df_instr_debug == NULL;
1917 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001918 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001919
1920 case UF_NOT_COMPILED:
1921 case UF_COMPILE_ERROR:
1922 case UF_COMPILING:
1923 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001924 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001925 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001926}
1927
1928/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 * Generate an ISN_DCALL or ISN_UCALL instruction.
1930 * Return FAIL if the number of arguments is wrong.
1931 */
1932 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001933generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934{
1935 isn_T *isn;
1936 garray_T *stack = &cctx->ctx_type_stack;
1937 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001938 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939
Bram Moolenaar080457c2020-03-03 21:53:32 +01001940 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 if (argcount > regular_args && !has_varargs(ufunc))
1942 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001943 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 return FAIL;
1945 }
1946 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1947 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001948 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 return FAIL;
1950 }
1951
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001952 if (ufunc->uf_def_status != UF_NOT_COMPILED
1953 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001954 {
1955 int i;
1956
1957 for (i = 0; i < argcount; ++i)
1958 {
1959 type_T *expected;
1960 type_T *actual;
1961
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001962 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1963 if (actual == &t_special
1964 && i >= regular_args - ufunc->uf_def_args.ga_len)
1965 {
1966 // assume v:none used for default argument value
1967 continue;
1968 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001969 if (i < regular_args)
1970 {
1971 if (ufunc->uf_arg_types == NULL)
1972 continue;
1973 expected = ufunc->uf_arg_types[i];
1974 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001975 else if (ufunc->uf_va_type == NULL
1976 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001977 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001978 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001979 else
1980 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001981 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001982 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001983 {
1984 arg_type_mismatch(expected, actual, i + 1);
1985 return FAIL;
1986 }
1987 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001988 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001989 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001990 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001991 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001992 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001993 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1994 {
1995 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1996 ufunc->uf_name);
1997 return FAIL;
1998 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002001 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002002 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002004 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 {
2006 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2007 isn->isn_arg.dfunc.cdf_argcount = argcount;
2008 }
2009 else
2010 {
2011 // A user function may be deleted and redefined later, can't use the
2012 // ufunc pointer, need to look it up again at runtime.
2013 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2014 isn->isn_arg.ufunc.cuf_argcount = argcount;
2015 }
2016
2017 stack->ga_len -= argcount; // drop the arguments
2018 if (ga_grow(stack, 1) == FAIL)
2019 return FAIL;
2020 // add return value
2021 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2022 ++stack->ga_len;
2023
2024 return OK;
2025}
2026
2027/*
2028 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2029 */
2030 static int
2031generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2032{
2033 isn_T *isn;
2034 garray_T *stack = &cctx->ctx_type_stack;
2035
Bram Moolenaar080457c2020-03-03 21:53:32 +01002036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2038 return FAIL;
2039 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2040 isn->isn_arg.ufunc.cuf_argcount = argcount;
2041
2042 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002043 if (ga_grow(stack, 1) == FAIL)
2044 return FAIL;
2045 // add return value
2046 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2047 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048
2049 return OK;
2050}
2051
2052/*
2053 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002054 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 */
2056 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002057generate_PCALL(
2058 cctx_T *cctx,
2059 int argcount,
2060 char_u *name,
2061 type_T *type,
2062 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063{
2064 isn_T *isn;
2065 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002066 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067
Bram Moolenaar080457c2020-03-03 21:53:32 +01002068 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002069
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002070 if (type->tt_type == VAR_ANY)
2071 ret_type = &t_any;
2072 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002073 {
2074 if (type->tt_argcount != -1)
2075 {
2076 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2077
2078 if (argcount < type->tt_min_argcount - varargs)
2079 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002080 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002081 return FAIL;
2082 }
2083 if (!varargs && argcount > type->tt_argcount)
2084 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002085 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002086 return FAIL;
2087 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002088 if (type->tt_args != NULL)
2089 {
2090 int i;
2091
2092 for (i = 0; i < argcount; ++i)
2093 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002094 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002095 type_T *actual = ((type_T **)stack->ga_data)[
2096 stack->ga_len + offset];
2097 type_T *expected;
2098
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002099 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002100 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002101 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002102 else if (i >= type->tt_min_argcount
2103 && actual == &t_special)
2104 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002105 else
2106 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002107 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002108 cctx, TRUE, FALSE) == FAIL)
2109 {
2110 arg_type_mismatch(expected, actual, i + 1);
2111 return FAIL;
2112 }
2113 }
2114 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002115 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002116 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002117 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002118 else
2119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002120 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002121 return FAIL;
2122 }
2123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2125 return FAIL;
2126 isn->isn_arg.pfunc.cpf_top = at_top;
2127 isn->isn_arg.pfunc.cpf_argcount = argcount;
2128
2129 stack->ga_len -= argcount; // drop the arguments
2130
2131 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002132 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002134 // If partial is above the arguments it must be cleared and replaced with
2135 // the return value.
2136 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2137 return FAIL;
2138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 return OK;
2140}
2141
2142/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002143 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 */
2145 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002146generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147{
2148 isn_T *isn;
2149 garray_T *stack = &cctx->ctx_type_stack;
2150 type_T *type;
2151
Bram Moolenaar080457c2020-03-03 21:53:32 +01002152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002153 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002155 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002157 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002159 if (type->tt_type != VAR_DICT && type != &t_any)
2160 {
2161 emsg(_(e_dictreq));
2162 return FAIL;
2163 }
2164 // change dict type to dict member type
2165 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002166 {
2167 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2168 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
2171 return OK;
2172}
2173
2174/*
2175 * Generate an ISN_ECHO instruction.
2176 */
2177 static int
2178generate_ECHO(cctx_T *cctx, int with_white, int count)
2179{
2180 isn_T *isn;
2181
Bram Moolenaar080457c2020-03-03 21:53:32 +01002182 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2184 return FAIL;
2185 isn->isn_arg.echo.echo_with_white = with_white;
2186 isn->isn_arg.echo.echo_count = count;
2187
2188 return OK;
2189}
2190
Bram Moolenaarad39c092020-02-26 18:23:43 +01002191/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002192 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002193 */
2194 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002195generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002196{
2197 isn_T *isn;
2198
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002199 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002200 return FAIL;
2201 isn->isn_arg.number = count;
2202
2203 return OK;
2204}
2205
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002206/*
2207 * Generate an ISN_PUT instruction.
2208 */
2209 static int
2210generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2211{
2212 isn_T *isn;
2213
2214 RETURN_OK_IF_SKIP(cctx);
2215 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2216 return FAIL;
2217 isn->isn_arg.put.put_regname = regname;
2218 isn->isn_arg.put.put_lnum = lnum;
2219 return OK;
2220}
2221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 static int
2223generate_EXEC(cctx_T *cctx, char_u *line)
2224{
2225 isn_T *isn;
2226
Bram Moolenaar080457c2020-03-03 21:53:32 +01002227 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2229 return FAIL;
2230 isn->isn_arg.string = vim_strsave(line);
2231 return OK;
2232}
2233
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002234 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002235generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2236{
2237 isn_T *isn;
2238 garray_T *stack = &cctx->ctx_type_stack;
2239
2240 RETURN_OK_IF_SKIP(cctx);
2241 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2242 return FAIL;
2243 isn->isn_arg.string = vim_strsave(line);
2244
2245 if (ga_grow(stack, 1) == FAIL)
2246 return FAIL;
2247 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2248 ++stack->ga_len;
2249
2250 return OK;
2251}
2252
2253 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002254generate_EXECCONCAT(cctx_T *cctx, int count)
2255{
2256 isn_T *isn;
2257
2258 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2259 return FAIL;
2260 isn->isn_arg.number = count;
2261 return OK;
2262}
2263
Bram Moolenaar08597872020-12-10 19:43:40 +01002264/*
2265 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2266 */
2267 static int
2268generate_RANGE(cctx_T *cctx, char_u *range)
2269{
2270 isn_T *isn;
2271 garray_T *stack = &cctx->ctx_type_stack;
2272
2273 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2274 return FAIL;
2275 isn->isn_arg.string = range;
2276
2277 if (ga_grow(stack, 1) == FAIL)
2278 return FAIL;
2279 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2280 ++stack->ga_len;
2281 return OK;
2282}
2283
Bram Moolenaar792f7862020-11-23 08:31:18 +01002284 static int
2285generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2286{
2287 isn_T *isn;
2288
2289 RETURN_OK_IF_SKIP(cctx);
2290 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2291 return FAIL;
2292 isn->isn_arg.unpack.unp_count = var_count;
2293 isn->isn_arg.unpack.unp_semicolon = semicolon;
2294 return OK;
2295}
2296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002298 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002299 */
2300 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002301generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002302{
2303 isn_T *isn;
2304
Bram Moolenaarfa984412021-03-25 22:15:28 +01002305 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002306 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002307 cctx->ctx_has_cmdmod = TRUE;
2308
2309 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002310 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002311 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2312 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2313 return FAIL;
2314 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002315 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002316 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002317 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002318
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002319 return OK;
2320}
2321
2322 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002323generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002324{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002325 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2326 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002327 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002328 return OK;
2329}
2330
Bram Moolenaarfa984412021-03-25 22:15:28 +01002331 static int
2332misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002333{
2334 garray_T *instr = &cctx->ctx_instr;
2335
Bram Moolenaara91a7132021-03-25 21:12:15 +01002336 if (cctx->ctx_has_cmdmod
2337 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2338 == ISN_CMDMOD)
2339 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002340 emsg(_(e_misplaced_command_modifier));
2341 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002342 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002343 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002344}
2345
2346/*
2347 * Get the index of the current instruction.
2348 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2349 */
2350 static int
2351current_instr_idx(cctx_T *cctx)
2352{
2353 garray_T *instr = &cctx->ctx_instr;
2354 int idx = instr->ga_len;
2355
Bram Moolenaare99d4222021-06-13 14:01:26 +02002356 while (idx > 0)
2357 {
2358 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002359 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002360 {
2361 --idx;
2362 continue;
2363 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002364#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002365 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2366 {
2367 --idx;
2368 continue;
2369 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002370#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002371 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2372 {
2373 --idx;
2374 continue;
2375 }
2376 break;
2377 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002378 return idx;
2379}
2380
Bram Moolenaarf002a412021-01-24 13:34:18 +01002381#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002382 static void
2383may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2384{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002385 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002386 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002387}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002388#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002389
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002390/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002392 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002394 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002395reserve_local(
2396 cctx_T *cctx,
2397 char_u *name,
2398 size_t len,
2399 int isConst,
2400 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002403 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002405 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002407 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002408 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 }
2410
2411 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002412 return NULL;
2413 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002414 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002416 // Every local variable uses the next entry on the stack. We could re-use
2417 // the last ones when leaving a scope, but then variables used in a closure
2418 // might get overwritten. To keep things simple do not re-use stack
2419 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002420 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2421 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002422
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002423 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 lvar->lv_const = isConst;
2425 lvar->lv_type = type;
2426
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002427 // Remember the name for debugging.
2428 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2429 return NULL;
2430 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2431 vim_strsave(lvar->lv_name);
2432 ++dfunc->df_var_names.ga_len;
2433
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002434 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435}
2436
2437/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002438 * Remove local variables above "new_top".
2439 */
2440 static void
2441unwind_locals(cctx_T *cctx, int new_top)
2442{
2443 if (cctx->ctx_locals.ga_len > new_top)
2444 {
2445 int idx;
2446 lvar_T *lvar;
2447
2448 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2449 {
2450 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2451 vim_free(lvar->lv_name);
2452 }
2453 }
2454 cctx->ctx_locals.ga_len = new_top;
2455}
2456
2457/*
2458 * Free all local variables.
2459 */
2460 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002461free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002462{
2463 unwind_locals(cctx, 0);
2464 ga_clear(&cctx->ctx_locals);
2465}
2466
2467/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002468 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2469 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2470 * error if the variable was defined with :const.
2471 */
2472 static int
2473check_item_writable(svar_T *sv, int check_writable, char_u *name)
2474{
2475 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2476 || (check_writable == ASSIGN_FINAL
2477 && sv->sv_const == ASSIGN_CONST))
2478 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002479 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002480 return FAIL;
2481 }
2482 return OK;
2483}
2484
2485/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002487 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 * Returns the index in "sn_var_vals" if found.
2489 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002490 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 */
2492 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002493get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494{
2495 hashtab_T *ht;
2496 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002497 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002498 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 int idx;
2500
Bram Moolenaare3d46852020-08-29 13:39:17 +02002501 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002503 if (sid == current_sctx.sc_sid)
2504 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002505 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002506
2507 if (sav == NULL)
2508 return -2;
2509 idx = sav->sav_var_vals_idx;
2510 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002511 if (check_item_writable(sv, check_writable, name) == FAIL)
2512 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002513 return idx;
2514 }
2515
2516 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 ht = &SCRIPT_VARS(sid);
2518 di = find_var_in_ht(ht, 0, name, TRUE);
2519 if (di == NULL)
2520 return -2;
2521
2522 // Now find the svar_T index in sn_var_vals.
2523 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2524 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002525 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 if (sv->sv_tv == &di->di_tv)
2527 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002528 if (check_item_writable(sv, check_writable, name) == FAIL)
2529 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 return idx;
2531 }
2532 }
2533 return -1;
2534}
2535
2536/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002537 * Find "name" in imported items of the current script or in "cctx" if not
2538 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 */
2540 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002541find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 int idx;
2544
Bram Moolenaare3d46852020-08-29 13:39:17 +02002545 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002546 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 if (cctx != NULL)
2548 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2549 {
2550 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2551 + idx;
2552
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002553 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2554 : STRLEN(import->imp_name) == len
2555 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 return import;
2557 }
2558
Bram Moolenaarefa94442020-08-08 22:16:00 +02002559 return find_imported_in_script(name, len, current_sctx.sc_sid);
2560}
2561
2562 imported_T *
2563find_imported_in_script(char_u *name, size_t len, int sid)
2564{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002565 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002566 int idx;
2567
Bram Moolenaare3d46852020-08-29 13:39:17 +02002568 if (!SCRIPT_ID_VALID(sid))
2569 return NULL;
2570 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2572 {
2573 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2574
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002575 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2576 : STRLEN(import->imp_name) == len
2577 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 return import;
2579 }
2580 return NULL;
2581}
2582
2583/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002584 * Free all imported variables.
2585 */
2586 static void
2587free_imported(cctx_T *cctx)
2588{
2589 int idx;
2590
2591 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2592 {
2593 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2594
2595 vim_free(import->imp_name);
2596 }
2597 ga_clear(&cctx->ctx_imports);
2598}
2599
2600/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002601 * Return a pointer to the next line that isn't empty or only contains a
2602 * comment. Skips over white space.
2603 * Returns NULL if there is none.
2604 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002605 char_u *
2606peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002607{
2608 int lnum = cctx->ctx_lnum;
2609
2610 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2611 {
2612 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002613 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002614
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002615 // ignore NULLs inserted for continuation lines
2616 if (line != NULL)
2617 {
2618 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002619 if (vim9_bad_comment(p))
2620 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002621 if (*p != NUL && !vim9_comment_start(p))
2622 return p;
2623 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002624 }
2625 return NULL;
2626}
2627
2628/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002629 * Called when checking for a following operator at "arg". When the rest of
2630 * the line is empty or only a comment, peek the next line. If there is a next
2631 * line return a pointer to it and set "nextp".
2632 * Otherwise skip over white space.
2633 */
2634 static char_u *
2635may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2636{
2637 char_u *p = skipwhite(arg);
2638
2639 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002640 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002641 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002642 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002643 if (*nextp != NULL)
2644 return *nextp;
2645 }
2646 return p;
2647}
2648
2649/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002650 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002651 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002652 * Returns NULL when at the end.
2653 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002654 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002657 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002658
2659 do
2660 {
2661 ++cctx->ctx_lnum;
2662 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002663 {
2664 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002665 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002666 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002667 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002668 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002669 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002670 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002671 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002672 return line;
2673}
2674
2675/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002676 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002677 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002678 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002679 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2680 */
2681 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002682may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002683{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002684 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002685 if (vim9_bad_comment(*arg))
2686 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002687 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002688 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002689 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002690
2691 if (next == NULL)
2692 return FAIL;
2693 *arg = skipwhite(next);
2694 }
2695 return OK;
2696}
2697
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002698/*
2699 * Idem, and give an error when failed.
2700 */
2701 static int
2702may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2703{
2704 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2705 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002706 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002707 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002708 return FAIL;
2709 }
2710 return OK;
2711}
2712
2713
Bram Moolenaara5565e42020-05-09 15:44:01 +02002714// Structure passed between the compile_expr* functions to keep track of
2715// constants that have been parsed but for which no code was produced yet. If
2716// possible expressions on these constants are applied at compile time. If
2717// that is not possible, the code to push the constants needs to be generated
2718// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002719// Using 50 should be more than enough of 5 levels of ().
2720#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002722 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002723 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002724 int pp_is_const; // all generated code was constants, used for a
2725 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002726} ppconst_T;
2727
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002728static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002729static int compile_expr0(char_u **arg, cctx_T *cctx);
2730static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2731
Bram Moolenaara5565e42020-05-09 15:44:01 +02002732/*
2733 * Generate a PUSH instruction for "tv".
2734 * "tv" will be consumed or cleared.
2735 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2736 */
2737 static int
2738generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2739{
2740 if (tv != NULL)
2741 {
2742 switch (tv->v_type)
2743 {
2744 case VAR_UNKNOWN:
2745 break;
2746 case VAR_BOOL:
2747 generate_PUSHBOOL(cctx, tv->vval.v_number);
2748 break;
2749 case VAR_SPECIAL:
2750 generate_PUSHSPEC(cctx, tv->vval.v_number);
2751 break;
2752 case VAR_NUMBER:
2753 generate_PUSHNR(cctx, tv->vval.v_number);
2754 break;
2755#ifdef FEAT_FLOAT
2756 case VAR_FLOAT:
2757 generate_PUSHF(cctx, tv->vval.v_float);
2758 break;
2759#endif
2760 case VAR_BLOB:
2761 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2762 tv->vval.v_blob = NULL;
2763 break;
2764 case VAR_STRING:
2765 generate_PUSHS(cctx, tv->vval.v_string);
2766 tv->vval.v_string = NULL;
2767 break;
2768 default:
2769 iemsg("constant type not supported");
2770 clear_tv(tv);
2771 return FAIL;
2772 }
2773 tv->v_type = VAR_UNKNOWN;
2774 }
2775 return OK;
2776}
2777
2778/*
2779 * Generate code for any ppconst entries.
2780 */
2781 static int
2782generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2783{
2784 int i;
2785 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002786 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002787
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002788 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002789 for (i = 0; i < ppconst->pp_used; ++i)
2790 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2791 ret = FAIL;
2792 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002793 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002794 return ret;
2795}
2796
2797/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002798 * Check that the last item of "ppconst" is a bool.
2799 */
2800 static int
2801check_ppconst_bool(ppconst_T *ppconst)
2802{
2803 if (ppconst->pp_used > 0)
2804 {
2805 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002806 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002807
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002808 return check_typval_type(&t_bool, tv, where);
2809 }
2810 return OK;
2811}
2812
2813/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002814 * Clear ppconst constants. Used when failing.
2815 */
2816 static void
2817clear_ppconst(ppconst_T *ppconst)
2818{
2819 int i;
2820
2821 for (i = 0; i < ppconst->pp_used; ++i)
2822 clear_tv(&ppconst->pp_tv[i]);
2823 ppconst->pp_used = 0;
2824}
2825
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002826/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002827 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002828 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002829 */
2830 static int
2831compile_member(int is_slice, cctx_T *cctx)
2832{
2833 type_T **typep;
2834 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002835 vartype_T vartype;
2836 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002837
Bram Moolenaar261417b2021-05-06 21:04:55 +02002838 // We can index a list, dict and blob. If we don't know the type
2839 // we can use the index value type. If we still don't know use an "ANY"
2840 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002841 typep = ((type_T **)stack->ga_data) + stack->ga_len
2842 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002843 vartype = (*typep)->tt_type;
2844 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002845 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002846 if (*typep == &t_any && idxtype == &t_string)
2847 vartype = VAR_DICT;
2848 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002849 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002850 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002851 return FAIL;
2852 if (is_slice)
2853 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002854 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2855 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856 FALSE, FALSE) == FAIL)
2857 return FAIL;
2858 }
2859 }
2860
Bram Moolenaar261417b2021-05-06 21:04:55 +02002861 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002862 {
2863 if (is_slice)
2864 {
2865 emsg(_(e_cannot_slice_dictionary));
2866 return FAIL;
2867 }
2868 if ((*typep)->tt_type == VAR_DICT)
2869 {
2870 *typep = (*typep)->tt_member;
2871 if (*typep == &t_unknown)
2872 // empty dict was used
2873 *typep = &t_any;
2874 }
2875 else
2876 {
2877 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2878 FALSE, FALSE) == FAIL)
2879 return FAIL;
2880 *typep = &t_any;
2881 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002882 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 return FAIL;
2884 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2885 return FAIL;
2886 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002887 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002888 {
2889 *typep = &t_string;
2890 if ((is_slice
2891 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2892 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2893 return FAIL;
2894 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002895 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002896 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002897 if (is_slice)
2898 {
2899 *typep = &t_blob;
2900 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2901 return FAIL;
2902 }
2903 else
2904 {
2905 *typep = &t_number;
2906 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2907 return FAIL;
2908 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002909 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002910 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002911 {
2912 if (is_slice)
2913 {
2914 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002915 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002916 2) == FAIL)
2917 return FAIL;
2918 }
2919 else
2920 {
2921 if ((*typep)->tt_type == VAR_LIST)
2922 {
2923 *typep = (*typep)->tt_member;
2924 if (*typep == &t_unknown)
2925 // empty list was used
2926 *typep = &t_any;
2927 }
2928 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002929 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2930 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002931 return FAIL;
2932 }
2933 }
2934 else
2935 {
2936 emsg(_(e_string_list_dict_or_blob_required));
2937 return FAIL;
2938 }
2939 return OK;
2940}
2941
2942/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002943 * Generate an instruction to load script-local variable "name", without the
2944 * leading "s:".
2945 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 */
2947 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002948compile_load_scriptvar(
2949 cctx_T *cctx,
2950 char_u *name, // variable NUL terminated
2951 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002952 char_u **end, // end of variable
2953 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002955 scriptitem_T *si;
2956 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 imported_T *import;
2958
Bram Moolenaare3d46852020-08-29 13:39:17 +02002959 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2960 return FAIL;
2961 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002962 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002963 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002965 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002966 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2967 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 if (idx >= 0)
2970 {
2971 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2972
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002973 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 current_sctx.sc_sid, idx, sv->sv_type);
2975 return OK;
2976 }
2977
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002978 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 if (import != NULL)
2980 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002981 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002982 {
2983 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002984 char_u *exp_name;
2985 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002986 ufunc_T *ufunc;
2987 type_T *type;
2988
2989 // Used "import * as Name", need to lookup the member.
2990 if (*p != '.')
2991 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002992 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002993 return FAIL;
2994 }
2995 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002996 if (VIM_ISWHITE(*p))
2997 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002998 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002999 return FAIL;
3000 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003001
Bram Moolenaar1c991142020-07-04 13:15:31 +02003002 // isolate one name
3003 exp_name = p;
3004 while (eval_isnamec(*p))
3005 ++p;
3006 cc = *p;
3007 *p = NUL;
3008
Bram Moolenaaredba7072021-03-13 21:14:18 +01003009 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3010 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003011 *p = cc;
3012 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003013 *end = p;
3014
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003015 if (idx < 0)
3016 {
3017 if (*p == '(' && ufunc != NULL)
3018 {
3019 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3020 return OK;
3021 }
3022 return FAIL;
3023 }
3024
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003025 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3026 import->imp_sid,
3027 idx,
3028 type);
3029 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003030 else if (import->imp_funcname != NULL)
3031 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003032 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003033 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3034 import->imp_sid,
3035 import->imp_var_vals_idx,
3036 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 return OK;
3038 }
3039
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003040 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003041 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 return FAIL;
3043}
3044
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003045 static int
3046generate_funcref(cctx_T *cctx, char_u *name)
3047{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003048 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003049
3050 if (ufunc == NULL)
3051 return FAIL;
3052
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003053 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003054 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3055 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003056 == FAIL)
3057 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003058 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003059}
3060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061/*
3062 * Compile a variable name into a load instruction.
3063 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003064 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 * When "error" is FALSE do not give an error when not found.
3066 */
3067 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003068compile_load(
3069 char_u **arg,
3070 char_u *end_arg,
3071 cctx_T *cctx,
3072 int is_expr,
3073 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074{
3075 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003076 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003077 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003079 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
3081 if (*(*arg + 1) == ':')
3082 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003083 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003084 {
3085 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086
Bram Moolenaarfa596382021-04-07 21:58:16 +02003087 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003088 switch (**arg)
3089 {
3090 case 'g': isn_type = ISN_LOADGDICT; break;
3091 case 'w': isn_type = ISN_LOADWDICT; break;
3092 case 't': isn_type = ISN_LOADTDICT; break;
3093 case 'b': isn_type = ISN_LOADBDICT; break;
3094 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003095 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003096 goto theend;
3097 }
3098 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3099 goto theend;
3100 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 else
3103 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003104 isntype_T isn_type = ISN_DROP;
3105
Bram Moolenaarfa596382021-04-07 21:58:16 +02003106 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3108 if (name == NULL)
3109 return FAIL;
3110
3111 switch (**arg)
3112 {
3113 case 'v': res = generate_LOADV(cctx, name, error);
3114 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003115 case 's': if (is_expr && ASCII_ISUPPER(*name)
3116 && find_func(name, FALSE, cctx) != NULL)
3117 res = generate_funcref(cctx, name);
3118 else
3119 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003120 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003121 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003122 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003123 {
3124 if (is_expr && ASCII_ISUPPER(*name)
3125 && find_func(name, FALSE, cctx) != NULL)
3126 res = generate_funcref(cctx, name);
3127 else
3128 isn_type = ISN_LOADG;
3129 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003130 else
3131 {
3132 isn_type = ISN_LOADAUTO;
3133 vim_free(name);
3134 name = vim_strnsave(*arg, end - *arg);
3135 if (name == NULL)
3136 return FAIL;
3137 }
3138 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003139 case 'w': isn_type = ISN_LOADW; break;
3140 case 't': isn_type = ISN_LOADT; break;
3141 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003142 default: // cannot happen, just in case
3143 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003144 goto theend;
3145 }
3146 if (isn_type != ISN_DROP)
3147 {
3148 // Global, Buffer-local, Window-local and Tabpage-local
3149 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003150 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003151 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
3154 }
3155 else
3156 {
3157 size_t len = end - *arg;
3158 int idx;
3159 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003160 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161
3162 name = vim_strnsave(*arg, end - *arg);
3163 if (name == NULL)
3164 return FAIL;
3165
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003166 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3167 {
3168 script_autoload(name, FALSE);
3169 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3170 }
3171 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3172 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003174 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003175 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 else
3178 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003179 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003180
Bram Moolenaar709664c2020-12-12 14:33:41 +01003181 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003183 type = lvar.lv_type;
3184 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003185 if (lvar.lv_from_outer != 0)
3186 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003187 else
3188 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 }
3190 else
3191 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003192 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003193 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003194 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003195 || find_imported(name, 0, cctx) != NULL)
3196 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003197
Bram Moolenaar0f769812020-09-12 18:32:34 +02003198 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003199 // uppercase letter it can be a user defined function.
3200 // generate_funcref() will fail if the function can't be found.
3201 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003202 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 }
3204 }
3205 if (gen_load)
3206 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003207 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003208 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003209 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003210 cctx->ctx_outer_used = TRUE;
3211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213
3214 *arg = end;
3215
3216theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003217 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003218 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 vim_free(name);
3220 return res;
3221}
3222
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003223 static void
3224clear_instr_ga(garray_T *gap)
3225{
3226 int idx;
3227
3228 for (idx = 0; idx < gap->ga_len; ++idx)
3229 delete_instr(((isn_T *)gap->ga_data) + idx);
3230 ga_clear(gap);
3231}
3232
3233/*
3234 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3235 * Returns FAIL if compilation fails.
3236 */
3237 static int
3238compile_string(isn_T *isn, cctx_T *cctx)
3239{
3240 char_u *s = isn->isn_arg.string;
3241 garray_T save_ga = cctx->ctx_instr;
3242 int expr_res;
3243 int trailing_error;
3244 int instr_count;
3245 isn_T *instr = NULL;
3246
Bram Moolenaarcd268012021-07-22 19:11:08 +02003247 // Remove the string type from the stack.
3248 --cctx->ctx_type_stack.ga_len;
3249
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003250 // Temporarily reset the list of instructions so that the jump labels are
3251 // correct.
3252 cctx->ctx_instr.ga_len = 0;
3253 cctx->ctx_instr.ga_maxlen = 0;
3254 cctx->ctx_instr.ga_data = NULL;
3255 expr_res = compile_expr0(&s, cctx);
3256 s = skipwhite(s);
3257 trailing_error = *s != NUL;
3258
Bram Moolenaarff652882021-05-16 15:24:49 +02003259 if (expr_res == FAIL || trailing_error
3260 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003261 {
3262 if (trailing_error)
3263 semsg(_(e_trailing_arg), s);
3264 clear_instr_ga(&cctx->ctx_instr);
3265 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003266 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003267 return FAIL;
3268 }
3269
3270 // Move the generated instructions into the ISN_INSTR instruction, then
3271 // restore the list of instructions.
3272 instr_count = cctx->ctx_instr.ga_len;
3273 instr = cctx->ctx_instr.ga_data;
3274 instr[instr_count].isn_type = ISN_FINISH;
3275
3276 cctx->ctx_instr = save_ga;
3277 vim_free(isn->isn_arg.string);
3278 isn->isn_type = ISN_INSTR;
3279 isn->isn_arg.instr = instr;
3280 return OK;
3281}
3282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283/*
3284 * Compile the argument expressions.
3285 * "arg" points to just after the "(" and is advanced to after the ")"
3286 */
3287 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003288compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003290 char_u *p = *arg;
3291 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003292 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003293 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294
Bram Moolenaare6085c52020-04-12 20:19:16 +02003295 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003297 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3298 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003299 if (*p == ')')
3300 {
3301 *arg = p + 1;
3302 return OK;
3303 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003304 if (must_end)
3305 {
3306 semsg(_(e_missing_comma_before_argument_str), p);
3307 return FAIL;
3308 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003309
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003310 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003311 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 return FAIL;
3313 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003314
Bram Moolenaarff652882021-05-16 15:24:49 +02003315 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 && cctx->ctx_instr.ga_len == instr_count + 1)
3317 {
3318 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3319
3320 // {skip} argument of searchpair() can be compiled if not empty
3321 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3322 compile_string(isn, cctx);
3323 }
3324
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003325 if (*p != ',' && *skipwhite(p) == ',')
3326 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003327 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003328 p = skipwhite(p);
3329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003331 {
3332 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003333 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003334 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003335 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003336 else
3337 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003338 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003339 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003341failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003342 emsg(_(e_missing_close));
3343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344}
3345
3346/*
3347 * Compile a function call: name(arg1, arg2)
3348 * "arg" points to "name", "arg + varlen" to the "(".
3349 * "argcount_init" is 1 for "value->method()"
3350 * Instructions:
3351 * EVAL arg1
3352 * EVAL arg2
3353 * BCALL / DCALL / UCALL
3354 */
3355 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003356compile_call(
3357 char_u **arg,
3358 size_t varlen,
3359 cctx_T *cctx,
3360 ppconst_T *ppconst,
3361 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362{
3363 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003364 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 int argcount = argcount_init;
3366 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003367 char_u fname_buf[FLEN_FIXED + 1];
3368 char_u *tofree = NULL;
3369 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003370 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003371 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003372 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003373 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Bram Moolenaara5565e42020-05-09 15:44:01 +02003375 // we can evaluate "has('name')" at compile time
3376 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3377 {
3378 char_u *s = skipwhite(*arg + varlen + 1);
3379 typval_T argvars[2];
3380
3381 argvars[0].v_type = VAR_UNKNOWN;
3382 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003383 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003384 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003385 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003386 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003387 if (*s == ')' && argvars[0].v_type == VAR_STRING
3388 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003389 {
3390 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3391
3392 *arg = s + 1;
3393 argvars[1].v_type = VAR_UNKNOWN;
3394 tv->v_type = VAR_NUMBER;
3395 tv->vval.v_number = 0;
3396 f_has(argvars, tv);
3397 clear_tv(&argvars[0]);
3398 ++ppconst->pp_used;
3399 return OK;
3400 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003401 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003402 }
3403
3404 if (generate_ppconst(cctx, ppconst) == FAIL)
3405 return FAIL;
3406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 if (varlen >= sizeof(namebuf))
3408 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003409 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 return FAIL;
3411 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003412 vim_strncpy(namebuf, *arg, varlen);
3413 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003415 // We handle the "skip" argument of searchpair() and searchpairpos()
3416 // differently.
3417 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3418 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3419 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3420 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003423 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003424 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425
Bram Moolenaar03290b82020-12-19 16:30:44 +01003426 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003427 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 {
3429 int idx;
3430
3431 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003432 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003434 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003435 if (STRCMP(name, "flatten") == 0)
3436 {
3437 emsg(_(e_cannot_use_flatten_in_vim9_script));
3438 goto theend;
3439 }
3440
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003441 if (STRCMP(name, "add") == 0 && argcount == 2)
3442 {
3443 garray_T *stack = &cctx->ctx_type_stack;
3444 type_T *type = ((type_T **)stack->ga_data)[
3445 stack->ga_len - 2];
3446
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003447 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003448 if (type->tt_type == VAR_LIST)
3449 {
3450 // inline "add(list, item)" so that the type can be checked
3451 res = generate_LISTAPPEND(cctx);
3452 idx = -1;
3453 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003454 else if (type->tt_type == VAR_BLOB)
3455 {
3456 // inline "add(blob, nr)" so that the type can be checked
3457 res = generate_BLOBAPPEND(cctx);
3458 idx = -1;
3459 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003460 }
3461
3462 if (idx >= 0)
3463 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3464 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003465 else
3466 semsg(_(e_unknownfunc), namebuf);
3467 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
3469
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003470 // An argument or local variable can be a function reference, this
3471 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003472 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003473 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003474 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003475 // If we can find the function by name generate the right call.
3476 // Skip global functions here, a local funcref takes precedence.
3477 ufunc = find_func(name, FALSE, cctx);
3478 if (ufunc != NULL && !func_is_global(ufunc))
3479 {
3480 res = generate_CALL(cctx, ufunc, argcount);
3481 goto theend;
3482 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003486 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003487 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003489 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003490 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003491 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003492 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003493 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003494
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003495 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003496 goto theend;
3497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498
Bram Moolenaar0f769812020-09-12 18:32:34 +02003499 // If we can find a global function by name generate the right call.
3500 if (ufunc != NULL)
3501 {
3502 res = generate_CALL(cctx, ufunc, argcount);
3503 goto theend;
3504 }
3505
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003506 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003507 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003508 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003509 res = generate_UCALL(cctx, name, argcount);
3510 else
3511 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003512
3513theend:
3514 vim_free(tofree);
3515 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516}
3517
3518// like NAMESPACE_CHAR but with 'a' and 'l'.
3519#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3520
3521/*
3522 * Find the end of a variable or function name. Unlike find_name_end() this
3523 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003524 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 * Return a pointer to just after the name. Equal to "arg" if there is no
3526 * valid name.
3527 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003528 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003529to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530{
3531 char_u *p;
3532
3533 // Quick check for valid starting character.
3534 if (!eval_isnamec1(*arg))
3535 return arg;
3536
3537 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3538 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3539 // and can be used in slice "[n:]".
3540 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003541 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3543 break;
3544 return p;
3545}
3546
3547/*
3548 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003549 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 */
3551 char_u *
3552to_name_const_end(char_u *arg)
3553{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003554 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 typval_T rettv;
3556
3557 if (p == arg && *arg == '[')
3558 {
3559
3560 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003561 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 p = arg;
3563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 return p;
3565}
3566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567/*
3568 * parse a list: [expr, expr]
3569 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003570 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 */
3572 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003573compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574{
3575 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003576 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003578 int is_const;
3579 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003581 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003583 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003584 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003585 semsg(_(e_list_end), *arg);
3586 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003587 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003588 if (*p == ',')
3589 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003590 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003591 return FAIL;
3592 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003593 if (*p == ']')
3594 {
3595 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003596 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003597 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003598 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003599 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003600 if (!is_const)
3601 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 ++count;
3603 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003604 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003606 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3607 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003608 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003609 return FAIL;
3610 }
3611 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003612 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 p = skipwhite(p);
3614 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003615 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003617 ppconst->pp_is_const = is_all_const;
3618 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619}
3620
3621/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003622 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003623 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003624 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 */
3626 static int
3627compile_lambda(char_u **arg, cctx_T *cctx)
3628{
Bram Moolenaare462f522020-12-27 14:43:30 +01003629 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 typval_T rettv;
3631 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003632 evalarg_T evalarg;
3633
3634 CLEAR_FIELD(evalarg);
3635 evalarg.eval_flags = EVAL_EVALUATE;
3636 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
3638 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003639 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3640 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003641 {
3642 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003643 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003644 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003645
Bram Moolenaar65c44152020-12-24 15:14:01 +01003646 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003648 ++ufunc->uf_refcount;
3649 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
Bram Moolenaara9931532021-06-12 15:58:16 +02003651 // Compile it here to get the return type. The return type is optional,
3652 // when it's missing use t_unknown. This is recognized in
3653 // compile_return().
3654 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3655 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003656 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657
Bram Moolenaar648594e2021-07-11 17:55:01 +02003658#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003659 // When the outer function is compiled for profiling, the lambda may be
3660 // called without profiling. Compile it here in the right context.
3661 if (cctx->ctx_compile_type == CT_PROFILE)
3662 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003663#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003664
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003665 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3666 // points into it. Point to the original line to avoid a dangling pointer.
3667 if (evalarg.eval_tofree_cmdline != NULL)
3668 {
3669 size_t off = *arg - evalarg.eval_tofree_cmdline;
3670
3671 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3672 + off;
3673 }
3674
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003675 clear_evalarg(&evalarg, NULL);
3676
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003677 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003678 {
3679 // The return type will now be known.
3680 set_function_type(ufunc);
3681
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003682 // The function reference count will be 1. When the ISN_FUNCREF
3683 // instruction is deleted the reference count is decremented and the
3684 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003685 return generate_FUNCREF(cctx, ufunc);
3686 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003687
3688 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 return FAIL;
3690}
3691
3692/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003693 * Get a lambda and compile it. Uses Vim9 syntax.
3694 */
3695 int
3696get_lambda_tv_and_compile(
3697 char_u **arg,
3698 typval_T *rettv,
3699 int types_optional,
3700 evalarg_T *evalarg)
3701{
3702 int r;
3703 ufunc_T *ufunc;
3704 int save_sc_version = current_sctx.sc_version;
3705
3706 // Get the funcref in "rettv".
3707 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3708 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3709 current_sctx.sc_version = save_sc_version;
3710 if (r != OK)
3711 return r;
3712
3713 // "rettv" will now be a partial referencing the function.
3714 ufunc = rettv->vval.v_partial->pt_func;
3715
3716 // Compile it here to get the return type. The return type is optional,
3717 // when it's missing use t_unknown. This is recognized in
3718 // compile_return().
3719 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3720 ufunc->uf_ret_type = &t_unknown;
3721 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3722
3723 if (ufunc->uf_def_status == UF_COMPILED)
3724 {
3725 // The return type will now be known.
3726 set_function_type(ufunc);
3727 return OK;
3728 }
3729 clear_tv(rettv);
3730 return FAIL;
3731}
3732
3733/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003734 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003736 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 */
3738 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003739compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740{
3741 garray_T *instr = &cctx->ctx_instr;
3742 int count = 0;
3743 dict_T *d = dict_alloc();
3744 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003745 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003746 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003747 int is_const;
3748 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
3750 if (d == NULL)
3751 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003752 if (generate_ppconst(cctx, ppconst) == FAIL)
3753 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003754 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003756 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757
Bram Moolenaar23c55272020-06-21 16:58:13 +02003758 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003759 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003760 *arg = NULL;
3761 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003762 }
3763
3764 if (**arg == '}')
3765 break;
3766
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003767 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003769 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003771 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003772 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003773 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003776 if (isn->isn_type == ISN_PUSHNR)
3777 {
3778 char buf[NUMBUFLEN];
3779
3780 // Convert to string at compile time.
3781 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3782 isn->isn_type = ISN_PUSHS;
3783 isn->isn_arg.string = vim_strsave((char_u *)buf);
3784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 if (isn->isn_type == ISN_PUSHS)
3786 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003787 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003788 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003789 *arg = skipwhite(*arg);
3790 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003791 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003792 emsg(_(e_missing_matching_bracket_after_dict_key));
3793 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003794 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003795 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003797 else
3798 {
3799 // {"name": value},
3800 // {'name': value},
3801 // {name: value} use "name" as a literal key
3802 key = get_literal_key(arg);
3803 if (key == NULL)
3804 return FAIL;
3805 if (generate_PUSHS(cctx, key) == FAIL)
3806 return FAIL;
3807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808
3809 // Check for duplicate keys, if using string keys.
3810 if (key != NULL)
3811 {
3812 item = dict_find(d, key, -1);
3813 if (item != NULL)
3814 {
3815 semsg(_(e_duplicate_key), key);
3816 goto failret;
3817 }
3818 item = dictitem_alloc(key);
3819 if (item != NULL)
3820 {
3821 item->di_tv.v_type = VAR_UNKNOWN;
3822 item->di_tv.v_lock = 0;
3823 if (dict_add(d, item) == FAIL)
3824 dictitem_free(item);
3825 }
3826 }
3827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 if (**arg != ':')
3829 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003830 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003831 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003832 else
3833 semsg(_(e_missing_dict_colon), *arg);
3834 return FAIL;
3835 }
3836 whitep = *arg + 1;
3837 if (!IS_WHITE_OR_NUL(*whitep))
3838 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003839 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 return FAIL;
3841 }
3842
Bram Moolenaar23c55272020-06-21 16:58:13 +02003843 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003844 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003845 *arg = NULL;
3846 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003847 }
3848
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003849 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003851 if (!is_const)
3852 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 ++count;
3854
Bram Moolenaar2c330432020-04-13 14:41:35 +02003855 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003856 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003857 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003858 *arg = NULL;
3859 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 if (**arg == '}')
3862 break;
3863 if (**arg != ',')
3864 {
3865 semsg(_(e_missing_dict_comma), *arg);
3866 goto failret;
3867 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003868 if (IS_WHITE_OR_NUL(*whitep))
3869 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003870 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003871 return FAIL;
3872 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003873 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003874 if (!IS_WHITE_OR_NUL(*whitep))
3875 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003876 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003877 return FAIL;
3878 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003879 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 }
3881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 *arg = *arg + 1;
3883
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003884 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003885 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003886 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003887 *arg += STRLEN(*arg);
3888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003890 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 return generate_NEWDICT(cctx, count);
3892
3893failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003894 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003895 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003896 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003897 *arg = (char_u *)"";
3898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 dict_unref(d);
3900 return FAIL;
3901}
3902
3903/*
3904 * Compile "&option".
3905 */
3906 static int
3907compile_get_option(char_u **arg, cctx_T *cctx)
3908{
3909 typval_T rettv;
3910 char_u *start = *arg;
3911 int ret;
3912
3913 // parse the option and get the current value to get the type.
3914 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003915 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 if (ret == OK)
3917 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003918 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003919 char_u *name = vim_strnsave(start, *arg - start);
3920 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3921 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922
3923 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3924 vim_free(name);
3925 }
3926 clear_tv(&rettv);
3927
3928 return ret;
3929}
3930
3931/*
3932 * Compile "$VAR".
3933 */
3934 static int
3935compile_get_env(char_u **arg, cctx_T *cctx)
3936{
3937 char_u *start = *arg;
3938 int len;
3939 int ret;
3940 char_u *name;
3941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 ++*arg;
3943 len = get_env_len(arg);
3944 if (len == 0)
3945 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003946 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 return FAIL;
3948 }
3949
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003950 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 name = vim_strnsave(start, len + 1);
3952 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3953 vim_free(name);
3954 return ret;
3955}
3956
3957/*
3958 * Compile "@r".
3959 */
3960 static int
3961compile_get_register(char_u **arg, cctx_T *cctx)
3962{
3963 int ret;
3964
3965 ++*arg;
3966 if (**arg == NUL)
3967 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003968 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 return FAIL;
3970 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003971 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 {
3973 emsg_invreg(**arg);
3974 return FAIL;
3975 }
3976 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3977 ++*arg;
3978 return ret;
3979}
3980
3981/*
3982 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003983 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 */
3985 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003986apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003988 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989
3990 // this works from end to start
3991 while (p > start)
3992 {
3993 --p;
3994 if (*p == '-' || *p == '+')
3995 {
3996 // only '-' has an effect, for '+' we only check the type
3997#ifdef FEAT_FLOAT
3998 if (rettv->v_type == VAR_FLOAT)
3999 {
4000 if (*p == '-')
4001 rettv->vval.v_float = -rettv->vval.v_float;
4002 }
4003 else
4004#endif
4005 {
4006 varnumber_T val;
4007 int error = FALSE;
4008
4009 // tv_get_number_chk() accepts a string, but we don't want that
4010 // here
4011 if (check_not_string(rettv) == FAIL)
4012 return FAIL;
4013 val = tv_get_number_chk(rettv, &error);
4014 clear_tv(rettv);
4015 if (error)
4016 return FAIL;
4017 if (*p == '-')
4018 val = -val;
4019 rettv->v_type = VAR_NUMBER;
4020 rettv->vval.v_number = val;
4021 }
4022 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004023 else if (numeric_only)
4024 {
4025 ++p;
4026 break;
4027 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004028 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 {
4030 int v = tv2bool(rettv);
4031
4032 // '!' is permissive in the type.
4033 clear_tv(rettv);
4034 rettv->v_type = VAR_BOOL;
4035 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4036 }
4037 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004038 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 return OK;
4040}
4041
4042/*
4043 * Recognize v: variables that are constants and set "rettv".
4044 */
4045 static void
4046get_vim_constant(char_u **arg, typval_T *rettv)
4047{
4048 if (STRNCMP(*arg, "v:true", 6) == 0)
4049 {
4050 rettv->v_type = VAR_BOOL;
4051 rettv->vval.v_number = VVAL_TRUE;
4052 *arg += 6;
4053 }
4054 else if (STRNCMP(*arg, "v:false", 7) == 0)
4055 {
4056 rettv->v_type = VAR_BOOL;
4057 rettv->vval.v_number = VVAL_FALSE;
4058 *arg += 7;
4059 }
4060 else if (STRNCMP(*arg, "v:null", 6) == 0)
4061 {
4062 rettv->v_type = VAR_SPECIAL;
4063 rettv->vval.v_number = VVAL_NULL;
4064 *arg += 6;
4065 }
4066 else if (STRNCMP(*arg, "v:none", 6) == 0)
4067 {
4068 rettv->v_type = VAR_SPECIAL;
4069 rettv->vval.v_number = VVAL_NONE;
4070 *arg += 6;
4071 }
4072}
4073
Bram Moolenaar657137c2021-01-09 15:45:23 +01004074 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004075get_compare_type(char_u *p, int *len, int *type_is)
4076{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004077 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004078 int i;
4079
4080 switch (p[0])
4081 {
4082 case '=': if (p[1] == '=')
4083 type = EXPR_EQUAL;
4084 else if (p[1] == '~')
4085 type = EXPR_MATCH;
4086 break;
4087 case '!': if (p[1] == '=')
4088 type = EXPR_NEQUAL;
4089 else if (p[1] == '~')
4090 type = EXPR_NOMATCH;
4091 break;
4092 case '>': if (p[1] != '=')
4093 {
4094 type = EXPR_GREATER;
4095 *len = 1;
4096 }
4097 else
4098 type = EXPR_GEQUAL;
4099 break;
4100 case '<': if (p[1] != '=')
4101 {
4102 type = EXPR_SMALLER;
4103 *len = 1;
4104 }
4105 else
4106 type = EXPR_SEQUAL;
4107 break;
4108 case 'i': if (p[1] == 's')
4109 {
4110 // "is" and "isnot"; but not a prefix of a name
4111 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4112 *len = 5;
4113 i = p[*len];
4114 if (!isalnum(i) && i != '_')
4115 {
4116 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4117 *type_is = TRUE;
4118 }
4119 }
4120 break;
4121 }
4122 return type;
4123}
4124
4125/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004126 * Skip over an expression, ignoring most errors.
4127 */
4128 static void
4129skip_expr_cctx(char_u **arg, cctx_T *cctx)
4130{
4131 evalarg_T evalarg;
4132
4133 CLEAR_FIELD(evalarg);
4134 evalarg.eval_cctx = cctx;
4135 skip_expr(arg, &evalarg);
4136}
4137
4138/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004140 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 */
4142 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004143compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004145 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146
4147 // this works from end to start
4148 while (p > start)
4149 {
4150 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004151 while (VIM_ISWHITE(*p))
4152 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 if (*p == '-' || *p == '+')
4154 {
4155 int negate = *p == '-';
4156 isn_T *isn;
4157
4158 // TODO: check type
4159 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4160 {
4161 --p;
4162 if (*p == '-')
4163 negate = !negate;
4164 }
4165 // only '-' has an effect, for '+' we only check the type
4166 if (negate)
4167 isn = generate_instr(cctx, ISN_NEGATENR);
4168 else
4169 isn = generate_instr(cctx, ISN_CHECKNR);
4170 if (isn == NULL)
4171 return FAIL;
4172 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004173 else if (numeric_only)
4174 {
4175 ++p;
4176 break;
4177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 else
4179 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004180 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004182 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004184 if (p[-1] == '!')
4185 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004188 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 return FAIL;
4190 }
4191 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004192 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 return OK;
4194}
4195
4196/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004197 * Compile "(expression)": recursive!
4198 * Return FAIL/OK.
4199 */
4200 static int
4201compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4202{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004203 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004204 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004205
Bram Moolenaar24156692021-01-14 20:35:49 +01004206 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4207 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004208 if (ppconst->pp_used <= PPSIZE - 10)
4209 {
4210 ret = compile_expr1(arg, cctx, ppconst);
4211 }
4212 else
4213 {
4214 // Not enough space in ppconst, flush constants.
4215 if (generate_ppconst(cctx, ppconst) == FAIL)
4216 return FAIL;
4217 ret = compile_expr0(arg, cctx);
4218 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004219 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4220 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004221 if (**arg == ')')
4222 ++*arg;
4223 else if (ret == OK)
4224 {
4225 emsg(_(e_missing_close));
4226 ret = FAIL;
4227 }
4228 return ret;
4229}
4230
4231/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004233 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 */
4235 static int
4236compile_subscript(
4237 char_u **arg,
4238 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004239 char_u *start_leader,
4240 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004241 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004243 char_u *name_start = *end_leader;
4244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 for (;;)
4246 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004247 char_u *p = skipwhite(*arg);
4248
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004249 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004250 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004251 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004252
4253 // If a following line starts with "->{" or "->X" advance to that
4254 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004255 // Also if a following line starts with ".x".
4256 if (next != NULL &&
4257 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004258 && (next[2] == '{'
4259 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004260 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004261 {
4262 next = next_line_from_context(cctx, TRUE);
4263 if (next == NULL)
4264 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004265 *arg = next;
4266 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004267 }
4268 }
4269
Bram Moolenaarcd268012021-07-22 19:11:08 +02004270 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4271 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004272 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004274 garray_T *stack = &cctx->ctx_type_stack;
4275 type_T *type;
4276 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004278 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004279 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004280 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004283 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4284
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004285 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004286 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004288 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 return FAIL;
4290 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004291 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004293 char_u *pstart = p;
4294
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004295 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004296 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004297 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 // something->method()
4300 // Apply the '!', '-' and '+' first:
4301 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004302 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004305 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004306 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004307 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004308 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004309 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004310 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004311 garray_T *stack = &cctx->ctx_type_stack;
4312 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004313 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004314 int expr_isn_start = cctx->ctx_instr.ga_len;
4315 int expr_isn_end;
4316 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004317
4318 // Funcref call: list->(Refs[2])(arg)
4319 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004320 //
4321 // Fist compile the function expression.
4322 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004323 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004324
4325 // Remember the next instruction index, where the instructions
4326 // for arguments are being written.
4327 expr_isn_end = cctx->ctx_instr.ga_len;
4328
4329 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004330 if (**arg != '(')
4331 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004332 if (*skipwhite(*arg) == '(')
4333 emsg(_(e_nowhitespace));
4334 else
4335 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004336 return FAIL;
4337 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004338 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004339 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004340 return FAIL;
4341
Bram Moolenaar2927c072021-04-05 19:41:21 +02004342 // Move the instructions for the arguments to before the
4343 // instructions of the expression and move the type of the
4344 // expression after the argument types. This is what ISN_PCALL
4345 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004346 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004347 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4348 if (arg_isn_count > 0)
4349 {
4350 int expr_isn_count = expr_isn_end - expr_isn_start;
4351 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4352
4353 if (isn == NULL)
4354 return FAIL;
4355 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4356 + expr_isn_start,
4357 sizeof(isn_T) * expr_isn_count);
4358 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4359 + expr_isn_start,
4360 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4361 sizeof(isn_T) * arg_isn_count);
4362 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4363 + expr_isn_start + arg_isn_count,
4364 isn, sizeof(isn_T) * expr_isn_count);
4365 vim_free(isn);
4366
4367 type = ((type_T **)stack->ga_data)[type_idx_start];
4368 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4369 ((type_T **)stack->ga_data) + type_idx_start + 1,
4370 sizeof(type_T *)
4371 * (stack->ga_len - type_idx_start - 1));
4372 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4373 }
4374
Bram Moolenaar7e368202020-12-25 21:56:57 +01004375 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004376 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 return FAIL;
4378 }
4379 else
4380 {
4381 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004382 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004383 if (!eval_isnamec1(*p))
4384 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004385 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004386 return FAIL;
4387 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004388 if (ASCII_ISALPHA(*p) && p[1] == ':')
4389 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004390 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 ;
4392 if (*p != '(')
4393 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004394 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 return FAIL;
4396 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
4399 }
4400 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004401 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004403 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004404
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004405 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004406 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004407 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004408 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004409 // TODO: more arguments
4410 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004411 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004412 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004413 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004415 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004416 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004417 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004418 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004419 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004420 // missing first index is equal to zero
4421 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004422 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004423 else
4424 {
4425 if (compile_expr0(arg, cctx) == FAIL)
4426 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004427 if (**arg == ':')
4428 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004429 semsg(_(e_white_space_required_before_and_after_str_at_str),
4430 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004431 return FAIL;
4432 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004433 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004434 return FAIL;
4435 *arg = skipwhite(*arg);
4436 }
4437 if (**arg == ':')
4438 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004439 is_slice = TRUE;
4440 ++*arg;
4441 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4442 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004443 semsg(_(e_white_space_required_before_and_after_str_at_str),
4444 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004445 return FAIL;
4446 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004447 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004448 return FAIL;
4449 if (**arg == ']')
4450 // missing second index is equal to end of string
4451 generate_PUSHNR(cctx, -1);
4452 else
4453 {
4454 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004455 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004456 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004457 return FAIL;
4458 *arg = skipwhite(*arg);
4459 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
4462 if (**arg != ']')
4463 {
4464 emsg(_(e_missbrac));
4465 return FAIL;
4466 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004467 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaare42939a2021-04-05 17:11:17 +02004469 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004470 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004472 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004474 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004475 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004476 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004477 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004478
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004479 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004480 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004481 {
4482 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004483 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004484 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004485 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004486 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 while (eval_isnamec(*p))
4488 MB_PTR_ADV(p);
4489 if (p == *arg)
4490 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004491 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 return FAIL;
4493 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004494 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
4496 *arg = p;
4497 }
4498 else
4499 break;
4500 }
4501
4502 // TODO - see handle_subscript():
4503 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4504 // Don't do this when "Func" is already a partial that was bound
4505 // explicitly (pt_auto is FALSE).
4506
4507 return OK;
4508}
4509
4510/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004511 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4512 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004514 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004515 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004516 *
4517 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 */
4519
4520/*
4521 * number number constant
4522 * 0zFFFFFFFF Blob constant
4523 * "string" string constant
4524 * 'string' literal string constant
4525 * &option-name option value
4526 * @r register contents
4527 * identifier variable value
4528 * function() function call
4529 * $VAR environment variable
4530 * (expression) nested expression
4531 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004532 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 *
4534 * Also handle:
4535 * ! in front logical NOT
4536 * - in front unary minus
4537 * + in front unary plus (ignored)
4538 * trailing (arg) funcref/partial call
4539 * trailing [] subscript in String or List
4540 * trailing .name entry in Dictionary
4541 * trailing ->name() method call
4542 */
4543 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004544compile_expr7(
4545 char_u **arg,
4546 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004547 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 char_u *start_leader, *end_leader;
4550 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004551 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004552 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004554 ppconst->pp_is_const = FALSE;
4555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 /*
4557 * Skip '!', '-' and '+' characters. They are handled later.
4558 */
4559 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004560 if (eval_leader(arg, TRUE) == FAIL)
4561 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 end_leader = *arg;
4563
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004564 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 switch (**arg)
4566 {
4567 /*
4568 * Number constant.
4569 */
4570 case '0': // also for blob starting with 0z
4571 case '1':
4572 case '2':
4573 case '3':
4574 case '4':
4575 case '5':
4576 case '6':
4577 case '7':
4578 case '8':
4579 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004580 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004582 // Apply "-" and "+" just before the number now, right to
4583 // left. Matters especially when "->" follows. Stops at
4584 // '!'.
4585 if (apply_leader(rettv, TRUE,
4586 start_leader, &end_leader) == FAIL)
4587 {
4588 clear_tv(rettv);
4589 return FAIL;
4590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 break;
4592
4593 /*
4594 * String constant: "string".
4595 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004596 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 return FAIL;
4598 break;
4599
4600 /*
4601 * Literal string constant: 'str''ing'.
4602 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004603 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 return FAIL;
4605 break;
4606
4607 /*
4608 * Constant Vim variable.
4609 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004610 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 ret = NOTDONE;
4612 break;
4613
4614 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615 * "true" constant
4616 */
4617 case 't': if (STRNCMP(*arg, "true", 4) == 0
4618 && !eval_isnamec((*arg)[4]))
4619 {
4620 *arg += 4;
4621 rettv->v_type = VAR_BOOL;
4622 rettv->vval.v_number = VVAL_TRUE;
4623 }
4624 else
4625 ret = NOTDONE;
4626 break;
4627
4628 /*
4629 * "false" constant
4630 */
4631 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4632 && !eval_isnamec((*arg)[5]))
4633 {
4634 *arg += 5;
4635 rettv->v_type = VAR_BOOL;
4636 rettv->vval.v_number = VVAL_FALSE;
4637 }
4638 else
4639 ret = NOTDONE;
4640 break;
4641
4642 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004643 * "null" constant
4644 */
4645 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004646 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004647 {
4648 *arg += 4;
4649 rettv->v_type = VAR_SPECIAL;
4650 rettv->vval.v_number = VVAL_NULL;
4651 }
4652 else
4653 ret = NOTDONE;
4654 break;
4655
4656 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 * List: [expr, expr]
4658 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004659 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4660 return FAIL;
4661 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 break;
4663
4664 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 * Dictionary: {'key': val, 'key': val}
4666 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004667 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4668 return FAIL;
4669 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 break;
4671
4672 /*
4673 * Option value: &name
4674 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004675 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4676 return FAIL;
4677 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 break;
4679
4680 /*
4681 * Environment variable: $VAR.
4682 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004683 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4684 return FAIL;
4685 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 break;
4687
4688 /*
4689 * Register contents: @r.
4690 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004691 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4692 return FAIL;
4693 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 break;
4695 /*
4696 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004697 * lambda: (arg, arg) => expr
4698 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004700 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4701 ret = compile_lambda(arg, cctx);
4702 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004703 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 break;
4705
4706 default: ret = NOTDONE;
4707 break;
4708 }
4709 if (ret == FAIL)
4710 return FAIL;
4711
Bram Moolenaar1c747212020-05-09 18:28:34 +02004712 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004714 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 clear_tv(rettv);
4716 else
4717 // A constant expression can possibly be handled compile time,
4718 // return the value instead of generating code.
4719 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 }
4721 else if (ret == NOTDONE)
4722 {
4723 char_u *p;
4724 int r;
4725
4726 if (!eval_isnamec1(**arg))
4727 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004728 if (!vim9_bad_comment(*arg))
4729 {
4730 if (ends_excmd(*skipwhite(*arg)))
4731 semsg(_(e_empty_expression_str), *arg);
4732 else
4733 semsg(_(e_name_expected_str), *arg);
4734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 return FAIL;
4736 }
4737
4738 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004739 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004740 if (p - *arg == (size_t)1 && **arg == '_')
4741 {
4742 emsg(_(e_cannot_use_underscore_here));
4743 return FAIL;
4744 }
4745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 {
4748 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004751 {
4752 if (generate_ppconst(cctx, ppconst) == FAIL)
4753 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004754 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 if (r == FAIL)
4757 return FAIL;
4758 }
4759
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004760 // Handle following "[]", ".member", etc.
4761 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004762 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004763 ppconst) == FAIL)
4764 return FAIL;
4765 if (ppconst->pp_used > 0)
4766 {
4767 // apply the '!', '-' and '+' before the constant
4768 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004769 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 return FAIL;
4771 return OK;
4772 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004773 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004775 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776}
4777
4778/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004779 * Give the "white on both sides" error, taking the operator from "p[len]".
4780 */
4781 void
4782error_white_both(char_u *op, int len)
4783{
4784 char_u buf[10];
4785
4786 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004787 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004788}
4789
4790/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004791 * <type>expr7: runtime type check / conversion
4792 */
4793 static int
4794compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4795{
4796 type_T *want_type = NULL;
4797
4798 // Recognize <type>
4799 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4800 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004801 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004802 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4803 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004804 return FAIL;
4805
4806 if (**arg != '>')
4807 {
4808 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004809 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004810 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004811 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004812 return FAIL;
4813 }
4814 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004815 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004816 return FAIL;
4817 }
4818
4819 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4820 return FAIL;
4821
4822 if (want_type != NULL)
4823 {
4824 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004825 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004826 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004827
Bram Moolenaard1103582020-08-14 22:44:25 +02004828 generate_ppconst(cctx, ppconst);
4829 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004830 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004831 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004832 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004833 return FAIL;
4834 }
4835 }
4836
4837 return OK;
4838}
4839
4840/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 * * number multiplication
4842 * / number division
4843 * % number modulo
4844 */
4845 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004846compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847{
4848 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004849 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004850 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004852 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004853 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 return FAIL;
4855
4856 /*
4857 * Repeat computing, until no "*", "/" or "%" is following.
4858 */
4859 for (;;)
4860 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004861 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 if (*op != '*' && *op != '/' && *op != '%')
4863 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864 if (next != NULL)
4865 {
4866 *arg = next_line_from_context(cctx, TRUE);
4867 op = skipwhite(*arg);
4868 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004869
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004870 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004872 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004873 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004875 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004876 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004878 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004879 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004881
4882 if (ppconst->pp_used == ppconst_used + 2
4883 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4884 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004885 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004886 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4887 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4888 varnumber_T res = 0;
4889 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004891 // both are numbers: compute the result
4892 switch (*op)
4893 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004894 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004895 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004896 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004897 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004898 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004899 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004900 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004901 break;
4902 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004903 if (failed)
4904 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004905 tv1->vval.v_number = res;
4906 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004907 }
4908 else
4909 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004910 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004911 generate_two_op(cctx, op);
4912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 }
4914
4915 return OK;
4916}
4917
4918/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004919 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 * - number subtraction
4921 * .. string concatenation
4922 */
4923 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925{
4926 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004927 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930
4931 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 return FAIL;
4934
4935 /*
4936 * Repeat computing, until no "+", "-" or ".." is following.
4937 */
4938 for (;;)
4939 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004940 op = may_peek_next_line(cctx, *arg, &next);
4941 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004943 if (op[0] == op[1] && *op != '.' && next)
4944 // Finding "++" or "--" on the next line is a separate command.
4945 // But ".." is concatenation.
4946 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004948 if (next != NULL)
4949 {
4950 *arg = next_line_from_context(cctx, TRUE);
4951 op = skipwhite(*arg);
4952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004954 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004956 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004957 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 }
4959
Bram Moolenaare0de1712020-12-02 17:36:54 +01004960 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004961 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004963 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004964 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 return FAIL;
4966
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004968 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4970 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4971 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4972 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4975 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004976
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004977 // concat/subtract/add constant numbers
4978 if (*op == '+')
4979 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4980 else if (*op == '-')
4981 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4982 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004983 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004984 // concatenate constant strings
4985 char_u *s1 = tv1->vval.v_string;
4986 char_u *s2 = tv2->vval.v_string;
4987 size_t len1 = STRLEN(s1);
4988
4989 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4990 if (tv1->vval.v_string == NULL)
4991 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004992 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004993 return FAIL;
4994 }
4995 mch_memmove(tv1->vval.v_string, s1, len1);
4996 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004997 vim_free(s1);
4998 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004999 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005000 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 }
5002 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005003 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005005 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005006 if (*op == '.')
5007 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005008 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5009 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005010 return FAIL;
5011 generate_instr_drop(cctx, ISN_CONCAT, 1);
5012 }
5013 else
5014 generate_two_op(cctx, op);
5015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 }
5017
5018 return OK;
5019}
5020
5021/*
5022 * expr5a == expr5b
5023 * expr5a =~ expr5b
5024 * expr5a != expr5b
5025 * expr5a !~ expr5b
5026 * expr5a > expr5b
5027 * expr5a >= expr5b
5028 * expr5a < expr5b
5029 * expr5a <= expr5b
5030 * expr5a is expr5b
5031 * expr5a isnot expr5b
5032 *
5033 * Produces instructions:
5034 * EVAL expr5a Push result of "expr5a"
5035 * EVAL expr5b Push result of "expr5b"
5036 * COMPARE one of the compare instructions
5037 */
5038 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005039compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005041 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005043 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005046 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047
5048 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005049 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 return FAIL;
5051
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005052 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005053 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054
5055 /*
5056 * If there is a comparative operator, use it.
5057 */
5058 if (type != EXPR_UNKNOWN)
5059 {
5060 int ic = FALSE; // Default: do not ignore case
5061
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005062 if (next != NULL)
5063 {
5064 *arg = next_line_from_context(cctx, TRUE);
5065 p = skipwhite(*arg);
5066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 if (type_is && (p[len] == '?' || p[len] == '#'))
5068 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005069 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 return FAIL;
5071 }
5072 // extra question mark appended: ignore case
5073 if (p[len] == '?')
5074 {
5075 ic = TRUE;
5076 ++len;
5077 }
5078 // extra '#' appended: match case (ignored)
5079 else if (p[len] == '#')
5080 ++len;
5081 // nothing appended: match case
5082
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005083 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005085 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005086 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 }
5088
5089 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005090 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005091 return FAIL;
5092
Bram Moolenaara5565e42020-05-09 15:44:01 +02005093 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 return FAIL;
5095
Bram Moolenaara5565e42020-05-09 15:44:01 +02005096 if (ppconst->pp_used == ppconst_used + 2)
5097 {
5098 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5099 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5100 int ret;
5101
5102 // Both sides are a constant, compute the result now.
5103 // First check for a valid combination of types, this is more
5104 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005105 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005106 ret = FAIL;
5107 else
5108 {
5109 ret = typval_compare(tv1, tv2, type, ic);
5110 tv1->v_type = VAR_BOOL;
5111 tv1->vval.v_number = tv1->vval.v_number
5112 ? VVAL_TRUE : VVAL_FALSE;
5113 clear_tv(tv2);
5114 --ppconst->pp_used;
5115 }
5116 return ret;
5117 }
5118
5119 generate_ppconst(cctx, ppconst);
5120 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 }
5122
5123 return OK;
5124}
5125
Bram Moolenaar7f141552020-05-09 17:35:53 +02005126static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128/*
5129 * Compile || or &&.
5130 */
5131 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132compile_and_or(
5133 char_u **arg,
5134 cctx_T *cctx,
5135 char *op,
5136 ppconst_T *ppconst,
5137 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005139 char_u *next;
5140 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 int opchar = *op;
5142
5143 if (p[0] == opchar && p[1] == opchar)
5144 {
5145 garray_T *instr = &cctx->ctx_instr;
5146 garray_T end_ga;
5147
5148 /*
5149 * Repeat until there is no following "||" or "&&"
5150 */
5151 ga_init2(&end_ga, sizeof(int), 10);
5152 while (p[0] == opchar && p[1] == opchar)
5153 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005154 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005155 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005156 int start_ctx_lnum = cctx->ctx_lnum;
5157 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005158 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005159
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005160 if (next != NULL)
5161 {
5162 *arg = next_line_from_context(cctx, TRUE);
5163 p = skipwhite(*arg);
5164 }
5165
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005166 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5167 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005168 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005169 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005170 return FAIL;
5171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005173 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005174 SOURCING_LNUM = start_lnum;
5175 save_lnum = cctx->ctx_lnum;
5176 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005177
5178 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005179 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005180 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005181 // TODO: use ppconst if the value is a constant
5182 generate_ppconst(cctx, ppconst);
5183
5184 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005185 status = bool_on_stack(cctx);
5186 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005187 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005188 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005189 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005190 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 {
5192 ga_clear(&end_ga);
5193 return FAIL;
5194 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5197 ++end_ga.ga_len;
5198 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005199 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
5201 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005202 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005203 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005204 {
5205 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005206 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005207 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005208
Bram Moolenaara5565e42020-05-09 15:44:01 +02005209 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5210 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 {
5212 ga_clear(&end_ga);
5213 return FAIL;
5214 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005215
5216 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005218
5219 if (check_ppconst_bool(ppconst) == FAIL)
5220 {
5221 ga_clear(&end_ga);
5222 return FAIL;
5223 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005224 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005226 // Every part must evaluate to a bool.
5227 if (bool_on_stack(cctx) == FAIL)
5228 {
5229 ga_clear(&end_ga);
5230 return FAIL;
5231 }
5232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 // Fill in the end label in all jumps.
5234 while (end_ga.ga_len > 0)
5235 {
5236 isn_T *isn;
5237
5238 --end_ga.ga_len;
5239 isn = ((isn_T *)instr->ga_data)
5240 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5241 isn->isn_arg.jump.jump_where = instr->ga_len;
5242 }
5243 ga_clear(&end_ga);
5244 }
5245
5246 return OK;
5247}
5248
5249/*
5250 * expr4a && expr4a && expr4a logical AND
5251 *
5252 * Produces instructions:
5253 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005254 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005255 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005257 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 * EVAL expr4c Push result of "expr4c"
5259 * end:
5260 */
5261 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005262compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005264 int ppconst_used = ppconst->pp_used;
5265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 return FAIL;
5269
5270 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005271 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272}
5273
5274/*
5275 * expr3a || expr3b || expr3c logical OR
5276 *
5277 * Produces instructions:
5278 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005279 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005280 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005282 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283 * EVAL expr3c Push result of "expr3c"
5284 * end:
5285 */
5286 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005289 int ppconst_used = ppconst->pp_used;
5290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005292 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 return FAIL;
5294
5295 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005296 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297}
5298
5299/*
5300 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005302 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 * JUMP_IF_FALSE alt jump if false
5304 * EVAL expr1a
5305 * JUMP_ALWAYS end
5306 * alt: EVAL expr1b
5307 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005308 *
5309 * Toplevel expression: expr2 ?? expr1
5310 * Produces instructions:
5311 * EVAL expr2 Push result of "expr2"
5312 * JUMP_AND_KEEP_IF_TRUE end jump if true
5313 * EVAL expr1
5314 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 */
5316 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005317compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318{
5319 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005320 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005321 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
Bram Moolenaar3988f642020-08-27 22:43:03 +02005323 // Ignore all kinds of errors when not producing code.
5324 if (cctx->ctx_skip == SKIP_YES)
5325 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005326 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005327 return OK;
5328 }
5329
Bram Moolenaar61a89812020-05-07 16:58:17 +02005330 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005331 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 return FAIL;
5333
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005334 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 if (*p == '?')
5336 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005337 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 garray_T *instr = &cctx->ctx_instr;
5339 garray_T *stack = &cctx->ctx_type_stack;
5340 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005341 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005343 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005344 int has_const_expr = FALSE;
5345 int const_value = FALSE;
5346 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005348 if (next != NULL)
5349 {
5350 *arg = next_line_from_context(cctx, TRUE);
5351 p = skipwhite(*arg);
5352 }
5353
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005354 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005355 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005356 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005357 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005358 return FAIL;
5359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaara5565e42020-05-09 15:44:01 +02005361 if (ppconst->pp_used == ppconst_used + 1)
5362 {
5363 // the condition is a constant, we know whether the ? or the :
5364 // expression is to be evaluated.
5365 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005366 if (op_falsy)
5367 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5368 else
5369 {
5370 int error = FALSE;
5371
5372 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5373 &error);
5374 if (error)
5375 return FAIL;
5376 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005377 cctx->ctx_skip = save_skip == SKIP_YES ||
5378 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5379
5380 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5381 // "left ?? right" and "left" is truthy: produce "left"
5382 generate_ppconst(cctx, ppconst);
5383 else
5384 {
5385 clear_tv(&ppconst->pp_tv[ppconst_used]);
5386 --ppconst->pp_used;
5387 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005388 }
5389 else
5390 {
5391 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005392 if (op_falsy)
5393 end_idx = instr->ga_len;
5394 generate_JUMP(cctx, op_falsy
5395 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5396 if (op_falsy)
5397 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399
5400 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005401 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005402 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005403 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005404 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
Bram Moolenaara5565e42020-05-09 15:44:01 +02005406 if (!has_const_expr)
5407 {
5408 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005410 if (!op_falsy)
5411 {
5412 // remember the type and drop it
5413 --stack->ga_len;
5414 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005416 end_idx = instr->ga_len;
5417 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005418
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005419 // jump here from JUMP_IF_FALSE
5420 isn = ((isn_T *)instr->ga_data) + alt_idx;
5421 isn->isn_arg.jump.jump_where = instr->ga_len;
5422 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005425 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005427 // Check for the ":".
5428 p = may_peek_next_line(cctx, *arg, &next);
5429 if (*p != ':')
5430 {
5431 emsg(_(e_missing_colon));
5432 return FAIL;
5433 }
5434 if (next != NULL)
5435 {
5436 *arg = next_line_from_context(cctx, TRUE);
5437 p = skipwhite(*arg);
5438 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005439
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005440 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5441 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005442 semsg(_(e_white_space_required_before_and_after_str_at_str),
5443 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005444 return FAIL;
5445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005447 // evaluate the third expression
5448 if (has_const_expr)
5449 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005450 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005451 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005452 return FAIL;
5453 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5454 return FAIL;
5455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456
Bram Moolenaara5565e42020-05-09 15:44:01 +02005457 if (!has_const_expr)
5458 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005459 type_T **typep;
5460
Bram Moolenaara5565e42020-05-09 15:44:01 +02005461 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462
Bram Moolenaara5565e42020-05-09 15:44:01 +02005463 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005464 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5465 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005466
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005468 isn = ((isn_T *)instr->ga_data) + end_idx;
5469 isn->isn_arg.jump.jump_where = instr->ga_len;
5470 }
5471
5472 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 }
5474 return OK;
5475}
5476
5477/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005478 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005479 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5480 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 */
5482 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005483compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484{
5485 ppconst_T ppconst;
5486
5487 CLEAR_FIELD(ppconst);
5488 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5489 {
5490 clear_ppconst(&ppconst);
5491 return FAIL;
5492 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005493 if (is_const != NULL)
5494 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005495 if (generate_ppconst(cctx, &ppconst) == FAIL)
5496 return FAIL;
5497 return OK;
5498}
5499
5500/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005501 * Toplevel expression.
5502 */
5503 static int
5504compile_expr0(char_u **arg, cctx_T *cctx)
5505{
5506 return compile_expr0_ext(arg, cctx, NULL);
5507}
5508
5509/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005510 * Compile "return [expr]".
5511 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512 */
5513 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005514compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515{
5516 char_u *p = arg;
5517 garray_T *stack = &cctx->ctx_type_stack;
5518 type_T *stack_type;
5519
5520 if (*p != NUL && *p != '|' && *p != '\n')
5521 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005522 if (legacy)
5523 {
5524 int save_flags = cmdmod.cmod_flags;
5525
5526 generate_LEGACY_EVAL(cctx, p);
5527 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5528 0, cctx, FALSE, FALSE) == FAIL)
5529 return NULL;
5530 cmdmod.cmod_flags |= CMOD_LEGACY;
5531 (void)skip_expr(&p, NULL);
5532 cmdmod.cmod_flags = save_flags;
5533 }
5534 else
5535 {
5536 // compile return argument into instructions
5537 if (compile_expr0(&p, cctx) == FAIL)
5538 return NULL;
5539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005541 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005542 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005543 // "check_return_type" with uf_ret_type set to &t_unknown is used
5544 // for an inline function without a specified return type. Set the
5545 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005546 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005547 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005548 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5549 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005550 || (!check_return_type
5551 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005552 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005553 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005554 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005555 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005556 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005557 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5558 && stack_type->tt_type != VAR_VOID
5559 && stack_type->tt_type != VAR_UNKNOWN)
5560 {
5561 emsg(_(e_returning_value_in_function_without_return_type));
5562 return NULL;
5563 }
5564 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005565 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005566 return NULL;
5567 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 }
5570 else
5571 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005572 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005573 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005574 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5575 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005577 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 return NULL;
5579 }
5580
5581 // No argument, return zero.
5582 generate_PUSHNR(cctx, 0);
5583 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005584
5585 // Undo any command modifiers.
5586 generate_undo_cmdmods(cctx);
5587
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005588 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 return NULL;
5590
5591 // "return val | endif" is possible
5592 return skipwhite(p);
5593}
5594
5595/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005596 * Get a line from the compilation context, compatible with exarg_T getline().
5597 * Return a pointer to the line in allocated memory.
5598 * Return NULL for end-of-file or some error.
5599 */
5600 static char_u *
5601exarg_getline(
5602 int c UNUSED,
5603 void *cookie,
5604 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005605 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005606{
5607 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005608 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005609
Bram Moolenaar66250c92020-08-20 15:02:42 +02005610 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005611 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005612 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005613 return NULL;
5614 ++cctx->ctx_lnum;
5615 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5616 // Comment lines result in NULL pointers, skip them.
5617 if (p != NULL)
5618 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005619 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005620}
5621
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005622 void
5623fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5624{
5625 eap->getline = exarg_getline;
5626 eap->cookie = cctx;
5627}
5628
Bram Moolenaar04b12692020-05-04 23:24:44 +02005629/*
5630 * Compile a nested :def command.
5631 */
5632 static char_u *
5633compile_nested_function(exarg_T *eap, cctx_T *cctx)
5634{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005635 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005636 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005637 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005638 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005639 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005640 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005641 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005642
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005643 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005644 {
5645 emsg(_(e_cannot_use_bang_with_nested_def));
5646 return NULL;
5647 }
5648
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005649 if (*name_start == '/')
5650 {
5651 name_end = skip_regexp(name_start + 1, '/', TRUE);
5652 if (*name_end == '/')
5653 ++name_end;
5654 eap->nextcmd = check_nextcmd(name_end);
5655 }
5656 if (name_end == name_start || *skipwhite(name_end) != '(')
5657 {
5658 if (!ends_excmd2(name_start, name_end))
5659 {
5660 semsg(_(e_invalid_command_str), eap->cmd);
5661 return NULL;
5662 }
5663
5664 // "def" or "def Name": list functions
5665 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5666 return NULL;
5667 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5668 }
5669
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005670 // Only g:Func() can use a namespace.
5671 if (name_start[1] == ':' && !is_global)
5672 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005673 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005674 return NULL;
5675 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005676 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005677 return NULL;
5678
Bram Moolenaar04b12692020-05-04 23:24:44 +02005679 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005680 fill_exarg_from_cctx(eap, cctx);
5681
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005683 lambda_name = vim_strsave(get_lambda_name());
5684 if (lambda_name == NULL)
5685 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005686 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005687
Bram Moolenaar822ba242020-05-24 23:00:18 +02005688 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005689 {
5690 r = eap->skip ? OK : FAIL;
5691 goto theend;
5692 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005693
5694 // copy over the block scope IDs before compiling
5695 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5696 {
5697 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5698
5699 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5700 if (ufunc->uf_block_ids != NULL)
5701 {
5702 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5703 sizeof(int) * block_depth);
5704 ufunc->uf_block_depth = block_depth;
5705 }
5706 }
5707
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005708 compile_type = COMPILE_TYPE(ufunc);
5709#ifdef FEAT_PROFILE
5710 // If the outer function is profiled, also compile the nested function for
5711 // profiling.
5712 if (cctx->ctx_compile_type == CT_PROFILE)
5713 compile_type = CT_PROFILE;
5714#endif
5715 if (func_needs_compiling(ufunc, compile_type)
5716 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005717 {
5718 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005719 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005720 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005721
Bram Moolenaar648594e2021-07-11 17:55:01 +02005722#ifdef FEAT_PROFILE
5723 // When the outer function is compiled for profiling, the nested function
5724 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005725 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005726 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5727#endif
5728
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005729 if (is_global)
5730 {
5731 char_u *func_name = vim_strnsave(name_start + 2,
5732 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005733
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005734 if (func_name == NULL)
5735 r = FAIL;
5736 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005737 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005738 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005739 lambda_name = NULL;
5740 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005741 }
5742 else
5743 {
5744 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005745 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005746 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005747
Bram Moolenaareef21022020-08-01 22:16:43 +02005748 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005749 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005750 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005751 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005752 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5753 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005754 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005755
5756theend:
5757 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005758 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005759}
5760
5761/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 * Return the length of an assignment operator, or zero if there isn't one.
5763 */
5764 int
5765assignment_len(char_u *p, int *heredoc)
5766{
5767 if (*p == '=')
5768 {
5769 if (p[1] == '<' && p[2] == '<')
5770 {
5771 *heredoc = TRUE;
5772 return 3;
5773 }
5774 return 1;
5775 }
5776 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5777 return 2;
5778 if (STRNCMP(p, "..=", 3) == 0)
5779 return 3;
5780 return 0;
5781}
5782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005784 * Generate the load instruction for "name".
5785 */
5786 static void
5787generate_loadvar(
5788 cctx_T *cctx,
5789 assign_dest_T dest,
5790 char_u *name,
5791 lvar_T *lvar,
5792 type_T *type)
5793{
5794 switch (dest)
5795 {
5796 case dest_option:
5797 // TODO: check the option exists
5798 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5799 break;
5800 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005801 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5802 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5803 else
5804 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005805 break;
5806 case dest_buffer:
5807 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5808 break;
5809 case dest_window:
5810 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5811 break;
5812 case dest_tab:
5813 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5814 break;
5815 case dest_script:
5816 compile_load_scriptvar(cctx,
5817 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5818 break;
5819 case dest_env:
5820 // Include $ in the name here
5821 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5822 break;
5823 case dest_reg:
5824 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5825 break;
5826 case dest_vimvar:
5827 generate_LOADV(cctx, name + 2, TRUE);
5828 break;
5829 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005830 if (lvar->lv_from_outer > 0)
5831 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5832 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005833 else
5834 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5835 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005836 case dest_expr:
5837 // list or dict value should already be on the stack.
5838 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005839 }
5840}
5841
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005842/*
5843 * Skip over "[expr]" or ".member".
5844 * Does not check for any errors.
5845 */
5846 static char_u *
5847skip_index(char_u *start)
5848{
5849 char_u *p = start;
5850
5851 if (*p == '[')
5852 {
5853 p = skipwhite(p + 1);
5854 (void)skip_expr(&p, NULL);
5855 p = skipwhite(p);
5856 if (*p == ']')
5857 return p + 1;
5858 return p;
5859 }
5860 // if (*p == '.')
5861 return to_name_end(p + 1, TRUE);
5862}
5863
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005864 void
5865vim9_declare_error(char_u *name)
5866{
5867 char *scope = "";
5868
5869 switch (*name)
5870 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005871 case 'g': scope = _("global"); break;
5872 case 'b': scope = _("buffer"); break;
5873 case 'w': scope = _("window"); break;
5874 case 't': scope = _("tab"); break;
5875 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005876 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005877 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005878 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005879 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005880 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005881 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005882 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005883 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005884 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005885}
5886
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005887/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005888 * For one assignment figure out the type of destination. Return it in "dest".
5889 * When not recognized "dest" is not set.
5890 * For an option "opt_flags" is set.
5891 * For a v:var "vimvaridx" is set.
5892 * "type" is set to the destination type if known, unchanted otherwise.
5893 * Return FAIL if an error message was given.
5894 */
5895 static int
5896get_var_dest(
5897 char_u *name,
5898 assign_dest_T *dest,
5899 int cmdidx,
5900 int *opt_flags,
5901 int *vimvaridx,
5902 type_T **type,
5903 cctx_T *cctx)
5904{
5905 char_u *p;
5906
5907 if (*name == '&')
5908 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005909 int cc;
5910 long numval;
5911 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005912
5913 *dest = dest_option;
5914 if (cmdidx == CMD_final || cmdidx == CMD_const)
5915 {
5916 emsg(_(e_const_option));
5917 return FAIL;
5918 }
5919 p = name;
5920 p = find_option_end(&p, opt_flags);
5921 if (p == NULL)
5922 {
5923 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005924 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005925 return FAIL;
5926 }
5927 cc = *p;
5928 *p = NUL;
5929 opt_type = get_option_value(skip_option_env_lead(name),
5930 &numval, NULL, *opt_flags);
5931 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005932 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005933 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005934 case gov_unknown:
5935 semsg(_(e_unknown_option), name);
5936 return FAIL;
5937 case gov_string:
5938 case gov_hidden_string:
5939 *type = &t_string;
5940 break;
5941 case gov_bool:
5942 case gov_hidden_bool:
5943 *type = &t_bool;
5944 break;
5945 case gov_number:
5946 case gov_hidden_number:
5947 *type = &t_number;
5948 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005949 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005950 }
5951 else if (*name == '$')
5952 {
5953 *dest = dest_env;
5954 *type = &t_string;
5955 }
5956 else if (*name == '@')
5957 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005958 if (name[1] != '@'
5959 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005960 {
5961 emsg_invreg(name[1]);
5962 return FAIL;
5963 }
5964 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005965 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005966 }
5967 else if (STRNCMP(name, "g:", 2) == 0)
5968 {
5969 *dest = dest_global;
5970 }
5971 else if (STRNCMP(name, "b:", 2) == 0)
5972 {
5973 *dest = dest_buffer;
5974 }
5975 else if (STRNCMP(name, "w:", 2) == 0)
5976 {
5977 *dest = dest_window;
5978 }
5979 else if (STRNCMP(name, "t:", 2) == 0)
5980 {
5981 *dest = dest_tab;
5982 }
5983 else if (STRNCMP(name, "v:", 2) == 0)
5984 {
5985 typval_T *vtv;
5986 int di_flags;
5987
5988 *vimvaridx = find_vim_var(name + 2, &di_flags);
5989 if (*vimvaridx < 0)
5990 {
5991 semsg(_(e_variable_not_found_str), name);
5992 return FAIL;
5993 }
5994 // We use the current value of "sandbox" here, is that OK?
5995 if (var_check_ro(di_flags, name, FALSE))
5996 return FAIL;
5997 *dest = dest_vimvar;
5998 vtv = get_vim_var_tv(*vimvaridx);
5999 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6000 }
6001 return OK;
6002}
6003
6004/*
6005 * Generate a STORE instruction for "dest", not being "dest_local".
6006 * Return FAIL when out of memory.
6007 */
6008 static int
6009generate_store_var(
6010 cctx_T *cctx,
6011 assign_dest_T dest,
6012 int opt_flags,
6013 int vimvaridx,
6014 int scriptvar_idx,
6015 int scriptvar_sid,
6016 type_T *type,
6017 char_u *name)
6018{
6019 switch (dest)
6020 {
6021 case dest_option:
6022 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6023 opt_flags);
6024 case dest_global:
6025 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006026 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6027 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006028 case dest_buffer:
6029 // include b: with the name, easier to execute that way
6030 return generate_STORE(cctx, ISN_STOREB, 0, name);
6031 case dest_window:
6032 // include w: with the name, easier to execute that way
6033 return generate_STORE(cctx, ISN_STOREW, 0, name);
6034 case dest_tab:
6035 // include t: with the name, easier to execute that way
6036 return generate_STORE(cctx, ISN_STORET, 0, name);
6037 case dest_env:
6038 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6039 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006040 return generate_STORE(cctx, ISN_STOREREG,
6041 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006042 case dest_vimvar:
6043 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6044 case dest_script:
6045 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006046 // "s:" may be included in the name.
6047 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006048 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006049 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6050 scriptvar_sid, scriptvar_idx, type);
6051 case dest_local:
6052 case dest_expr:
6053 // cannot happen
6054 break;
6055 }
6056 return FAIL;
6057}
6058
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006060generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6061{
6062 if (lhs->lhs_dest != dest_local)
6063 return generate_store_var(cctx, lhs->lhs_dest,
6064 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6065 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6066 lhs->lhs_type, lhs->lhs_name);
6067
6068 if (lhs->lhs_lvar != NULL)
6069 {
6070 garray_T *instr = &cctx->ctx_instr;
6071 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6072
6073 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6074 // ISN_STORENR
6075 if (lhs->lhs_lvar->lv_from_outer == 0
6076 && instr->ga_len == instr_count + 1
6077 && isn->isn_type == ISN_PUSHNR)
6078 {
6079 varnumber_T val = isn->isn_arg.number;
6080 garray_T *stack = &cctx->ctx_type_stack;
6081
6082 isn->isn_type = ISN_STORENR;
6083 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6084 isn->isn_arg.storenr.stnr_val = val;
6085 if (stack->ga_len > 0)
6086 --stack->ga_len;
6087 }
6088 else if (lhs->lhs_lvar->lv_from_outer > 0)
6089 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6090 lhs->lhs_lvar->lv_from_outer);
6091 else
6092 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6093 }
6094 return OK;
6095}
6096
6097 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006098is_decl_command(int cmdidx)
6099{
6100 return cmdidx == CMD_let || cmdidx == CMD_var
6101 || cmdidx == CMD_final || cmdidx == CMD_const;
6102}
6103
6104/*
6105 * Figure out the LHS type and other properties for an assignment or one item
6106 * of ":unlet" with an index.
6107 * Returns OK or FAIL.
6108 */
6109 static int
6110compile_lhs(
6111 char_u *var_start,
6112 lhs_T *lhs,
6113 int cmdidx,
6114 int heredoc,
6115 int oplen,
6116 cctx_T *cctx)
6117{
6118 char_u *var_end;
6119 int is_decl = is_decl_command(cmdidx);
6120
6121 CLEAR_POINTER(lhs);
6122 lhs->lhs_dest = dest_local;
6123 lhs->lhs_vimvaridx = -1;
6124 lhs->lhs_scriptvar_idx = -1;
6125
6126 // "dest_end" is the end of the destination, including "[expr]" or
6127 // ".name".
6128 // "var_end" is the end of the variable/option/etc. name.
6129 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6130 if (*var_start == '@')
6131 var_end = var_start + 2;
6132 else
6133 {
6134 // skip over the leading "&", "&l:", "&g:" and "$"
6135 var_end = skip_option_env_lead(var_start);
6136 var_end = to_name_end(var_end, TRUE);
6137 }
6138
6139 // "a: type" is declaring variable "a" with a type, not dict "a:".
6140 if (is_decl && lhs->lhs_dest_end == var_start + 2
6141 && lhs->lhs_dest_end[-1] == ':')
6142 --lhs->lhs_dest_end;
6143 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6144 --var_end;
6145
6146 // compute the length of the destination without "[expr]" or ".name"
6147 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006148 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006149 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6150 if (lhs->lhs_name == NULL)
6151 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006152
6153 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6154 // Something follows after the variable: "var[idx]" or "var.key".
6155 lhs->lhs_has_index = TRUE;
6156
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if (heredoc)
6158 lhs->lhs_type = &t_list_string;
6159 else
6160 lhs->lhs_type = &t_any;
6161
6162 if (cctx->ctx_skip != SKIP_YES)
6163 {
6164 int declare_error = FALSE;
6165
6166 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6167 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6168 &lhs->lhs_type, cctx) == FAIL)
6169 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006170 if (lhs->lhs_dest != dest_local
6171 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006172 {
6173 // Specific kind of variable recognized.
6174 declare_error = is_decl;
6175 }
6176 else
6177 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006178 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006179 if (check_reserved_name(lhs->lhs_name) == FAIL)
6180 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006181
6182 if (lookup_local(var_start, lhs->lhs_varlen,
6183 &lhs->lhs_local_lvar, cctx) == OK)
6184 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6185 else
6186 {
6187 CLEAR_FIELD(lhs->lhs_arg_lvar);
6188 if (arg_exists(var_start, lhs->lhs_varlen,
6189 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6190 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6191 {
6192 if (is_decl)
6193 {
6194 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6195 return FAIL;
6196 }
6197 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6198 }
6199 }
6200 if (lhs->lhs_lvar != NULL)
6201 {
6202 if (is_decl)
6203 {
6204 semsg(_(e_variable_already_declared), lhs->lhs_name);
6205 return FAIL;
6206 }
6207 }
6208 else
6209 {
6210 int script_namespace = lhs->lhs_varlen > 1
6211 && STRNCMP(var_start, "s:", 2) == 0;
6212 int script_var = (script_namespace
6213 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006214 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006215 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006216 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 imported_T *import =
6218 find_imported(var_start, lhs->lhs_varlen, cctx);
6219
6220 if (script_namespace || script_var || import != NULL)
6221 {
6222 char_u *rawname = lhs->lhs_name
6223 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6224
6225 if (is_decl)
6226 {
6227 if (script_namespace)
6228 semsg(_(e_cannot_declare_script_variable_in_function),
6229 lhs->lhs_name);
6230 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006231 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006232 lhs->lhs_name);
6233 return FAIL;
6234 }
6235 else if (cctx->ctx_ufunc->uf_script_ctx_version
6236 == SCRIPT_VERSION_VIM9
6237 && script_namespace
6238 && !script_var && import == NULL)
6239 {
6240 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6241 return FAIL;
6242 }
6243
6244 lhs->lhs_dest = dest_script;
6245
6246 // existing script-local variables should have a type
6247 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6248 if (import != NULL)
6249 lhs->lhs_scriptvar_sid = import->imp_sid;
6250 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6251 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006252 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006253 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006254 lhs->lhs_scriptvar_sid, rawname,
6255 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6256 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257 if (lhs->lhs_scriptvar_idx >= 0)
6258 {
6259 scriptitem_T *si = SCRIPT_ITEM(
6260 lhs->lhs_scriptvar_sid);
6261 svar_T *sv =
6262 ((svar_T *)si->sn_var_vals.ga_data)
6263 + lhs->lhs_scriptvar_idx;
6264 lhs->lhs_type = sv->sv_type;
6265 }
6266 }
6267 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006268 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269 == FAIL)
6270 return FAIL;
6271 }
6272 }
6273
6274 if (declare_error)
6275 {
6276 vim9_declare_error(lhs->lhs_name);
6277 return FAIL;
6278 }
6279 }
6280
6281 // handle "a:name" as a name, not index "name" on "a"
6282 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6283 var_end = lhs->lhs_dest_end;
6284
6285 if (lhs->lhs_dest != dest_option)
6286 {
6287 if (is_decl && *var_end == ':')
6288 {
6289 char_u *p;
6290
6291 // parse optional type: "let var: type = expr"
6292 if (!VIM_ISWHITE(var_end[1]))
6293 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006294 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006295 return FAIL;
6296 }
6297 p = skipwhite(var_end + 1);
6298 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6299 if (lhs->lhs_type == NULL)
6300 return FAIL;
6301 lhs->lhs_has_type = TRUE;
6302 }
6303 else if (lhs->lhs_lvar != NULL)
6304 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6305 }
6306
Bram Moolenaare42939a2021-04-05 17:11:17 +02006307 if (oplen == 3 && !heredoc
6308 && lhs->lhs_dest != dest_global
6309 && !lhs->lhs_has_index
6310 && lhs->lhs_type->tt_type != VAR_STRING
6311 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006312 {
6313 emsg(_(e_can_only_concatenate_to_string));
6314 return FAIL;
6315 }
6316
6317 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6318 && cctx->ctx_skip != SKIP_YES)
6319 {
6320 if (oplen > 1 && !heredoc)
6321 {
6322 // +=, /=, etc. require an existing variable
6323 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6324 return FAIL;
6325 }
6326 if (!is_decl)
6327 {
6328 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6329 return FAIL;
6330 }
6331
Bram Moolenaar3f327882021-03-17 20:56:38 +01006332 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 if ((lhs->lhs_type->tt_type == VAR_FUNC
6334 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006335 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006337
6338 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6340 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6341 if (lhs->lhs_lvar == NULL)
6342 return FAIL;
6343 lhs->lhs_new_local = TRUE;
6344 }
6345
6346 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006347 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006348 {
6349 // Something follows after the variable: "var[idx]" or "var.key".
6350 // TODO: should we also handle "->func()" here?
6351 if (is_decl)
6352 {
6353 emsg(_(e_cannot_use_index_when_declaring_variable));
6354 return FAIL;
6355 }
6356
6357 if (var_start[lhs->lhs_varlen] == '['
6358 || var_start[lhs->lhs_varlen] == '.')
6359 {
6360 char_u *after = var_start + lhs->lhs_varlen;
6361 char_u *p;
6362
6363 // Only the last index is used below, if there are others
6364 // before it generate code for the expression. Thus for
6365 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6366 for (;;)
6367 {
6368 p = skip_index(after);
6369 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006370 {
6371 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006373 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006374 after = p;
6375 }
6376 if (after > var_start + lhs->lhs_varlen)
6377 {
6378 lhs->lhs_varlen = after - var_start;
6379 lhs->lhs_dest = dest_expr;
6380 // We don't know the type before evaluating the expression,
6381 // use "any" until then.
6382 lhs->lhs_type = &t_any;
6383 }
6384
Bram Moolenaar752fc692021-01-04 21:57:11 +01006385 if (lhs->lhs_type->tt_member == NULL)
6386 lhs->lhs_member_type = &t_any;
6387 else
6388 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6389 }
6390 else
6391 {
6392 semsg("Not supported yet: %s", var_start);
6393 return FAIL;
6394 }
6395 }
6396 return OK;
6397}
6398
6399/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006400 * Figure out the LHS and check a few errors.
6401 */
6402 static int
6403compile_assign_lhs(
6404 char_u *var_start,
6405 lhs_T *lhs,
6406 int cmdidx,
6407 int is_decl,
6408 int heredoc,
6409 int oplen,
6410 cctx_T *cctx)
6411{
6412 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6413 return FAIL;
6414
6415 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6416 {
6417 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6418 return FAIL;
6419 }
6420 if (!is_decl && lhs->lhs_lvar != NULL
6421 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6422 {
6423 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6424 return FAIL;
6425 }
6426 return OK;
6427}
6428
6429/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006430 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6431 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006432 */
6433 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006434compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 char_u *var_start,
6436 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006437 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006438 cctx_T *cctx)
6439{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006441 char_u *p;
6442 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006443 int need_white_before = TRUE;
6444 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006445
Bram Moolenaar752fc692021-01-04 21:57:11 +01006446 p = var_start + varlen;
6447 if (*p == '[')
6448 {
6449 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006450 if (*p == ':')
6451 {
6452 // empty first index, push zero
6453 r = generate_PUSHNR(cctx, 0);
6454 need_white_before = FALSE;
6455 }
6456 else
6457 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006458
6459 if (r == OK && *skipwhite(p) == ':')
6460 {
6461 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006462 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006463 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006464 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006465 empty_second = *skipwhite(p + 1) == ']';
6466 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6467 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006468 {
6469 semsg(_(e_white_space_required_before_and_after_str_at_str),
6470 ":", p);
6471 return FAIL;
6472 }
6473 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006474 if (*p == ']')
6475 // empty second index, push "none"
6476 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6477 else
6478 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006479 }
6480
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 if (r == OK && *skipwhite(p) != ']')
6482 {
6483 // this should not happen
6484 emsg(_(e_missbrac));
6485 r = FAIL;
6486 }
6487 }
6488 else // if (*p == '.')
6489 {
6490 char_u *key_end = to_name_end(p + 1, TRUE);
6491 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6492
6493 r = generate_PUSHS(cctx, key);
6494 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006495 return r;
6496}
6497
6498/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006499 * For a LHS with an index, load the variable to be indexed.
6500 */
6501 static int
6502compile_load_lhs(
6503 lhs_T *lhs,
6504 char_u *var_start,
6505 type_T *rhs_type,
6506 cctx_T *cctx)
6507{
6508 if (lhs->lhs_dest == dest_expr)
6509 {
6510 size_t varlen = lhs->lhs_varlen;
6511 int c = var_start[varlen];
6512 char_u *p = var_start;
6513 garray_T *stack = &cctx->ctx_type_stack;
6514
6515 // Evaluate "ll[expr]" of "ll[expr][idx]"
6516 var_start[varlen] = NUL;
6517 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6518 {
6519 // this should not happen
6520 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006521 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006522 return FAIL;
6523 }
6524 var_start[varlen] = c;
6525
6526 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6527 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6528 // now we can properly check the type
6529 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6530 && rhs_type != &t_void
6531 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6532 FALSE, FALSE) == FAIL)
6533 return FAIL;
6534 }
6535 else
6536 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6537 lhs->lhs_lvar, lhs->lhs_type);
6538 return OK;
6539}
6540
6541/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006542 * Produce code for loading "lhs" and also take care of an index.
6543 * Return OK/FAIL.
6544 */
6545 static int
6546compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6547{
6548 compile_load_lhs(lhs, var_start, NULL, cctx);
6549
6550 if (lhs->lhs_has_index)
6551 {
6552 int range = FALSE;
6553
6554 // Get member from list or dict. First compile the
6555 // index value.
6556 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6557 return FAIL;
6558 if (range)
6559 {
6560 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6561 var_start);
6562 return FAIL;
6563 }
6564
6565 // Get the member.
6566 if (compile_member(FALSE, cctx) == FAIL)
6567 return FAIL;
6568 }
6569 return OK;
6570}
6571
6572/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006573 * Assignment to a list or dict member, or ":unlet" for the item, using the
6574 * information in "lhs".
6575 * Returns OK or FAIL.
6576 */
6577 static int
6578compile_assign_unlet(
6579 char_u *var_start,
6580 lhs_T *lhs,
6581 int is_assign,
6582 type_T *rhs_type,
6583 cctx_T *cctx)
6584{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006585 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006586 garray_T *stack = &cctx->ctx_type_stack;
6587 int range = FALSE;
6588
Bram Moolenaar68452172021-04-12 21:21:02 +02006589 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006591 if (is_assign && range && lhs->lhs_type != &t_blob
6592 && lhs->lhs_type != &t_any)
6593 {
6594 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6595 return FAIL;
6596 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006597
6598 if (lhs->lhs_type == &t_any)
6599 {
6600 // Index on variable of unknown type: check at runtime.
6601 dest_type = VAR_ANY;
6602 }
6603 else
6604 {
6605 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006606 if (dest_type == VAR_DICT && range)
6607 {
6608 emsg(e_cannot_use_range_with_dictionary);
6609 return FAIL;
6610 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006611 if (dest_type == VAR_DICT
6612 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006614 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006615 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006616 type_T *type;
6617
6618 if (range)
6619 {
6620 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6621 if (need_type(type, &t_number,
6622 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006623 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006624 }
6625 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006626 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006627 && need_type(type, &t_number,
6628 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006629 return FAIL;
6630 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006631 }
6632
6633 // Load the dict or list. On the stack we then have:
6634 // - value (for assignment, not for :unlet)
6635 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006636 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006637 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006638 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6639 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640
Bram Moolenaar68452172021-04-12 21:21:02 +02006641 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6642 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006643 {
6644 if (is_assign)
6645 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006646 if (range)
6647 {
6648 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6649 return FAIL;
6650 }
6651 else
6652 {
6653 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654
Bram Moolenaar68452172021-04-12 21:21:02 +02006655 if (isn == NULL)
6656 return FAIL;
6657 isn->isn_arg.vartype = dest_type;
6658 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006660 else if (range)
6661 {
6662 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6663 return FAIL;
6664 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665 else
6666 {
6667 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6668 return FAIL;
6669 }
6670 }
6671 else
6672 {
6673 emsg(_(e_indexable_type_required));
6674 return FAIL;
6675 }
6676
6677 return OK;
6678}
6679
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006680/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006681 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006682 * "let name"
6683 * "var name = expr"
6684 * "final name = expr"
6685 * "const name = expr"
6686 * "name = expr"
6687 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006688 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006689 * Return NULL for an error.
6690 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 */
6692 static char_u *
6693compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6694{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006695 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006697 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 char_u *ret = NULL;
6699 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006700 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006702 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006704 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 int oplen = 0;
6707 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006708 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006709 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006711 int is_decl = is_decl_command(cmdidx);
6712 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006713 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006715 // Skip over the "var" or "[var, var]" to get to any "=".
6716 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6717 if (p == NULL)
6718 return *arg == '[' ? arg : NULL;
6719
6720 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006722 // TODO: should we allow this, and figure out type inference from list
6723 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006724 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 return NULL;
6726 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006727 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 sp = p;
6730 p = skipwhite(p);
6731 op = p;
6732 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006733
6734 if (var_count > 0 && oplen == 0)
6735 // can be something like "[1, 2]->func()"
6736 return arg;
6737
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006738 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006740 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006741 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006742 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006743 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6744 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006745 if (VIM_ISWHITE(eap->cmd[2]))
6746 {
6747 semsg(_(e_no_white_space_allowed_after_str_str),
6748 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6749 return NULL;
6750 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006751 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6752 oplen = 2;
6753 incdec = TRUE;
6754 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 if (heredoc)
6757 {
6758 list_T *l;
6759 listitem_T *li;
6760
6761 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006762 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006764 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006765 if (l == NULL)
6766 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767
Bram Moolenaar078269b2020-09-21 20:35:55 +02006768 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006770 // Push each line and the create the list.
6771 FOR_ALL_LIST_ITEMS(l, li)
6772 {
6773 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6774 li->li_tv.vval.v_string = NULL;
6775 }
6776 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 list_free(l);
6779 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006780 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006782 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006784 char_u *wp;
6785
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006786 // for "[var, var] = expr" evaluate the expression here, loop over the
6787 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006788 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006789
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006790 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006791 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006792 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793 if (compile_expr0(&p, cctx) == FAIL)
6794 return NULL;
6795 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006797 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006799 type_T *stacktype;
6800
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006801 stacktype = stack->ga_len == 0 ? &t_void
6802 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006803 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006805 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 goto theend;
6807 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006808 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006809 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006810 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006811 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006812 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6813 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006814 if (stacktype->tt_member != NULL)
6815 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006816 }
6817 }
6818
6819 /*
6820 * Loop over variables in "[var, var] = expr".
6821 * For "var = expr" and "let var: type" this is done only once.
6822 */
6823 if (var_count > 0)
6824 var_start = skipwhite(arg + 1); // skip over the "["
6825 else
6826 var_start = arg;
6827 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6828 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006829 int instr_count = -1;
6830 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006831
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006832 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6833 {
6834 // Ignore underscore in "[a, _, b] = list".
6835 if (var_count > 0)
6836 {
6837 var_start = skipwhite(var_start + 2);
6838 continue;
6839 }
6840 emsg(_(e_cannot_use_underscore_here));
6841 goto theend;
6842 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006843 vim_free(lhs.lhs_name);
6844
6845 /*
6846 * Figure out the LHS type and other properties.
6847 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006848 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6849 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006850 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006851 if (!heredoc)
6852 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006853 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006854 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006855 if (oplen > 0 && var_count == 0)
6856 {
6857 // skip over the "=" and the expression
6858 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006859 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006860 }
6861 }
6862 else if (oplen > 0)
6863 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006864 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006865 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006866
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006867 // for "+=", "*=", "..=" etc. first load the current value
6868 if (*op != '='
6869 && compile_load_lhs_with_index(&lhs, var_start,
6870 cctx) == FAIL)
6871 goto theend;
6872
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006873 // For "var = expr" evaluate the expression.
6874 if (var_count == 0)
6875 {
6876 int r;
6877
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006878 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006879 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006880 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006881 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006882 r = generate_PUSHNR(cctx, 1);
6883 }
6884 else
6885 {
6886 // Temporarily hide the new local variable here, it is
6887 // not available to this expression.
6888 if (lhs.lhs_new_local)
6889 --cctx->ctx_locals.ga_len;
6890 wp = op + oplen;
6891 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6892 {
6893 if (lhs.lhs_new_local)
6894 ++cctx->ctx_locals.ga_len;
6895 goto theend;
6896 }
6897 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006898 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006899 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006900 if (r == FAIL)
6901 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006902 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006903 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006904 else if (semicolon && var_idx == var_count - 1)
6905 {
6906 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006907 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006908 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6909 goto theend;
6910 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006911 else
6912 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006913 // For "[var, var] = expr" get the "var_idx" item from the
6914 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006915 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006916 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006917 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006918
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006919 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006920 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006922 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006923 if ((rhs_type->tt_type == VAR_FUNC
6924 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006925 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006926 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006927 goto theend;
6928
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006930 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006931 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006932 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006933 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006934 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 }
6936 else
6937 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006938 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006939 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006940 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006941 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006942 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006944 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006945 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006946 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006947 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006948 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006949 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006950 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006951 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006952 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006953
Bram Moolenaar77709b12021-04-03 21:01:01 +02006954 // Without operator check type here, otherwise below.
6955 // Use the line number of the assignment.
6956 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006957 if (lhs.lhs_has_index)
6958 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006959 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006960 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006961 goto theend;
6962 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006963 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006964 else
6965 {
6966 type_T *lhs_type = lhs.lhs_member_type;
6967
6968 // Special case: assigning to @# can use a number or a
6969 // string.
6970 if (lhs_type == &t_number_or_string
6971 && rhs_type->tt_type == VAR_NUMBER)
6972 lhs_type = &t_number;
6973 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006974 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006975 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006978 else if (cmdidx == CMD_final)
6979 {
6980 emsg(_(e_final_requires_a_value));
6981 goto theend;
6982 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006983 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006985 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006986 goto theend;
6987 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006988 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006990 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006991 goto theend;
6992 }
6993 else
6994 {
6995 // variables are always initialized
6996 if (ga_grow(instr, 1) == FAIL)
6997 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006998 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006999 {
7000 case VAR_BOOL:
7001 generate_PUSHBOOL(cctx, VVAL_FALSE);
7002 break;
7003 case VAR_FLOAT:
7004#ifdef FEAT_FLOAT
7005 generate_PUSHF(cctx, 0.0);
7006#endif
7007 break;
7008 case VAR_STRING:
7009 generate_PUSHS(cctx, NULL);
7010 break;
7011 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007012 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007013 break;
7014 case VAR_FUNC:
7015 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7016 break;
7017 case VAR_LIST:
7018 generate_NEWLIST(cctx, 0);
7019 break;
7020 case VAR_DICT:
7021 generate_NEWDICT(cctx, 0);
7022 break;
7023 case VAR_JOB:
7024 generate_PUSHJOB(cctx, NULL);
7025 break;
7026 case VAR_CHANNEL:
7027 generate_PUSHCHANNEL(cctx, NULL);
7028 break;
7029 case VAR_NUMBER:
7030 case VAR_UNKNOWN:
7031 case VAR_ANY:
7032 case VAR_PARTIAL:
7033 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007034 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035 case VAR_SPECIAL: // cannot happen
7036 generate_PUSHNR(cctx, 0);
7037 break;
7038 }
7039 }
7040 if (var_count == 0)
7041 end = p;
7042 }
7043
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007044 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007045 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007046 break;
7047
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 if (oplen > 0 && *op != '=')
7049 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007050 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007051 type_T *stacktype;
7052
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007053 if (*op == '.')
7054 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007055 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007056 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007057 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007058 if (
7059#ifdef FEAT_FLOAT
7060 // If variable is float operation with number is OK.
7061 !(expected == &t_float && stacktype == &t_number) &&
7062#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007063 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007064 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007065 goto theend;
7066
7067 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007068 {
7069 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7070 goto theend;
7071 }
7072 else if (*op == '+')
7073 {
7074 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007075 operator_type(lhs.lhs_member_type, stacktype),
7076 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007077 goto theend;
7078 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007079 else if (generate_two_op(cctx, op) == FAIL)
7080 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007083 // Use the line number of the assignment for store instruction.
7084 save_lnum = cctx->ctx_lnum;
7085 cctx->ctx_lnum = start_lnum - 1;
7086
Bram Moolenaar752fc692021-01-04 21:57:11 +01007087 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007088 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007089 // Use the info in "lhs" to store the value at the index in the
7090 // list or dict.
7091 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7092 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007093 {
7094 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007095 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007096 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007097 }
7098 else
7099 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007100 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007101 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007102 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007103 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007104 generate_LOCKCONST(cctx);
7105
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007106 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007107 && (lhs.lhs_type->tt_type == VAR_DICT
7108 || lhs.lhs_type->tt_type == VAR_LIST)
7109 && lhs.lhs_type->tt_member != NULL
7110 && lhs.lhs_type->tt_member != &t_any
7111 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007112 // Set the type in the list or dict, so that it can be checked,
7113 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007114 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007115
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007116 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007117 {
7118 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007119 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007120 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007121 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007122 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007123
7124 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007125 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007127
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007128 // For "[var, var] = expr" drop the "expr" value.
7129 // Also for "[var, var; _] = expr".
7130 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007131 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007132 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007133 goto theend;
7134 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007135
Bram Moolenaarb2097502020-07-19 17:17:02 +02007136 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137
7138theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007139 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 return ret;
7141}
7142
7143/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007144 * Check for an assignment at "eap->cmd", compile it if found.
7145 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7146 */
7147 static int
7148may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7149{
7150 char_u *pskip;
7151 char_u *p;
7152
7153 // Assuming the command starts with a variable or function name,
7154 // find what follows.
7155 // Skip over "var.member", "var[idx]" and the like.
7156 // Also "&opt = val", "$ENV = val" and "@r = val".
7157 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7158 ? eap->cmd + 1 : eap->cmd;
7159 p = to_name_end(pskip, TRUE);
7160 if (p > eap->cmd && *p != NUL)
7161 {
7162 char_u *var_end;
7163 int oplen;
7164 int heredoc;
7165
7166 if (eap->cmd[0] == '@')
7167 var_end = eap->cmd + 2;
7168 else
7169 var_end = find_name_end(pskip, NULL, NULL,
7170 FNE_CHECK_START | FNE_INCL_BR);
7171 oplen = assignment_len(skipwhite(var_end), &heredoc);
7172 if (oplen > 0)
7173 {
7174 size_t len = p - eap->cmd;
7175
7176 // Recognize an assignment if we recognize the variable
7177 // name:
7178 // "g:var = expr"
7179 // "local = expr" where "local" is a local var.
7180 // "script = expr" where "script" is a script-local var.
7181 // "import = expr" where "import" is an imported var
7182 // "&opt = expr"
7183 // "$ENV = expr"
7184 // "@r = expr"
7185 if (*eap->cmd == '&'
7186 || *eap->cmd == '$'
7187 || *eap->cmd == '@'
7188 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007189 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007190 {
7191 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7192 if (*line == NULL || *line == eap->cmd)
7193 return FAIL;
7194 return OK;
7195 }
7196 }
7197 }
7198
7199 if (*eap->cmd == '[')
7200 {
7201 // [var, var] = expr
7202 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7203 if (*line == NULL)
7204 return FAIL;
7205 if (*line != eap->cmd)
7206 return OK;
7207 }
7208 return NOTDONE;
7209}
7210
7211/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007212 * Check if "name" can be "unlet".
7213 */
7214 int
7215check_vim9_unlet(char_u *name)
7216{
7217 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7218 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007219 // "unlet s:var" is allowed in legacy script.
7220 if (*name == 's' && !script_is_vim9())
7221 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007222 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007223 return FAIL;
7224 }
7225 return OK;
7226}
7227
7228/*
7229 * Callback passed to ex_unletlock().
7230 */
7231 static int
7232compile_unlet(
7233 lval_T *lvp,
7234 char_u *name_end,
7235 exarg_T *eap,
7236 int deep UNUSED,
7237 void *coookie)
7238{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007239 cctx_T *cctx = coookie;
7240 char_u *p = lvp->ll_name;
7241 int cc = *name_end;
7242 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007243
Bram Moolenaar752fc692021-01-04 21:57:11 +01007244 if (cctx->ctx_skip == SKIP_YES)
7245 return OK;
7246
7247 *name_end = NUL;
7248 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007249 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007250 // :unlet $ENV_VAR
7251 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7252 }
7253 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7254 {
7255 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007256
Bram Moolenaar752fc692021-01-04 21:57:11 +01007257 // This is similar to assigning: lookup the list/dict, compile the
7258 // idx/key. Then instead of storing the value unlet the item.
7259 // unlet {list}[idx]
7260 // unlet {dict}[key] dict.key
7261 //
7262 // Figure out the LHS type and other properties.
7263 //
7264 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7265
7266 // : unlet an indexed item
7267 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007268 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007269 iemsg("called compile_lhs() without an index");
7270 ret = FAIL;
7271 }
7272 else
7273 {
7274 // Use the info in "lhs" to unlet the item at the index in the
7275 // list or dict.
7276 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007277 }
7278
Bram Moolenaar752fc692021-01-04 21:57:11 +01007279 vim_free(lhs.lhs_name);
7280 }
7281 else if (check_vim9_unlet(p) == FAIL)
7282 {
7283 ret = FAIL;
7284 }
7285 else
7286 {
7287 // Normal name. Only supports g:, w:, t: and b: namespaces.
7288 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007289 }
7290
Bram Moolenaar752fc692021-01-04 21:57:11 +01007291 *name_end = cc;
7292 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007293}
7294
7295/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007296 * Callback passed to ex_unletlock().
7297 */
7298 static int
7299compile_lock_unlock(
7300 lval_T *lvp,
7301 char_u *name_end,
7302 exarg_T *eap,
7303 int deep UNUSED,
7304 void *coookie)
7305{
7306 cctx_T *cctx = coookie;
7307 int cc = *name_end;
7308 char_u *p = lvp->ll_name;
7309 int ret = OK;
7310 size_t len;
7311 char_u *buf;
7312
7313 if (cctx->ctx_skip == SKIP_YES)
7314 return OK;
7315
7316 // Cannot use :lockvar and :unlockvar on local variables.
7317 if (p[1] != ':')
7318 {
7319 char_u *end = skip_var_one(p, FALSE);
7320
7321 if (lookup_local(p, end - p, NULL, cctx) == OK)
7322 {
7323 emsg(_(e_cannot_lock_unlock_local_variable));
7324 return FAIL;
7325 }
7326 }
7327
7328 // Checking is done at runtime.
7329 *name_end = NUL;
7330 len = name_end - p + 20;
7331 buf = alloc(len);
7332 if (buf == NULL)
7333 ret = FAIL;
7334 else
7335 {
7336 vim_snprintf((char *)buf, len, "%s %s",
7337 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7338 p);
7339 ret = generate_EXEC(cctx, buf);
7340
7341 vim_free(buf);
7342 *name_end = cc;
7343 }
7344 return ret;
7345}
7346
7347/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007348 * compile "unlet var", "lock var" and "unlock var"
7349 * "arg" points to "var".
7350 */
7351 static char_u *
7352compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7353{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007354 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7355 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7356 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007357 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7358}
7359
7360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7362 */
7363 static int
7364compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7365{
7366 garray_T *instr = &cctx->ctx_instr;
7367 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7368
7369 if (endlabel == NULL)
7370 return FAIL;
7371 endlabel->el_next = *el;
7372 *el = endlabel;
7373 endlabel->el_end_label = instr->ga_len;
7374
7375 generate_JUMP(cctx, when, 0);
7376 return OK;
7377}
7378
7379 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007380compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381{
7382 garray_T *instr = &cctx->ctx_instr;
7383
7384 while (*el != NULL)
7385 {
7386 endlabel_T *cur = (*el);
7387 isn_T *isn;
7388
7389 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007390 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 *el = cur->el_next;
7392 vim_free(cur);
7393 }
7394}
7395
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007396 static void
7397compile_free_jump_to_end(endlabel_T **el)
7398{
7399 while (*el != NULL)
7400 {
7401 endlabel_T *cur = (*el);
7402
7403 *el = cur->el_next;
7404 vim_free(cur);
7405 }
7406}
7407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408/*
7409 * Create a new scope and set up the generic items.
7410 */
7411 static scope_T *
7412new_scope(cctx_T *cctx, scopetype_T type)
7413{
7414 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7415
7416 if (scope == NULL)
7417 return NULL;
7418 scope->se_outer = cctx->ctx_scope;
7419 cctx->ctx_scope = scope;
7420 scope->se_type = type;
7421 scope->se_local_count = cctx->ctx_locals.ga_len;
7422 return scope;
7423}
7424
7425/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007426 * Free the current scope and go back to the outer scope.
7427 */
7428 static void
7429drop_scope(cctx_T *cctx)
7430{
7431 scope_T *scope = cctx->ctx_scope;
7432
7433 if (scope == NULL)
7434 {
7435 iemsg("calling drop_scope() without a scope");
7436 return;
7437 }
7438 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007439 switch (scope->se_type)
7440 {
7441 case IF_SCOPE:
7442 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7443 case FOR_SCOPE:
7444 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7445 case WHILE_SCOPE:
7446 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7447 case TRY_SCOPE:
7448 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7449 case NO_SCOPE:
7450 case BLOCK_SCOPE:
7451 break;
7452 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007453 vim_free(scope);
7454}
7455
7456/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 * compile "if expr"
7458 *
7459 * "if expr" Produces instructions:
7460 * EVAL expr Push result of "expr"
7461 * JUMP_IF_FALSE end
7462 * ... body ...
7463 * end:
7464 *
7465 * "if expr | else" Produces instructions:
7466 * EVAL expr Push result of "expr"
7467 * JUMP_IF_FALSE else
7468 * ... body ...
7469 * JUMP_ALWAYS end
7470 * else:
7471 * ... body ...
7472 * end:
7473 *
7474 * "if expr1 | elseif expr2 | else" Produces instructions:
7475 * EVAL expr Push result of "expr"
7476 * JUMP_IF_FALSE elseif
7477 * ... body ...
7478 * JUMP_ALWAYS end
7479 * elseif:
7480 * EVAL expr Push result of "expr"
7481 * JUMP_IF_FALSE else
7482 * ... body ...
7483 * JUMP_ALWAYS end
7484 * else:
7485 * ... body ...
7486 * end:
7487 */
7488 static char_u *
7489compile_if(char_u *arg, cctx_T *cctx)
7490{
7491 char_u *p = arg;
7492 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007493 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007495 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007496 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497
Bram Moolenaara5565e42020-05-09 15:44:01 +02007498 CLEAR_FIELD(ppconst);
7499 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007500 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007501 clear_ppconst(&ppconst);
7502 return NULL;
7503 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007504 if (!ends_excmd2(arg, skipwhite(p)))
7505 {
7506 semsg(_(e_trailing_arg), p);
7507 return NULL;
7508 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007509 if (cctx->ctx_skip == SKIP_YES)
7510 clear_ppconst(&ppconst);
7511 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007512 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007513 int error = FALSE;
7514 int v;
7515
Bram Moolenaara5565e42020-05-09 15:44:01 +02007516 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007517 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007518 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007519 if (error)
7520 return NULL;
7521 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007522 }
7523 else
7524 {
7525 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007526 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007527 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007528 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007529 if (bool_on_stack(cctx) == FAIL)
7530 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532
Bram Moolenaara91a7132021-03-25 21:12:15 +01007533 // CMDMOD_REV must come before the jump
7534 generate_undo_cmdmods(cctx);
7535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 scope = new_scope(cctx, IF_SCOPE);
7537 if (scope == NULL)
7538 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007539 scope->se_skip_save = skip_save;
7540 // "is_had_return" will be reset if any block does not end in :return
7541 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007543 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007544 {
7545 // "where" is set when ":elseif", "else" or ":endif" is found
7546 scope->se_u.se_if.is_if_label = instr->ga_len;
7547 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7548 }
7549 else
7550 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551
Bram Moolenaarced68a02021-01-24 17:53:47 +01007552#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007553 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007554 && skip_save != SKIP_YES)
7555 {
7556 // generated a profile start, need to generate a profile end, since it
7557 // won't be done after returning
7558 cctx->ctx_skip = SKIP_NOT;
7559 generate_instr(cctx, ISN_PROF_END);
7560 cctx->ctx_skip = SKIP_YES;
7561 }
7562#endif
7563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 return p;
7565}
7566
7567 static char_u *
7568compile_elseif(char_u *arg, cctx_T *cctx)
7569{
7570 char_u *p = arg;
7571 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007572 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 isn_T *isn;
7574 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007575 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007576 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577
7578 if (scope == NULL || scope->se_type != IF_SCOPE)
7579 {
7580 emsg(_(e_elseif_without_if));
7581 return NULL;
7582 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007583 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007584 if (!cctx->ctx_had_return)
7585 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586
Bram Moolenaarced68a02021-01-24 17:53:47 +01007587 if (cctx->ctx_skip == SKIP_NOT)
7588 {
7589 // previous block was executed, this one and following will not
7590 cctx->ctx_skip = SKIP_YES;
7591 scope->se_u.se_if.is_seen_skip_not = TRUE;
7592 }
7593 if (scope->se_u.se_if.is_seen_skip_not)
7594 {
7595 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007596 // Do not count the "elseif" for profiling and cmdmod
7597 instr->ga_len = current_instr_idx(cctx);
7598
Bram Moolenaarced68a02021-01-24 17:53:47 +01007599 skip_expr_cctx(&p, cctx);
7600 return p;
7601 }
7602
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007603 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007604 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007605 int moved_cmdmod = FALSE;
7606
7607 // Move any CMDMOD instruction to after the jump
7608 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7609 {
7610 if (ga_grow(instr, 1) == FAIL)
7611 return NULL;
7612 ((isn_T *)instr->ga_data)[instr->ga_len] =
7613 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7614 --instr->ga_len;
7615 moved_cmdmod = TRUE;
7616 }
7617
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007618 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007620 return NULL;
7621 // previous "if" or "elseif" jumps here
7622 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7623 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007624 if (moved_cmdmod)
7625 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007628 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007629 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007630 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007631 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007632 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007633#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007634 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007635 {
7636 // the previous block was skipped, need to profile this line
7637 generate_instr(cctx, ISN_PROF_START);
7638 instr_count = instr->ga_len;
7639 }
7640#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007641 if (cctx->ctx_compile_type == CT_DEBUG)
7642 {
7643 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007644 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007645 instr_count = instr->ga_len;
7646 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007647 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007648 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007649 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007650 clear_ppconst(&ppconst);
7651 return NULL;
7652 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007653 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007654 if (!ends_excmd2(arg, skipwhite(p)))
7655 {
7656 semsg(_(e_trailing_arg), p);
7657 return NULL;
7658 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007659 if (scope->se_skip_save == SKIP_YES)
7660 clear_ppconst(&ppconst);
7661 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007662 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007663 int error = FALSE;
7664 int v;
7665
Bram Moolenaar7f141552020-05-09 17:35:53 +02007666 // The expression results in a constant.
7667 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007668 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7669 if (error)
7670 return NULL;
7671 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007672 clear_ppconst(&ppconst);
7673 scope->se_u.se_if.is_if_label = -1;
7674 }
7675 else
7676 {
7677 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007678 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007679 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007680 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007681 if (bool_on_stack(cctx) == FAIL)
7682 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683
Bram Moolenaara91a7132021-03-25 21:12:15 +01007684 // CMDMOD_REV must come before the jump
7685 generate_undo_cmdmods(cctx);
7686
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007687 // "where" is set when ":elseif", "else" or ":endif" is found
7688 scope->se_u.se_if.is_if_label = instr->ga_len;
7689 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691
7692 return p;
7693}
7694
7695 static char_u *
7696compile_else(char_u *arg, cctx_T *cctx)
7697{
7698 char_u *p = arg;
7699 garray_T *instr = &cctx->ctx_instr;
7700 isn_T *isn;
7701 scope_T *scope = cctx->ctx_scope;
7702
7703 if (scope == NULL || scope->se_type != IF_SCOPE)
7704 {
7705 emsg(_(e_else_without_if));
7706 return NULL;
7707 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007708 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007709 if (!cctx->ctx_had_return)
7710 scope->se_u.se_if.is_had_return = FALSE;
7711 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712
Bram Moolenaarced68a02021-01-24 17:53:47 +01007713#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007714 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007715 {
7716 if (cctx->ctx_skip == SKIP_NOT
7717 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7718 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007719 // the previous block was executed, do not count "else" for
7720 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007721 --instr->ga_len;
7722 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7723 {
7724 // the previous block was not executed, this one will, do count the
7725 // "else" for profiling
7726 cctx->ctx_skip = SKIP_NOT;
7727 generate_instr(cctx, ISN_PROF_END);
7728 generate_instr(cctx, ISN_PROF_START);
7729 cctx->ctx_skip = SKIP_YES;
7730 }
7731 }
7732#endif
7733
7734 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007735 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007736 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007737 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007738 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007739 if (!cctx->ctx_had_return
7740 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7741 JUMP_ALWAYS, cctx) == FAIL)
7742 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007743 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007744
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007745 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007746 {
7747 if (scope->se_u.se_if.is_if_label >= 0)
7748 {
7749 // previous "if" or "elseif" jumps here
7750 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7751 isn->isn_arg.jump.jump_where = instr->ga_len;
7752 scope->se_u.se_if.is_if_label = -1;
7753 }
7754 }
7755
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007756 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007757 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759
7760 return p;
7761}
7762
7763 static char_u *
7764compile_endif(char_u *arg, cctx_T *cctx)
7765{
7766 scope_T *scope = cctx->ctx_scope;
7767 ifscope_T *ifscope;
7768 garray_T *instr = &cctx->ctx_instr;
7769 isn_T *isn;
7770
Bram Moolenaarfa984412021-03-25 22:15:28 +01007771 if (misplaced_cmdmod(cctx))
7772 return NULL;
7773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 if (scope == NULL || scope->se_type != IF_SCOPE)
7775 {
7776 emsg(_(e_endif_without_if));
7777 return NULL;
7778 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007779 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007780 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007781 if (!cctx->ctx_had_return)
7782 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007784 if (scope->se_u.se_if.is_if_label >= 0)
7785 {
7786 // previous "if" or "elseif" jumps here
7787 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7788 isn->isn_arg.jump.jump_where = instr->ga_len;
7789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007791 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007792
7793#ifdef FEAT_PROFILE
7794 // even when skipping we count the endif as executed, unless the block it's
7795 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007796 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007797 && scope->se_skip_save != SKIP_YES)
7798 {
7799 cctx->ctx_skip = SKIP_NOT;
7800 generate_instr(cctx, ISN_PROF_START);
7801 }
7802#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007803 cctx->ctx_skip = scope->se_skip_save;
7804
7805 // If all the blocks end in :return and there is an :else then the
7806 // had_return flag is set.
7807 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007809 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 return arg;
7811}
7812
7813/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007814 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 *
7816 * Produces instructions:
7817 * PUSHNR -1
7818 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007819 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7821 * - if beyond end, jump to "end"
7822 * - otherwise get item from list and push it
7823 * STORE var Store item in "var"
7824 * ... body ...
7825 * JUMP top Jump back to repeat
7826 * end: DROP Drop the result of "expr"
7827 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007828 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7829 * UNPACK 2 Split item in 2
7830 * STORE var1 Store item in "var1"
7831 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 */
7833 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007834compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007836 char_u *arg;
7837 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007838 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007840 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007841 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007842 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007843 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007846 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007848 lvar_T *loop_lvar; // loop iteration variable
7849 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007851 type_T *item_type = &t_any;
7852 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007853 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854
Bram Moolenaar792f7862020-11-23 08:31:18 +01007855 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007856 if (p == NULL)
7857 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007858 if (var_count == 0)
7859 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007860 else
7861 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862
7863 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007864 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007865 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7866 return NULL;
7867 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007869 if (*p == ':' && wp != p)
7870 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7871 else
7872 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 return NULL;
7874 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007875 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007876 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7877 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007879 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7880 // instruction.
7881 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7882 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7883 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007884 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007885 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007886 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7887 .isn_arg.debug.dbg_break_lnum;
7888 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 scope = new_scope(cctx, FOR_SCOPE);
7891 if (scope == NULL)
7892 return NULL;
7893
Bram Moolenaar792f7862020-11-23 08:31:18 +01007894 // Reserve a variable to store the loop iteration counter and initialize it
7895 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007896 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7897 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007898 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007899 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007900 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007902 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007903 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904
7905 // compile "expr", it remains on the stack until "endfor"
7906 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007907 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007908 {
7909 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007910 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007911 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007912 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007914 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007915 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007917 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007918 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007920 semsg(_(e_for_loop_on_str_not_supported),
7921 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007922 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923 return NULL;
7924 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007925
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007926 if (vartype->tt_type == VAR_STRING)
7927 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007928 else if (vartype->tt_type == VAR_BLOB)
7929 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007930 else if (vartype->tt_type == VAR_LIST
7931 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007932 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007933 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007934 item_type = vartype->tt_member;
7935 else if (vartype->tt_member->tt_type == VAR_LIST
7936 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007937 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007938 item_type = vartype->tt_member->tt_member;
7939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007941 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007942 generate_undo_cmdmods(cctx);
7943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007945 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007946
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007947 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948
Bram Moolenaar792f7862020-11-23 08:31:18 +01007949 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007950 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007951 {
7952 generate_UNPACK(cctx, var_count, semicolon);
7953 arg = skipwhite(arg + 1); // skip white after '['
7954
7955 // the list item is replaced by a number of items
7956 if (ga_grow(stack, var_count - 1) == FAIL)
7957 {
7958 drop_scope(cctx);
7959 return NULL;
7960 }
7961 --stack->ga_len;
7962 for (idx = 0; idx < var_count; ++idx)
7963 {
7964 ((type_T **)stack->ga_data)[stack->ga_len] =
7965 (semicolon && idx == 0) ? vartype : item_type;
7966 ++stack->ga_len;
7967 }
7968 }
7969
7970 for (idx = 0; idx < var_count; ++idx)
7971 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007972 assign_dest_T dest = dest_local;
7973 int opt_flags = 0;
7974 int vimvaridx = -1;
7975 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007976 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02007977 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007978
7979 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007980 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007981 name = vim_strnsave(arg, varlen);
7982 if (name == NULL)
7983 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007984 if (*p == ':')
7985 {
7986 p = skipwhite(p + 1);
7987 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7988 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007989
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007990 // TODO: script var not supported?
7991 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7992 &vimvaridx, &type, cctx) == FAIL)
7993 goto failed;
7994 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007995 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007996 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7997 0, 0, type, name) == FAIL)
7998 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007999 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008000 else if (varlen == 1 && *arg == '_')
8001 {
8002 // Assigning to "_": drop the value.
8003 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8004 goto failed;
8005 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008006 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008007 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008008 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008009 {
8010 semsg(_(e_variable_already_declared), arg);
8011 goto failed;
8012 }
8013
Bram Moolenaarea870692020-12-02 14:24:30 +01008014 if (STRNCMP(name, "s:", 2) == 0)
8015 {
8016 semsg(_(e_cannot_declare_script_variable_in_function), name);
8017 goto failed;
8018 }
8019
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008020 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008021 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008022 where.wt_variable = TRUE;
8023 if (lhs_type == &t_any)
8024 lhs_type = item_type;
8025 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008026 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008027 ? need_type(item_type, lhs_type,
8028 -1, 0, cctx, FALSE, FALSE)
8029 : check_type(lhs_type, item_type, TRUE, where))
8030 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008031 goto failed;
8032 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008033 if (var_lvar == NULL)
8034 // out of memory or used as an argument
8035 goto failed;
8036
8037 if (semicolon && idx == var_count - 1)
8038 var_lvar->lv_type = vartype;
8039 else
8040 var_lvar->lv_type = item_type;
8041 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8042 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008043
8044 if (*p == ',' || *p == ';')
8045 ++p;
8046 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008047 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008048 }
8049
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008050 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008051 {
8052 int save_prev_lnum = cctx->ctx_prev_lnum;
8053
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008054 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008055 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8056 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008057 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008058 cctx->ctx_prev_lnum = save_prev_lnum;
8059 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008060
Bram Moolenaar792f7862020-11-23 08:31:18 +01008061 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008062
8063failed:
8064 vim_free(name);
8065 drop_scope(cctx);
8066 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067}
8068
8069/*
8070 * compile "endfor"
8071 */
8072 static char_u *
8073compile_endfor(char_u *arg, cctx_T *cctx)
8074{
8075 garray_T *instr = &cctx->ctx_instr;
8076 scope_T *scope = cctx->ctx_scope;
8077 forscope_T *forscope;
8078 isn_T *isn;
8079
Bram Moolenaarfa984412021-03-25 22:15:28 +01008080 if (misplaced_cmdmod(cctx))
8081 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 if (scope == NULL || scope->se_type != FOR_SCOPE)
8084 {
8085 emsg(_(e_for));
8086 return NULL;
8087 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008088 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008090 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091
8092 // At end of ":for" scope jump back to the FOR instruction.
8093 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8094
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008095 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8097 isn->isn_arg.forloop.for_end = instr->ga_len;
8098
8099 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008100 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008101
8102 // Below the ":for" scope drop the "expr" list from the stack.
8103 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8104 return NULL;
8105
8106 vim_free(scope);
8107
8108 return arg;
8109}
8110
8111/*
8112 * compile "while expr"
8113 *
8114 * Produces instructions:
8115 * top: EVAL expr Push result of "expr"
8116 * JUMP_IF_FALSE end jump if false
8117 * ... body ...
8118 * JUMP top Jump back to repeat
8119 * end:
8120 *
8121 */
8122 static char_u *
8123compile_while(char_u *arg, cctx_T *cctx)
8124{
8125 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008126 scope_T *scope;
8127
8128 scope = new_scope(cctx, WHILE_SCOPE);
8129 if (scope == NULL)
8130 return NULL;
8131
Bram Moolenaara91a7132021-03-25 21:12:15 +01008132 // "endwhile" jumps back here, one before when profiling or using cmdmods
8133 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134
8135 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008136 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008138 if (!ends_excmd2(arg, skipwhite(p)))
8139 {
8140 semsg(_(e_trailing_arg), p);
8141 return NULL;
8142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143
Bram Moolenaar13106602020-10-04 16:06:05 +02008144 if (bool_on_stack(cctx) == FAIL)
8145 return FAIL;
8146
Bram Moolenaara91a7132021-03-25 21:12:15 +01008147 // CMDMOD_REV must come before the jump
8148 generate_undo_cmdmods(cctx);
8149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008151 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 JUMP_IF_FALSE, cctx) == FAIL)
8153 return FAIL;
8154
8155 return p;
8156}
8157
8158/*
8159 * compile "endwhile"
8160 */
8161 static char_u *
8162compile_endwhile(char_u *arg, cctx_T *cctx)
8163{
8164 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008165 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166
Bram Moolenaarfa984412021-03-25 22:15:28 +01008167 if (misplaced_cmdmod(cctx))
8168 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8170 {
8171 emsg(_(e_while));
8172 return NULL;
8173 }
8174 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008175 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176
Bram Moolenaarf002a412021-01-24 13:34:18 +01008177#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008178 // count the endwhile before jumping
8179 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008180#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008183 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184
8185 // Fill in the "end" label in the WHILE statement so it can jump here.
8186 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008187 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8188 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
8190 vim_free(scope);
8191
8192 return arg;
8193}
8194
8195/*
8196 * compile "continue"
8197 */
8198 static char_u *
8199compile_continue(char_u *arg, cctx_T *cctx)
8200{
8201 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008202 int try_scopes = 0;
8203 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008204
8205 for (;;)
8206 {
8207 if (scope == NULL)
8208 {
8209 emsg(_(e_continue));
8210 return NULL;
8211 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008212 if (scope->se_type == FOR_SCOPE)
8213 {
8214 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008216 }
8217 if (scope->se_type == WHILE_SCOPE)
8218 {
8219 loop_label = scope->se_u.se_while.ws_top_label;
8220 break;
8221 }
8222 if (scope->se_type == TRY_SCOPE)
8223 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 scope = scope->se_outer;
8225 }
8226
Bram Moolenaarc150c092021-02-13 15:02:46 +01008227 if (try_scopes > 0)
8228 // Inside one or more try/catch blocks we first need to jump to the
8229 // "finally" or "endtry" to cleanup.
8230 generate_TRYCONT(cctx, try_scopes, loop_label);
8231 else
8232 // Jump back to the FOR or WHILE instruction.
8233 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008235 return arg;
8236}
8237
8238/*
8239 * compile "break"
8240 */
8241 static char_u *
8242compile_break(char_u *arg, cctx_T *cctx)
8243{
8244 scope_T *scope = cctx->ctx_scope;
8245 endlabel_T **el;
8246
8247 for (;;)
8248 {
8249 if (scope == NULL)
8250 {
8251 emsg(_(e_break));
8252 return NULL;
8253 }
8254 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8255 break;
8256 scope = scope->se_outer;
8257 }
8258
8259 // Jump to the end of the FOR or WHILE loop.
8260 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008261 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008263 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8265 return FAIL;
8266
8267 return arg;
8268}
8269
8270/*
8271 * compile "{" start of block
8272 */
8273 static char_u *
8274compile_block(char_u *arg, cctx_T *cctx)
8275{
8276 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8277 return NULL;
8278 return skipwhite(arg + 1);
8279}
8280
8281/*
8282 * compile end of block: drop one scope
8283 */
8284 static void
8285compile_endblock(cctx_T *cctx)
8286{
8287 scope_T *scope = cctx->ctx_scope;
8288
8289 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008290 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 vim_free(scope);
8292}
8293
8294/*
8295 * compile "try"
8296 * Creates a new scope for the try-endtry, pointing to the first catch and
8297 * finally.
8298 * Creates another scope for the "try" block itself.
8299 * TRY instruction sets up exception handling at runtime.
8300 *
8301 * "try"
8302 * TRY -> catch1, -> finally push trystack entry
8303 * ... try block
8304 * "throw {exception}"
8305 * EVAL {exception}
8306 * THROW create exception
8307 * ... try block
8308 * " catch {expr}"
8309 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008310 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311 * EVAL {expr}
8312 * MATCH
8313 * JUMP nomatch -> catch2
8314 * CATCH remove exception
8315 * ... catch block
8316 * " catch"
8317 * JUMP -> finally
8318 * catch2: CATCH remove exception
8319 * ... catch block
8320 * " finally"
8321 * finally:
8322 * ... finally block
8323 * " endtry"
8324 * ENDTRY pop trystack entry, may rethrow
8325 */
8326 static char_u *
8327compile_try(char_u *arg, cctx_T *cctx)
8328{
8329 garray_T *instr = &cctx->ctx_instr;
8330 scope_T *try_scope;
8331 scope_T *scope;
8332
Bram Moolenaarfa984412021-03-25 22:15:28 +01008333 if (misplaced_cmdmod(cctx))
8334 return NULL;
8335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 // scope that holds the jumps that go to catch/finally/endtry
8337 try_scope = new_scope(cctx, TRY_SCOPE);
8338 if (try_scope == NULL)
8339 return NULL;
8340
Bram Moolenaar69f70502021-01-01 16:10:46 +01008341 if (cctx->ctx_skip != SKIP_YES)
8342 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008343 isn_T *isn;
8344
8345 // "try_catch" is set when the first ":catch" is found or when no catch
8346 // is found and ":finally" is found.
8347 // "try_finally" is set when ":finally" is found
8348 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008349 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008350 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8351 return NULL;
8352 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8353 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008354 return NULL;
8355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356
8357 // scope for the try block itself
8358 scope = new_scope(cctx, BLOCK_SCOPE);
8359 if (scope == NULL)
8360 return NULL;
8361
8362 return arg;
8363}
8364
8365/*
8366 * compile "catch {expr}"
8367 */
8368 static char_u *
8369compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8370{
8371 scope_T *scope = cctx->ctx_scope;
8372 garray_T *instr = &cctx->ctx_instr;
8373 char_u *p;
8374 isn_T *isn;
8375
Bram Moolenaarfa984412021-03-25 22:15:28 +01008376 if (misplaced_cmdmod(cctx))
8377 return NULL;
8378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379 // end block scope from :try or :catch
8380 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8381 compile_endblock(cctx);
8382 scope = cctx->ctx_scope;
8383
8384 // Error if not in a :try scope
8385 if (scope == NULL || scope->se_type != TRY_SCOPE)
8386 {
8387 emsg(_(e_catch));
8388 return NULL;
8389 }
8390
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008391 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008393 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 return NULL;
8395 }
8396
Bram Moolenaar69f70502021-01-01 16:10:46 +01008397 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008398 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008399#ifdef FEAT_PROFILE
8400 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008401 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008402 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008403 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008404 .isn_type == ISN_PROF_START)
8405 --instr->ga_len;
8406#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008407 // Jump from end of previous block to :finally or :endtry
8408 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8409 JUMP_ALWAYS, cctx) == FAIL)
8410 return NULL;
8411
8412 // End :try or :catch scope: set value in ISN_TRY instruction
8413 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008414 if (isn->isn_arg.try.try_ref->try_catch == 0)
8415 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008416 if (scope->se_u.se_try.ts_catch_label != 0)
8417 {
8418 // Previous catch without match jumps here
8419 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8420 isn->isn_arg.jump.jump_where = instr->ga_len;
8421 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008422#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008423 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008424 {
8425 // a "throw" that jumps here needs to be counted
8426 generate_instr(cctx, ISN_PROF_END);
8427 // the "catch" is also counted
8428 generate_instr(cctx, ISN_PROF_START);
8429 }
8430#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008431 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008432 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 }
8434
8435 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008436 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008438 scope->se_u.se_try.ts_caught_all = TRUE;
8439 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 }
8441 else
8442 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008443 char_u *end;
8444 char_u *pat;
8445 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008446 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008447 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 // Push v:exception, push {expr} and MATCH
8450 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8451
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008452 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008453 if (*end != *p)
8454 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008455 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008456 vim_free(tofree);
8457 return FAIL;
8458 }
8459 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008460 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008461 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008462 len = (int)(end - tofree);
8463 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008464 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008465 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008466 if (pat == NULL)
8467 return FAIL;
8468 if (generate_PUSHS(cctx, pat) == FAIL)
8469 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8472 return NULL;
8473
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008474 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008475 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8476 return NULL;
8477 }
8478
Bram Moolenaar69f70502021-01-01 16:10:46 +01008479 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008480 return NULL;
8481
8482 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8483 return NULL;
8484 return p;
8485}
8486
8487 static char_u *
8488compile_finally(char_u *arg, cctx_T *cctx)
8489{
8490 scope_T *scope = cctx->ctx_scope;
8491 garray_T *instr = &cctx->ctx_instr;
8492 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008493 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494
Bram Moolenaarfa984412021-03-25 22:15:28 +01008495 if (misplaced_cmdmod(cctx))
8496 return NULL;
8497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 // end block scope from :try or :catch
8499 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8500 compile_endblock(cctx);
8501 scope = cctx->ctx_scope;
8502
8503 // Error if not in a :try scope
8504 if (scope == NULL || scope->se_type != TRY_SCOPE)
8505 {
8506 emsg(_(e_finally));
8507 return NULL;
8508 }
8509
8510 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008511 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008512 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008513 {
8514 emsg(_(e_finally_dup));
8515 return NULL;
8516 }
8517
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008518 this_instr = instr->ga_len;
8519#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008520 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008521 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008522 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008523 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008524 // jump to the profile start of the "finally"
8525 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008526
8527 // jump to the profile end above it
8528 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8529 .isn_type == ISN_PROF_END)
8530 --this_instr;
8531 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008532#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008534 // Fill in the "end" label in jumps at the end of the blocks.
8535 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8536 this_instr, cctx);
8537
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008538 // If there is no :catch then an exception jumps to :finally.
8539 if (isn->isn_arg.try.try_ref->try_catch == 0)
8540 isn->isn_arg.try.try_ref->try_catch = this_instr;
8541 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008542 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 {
8544 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008545 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008546 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008547 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008549 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8550 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 // TODO: set index in ts_finally_label jumps
8553
8554 return arg;
8555}
8556
8557 static char_u *
8558compile_endtry(char_u *arg, cctx_T *cctx)
8559{
8560 scope_T *scope = cctx->ctx_scope;
8561 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008562 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563
Bram Moolenaarfa984412021-03-25 22:15:28 +01008564 if (misplaced_cmdmod(cctx))
8565 return NULL;
8566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 // end block scope from :catch or :finally
8568 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8569 compile_endblock(cctx);
8570 scope = cctx->ctx_scope;
8571
8572 // Error if not in a :try scope
8573 if (scope == NULL || scope->se_type != TRY_SCOPE)
8574 {
8575 if (scope == NULL)
8576 emsg(_(e_no_endtry));
8577 else if (scope->se_type == WHILE_SCOPE)
8578 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008579 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008580 emsg(_(e_endfor));
8581 else
8582 emsg(_(e_endif));
8583 return NULL;
8584 }
8585
Bram Moolenaarc150c092021-02-13 15:02:46 +01008586 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008587 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008589 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8590 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008591 {
8592 emsg(_(e_missing_catch_or_finally));
8593 return NULL;
8594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008595
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008596#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008597 if (cctx->ctx_compile_type == CT_PROFILE
8598 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008599 .isn_type == ISN_PROF_START)
8600 // move the profile start after "endtry" so that it's not counted when
8601 // the exception is rethrown.
8602 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008603#endif
8604
Bram Moolenaar69f70502021-01-01 16:10:46 +01008605 // Fill in the "end" label in jumps at the end of the blocks, if not
8606 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008607 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8608 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609
Bram Moolenaar69f70502021-01-01 16:10:46 +01008610 if (scope->se_u.se_try.ts_catch_label != 0)
8611 {
8612 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008613 isn_T *isn = ((isn_T *)instr->ga_data)
8614 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008615 isn->isn_arg.jump.jump_where = instr->ga_len;
8616 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008617 }
8618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008619 compile_endblock(cctx);
8620
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008621 if (cctx->ctx_skip != SKIP_YES)
8622 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008623 // End :catch or :finally scope: set instruction index in ISN_TRY
8624 // instruction
8625 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008626 if (cctx->ctx_skip != SKIP_YES
8627 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8628 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008629#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008630 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008631 generate_instr(cctx, ISN_PROF_START);
8632#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 return arg;
8635}
8636
8637/*
8638 * compile "throw {expr}"
8639 */
8640 static char_u *
8641compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8642{
8643 char_u *p = skipwhite(arg);
8644
Bram Moolenaara5565e42020-05-09 15:44:01 +02008645 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008647 if (cctx->ctx_skip == SKIP_YES)
8648 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008649 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 return NULL;
8651 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8652 return NULL;
8653
8654 return p;
8655}
8656
Bram Moolenaarc3235272021-07-10 19:42:03 +02008657 static char_u *
8658compile_eval(char_u *arg, cctx_T *cctx)
8659{
8660 char_u *p = arg;
8661 int name_only;
8662 char_u *alias;
8663 long lnum = SOURCING_LNUM;
8664
8665 // find_ex_command() will consider a variable name an expression, assuming
8666 // that something follows on the next line. Check that something actually
8667 // follows, otherwise it's probably a misplaced command.
8668 get_name_len(&p, &alias, FALSE, FALSE);
8669 name_only = ends_excmd2(arg, skipwhite(p));
8670 vim_free(alias);
8671
8672 p = arg;
8673 if (compile_expr0(&p, cctx) == FAIL)
8674 return NULL;
8675
8676 if (name_only && lnum == SOURCING_LNUM)
8677 {
8678 semsg(_(e_expression_without_effect_str), arg);
8679 return NULL;
8680 }
8681
8682 // drop the result
8683 generate_instr_drop(cctx, ISN_DROP, 1);
8684
8685 return skipwhite(p);
8686}
8687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688/*
8689 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008690 * compile "echomsg expr"
8691 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008692 * compile "execute expr"
8693 */
8694 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008695compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008696{
8697 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008698 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008699 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008700 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008701 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008702 garray_T *stack = &cctx->ctx_type_stack;
8703 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008704
8705 for (;;)
8706 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008707 if (ends_excmd2(prev, p))
8708 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008709 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008710 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008711 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008712
8713 if (cctx->ctx_skip != SKIP_YES)
8714 {
8715 // check for non-void type
8716 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8717 if (type->tt_type == VAR_VOID)
8718 {
8719 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8720 return NULL;
8721 }
8722 }
8723
Bram Moolenaarad39c092020-02-26 18:23:43 +01008724 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008725 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008726 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008727 }
8728
Bram Moolenaare4984292020-12-13 14:19:25 +01008729 if (count > 0)
8730 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008731 long save_lnum = cctx->ctx_lnum;
8732
8733 // Use the line number where the command started.
8734 cctx->ctx_lnum = start_ctx_lnum;
8735
Bram Moolenaare4984292020-12-13 14:19:25 +01008736 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8737 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8738 else if (cmdidx == CMD_execute)
8739 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8740 else if (cmdidx == CMD_echomsg)
8741 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8742 else
8743 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008744
8745 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 return p;
8748}
8749
8750/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008751 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008752 * instruction to compute it and return OK.
8753 * Otherwise return FAIL, the caller must deal with any range.
8754 */
8755 static int
8756compile_variable_range(exarg_T *eap, cctx_T *cctx)
8757{
8758 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8759 char_u *p = skipdigits(eap->cmd);
8760
8761 if (p == range_end)
8762 return FAIL;
8763 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8764}
8765
8766/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008767 * :put r
8768 * :put ={expr}
8769 */
8770 static char_u *
8771compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8772{
8773 char_u *line = arg;
8774 linenr_T lnum;
8775 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008776 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008777
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008778 eap->regname = *line;
8779
8780 if (eap->regname == '=')
8781 {
8782 char_u *p = line + 1;
8783
8784 if (compile_expr0(&p, cctx) == FAIL)
8785 return NULL;
8786 line = p;
8787 }
8788 else if (eap->regname != NUL)
8789 ++line;
8790
Bram Moolenaar08597872020-12-10 19:43:40 +01008791 if (compile_variable_range(eap, cctx) == OK)
8792 {
8793 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8794 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008795 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008796 {
8797 // Either no range or a number.
8798 // "errormsg" will not be set because the range is ADDR_LINES.
8799 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008800 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008801 return NULL;
8802 if (eap->addr_count == 0)
8803 lnum = -1;
8804 else
8805 lnum = eap->line2;
8806 if (above)
8807 --lnum;
8808 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008809
8810 generate_PUT(cctx, eap->regname, lnum);
8811 return line;
8812}
8813
8814/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008815 * A command that is not compiled, execute with legacy code.
8816 */
8817 static char_u *
8818compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8819{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008820 char_u *p;
8821 int has_expr = FALSE;
8822 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008823
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008824 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008825 goto theend;
8826
Bram Moolenaare729ce22021-06-06 21:38:09 +02008827 // If there was a prececing command modifier, drop it and include it in the
8828 // EXEC command.
8829 if (cctx->ctx_has_cmdmod)
8830 {
8831 garray_T *instr = &cctx->ctx_instr;
8832 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8833
8834 if (isn->isn_type == ISN_CMDMOD)
8835 {
8836 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8837 ->cmod_filter_regmatch.regprog);
8838 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8839 --instr->ga_len;
8840 cctx->ctx_has_cmdmod = FALSE;
8841 }
8842 }
8843
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008844 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008845 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008846 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008847 int usefilter = FALSE;
8848
8849 has_expr = argt & (EX_XFILE | EX_EXPAND);
8850
8851 // If the command can be followed by a bar, find the bar and truncate
8852 // it, so that the following command can be compiled.
8853 // The '|' is overwritten with a NUL, it is put back below.
8854 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8855 && *eap->arg == '!')
8856 // :w !filter or :r !filter or :r! filter
8857 usefilter = TRUE;
8858 if ((argt & EX_TRLBAR) && !usefilter)
8859 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008860 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008861 separate_nextcmd(eap);
8862 if (eap->nextcmd != NULL)
8863 nextcmd = eap->nextcmd;
8864 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008865 else if (eap->cmdidx == CMD_wincmd)
8866 {
8867 p = eap->arg;
8868 if (*p != NUL)
8869 ++p;
8870 if (*p == 'g' || *p == Ctrl_G)
8871 ++p;
8872 p = skipwhite(p);
8873 if (*p == '|')
8874 {
8875 *p = NUL;
8876 nextcmd = p + 1;
8877 }
8878 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008879 }
8880
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008881 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8882 {
8883 // expand filename in "syntax include [@group] filename"
8884 has_expr = TRUE;
8885 eap->arg = skipwhite(eap->arg + 7);
8886 if (*eap->arg == '@')
8887 eap->arg = skiptowhite(eap->arg);
8888 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008889
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008890 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8891 && STRLEN(eap->arg) > 4)
8892 {
8893 int delim = *eap->arg;
8894
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008895 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008896 if (*p == delim)
8897 {
8898 eap->arg = p + 1;
8899 has_expr = TRUE;
8900 }
8901 }
8902
Bram Moolenaarecac5912021-01-05 19:23:28 +01008903 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8904 {
8905 // TODO: should only expand when appropriate for the command
8906 eap->arg = skiptowhite(eap->arg);
8907 has_expr = TRUE;
8908 }
8909
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008910 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008911 {
8912 int count = 0;
8913 char_u *start = skipwhite(line);
8914
8915 // :cmd xxx`=expr1`yyy`=expr2`zzz
8916 // PUSHS ":cmd xxx"
8917 // eval expr1
8918 // PUSHS "yyy"
8919 // eval expr2
8920 // PUSHS "zzz"
8921 // EXECCONCAT 5
8922 for (;;)
8923 {
8924 if (p > start)
8925 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008926 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008927 ++count;
8928 }
8929 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008930 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008931 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008932 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008933 ++count;
8934 p = skipwhite(p);
8935 if (*p != '`')
8936 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008937 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008938 return NULL;
8939 }
8940 start = p + 1;
8941
8942 p = (char_u *)strstr((char *)start, "`=");
8943 if (p == NULL)
8944 {
8945 if (*skipwhite(start) != NUL)
8946 {
8947 generate_PUSHS(cctx, vim_strsave(start));
8948 ++count;
8949 }
8950 break;
8951 }
8952 }
8953 generate_EXECCONCAT(cctx, count);
8954 }
8955 else
8956 generate_EXEC(cctx, line);
8957
8958theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008959 if (*nextcmd != NUL)
8960 {
8961 // the parser expects a pointer to the bar, put it back
8962 --nextcmd;
8963 *nextcmd = '|';
8964 }
8965
8966 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008967}
8968
Bram Moolenaar20677332021-06-06 17:02:53 +02008969/*
8970 * A script command with heredoc, e.g.
8971 * ruby << EOF
8972 * command
8973 * EOF
8974 * Has been turned into one long line with NL characters by
8975 * get_function_body():
8976 * ruby << EOF<NL> command<NL>EOF
8977 */
8978 static char_u *
8979compile_script(char_u *line, cctx_T *cctx)
8980{
8981 if (cctx->ctx_skip != SKIP_YES)
8982 {
8983 isn_T *isn;
8984
8985 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8986 return NULL;
8987 isn->isn_arg.string = vim_strsave(line);
8988 }
8989 return (char_u *)"";
8990}
8991
Bram Moolenaar8238f082021-04-20 21:10:48 +02008992
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008993/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008994 * :s/pat/repl/
8995 */
8996 static char_u *
8997compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8998{
8999 char_u *cmd = eap->arg;
9000 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9001
9002 if (expr != NULL)
9003 {
9004 int delimiter = *cmd++;
9005
9006 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009007 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009008 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9009 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009010 garray_T save_ga = cctx->ctx_instr;
9011 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009012 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009013 int trailing_error;
9014 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009015 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009016 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009017
9018 cmd += 3;
9019 end = skip_substitute(cmd, delimiter);
9020
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009021 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009022 // labels are correct.
9023 cctx->ctx_instr.ga_len = 0;
9024 cctx->ctx_instr.ga_maxlen = 0;
9025 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009026 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009027 if (end[-1] == NUL)
9028 end[-1] = delimiter;
9029 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009030 trailing_error = *cmd != delimiter && *cmd != NUL;
9031
Bram Moolenaar169502f2021-04-21 12:19:35 +02009032 if (expr_res == FAIL || trailing_error
9033 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009034 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009035 if (trailing_error)
9036 semsg(_(e_trailing_arg), cmd);
9037 clear_instr_ga(&cctx->ctx_instr);
9038 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009039 return NULL;
9040 }
9041
Bram Moolenaar8238f082021-04-20 21:10:48 +02009042 // Move the generated instructions into the ISN_SUBSTITUTE
9043 // instructions, then restore the list of instructions before
9044 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009045 instr_count = cctx->ctx_instr.ga_len;
9046 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009047 instr[instr_count].isn_type = ISN_FINISH;
9048
9049 cctx->ctx_instr = save_ga;
9050 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9051 {
9052 int idx;
9053
9054 for (idx = 0; idx < instr_count; ++idx)
9055 delete_instr(instr + idx);
9056 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009057 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009058 }
9059 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9060 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009061
9062 // skip over flags
9063 if (*end == '&')
9064 ++end;
9065 while (ASCII_ISALPHA(*end) || *end == '#')
9066 ++end;
9067 return end;
9068 }
9069 }
9070
9071 return compile_exec(arg, eap, cctx);
9072}
9073
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009074 static char_u *
9075compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9076{
9077 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009078 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009079
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009080 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009081 {
9082 if (STRNCMP(arg, "END", 3) == 0)
9083 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009084 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009085 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009086 // First load the current variable value.
9087 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9088 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009089 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009090 }
9091
9092 // Gets the redirected text and put it on the stack, then store it
9093 // in the variable.
9094 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9095
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009096 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009097 generate_instr_drop(cctx, ISN_CONCAT, 1);
9098
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009099 if (lhs->lhs_has_index)
9100 {
9101 // Use the info in "lhs" to store the value at the index in the
9102 // list or dict.
9103 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9104 &t_string, cctx) == FAIL)
9105 return NULL;
9106 }
9107 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009108 return NULL;
9109
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009110 VIM_CLEAR(lhs->lhs_name);
9111 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009112 return arg + 3;
9113 }
9114 emsg(_(e_cannot_nest_redir));
9115 return NULL;
9116 }
9117
9118 if (arg[0] == '=' && arg[1] == '>')
9119 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009120 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009121
9122 // redirect to a variable is compiled
9123 arg += 2;
9124 if (*arg == '>')
9125 {
9126 ++arg;
9127 append = TRUE;
9128 }
9129 arg = skipwhite(arg);
9130
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009131 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009132 FALSE, FALSE, 1, cctx) == FAIL)
9133 return NULL;
9134 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009135 lhs->lhs_append = append;
9136 if (lhs->lhs_has_index)
9137 {
9138 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9139 if (lhs->lhs_whole == NULL)
9140 return NULL;
9141 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009142
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009143 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009144 }
9145
9146 // other redirects are handled like at script level
9147 return compile_exec(line, eap, cctx);
9148}
9149
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009150#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009151 static char_u *
9152compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9153{
9154 isn_T *isn;
9155 char_u *p;
9156
9157 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9158 if (isn == NULL)
9159 return NULL;
9160 isn->isn_arg.number = eap->cmdidx;
9161
9162 p = eap->arg;
9163 if (compile_expr0(&p, cctx) == FAIL)
9164 return NULL;
9165
9166 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9167 if (isn == NULL)
9168 return NULL;
9169 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9170 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9171 return NULL;
9172 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9173 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9174 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9175
9176 return p;
9177}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009178#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009179
Bram Moolenaar4c137212021-04-19 16:48:48 +02009180/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009181 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009182 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009183 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009184 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009185add_def_function(ufunc_T *ufunc)
9186{
9187 dfunc_T *dfunc;
9188
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009189 if (def_functions.ga_len == 0)
9190 {
9191 // The first position is not used, so that a zero uf_dfunc_idx means it
9192 // wasn't set.
9193 if (ga_grow(&def_functions, 1) == FAIL)
9194 return FAIL;
9195 ++def_functions.ga_len;
9196 }
9197
Bram Moolenaar09689a02020-05-09 22:50:08 +02009198 // Add the function to "def_functions".
9199 if (ga_grow(&def_functions, 1) == FAIL)
9200 return FAIL;
9201 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9202 CLEAR_POINTER(dfunc);
9203 dfunc->df_idx = def_functions.ga_len;
9204 ufunc->uf_dfunc_idx = dfunc->df_idx;
9205 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009206 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009207 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009208 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009209 ++def_functions.ga_len;
9210 return OK;
9211}
9212
9213/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 * After ex_function() has collected all the function lines: parse and compile
9215 * the lines into instructions.
9216 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009217 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9218 * the return statement (used for lambda). When uf_ret_type is already set
9219 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009220 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009221 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009222 * This can be used recursively through compile_lambda(), which may reallocate
9223 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009224 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009226 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009227compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009228 ufunc_T *ufunc,
9229 int check_return_type,
9230 compiletype_T compile_type,
9231 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009232{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009233 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009234 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009237 cctx_T cctx;
9238 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009239 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009240 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009241 int ret = FAIL;
9242 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009243 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009244 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009245 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009246 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009247#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009248 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009249#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009250 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009251
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009252 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009253 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009254 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009256 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9257 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009258 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009259
9260 switch (compile_type)
9261 {
9262 case CT_PROFILE:
9263#ifdef FEAT_PROFILE
9264 instr_dest = dfunc->df_instr_prof; break;
9265#endif
9266 case CT_NONE: instr_dest = dfunc->df_instr; break;
9267 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9268 }
9269 if (instr_dest != NULL)
9270 // Was compiled in this mode before: Free old instructions.
9271 delete_def_function_contents(dfunc, FALSE);
9272 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009273 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009274 else
9275 {
9276 if (add_def_function(ufunc) == FAIL)
9277 return FAIL;
9278 new_def_function = TRUE;
9279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009280
Bram Moolenaar985116a2020-07-12 17:31:09 +02009281 ufunc->uf_def_status = UF_COMPILING;
9282
Bram Moolenaara80faa82020-04-12 19:37:17 +02009283 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009284
Bram Moolenaare99d4222021-06-13 14:01:26 +02009285 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009286 cctx.ctx_ufunc = ufunc;
9287 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009288 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9290 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9291 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9292 cctx.ctx_type_list = &ufunc->uf_type_list;
9293 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9294 instr = &cctx.ctx_instr;
9295
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009296 // Set the context to the function, it may be compiled when called from
9297 // another script. Set the script version to the most modern one.
9298 // The line number will be set in next_line_from_context().
9299 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009300 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9301
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009302 // Don't use the flag from ":legacy" here.
9303 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9304
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009305 // Make sure error messages are OK.
9306 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9307 if (do_estack_push)
9308 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009309 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009310
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009311 if (ufunc->uf_def_args.ga_len > 0)
9312 {
9313 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009314 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009315 int i;
9316 char_u *arg;
9317 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009318 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009319
9320 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009321 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009322 for (i = 0; i < count; ++i)
9323 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009324 garray_T *stack = &cctx.ctx_type_stack;
9325 type_T *val_type;
9326 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009327 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009328 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009329 int jump_instr_idx = instr->ga_len;
9330 isn_T *isn;
9331
9332 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9333 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9334 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009335
9336 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009337 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009338
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009339 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009340 r = compile_expr0(&arg, &cctx);
9341
Bram Moolenaar12bce952021-03-11 20:04:04 +01009342 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009343 goto erret;
9344
9345 // If no type specified use the type of the default value.
9346 // Otherwise check that the default value type matches the
9347 // specified type.
9348 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009349 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009350 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009351 {
9352 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009353 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009354 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009355 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009356 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009357 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009358
9359 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009360 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009361
9362 // set instruction index in JUMP_IF_ARG_SET to here
9363 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9364 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009365 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009366
9367 if (did_set_arg_type)
9368 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009369 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009370 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009371
9372 /*
9373 * Loop over all the lines of the function and generate instructions.
9374 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009375 for (;;)
9376 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009377 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009378 int starts_with_colon = FALSE;
9379 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009380 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009381
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009382 // Bail out on the first error to avoid a flood of errors and report
9383 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009384 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009385 goto erret;
9386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009387 if (line != NULL && *line == '|')
9388 // the line continues after a '|'
9389 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009390 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009391 && !(*line == '#' && (line == cctx.ctx_line_start
9392 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009393 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009394 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009395 goto erret;
9396 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009397 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9398 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009399 else
9400 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009401 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009402 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009403 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009404 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009405#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009406 if (cctx.ctx_skip != SKIP_YES)
9407 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009408#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009410 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009411 // Make a copy, splitting off nextcmd and removing trailing spaces
9412 // may change it.
9413 if (line != NULL)
9414 {
9415 line = vim_strsave(line);
9416 vim_free(line_to_free);
9417 line_to_free = line;
9418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009419 }
9420
Bram Moolenaara80faa82020-04-12 19:37:17 +02009421 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009422 ea.cmdlinep = &line;
9423 ea.cmd = skipwhite(line);
9424
Bram Moolenaarb2049902021-01-24 12:53:53 +01009425 if (*ea.cmd == '#')
9426 {
9427 // "#" starts a comment
9428 line = (char_u *)"";
9429 continue;
9430 }
9431
Bram Moolenaarf002a412021-01-24 13:34:18 +01009432#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009433 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9434 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009435 {
9436 may_generate_prof_end(&cctx, prof_lnum);
9437
9438 prof_lnum = cctx.ctx_lnum;
9439 generate_instr(&cctx, ISN_PROF_START);
9440 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009441#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009442 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9443 && cctx.ctx_skip != SKIP_YES)
9444 {
9445 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009446 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009447 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009448 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009449
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009450 // Some things can be recognized by the first character.
9451 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009452 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009453 case '}':
9454 {
9455 // "}" ends a block scope
9456 scopetype_T stype = cctx.ctx_scope == NULL
9457 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009458
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009459 if (stype == BLOCK_SCOPE)
9460 {
9461 compile_endblock(&cctx);
9462 line = ea.cmd;
9463 }
9464 else
9465 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009466 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009467 goto erret;
9468 }
9469 if (line != NULL)
9470 line = skipwhite(ea.cmd + 1);
9471 continue;
9472 }
9473
9474 case '{':
9475 // "{" starts a block scope
9476 // "{'a': 1}->func() is something else
9477 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9478 {
9479 line = compile_block(ea.cmd, &cctx);
9480 continue;
9481 }
9482 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009483 }
9484
9485 /*
9486 * COMMAND MODIFIERS
9487 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009488 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009489 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9490 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009491 {
9492 if (errormsg != NULL)
9493 goto erret;
9494 // empty line or comment
9495 line = (char_u *)"";
9496 continue;
9497 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009498 generate_cmdmods(&cctx, &local_cmdmod);
9499 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009500
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009501 // Check if there was a colon after the last command modifier or before
9502 // the current position.
9503 for (p = ea.cmd; p >= line; --p)
9504 {
9505 if (*p == ':')
9506 starts_with_colon = TRUE;
9507 if (p < ea.cmd && !VIM_ISWHITE(*p))
9508 break;
9509 }
9510
Bram Moolenaarce024c32021-06-26 13:00:49 +02009511 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009512 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009513 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009514 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009515 if (checkforcmd(&ea.cmd, "call", 3))
9516 {
9517 if (*ea.cmd == '(')
9518 // not for "call()"
9519 ea.cmd = p;
9520 else
9521 ea.cmd = skipwhite(ea.cmd);
9522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009523
Bram Moolenaarce024c32021-06-26 13:00:49 +02009524 if (!starts_with_colon)
9525 {
9526 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009527
Bram Moolenaarce024c32021-06-26 13:00:49 +02009528 // Check for assignment after command modifiers.
9529 assign = may_compile_assignment(&ea, &line, &cctx);
9530 if (assign == OK)
9531 goto nextline;
9532 if (assign == FAIL)
9533 goto erret;
9534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 }
9536
9537 /*
9538 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009539 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009540 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009541 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009542 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009543 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9544 && (starts_with_colon || !(*cmd == '\''
9545 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009546 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009547 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009548 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009549 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009550 if (!starts_with_colon)
9551 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009552 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009553 goto erret;
9554 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009555 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009556 if (ends_excmd2(line, ea.cmd))
9557 {
9558 // A range without a command: jump to the line.
9559 // TODO: compile to a more efficient command, possibly
9560 // calling parse_cmd_address().
9561 ea.cmdidx = CMD_SIZE;
9562 line = compile_exec(line, &ea, &cctx);
9563 goto nextline;
9564 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009565 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009566 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009567 p = find_ex_command(&ea, NULL,
9568 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009569 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009570
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009571 if (p == NULL)
9572 {
9573 if (cctx.ctx_skip != SKIP_YES)
9574 emsg(_(e_ambiguous_use_of_user_defined_command));
9575 goto erret;
9576 }
9577
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009578 // When using ":legacy cmd" always use compile_exec().
9579 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009580 {
9581 char_u *start = ea.cmd;
9582
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009583 switch (ea.cmdidx)
9584 {
9585 case CMD_if:
9586 case CMD_elseif:
9587 case CMD_else:
9588 case CMD_endif:
9589 case CMD_for:
9590 case CMD_endfor:
9591 case CMD_continue:
9592 case CMD_break:
9593 case CMD_while:
9594 case CMD_endwhile:
9595 case CMD_try:
9596 case CMD_catch:
9597 case CMD_finally:
9598 case CMD_endtry:
9599 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9600 goto erret;
9601 default: break;
9602 }
9603
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009604 // ":legacy return expr" needs to be handled differently.
9605 if (checkforcmd(&start, "return", 4))
9606 ea.cmdidx = CMD_return;
9607 else
9608 ea.cmdidx = CMD_legacy;
9609 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009611 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9612 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009613 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009614 {
9615 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009616 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009617 }
9618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009619 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009620 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009621 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009622 // CMD_var cannot happen, compile_assignment() above would be
9623 // used. Most likely an assignment to a non-existing variable.
9624 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009625 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009627 }
9628
Bram Moolenaar3988f642020-08-27 22:43:03 +02009629 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009630 && ea.cmdidx != CMD_elseif
9631 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009632 && ea.cmdidx != CMD_endif
9633 && ea.cmdidx != CMD_endfor
9634 && ea.cmdidx != CMD_endwhile
9635 && ea.cmdidx != CMD_catch
9636 && ea.cmdidx != CMD_finally
9637 && ea.cmdidx != CMD_endtry)
9638 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009639 emsg(_(e_unreachable_code_after_return));
9640 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009641 }
9642
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009643 p = skipwhite(p);
9644 if (ea.cmdidx != CMD_SIZE
9645 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9646 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009647 if (ea.cmdidx >= 0)
9648 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009649 if ((ea.argt & EX_BANG) && *p == '!')
9650 {
9651 ea.forceit = TRUE;
9652 p = skipwhite(p + 1);
9653 }
9654 }
9655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009656 switch (ea.cmdidx)
9657 {
9658 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009659 ea.arg = p;
9660 line = compile_nested_function(&ea, &cctx);
9661 break;
9662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009663 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009664 // TODO: should we allow this, e.g. to declare a global
9665 // function?
9666 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009667 goto erret;
9668
9669 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009670 line = compile_return(p, check_return_type,
9671 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009672 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673 break;
9674
9675 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009676 emsg(_(e_cannot_use_let_in_vim9_script));
9677 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009678 case CMD_var:
9679 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009680 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009681 case CMD_increment:
9682 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009684 if (line == p)
9685 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009686 break;
9687
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009688 case CMD_unlet:
9689 case CMD_unlockvar:
9690 case CMD_lockvar:
9691 line = compile_unletlock(p, &ea, &cctx);
9692 break;
9693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009694 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009695 emsg(_(e_import_can_only_be_used_in_script));
9696 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009697 break;
9698
9699 case CMD_if:
9700 line = compile_if(p, &cctx);
9701 break;
9702 case CMD_elseif:
9703 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009704 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009705 break;
9706 case CMD_else:
9707 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009708 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009709 break;
9710 case CMD_endif:
9711 line = compile_endif(p, &cctx);
9712 break;
9713
9714 case CMD_while:
9715 line = compile_while(p, &cctx);
9716 break;
9717 case CMD_endwhile:
9718 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009719 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009720 break;
9721
9722 case CMD_for:
9723 line = compile_for(p, &cctx);
9724 break;
9725 case CMD_endfor:
9726 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009727 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009728 break;
9729 case CMD_continue:
9730 line = compile_continue(p, &cctx);
9731 break;
9732 case CMD_break:
9733 line = compile_break(p, &cctx);
9734 break;
9735
9736 case CMD_try:
9737 line = compile_try(p, &cctx);
9738 break;
9739 case CMD_catch:
9740 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009741 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009742 break;
9743 case CMD_finally:
9744 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009745 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009746 break;
9747 case CMD_endtry:
9748 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009749 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009750 break;
9751 case CMD_throw:
9752 line = compile_throw(p, &cctx);
9753 break;
9754
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009755 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009756 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009757 break;
9758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009761 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009762 case CMD_echomsg:
9763 case CMD_echoerr:
9764 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009765 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009766
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009767 case CMD_put:
9768 ea.cmd = cmd;
9769 line = compile_put(p, &ea, &cctx);
9770 break;
9771
Bram Moolenaar4c137212021-04-19 16:48:48 +02009772 case CMD_substitute:
9773 if (cctx.ctx_skip == SKIP_YES)
9774 line = (char_u *)"";
9775 else
9776 {
9777 ea.arg = p;
9778 line = compile_substitute(line, &ea, &cctx);
9779 }
9780 break;
9781
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009782 case CMD_redir:
9783 ea.arg = p;
9784 line = compile_redir(line, &ea, &cctx);
9785 break;
9786
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009787 case CMD_cexpr:
9788 case CMD_lexpr:
9789 case CMD_caddexpr:
9790 case CMD_laddexpr:
9791 case CMD_cgetexpr:
9792 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009793#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009794 ea.arg = p;
9795 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009796#else
9797 ex_ni(&ea);
9798 line = NULL;
9799#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009800 break;
9801
Bram Moolenaar3988f642020-08-27 22:43:03 +02009802 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009803
Bram Moolenaarae616492020-07-28 20:07:27 +02009804 case CMD_append:
9805 case CMD_change:
9806 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009807 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009808 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009809 case CMD_xit:
9810 not_in_vim9(&ea);
9811 goto erret;
9812
Bram Moolenaar002262f2020-07-08 17:47:57 +02009813 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009814 if (cctx.ctx_skip != SKIP_YES)
9815 {
9816 semsg(_(e_invalid_command_str), ea.cmd);
9817 goto erret;
9818 }
9819 // We don't check for a next command here.
9820 line = (char_u *)"";
9821 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009822
Bram Moolenaar20677332021-06-06 17:02:53 +02009823 case CMD_lua:
9824 case CMD_mzscheme:
9825 case CMD_perl:
9826 case CMD_py3:
9827 case CMD_python3:
9828 case CMD_python:
9829 case CMD_pythonx:
9830 case CMD_ruby:
9831 case CMD_tcl:
9832 ea.arg = p;
9833 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009834 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009835 else
9836 // heredoc lines have been concatenated with NL
9837 // characters in get_function_body()
9838 line = compile_script(line, &cctx);
9839 break;
9840
9841 default:
9842 // Not recognized, execute with do_cmdline_cmd().
9843 ea.arg = p;
9844 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 break;
9846 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009847nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009848 if (line == NULL)
9849 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009850 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009852 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009853 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009855 if (cctx.ctx_type_stack.ga_len < 0)
9856 {
9857 iemsg("Type stack underflow");
9858 goto erret;
9859 }
9860 }
9861
9862 if (cctx.ctx_scope != NULL)
9863 {
9864 if (cctx.ctx_scope->se_type == IF_SCOPE)
9865 emsg(_(e_endif));
9866 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9867 emsg(_(e_endwhile));
9868 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9869 emsg(_(e_endfor));
9870 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009871 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009872 goto erret;
9873 }
9874
Bram Moolenaarefd88552020-06-18 20:50:10 +02009875 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009876 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009877 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9878 ufunc->uf_ret_type = &t_void;
9879 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009881 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882 goto erret;
9883 }
9884
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009885 // Return void if there is no return at the end.
9886 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887 }
9888
Bram Moolenaar599410c2021-04-10 14:03:43 +02009889 // When compiled with ":silent!" and there was an error don't consider the
9890 // function compiled.
9891 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009892 {
9893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9894 + ufunc->uf_dfunc_idx;
9895 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009896 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009897#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009898 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009899 {
9900 dfunc->df_instr_prof = instr->ga_data;
9901 dfunc->df_instr_prof_count = instr->ga_len;
9902 }
9903 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009904#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009905 if (cctx.ctx_compile_type == CT_DEBUG)
9906 {
9907 dfunc->df_instr_debug = instr->ga_data;
9908 dfunc->df_instr_debug_count = instr->ga_len;
9909 }
9910 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009911 {
9912 dfunc->df_instr = instr->ga_data;
9913 dfunc->df_instr_count = instr->ga_len;
9914 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009915 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009916 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009917 if (cctx.ctx_outer_used)
9918 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009919 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009921
9922 ret = OK;
9923
9924erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009925 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009926 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009927 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9928 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009929
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009930 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009931 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009932 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009933 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009934
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009935 // If using the last entry in the table and it was added above, we
9936 // might as well remove it.
9937 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009938 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009939 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009940 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009941 ufunc->uf_dfunc_idx = 0;
9942 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009943 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009944
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009945 while (cctx.ctx_scope != NULL)
9946 drop_scope(&cctx);
9947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009948 if (errormsg != NULL)
9949 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009950 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009951 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009952 }
9953
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009954 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9955 {
9956 if (ret == OK)
9957 {
9958 emsg(_(e_missing_redir_end));
9959 ret = FAIL;
9960 }
9961 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009962 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009963 }
9964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009965 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009966 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009967 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009968 if (do_estack_push)
9969 estack_pop();
9970
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009971 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009972 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009973 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009975 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009976}
9977
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009978 void
9979set_function_type(ufunc_T *ufunc)
9980{
9981 int varargs = ufunc->uf_va_name != NULL;
9982 int argcount = ufunc->uf_args.ga_len;
9983
9984 // Create a type for the function, with the return type and any
9985 // argument types.
9986 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9987 // The type is included in "tt_args".
9988 if (argcount > 0 || varargs)
9989 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009990 if (ufunc->uf_type_list.ga_itemsize == 0)
9991 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009992 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9993 argcount, &ufunc->uf_type_list);
9994 // Add argument types to the function type.
9995 if (func_type_add_arg_types(ufunc->uf_func_type,
9996 argcount + varargs,
9997 &ufunc->uf_type_list) == FAIL)
9998 return;
9999 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10000 ufunc->uf_func_type->tt_min_argcount =
10001 argcount - ufunc->uf_def_args.ga_len;
10002 if (ufunc->uf_arg_types == NULL)
10003 {
10004 int i;
10005
10006 // lambda does not have argument types.
10007 for (i = 0; i < argcount; ++i)
10008 ufunc->uf_func_type->tt_args[i] = &t_any;
10009 }
10010 else
10011 mch_memmove(ufunc->uf_func_type->tt_args,
10012 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10013 if (varargs)
10014 {
10015 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010016 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010017 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10018 }
10019 }
10020 else
10021 // No arguments, can use a predefined type.
10022 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10023 argcount, &ufunc->uf_type_list);
10024}
10025
10026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010027/*
10028 * Delete an instruction, free what it contains.
10029 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010030 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010031delete_instr(isn_T *isn)
10032{
10033 switch (isn->isn_type)
10034 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010035 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010036 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010037 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010038 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010039 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010040 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010041 case ISN_LOADENV:
10042 case ISN_LOADG:
10043 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010044 case ISN_LOADT:
10045 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010046 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010047 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010049 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010050 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010051 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010052 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010054 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010055 case ISN_STOREW:
10056 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010057 vim_free(isn->isn_arg.string);
10058 break;
10059
Bram Moolenaar4c137212021-04-19 16:48:48 +020010060 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010061 {
10062 int idx;
10063 isn_T *list = isn->isn_arg.subs.subs_instr;
10064
10065 vim_free(isn->isn_arg.subs.subs_cmd);
10066 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10067 delete_instr(list + idx);
10068 vim_free(list);
10069 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010070 break;
10071
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010072 case ISN_INSTR:
10073 {
10074 int idx;
10075 isn_T *list = isn->isn_arg.instr;
10076
10077 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10078 delete_instr(list + idx);
10079 vim_free(list);
10080 }
10081 break;
10082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010083 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010084 case ISN_STORES:
10085 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010086 break;
10087
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010088 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010089 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010090 vim_free(isn->isn_arg.unlet.ul_name);
10091 break;
10092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010093 case ISN_STOREOPT:
10094 vim_free(isn->isn_arg.storeopt.so_name);
10095 break;
10096
10097 case ISN_PUSHBLOB: // push blob isn_arg.blob
10098 blob_unref(isn->isn_arg.blob);
10099 break;
10100
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010101 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010103 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010104#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010105 break;
10106
10107 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010108#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010109 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010110#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010111 break;
10112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010113 case ISN_UCALL:
10114 vim_free(isn->isn_arg.ufunc.cuf_name);
10115 break;
10116
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010117 case ISN_FUNCREF:
10118 {
10119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10120 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010121 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010122
Bram Moolenaar077a4232020-12-22 18:33:27 +010010123 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10124 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010125 }
10126 break;
10127
10128 case ISN_DCALL:
10129 {
10130 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10131 + isn->isn_arg.dfunc.cdf_idx;
10132
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010133 if (dfunc->df_ufunc != NULL
10134 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010135 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010136 }
10137 break;
10138
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010139 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010140 {
10141 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10142 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10143
10144 if (ufunc != NULL)
10145 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010146 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010147 func_ptr_unref(ufunc);
10148 }
10149
10150 vim_free(lambda);
10151 vim_free(isn->isn_arg.newfunc.nf_global);
10152 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010153 break;
10154
Bram Moolenaar5e654232020-09-16 15:22:00 +020010155 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010156 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010157 free_type(isn->isn_arg.type.ct_type);
10158 break;
10159
Bram Moolenaar02194d22020-10-24 23:08:38 +020010160 case ISN_CMDMOD:
10161 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10162 ->cmod_filter_regmatch.regprog);
10163 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10164 break;
10165
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010166 case ISN_LOADSCRIPT:
10167 case ISN_STORESCRIPT:
10168 vim_free(isn->isn_arg.script.scriptref);
10169 break;
10170
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010171 case ISN_TRY:
10172 vim_free(isn->isn_arg.try.try_ref);
10173 break;
10174
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010175 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010176 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010177 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10178 break;
10179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010180 case ISN_2BOOL:
10181 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010182 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010183 case ISN_ADDBLOB:
10184 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010185 case ISN_ANYINDEX:
10186 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010187 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010188 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010189 case ISN_BLOBINDEX:
10190 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010191 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010192 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010193 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010194 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010195 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196 case ISN_COMPAREANY:
10197 case ISN_COMPAREBLOB:
10198 case ISN_COMPAREBOOL:
10199 case ISN_COMPAREDICT:
10200 case ISN_COMPAREFLOAT:
10201 case ISN_COMPAREFUNC:
10202 case ISN_COMPARELIST:
10203 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010204 case ISN_COMPARESPECIAL:
10205 case ISN_COMPARESTRING:
10206 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010207 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010208 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 case ISN_DROP:
10210 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010211 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010212 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010213 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010214 case ISN_EXECCONCAT:
10215 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010216 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010217 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010218 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010219 case ISN_GETITEM:
10220 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010221 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010222 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010223 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010224 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010225 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010226 case ISN_LOADBDICT:
10227 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010228 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010229 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010230 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010231 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010232 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010233 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010234 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010235 case ISN_NEGATENR:
10236 case ISN_NEWDICT:
10237 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010238 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010239 case ISN_OPFLOAT:
10240 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010242 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010243 case ISN_PROF_END:
10244 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010245 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010246 case ISN_PUSHF:
10247 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010249 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010250 case ISN_REDIREND:
10251 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010252 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010253 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010254 case ISN_SHUFFLE:
10255 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010256 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010257 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010258 case ISN_STORENR:
10259 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010260 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010261 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010262 case ISN_STOREV:
10263 case ISN_STRINDEX:
10264 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010265 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010266 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010267 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010268 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010269 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010270 // nothing allocated
10271 break;
10272 }
10273}
10274
10275/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010276 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010277 */
10278 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010279delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010280{
10281 int idx;
10282
10283 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010284 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010285
10286 if (dfunc->df_instr != NULL)
10287 {
10288 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10289 delete_instr(dfunc->df_instr + idx);
10290 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010291 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010292 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010293 if (dfunc->df_instr_debug != NULL)
10294 {
10295 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10296 delete_instr(dfunc->df_instr_debug + idx);
10297 VIM_CLEAR(dfunc->df_instr_debug);
10298 dfunc->df_instr_debug = NULL;
10299 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010300#ifdef FEAT_PROFILE
10301 if (dfunc->df_instr_prof != NULL)
10302 {
10303 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10304 delete_instr(dfunc->df_instr_prof + idx);
10305 VIM_CLEAR(dfunc->df_instr_prof);
10306 dfunc->df_instr_prof = NULL;
10307 }
10308#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010309
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010310 if (mark_deleted)
10311 dfunc->df_deleted = TRUE;
10312 if (dfunc->df_ufunc != NULL)
10313 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010314}
10315
10316/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010317 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010318 * function, unless another user function still uses it.
10319 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010320 */
10321 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010322unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010323{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010324 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010325 {
10326 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10327 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010328
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010329 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010330 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010331 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010332 ufunc->uf_dfunc_idx = 0;
10333 if (dfunc->df_ufunc == ufunc)
10334 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010335 }
10336}
10337
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010338/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010339 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010340 */
10341 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010342link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010343{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010344 if (ufunc->uf_dfunc_idx > 0)
10345 {
10346 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10347 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010348
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010349 ++dfunc->df_refcount;
10350 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010351}
10352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010353#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010354/*
10355 * Free all functions defined with ":def".
10356 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010357 void
10358free_def_functions(void)
10359{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010360 int idx;
10361
10362 for (idx = 0; idx < def_functions.ga_len; ++idx)
10363 {
10364 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10365
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010366 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010367 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010368 }
10369
10370 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010371}
10372#endif
10373
10374
10375#endif // FEAT_EVAL