blob: 5a39906efb4dd3825efeb02993ca36feac434a0b [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 Moolenaarf785aa12021-02-11 21:19:34 +01001036 where_T where;
1037
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;
1048 where.wt_variable = FALSE;
1049 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001050 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001051
1052 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001053 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001054 if ((!actual_is_const || actual == &t_any)
1055 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001056 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001057 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001058 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001059 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001060
1061 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001062 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001063 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001064}
1065
1066/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001067 * Check that the top of the type stack has a type that can be used as a
1068 * condition. Give an error and return FAIL if not.
1069 */
1070 static int
1071bool_on_stack(cctx_T *cctx)
1072{
1073 garray_T *stack = &cctx->ctx_type_stack;
1074 type_T *type;
1075
1076 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1077 if (type == &t_bool)
1078 return OK;
1079
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001080 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001081 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1082 // This requires a runtime type check.
1083 return generate_COND2BOOL(cctx);
1084
Bram Moolenaar351ead02021-01-16 16:07:01 +01001085 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001086}
1087
1088/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 * Generate an ISN_PUSHNR instruction.
1090 */
1091 static int
1092generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1093{
1094 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001095 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096
Bram Moolenaar080457c2020-03-03 21:53:32 +01001097 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1099 return FAIL;
1100 isn->isn_arg.number = number;
1101
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001102 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001103 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001104 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_PUSHBOOL instruction.
1110 */
1111 static int
1112generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1113{
1114 isn_T *isn;
1115
Bram Moolenaar080457c2020-03-03 21:53:32 +01001116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.number = number;
1120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_PUSHSPEC instruction.
1126 */
1127 static int
1128generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1129{
1130 isn_T *isn;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.number = number;
1136
1137 return OK;
1138}
1139
1140#ifdef FEAT_FLOAT
1141/*
1142 * Generate an ISN_PUSHF instruction.
1143 */
1144 static int
1145generate_PUSHF(cctx_T *cctx, float_T fnumber)
1146{
1147 isn_T *isn;
1148
Bram Moolenaar080457c2020-03-03 21:53:32 +01001149 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1151 return FAIL;
1152 isn->isn_arg.fnumber = fnumber;
1153
1154 return OK;
1155}
1156#endif
1157
1158/*
1159 * Generate an ISN_PUSHS instruction.
1160 * Consumes "str".
1161 */
1162 static int
1163generate_PUSHS(cctx_T *cctx, char_u *str)
1164{
1165 isn_T *isn;
1166
Bram Moolenaar508b5612021-01-02 18:17:26 +01001167 if (cctx->ctx_skip == SKIP_YES)
1168 {
1169 vim_free(str);
1170 return OK;
1171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1173 return FAIL;
1174 isn->isn_arg.string = str;
1175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001180 * Generate an ISN_PUSHCHANNEL instruction.
1181 * Consumes "channel".
1182 */
1183 static int
1184generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001189 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.channel = channel;
1192
1193 return OK;
1194}
1195
1196/*
1197 * Generate an ISN_PUSHJOB instruction.
1198 * Consumes "job".
1199 */
1200 static int
1201generate_PUSHJOB(cctx_T *cctx, job_T *job)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001206 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001207 return FAIL;
1208 isn->isn_arg.job = job;
1209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 * Generate an ISN_PUSHBLOB instruction.
1215 * Consumes "blob".
1216 */
1217 static int
1218generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1219{
1220 isn_T *isn;
1221
Bram Moolenaar080457c2020-03-03 21:53:32 +01001222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1224 return FAIL;
1225 isn->isn_arg.blob = blob;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001231 * Generate an ISN_PUSHFUNC instruction with name "name".
1232 * Consumes "name".
1233 */
1234 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001235generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001236{
1237 isn_T *isn;
1238
Bram Moolenaar080457c2020-03-03 21:53:32 +01001239 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001240 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001241 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001242 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001248 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001249 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1250 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001251 */
1252 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001253generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001257 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1258 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001259 type_T *item_type = &t_any;
1260
1261 RETURN_OK_IF_SKIP(cctx);
1262
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001263 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001264 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001265 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001266 emsg(_(e_listreq));
1267 return FAIL;
1268 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001269 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001270 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1271 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001272 isn->isn_arg.getitem.gi_index = index;
1273 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001274
1275 // add the item type to the type stack
1276 if (ga_grow(stack, 1) == FAIL)
1277 return FAIL;
1278 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1279 ++stack->ga_len;
1280 return OK;
1281}
1282
1283/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001284 * Generate an ISN_SLICE instruction with "count".
1285 */
1286 static int
1287generate_SLICE(cctx_T *cctx, int count)
1288{
1289 isn_T *isn;
1290
1291 RETURN_OK_IF_SKIP(cctx);
1292 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1293 return FAIL;
1294 isn->isn_arg.number = count;
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_CHECKLEN instruction with "min_len".
1300 */
1301 static int
1302generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307
1308 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1309 return FAIL;
1310 isn->isn_arg.checklen.cl_min_len = min_len;
1311 isn->isn_arg.checklen.cl_more_OK = more_OK;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 * Generate an ISN_STORE instruction.
1318 */
1319 static int
1320generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1321{
1322 isn_T *isn;
1323
Bram Moolenaar080457c2020-03-03 21:53:32 +01001324 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1326 return FAIL;
1327 if (name != NULL)
1328 isn->isn_arg.string = vim_strsave(name);
1329 else
1330 isn->isn_arg.number = idx;
1331
1332 return OK;
1333}
1334
1335/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001336 * Generate an ISN_STOREOUTER instruction.
1337 */
1338 static int
1339generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1340{
1341 isn_T *isn;
1342
1343 RETURN_OK_IF_SKIP(cctx);
1344 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.outer.outer_idx = idx;
1347 isn->isn_arg.outer.outer_depth = level;
1348
1349 return OK;
1350}
1351
1352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1354 */
1355 static int
1356generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1357{
1358 isn_T *isn;
1359
Bram Moolenaar080457c2020-03-03 21:53:32 +01001360 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1362 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001363 isn->isn_arg.storenr.stnr_idx = idx;
1364 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_STOREOPT instruction
1371 */
1372 static int
1373generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1374{
1375 isn_T *isn;
1376
Bram Moolenaar080457c2020-03-03 21:53:32 +01001377 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001378 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 return FAIL;
1380 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1381 isn->isn_arg.storeopt.so_flags = opt_flags;
1382
1383 return OK;
1384}
1385
1386/*
1387 * Generate an ISN_LOAD or similar instruction.
1388 */
1389 static int
1390generate_LOAD(
1391 cctx_T *cctx,
1392 isntype_T isn_type,
1393 int idx,
1394 char_u *name,
1395 type_T *type)
1396{
1397 isn_T *isn;
1398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1401 return FAIL;
1402 if (name != NULL)
1403 isn->isn_arg.string = vim_strsave(name);
1404 else
1405 isn->isn_arg.number = idx;
1406
1407 return OK;
1408}
1409
1410/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001411 * Generate an ISN_LOADOUTER instruction
1412 */
1413 static int
1414generate_LOADOUTER(
1415 cctx_T *cctx,
1416 int idx,
1417 int nesting,
1418 type_T *type)
1419{
1420 isn_T *isn;
1421
1422 RETURN_OK_IF_SKIP(cctx);
1423 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1424 return FAIL;
1425 isn->isn_arg.outer.outer_idx = idx;
1426 isn->isn_arg.outer.outer_depth = nesting;
1427
1428 return OK;
1429}
1430
1431/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001432 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001433 */
1434 static int
1435generate_LOADV(
1436 cctx_T *cctx,
1437 char_u *name,
1438 int error)
1439{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001440 int di_flags;
1441 int vidx = find_vim_var(name, &di_flags);
1442 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445 if (vidx < 0)
1446 {
1447 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001448 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001449 return FAIL;
1450 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001451 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001452
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001453 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454}
1455
1456/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001457 * Generate an ISN_UNLET instruction.
1458 */
1459 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001460generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001461{
1462 isn_T *isn;
1463
1464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001465 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001466 return FAIL;
1467 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1468 isn->isn_arg.unlet.ul_forceit = forceit;
1469
1470 return OK;
1471}
1472
1473/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001474 * Generate an ISN_LOCKCONST instruction.
1475 */
1476 static int
1477generate_LOCKCONST(cctx_T *cctx)
1478{
1479 isn_T *isn;
1480
1481 RETURN_OK_IF_SKIP(cctx);
1482 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1483 return FAIL;
1484 return OK;
1485}
1486
1487/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 * Generate an ISN_LOADS instruction.
1489 */
1490 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001491generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 int sid,
1496 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497{
1498 isn_T *isn;
1499
Bram Moolenaar080457c2020-03-03 21:53:32 +01001500 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001501 if (isn_type == ISN_LOADS)
1502 isn = generate_instr_type(cctx, isn_type, type);
1503 else
1504 isn = generate_instr_drop(cctx, isn_type, 1);
1505 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001507 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1508 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509
1510 return OK;
1511}
1512
1513/*
1514 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1515 */
1516 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001517generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 cctx_T *cctx,
1519 isntype_T isn_type,
1520 int sid,
1521 int idx,
1522 type_T *type)
1523{
1524 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001525 scriptref_T *sref;
1526 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527
Bram Moolenaar080457c2020-03-03 21:53:32 +01001528 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 if (isn_type == ISN_LOADSCRIPT)
1530 isn = generate_instr_type(cctx, isn_type, type);
1531 else
1532 isn = generate_instr_drop(cctx, isn_type, 1);
1533 if (isn == NULL)
1534 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001535
1536 // This requires three arguments, which doesn't fit in an instruction, thus
1537 // we need to allocate a struct for this.
1538 sref = ALLOC_ONE(scriptref_T);
1539 if (sref == NULL)
1540 return FAIL;
1541 isn->isn_arg.script.scriptref = sref;
1542 sref->sref_sid = sid;
1543 sref->sref_idx = idx;
1544 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001545 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 return OK;
1547}
1548
1549/*
1550 * Generate an ISN_NEWLIST instruction.
1551 */
1552 static int
1553generate_NEWLIST(cctx_T *cctx, int count)
1554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 type_T *type;
1558 type_T *member;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.number = count;
1564
Bram Moolenaar127542b2020-08-09 17:22:04 +02001565 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001566 if (count == 0)
1567 member = &t_void;
1568 else
1569 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001570 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1571 cctx->ctx_type_list);
1572 type = get_list_type(member, cctx->ctx_type_list);
1573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 // drop the value types
1575 stack->ga_len -= count;
1576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 // add the list type to the type stack
1578 if (ga_grow(stack, 1) == FAIL)
1579 return FAIL;
1580 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1581 ++stack->ga_len;
1582
1583 return OK;
1584}
1585
1586/*
1587 * Generate an ISN_NEWDICT instruction.
1588 */
1589 static int
1590generate_NEWDICT(cctx_T *cctx, int count)
1591{
1592 isn_T *isn;
1593 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 type_T *type;
1595 type_T *member;
1596
Bram Moolenaar080457c2020-03-03 21:53:32 +01001597 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1599 return FAIL;
1600 isn->isn_arg.number = count;
1601
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001602 if (count == 0)
1603 member = &t_void;
1604 else
1605 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001606 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1607 cctx->ctx_type_list);
1608 type = get_dict_type(member, cctx->ctx_type_list);
1609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 // drop the key and value types
1611 stack->ga_len -= 2 * count;
1612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 // add the dict type to the type stack
1614 if (ga_grow(stack, 1) == FAIL)
1615 return FAIL;
1616 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1617 ++stack->ga_len;
1618
1619 return OK;
1620}
1621
1622/*
1623 * Generate an ISN_FUNCREF instruction.
1624 */
1625 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001626generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627{
1628 isn_T *isn;
1629 garray_T *stack = &cctx->ctx_type_stack;
1630
Bram Moolenaar080457c2020-03-03 21:53:32 +01001631 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1633 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001634 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001635 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001637 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001638 // the nested context, including this one.
1639 if (ufunc->uf_flags & FC_CLOSURE)
1640 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 if (ga_grow(stack, 1) == FAIL)
1643 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001644 ((type_T **)stack->ga_data)[stack->ga_len] =
1645 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 ++stack->ga_len;
1647
1648 return OK;
1649}
1650
1651/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001652 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001653 * "lambda_name" and "func_name" must be in allocated memory and will be
1654 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001655 */
1656 static int
1657generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1658{
1659 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001660
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001661 if (cctx->ctx_skip == SKIP_YES)
1662 {
1663 vim_free(lambda_name);
1664 vim_free(func_name);
1665 return OK;
1666 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001667 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001668 {
1669 vim_free(lambda_name);
1670 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001671 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001672 }
1673 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001674 isn->isn_arg.newfunc.nf_global = func_name;
1675
1676 return OK;
1677}
1678
1679/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001680 * Generate an ISN_DEF instruction: list functions
1681 */
1682 static int
1683generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1684{
1685 isn_T *isn;
1686
1687 RETURN_OK_IF_SKIP(cctx);
1688 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1689 return FAIL;
1690 if (len > 0)
1691 {
1692 isn->isn_arg.string = vim_strnsave(name, len);
1693 if (isn->isn_arg.string == NULL)
1694 return FAIL;
1695 }
1696 return OK;
1697}
1698
1699/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 * Generate an ISN_JUMP instruction.
1701 */
1702 static int
1703generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1704{
1705 isn_T *isn;
1706 garray_T *stack = &cctx->ctx_type_stack;
1707
Bram Moolenaar080457c2020-03-03 21:53:32 +01001708 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1710 return FAIL;
1711 isn->isn_arg.jump.jump_when = when;
1712 isn->isn_arg.jump.jump_where = where;
1713
1714 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1715 --stack->ga_len;
1716
1717 return OK;
1718}
1719
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001720/*
1721 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1722 */
1723 static int
1724generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1725{
1726 isn_T *isn;
1727
1728 RETURN_OK_IF_SKIP(cctx);
1729 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1730 return FAIL;
1731 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1732 // jump_where is set later
1733 return OK;
1734}
1735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 static int
1737generate_FOR(cctx_T *cctx, int loop_idx)
1738{
1739 isn_T *isn;
1740 garray_T *stack = &cctx->ctx_type_stack;
1741
Bram Moolenaar080457c2020-03-03 21:53:32 +01001742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1744 return FAIL;
1745 isn->isn_arg.forloop.for_idx = loop_idx;
1746
1747 if (ga_grow(stack, 1) == FAIL)
1748 return FAIL;
1749 // type doesn't matter, will be stored next
1750 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1751 ++stack->ga_len;
1752
1753 return OK;
1754}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001755/*
1756 * Generate an ISN_TRYCONT instruction.
1757 */
1758 static int
1759generate_TRYCONT(cctx_T *cctx, int levels, int where)
1760{
1761 isn_T *isn;
1762
1763 RETURN_OK_IF_SKIP(cctx);
1764 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1765 return FAIL;
1766 isn->isn_arg.trycont.tct_levels = levels;
1767 isn->isn_arg.trycont.tct_where = where;
1768
1769 return OK;
1770}
1771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772
1773/*
1774 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001775 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 * Return FAIL if the number of arguments is wrong.
1777 */
1778 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001779generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780{
1781 isn_T *isn;
1782 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001783 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001784 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001785 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001786 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
Bram Moolenaar080457c2020-03-03 21:53:32 +01001788 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001789 argoff = check_internal_func(func_idx, argcount);
1790 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return FAIL;
1792
Bram Moolenaar389df252020-07-09 21:20:47 +02001793 if (method_call && argoff > 1)
1794 {
1795 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.shuffle.shfl_item = argcount;
1798 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1799 }
1800
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001801 if (argcount > 0)
1802 {
1803 // Check the types of the arguments.
1804 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001805 if (method_call && argoff > 1)
1806 {
1807 int i;
1808
1809 for (i = 0; i < argcount; ++i)
1810 shuffled_argtypes[i] = (i < argoff - 1)
1811 ? argtypes[i + 1]
1812 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1813 argtypes = shuffled_argtypes;
1814 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001815 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1816 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001817 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001818 if (internal_func_is_map(func_idx))
1819 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001820 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1823 return FAIL;
1824 isn->isn_arg.bfunc.cbf_idx = func_idx;
1825 isn->isn_arg.bfunc.cbf_argcount = argcount;
1826
Bram Moolenaar94738d82020-10-21 14:25:07 +02001827 // Drop the argument types and push the return type.
1828 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if (ga_grow(stack, 1) == FAIL)
1830 return FAIL;
1831 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001832 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001833 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001835 if (maptype != NULL && maptype->tt_member != NULL
1836 && maptype->tt_member != &t_any)
1837 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001838 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 return OK;
1841}
1842
1843/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001844 * Generate an ISN_LISTAPPEND instruction. Works like add().
1845 * Argument count is already checked.
1846 */
1847 static int
1848generate_LISTAPPEND(cctx_T *cctx)
1849{
1850 garray_T *stack = &cctx->ctx_type_stack;
1851 type_T *list_type;
1852 type_T *item_type;
1853 type_T *expected;
1854
1855 // Caller already checked that list_type is a list.
1856 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1857 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1858 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001859 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001860 return FAIL;
1861
1862 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1863 return FAIL;
1864
1865 --stack->ga_len; // drop the argument
1866 return OK;
1867}
1868
1869/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001870 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1871 * Argument count is already checked.
1872 */
1873 static int
1874generate_BLOBAPPEND(cctx_T *cctx)
1875{
1876 garray_T *stack = &cctx->ctx_type_stack;
1877 type_T *item_type;
1878
1879 // Caller already checked that blob_type is a blob.
1880 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001881 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001882 return FAIL;
1883
1884 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1885 return FAIL;
1886
1887 --stack->ga_len; // drop the argument
1888 return OK;
1889}
1890
1891/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001892 * Return TRUE if "ufunc" should be compiled, taking into account whether
1893 * "profile" indicates profiling is to be done.
1894 */
1895 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001896func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001897{
1898 switch (ufunc->uf_def_status)
1899 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001900 case UF_TO_BE_COMPILED:
1901 return TRUE;
1902
Bram Moolenaarb2049902021-01-24 12:53:53 +01001903 case UF_COMPILED:
1904 {
1905 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1906 + ufunc->uf_dfunc_idx;
1907
Bram Moolenaare99d4222021-06-13 14:01:26 +02001908 switch (compile_type)
1909 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001910 case CT_PROFILE:
1911#ifdef FEAT_PROFILE
1912 return dfunc->df_instr_prof == NULL;
1913#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001914 case CT_NONE:
1915 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001916 case CT_DEBUG:
1917 return dfunc->df_instr_debug == NULL;
1918 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001919 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001920
1921 case UF_NOT_COMPILED:
1922 case UF_COMPILE_ERROR:
1923 case UF_COMPILING:
1924 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001925 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001926 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001927}
1928
1929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 * Generate an ISN_DCALL or ISN_UCALL instruction.
1931 * Return FAIL if the number of arguments is wrong.
1932 */
1933 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001934generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935{
1936 isn_T *isn;
1937 garray_T *stack = &cctx->ctx_type_stack;
1938 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001939 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940
Bram Moolenaar080457c2020-03-03 21:53:32 +01001941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 if (argcount > regular_args && !has_varargs(ufunc))
1943 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001944 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 return FAIL;
1946 }
1947 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1948 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001949 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 return FAIL;
1951 }
1952
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001953 if (ufunc->uf_def_status != UF_NOT_COMPILED
1954 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001955 {
1956 int i;
1957
1958 for (i = 0; i < argcount; ++i)
1959 {
1960 type_T *expected;
1961 type_T *actual;
1962
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001963 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1964 if (actual == &t_special
1965 && i >= regular_args - ufunc->uf_def_args.ga_len)
1966 {
1967 // assume v:none used for default argument value
1968 continue;
1969 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001970 if (i < regular_args)
1971 {
1972 if (ufunc->uf_arg_types == NULL)
1973 continue;
1974 expected = ufunc->uf_arg_types[i];
1975 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001976 else if (ufunc->uf_va_type == NULL
1977 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001978 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001979 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001980 else
1981 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001982 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001983 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001984 {
1985 arg_type_mismatch(expected, actual, i + 1);
1986 return FAIL;
1987 }
1988 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001989 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001990 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001991 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001992 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001993 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001994 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1995 {
1996 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1997 ufunc->uf_name);
1998 return FAIL;
1999 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002002 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002003 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002005 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 {
2007 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2008 isn->isn_arg.dfunc.cdf_argcount = argcount;
2009 }
2010 else
2011 {
2012 // A user function may be deleted and redefined later, can't use the
2013 // ufunc pointer, need to look it up again at runtime.
2014 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2015 isn->isn_arg.ufunc.cuf_argcount = argcount;
2016 }
2017
2018 stack->ga_len -= argcount; // drop the arguments
2019 if (ga_grow(stack, 1) == FAIL)
2020 return FAIL;
2021 // add return value
2022 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2023 ++stack->ga_len;
2024
2025 return OK;
2026}
2027
2028/*
2029 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2030 */
2031 static int
2032generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2033{
2034 isn_T *isn;
2035 garray_T *stack = &cctx->ctx_type_stack;
2036
Bram Moolenaar080457c2020-03-03 21:53:32 +01002037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2039 return FAIL;
2040 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2041 isn->isn_arg.ufunc.cuf_argcount = argcount;
2042
2043 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002044 if (ga_grow(stack, 1) == FAIL)
2045 return FAIL;
2046 // add return value
2047 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2048 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049
2050 return OK;
2051}
2052
2053/*
2054 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002055 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 */
2057 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002058generate_PCALL(
2059 cctx_T *cctx,
2060 int argcount,
2061 char_u *name,
2062 type_T *type,
2063 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064{
2065 isn_T *isn;
2066 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002067 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068
Bram Moolenaar080457c2020-03-03 21:53:32 +01002069 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002070
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002071 if (type->tt_type == VAR_ANY)
2072 ret_type = &t_any;
2073 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002074 {
2075 if (type->tt_argcount != -1)
2076 {
2077 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2078
2079 if (argcount < type->tt_min_argcount - varargs)
2080 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002081 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002082 return FAIL;
2083 }
2084 if (!varargs && argcount > type->tt_argcount)
2085 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002086 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002087 return FAIL;
2088 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002089 if (type->tt_args != NULL)
2090 {
2091 int i;
2092
2093 for (i = 0; i < argcount; ++i)
2094 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002095 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002096 type_T *actual = ((type_T **)stack->ga_data)[
2097 stack->ga_len + offset];
2098 type_T *expected;
2099
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002100 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002101 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002102 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002103 else if (i >= type->tt_min_argcount
2104 && actual == &t_special)
2105 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002106 else
2107 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002108 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002109 cctx, TRUE, FALSE) == FAIL)
2110 {
2111 arg_type_mismatch(expected, actual, i + 1);
2112 return FAIL;
2113 }
2114 }
2115 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002116 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002117 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002118 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002119 else
2120 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002121 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002122 return FAIL;
2123 }
2124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2126 return FAIL;
2127 isn->isn_arg.pfunc.cpf_top = at_top;
2128 isn->isn_arg.pfunc.cpf_argcount = argcount;
2129
2130 stack->ga_len -= argcount; // drop the arguments
2131
2132 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002133 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002135 // If partial is above the arguments it must be cleared and replaced with
2136 // the return value.
2137 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2138 return FAIL;
2139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 return OK;
2141}
2142
2143/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002144 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 */
2146 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002147generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148{
2149 isn_T *isn;
2150 garray_T *stack = &cctx->ctx_type_stack;
2151 type_T *type;
2152
Bram Moolenaar080457c2020-03-03 21:53:32 +01002153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002154 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002156 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002158 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002160 if (type->tt_type != VAR_DICT && type != &t_any)
2161 {
2162 emsg(_(e_dictreq));
2163 return FAIL;
2164 }
2165 // change dict type to dict member type
2166 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002167 {
2168 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2169 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171
2172 return OK;
2173}
2174
2175/*
2176 * Generate an ISN_ECHO instruction.
2177 */
2178 static int
2179generate_ECHO(cctx_T *cctx, int with_white, int count)
2180{
2181 isn_T *isn;
2182
Bram Moolenaar080457c2020-03-03 21:53:32 +01002183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2185 return FAIL;
2186 isn->isn_arg.echo.echo_with_white = with_white;
2187 isn->isn_arg.echo.echo_count = count;
2188
2189 return OK;
2190}
2191
Bram Moolenaarad39c092020-02-26 18:23:43 +01002192/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002193 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002194 */
2195 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002196generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002197{
2198 isn_T *isn;
2199
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002200 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002201 return FAIL;
2202 isn->isn_arg.number = count;
2203
2204 return OK;
2205}
2206
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002207/*
2208 * Generate an ISN_PUT instruction.
2209 */
2210 static int
2211generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2212{
2213 isn_T *isn;
2214
2215 RETURN_OK_IF_SKIP(cctx);
2216 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2217 return FAIL;
2218 isn->isn_arg.put.put_regname = regname;
2219 isn->isn_arg.put.put_lnum = lnum;
2220 return OK;
2221}
2222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 static int
2224generate_EXEC(cctx_T *cctx, char_u *line)
2225{
2226 isn_T *isn;
2227
Bram Moolenaar080457c2020-03-03 21:53:32 +01002228 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2230 return FAIL;
2231 isn->isn_arg.string = vim_strsave(line);
2232 return OK;
2233}
2234
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002235 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002236generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2237{
2238 isn_T *isn;
2239 garray_T *stack = &cctx->ctx_type_stack;
2240
2241 RETURN_OK_IF_SKIP(cctx);
2242 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2243 return FAIL;
2244 isn->isn_arg.string = vim_strsave(line);
2245
2246 if (ga_grow(stack, 1) == FAIL)
2247 return FAIL;
2248 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2249 ++stack->ga_len;
2250
2251 return OK;
2252}
2253
2254 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002255generate_EXECCONCAT(cctx_T *cctx, int count)
2256{
2257 isn_T *isn;
2258
2259 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2260 return FAIL;
2261 isn->isn_arg.number = count;
2262 return OK;
2263}
2264
Bram Moolenaar08597872020-12-10 19:43:40 +01002265/*
2266 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2267 */
2268 static int
2269generate_RANGE(cctx_T *cctx, char_u *range)
2270{
2271 isn_T *isn;
2272 garray_T *stack = &cctx->ctx_type_stack;
2273
2274 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2275 return FAIL;
2276 isn->isn_arg.string = range;
2277
2278 if (ga_grow(stack, 1) == FAIL)
2279 return FAIL;
2280 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2281 ++stack->ga_len;
2282 return OK;
2283}
2284
Bram Moolenaar792f7862020-11-23 08:31:18 +01002285 static int
2286generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2287{
2288 isn_T *isn;
2289
2290 RETURN_OK_IF_SKIP(cctx);
2291 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2292 return FAIL;
2293 isn->isn_arg.unpack.unp_count = var_count;
2294 isn->isn_arg.unpack.unp_semicolon = semicolon;
2295 return OK;
2296}
2297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002299 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002300 */
2301 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002302generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002303{
2304 isn_T *isn;
2305
Bram Moolenaarfa984412021-03-25 22:15:28 +01002306 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002307 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002308 cctx->ctx_has_cmdmod = TRUE;
2309
2310 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002311 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002312 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2313 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2314 return FAIL;
2315 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002316 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002317 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002318 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002319
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002320 return OK;
2321}
2322
2323 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002324generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002325{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002326 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2327 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002328 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002329 return OK;
2330}
2331
Bram Moolenaarfa984412021-03-25 22:15:28 +01002332 static int
2333misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002334{
2335 garray_T *instr = &cctx->ctx_instr;
2336
Bram Moolenaara91a7132021-03-25 21:12:15 +01002337 if (cctx->ctx_has_cmdmod
2338 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2339 == ISN_CMDMOD)
2340 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002341 emsg(_(e_misplaced_command_modifier));
2342 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002343 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002344 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002345}
2346
2347/*
2348 * Get the index of the current instruction.
2349 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2350 */
2351 static int
2352current_instr_idx(cctx_T *cctx)
2353{
2354 garray_T *instr = &cctx->ctx_instr;
2355 int idx = instr->ga_len;
2356
Bram Moolenaare99d4222021-06-13 14:01:26 +02002357 while (idx > 0)
2358 {
2359 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002360 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002361 {
2362 --idx;
2363 continue;
2364 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002365#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002366 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2367 {
2368 --idx;
2369 continue;
2370 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002371#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002372 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2373 {
2374 --idx;
2375 continue;
2376 }
2377 break;
2378 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002379 return idx;
2380}
2381
Bram Moolenaarf002a412021-01-24 13:34:18 +01002382#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002383 static void
2384may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2385{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002386 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002387 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002388}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002389#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002390
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002391/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002393 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002395 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002396reserve_local(
2397 cctx_T *cctx,
2398 char_u *name,
2399 size_t len,
2400 int isConst,
2401 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002404 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002406 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002408 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002409 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411
2412 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002413 return NULL;
2414 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002415 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417 // Every local variable uses the next entry on the stack. We could re-use
2418 // the last ones when leaving a scope, but then variables used in a closure
2419 // might get overwritten. To keep things simple do not re-use stack
2420 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002421 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2422 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002423
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002424 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 lvar->lv_const = isConst;
2426 lvar->lv_type = type;
2427
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002428 // Remember the name for debugging.
2429 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2430 return NULL;
2431 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2432 vim_strsave(lvar->lv_name);
2433 ++dfunc->df_var_names.ga_len;
2434
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002435 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436}
2437
2438/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002439 * Remove local variables above "new_top".
2440 */
2441 static void
2442unwind_locals(cctx_T *cctx, int new_top)
2443{
2444 if (cctx->ctx_locals.ga_len > new_top)
2445 {
2446 int idx;
2447 lvar_T *lvar;
2448
2449 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2450 {
2451 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2452 vim_free(lvar->lv_name);
2453 }
2454 }
2455 cctx->ctx_locals.ga_len = new_top;
2456}
2457
2458/*
2459 * Free all local variables.
2460 */
2461 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002462free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002463{
2464 unwind_locals(cctx, 0);
2465 ga_clear(&cctx->ctx_locals);
2466}
2467
2468/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002469 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2470 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2471 * error if the variable was defined with :const.
2472 */
2473 static int
2474check_item_writable(svar_T *sv, int check_writable, char_u *name)
2475{
2476 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2477 || (check_writable == ASSIGN_FINAL
2478 && sv->sv_const == ASSIGN_CONST))
2479 {
2480 semsg(_(e_readonlyvar), name);
2481 return FAIL;
2482 }
2483 return OK;
2484}
2485
2486/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002488 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 * Returns the index in "sn_var_vals" if found.
2490 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002491 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 */
2493 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002494get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495{
2496 hashtab_T *ht;
2497 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002498 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002499 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 int idx;
2501
Bram Moolenaare3d46852020-08-29 13:39:17 +02002502 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002504 if (sid == current_sctx.sc_sid)
2505 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002506 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002507
2508 if (sav == NULL)
2509 return -2;
2510 idx = sav->sav_var_vals_idx;
2511 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002512 if (check_item_writable(sv, check_writable, name) == FAIL)
2513 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 return idx;
2515 }
2516
2517 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 ht = &SCRIPT_VARS(sid);
2519 di = find_var_in_ht(ht, 0, name, TRUE);
2520 if (di == NULL)
2521 return -2;
2522
2523 // Now find the svar_T index in sn_var_vals.
2524 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2525 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002526 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 if (sv->sv_tv == &di->di_tv)
2528 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002529 if (check_item_writable(sv, check_writable, name) == FAIL)
2530 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 return idx;
2532 }
2533 }
2534 return -1;
2535}
2536
2537/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002538 * Find "name" in imported items of the current script or in "cctx" if not
2539 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 */
2541 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002542find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 int idx;
2545
Bram Moolenaare3d46852020-08-29 13:39:17 +02002546 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002547 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 if (cctx != NULL)
2549 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2550 {
2551 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2552 + idx;
2553
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002554 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2555 : STRLEN(import->imp_name) == len
2556 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 return import;
2558 }
2559
Bram Moolenaarefa94442020-08-08 22:16:00 +02002560 return find_imported_in_script(name, len, current_sctx.sc_sid);
2561}
2562
2563 imported_T *
2564find_imported_in_script(char_u *name, size_t len, int sid)
2565{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002566 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002567 int idx;
2568
Bram Moolenaare3d46852020-08-29 13:39:17 +02002569 if (!SCRIPT_ID_VALID(sid))
2570 return NULL;
2571 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2573 {
2574 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2575
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002576 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2577 : STRLEN(import->imp_name) == len
2578 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 return import;
2580 }
2581 return NULL;
2582}
2583
2584/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002585 * Free all imported variables.
2586 */
2587 static void
2588free_imported(cctx_T *cctx)
2589{
2590 int idx;
2591
2592 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2593 {
2594 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2595
2596 vim_free(import->imp_name);
2597 }
2598 ga_clear(&cctx->ctx_imports);
2599}
2600
2601/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002602 * Return a pointer to the next line that isn't empty or only contains a
2603 * comment. Skips over white space.
2604 * Returns NULL if there is none.
2605 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002606 char_u *
2607peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002608{
2609 int lnum = cctx->ctx_lnum;
2610
2611 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2612 {
2613 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002614 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002615
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002616 // ignore NULLs inserted for continuation lines
2617 if (line != NULL)
2618 {
2619 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002620 if (vim9_bad_comment(p))
2621 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002622 if (*p != NUL && !vim9_comment_start(p))
2623 return p;
2624 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002625 }
2626 return NULL;
2627}
2628
2629/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002630 * Called when checking for a following operator at "arg". When the rest of
2631 * the line is empty or only a comment, peek the next line. If there is a next
2632 * line return a pointer to it and set "nextp".
2633 * Otherwise skip over white space.
2634 */
2635 static char_u *
2636may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2637{
2638 char_u *p = skipwhite(arg);
2639
2640 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002641 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002642 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002643 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002644 if (*nextp != NULL)
2645 return *nextp;
2646 }
2647 return p;
2648}
2649
2650/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002651 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002652 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 * Returns NULL when at the end.
2654 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002655 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002656next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002657{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002658 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002659
2660 do
2661 {
2662 ++cctx->ctx_lnum;
2663 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002664 {
2665 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002666 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002667 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002668 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002669 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002670 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002671 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002672 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002673 return line;
2674}
2675
2676/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002677 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002678 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002679 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002680 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2681 */
2682 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002683may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002684{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002685 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002686 if (vim9_bad_comment(*arg))
2687 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002688 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002689 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002690 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002691
2692 if (next == NULL)
2693 return FAIL;
2694 *arg = skipwhite(next);
2695 }
2696 return OK;
2697}
2698
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002699/*
2700 * Idem, and give an error when failed.
2701 */
2702 static int
2703may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2704{
2705 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2706 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002707 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002708 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002709 return FAIL;
2710 }
2711 return OK;
2712}
2713
2714
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715// Structure passed between the compile_expr* functions to keep track of
2716// constants that have been parsed but for which no code was produced yet. If
2717// possible expressions on these constants are applied at compile time. If
2718// that is not possible, the code to push the constants needs to be generated
2719// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002720// Using 50 should be more than enough of 5 levels of ().
2721#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002722typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002723 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002725 int pp_is_const; // all generated code was constants, used for a
2726 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002727} ppconst_T;
2728
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002729static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002730static int compile_expr0(char_u **arg, cctx_T *cctx);
2731static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2732
Bram Moolenaara5565e42020-05-09 15:44:01 +02002733/*
2734 * Generate a PUSH instruction for "tv".
2735 * "tv" will be consumed or cleared.
2736 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2737 */
2738 static int
2739generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2740{
2741 if (tv != NULL)
2742 {
2743 switch (tv->v_type)
2744 {
2745 case VAR_UNKNOWN:
2746 break;
2747 case VAR_BOOL:
2748 generate_PUSHBOOL(cctx, tv->vval.v_number);
2749 break;
2750 case VAR_SPECIAL:
2751 generate_PUSHSPEC(cctx, tv->vval.v_number);
2752 break;
2753 case VAR_NUMBER:
2754 generate_PUSHNR(cctx, tv->vval.v_number);
2755 break;
2756#ifdef FEAT_FLOAT
2757 case VAR_FLOAT:
2758 generate_PUSHF(cctx, tv->vval.v_float);
2759 break;
2760#endif
2761 case VAR_BLOB:
2762 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2763 tv->vval.v_blob = NULL;
2764 break;
2765 case VAR_STRING:
2766 generate_PUSHS(cctx, tv->vval.v_string);
2767 tv->vval.v_string = NULL;
2768 break;
2769 default:
2770 iemsg("constant type not supported");
2771 clear_tv(tv);
2772 return FAIL;
2773 }
2774 tv->v_type = VAR_UNKNOWN;
2775 }
2776 return OK;
2777}
2778
2779/*
2780 * Generate code for any ppconst entries.
2781 */
2782 static int
2783generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2784{
2785 int i;
2786 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002787 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002788
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002789 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002790 for (i = 0; i < ppconst->pp_used; ++i)
2791 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2792 ret = FAIL;
2793 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002794 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002795 return ret;
2796}
2797
2798/*
2799 * Clear ppconst constants. Used when failing.
2800 */
2801 static void
2802clear_ppconst(ppconst_T *ppconst)
2803{
2804 int i;
2805
2806 for (i = 0; i < ppconst->pp_used; ++i)
2807 clear_tv(&ppconst->pp_tv[i]);
2808 ppconst->pp_used = 0;
2809}
2810
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002811/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002812 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002813 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002814 */
2815 static int
2816compile_member(int is_slice, cctx_T *cctx)
2817{
2818 type_T **typep;
2819 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002820 vartype_T vartype;
2821 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002822
Bram Moolenaar261417b2021-05-06 21:04:55 +02002823 // We can index a list, dict and blob. If we don't know the type
2824 // we can use the index value type. If we still don't know use an "ANY"
2825 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002826 typep = ((type_T **)stack->ga_data) + stack->ga_len
2827 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002828 vartype = (*typep)->tt_type;
2829 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002830 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002831 if (*typep == &t_any && idxtype == &t_string)
2832 vartype = VAR_DICT;
2833 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002834 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002835 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002836 return FAIL;
2837 if (is_slice)
2838 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002839 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2840 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002841 FALSE, FALSE) == FAIL)
2842 return FAIL;
2843 }
2844 }
2845
Bram Moolenaar261417b2021-05-06 21:04:55 +02002846 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002847 {
2848 if (is_slice)
2849 {
2850 emsg(_(e_cannot_slice_dictionary));
2851 return FAIL;
2852 }
2853 if ((*typep)->tt_type == VAR_DICT)
2854 {
2855 *typep = (*typep)->tt_member;
2856 if (*typep == &t_unknown)
2857 // empty dict was used
2858 *typep = &t_any;
2859 }
2860 else
2861 {
2862 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2863 FALSE, FALSE) == FAIL)
2864 return FAIL;
2865 *typep = &t_any;
2866 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002867 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 return FAIL;
2869 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2870 return FAIL;
2871 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002872 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002873 {
2874 *typep = &t_string;
2875 if ((is_slice
2876 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2877 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2878 return FAIL;
2879 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002881 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002882 if (is_slice)
2883 {
2884 *typep = &t_blob;
2885 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2886 return FAIL;
2887 }
2888 else
2889 {
2890 *typep = &t_number;
2891 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2892 return FAIL;
2893 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002894 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002895 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002896 {
2897 if (is_slice)
2898 {
2899 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002900 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002901 2) == FAIL)
2902 return FAIL;
2903 }
2904 else
2905 {
2906 if ((*typep)->tt_type == VAR_LIST)
2907 {
2908 *typep = (*typep)->tt_member;
2909 if (*typep == &t_unknown)
2910 // empty list was used
2911 *typep = &t_any;
2912 }
2913 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002914 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2915 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002916 return FAIL;
2917 }
2918 }
2919 else
2920 {
2921 emsg(_(e_string_list_dict_or_blob_required));
2922 return FAIL;
2923 }
2924 return OK;
2925}
2926
2927/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002928 * Generate an instruction to load script-local variable "name", without the
2929 * leading "s:".
2930 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 */
2932 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002933compile_load_scriptvar(
2934 cctx_T *cctx,
2935 char_u *name, // variable NUL terminated
2936 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002937 char_u **end, // end of variable
2938 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002940 scriptitem_T *si;
2941 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 imported_T *import;
2943
Bram Moolenaare3d46852020-08-29 13:39:17 +02002944 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2945 return FAIL;
2946 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002947 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002948 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002950 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002951 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2952 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 }
2954 if (idx >= 0)
2955 {
2956 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2957
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002958 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 current_sctx.sc_sid, idx, sv->sv_type);
2960 return OK;
2961 }
2962
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002963 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 if (import != NULL)
2965 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002966 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002967 {
2968 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002969 char_u *exp_name;
2970 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002971 ufunc_T *ufunc;
2972 type_T *type;
2973
2974 // Used "import * as Name", need to lookup the member.
2975 if (*p != '.')
2976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002977 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002978 return FAIL;
2979 }
2980 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002981 if (VIM_ISWHITE(*p))
2982 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002983 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002984 return FAIL;
2985 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002986
Bram Moolenaar1c991142020-07-04 13:15:31 +02002987 // isolate one name
2988 exp_name = p;
2989 while (eval_isnamec(*p))
2990 ++p;
2991 cc = *p;
2992 *p = NUL;
2993
Bram Moolenaaredba7072021-03-13 21:14:18 +01002994 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2995 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002996 *p = cc;
2997 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002998 *end = p;
2999
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003000 if (idx < 0)
3001 {
3002 if (*p == '(' && ufunc != NULL)
3003 {
3004 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3005 return OK;
3006 }
3007 return FAIL;
3008 }
3009
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003010 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3011 import->imp_sid,
3012 idx,
3013 type);
3014 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003015 else if (import->imp_funcname != NULL)
3016 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003017 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003018 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3019 import->imp_sid,
3020 import->imp_var_vals_idx,
3021 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 return OK;
3023 }
3024
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003025 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003026 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 return FAIL;
3028}
3029
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003030 static int
3031generate_funcref(cctx_T *cctx, char_u *name)
3032{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003033 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003034
3035 if (ufunc == NULL)
3036 return FAIL;
3037
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003038 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003039 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3040 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003041 == FAIL)
3042 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003043 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003044}
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046/*
3047 * Compile a variable name into a load instruction.
3048 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003049 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 * When "error" is FALSE do not give an error when not found.
3051 */
3052 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003053compile_load(
3054 char_u **arg,
3055 char_u *end_arg,
3056 cctx_T *cctx,
3057 int is_expr,
3058 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059{
3060 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003061 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003062 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003064 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065
3066 if (*(*arg + 1) == ':')
3067 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003068 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003069 {
3070 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071
Bram Moolenaarfa596382021-04-07 21:58:16 +02003072 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003073 switch (**arg)
3074 {
3075 case 'g': isn_type = ISN_LOADGDICT; break;
3076 case 'w': isn_type = ISN_LOADWDICT; break;
3077 case 't': isn_type = ISN_LOADTDICT; break;
3078 case 'b': isn_type = ISN_LOADBDICT; break;
3079 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003080 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003081 goto theend;
3082 }
3083 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3084 goto theend;
3085 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 else
3088 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003089 isntype_T isn_type = ISN_DROP;
3090
Bram Moolenaarfa596382021-04-07 21:58:16 +02003091 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003092 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3093 if (name == NULL)
3094 return FAIL;
3095
3096 switch (**arg)
3097 {
3098 case 'v': res = generate_LOADV(cctx, name, error);
3099 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003100 case 's': if (is_expr && ASCII_ISUPPER(*name)
3101 && find_func(name, FALSE, cctx) != NULL)
3102 res = generate_funcref(cctx, name);
3103 else
3104 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003105 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003106 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003107 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003108 {
3109 if (is_expr && ASCII_ISUPPER(*name)
3110 && find_func(name, FALSE, cctx) != NULL)
3111 res = generate_funcref(cctx, name);
3112 else
3113 isn_type = ISN_LOADG;
3114 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003115 else
3116 {
3117 isn_type = ISN_LOADAUTO;
3118 vim_free(name);
3119 name = vim_strnsave(*arg, end - *arg);
3120 if (name == NULL)
3121 return FAIL;
3122 }
3123 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003124 case 'w': isn_type = ISN_LOADW; break;
3125 case 't': isn_type = ISN_LOADT; break;
3126 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003127 default: // cannot happen, just in case
3128 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003129 goto theend;
3130 }
3131 if (isn_type != ISN_DROP)
3132 {
3133 // Global, Buffer-local, Window-local and Tabpage-local
3134 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003135 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003136 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 }
3139 }
3140 else
3141 {
3142 size_t len = end - *arg;
3143 int idx;
3144 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003145 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
3147 name = vim_strnsave(*arg, end - *arg);
3148 if (name == NULL)
3149 return FAIL;
3150
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003151 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3152 {
3153 script_autoload(name, FALSE);
3154 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3155 }
3156 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3157 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003159 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003160 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 }
3162 else
3163 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003164 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003165
Bram Moolenaar709664c2020-12-12 14:33:41 +01003166 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003168 type = lvar.lv_type;
3169 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003170 if (lvar.lv_from_outer != 0)
3171 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003172 else
3173 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 }
3175 else
3176 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003177 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003178 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003179 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003180 || find_imported(name, 0, cctx) != NULL)
3181 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003182
Bram Moolenaar0f769812020-09-12 18:32:34 +02003183 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003184 // uppercase letter it can be a user defined function.
3185 // generate_funcref() will fail if the function can't be found.
3186 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003187 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189 }
3190 if (gen_load)
3191 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003192 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003193 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003194 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003195 cctx->ctx_outer_used = TRUE;
3196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 }
3198
3199 *arg = end;
3200
3201theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003202 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003203 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 vim_free(name);
3205 return res;
3206}
3207
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003208 static void
3209clear_instr_ga(garray_T *gap)
3210{
3211 int idx;
3212
3213 for (idx = 0; idx < gap->ga_len; ++idx)
3214 delete_instr(((isn_T *)gap->ga_data) + idx);
3215 ga_clear(gap);
3216}
3217
3218/*
3219 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3220 * Returns FAIL if compilation fails.
3221 */
3222 static int
3223compile_string(isn_T *isn, cctx_T *cctx)
3224{
3225 char_u *s = isn->isn_arg.string;
3226 garray_T save_ga = cctx->ctx_instr;
3227 int expr_res;
3228 int trailing_error;
3229 int instr_count;
3230 isn_T *instr = NULL;
3231
3232 // Temporarily reset the list of instructions so that the jump labels are
3233 // correct.
3234 cctx->ctx_instr.ga_len = 0;
3235 cctx->ctx_instr.ga_maxlen = 0;
3236 cctx->ctx_instr.ga_data = NULL;
3237 expr_res = compile_expr0(&s, cctx);
3238 s = skipwhite(s);
3239 trailing_error = *s != NUL;
3240
Bram Moolenaarff652882021-05-16 15:24:49 +02003241 if (expr_res == FAIL || trailing_error
3242 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003243 {
3244 if (trailing_error)
3245 semsg(_(e_trailing_arg), s);
3246 clear_instr_ga(&cctx->ctx_instr);
3247 cctx->ctx_instr = save_ga;
3248 return FAIL;
3249 }
3250
3251 // Move the generated instructions into the ISN_INSTR instruction, then
3252 // restore the list of instructions.
3253 instr_count = cctx->ctx_instr.ga_len;
3254 instr = cctx->ctx_instr.ga_data;
3255 instr[instr_count].isn_type = ISN_FINISH;
3256
3257 cctx->ctx_instr = save_ga;
3258 vim_free(isn->isn_arg.string);
3259 isn->isn_type = ISN_INSTR;
3260 isn->isn_arg.instr = instr;
3261 return OK;
3262}
3263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264/*
3265 * Compile the argument expressions.
3266 * "arg" points to just after the "(" and is advanced to after the ")"
3267 */
3268 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003269compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003271 char_u *p = *arg;
3272 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003273 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003274 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
Bram Moolenaare6085c52020-04-12 20:19:16 +02003276 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003278 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3279 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003280 if (*p == ')')
3281 {
3282 *arg = p + 1;
3283 return OK;
3284 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003285 if (must_end)
3286 {
3287 semsg(_(e_missing_comma_before_argument_str), p);
3288 return FAIL;
3289 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003290
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003291 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003292 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 return FAIL;
3294 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003295
Bram Moolenaarff652882021-05-16 15:24:49 +02003296 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003297 && cctx->ctx_instr.ga_len == instr_count + 1)
3298 {
3299 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3300
3301 // {skip} argument of searchpair() can be compiled if not empty
3302 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3303 compile_string(isn, cctx);
3304 }
3305
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003306 if (*p != ',' && *skipwhite(p) == ',')
3307 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003308 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003309 p = skipwhite(p);
3310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003312 {
3313 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003314 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003315 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003316 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003317 else
3318 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003319 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003320 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003322failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003323 emsg(_(e_missing_close));
3324 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325}
3326
3327/*
3328 * Compile a function call: name(arg1, arg2)
3329 * "arg" points to "name", "arg + varlen" to the "(".
3330 * "argcount_init" is 1 for "value->method()"
3331 * Instructions:
3332 * EVAL arg1
3333 * EVAL arg2
3334 * BCALL / DCALL / UCALL
3335 */
3336 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003337compile_call(
3338 char_u **arg,
3339 size_t varlen,
3340 cctx_T *cctx,
3341 ppconst_T *ppconst,
3342 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343{
3344 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003345 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 int argcount = argcount_init;
3347 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003348 char_u fname_buf[FLEN_FIXED + 1];
3349 char_u *tofree = NULL;
3350 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003351 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003352 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003353 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003354 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355
Bram Moolenaara5565e42020-05-09 15:44:01 +02003356 // we can evaluate "has('name')" at compile time
3357 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3358 {
3359 char_u *s = skipwhite(*arg + varlen + 1);
3360 typval_T argvars[2];
3361
3362 argvars[0].v_type = VAR_UNKNOWN;
3363 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003364 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003365 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003366 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003367 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003368 if (*s == ')' && argvars[0].v_type == VAR_STRING
3369 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003370 {
3371 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3372
3373 *arg = s + 1;
3374 argvars[1].v_type = VAR_UNKNOWN;
3375 tv->v_type = VAR_NUMBER;
3376 tv->vval.v_number = 0;
3377 f_has(argvars, tv);
3378 clear_tv(&argvars[0]);
3379 ++ppconst->pp_used;
3380 return OK;
3381 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003382 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003383 }
3384
3385 if (generate_ppconst(cctx, ppconst) == FAIL)
3386 return FAIL;
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 if (varlen >= sizeof(namebuf))
3389 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003390 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 return FAIL;
3392 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003393 vim_strncpy(namebuf, *arg, varlen);
3394 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003396 // We handle the "skip" argument of searchpair() and searchpairpos()
3397 // differently.
3398 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3399 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3400 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3401 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003404 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003405 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406
Bram Moolenaar03290b82020-12-19 16:30:44 +01003407 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003408 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 {
3410 int idx;
3411
3412 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003413 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003415 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003416 if (STRCMP(name, "flatten") == 0)
3417 {
3418 emsg(_(e_cannot_use_flatten_in_vim9_script));
3419 goto theend;
3420 }
3421
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003422 if (STRCMP(name, "add") == 0 && argcount == 2)
3423 {
3424 garray_T *stack = &cctx->ctx_type_stack;
3425 type_T *type = ((type_T **)stack->ga_data)[
3426 stack->ga_len - 2];
3427
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003428 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003429 if (type->tt_type == VAR_LIST)
3430 {
3431 // inline "add(list, item)" so that the type can be checked
3432 res = generate_LISTAPPEND(cctx);
3433 idx = -1;
3434 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003435 else if (type->tt_type == VAR_BLOB)
3436 {
3437 // inline "add(blob, nr)" so that the type can be checked
3438 res = generate_BLOBAPPEND(cctx);
3439 idx = -1;
3440 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003441 }
3442
3443 if (idx >= 0)
3444 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3445 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003446 else
3447 semsg(_(e_unknownfunc), namebuf);
3448 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 }
3450
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003451 // An argument or local variable can be a function reference, this
3452 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003453 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003454 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003455 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003456 // If we can find the function by name generate the right call.
3457 // Skip global functions here, a local funcref takes precedence.
3458 ufunc = find_func(name, FALSE, cctx);
3459 if (ufunc != NULL && !func_is_global(ufunc))
3460 {
3461 res = generate_CALL(cctx, ufunc, argcount);
3462 goto theend;
3463 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465
3466 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003467 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003468 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003470 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003471 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003472 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003473 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003474 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003475
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003476 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003477 goto theend;
3478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaar0f769812020-09-12 18:32:34 +02003480 // If we can find a global function by name generate the right call.
3481 if (ufunc != NULL)
3482 {
3483 res = generate_CALL(cctx, ufunc, argcount);
3484 goto theend;
3485 }
3486
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003487 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003488 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003489 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003490 res = generate_UCALL(cctx, name, argcount);
3491 else
3492 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003493
3494theend:
3495 vim_free(tofree);
3496 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497}
3498
3499// like NAMESPACE_CHAR but with 'a' and 'l'.
3500#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3501
3502/*
3503 * Find the end of a variable or function name. Unlike find_name_end() this
3504 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003505 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 * Return a pointer to just after the name. Equal to "arg" if there is no
3507 * valid name.
3508 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003509 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003510to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511{
3512 char_u *p;
3513
3514 // Quick check for valid starting character.
3515 if (!eval_isnamec1(*arg))
3516 return arg;
3517
3518 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3519 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3520 // and can be used in slice "[n:]".
3521 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003522 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3524 break;
3525 return p;
3526}
3527
3528/*
3529 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003530 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 */
3532 char_u *
3533to_name_const_end(char_u *arg)
3534{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003535 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 typval_T rettv;
3537
3538 if (p == arg && *arg == '[')
3539 {
3540
3541 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003542 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 p = arg;
3544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 return p;
3546}
3547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548/*
3549 * parse a list: [expr, expr]
3550 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003551 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 */
3553 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003554compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555{
3556 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003557 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003559 int is_const;
3560 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003564 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003565 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003566 semsg(_(e_list_end), *arg);
3567 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003568 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003569 if (*p == ',')
3570 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003571 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003572 return FAIL;
3573 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003574 if (*p == ']')
3575 {
3576 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003577 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003578 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003579 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003580 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003581 if (!is_const)
3582 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 ++count;
3584 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003585 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003587 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3588 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003589 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003590 return FAIL;
3591 }
3592 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003593 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 p = skipwhite(p);
3595 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003596 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003598 ppconst->pp_is_const = is_all_const;
3599 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600}
3601
3602/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003603 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003604 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003605 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 */
3607 static int
3608compile_lambda(char_u **arg, cctx_T *cctx)
3609{
Bram Moolenaare462f522020-12-27 14:43:30 +01003610 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 typval_T rettv;
3612 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003613 evalarg_T evalarg;
3614
3615 CLEAR_FIELD(evalarg);
3616 evalarg.eval_flags = EVAL_EVALUATE;
3617 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618
3619 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003620 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3621 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003622 {
3623 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003624 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003625 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003626
Bram Moolenaar65c44152020-12-24 15:14:01 +01003627 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003629 ++ufunc->uf_refcount;
3630 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631
Bram Moolenaara9931532021-06-12 15:58:16 +02003632 // Compile it here to get the return type. The return type is optional,
3633 // when it's missing use t_unknown. This is recognized in
3634 // compile_return().
3635 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3636 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003637 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar648594e2021-07-11 17:55:01 +02003639#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003640 // When the outer function is compiled for profiling, the lambda may be
3641 // called without profiling. Compile it here in the right context.
3642 if (cctx->ctx_compile_type == CT_PROFILE)
3643 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003644#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003645
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003646 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3647 // points into it. Point to the original line to avoid a dangling pointer.
3648 if (evalarg.eval_tofree_cmdline != NULL)
3649 {
3650 size_t off = *arg - evalarg.eval_tofree_cmdline;
3651
3652 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3653 + off;
3654 }
3655
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003656 clear_evalarg(&evalarg, NULL);
3657
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003658 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003659 {
3660 // The return type will now be known.
3661 set_function_type(ufunc);
3662
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003663 // The function reference count will be 1. When the ISN_FUNCREF
3664 // instruction is deleted the reference count is decremented and the
3665 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003666 return generate_FUNCREF(cctx, ufunc);
3667 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003668
3669 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 return FAIL;
3671}
3672
3673/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003674 * Get a lambda and compile it. Uses Vim9 syntax.
3675 */
3676 int
3677get_lambda_tv_and_compile(
3678 char_u **arg,
3679 typval_T *rettv,
3680 int types_optional,
3681 evalarg_T *evalarg)
3682{
3683 int r;
3684 ufunc_T *ufunc;
3685 int save_sc_version = current_sctx.sc_version;
3686
3687 // Get the funcref in "rettv".
3688 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3689 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3690 current_sctx.sc_version = save_sc_version;
3691 if (r != OK)
3692 return r;
3693
3694 // "rettv" will now be a partial referencing the function.
3695 ufunc = rettv->vval.v_partial->pt_func;
3696
3697 // Compile it here to get the return type. The return type is optional,
3698 // when it's missing use t_unknown. This is recognized in
3699 // compile_return().
3700 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3701 ufunc->uf_ret_type = &t_unknown;
3702 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3703
3704 if (ufunc->uf_def_status == UF_COMPILED)
3705 {
3706 // The return type will now be known.
3707 set_function_type(ufunc);
3708 return OK;
3709 }
3710 clear_tv(rettv);
3711 return FAIL;
3712}
3713
3714/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003715 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003717 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 */
3719 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003720compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721{
3722 garray_T *instr = &cctx->ctx_instr;
3723 int count = 0;
3724 dict_T *d = dict_alloc();
3725 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003726 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003727 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003728 int is_const;
3729 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730
3731 if (d == NULL)
3732 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003733 if (generate_ppconst(cctx, ppconst) == FAIL)
3734 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003735 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003737 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738
Bram Moolenaar23c55272020-06-21 16:58:13 +02003739 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003740 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003741 *arg = NULL;
3742 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003743 }
3744
3745 if (**arg == '}')
3746 break;
3747
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003748 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003750 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003752 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003753 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003754 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003757 if (isn->isn_type == ISN_PUSHNR)
3758 {
3759 char buf[NUMBUFLEN];
3760
3761 // Convert to string at compile time.
3762 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3763 isn->isn_type = ISN_PUSHS;
3764 isn->isn_arg.string = vim_strsave((char_u *)buf);
3765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 if (isn->isn_type == ISN_PUSHS)
3767 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003768 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003769 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003770 *arg = skipwhite(*arg);
3771 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003772 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003773 emsg(_(e_missing_matching_bracket_after_dict_key));
3774 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003775 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003776 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003778 else
3779 {
3780 // {"name": value},
3781 // {'name': value},
3782 // {name: value} use "name" as a literal key
3783 key = get_literal_key(arg);
3784 if (key == NULL)
3785 return FAIL;
3786 if (generate_PUSHS(cctx, key) == FAIL)
3787 return FAIL;
3788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 // Check for duplicate keys, if using string keys.
3791 if (key != NULL)
3792 {
3793 item = dict_find(d, key, -1);
3794 if (item != NULL)
3795 {
3796 semsg(_(e_duplicate_key), key);
3797 goto failret;
3798 }
3799 item = dictitem_alloc(key);
3800 if (item != NULL)
3801 {
3802 item->di_tv.v_type = VAR_UNKNOWN;
3803 item->di_tv.v_lock = 0;
3804 if (dict_add(d, item) == FAIL)
3805 dictitem_free(item);
3806 }
3807 }
3808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 if (**arg != ':')
3810 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003811 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003812 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003813 else
3814 semsg(_(e_missing_dict_colon), *arg);
3815 return FAIL;
3816 }
3817 whitep = *arg + 1;
3818 if (!IS_WHITE_OR_NUL(*whitep))
3819 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003820 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 return FAIL;
3822 }
3823
Bram Moolenaar23c55272020-06-21 16:58:13 +02003824 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003825 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003826 *arg = NULL;
3827 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003828 }
3829
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003830 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003832 if (!is_const)
3833 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 ++count;
3835
Bram Moolenaar2c330432020-04-13 14:41:35 +02003836 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003837 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003838 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003839 *arg = NULL;
3840 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (**arg == '}')
3843 break;
3844 if (**arg != ',')
3845 {
3846 semsg(_(e_missing_dict_comma), *arg);
3847 goto failret;
3848 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003849 if (IS_WHITE_OR_NUL(*whitep))
3850 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003851 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003852 return FAIL;
3853 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003854 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003855 if (!IS_WHITE_OR_NUL(*whitep))
3856 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003857 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003858 return FAIL;
3859 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003860 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 }
3862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 *arg = *arg + 1;
3864
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003865 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003866 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003867 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003868 *arg += STRLEN(*arg);
3869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003871 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return generate_NEWDICT(cctx, count);
3873
3874failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003875 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003876 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003877 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003878 *arg = (char_u *)"";
3879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 dict_unref(d);
3881 return FAIL;
3882}
3883
3884/*
3885 * Compile "&option".
3886 */
3887 static int
3888compile_get_option(char_u **arg, cctx_T *cctx)
3889{
3890 typval_T rettv;
3891 char_u *start = *arg;
3892 int ret;
3893
3894 // parse the option and get the current value to get the type.
3895 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003896 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 if (ret == OK)
3898 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003899 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003900 char_u *name = vim_strnsave(start, *arg - start);
3901 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3902 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903
3904 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3905 vim_free(name);
3906 }
3907 clear_tv(&rettv);
3908
3909 return ret;
3910}
3911
3912/*
3913 * Compile "$VAR".
3914 */
3915 static int
3916compile_get_env(char_u **arg, cctx_T *cctx)
3917{
3918 char_u *start = *arg;
3919 int len;
3920 int ret;
3921 char_u *name;
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 ++*arg;
3924 len = get_env_len(arg);
3925 if (len == 0)
3926 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003927 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 return FAIL;
3929 }
3930
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003931 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 name = vim_strnsave(start, len + 1);
3933 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3934 vim_free(name);
3935 return ret;
3936}
3937
3938/*
3939 * Compile "@r".
3940 */
3941 static int
3942compile_get_register(char_u **arg, cctx_T *cctx)
3943{
3944 int ret;
3945
3946 ++*arg;
3947 if (**arg == NUL)
3948 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003949 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 return FAIL;
3951 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003952 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 {
3954 emsg_invreg(**arg);
3955 return FAIL;
3956 }
3957 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3958 ++*arg;
3959 return ret;
3960}
3961
3962/*
3963 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003964 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 */
3966 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003967apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970
3971 // this works from end to start
3972 while (p > start)
3973 {
3974 --p;
3975 if (*p == '-' || *p == '+')
3976 {
3977 // only '-' has an effect, for '+' we only check the type
3978#ifdef FEAT_FLOAT
3979 if (rettv->v_type == VAR_FLOAT)
3980 {
3981 if (*p == '-')
3982 rettv->vval.v_float = -rettv->vval.v_float;
3983 }
3984 else
3985#endif
3986 {
3987 varnumber_T val;
3988 int error = FALSE;
3989
3990 // tv_get_number_chk() accepts a string, but we don't want that
3991 // here
3992 if (check_not_string(rettv) == FAIL)
3993 return FAIL;
3994 val = tv_get_number_chk(rettv, &error);
3995 clear_tv(rettv);
3996 if (error)
3997 return FAIL;
3998 if (*p == '-')
3999 val = -val;
4000 rettv->v_type = VAR_NUMBER;
4001 rettv->vval.v_number = val;
4002 }
4003 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004004 else if (numeric_only)
4005 {
4006 ++p;
4007 break;
4008 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004009 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 {
4011 int v = tv2bool(rettv);
4012
4013 // '!' is permissive in the type.
4014 clear_tv(rettv);
4015 rettv->v_type = VAR_BOOL;
4016 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4017 }
4018 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004019 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 return OK;
4021}
4022
4023/*
4024 * Recognize v: variables that are constants and set "rettv".
4025 */
4026 static void
4027get_vim_constant(char_u **arg, typval_T *rettv)
4028{
4029 if (STRNCMP(*arg, "v:true", 6) == 0)
4030 {
4031 rettv->v_type = VAR_BOOL;
4032 rettv->vval.v_number = VVAL_TRUE;
4033 *arg += 6;
4034 }
4035 else if (STRNCMP(*arg, "v:false", 7) == 0)
4036 {
4037 rettv->v_type = VAR_BOOL;
4038 rettv->vval.v_number = VVAL_FALSE;
4039 *arg += 7;
4040 }
4041 else if (STRNCMP(*arg, "v:null", 6) == 0)
4042 {
4043 rettv->v_type = VAR_SPECIAL;
4044 rettv->vval.v_number = VVAL_NULL;
4045 *arg += 6;
4046 }
4047 else if (STRNCMP(*arg, "v:none", 6) == 0)
4048 {
4049 rettv->v_type = VAR_SPECIAL;
4050 rettv->vval.v_number = VVAL_NONE;
4051 *arg += 6;
4052 }
4053}
4054
Bram Moolenaar657137c2021-01-09 15:45:23 +01004055 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004056get_compare_type(char_u *p, int *len, int *type_is)
4057{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004058 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004059 int i;
4060
4061 switch (p[0])
4062 {
4063 case '=': if (p[1] == '=')
4064 type = EXPR_EQUAL;
4065 else if (p[1] == '~')
4066 type = EXPR_MATCH;
4067 break;
4068 case '!': if (p[1] == '=')
4069 type = EXPR_NEQUAL;
4070 else if (p[1] == '~')
4071 type = EXPR_NOMATCH;
4072 break;
4073 case '>': if (p[1] != '=')
4074 {
4075 type = EXPR_GREATER;
4076 *len = 1;
4077 }
4078 else
4079 type = EXPR_GEQUAL;
4080 break;
4081 case '<': if (p[1] != '=')
4082 {
4083 type = EXPR_SMALLER;
4084 *len = 1;
4085 }
4086 else
4087 type = EXPR_SEQUAL;
4088 break;
4089 case 'i': if (p[1] == 's')
4090 {
4091 // "is" and "isnot"; but not a prefix of a name
4092 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4093 *len = 5;
4094 i = p[*len];
4095 if (!isalnum(i) && i != '_')
4096 {
4097 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4098 *type_is = TRUE;
4099 }
4100 }
4101 break;
4102 }
4103 return type;
4104}
4105
4106/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004107 * Skip over an expression, ignoring most errors.
4108 */
4109 static void
4110skip_expr_cctx(char_u **arg, cctx_T *cctx)
4111{
4112 evalarg_T evalarg;
4113
4114 CLEAR_FIELD(evalarg);
4115 evalarg.eval_cctx = cctx;
4116 skip_expr(arg, &evalarg);
4117}
4118
4119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004121 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 */
4123 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004124compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004126 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127
4128 // this works from end to start
4129 while (p > start)
4130 {
4131 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004132 while (VIM_ISWHITE(*p))
4133 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 if (*p == '-' || *p == '+')
4135 {
4136 int negate = *p == '-';
4137 isn_T *isn;
4138
4139 // TODO: check type
4140 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4141 {
4142 --p;
4143 if (*p == '-')
4144 negate = !negate;
4145 }
4146 // only '-' has an effect, for '+' we only check the type
4147 if (negate)
4148 isn = generate_instr(cctx, ISN_NEGATENR);
4149 else
4150 isn = generate_instr(cctx, ISN_CHECKNR);
4151 if (isn == NULL)
4152 return FAIL;
4153 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004154 else if (numeric_only)
4155 {
4156 ++p;
4157 break;
4158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 else
4160 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004161 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004163 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004165 if (p[-1] == '!')
4166 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004169 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 return FAIL;
4171 }
4172 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004173 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return OK;
4175}
4176
4177/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004178 * Compile "(expression)": recursive!
4179 * Return FAIL/OK.
4180 */
4181 static int
4182compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4183{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004184 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004185 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004186
Bram Moolenaar24156692021-01-14 20:35:49 +01004187 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4188 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004189 if (ppconst->pp_used <= PPSIZE - 10)
4190 {
4191 ret = compile_expr1(arg, cctx, ppconst);
4192 }
4193 else
4194 {
4195 // Not enough space in ppconst, flush constants.
4196 if (generate_ppconst(cctx, ppconst) == FAIL)
4197 return FAIL;
4198 ret = compile_expr0(arg, cctx);
4199 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004200 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4201 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004202 if (**arg == ')')
4203 ++*arg;
4204 else if (ret == OK)
4205 {
4206 emsg(_(e_missing_close));
4207 ret = FAIL;
4208 }
4209 return ret;
4210}
4211
4212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004214 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 */
4216 static int
4217compile_subscript(
4218 char_u **arg,
4219 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004220 char_u *start_leader,
4221 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004222 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004224 char_u *name_start = *end_leader;
4225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 for (;;)
4227 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004228 char_u *p = skipwhite(*arg);
4229
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004230 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004231 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004232 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004233
4234 // If a following line starts with "->{" or "->X" advance to that
4235 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004236 // Also if a following line starts with ".x".
4237 if (next != NULL &&
4238 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004239 && (next[2] == '{'
4240 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004241 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004242 {
4243 next = next_line_from_context(cctx, TRUE);
4244 if (next == NULL)
4245 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004246 *arg = next;
4247 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004248 }
4249 }
4250
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004251 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004252 // not a function call.
4253 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004255 garray_T *stack = &cctx->ctx_type_stack;
4256 type_T *type;
4257 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004259 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004261 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004264 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4265
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004266 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004267 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004269 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 return FAIL;
4271 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004272 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004274 char_u *pstart = p;
4275
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004276 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004277 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004278 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 // something->method()
4281 // Apply the '!', '-' and '+' first:
4282 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004283 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004286 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004287 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004288 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004289 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004290 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004291 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004292 garray_T *stack = &cctx->ctx_type_stack;
4293 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004294 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004295 int expr_isn_start = cctx->ctx_instr.ga_len;
4296 int expr_isn_end;
4297 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004298
4299 // Funcref call: list->(Refs[2])(arg)
4300 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004301 //
4302 // Fist compile the function expression.
4303 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004304 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004305
4306 // Remember the next instruction index, where the instructions
4307 // for arguments are being written.
4308 expr_isn_end = cctx->ctx_instr.ga_len;
4309
4310 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004311 if (**arg != '(')
4312 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004313 if (*skipwhite(*arg) == '(')
4314 emsg(_(e_nowhitespace));
4315 else
4316 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004317 return FAIL;
4318 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004319 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004320 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004321 return FAIL;
4322
Bram Moolenaar2927c072021-04-05 19:41:21 +02004323 // Move the instructions for the arguments to before the
4324 // instructions of the expression and move the type of the
4325 // expression after the argument types. This is what ISN_PCALL
4326 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004327 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004328 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4329 if (arg_isn_count > 0)
4330 {
4331 int expr_isn_count = expr_isn_end - expr_isn_start;
4332 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4333
4334 if (isn == NULL)
4335 return FAIL;
4336 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4337 + expr_isn_start,
4338 sizeof(isn_T) * expr_isn_count);
4339 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4340 + expr_isn_start,
4341 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4342 sizeof(isn_T) * arg_isn_count);
4343 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4344 + expr_isn_start + arg_isn_count,
4345 isn, sizeof(isn_T) * expr_isn_count);
4346 vim_free(isn);
4347
4348 type = ((type_T **)stack->ga_data)[type_idx_start];
4349 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4350 ((type_T **)stack->ga_data) + type_idx_start + 1,
4351 sizeof(type_T *)
4352 * (stack->ga_len - type_idx_start - 1));
4353 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4354 }
4355
Bram Moolenaar7e368202020-12-25 21:56:57 +01004356 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004357 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 return FAIL;
4359 }
4360 else
4361 {
4362 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004363 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004364 if (!eval_isnamec1(*p))
4365 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004366 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004367 return FAIL;
4368 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004369 if (ASCII_ISALPHA(*p) && p[1] == ':')
4370 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004371 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 ;
4373 if (*p != '(')
4374 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004375 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 return FAIL;
4377 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 return FAIL;
4380 }
4381 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004382 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004384 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004385
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004386 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004387 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004388 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004389 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004390 // TODO: more arguments
4391 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004392 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004393 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004394 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004395
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004396 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004397 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004398 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004399 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004400 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004401 // missing first index is equal to zero
4402 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004403 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004404 else
4405 {
4406 if (compile_expr0(arg, cctx) == FAIL)
4407 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004408 if (**arg == ':')
4409 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004410 semsg(_(e_white_space_required_before_and_after_str_at_str),
4411 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004412 return FAIL;
4413 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004414 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004415 return FAIL;
4416 *arg = skipwhite(*arg);
4417 }
4418 if (**arg == ':')
4419 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004420 is_slice = TRUE;
4421 ++*arg;
4422 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4423 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004424 semsg(_(e_white_space_required_before_and_after_str_at_str),
4425 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004426 return FAIL;
4427 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004428 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004429 return FAIL;
4430 if (**arg == ']')
4431 // missing second index is equal to end of string
4432 generate_PUSHNR(cctx, -1);
4433 else
4434 {
4435 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004436 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004437 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004438 return FAIL;
4439 *arg = skipwhite(*arg);
4440 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442
4443 if (**arg != ']')
4444 {
4445 emsg(_(e_missbrac));
4446 return FAIL;
4447 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004448 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449
Bram Moolenaare42939a2021-04-05 17:11:17 +02004450 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004453 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004455 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004456 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004457 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004458 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004459
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004460 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004461 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004462 {
4463 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004464 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004465 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004466 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004467 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 while (eval_isnamec(*p))
4469 MB_PTR_ADV(p);
4470 if (p == *arg)
4471 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004472 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 return FAIL;
4474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004475 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 return FAIL;
4477 *arg = p;
4478 }
4479 else
4480 break;
4481 }
4482
4483 // TODO - see handle_subscript():
4484 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4485 // Don't do this when "Func" is already a partial that was bound
4486 // explicitly (pt_auto is FALSE).
4487
4488 return OK;
4489}
4490
4491/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004492 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4493 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004495 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004496 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004497 *
4498 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 */
4500
4501/*
4502 * number number constant
4503 * 0zFFFFFFFF Blob constant
4504 * "string" string constant
4505 * 'string' literal string constant
4506 * &option-name option value
4507 * @r register contents
4508 * identifier variable value
4509 * function() function call
4510 * $VAR environment variable
4511 * (expression) nested expression
4512 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004513 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 *
4515 * Also handle:
4516 * ! in front logical NOT
4517 * - in front unary minus
4518 * + in front unary plus (ignored)
4519 * trailing (arg) funcref/partial call
4520 * trailing [] subscript in String or List
4521 * trailing .name entry in Dictionary
4522 * trailing ->name() method call
4523 */
4524 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004525compile_expr7(
4526 char_u **arg,
4527 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004528 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 char_u *start_leader, *end_leader;
4531 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004532 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004533 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004535 ppconst->pp_is_const = FALSE;
4536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 /*
4538 * Skip '!', '-' and '+' characters. They are handled later.
4539 */
4540 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004541 if (eval_leader(arg, TRUE) == FAIL)
4542 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 end_leader = *arg;
4544
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004545 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 switch (**arg)
4547 {
4548 /*
4549 * Number constant.
4550 */
4551 case '0': // also for blob starting with 0z
4552 case '1':
4553 case '2':
4554 case '3':
4555 case '4':
4556 case '5':
4557 case '6':
4558 case '7':
4559 case '8':
4560 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004561 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004563 // Apply "-" and "+" just before the number now, right to
4564 // left. Matters especially when "->" follows. Stops at
4565 // '!'.
4566 if (apply_leader(rettv, TRUE,
4567 start_leader, &end_leader) == FAIL)
4568 {
4569 clear_tv(rettv);
4570 return FAIL;
4571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 break;
4573
4574 /*
4575 * String constant: "string".
4576 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004577 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 return FAIL;
4579 break;
4580
4581 /*
4582 * Literal string constant: 'str''ing'.
4583 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004584 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 return FAIL;
4586 break;
4587
4588 /*
4589 * Constant Vim variable.
4590 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004591 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 ret = NOTDONE;
4593 break;
4594
4595 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004596 * "true" constant
4597 */
4598 case 't': if (STRNCMP(*arg, "true", 4) == 0
4599 && !eval_isnamec((*arg)[4]))
4600 {
4601 *arg += 4;
4602 rettv->v_type = VAR_BOOL;
4603 rettv->vval.v_number = VVAL_TRUE;
4604 }
4605 else
4606 ret = NOTDONE;
4607 break;
4608
4609 /*
4610 * "false" constant
4611 */
4612 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4613 && !eval_isnamec((*arg)[5]))
4614 {
4615 *arg += 5;
4616 rettv->v_type = VAR_BOOL;
4617 rettv->vval.v_number = VVAL_FALSE;
4618 }
4619 else
4620 ret = NOTDONE;
4621 break;
4622
4623 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004624 * "null" constant
4625 */
4626 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004627 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004628 {
4629 *arg += 4;
4630 rettv->v_type = VAR_SPECIAL;
4631 rettv->vval.v_number = VVAL_NULL;
4632 }
4633 else
4634 ret = NOTDONE;
4635 break;
4636
4637 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 * List: [expr, expr]
4639 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004640 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4641 return FAIL;
4642 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 break;
4644
4645 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 * Dictionary: {'key': val, 'key': val}
4647 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004648 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4649 return FAIL;
4650 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 break;
4652
4653 /*
4654 * Option value: &name
4655 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004656 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4657 return FAIL;
4658 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 break;
4660
4661 /*
4662 * Environment variable: $VAR.
4663 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004664 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4665 return FAIL;
4666 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 break;
4668
4669 /*
4670 * Register contents: @r.
4671 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004672 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4673 return FAIL;
4674 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 break;
4676 /*
4677 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004678 * lambda: (arg, arg) => expr
4679 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004681 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4682 ret = compile_lambda(arg, cctx);
4683 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004684 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 break;
4686
4687 default: ret = NOTDONE;
4688 break;
4689 }
4690 if (ret == FAIL)
4691 return FAIL;
4692
Bram Moolenaar1c747212020-05-09 18:28:34 +02004693 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004695 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 clear_tv(rettv);
4697 else
4698 // A constant expression can possibly be handled compile time,
4699 // return the value instead of generating code.
4700 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 }
4702 else if (ret == NOTDONE)
4703 {
4704 char_u *p;
4705 int r;
4706
4707 if (!eval_isnamec1(**arg))
4708 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004709 if (!vim9_bad_comment(*arg))
4710 {
4711 if (ends_excmd(*skipwhite(*arg)))
4712 semsg(_(e_empty_expression_str), *arg);
4713 else
4714 semsg(_(e_name_expected_str), *arg);
4715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 return FAIL;
4717 }
4718
4719 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004720 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004721 if (p - *arg == (size_t)1 && **arg == '_')
4722 {
4723 emsg(_(e_cannot_use_underscore_here));
4724 return FAIL;
4725 }
4726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004728 {
4729 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004732 {
4733 if (generate_ppconst(cctx, ppconst) == FAIL)
4734 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004735 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 if (r == FAIL)
4738 return FAIL;
4739 }
4740
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004741 // Handle following "[]", ".member", etc.
4742 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004743 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004744 ppconst) == FAIL)
4745 return FAIL;
4746 if (ppconst->pp_used > 0)
4747 {
4748 // apply the '!', '-' and '+' before the constant
4749 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004750 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004751 return FAIL;
4752 return OK;
4753 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004754 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004756 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757}
4758
4759/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004760 * Give the "white on both sides" error, taking the operator from "p[len]".
4761 */
4762 void
4763error_white_both(char_u *op, int len)
4764{
4765 char_u buf[10];
4766
4767 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004768 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004769}
4770
4771/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004772 * <type>expr7: runtime type check / conversion
4773 */
4774 static int
4775compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4776{
4777 type_T *want_type = NULL;
4778
4779 // Recognize <type>
4780 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4781 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004782 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004783 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4784 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004785 return FAIL;
4786
4787 if (**arg != '>')
4788 {
4789 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004790 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004791 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004792 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004793 return FAIL;
4794 }
4795 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004796 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004797 return FAIL;
4798 }
4799
4800 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4801 return FAIL;
4802
4803 if (want_type != NULL)
4804 {
4805 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004806 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004807 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004808
Bram Moolenaard1103582020-08-14 22:44:25 +02004809 generate_ppconst(cctx, ppconst);
4810 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004811 where.wt_index = 0;
4812 where.wt_variable = FALSE;
4813 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004814 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004815 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004816 return FAIL;
4817 }
4818 }
4819
4820 return OK;
4821}
4822
4823/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 * * number multiplication
4825 * / number division
4826 * % number modulo
4827 */
4828 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004829compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830{
4831 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004832 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004833 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004835 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004836 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 return FAIL;
4838
4839 /*
4840 * Repeat computing, until no "*", "/" or "%" is following.
4841 */
4842 for (;;)
4843 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004844 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 if (*op != '*' && *op != '/' && *op != '%')
4846 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004847 if (next != NULL)
4848 {
4849 *arg = next_line_from_context(cctx, TRUE);
4850 op = skipwhite(*arg);
4851 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004852
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004853 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004855 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004856 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004858 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004859 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004861 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004862 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004864
4865 if (ppconst->pp_used == ppconst_used + 2
4866 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4867 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004868 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004869 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4870 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4871 varnumber_T res = 0;
4872 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004874 // both are numbers: compute the result
4875 switch (*op)
4876 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004877 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004878 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004879 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004880 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004881 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004882 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004883 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004884 break;
4885 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004886 if (failed)
4887 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004888 tv1->vval.v_number = res;
4889 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004890 }
4891 else
4892 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004893 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004894 generate_two_op(cctx, op);
4895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 }
4897
4898 return OK;
4899}
4900
4901/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004902 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 * - number subtraction
4904 * .. string concatenation
4905 */
4906 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004907compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908{
4909 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004910 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913
4914 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004915 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 return FAIL;
4917
4918 /*
4919 * Repeat computing, until no "+", "-" or ".." is following.
4920 */
4921 for (;;)
4922 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004923 op = may_peek_next_line(cctx, *arg, &next);
4924 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004926 if (op[0] == op[1] && *op != '.' && next)
4927 // Finding "++" or "--" on the next line is a separate command.
4928 // But ".." is concatenation.
4929 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004931 if (next != NULL)
4932 {
4933 *arg = next_line_from_context(cctx, TRUE);
4934 op = skipwhite(*arg);
4935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004937 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004939 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004940 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 }
4942
Bram Moolenaare0de1712020-12-02 17:36:54 +01004943 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004944 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004946 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 return FAIL;
4949
Bram Moolenaara5565e42020-05-09 15:44:01 +02004950 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004951 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004952 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4953 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4954 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4955 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004957 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4958 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004959
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004960 // concat/subtract/add constant numbers
4961 if (*op == '+')
4962 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4963 else if (*op == '-')
4964 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4965 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004966 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004967 // concatenate constant strings
4968 char_u *s1 = tv1->vval.v_string;
4969 char_u *s2 = tv2->vval.v_string;
4970 size_t len1 = STRLEN(s1);
4971
4972 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4973 if (tv1->vval.v_string == NULL)
4974 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004975 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004976 return FAIL;
4977 }
4978 mch_memmove(tv1->vval.v_string, s1, len1);
4979 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004980 vim_free(s1);
4981 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004982 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004983 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 }
4985 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004986 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004988 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004989 if (*op == '.')
4990 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004991 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4992 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004993 return FAIL;
4994 generate_instr_drop(cctx, ISN_CONCAT, 1);
4995 }
4996 else
4997 generate_two_op(cctx, op);
4998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 }
5000
5001 return OK;
5002}
5003
5004/*
5005 * expr5a == expr5b
5006 * expr5a =~ expr5b
5007 * expr5a != expr5b
5008 * expr5a !~ expr5b
5009 * expr5a > expr5b
5010 * expr5a >= expr5b
5011 * expr5a < expr5b
5012 * expr5a <= expr5b
5013 * expr5a is expr5b
5014 * expr5a isnot expr5b
5015 *
5016 * Produces instructions:
5017 * EVAL expr5a Push result of "expr5a"
5018 * EVAL expr5b Push result of "expr5b"
5019 * COMPARE one of the compare instructions
5020 */
5021 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005024 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005026 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005029 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030
5031 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 return FAIL;
5034
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005035 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005036 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037
5038 /*
5039 * If there is a comparative operator, use it.
5040 */
5041 if (type != EXPR_UNKNOWN)
5042 {
5043 int ic = FALSE; // Default: do not ignore case
5044
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005045 if (next != NULL)
5046 {
5047 *arg = next_line_from_context(cctx, TRUE);
5048 p = skipwhite(*arg);
5049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 if (type_is && (p[len] == '?' || p[len] == '#'))
5051 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005052 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 return FAIL;
5054 }
5055 // extra question mark appended: ignore case
5056 if (p[len] == '?')
5057 {
5058 ic = TRUE;
5059 ++len;
5060 }
5061 // extra '#' appended: match case (ignored)
5062 else if (p[len] == '#')
5063 ++len;
5064 // nothing appended: match case
5065
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005066 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005068 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005069 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
5071
5072 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005073 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005074 return FAIL;
5075
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 return FAIL;
5078
Bram Moolenaara5565e42020-05-09 15:44:01 +02005079 if (ppconst->pp_used == ppconst_used + 2)
5080 {
5081 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5082 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5083 int ret;
5084
5085 // Both sides are a constant, compute the result now.
5086 // First check for a valid combination of types, this is more
5087 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005088 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005089 ret = FAIL;
5090 else
5091 {
5092 ret = typval_compare(tv1, tv2, type, ic);
5093 tv1->v_type = VAR_BOOL;
5094 tv1->vval.v_number = tv1->vval.v_number
5095 ? VVAL_TRUE : VVAL_FALSE;
5096 clear_tv(tv2);
5097 --ppconst->pp_used;
5098 }
5099 return ret;
5100 }
5101
5102 generate_ppconst(cctx, ppconst);
5103 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 }
5105
5106 return OK;
5107}
5108
Bram Moolenaar7f141552020-05-09 17:35:53 +02005109static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111/*
5112 * Compile || or &&.
5113 */
5114 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005115compile_and_or(
5116 char_u **arg,
5117 cctx_T *cctx,
5118 char *op,
5119 ppconst_T *ppconst,
5120 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005122 char_u *next;
5123 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 int opchar = *op;
5125
5126 if (p[0] == opchar && p[1] == opchar)
5127 {
5128 garray_T *instr = &cctx->ctx_instr;
5129 garray_T end_ga;
5130
5131 /*
5132 * Repeat until there is no following "||" or "&&"
5133 */
5134 ga_init2(&end_ga, sizeof(int), 10);
5135 while (p[0] == opchar && p[1] == opchar)
5136 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005137 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005138 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005139 int start_ctx_lnum = cctx->ctx_lnum;
5140 int save_lnum;
5141
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005142 if (next != NULL)
5143 {
5144 *arg = next_line_from_context(cctx, TRUE);
5145 p = skipwhite(*arg);
5146 }
5147
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005148 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5149 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005150 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005151 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005152 return FAIL;
5153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005155 // TODO: use ppconst if the value is a constant and check
5156 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005157 generate_ppconst(cctx, ppconst);
5158
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005159 // Every part must evaluate to a bool.
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005160 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005161 SOURCING_LNUM = start_lnum;
5162 save_lnum = cctx->ctx_lnum;
5163 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005164 if (bool_on_stack(cctx) == FAIL)
5165 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005166 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005167 ga_clear(&end_ga);
5168 return FAIL;
5169 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005170 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 if (ga_grow(&end_ga, 1) == FAIL)
5173 {
5174 ga_clear(&end_ga);
5175 return FAIL;
5176 }
5177 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5178 ++end_ga.ga_len;
5179 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005180 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
5182 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005183 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005184 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005185 {
5186 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005187 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005188 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005189
Bram Moolenaara5565e42020-05-09 15:44:01 +02005190 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5191 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 {
5193 ga_clear(&end_ga);
5194 return FAIL;
5195 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005196
5197 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005199 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005201 // Every part must evaluate to a bool.
5202 if (bool_on_stack(cctx) == FAIL)
5203 {
5204 ga_clear(&end_ga);
5205 return FAIL;
5206 }
5207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 // Fill in the end label in all jumps.
5209 while (end_ga.ga_len > 0)
5210 {
5211 isn_T *isn;
5212
5213 --end_ga.ga_len;
5214 isn = ((isn_T *)instr->ga_data)
5215 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5216 isn->isn_arg.jump.jump_where = instr->ga_len;
5217 }
5218 ga_clear(&end_ga);
5219 }
5220
5221 return OK;
5222}
5223
5224/*
5225 * expr4a && expr4a && expr4a logical AND
5226 *
5227 * Produces instructions:
5228 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005229 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005230 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005232 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 * EVAL expr4c Push result of "expr4c"
5234 * end:
5235 */
5236 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005237compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005239 int ppconst_used = ppconst->pp_used;
5240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005242 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 return FAIL;
5244
5245 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005246 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247}
5248
5249/*
5250 * expr3a || expr3b || expr3c logical OR
5251 *
5252 * Produces instructions:
5253 * EVAL expr3a Push result of "expr3a"
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_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005257 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 * EVAL expr3c Push result of "expr3c"
5259 * end:
5260 */
5261 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005262compile_expr2(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 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 if (compile_expr3(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 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005277 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 * JUMP_IF_FALSE alt jump if false
5279 * EVAL expr1a
5280 * JUMP_ALWAYS end
5281 * alt: EVAL expr1b
5282 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005283 *
5284 * Toplevel expression: expr2 ?? expr1
5285 * Produces instructions:
5286 * EVAL expr2 Push result of "expr2"
5287 * JUMP_AND_KEEP_IF_TRUE end jump if true
5288 * EVAL expr1
5289 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 */
5291 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005292compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293{
5294 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005295 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005296 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297
Bram Moolenaar3988f642020-08-27 22:43:03 +02005298 // Ignore all kinds of errors when not producing code.
5299 if (cctx->ctx_skip == SKIP_YES)
5300 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005301 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005302 return OK;
5303 }
5304
Bram Moolenaar61a89812020-05-07 16:58:17 +02005305 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005306 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 return FAIL;
5308
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005309 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 if (*p == '?')
5311 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005312 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 garray_T *instr = &cctx->ctx_instr;
5314 garray_T *stack = &cctx->ctx_type_stack;
5315 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005316 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005318 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 int has_const_expr = FALSE;
5320 int const_value = FALSE;
5321 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005323 if (next != NULL)
5324 {
5325 *arg = next_line_from_context(cctx, TRUE);
5326 p = skipwhite(*arg);
5327 }
5328
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005330 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005331 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005332 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005333 return FAIL;
5334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 if (ppconst->pp_used == ppconst_used + 1)
5337 {
5338 // the condition is a constant, we know whether the ? or the :
5339 // expression is to be evaluated.
5340 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005341 if (op_falsy)
5342 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5343 else
5344 {
5345 int error = FALSE;
5346
5347 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5348 &error);
5349 if (error)
5350 return FAIL;
5351 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005352 cctx->ctx_skip = save_skip == SKIP_YES ||
5353 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5354
5355 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5356 // "left ?? right" and "left" is truthy: produce "left"
5357 generate_ppconst(cctx, ppconst);
5358 else
5359 {
5360 clear_tv(&ppconst->pp_tv[ppconst_used]);
5361 --ppconst->pp_used;
5362 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005363 }
5364 else
5365 {
5366 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005367 if (op_falsy)
5368 end_idx = instr->ga_len;
5369 generate_JUMP(cctx, op_falsy
5370 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5371 if (op_falsy)
5372 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374
5375 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005376 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005377 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005378 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005379 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380
Bram Moolenaara5565e42020-05-09 15:44:01 +02005381 if (!has_const_expr)
5382 {
5383 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005385 if (!op_falsy)
5386 {
5387 // remember the type and drop it
5388 --stack->ga_len;
5389 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005391 end_idx = instr->ga_len;
5392 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005393
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005394 // jump here from JUMP_IF_FALSE
5395 isn = ((isn_T *)instr->ga_data) + alt_idx;
5396 isn->isn_arg.jump.jump_where = instr->ga_len;
5397 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005400 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005402 // Check for the ":".
5403 p = may_peek_next_line(cctx, *arg, &next);
5404 if (*p != ':')
5405 {
5406 emsg(_(e_missing_colon));
5407 return FAIL;
5408 }
5409 if (next != NULL)
5410 {
5411 *arg = next_line_from_context(cctx, TRUE);
5412 p = skipwhite(*arg);
5413 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005414
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005415 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5416 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005417 semsg(_(e_white_space_required_before_and_after_str_at_str),
5418 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005419 return FAIL;
5420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005422 // evaluate the third expression
5423 if (has_const_expr)
5424 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005425 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005426 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005427 return FAIL;
5428 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5429 return FAIL;
5430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaara5565e42020-05-09 15:44:01 +02005432 if (!has_const_expr)
5433 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005434 type_T **typep;
5435
Bram Moolenaara5565e42020-05-09 15:44:01 +02005436 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437
Bram Moolenaara5565e42020-05-09 15:44:01 +02005438 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005439 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5440 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005441
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005442 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005443 isn = ((isn_T *)instr->ga_data) + end_idx;
5444 isn->isn_arg.jump.jump_where = instr->ga_len;
5445 }
5446
5447 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 }
5449 return OK;
5450}
5451
5452/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005454 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5455 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005456 */
5457 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005458compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005459{
5460 ppconst_T ppconst;
5461
5462 CLEAR_FIELD(ppconst);
5463 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5464 {
5465 clear_ppconst(&ppconst);
5466 return FAIL;
5467 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005468 if (is_const != NULL)
5469 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005470 if (generate_ppconst(cctx, &ppconst) == FAIL)
5471 return FAIL;
5472 return OK;
5473}
5474
5475/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005476 * Toplevel expression.
5477 */
5478 static int
5479compile_expr0(char_u **arg, cctx_T *cctx)
5480{
5481 return compile_expr0_ext(arg, cctx, NULL);
5482}
5483
5484/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005485 * Compile "return [expr]".
5486 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 */
5488 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005489compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490{
5491 char_u *p = arg;
5492 garray_T *stack = &cctx->ctx_type_stack;
5493 type_T *stack_type;
5494
5495 if (*p != NUL && *p != '|' && *p != '\n')
5496 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005497 if (legacy)
5498 {
5499 int save_flags = cmdmod.cmod_flags;
5500
5501 generate_LEGACY_EVAL(cctx, p);
5502 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5503 0, cctx, FALSE, FALSE) == FAIL)
5504 return NULL;
5505 cmdmod.cmod_flags |= CMOD_LEGACY;
5506 (void)skip_expr(&p, NULL);
5507 cmdmod.cmod_flags = save_flags;
5508 }
5509 else
5510 {
5511 // compile return argument into instructions
5512 if (compile_expr0(&p, cctx) == FAIL)
5513 return NULL;
5514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005516 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005517 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005518 // "check_return_type" with uf_ret_type set to &t_unknown is used
5519 // for an inline function without a specified return type. Set the
5520 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005521 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005522 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005523 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5524 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005525 || (!check_return_type
5526 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005527 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005528 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005529 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005530 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005531 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005532 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5533 && stack_type->tt_type != VAR_VOID
5534 && stack_type->tt_type != VAR_UNKNOWN)
5535 {
5536 emsg(_(e_returning_value_in_function_without_return_type));
5537 return NULL;
5538 }
5539 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005540 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005541 return NULL;
5542 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 }
5545 else
5546 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005547 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005548 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005549 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5550 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005552 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 return NULL;
5554 }
5555
5556 // No argument, return zero.
5557 generate_PUSHNR(cctx, 0);
5558 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005559
5560 // Undo any command modifiers.
5561 generate_undo_cmdmods(cctx);
5562
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005563 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 return NULL;
5565
5566 // "return val | endif" is possible
5567 return skipwhite(p);
5568}
5569
5570/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005571 * Get a line from the compilation context, compatible with exarg_T getline().
5572 * Return a pointer to the line in allocated memory.
5573 * Return NULL for end-of-file or some error.
5574 */
5575 static char_u *
5576exarg_getline(
5577 int c UNUSED,
5578 void *cookie,
5579 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005580 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005581{
5582 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005583 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005584
Bram Moolenaar66250c92020-08-20 15:02:42 +02005585 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005586 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005587 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005588 return NULL;
5589 ++cctx->ctx_lnum;
5590 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5591 // Comment lines result in NULL pointers, skip them.
5592 if (p != NULL)
5593 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005594 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005595}
5596
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005597 void
5598fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5599{
5600 eap->getline = exarg_getline;
5601 eap->cookie = cctx;
5602}
5603
Bram Moolenaar04b12692020-05-04 23:24:44 +02005604/*
5605 * Compile a nested :def command.
5606 */
5607 static char_u *
5608compile_nested_function(exarg_T *eap, cctx_T *cctx)
5609{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005610 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005611 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005612 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005613 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005614 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005615 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005616 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005617
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005618 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005619 {
5620 emsg(_(e_cannot_use_bang_with_nested_def));
5621 return NULL;
5622 }
5623
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005624 if (*name_start == '/')
5625 {
5626 name_end = skip_regexp(name_start + 1, '/', TRUE);
5627 if (*name_end == '/')
5628 ++name_end;
5629 eap->nextcmd = check_nextcmd(name_end);
5630 }
5631 if (name_end == name_start || *skipwhite(name_end) != '(')
5632 {
5633 if (!ends_excmd2(name_start, name_end))
5634 {
5635 semsg(_(e_invalid_command_str), eap->cmd);
5636 return NULL;
5637 }
5638
5639 // "def" or "def Name": list functions
5640 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5641 return NULL;
5642 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5643 }
5644
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005645 // Only g:Func() can use a namespace.
5646 if (name_start[1] == ':' && !is_global)
5647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005648 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005649 return NULL;
5650 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005651 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005652 return NULL;
5653
Bram Moolenaar04b12692020-05-04 23:24:44 +02005654 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005655 fill_exarg_from_cctx(eap, cctx);
5656
Bram Moolenaar04b12692020-05-04 23:24:44 +02005657 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005658 lambda_name = vim_strsave(get_lambda_name());
5659 if (lambda_name == NULL)
5660 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005661 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005662
Bram Moolenaar822ba242020-05-24 23:00:18 +02005663 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005664 {
5665 r = eap->skip ? OK : FAIL;
5666 goto theend;
5667 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005668
5669 // copy over the block scope IDs before compiling
5670 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5671 {
5672 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5673
5674 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5675 if (ufunc->uf_block_ids != NULL)
5676 {
5677 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5678 sizeof(int) * block_depth);
5679 ufunc->uf_block_depth = block_depth;
5680 }
5681 }
5682
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005683 compile_type = COMPILE_TYPE(ufunc);
5684#ifdef FEAT_PROFILE
5685 // If the outer function is profiled, also compile the nested function for
5686 // profiling.
5687 if (cctx->ctx_compile_type == CT_PROFILE)
5688 compile_type = CT_PROFILE;
5689#endif
5690 if (func_needs_compiling(ufunc, compile_type)
5691 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005692 {
5693 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005694 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005695 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005696
Bram Moolenaar648594e2021-07-11 17:55:01 +02005697#ifdef FEAT_PROFILE
5698 // When the outer function is compiled for profiling, the nested function
5699 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005700 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005701 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5702#endif
5703
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005704 if (is_global)
5705 {
5706 char_u *func_name = vim_strnsave(name_start + 2,
5707 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005708
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005709 if (func_name == NULL)
5710 r = FAIL;
5711 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005712 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005713 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005714 lambda_name = NULL;
5715 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005716 }
5717 else
5718 {
5719 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005720 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005721 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005722
Bram Moolenaareef21022020-08-01 22:16:43 +02005723 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005724 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005725 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005726 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005727 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5728 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005729 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005730
5731theend:
5732 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005733 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005734}
5735
5736/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 * Return the length of an assignment operator, or zero if there isn't one.
5738 */
5739 int
5740assignment_len(char_u *p, int *heredoc)
5741{
5742 if (*p == '=')
5743 {
5744 if (p[1] == '<' && p[2] == '<')
5745 {
5746 *heredoc = TRUE;
5747 return 3;
5748 }
5749 return 1;
5750 }
5751 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5752 return 2;
5753 if (STRNCMP(p, "..=", 3) == 0)
5754 return 3;
5755 return 0;
5756}
5757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005759 * Generate the load instruction for "name".
5760 */
5761 static void
5762generate_loadvar(
5763 cctx_T *cctx,
5764 assign_dest_T dest,
5765 char_u *name,
5766 lvar_T *lvar,
5767 type_T *type)
5768{
5769 switch (dest)
5770 {
5771 case dest_option:
5772 // TODO: check the option exists
5773 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5774 break;
5775 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005776 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5777 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5778 else
5779 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005780 break;
5781 case dest_buffer:
5782 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5783 break;
5784 case dest_window:
5785 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5786 break;
5787 case dest_tab:
5788 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5789 break;
5790 case dest_script:
5791 compile_load_scriptvar(cctx,
5792 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5793 break;
5794 case dest_env:
5795 // Include $ in the name here
5796 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5797 break;
5798 case dest_reg:
5799 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5800 break;
5801 case dest_vimvar:
5802 generate_LOADV(cctx, name + 2, TRUE);
5803 break;
5804 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005805 if (lvar->lv_from_outer > 0)
5806 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5807 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005808 else
5809 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5810 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005811 case dest_expr:
5812 // list or dict value should already be on the stack.
5813 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005814 }
5815}
5816
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005817/*
5818 * Skip over "[expr]" or ".member".
5819 * Does not check for any errors.
5820 */
5821 static char_u *
5822skip_index(char_u *start)
5823{
5824 char_u *p = start;
5825
5826 if (*p == '[')
5827 {
5828 p = skipwhite(p + 1);
5829 (void)skip_expr(&p, NULL);
5830 p = skipwhite(p);
5831 if (*p == ']')
5832 return p + 1;
5833 return p;
5834 }
5835 // if (*p == '.')
5836 return to_name_end(p + 1, TRUE);
5837}
5838
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005839 void
5840vim9_declare_error(char_u *name)
5841{
5842 char *scope = "";
5843
5844 switch (*name)
5845 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005846 case 'g': scope = _("global"); break;
5847 case 'b': scope = _("buffer"); break;
5848 case 'w': scope = _("window"); break;
5849 case 't': scope = _("tab"); break;
5850 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005851 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005852 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005853 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005854 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005855 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005856 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005857 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005858 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005859 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005860}
5861
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005862/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005863 * For one assignment figure out the type of destination. Return it in "dest".
5864 * When not recognized "dest" is not set.
5865 * For an option "opt_flags" is set.
5866 * For a v:var "vimvaridx" is set.
5867 * "type" is set to the destination type if known, unchanted otherwise.
5868 * Return FAIL if an error message was given.
5869 */
5870 static int
5871get_var_dest(
5872 char_u *name,
5873 assign_dest_T *dest,
5874 int cmdidx,
5875 int *opt_flags,
5876 int *vimvaridx,
5877 type_T **type,
5878 cctx_T *cctx)
5879{
5880 char_u *p;
5881
5882 if (*name == '&')
5883 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005884 int cc;
5885 long numval;
5886 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005887
5888 *dest = dest_option;
5889 if (cmdidx == CMD_final || cmdidx == CMD_const)
5890 {
5891 emsg(_(e_const_option));
5892 return FAIL;
5893 }
5894 p = name;
5895 p = find_option_end(&p, opt_flags);
5896 if (p == NULL)
5897 {
5898 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005899 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005900 return FAIL;
5901 }
5902 cc = *p;
5903 *p = NUL;
5904 opt_type = get_option_value(skip_option_env_lead(name),
5905 &numval, NULL, *opt_flags);
5906 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005907 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005908 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005909 case gov_unknown:
5910 semsg(_(e_unknown_option), name);
5911 return FAIL;
5912 case gov_string:
5913 case gov_hidden_string:
5914 *type = &t_string;
5915 break;
5916 case gov_bool:
5917 case gov_hidden_bool:
5918 *type = &t_bool;
5919 break;
5920 case gov_number:
5921 case gov_hidden_number:
5922 *type = &t_number;
5923 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005924 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005925 }
5926 else if (*name == '$')
5927 {
5928 *dest = dest_env;
5929 *type = &t_string;
5930 }
5931 else if (*name == '@')
5932 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005933 if (name[1] != '@'
5934 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005935 {
5936 emsg_invreg(name[1]);
5937 return FAIL;
5938 }
5939 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005940 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005941 }
5942 else if (STRNCMP(name, "g:", 2) == 0)
5943 {
5944 *dest = dest_global;
5945 }
5946 else if (STRNCMP(name, "b:", 2) == 0)
5947 {
5948 *dest = dest_buffer;
5949 }
5950 else if (STRNCMP(name, "w:", 2) == 0)
5951 {
5952 *dest = dest_window;
5953 }
5954 else if (STRNCMP(name, "t:", 2) == 0)
5955 {
5956 *dest = dest_tab;
5957 }
5958 else if (STRNCMP(name, "v:", 2) == 0)
5959 {
5960 typval_T *vtv;
5961 int di_flags;
5962
5963 *vimvaridx = find_vim_var(name + 2, &di_flags);
5964 if (*vimvaridx < 0)
5965 {
5966 semsg(_(e_variable_not_found_str), name);
5967 return FAIL;
5968 }
5969 // We use the current value of "sandbox" here, is that OK?
5970 if (var_check_ro(di_flags, name, FALSE))
5971 return FAIL;
5972 *dest = dest_vimvar;
5973 vtv = get_vim_var_tv(*vimvaridx);
5974 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5975 }
5976 return OK;
5977}
5978
5979/*
5980 * Generate a STORE instruction for "dest", not being "dest_local".
5981 * Return FAIL when out of memory.
5982 */
5983 static int
5984generate_store_var(
5985 cctx_T *cctx,
5986 assign_dest_T dest,
5987 int opt_flags,
5988 int vimvaridx,
5989 int scriptvar_idx,
5990 int scriptvar_sid,
5991 type_T *type,
5992 char_u *name)
5993{
5994 switch (dest)
5995 {
5996 case dest_option:
5997 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5998 opt_flags);
5999 case dest_global:
6000 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006001 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6002 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006003 case dest_buffer:
6004 // include b: with the name, easier to execute that way
6005 return generate_STORE(cctx, ISN_STOREB, 0, name);
6006 case dest_window:
6007 // include w: with the name, easier to execute that way
6008 return generate_STORE(cctx, ISN_STOREW, 0, name);
6009 case dest_tab:
6010 // include t: with the name, easier to execute that way
6011 return generate_STORE(cctx, ISN_STORET, 0, name);
6012 case dest_env:
6013 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6014 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006015 return generate_STORE(cctx, ISN_STOREREG,
6016 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006017 case dest_vimvar:
6018 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6019 case dest_script:
6020 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006021 // "s:" may be included in the name.
6022 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006023 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006024 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6025 scriptvar_sid, scriptvar_idx, type);
6026 case dest_local:
6027 case dest_expr:
6028 // cannot happen
6029 break;
6030 }
6031 return FAIL;
6032}
6033
Bram Moolenaar752fc692021-01-04 21:57:11 +01006034 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006035generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6036{
6037 if (lhs->lhs_dest != dest_local)
6038 return generate_store_var(cctx, lhs->lhs_dest,
6039 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6040 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6041 lhs->lhs_type, lhs->lhs_name);
6042
6043 if (lhs->lhs_lvar != NULL)
6044 {
6045 garray_T *instr = &cctx->ctx_instr;
6046 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6047
6048 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6049 // ISN_STORENR
6050 if (lhs->lhs_lvar->lv_from_outer == 0
6051 && instr->ga_len == instr_count + 1
6052 && isn->isn_type == ISN_PUSHNR)
6053 {
6054 varnumber_T val = isn->isn_arg.number;
6055 garray_T *stack = &cctx->ctx_type_stack;
6056
6057 isn->isn_type = ISN_STORENR;
6058 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6059 isn->isn_arg.storenr.stnr_val = val;
6060 if (stack->ga_len > 0)
6061 --stack->ga_len;
6062 }
6063 else if (lhs->lhs_lvar->lv_from_outer > 0)
6064 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6065 lhs->lhs_lvar->lv_from_outer);
6066 else
6067 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6068 }
6069 return OK;
6070}
6071
6072 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006073is_decl_command(int cmdidx)
6074{
6075 return cmdidx == CMD_let || cmdidx == CMD_var
6076 || cmdidx == CMD_final || cmdidx == CMD_const;
6077}
6078
6079/*
6080 * Figure out the LHS type and other properties for an assignment or one item
6081 * of ":unlet" with an index.
6082 * Returns OK or FAIL.
6083 */
6084 static int
6085compile_lhs(
6086 char_u *var_start,
6087 lhs_T *lhs,
6088 int cmdidx,
6089 int heredoc,
6090 int oplen,
6091 cctx_T *cctx)
6092{
6093 char_u *var_end;
6094 int is_decl = is_decl_command(cmdidx);
6095
6096 CLEAR_POINTER(lhs);
6097 lhs->lhs_dest = dest_local;
6098 lhs->lhs_vimvaridx = -1;
6099 lhs->lhs_scriptvar_idx = -1;
6100
6101 // "dest_end" is the end of the destination, including "[expr]" or
6102 // ".name".
6103 // "var_end" is the end of the variable/option/etc. name.
6104 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6105 if (*var_start == '@')
6106 var_end = var_start + 2;
6107 else
6108 {
6109 // skip over the leading "&", "&l:", "&g:" and "$"
6110 var_end = skip_option_env_lead(var_start);
6111 var_end = to_name_end(var_end, TRUE);
6112 }
6113
6114 // "a: type" is declaring variable "a" with a type, not dict "a:".
6115 if (is_decl && lhs->lhs_dest_end == var_start + 2
6116 && lhs->lhs_dest_end[-1] == ':')
6117 --lhs->lhs_dest_end;
6118 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6119 --var_end;
6120
6121 // compute the length of the destination without "[expr]" or ".name"
6122 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006123 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006124 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6125 if (lhs->lhs_name == NULL)
6126 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006127
6128 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6129 // Something follows after the variable: "var[idx]" or "var.key".
6130 lhs->lhs_has_index = TRUE;
6131
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 if (heredoc)
6133 lhs->lhs_type = &t_list_string;
6134 else
6135 lhs->lhs_type = &t_any;
6136
6137 if (cctx->ctx_skip != SKIP_YES)
6138 {
6139 int declare_error = FALSE;
6140
6141 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6142 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6143 &lhs->lhs_type, cctx) == FAIL)
6144 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006145 if (lhs->lhs_dest != dest_local
6146 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006147 {
6148 // Specific kind of variable recognized.
6149 declare_error = is_decl;
6150 }
6151 else
6152 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006154 if (check_reserved_name(lhs->lhs_name) == FAIL)
6155 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156
6157 if (lookup_local(var_start, lhs->lhs_varlen,
6158 &lhs->lhs_local_lvar, cctx) == OK)
6159 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6160 else
6161 {
6162 CLEAR_FIELD(lhs->lhs_arg_lvar);
6163 if (arg_exists(var_start, lhs->lhs_varlen,
6164 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6165 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6166 {
6167 if (is_decl)
6168 {
6169 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6170 return FAIL;
6171 }
6172 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6173 }
6174 }
6175 if (lhs->lhs_lvar != NULL)
6176 {
6177 if (is_decl)
6178 {
6179 semsg(_(e_variable_already_declared), lhs->lhs_name);
6180 return FAIL;
6181 }
6182 }
6183 else
6184 {
6185 int script_namespace = lhs->lhs_varlen > 1
6186 && STRNCMP(var_start, "s:", 2) == 0;
6187 int script_var = (script_namespace
6188 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006189 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006190 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006191 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 imported_T *import =
6193 find_imported(var_start, lhs->lhs_varlen, cctx);
6194
6195 if (script_namespace || script_var || import != NULL)
6196 {
6197 char_u *rawname = lhs->lhs_name
6198 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6199
6200 if (is_decl)
6201 {
6202 if (script_namespace)
6203 semsg(_(e_cannot_declare_script_variable_in_function),
6204 lhs->lhs_name);
6205 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006206 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 lhs->lhs_name);
6208 return FAIL;
6209 }
6210 else if (cctx->ctx_ufunc->uf_script_ctx_version
6211 == SCRIPT_VERSION_VIM9
6212 && script_namespace
6213 && !script_var && import == NULL)
6214 {
6215 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6216 return FAIL;
6217 }
6218
6219 lhs->lhs_dest = dest_script;
6220
6221 // existing script-local variables should have a type
6222 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6223 if (import != NULL)
6224 lhs->lhs_scriptvar_sid = import->imp_sid;
6225 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6226 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006227 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006229 lhs->lhs_scriptvar_sid, rawname,
6230 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6231 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006232 if (lhs->lhs_scriptvar_idx >= 0)
6233 {
6234 scriptitem_T *si = SCRIPT_ITEM(
6235 lhs->lhs_scriptvar_sid);
6236 svar_T *sv =
6237 ((svar_T *)si->sn_var_vals.ga_data)
6238 + lhs->lhs_scriptvar_idx;
6239 lhs->lhs_type = sv->sv_type;
6240 }
6241 }
6242 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006243 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 == FAIL)
6245 return FAIL;
6246 }
6247 }
6248
6249 if (declare_error)
6250 {
6251 vim9_declare_error(lhs->lhs_name);
6252 return FAIL;
6253 }
6254 }
6255
6256 // handle "a:name" as a name, not index "name" on "a"
6257 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6258 var_end = lhs->lhs_dest_end;
6259
6260 if (lhs->lhs_dest != dest_option)
6261 {
6262 if (is_decl && *var_end == ':')
6263 {
6264 char_u *p;
6265
6266 // parse optional type: "let var: type = expr"
6267 if (!VIM_ISWHITE(var_end[1]))
6268 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006269 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 return FAIL;
6271 }
6272 p = skipwhite(var_end + 1);
6273 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6274 if (lhs->lhs_type == NULL)
6275 return FAIL;
6276 lhs->lhs_has_type = TRUE;
6277 }
6278 else if (lhs->lhs_lvar != NULL)
6279 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6280 }
6281
Bram Moolenaare42939a2021-04-05 17:11:17 +02006282 if (oplen == 3 && !heredoc
6283 && lhs->lhs_dest != dest_global
6284 && !lhs->lhs_has_index
6285 && lhs->lhs_type->tt_type != VAR_STRING
6286 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006287 {
6288 emsg(_(e_can_only_concatenate_to_string));
6289 return FAIL;
6290 }
6291
6292 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6293 && cctx->ctx_skip != SKIP_YES)
6294 {
6295 if (oplen > 1 && !heredoc)
6296 {
6297 // +=, /=, etc. require an existing variable
6298 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6299 return FAIL;
6300 }
6301 if (!is_decl)
6302 {
6303 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6304 return FAIL;
6305 }
6306
Bram Moolenaar3f327882021-03-17 20:56:38 +01006307 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 if ((lhs->lhs_type->tt_type == VAR_FUNC
6309 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006310 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006311 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006312
6313 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6315 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6316 if (lhs->lhs_lvar == NULL)
6317 return FAIL;
6318 lhs->lhs_new_local = TRUE;
6319 }
6320
6321 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006322 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006323 {
6324 // Something follows after the variable: "var[idx]" or "var.key".
6325 // TODO: should we also handle "->func()" here?
6326 if (is_decl)
6327 {
6328 emsg(_(e_cannot_use_index_when_declaring_variable));
6329 return FAIL;
6330 }
6331
6332 if (var_start[lhs->lhs_varlen] == '['
6333 || var_start[lhs->lhs_varlen] == '.')
6334 {
6335 char_u *after = var_start + lhs->lhs_varlen;
6336 char_u *p;
6337
6338 // Only the last index is used below, if there are others
6339 // before it generate code for the expression. Thus for
6340 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6341 for (;;)
6342 {
6343 p = skip_index(after);
6344 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006345 {
6346 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006347 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006348 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006349 after = p;
6350 }
6351 if (after > var_start + lhs->lhs_varlen)
6352 {
6353 lhs->lhs_varlen = after - var_start;
6354 lhs->lhs_dest = dest_expr;
6355 // We don't know the type before evaluating the expression,
6356 // use "any" until then.
6357 lhs->lhs_type = &t_any;
6358 }
6359
Bram Moolenaar752fc692021-01-04 21:57:11 +01006360 if (lhs->lhs_type->tt_member == NULL)
6361 lhs->lhs_member_type = &t_any;
6362 else
6363 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6364 }
6365 else
6366 {
6367 semsg("Not supported yet: %s", var_start);
6368 return FAIL;
6369 }
6370 }
6371 return OK;
6372}
6373
6374/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006375 * Figure out the LHS and check a few errors.
6376 */
6377 static int
6378compile_assign_lhs(
6379 char_u *var_start,
6380 lhs_T *lhs,
6381 int cmdidx,
6382 int is_decl,
6383 int heredoc,
6384 int oplen,
6385 cctx_T *cctx)
6386{
6387 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6388 return FAIL;
6389
6390 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6391 {
6392 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6393 return FAIL;
6394 }
6395 if (!is_decl && lhs->lhs_lvar != NULL
6396 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6397 {
6398 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6399 return FAIL;
6400 }
6401 return OK;
6402}
6403
6404/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006405 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6406 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006407 */
6408 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006409compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006410 char_u *var_start,
6411 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006412 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006413 cctx_T *cctx)
6414{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006415 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006416 char_u *p;
6417 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006418 int need_white_before = TRUE;
6419 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420
Bram Moolenaar752fc692021-01-04 21:57:11 +01006421 p = var_start + varlen;
6422 if (*p == '[')
6423 {
6424 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006425 if (*p == ':')
6426 {
6427 // empty first index, push zero
6428 r = generate_PUSHNR(cctx, 0);
6429 need_white_before = FALSE;
6430 }
6431 else
6432 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006433
6434 if (r == OK && *skipwhite(p) == ':')
6435 {
6436 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006437 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006438 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006439 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006440 empty_second = *skipwhite(p + 1) == ']';
6441 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6442 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006443 {
6444 semsg(_(e_white_space_required_before_and_after_str_at_str),
6445 ":", p);
6446 return FAIL;
6447 }
6448 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006449 if (*p == ']')
6450 // empty second index, push "none"
6451 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6452 else
6453 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006454 }
6455
Bram Moolenaar752fc692021-01-04 21:57:11 +01006456 if (r == OK && *skipwhite(p) != ']')
6457 {
6458 // this should not happen
6459 emsg(_(e_missbrac));
6460 r = FAIL;
6461 }
6462 }
6463 else // if (*p == '.')
6464 {
6465 char_u *key_end = to_name_end(p + 1, TRUE);
6466 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6467
6468 r = generate_PUSHS(cctx, key);
6469 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006470 return r;
6471}
6472
6473/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006474 * For a LHS with an index, load the variable to be indexed.
6475 */
6476 static int
6477compile_load_lhs(
6478 lhs_T *lhs,
6479 char_u *var_start,
6480 type_T *rhs_type,
6481 cctx_T *cctx)
6482{
6483 if (lhs->lhs_dest == dest_expr)
6484 {
6485 size_t varlen = lhs->lhs_varlen;
6486 int c = var_start[varlen];
6487 char_u *p = var_start;
6488 garray_T *stack = &cctx->ctx_type_stack;
6489
6490 // Evaluate "ll[expr]" of "ll[expr][idx]"
6491 var_start[varlen] = NUL;
6492 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6493 {
6494 // this should not happen
6495 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006496 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006497 return FAIL;
6498 }
6499 var_start[varlen] = c;
6500
6501 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6502 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6503 // now we can properly check the type
6504 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6505 && rhs_type != &t_void
6506 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6507 FALSE, FALSE) == FAIL)
6508 return FAIL;
6509 }
6510 else
6511 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6512 lhs->lhs_lvar, lhs->lhs_type);
6513 return OK;
6514}
6515
6516/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006517 * Produce code for loading "lhs" and also take care of an index.
6518 * Return OK/FAIL.
6519 */
6520 static int
6521compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6522{
6523 compile_load_lhs(lhs, var_start, NULL, cctx);
6524
6525 if (lhs->lhs_has_index)
6526 {
6527 int range = FALSE;
6528
6529 // Get member from list or dict. First compile the
6530 // index value.
6531 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6532 return FAIL;
6533 if (range)
6534 {
6535 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6536 var_start);
6537 return FAIL;
6538 }
6539
6540 // Get the member.
6541 if (compile_member(FALSE, cctx) == FAIL)
6542 return FAIL;
6543 }
6544 return OK;
6545}
6546
6547/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006548 * Assignment to a list or dict member, or ":unlet" for the item, using the
6549 * information in "lhs".
6550 * Returns OK or FAIL.
6551 */
6552 static int
6553compile_assign_unlet(
6554 char_u *var_start,
6555 lhs_T *lhs,
6556 int is_assign,
6557 type_T *rhs_type,
6558 cctx_T *cctx)
6559{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006560 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006561 garray_T *stack = &cctx->ctx_type_stack;
6562 int range = FALSE;
6563
Bram Moolenaar68452172021-04-12 21:21:02 +02006564 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006565 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006566 if (is_assign && range && lhs->lhs_type != &t_blob
6567 && lhs->lhs_type != &t_any)
6568 {
6569 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6570 return FAIL;
6571 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006572
6573 if (lhs->lhs_type == &t_any)
6574 {
6575 // Index on variable of unknown type: check at runtime.
6576 dest_type = VAR_ANY;
6577 }
6578 else
6579 {
6580 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006581 if (dest_type == VAR_DICT && range)
6582 {
6583 emsg(e_cannot_use_range_with_dictionary);
6584 return FAIL;
6585 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006586 if (dest_type == VAR_DICT
6587 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006588 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006589 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006590 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006591 type_T *type;
6592
6593 if (range)
6594 {
6595 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6596 if (need_type(type, &t_number,
6597 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006598 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006599 }
6600 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006601 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006602 && need_type(type, &t_number,
6603 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006604 return FAIL;
6605 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006606 }
6607
6608 // Load the dict or list. On the stack we then have:
6609 // - value (for assignment, not for :unlet)
6610 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006611 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006613 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6614 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006615
Bram Moolenaar68452172021-04-12 21:21:02 +02006616 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6617 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006618 {
6619 if (is_assign)
6620 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006621 if (range)
6622 {
6623 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6624 return FAIL;
6625 }
6626 else
6627 {
6628 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006629
Bram Moolenaar68452172021-04-12 21:21:02 +02006630 if (isn == NULL)
6631 return FAIL;
6632 isn->isn_arg.vartype = dest_type;
6633 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006634 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006635 else if (range)
6636 {
6637 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6638 return FAIL;
6639 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 else
6641 {
6642 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6643 return FAIL;
6644 }
6645 }
6646 else
6647 {
6648 emsg(_(e_indexable_type_required));
6649 return FAIL;
6650 }
6651
6652 return OK;
6653}
6654
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006655/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006656 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006657 * "let name"
6658 * "var name = expr"
6659 * "final name = expr"
6660 * "const name = expr"
6661 * "name = expr"
6662 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006663 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006664 * Return NULL for an error.
6665 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 */
6667 static char_u *
6668compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6669{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006670 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006672 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 char_u *ret = NULL;
6674 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006675 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006677 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006679 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 int oplen = 0;
6682 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006683 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006684 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006686 int is_decl = is_decl_command(cmdidx);
6687 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006688 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690 // Skip over the "var" or "[var, var]" to get to any "=".
6691 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6692 if (p == NULL)
6693 return *arg == '[' ? arg : NULL;
6694
6695 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006697 // TODO: should we allow this, and figure out type inference from list
6698 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006699 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 return NULL;
6701 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006702 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 sp = p;
6705 p = skipwhite(p);
6706 op = p;
6707 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708
6709 if (var_count > 0 && oplen == 0)
6710 // can be something like "[1, 2]->func()"
6711 return arg;
6712
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006713 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006715 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006717 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006718 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6719 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006720 if (VIM_ISWHITE(eap->cmd[2]))
6721 {
6722 semsg(_(e_no_white_space_allowed_after_str_str),
6723 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6724 return NULL;
6725 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006726 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6727 oplen = 2;
6728 incdec = TRUE;
6729 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 if (heredoc)
6732 {
6733 list_T *l;
6734 listitem_T *li;
6735
6736 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006737 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006739 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006740 if (l == NULL)
6741 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742
Bram Moolenaar078269b2020-09-21 20:35:55 +02006743 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006745 // Push each line and the create the list.
6746 FOR_ALL_LIST_ITEMS(l, li)
6747 {
6748 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6749 li->li_tv.vval.v_string = NULL;
6750 }
6751 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 list_free(l);
6754 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006755 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006757 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006759 char_u *wp;
6760
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006761 // for "[var, var] = expr" evaluate the expression here, loop over the
6762 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006763 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006764
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006765 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006766 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006767 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006768 if (compile_expr0(&p, cctx) == FAIL)
6769 return NULL;
6770 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006772 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006774 type_T *stacktype;
6775
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006776 stacktype = stack->ga_len == 0 ? &t_void
6777 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006778 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006780 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006781 goto theend;
6782 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006783 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006784 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006785 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006786 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006787 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6788 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006789 if (stacktype->tt_member != NULL)
6790 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006791 }
6792 }
6793
6794 /*
6795 * Loop over variables in "[var, var] = expr".
6796 * For "var = expr" and "let var: type" this is done only once.
6797 */
6798 if (var_count > 0)
6799 var_start = skipwhite(arg + 1); // skip over the "["
6800 else
6801 var_start = arg;
6802 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6803 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006804 int instr_count = -1;
6805 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006807 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6808 {
6809 // Ignore underscore in "[a, _, b] = list".
6810 if (var_count > 0)
6811 {
6812 var_start = skipwhite(var_start + 2);
6813 continue;
6814 }
6815 emsg(_(e_cannot_use_underscore_here));
6816 goto theend;
6817 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006818 vim_free(lhs.lhs_name);
6819
6820 /*
6821 * Figure out the LHS type and other properties.
6822 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006823 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6824 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006826 if (!heredoc)
6827 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006828 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006829 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006830 if (oplen > 0 && var_count == 0)
6831 {
6832 // skip over the "=" and the expression
6833 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006834 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006835 }
6836 }
6837 else if (oplen > 0)
6838 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006839 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006840 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006841
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006842 // for "+=", "*=", "..=" etc. first load the current value
6843 if (*op != '='
6844 && compile_load_lhs_with_index(&lhs, var_start,
6845 cctx) == FAIL)
6846 goto theend;
6847
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006848 // For "var = expr" evaluate the expression.
6849 if (var_count == 0)
6850 {
6851 int r;
6852
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006853 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006854 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006855 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006856 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006857 r = generate_PUSHNR(cctx, 1);
6858 }
6859 else
6860 {
6861 // Temporarily hide the new local variable here, it is
6862 // not available to this expression.
6863 if (lhs.lhs_new_local)
6864 --cctx->ctx_locals.ga_len;
6865 wp = op + oplen;
6866 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6867 {
6868 if (lhs.lhs_new_local)
6869 ++cctx->ctx_locals.ga_len;
6870 goto theend;
6871 }
6872 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006873 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006874 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006875 if (r == FAIL)
6876 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006877 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006878 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006879 else if (semicolon && var_idx == var_count - 1)
6880 {
6881 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006882 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006883 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6884 goto theend;
6885 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006886 else
6887 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006888 // For "[var, var] = expr" get the "var_idx" item from the
6889 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006890 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006891 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006892 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006893
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006894 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006895 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006896 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006897 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006898 if ((rhs_type->tt_type == VAR_FUNC
6899 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006900 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006901 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006902 goto theend;
6903
Bram Moolenaar752fc692021-01-04 21:57:11 +01006904 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006905 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006906 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006908 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006909 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006910 }
6911 else
6912 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006913 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006914 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006915 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006916 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006917 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006918 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006919 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006920 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006921 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006922 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006924 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006925 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006926 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006927 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006928
Bram Moolenaar77709b12021-04-03 21:01:01 +02006929 // Without operator check type here, otherwise below.
6930 // Use the line number of the assignment.
6931 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006932 if (lhs.lhs_has_index)
6933 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006934 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006935 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006936 goto theend;
6937 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006938 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006939 else
6940 {
6941 type_T *lhs_type = lhs.lhs_member_type;
6942
6943 // Special case: assigning to @# can use a number or a
6944 // string.
6945 if (lhs_type == &t_number_or_string
6946 && rhs_type->tt_type == VAR_NUMBER)
6947 lhs_type = &t_number;
6948 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006949 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006950 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006953 else if (cmdidx == CMD_final)
6954 {
6955 emsg(_(e_final_requires_a_value));
6956 goto theend;
6957 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006958 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006960 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006961 goto theend;
6962 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006963 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006964 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006965 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006966 goto theend;
6967 }
6968 else
6969 {
6970 // variables are always initialized
6971 if (ga_grow(instr, 1) == FAIL)
6972 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006973 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006974 {
6975 case VAR_BOOL:
6976 generate_PUSHBOOL(cctx, VVAL_FALSE);
6977 break;
6978 case VAR_FLOAT:
6979#ifdef FEAT_FLOAT
6980 generate_PUSHF(cctx, 0.0);
6981#endif
6982 break;
6983 case VAR_STRING:
6984 generate_PUSHS(cctx, NULL);
6985 break;
6986 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006987 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006988 break;
6989 case VAR_FUNC:
6990 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6991 break;
6992 case VAR_LIST:
6993 generate_NEWLIST(cctx, 0);
6994 break;
6995 case VAR_DICT:
6996 generate_NEWDICT(cctx, 0);
6997 break;
6998 case VAR_JOB:
6999 generate_PUSHJOB(cctx, NULL);
7000 break;
7001 case VAR_CHANNEL:
7002 generate_PUSHCHANNEL(cctx, NULL);
7003 break;
7004 case VAR_NUMBER:
7005 case VAR_UNKNOWN:
7006 case VAR_ANY:
7007 case VAR_PARTIAL:
7008 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007009 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007010 case VAR_SPECIAL: // cannot happen
7011 generate_PUSHNR(cctx, 0);
7012 break;
7013 }
7014 }
7015 if (var_count == 0)
7016 end = p;
7017 }
7018
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007019 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007020 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007021 break;
7022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007023 if (oplen > 0 && *op != '=')
7024 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007025 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007026 type_T *stacktype;
7027
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007028 if (*op == '.')
7029 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007030 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007031 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007032 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007033 if (
7034#ifdef FEAT_FLOAT
7035 // If variable is float operation with number is OK.
7036 !(expected == &t_float && stacktype == &t_number) &&
7037#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007038 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007039 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007040 goto theend;
7041
7042 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007043 {
7044 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7045 goto theend;
7046 }
7047 else if (*op == '+')
7048 {
7049 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007050 operator_type(lhs.lhs_member_type, stacktype),
7051 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007052 goto theend;
7053 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007054 else if (generate_two_op(cctx, op) == FAIL)
7055 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007058 // Use the line number of the assignment for store instruction.
7059 save_lnum = cctx->ctx_lnum;
7060 cctx->ctx_lnum = start_lnum - 1;
7061
Bram Moolenaar752fc692021-01-04 21:57:11 +01007062 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007063 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007064 // Use the info in "lhs" to store the value at the index in the
7065 // list or dict.
7066 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7067 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007068 {
7069 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007070 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007071 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007072 }
7073 else
7074 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007075 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007076 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007077 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007078 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007079 generate_LOCKCONST(cctx);
7080
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007081 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007082 && (lhs.lhs_type->tt_type == VAR_DICT
7083 || lhs.lhs_type->tt_type == VAR_LIST)
7084 && lhs.lhs_type->tt_member != NULL
7085 && lhs.lhs_type->tt_member != &t_any
7086 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007087 // Set the type in the list or dict, so that it can be checked,
7088 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007089 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007090
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007091 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007092 {
7093 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007094 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007095 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007096 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007097 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007098
7099 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007100 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007102
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007103 // For "[var, var] = expr" drop the "expr" value.
7104 // Also for "[var, var; _] = expr".
7105 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007106 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007107 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007108 goto theend;
7109 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007110
Bram Moolenaarb2097502020-07-19 17:17:02 +02007111 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112
7113theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007114 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 return ret;
7116}
7117
7118/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007119 * Check for an assignment at "eap->cmd", compile it if found.
7120 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7121 */
7122 static int
7123may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7124{
7125 char_u *pskip;
7126 char_u *p;
7127
7128 // Assuming the command starts with a variable or function name,
7129 // find what follows.
7130 // Skip over "var.member", "var[idx]" and the like.
7131 // Also "&opt = val", "$ENV = val" and "@r = val".
7132 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7133 ? eap->cmd + 1 : eap->cmd;
7134 p = to_name_end(pskip, TRUE);
7135 if (p > eap->cmd && *p != NUL)
7136 {
7137 char_u *var_end;
7138 int oplen;
7139 int heredoc;
7140
7141 if (eap->cmd[0] == '@')
7142 var_end = eap->cmd + 2;
7143 else
7144 var_end = find_name_end(pskip, NULL, NULL,
7145 FNE_CHECK_START | FNE_INCL_BR);
7146 oplen = assignment_len(skipwhite(var_end), &heredoc);
7147 if (oplen > 0)
7148 {
7149 size_t len = p - eap->cmd;
7150
7151 // Recognize an assignment if we recognize the variable
7152 // name:
7153 // "g:var = expr"
7154 // "local = expr" where "local" is a local var.
7155 // "script = expr" where "script" is a script-local var.
7156 // "import = expr" where "import" is an imported var
7157 // "&opt = expr"
7158 // "$ENV = expr"
7159 // "@r = expr"
7160 if (*eap->cmd == '&'
7161 || *eap->cmd == '$'
7162 || *eap->cmd == '@'
7163 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007164 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007165 {
7166 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7167 if (*line == NULL || *line == eap->cmd)
7168 return FAIL;
7169 return OK;
7170 }
7171 }
7172 }
7173
7174 if (*eap->cmd == '[')
7175 {
7176 // [var, var] = expr
7177 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7178 if (*line == NULL)
7179 return FAIL;
7180 if (*line != eap->cmd)
7181 return OK;
7182 }
7183 return NOTDONE;
7184}
7185
7186/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007187 * Check if "name" can be "unlet".
7188 */
7189 int
7190check_vim9_unlet(char_u *name)
7191{
7192 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7193 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007194 // "unlet s:var" is allowed in legacy script.
7195 if (*name == 's' && !script_is_vim9())
7196 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007197 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007198 return FAIL;
7199 }
7200 return OK;
7201}
7202
7203/*
7204 * Callback passed to ex_unletlock().
7205 */
7206 static int
7207compile_unlet(
7208 lval_T *lvp,
7209 char_u *name_end,
7210 exarg_T *eap,
7211 int deep UNUSED,
7212 void *coookie)
7213{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007214 cctx_T *cctx = coookie;
7215 char_u *p = lvp->ll_name;
7216 int cc = *name_end;
7217 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007218
Bram Moolenaar752fc692021-01-04 21:57:11 +01007219 if (cctx->ctx_skip == SKIP_YES)
7220 return OK;
7221
7222 *name_end = NUL;
7223 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007224 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007225 // :unlet $ENV_VAR
7226 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7227 }
7228 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7229 {
7230 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007231
Bram Moolenaar752fc692021-01-04 21:57:11 +01007232 // This is similar to assigning: lookup the list/dict, compile the
7233 // idx/key. Then instead of storing the value unlet the item.
7234 // unlet {list}[idx]
7235 // unlet {dict}[key] dict.key
7236 //
7237 // Figure out the LHS type and other properties.
7238 //
7239 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7240
7241 // : unlet an indexed item
7242 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007243 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007244 iemsg("called compile_lhs() without an index");
7245 ret = FAIL;
7246 }
7247 else
7248 {
7249 // Use the info in "lhs" to unlet the item at the index in the
7250 // list or dict.
7251 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007252 }
7253
Bram Moolenaar752fc692021-01-04 21:57:11 +01007254 vim_free(lhs.lhs_name);
7255 }
7256 else if (check_vim9_unlet(p) == FAIL)
7257 {
7258 ret = FAIL;
7259 }
7260 else
7261 {
7262 // Normal name. Only supports g:, w:, t: and b: namespaces.
7263 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007264 }
7265
Bram Moolenaar752fc692021-01-04 21:57:11 +01007266 *name_end = cc;
7267 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007268}
7269
7270/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007271 * Callback passed to ex_unletlock().
7272 */
7273 static int
7274compile_lock_unlock(
7275 lval_T *lvp,
7276 char_u *name_end,
7277 exarg_T *eap,
7278 int deep UNUSED,
7279 void *coookie)
7280{
7281 cctx_T *cctx = coookie;
7282 int cc = *name_end;
7283 char_u *p = lvp->ll_name;
7284 int ret = OK;
7285 size_t len;
7286 char_u *buf;
7287
7288 if (cctx->ctx_skip == SKIP_YES)
7289 return OK;
7290
7291 // Cannot use :lockvar and :unlockvar on local variables.
7292 if (p[1] != ':')
7293 {
7294 char_u *end = skip_var_one(p, FALSE);
7295
7296 if (lookup_local(p, end - p, NULL, cctx) == OK)
7297 {
7298 emsg(_(e_cannot_lock_unlock_local_variable));
7299 return FAIL;
7300 }
7301 }
7302
7303 // Checking is done at runtime.
7304 *name_end = NUL;
7305 len = name_end - p + 20;
7306 buf = alloc(len);
7307 if (buf == NULL)
7308 ret = FAIL;
7309 else
7310 {
7311 vim_snprintf((char *)buf, len, "%s %s",
7312 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7313 p);
7314 ret = generate_EXEC(cctx, buf);
7315
7316 vim_free(buf);
7317 *name_end = cc;
7318 }
7319 return ret;
7320}
7321
7322/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007323 * compile "unlet var", "lock var" and "unlock var"
7324 * "arg" points to "var".
7325 */
7326 static char_u *
7327compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7328{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007329 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7330 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7331 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007332 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7333}
7334
7335/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7337 */
7338 static int
7339compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7340{
7341 garray_T *instr = &cctx->ctx_instr;
7342 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7343
7344 if (endlabel == NULL)
7345 return FAIL;
7346 endlabel->el_next = *el;
7347 *el = endlabel;
7348 endlabel->el_end_label = instr->ga_len;
7349
7350 generate_JUMP(cctx, when, 0);
7351 return OK;
7352}
7353
7354 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007355compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356{
7357 garray_T *instr = &cctx->ctx_instr;
7358
7359 while (*el != NULL)
7360 {
7361 endlabel_T *cur = (*el);
7362 isn_T *isn;
7363
7364 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007365 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 *el = cur->el_next;
7367 vim_free(cur);
7368 }
7369}
7370
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007371 static void
7372compile_free_jump_to_end(endlabel_T **el)
7373{
7374 while (*el != NULL)
7375 {
7376 endlabel_T *cur = (*el);
7377
7378 *el = cur->el_next;
7379 vim_free(cur);
7380 }
7381}
7382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383/*
7384 * Create a new scope and set up the generic items.
7385 */
7386 static scope_T *
7387new_scope(cctx_T *cctx, scopetype_T type)
7388{
7389 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7390
7391 if (scope == NULL)
7392 return NULL;
7393 scope->se_outer = cctx->ctx_scope;
7394 cctx->ctx_scope = scope;
7395 scope->se_type = type;
7396 scope->se_local_count = cctx->ctx_locals.ga_len;
7397 return scope;
7398}
7399
7400/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007401 * Free the current scope and go back to the outer scope.
7402 */
7403 static void
7404drop_scope(cctx_T *cctx)
7405{
7406 scope_T *scope = cctx->ctx_scope;
7407
7408 if (scope == NULL)
7409 {
7410 iemsg("calling drop_scope() without a scope");
7411 return;
7412 }
7413 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007414 switch (scope->se_type)
7415 {
7416 case IF_SCOPE:
7417 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7418 case FOR_SCOPE:
7419 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7420 case WHILE_SCOPE:
7421 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7422 case TRY_SCOPE:
7423 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7424 case NO_SCOPE:
7425 case BLOCK_SCOPE:
7426 break;
7427 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007428 vim_free(scope);
7429}
7430
7431/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 * compile "if expr"
7433 *
7434 * "if expr" Produces instructions:
7435 * EVAL expr Push result of "expr"
7436 * JUMP_IF_FALSE end
7437 * ... body ...
7438 * end:
7439 *
7440 * "if expr | else" Produces instructions:
7441 * EVAL expr Push result of "expr"
7442 * JUMP_IF_FALSE else
7443 * ... body ...
7444 * JUMP_ALWAYS end
7445 * else:
7446 * ... body ...
7447 * end:
7448 *
7449 * "if expr1 | elseif expr2 | else" Produces instructions:
7450 * EVAL expr Push result of "expr"
7451 * JUMP_IF_FALSE elseif
7452 * ... body ...
7453 * JUMP_ALWAYS end
7454 * elseif:
7455 * EVAL expr Push result of "expr"
7456 * JUMP_IF_FALSE else
7457 * ... body ...
7458 * JUMP_ALWAYS end
7459 * else:
7460 * ... body ...
7461 * end:
7462 */
7463 static char_u *
7464compile_if(char_u *arg, cctx_T *cctx)
7465{
7466 char_u *p = arg;
7467 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007468 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007470 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007471 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
Bram Moolenaara5565e42020-05-09 15:44:01 +02007473 CLEAR_FIELD(ppconst);
7474 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007475 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007476 clear_ppconst(&ppconst);
7477 return NULL;
7478 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007479 if (!ends_excmd2(arg, skipwhite(p)))
7480 {
7481 semsg(_(e_trailing_arg), p);
7482 return NULL;
7483 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007484 if (cctx->ctx_skip == SKIP_YES)
7485 clear_ppconst(&ppconst);
7486 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007487 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007488 int error = FALSE;
7489 int v;
7490
Bram Moolenaara5565e42020-05-09 15:44:01 +02007491 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007492 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007493 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007494 if (error)
7495 return NULL;
7496 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007497 }
7498 else
7499 {
7500 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007501 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007502 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007503 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007504 if (bool_on_stack(cctx) == FAIL)
7505 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507
Bram Moolenaara91a7132021-03-25 21:12:15 +01007508 // CMDMOD_REV must come before the jump
7509 generate_undo_cmdmods(cctx);
7510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 scope = new_scope(cctx, IF_SCOPE);
7512 if (scope == NULL)
7513 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007514 scope->se_skip_save = skip_save;
7515 // "is_had_return" will be reset if any block does not end in :return
7516 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007518 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007519 {
7520 // "where" is set when ":elseif", "else" or ":endif" is found
7521 scope->se_u.se_if.is_if_label = instr->ga_len;
7522 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7523 }
7524 else
7525 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526
Bram Moolenaarced68a02021-01-24 17:53:47 +01007527#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007528 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007529 && skip_save != SKIP_YES)
7530 {
7531 // generated a profile start, need to generate a profile end, since it
7532 // won't be done after returning
7533 cctx->ctx_skip = SKIP_NOT;
7534 generate_instr(cctx, ISN_PROF_END);
7535 cctx->ctx_skip = SKIP_YES;
7536 }
7537#endif
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 return p;
7540}
7541
7542 static char_u *
7543compile_elseif(char_u *arg, cctx_T *cctx)
7544{
7545 char_u *p = arg;
7546 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007547 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 isn_T *isn;
7549 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007550 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007551 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552
7553 if (scope == NULL || scope->se_type != IF_SCOPE)
7554 {
7555 emsg(_(e_elseif_without_if));
7556 return NULL;
7557 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007558 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007559 if (!cctx->ctx_had_return)
7560 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561
Bram Moolenaarced68a02021-01-24 17:53:47 +01007562 if (cctx->ctx_skip == SKIP_NOT)
7563 {
7564 // previous block was executed, this one and following will not
7565 cctx->ctx_skip = SKIP_YES;
7566 scope->se_u.se_if.is_seen_skip_not = TRUE;
7567 }
7568 if (scope->se_u.se_if.is_seen_skip_not)
7569 {
7570 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007571 // Do not count the "elseif" for profiling and cmdmod
7572 instr->ga_len = current_instr_idx(cctx);
7573
Bram Moolenaarced68a02021-01-24 17:53:47 +01007574 skip_expr_cctx(&p, cctx);
7575 return p;
7576 }
7577
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007578 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007579 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007580 int moved_cmdmod = FALSE;
7581
7582 // Move any CMDMOD instruction to after the jump
7583 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7584 {
7585 if (ga_grow(instr, 1) == FAIL)
7586 return NULL;
7587 ((isn_T *)instr->ga_data)[instr->ga_len] =
7588 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7589 --instr->ga_len;
7590 moved_cmdmod = TRUE;
7591 }
7592
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007593 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007595 return NULL;
7596 // previous "if" or "elseif" jumps here
7597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7598 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007599 if (moved_cmdmod)
7600 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007603 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007604 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007605 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007606 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007607 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007608#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007609 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007610 {
7611 // the previous block was skipped, need to profile this line
7612 generate_instr(cctx, ISN_PROF_START);
7613 instr_count = instr->ga_len;
7614 }
7615#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007616 if (cctx->ctx_compile_type == CT_DEBUG)
7617 {
7618 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007619 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007620 instr_count = instr->ga_len;
7621 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007622 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007623 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007624 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007625 clear_ppconst(&ppconst);
7626 return NULL;
7627 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007628 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007629 if (!ends_excmd2(arg, skipwhite(p)))
7630 {
7631 semsg(_(e_trailing_arg), p);
7632 return NULL;
7633 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007634 if (scope->se_skip_save == SKIP_YES)
7635 clear_ppconst(&ppconst);
7636 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007637 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007638 int error = FALSE;
7639 int v;
7640
Bram Moolenaar7f141552020-05-09 17:35:53 +02007641 // The expression results in a constant.
7642 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007643 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7644 if (error)
7645 return NULL;
7646 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007647 clear_ppconst(&ppconst);
7648 scope->se_u.se_if.is_if_label = -1;
7649 }
7650 else
7651 {
7652 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007653 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007654 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007655 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007656 if (bool_on_stack(cctx) == FAIL)
7657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658
Bram Moolenaara91a7132021-03-25 21:12:15 +01007659 // CMDMOD_REV must come before the jump
7660 generate_undo_cmdmods(cctx);
7661
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007662 // "where" is set when ":elseif", "else" or ":endif" is found
7663 scope->se_u.se_if.is_if_label = instr->ga_len;
7664 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666
7667 return p;
7668}
7669
7670 static char_u *
7671compile_else(char_u *arg, cctx_T *cctx)
7672{
7673 char_u *p = arg;
7674 garray_T *instr = &cctx->ctx_instr;
7675 isn_T *isn;
7676 scope_T *scope = cctx->ctx_scope;
7677
7678 if (scope == NULL || scope->se_type != IF_SCOPE)
7679 {
7680 emsg(_(e_else_without_if));
7681 return NULL;
7682 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007683 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007684 if (!cctx->ctx_had_return)
7685 scope->se_u.se_if.is_had_return = FALSE;
7686 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687
Bram Moolenaarced68a02021-01-24 17:53:47 +01007688#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007689 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007690 {
7691 if (cctx->ctx_skip == SKIP_NOT
7692 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7693 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007694 // the previous block was executed, do not count "else" for
7695 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007696 --instr->ga_len;
7697 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7698 {
7699 // the previous block was not executed, this one will, do count the
7700 // "else" for profiling
7701 cctx->ctx_skip = SKIP_NOT;
7702 generate_instr(cctx, ISN_PROF_END);
7703 generate_instr(cctx, ISN_PROF_START);
7704 cctx->ctx_skip = SKIP_YES;
7705 }
7706 }
7707#endif
7708
7709 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007710 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007711 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007712 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007713 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007714 if (!cctx->ctx_had_return
7715 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7716 JUMP_ALWAYS, cctx) == FAIL)
7717 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007718 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007719
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007720 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007721 {
7722 if (scope->se_u.se_if.is_if_label >= 0)
7723 {
7724 // previous "if" or "elseif" jumps here
7725 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7726 isn->isn_arg.jump.jump_where = instr->ga_len;
7727 scope->se_u.se_if.is_if_label = -1;
7728 }
7729 }
7730
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007731 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007732 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734
7735 return p;
7736}
7737
7738 static char_u *
7739compile_endif(char_u *arg, cctx_T *cctx)
7740{
7741 scope_T *scope = cctx->ctx_scope;
7742 ifscope_T *ifscope;
7743 garray_T *instr = &cctx->ctx_instr;
7744 isn_T *isn;
7745
Bram Moolenaarfa984412021-03-25 22:15:28 +01007746 if (misplaced_cmdmod(cctx))
7747 return NULL;
7748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 if (scope == NULL || scope->se_type != IF_SCOPE)
7750 {
7751 emsg(_(e_endif_without_if));
7752 return NULL;
7753 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007754 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007755 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007756 if (!cctx->ctx_had_return)
7757 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007759 if (scope->se_u.se_if.is_if_label >= 0)
7760 {
7761 // previous "if" or "elseif" jumps here
7762 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7763 isn->isn_arg.jump.jump_where = instr->ga_len;
7764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007766 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007767
7768#ifdef FEAT_PROFILE
7769 // even when skipping we count the endif as executed, unless the block it's
7770 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007771 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007772 && scope->se_skip_save != SKIP_YES)
7773 {
7774 cctx->ctx_skip = SKIP_NOT;
7775 generate_instr(cctx, ISN_PROF_START);
7776 }
7777#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007778 cctx->ctx_skip = scope->se_skip_save;
7779
7780 // If all the blocks end in :return and there is an :else then the
7781 // had_return flag is set.
7782 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007784 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 return arg;
7786}
7787
7788/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007789 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 *
7791 * Produces instructions:
7792 * PUSHNR -1
7793 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007794 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7796 * - if beyond end, jump to "end"
7797 * - otherwise get item from list and push it
7798 * STORE var Store item in "var"
7799 * ... body ...
7800 * JUMP top Jump back to repeat
7801 * end: DROP Drop the result of "expr"
7802 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007803 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7804 * UNPACK 2 Split item in 2
7805 * STORE var1 Store item in "var1"
7806 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 */
7808 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007809compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007811 char_u *arg;
7812 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007813 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007815 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007816 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007817 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007818 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007821 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007823 lvar_T *loop_lvar; // loop iteration variable
7824 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007826 type_T *item_type = &t_any;
7827 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007828 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829
Bram Moolenaar792f7862020-11-23 08:31:18 +01007830 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007831 if (p == NULL)
7832 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007833 if (var_count == 0)
7834 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007835 else
7836 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837
7838 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007839 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007840 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7841 return NULL;
7842 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007844 if (*p == ':' && wp != p)
7845 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7846 else
7847 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 return NULL;
7849 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007850 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007851 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7852 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007854 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7855 // instruction.
7856 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7857 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7858 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007859 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007860 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007861 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7862 .isn_arg.debug.dbg_break_lnum;
7863 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 scope = new_scope(cctx, FOR_SCOPE);
7866 if (scope == NULL)
7867 return NULL;
7868
Bram Moolenaar792f7862020-11-23 08:31:18 +01007869 // Reserve a variable to store the loop iteration counter and initialize it
7870 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007871 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7872 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007873 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007874 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007875 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007877 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007878 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879
7880 // compile "expr", it remains on the stack until "endfor"
7881 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007882 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007883 {
7884 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007886 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007887 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007889 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007890 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007892 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007893 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007895 semsg(_(e_for_loop_on_str_not_supported),
7896 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007897 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 return NULL;
7899 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007900
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007901 if (vartype->tt_type == VAR_STRING)
7902 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007903 else if (vartype->tt_type == VAR_BLOB)
7904 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007905 else if (vartype->tt_type == VAR_LIST
7906 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007907 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007908 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007909 item_type = vartype->tt_member;
7910 else if (vartype->tt_member->tt_type == VAR_LIST
7911 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007912 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007913 item_type = vartype->tt_member->tt_member;
7914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007916 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007917 generate_undo_cmdmods(cctx);
7918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007920 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007921
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007922 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923
Bram Moolenaar792f7862020-11-23 08:31:18 +01007924 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007925 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007926 {
7927 generate_UNPACK(cctx, var_count, semicolon);
7928 arg = skipwhite(arg + 1); // skip white after '['
7929
7930 // the list item is replaced by a number of items
7931 if (ga_grow(stack, var_count - 1) == FAIL)
7932 {
7933 drop_scope(cctx);
7934 return NULL;
7935 }
7936 --stack->ga_len;
7937 for (idx = 0; idx < var_count; ++idx)
7938 {
7939 ((type_T **)stack->ga_data)[stack->ga_len] =
7940 (semicolon && idx == 0) ? vartype : item_type;
7941 ++stack->ga_len;
7942 }
7943 }
7944
7945 for (idx = 0; idx < var_count; ++idx)
7946 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007947 assign_dest_T dest = dest_local;
7948 int opt_flags = 0;
7949 int vimvaridx = -1;
7950 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007951 type_T *lhs_type = &t_any;
7952 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007953
7954 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007955 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007956 name = vim_strnsave(arg, varlen);
7957 if (name == NULL)
7958 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007959 if (*p == ':')
7960 {
7961 p = skipwhite(p + 1);
7962 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7963 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007964
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007965 // TODO: script var not supported?
7966 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7967 &vimvaridx, &type, cctx) == FAIL)
7968 goto failed;
7969 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007970 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007971 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7972 0, 0, type, name) == FAIL)
7973 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007974 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007975 else if (varlen == 1 && *arg == '_')
7976 {
7977 // Assigning to "_": drop the value.
7978 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7979 goto failed;
7980 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007981 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007982 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007983 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007984 {
7985 semsg(_(e_variable_already_declared), arg);
7986 goto failed;
7987 }
7988
Bram Moolenaarea870692020-12-02 14:24:30 +01007989 if (STRNCMP(name, "s:", 2) == 0)
7990 {
7991 semsg(_(e_cannot_declare_script_variable_in_function), name);
7992 goto failed;
7993 }
7994
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007995 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007996 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007997 where.wt_variable = TRUE;
7998 if (lhs_type == &t_any)
7999 lhs_type = item_type;
8000 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008001 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008002 ? need_type(item_type, lhs_type,
8003 -1, 0, cctx, FALSE, FALSE)
8004 : check_type(lhs_type, item_type, TRUE, where))
8005 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008006 goto failed;
8007 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008008 if (var_lvar == NULL)
8009 // out of memory or used as an argument
8010 goto failed;
8011
8012 if (semicolon && idx == var_count - 1)
8013 var_lvar->lv_type = vartype;
8014 else
8015 var_lvar->lv_type = item_type;
8016 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8017 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008018
8019 if (*p == ',' || *p == ';')
8020 ++p;
8021 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008022 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008023 }
8024
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008025 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008026 {
8027 int save_prev_lnum = cctx->ctx_prev_lnum;
8028
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008029 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008030 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8031 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008032 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008033 cctx->ctx_prev_lnum = save_prev_lnum;
8034 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008035
Bram Moolenaar792f7862020-11-23 08:31:18 +01008036 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008037
8038failed:
8039 vim_free(name);
8040 drop_scope(cctx);
8041 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042}
8043
8044/*
8045 * compile "endfor"
8046 */
8047 static char_u *
8048compile_endfor(char_u *arg, cctx_T *cctx)
8049{
8050 garray_T *instr = &cctx->ctx_instr;
8051 scope_T *scope = cctx->ctx_scope;
8052 forscope_T *forscope;
8053 isn_T *isn;
8054
Bram Moolenaarfa984412021-03-25 22:15:28 +01008055 if (misplaced_cmdmod(cctx))
8056 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 if (scope == NULL || scope->se_type != FOR_SCOPE)
8059 {
8060 emsg(_(e_for));
8061 return NULL;
8062 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008063 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008065 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066
8067 // At end of ":for" scope jump back to the FOR instruction.
8068 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8069
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008070 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8072 isn->isn_arg.forloop.for_end = instr->ga_len;
8073
8074 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008075 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076
8077 // Below the ":for" scope drop the "expr" list from the stack.
8078 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8079 return NULL;
8080
8081 vim_free(scope);
8082
8083 return arg;
8084}
8085
8086/*
8087 * compile "while expr"
8088 *
8089 * Produces instructions:
8090 * top: EVAL expr Push result of "expr"
8091 * JUMP_IF_FALSE end jump if false
8092 * ... body ...
8093 * JUMP top Jump back to repeat
8094 * end:
8095 *
8096 */
8097 static char_u *
8098compile_while(char_u *arg, cctx_T *cctx)
8099{
8100 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008101 scope_T *scope;
8102
8103 scope = new_scope(cctx, WHILE_SCOPE);
8104 if (scope == NULL)
8105 return NULL;
8106
Bram Moolenaara91a7132021-03-25 21:12:15 +01008107 // "endwhile" jumps back here, one before when profiling or using cmdmods
8108 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109
8110 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008111 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008113 if (!ends_excmd2(arg, skipwhite(p)))
8114 {
8115 semsg(_(e_trailing_arg), p);
8116 return NULL;
8117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008118
Bram Moolenaar13106602020-10-04 16:06:05 +02008119 if (bool_on_stack(cctx) == FAIL)
8120 return FAIL;
8121
Bram Moolenaara91a7132021-03-25 21:12:15 +01008122 // CMDMOD_REV must come before the jump
8123 generate_undo_cmdmods(cctx);
8124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008125 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008126 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 JUMP_IF_FALSE, cctx) == FAIL)
8128 return FAIL;
8129
8130 return p;
8131}
8132
8133/*
8134 * compile "endwhile"
8135 */
8136 static char_u *
8137compile_endwhile(char_u *arg, cctx_T *cctx)
8138{
8139 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008140 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141
Bram Moolenaarfa984412021-03-25 22:15:28 +01008142 if (misplaced_cmdmod(cctx))
8143 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8145 {
8146 emsg(_(e_while));
8147 return NULL;
8148 }
8149 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008150 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151
Bram Moolenaarf002a412021-01-24 13:34:18 +01008152#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008153 // count the endwhile before jumping
8154 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008155#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008158 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159
8160 // Fill in the "end" label in the WHILE statement so it can jump here.
8161 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008162 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8163 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164
8165 vim_free(scope);
8166
8167 return arg;
8168}
8169
8170/*
8171 * compile "continue"
8172 */
8173 static char_u *
8174compile_continue(char_u *arg, cctx_T *cctx)
8175{
8176 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008177 int try_scopes = 0;
8178 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179
8180 for (;;)
8181 {
8182 if (scope == NULL)
8183 {
8184 emsg(_(e_continue));
8185 return NULL;
8186 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008187 if (scope->se_type == FOR_SCOPE)
8188 {
8189 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008191 }
8192 if (scope->se_type == WHILE_SCOPE)
8193 {
8194 loop_label = scope->se_u.se_while.ws_top_label;
8195 break;
8196 }
8197 if (scope->se_type == TRY_SCOPE)
8198 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 scope = scope->se_outer;
8200 }
8201
Bram Moolenaarc150c092021-02-13 15:02:46 +01008202 if (try_scopes > 0)
8203 // Inside one or more try/catch blocks we first need to jump to the
8204 // "finally" or "endtry" to cleanup.
8205 generate_TRYCONT(cctx, try_scopes, loop_label);
8206 else
8207 // Jump back to the FOR or WHILE instruction.
8208 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 return arg;
8211}
8212
8213/*
8214 * compile "break"
8215 */
8216 static char_u *
8217compile_break(char_u *arg, cctx_T *cctx)
8218{
8219 scope_T *scope = cctx->ctx_scope;
8220 endlabel_T **el;
8221
8222 for (;;)
8223 {
8224 if (scope == NULL)
8225 {
8226 emsg(_(e_break));
8227 return NULL;
8228 }
8229 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8230 break;
8231 scope = scope->se_outer;
8232 }
8233
8234 // Jump to the end of the FOR or WHILE loop.
8235 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008236 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008238 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008239 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8240 return FAIL;
8241
8242 return arg;
8243}
8244
8245/*
8246 * compile "{" start of block
8247 */
8248 static char_u *
8249compile_block(char_u *arg, cctx_T *cctx)
8250{
8251 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8252 return NULL;
8253 return skipwhite(arg + 1);
8254}
8255
8256/*
8257 * compile end of block: drop one scope
8258 */
8259 static void
8260compile_endblock(cctx_T *cctx)
8261{
8262 scope_T *scope = cctx->ctx_scope;
8263
8264 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008265 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 vim_free(scope);
8267}
8268
8269/*
8270 * compile "try"
8271 * Creates a new scope for the try-endtry, pointing to the first catch and
8272 * finally.
8273 * Creates another scope for the "try" block itself.
8274 * TRY instruction sets up exception handling at runtime.
8275 *
8276 * "try"
8277 * TRY -> catch1, -> finally push trystack entry
8278 * ... try block
8279 * "throw {exception}"
8280 * EVAL {exception}
8281 * THROW create exception
8282 * ... try block
8283 * " catch {expr}"
8284 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008285 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 * EVAL {expr}
8287 * MATCH
8288 * JUMP nomatch -> catch2
8289 * CATCH remove exception
8290 * ... catch block
8291 * " catch"
8292 * JUMP -> finally
8293 * catch2: CATCH remove exception
8294 * ... catch block
8295 * " finally"
8296 * finally:
8297 * ... finally block
8298 * " endtry"
8299 * ENDTRY pop trystack entry, may rethrow
8300 */
8301 static char_u *
8302compile_try(char_u *arg, cctx_T *cctx)
8303{
8304 garray_T *instr = &cctx->ctx_instr;
8305 scope_T *try_scope;
8306 scope_T *scope;
8307
Bram Moolenaarfa984412021-03-25 22:15:28 +01008308 if (misplaced_cmdmod(cctx))
8309 return NULL;
8310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311 // scope that holds the jumps that go to catch/finally/endtry
8312 try_scope = new_scope(cctx, TRY_SCOPE);
8313 if (try_scope == NULL)
8314 return NULL;
8315
Bram Moolenaar69f70502021-01-01 16:10:46 +01008316 if (cctx->ctx_skip != SKIP_YES)
8317 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008318 isn_T *isn;
8319
8320 // "try_catch" is set when the first ":catch" is found or when no catch
8321 // is found and ":finally" is found.
8322 // "try_finally" is set when ":finally" is found
8323 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008324 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008325 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8326 return NULL;
8327 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8328 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008329 return NULL;
8330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331
8332 // scope for the try block itself
8333 scope = new_scope(cctx, BLOCK_SCOPE);
8334 if (scope == NULL)
8335 return NULL;
8336
8337 return arg;
8338}
8339
8340/*
8341 * compile "catch {expr}"
8342 */
8343 static char_u *
8344compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8345{
8346 scope_T *scope = cctx->ctx_scope;
8347 garray_T *instr = &cctx->ctx_instr;
8348 char_u *p;
8349 isn_T *isn;
8350
Bram Moolenaarfa984412021-03-25 22:15:28 +01008351 if (misplaced_cmdmod(cctx))
8352 return NULL;
8353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 // end block scope from :try or :catch
8355 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8356 compile_endblock(cctx);
8357 scope = cctx->ctx_scope;
8358
8359 // Error if not in a :try scope
8360 if (scope == NULL || scope->se_type != TRY_SCOPE)
8361 {
8362 emsg(_(e_catch));
8363 return NULL;
8364 }
8365
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008366 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008367 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008368 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008369 return NULL;
8370 }
8371
Bram Moolenaar69f70502021-01-01 16:10:46 +01008372 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008374#ifdef FEAT_PROFILE
8375 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008376 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008377 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008378 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008379 .isn_type == ISN_PROF_START)
8380 --instr->ga_len;
8381#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008382 // Jump from end of previous block to :finally or :endtry
8383 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8384 JUMP_ALWAYS, cctx) == FAIL)
8385 return NULL;
8386
8387 // End :try or :catch scope: set value in ISN_TRY instruction
8388 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008389 if (isn->isn_arg.try.try_ref->try_catch == 0)
8390 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008391 if (scope->se_u.se_try.ts_catch_label != 0)
8392 {
8393 // Previous catch without match jumps here
8394 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8395 isn->isn_arg.jump.jump_where = instr->ga_len;
8396 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008397#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008398 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008399 {
8400 // a "throw" that jumps here needs to be counted
8401 generate_instr(cctx, ISN_PROF_END);
8402 // the "catch" is also counted
8403 generate_instr(cctx, ISN_PROF_START);
8404 }
8405#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008406 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008407 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408 }
8409
8410 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008411 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008413 scope->se_u.se_try.ts_caught_all = TRUE;
8414 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 }
8416 else
8417 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008418 char_u *end;
8419 char_u *pat;
8420 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008421 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008422 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424 // Push v:exception, push {expr} and MATCH
8425 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8426
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008427 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008428 if (*end != *p)
8429 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008430 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008431 vim_free(tofree);
8432 return FAIL;
8433 }
8434 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008435 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008436 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008437 len = (int)(end - tofree);
8438 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008439 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008440 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008441 if (pat == NULL)
8442 return FAIL;
8443 if (generate_PUSHS(cctx, pat) == FAIL)
8444 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8447 return NULL;
8448
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008449 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8451 return NULL;
8452 }
8453
Bram Moolenaar69f70502021-01-01 16:10:46 +01008454 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455 return NULL;
8456
8457 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8458 return NULL;
8459 return p;
8460}
8461
8462 static char_u *
8463compile_finally(char_u *arg, cctx_T *cctx)
8464{
8465 scope_T *scope = cctx->ctx_scope;
8466 garray_T *instr = &cctx->ctx_instr;
8467 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008468 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008469
Bram Moolenaarfa984412021-03-25 22:15:28 +01008470 if (misplaced_cmdmod(cctx))
8471 return NULL;
8472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 // end block scope from :try or :catch
8474 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8475 compile_endblock(cctx);
8476 scope = cctx->ctx_scope;
8477
8478 // Error if not in a :try scope
8479 if (scope == NULL || scope->se_type != TRY_SCOPE)
8480 {
8481 emsg(_(e_finally));
8482 return NULL;
8483 }
8484
8485 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008486 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008487 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 {
8489 emsg(_(e_finally_dup));
8490 return NULL;
8491 }
8492
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008493 this_instr = instr->ga_len;
8494#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008495 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008496 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008497 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008498 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008499 // jump to the profile start of the "finally"
8500 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008501
8502 // jump to the profile end above it
8503 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8504 .isn_type == ISN_PROF_END)
8505 --this_instr;
8506 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008507#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008509 // Fill in the "end" label in jumps at the end of the blocks.
8510 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8511 this_instr, cctx);
8512
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008513 // If there is no :catch then an exception jumps to :finally.
8514 if (isn->isn_arg.try.try_ref->try_catch == 0)
8515 isn->isn_arg.try.try_ref->try_catch = this_instr;
8516 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008517 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008518 {
8519 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008520 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008521 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008522 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008524 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8525 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008527 // TODO: set index in ts_finally_label jumps
8528
8529 return arg;
8530}
8531
8532 static char_u *
8533compile_endtry(char_u *arg, cctx_T *cctx)
8534{
8535 scope_T *scope = cctx->ctx_scope;
8536 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008537 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538
Bram Moolenaarfa984412021-03-25 22:15:28 +01008539 if (misplaced_cmdmod(cctx))
8540 return NULL;
8541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 // end block scope from :catch or :finally
8543 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8544 compile_endblock(cctx);
8545 scope = cctx->ctx_scope;
8546
8547 // Error if not in a :try scope
8548 if (scope == NULL || scope->se_type != TRY_SCOPE)
8549 {
8550 if (scope == NULL)
8551 emsg(_(e_no_endtry));
8552 else if (scope->se_type == WHILE_SCOPE)
8553 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008554 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008555 emsg(_(e_endfor));
8556 else
8557 emsg(_(e_endif));
8558 return NULL;
8559 }
8560
Bram Moolenaarc150c092021-02-13 15:02:46 +01008561 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008562 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008564 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8565 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008566 {
8567 emsg(_(e_missing_catch_or_finally));
8568 return NULL;
8569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008571#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008572 if (cctx->ctx_compile_type == CT_PROFILE
8573 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008574 .isn_type == ISN_PROF_START)
8575 // move the profile start after "endtry" so that it's not counted when
8576 // the exception is rethrown.
8577 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008578#endif
8579
Bram Moolenaar69f70502021-01-01 16:10:46 +01008580 // Fill in the "end" label in jumps at the end of the blocks, if not
8581 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008582 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8583 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008584
Bram Moolenaar69f70502021-01-01 16:10:46 +01008585 if (scope->se_u.se_try.ts_catch_label != 0)
8586 {
8587 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008588 isn_T *isn = ((isn_T *)instr->ga_data)
8589 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008590 isn->isn_arg.jump.jump_where = instr->ga_len;
8591 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008592 }
8593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 compile_endblock(cctx);
8595
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008596 if (cctx->ctx_skip != SKIP_YES)
8597 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008598 // End :catch or :finally scope: set instruction index in ISN_TRY
8599 // instruction
8600 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008601 if (cctx->ctx_skip != SKIP_YES
8602 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8603 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008604#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008605 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008606 generate_instr(cctx, ISN_PROF_START);
8607#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 return arg;
8610}
8611
8612/*
8613 * compile "throw {expr}"
8614 */
8615 static char_u *
8616compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8617{
8618 char_u *p = skipwhite(arg);
8619
Bram Moolenaara5565e42020-05-09 15:44:01 +02008620 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008621 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008622 if (cctx->ctx_skip == SKIP_YES)
8623 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008624 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008625 return NULL;
8626 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8627 return NULL;
8628
8629 return p;
8630}
8631
Bram Moolenaarc3235272021-07-10 19:42:03 +02008632 static char_u *
8633compile_eval(char_u *arg, cctx_T *cctx)
8634{
8635 char_u *p = arg;
8636 int name_only;
8637 char_u *alias;
8638 long lnum = SOURCING_LNUM;
8639
8640 // find_ex_command() will consider a variable name an expression, assuming
8641 // that something follows on the next line. Check that something actually
8642 // follows, otherwise it's probably a misplaced command.
8643 get_name_len(&p, &alias, FALSE, FALSE);
8644 name_only = ends_excmd2(arg, skipwhite(p));
8645 vim_free(alias);
8646
8647 p = arg;
8648 if (compile_expr0(&p, cctx) == FAIL)
8649 return NULL;
8650
8651 if (name_only && lnum == SOURCING_LNUM)
8652 {
8653 semsg(_(e_expression_without_effect_str), arg);
8654 return NULL;
8655 }
8656
8657 // drop the result
8658 generate_instr_drop(cctx, ISN_DROP, 1);
8659
8660 return skipwhite(p);
8661}
8662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663/*
8664 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008665 * compile "echomsg expr"
8666 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008667 * compile "execute expr"
8668 */
8669 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008670compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008671{
8672 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008673 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008674 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008675 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008676 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008677 garray_T *stack = &cctx->ctx_type_stack;
8678 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008679
8680 for (;;)
8681 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008682 if (ends_excmd2(prev, p))
8683 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008684 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008685 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008686 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008687
8688 if (cctx->ctx_skip != SKIP_YES)
8689 {
8690 // check for non-void type
8691 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8692 if (type->tt_type == VAR_VOID)
8693 {
8694 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8695 return NULL;
8696 }
8697 }
8698
Bram Moolenaarad39c092020-02-26 18:23:43 +01008699 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008700 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008701 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008702 }
8703
Bram Moolenaare4984292020-12-13 14:19:25 +01008704 if (count > 0)
8705 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008706 long save_lnum = cctx->ctx_lnum;
8707
8708 // Use the line number where the command started.
8709 cctx->ctx_lnum = start_ctx_lnum;
8710
Bram Moolenaare4984292020-12-13 14:19:25 +01008711 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8712 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8713 else if (cmdidx == CMD_execute)
8714 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8715 else if (cmdidx == CMD_echomsg)
8716 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8717 else
8718 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008719
8720 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008722 return p;
8723}
8724
8725/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008726 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008727 * instruction to compute it and return OK.
8728 * Otherwise return FAIL, the caller must deal with any range.
8729 */
8730 static int
8731compile_variable_range(exarg_T *eap, cctx_T *cctx)
8732{
8733 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8734 char_u *p = skipdigits(eap->cmd);
8735
8736 if (p == range_end)
8737 return FAIL;
8738 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8739}
8740
8741/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008742 * :put r
8743 * :put ={expr}
8744 */
8745 static char_u *
8746compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8747{
8748 char_u *line = arg;
8749 linenr_T lnum;
8750 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008751 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008752
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008753 eap->regname = *line;
8754
8755 if (eap->regname == '=')
8756 {
8757 char_u *p = line + 1;
8758
8759 if (compile_expr0(&p, cctx) == FAIL)
8760 return NULL;
8761 line = p;
8762 }
8763 else if (eap->regname != NUL)
8764 ++line;
8765
Bram Moolenaar08597872020-12-10 19:43:40 +01008766 if (compile_variable_range(eap, cctx) == OK)
8767 {
8768 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8769 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008770 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008771 {
8772 // Either no range or a number.
8773 // "errormsg" will not be set because the range is ADDR_LINES.
8774 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008775 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008776 return NULL;
8777 if (eap->addr_count == 0)
8778 lnum = -1;
8779 else
8780 lnum = eap->line2;
8781 if (above)
8782 --lnum;
8783 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008784
8785 generate_PUT(cctx, eap->regname, lnum);
8786 return line;
8787}
8788
8789/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008790 * A command that is not compiled, execute with legacy code.
8791 */
8792 static char_u *
8793compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8794{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008795 char_u *p;
8796 int has_expr = FALSE;
8797 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008798
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008799 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008800 goto theend;
8801
Bram Moolenaare729ce22021-06-06 21:38:09 +02008802 // If there was a prececing command modifier, drop it and include it in the
8803 // EXEC command.
8804 if (cctx->ctx_has_cmdmod)
8805 {
8806 garray_T *instr = &cctx->ctx_instr;
8807 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8808
8809 if (isn->isn_type == ISN_CMDMOD)
8810 {
8811 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8812 ->cmod_filter_regmatch.regprog);
8813 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8814 --instr->ga_len;
8815 cctx->ctx_has_cmdmod = FALSE;
8816 }
8817 }
8818
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008819 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008820 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008821 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008822 int usefilter = FALSE;
8823
8824 has_expr = argt & (EX_XFILE | EX_EXPAND);
8825
8826 // If the command can be followed by a bar, find the bar and truncate
8827 // it, so that the following command can be compiled.
8828 // The '|' is overwritten with a NUL, it is put back below.
8829 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8830 && *eap->arg == '!')
8831 // :w !filter or :r !filter or :r! filter
8832 usefilter = TRUE;
8833 if ((argt & EX_TRLBAR) && !usefilter)
8834 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008835 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008836 separate_nextcmd(eap);
8837 if (eap->nextcmd != NULL)
8838 nextcmd = eap->nextcmd;
8839 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008840 else if (eap->cmdidx == CMD_wincmd)
8841 {
8842 p = eap->arg;
8843 if (*p != NUL)
8844 ++p;
8845 if (*p == 'g' || *p == Ctrl_G)
8846 ++p;
8847 p = skipwhite(p);
8848 if (*p == '|')
8849 {
8850 *p = NUL;
8851 nextcmd = p + 1;
8852 }
8853 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008854 }
8855
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008856 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8857 {
8858 // expand filename in "syntax include [@group] filename"
8859 has_expr = TRUE;
8860 eap->arg = skipwhite(eap->arg + 7);
8861 if (*eap->arg == '@')
8862 eap->arg = skiptowhite(eap->arg);
8863 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008864
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008865 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8866 && STRLEN(eap->arg) > 4)
8867 {
8868 int delim = *eap->arg;
8869
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008870 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008871 if (*p == delim)
8872 {
8873 eap->arg = p + 1;
8874 has_expr = TRUE;
8875 }
8876 }
8877
Bram Moolenaarecac5912021-01-05 19:23:28 +01008878 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8879 {
8880 // TODO: should only expand when appropriate for the command
8881 eap->arg = skiptowhite(eap->arg);
8882 has_expr = TRUE;
8883 }
8884
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008885 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008886 {
8887 int count = 0;
8888 char_u *start = skipwhite(line);
8889
8890 // :cmd xxx`=expr1`yyy`=expr2`zzz
8891 // PUSHS ":cmd xxx"
8892 // eval expr1
8893 // PUSHS "yyy"
8894 // eval expr2
8895 // PUSHS "zzz"
8896 // EXECCONCAT 5
8897 for (;;)
8898 {
8899 if (p > start)
8900 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008901 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008902 ++count;
8903 }
8904 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008905 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008906 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008907 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008908 ++count;
8909 p = skipwhite(p);
8910 if (*p != '`')
8911 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008912 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008913 return NULL;
8914 }
8915 start = p + 1;
8916
8917 p = (char_u *)strstr((char *)start, "`=");
8918 if (p == NULL)
8919 {
8920 if (*skipwhite(start) != NUL)
8921 {
8922 generate_PUSHS(cctx, vim_strsave(start));
8923 ++count;
8924 }
8925 break;
8926 }
8927 }
8928 generate_EXECCONCAT(cctx, count);
8929 }
8930 else
8931 generate_EXEC(cctx, line);
8932
8933theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008934 if (*nextcmd != NUL)
8935 {
8936 // the parser expects a pointer to the bar, put it back
8937 --nextcmd;
8938 *nextcmd = '|';
8939 }
8940
8941 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008942}
8943
Bram Moolenaar20677332021-06-06 17:02:53 +02008944/*
8945 * A script command with heredoc, e.g.
8946 * ruby << EOF
8947 * command
8948 * EOF
8949 * Has been turned into one long line with NL characters by
8950 * get_function_body():
8951 * ruby << EOF<NL> command<NL>EOF
8952 */
8953 static char_u *
8954compile_script(char_u *line, cctx_T *cctx)
8955{
8956 if (cctx->ctx_skip != SKIP_YES)
8957 {
8958 isn_T *isn;
8959
8960 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8961 return NULL;
8962 isn->isn_arg.string = vim_strsave(line);
8963 }
8964 return (char_u *)"";
8965}
8966
Bram Moolenaar8238f082021-04-20 21:10:48 +02008967
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008968/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008969 * :s/pat/repl/
8970 */
8971 static char_u *
8972compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8973{
8974 char_u *cmd = eap->arg;
8975 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8976
8977 if (expr != NULL)
8978 {
8979 int delimiter = *cmd++;
8980
8981 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008982 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008983 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8984 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008985 garray_T save_ga = cctx->ctx_instr;
8986 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008987 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008988 int trailing_error;
8989 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008990 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008991 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008992
8993 cmd += 3;
8994 end = skip_substitute(cmd, delimiter);
8995
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008996 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008997 // labels are correct.
8998 cctx->ctx_instr.ga_len = 0;
8999 cctx->ctx_instr.ga_maxlen = 0;
9000 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009001 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009002 if (end[-1] == NUL)
9003 end[-1] = delimiter;
9004 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009005 trailing_error = *cmd != delimiter && *cmd != NUL;
9006
Bram Moolenaar169502f2021-04-21 12:19:35 +02009007 if (expr_res == FAIL || trailing_error
9008 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009009 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009010 if (trailing_error)
9011 semsg(_(e_trailing_arg), cmd);
9012 clear_instr_ga(&cctx->ctx_instr);
9013 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009014 return NULL;
9015 }
9016
Bram Moolenaar8238f082021-04-20 21:10:48 +02009017 // Move the generated instructions into the ISN_SUBSTITUTE
9018 // instructions, then restore the list of instructions before
9019 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009020 instr_count = cctx->ctx_instr.ga_len;
9021 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009022 instr[instr_count].isn_type = ISN_FINISH;
9023
9024 cctx->ctx_instr = save_ga;
9025 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9026 {
9027 int idx;
9028
9029 for (idx = 0; idx < instr_count; ++idx)
9030 delete_instr(instr + idx);
9031 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009032 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009033 }
9034 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9035 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009036
9037 // skip over flags
9038 if (*end == '&')
9039 ++end;
9040 while (ASCII_ISALPHA(*end) || *end == '#')
9041 ++end;
9042 return end;
9043 }
9044 }
9045
9046 return compile_exec(arg, eap, cctx);
9047}
9048
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009049 static char_u *
9050compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9051{
9052 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009053 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009054
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009055 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009056 {
9057 if (STRNCMP(arg, "END", 3) == 0)
9058 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009059 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009060 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009061 // First load the current variable value.
9062 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9063 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009064 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009065 }
9066
9067 // Gets the redirected text and put it on the stack, then store it
9068 // in the variable.
9069 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9070
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009071 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009072 generate_instr_drop(cctx, ISN_CONCAT, 1);
9073
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009074 if (lhs->lhs_has_index)
9075 {
9076 // Use the info in "lhs" to store the value at the index in the
9077 // list or dict.
9078 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9079 &t_string, cctx) == FAIL)
9080 return NULL;
9081 }
9082 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009083 return NULL;
9084
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009085 VIM_CLEAR(lhs->lhs_name);
9086 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009087 return arg + 3;
9088 }
9089 emsg(_(e_cannot_nest_redir));
9090 return NULL;
9091 }
9092
9093 if (arg[0] == '=' && arg[1] == '>')
9094 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009095 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009096
9097 // redirect to a variable is compiled
9098 arg += 2;
9099 if (*arg == '>')
9100 {
9101 ++arg;
9102 append = TRUE;
9103 }
9104 arg = skipwhite(arg);
9105
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009106 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009107 FALSE, FALSE, 1, cctx) == FAIL)
9108 return NULL;
9109 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009110 lhs->lhs_append = append;
9111 if (lhs->lhs_has_index)
9112 {
9113 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9114 if (lhs->lhs_whole == NULL)
9115 return NULL;
9116 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009117
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009118 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009119 }
9120
9121 // other redirects are handled like at script level
9122 return compile_exec(line, eap, cctx);
9123}
9124
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009125#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009126 static char_u *
9127compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9128{
9129 isn_T *isn;
9130 char_u *p;
9131
9132 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9133 if (isn == NULL)
9134 return NULL;
9135 isn->isn_arg.number = eap->cmdidx;
9136
9137 p = eap->arg;
9138 if (compile_expr0(&p, cctx) == FAIL)
9139 return NULL;
9140
9141 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9142 if (isn == NULL)
9143 return NULL;
9144 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9145 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9146 return NULL;
9147 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9148 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9149 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9150
9151 return p;
9152}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009153#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009154
Bram Moolenaar4c137212021-04-19 16:48:48 +02009155/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009156 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009157 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009158 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009159 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009160add_def_function(ufunc_T *ufunc)
9161{
9162 dfunc_T *dfunc;
9163
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009164 if (def_functions.ga_len == 0)
9165 {
9166 // The first position is not used, so that a zero uf_dfunc_idx means it
9167 // wasn't set.
9168 if (ga_grow(&def_functions, 1) == FAIL)
9169 return FAIL;
9170 ++def_functions.ga_len;
9171 }
9172
Bram Moolenaar09689a02020-05-09 22:50:08 +02009173 // Add the function to "def_functions".
9174 if (ga_grow(&def_functions, 1) == FAIL)
9175 return FAIL;
9176 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9177 CLEAR_POINTER(dfunc);
9178 dfunc->df_idx = def_functions.ga_len;
9179 ufunc->uf_dfunc_idx = dfunc->df_idx;
9180 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009181 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009182 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009183 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009184 ++def_functions.ga_len;
9185 return OK;
9186}
9187
9188/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009189 * After ex_function() has collected all the function lines: parse and compile
9190 * the lines into instructions.
9191 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009192 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9193 * the return statement (used for lambda). When uf_ret_type is already set
9194 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009195 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009196 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009197 * This can be used recursively through compile_lambda(), which may reallocate
9198 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009199 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009200 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009201 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009202compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009203 ufunc_T *ufunc,
9204 int check_return_type,
9205 compiletype_T compile_type,
9206 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009208 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009209 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009210 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009211 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212 cctx_T cctx;
9213 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009214 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009215 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009216 int ret = FAIL;
9217 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009218 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009219 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009220 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009221 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009222#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009223 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009224#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009225 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009226
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009227 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009228 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009229 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009230 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009231 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9232 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009233 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009234
9235 switch (compile_type)
9236 {
9237 case CT_PROFILE:
9238#ifdef FEAT_PROFILE
9239 instr_dest = dfunc->df_instr_prof; break;
9240#endif
9241 case CT_NONE: instr_dest = dfunc->df_instr; break;
9242 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9243 }
9244 if (instr_dest != NULL)
9245 // Was compiled in this mode before: Free old instructions.
9246 delete_def_function_contents(dfunc, FALSE);
9247 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009249 else
9250 {
9251 if (add_def_function(ufunc) == FAIL)
9252 return FAIL;
9253 new_def_function = TRUE;
9254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255
Bram Moolenaar985116a2020-07-12 17:31:09 +02009256 ufunc->uf_def_status = UF_COMPILING;
9257
Bram Moolenaara80faa82020-04-12 19:37:17 +02009258 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009259
Bram Moolenaare99d4222021-06-13 14:01:26 +02009260 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009261 cctx.ctx_ufunc = ufunc;
9262 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009263 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009264 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9265 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9266 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9267 cctx.ctx_type_list = &ufunc->uf_type_list;
9268 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9269 instr = &cctx.ctx_instr;
9270
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009271 // Set the context to the function, it may be compiled when called from
9272 // another script. Set the script version to the most modern one.
9273 // The line number will be set in next_line_from_context().
9274 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9276
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009277 // Don't use the flag from ":legacy" here.
9278 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9279
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009280 // Make sure error messages are OK.
9281 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9282 if (do_estack_push)
9283 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009284 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009285
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009286 if (ufunc->uf_def_args.ga_len > 0)
9287 {
9288 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009289 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009290 int i;
9291 char_u *arg;
9292 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009293 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009294
9295 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009296 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009297 for (i = 0; i < count; ++i)
9298 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009299 garray_T *stack = &cctx.ctx_type_stack;
9300 type_T *val_type;
9301 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009302 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009303 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009304 int jump_instr_idx = instr->ga_len;
9305 isn_T *isn;
9306
9307 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9308 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9309 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009310
9311 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009312 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009314 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009315 r = compile_expr0(&arg, &cctx);
9316
Bram Moolenaar12bce952021-03-11 20:04:04 +01009317 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009318 goto erret;
9319
9320 // If no type specified use the type of the default value.
9321 // Otherwise check that the default value type matches the
9322 // specified type.
9323 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009324 where.wt_index = arg_idx + 1;
9325 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009326 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009327 {
9328 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009329 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009330 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009331 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009332 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009333 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009334
9335 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009336 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009337
9338 // set instruction index in JUMP_IF_ARG_SET to here
9339 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9340 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009341 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009342
9343 if (did_set_arg_type)
9344 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009345 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009346 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009347
9348 /*
9349 * Loop over all the lines of the function and generate instructions.
9350 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009351 for (;;)
9352 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009353 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009354 int starts_with_colon = FALSE;
9355 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009356 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009357
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009358 // Bail out on the first error to avoid a flood of errors and report
9359 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009360 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009361 goto erret;
9362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009363 if (line != NULL && *line == '|')
9364 // the line continues after a '|'
9365 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009366 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009367 && !(*line == '#' && (line == cctx.ctx_line_start
9368 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009369 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009370 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371 goto erret;
9372 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009373 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9374 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009375 else
9376 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009377 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009378 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009379 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009380 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009381#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009382 if (cctx.ctx_skip != SKIP_YES)
9383 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009384#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009385 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009386 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009387 // Make a copy, splitting off nextcmd and removing trailing spaces
9388 // may change it.
9389 if (line != NULL)
9390 {
9391 line = vim_strsave(line);
9392 vim_free(line_to_free);
9393 line_to_free = line;
9394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009395 }
9396
Bram Moolenaara80faa82020-04-12 19:37:17 +02009397 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 ea.cmdlinep = &line;
9399 ea.cmd = skipwhite(line);
9400
Bram Moolenaarb2049902021-01-24 12:53:53 +01009401 if (*ea.cmd == '#')
9402 {
9403 // "#" starts a comment
9404 line = (char_u *)"";
9405 continue;
9406 }
9407
Bram Moolenaarf002a412021-01-24 13:34:18 +01009408#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009409 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9410 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009411 {
9412 may_generate_prof_end(&cctx, prof_lnum);
9413
9414 prof_lnum = cctx.ctx_lnum;
9415 generate_instr(&cctx, ISN_PROF_START);
9416 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009417#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009418 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9419 && cctx.ctx_skip != SKIP_YES)
9420 {
9421 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009422 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009423 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009424 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009425
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009426 // Some things can be recognized by the first character.
9427 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009428 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009429 case '}':
9430 {
9431 // "}" ends a block scope
9432 scopetype_T stype = cctx.ctx_scope == NULL
9433 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009434
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009435 if (stype == BLOCK_SCOPE)
9436 {
9437 compile_endblock(&cctx);
9438 line = ea.cmd;
9439 }
9440 else
9441 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009442 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009443 goto erret;
9444 }
9445 if (line != NULL)
9446 line = skipwhite(ea.cmd + 1);
9447 continue;
9448 }
9449
9450 case '{':
9451 // "{" starts a block scope
9452 // "{'a': 1}->func() is something else
9453 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9454 {
9455 line = compile_block(ea.cmd, &cctx);
9456 continue;
9457 }
9458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 }
9460
9461 /*
9462 * COMMAND MODIFIERS
9463 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009464 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009465 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9466 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009467 {
9468 if (errormsg != NULL)
9469 goto erret;
9470 // empty line or comment
9471 line = (char_u *)"";
9472 continue;
9473 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009474 generate_cmdmods(&cctx, &local_cmdmod);
9475 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009476
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009477 // Check if there was a colon after the last command modifier or before
9478 // the current position.
9479 for (p = ea.cmd; p >= line; --p)
9480 {
9481 if (*p == ':')
9482 starts_with_colon = TRUE;
9483 if (p < ea.cmd && !VIM_ISWHITE(*p))
9484 break;
9485 }
9486
Bram Moolenaarce024c32021-06-26 13:00:49 +02009487 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009488 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009489 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009490 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009491 if (checkforcmd(&ea.cmd, "call", 3))
9492 {
9493 if (*ea.cmd == '(')
9494 // not for "call()"
9495 ea.cmd = p;
9496 else
9497 ea.cmd = skipwhite(ea.cmd);
9498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009499
Bram Moolenaarce024c32021-06-26 13:00:49 +02009500 if (!starts_with_colon)
9501 {
9502 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009503
Bram Moolenaarce024c32021-06-26 13:00:49 +02009504 // Check for assignment after command modifiers.
9505 assign = may_compile_assignment(&ea, &line, &cctx);
9506 if (assign == OK)
9507 goto nextline;
9508 if (assign == FAIL)
9509 goto erret;
9510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009511 }
9512
9513 /*
9514 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009515 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009516 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009517 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009518 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009519 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9520 && (starts_with_colon || !(*cmd == '\''
9521 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009522 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009523 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009524 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009525 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009526 if (!starts_with_colon)
9527 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009528 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009529 goto erret;
9530 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009531 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009532 if (ends_excmd2(line, ea.cmd))
9533 {
9534 // A range without a command: jump to the line.
9535 // TODO: compile to a more efficient command, possibly
9536 // calling parse_cmd_address().
9537 ea.cmdidx = CMD_SIZE;
9538 line = compile_exec(line, &ea, &cctx);
9539 goto nextline;
9540 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009541 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009542 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009543 p = find_ex_command(&ea, NULL,
9544 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009545 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009546
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009547 if (p == NULL)
9548 {
9549 if (cctx.ctx_skip != SKIP_YES)
9550 emsg(_(e_ambiguous_use_of_user_defined_command));
9551 goto erret;
9552 }
9553
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009554 // When using ":legacy cmd" always use compile_exec().
9555 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009556 {
9557 char_u *start = ea.cmd;
9558
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009559 switch (ea.cmdidx)
9560 {
9561 case CMD_if:
9562 case CMD_elseif:
9563 case CMD_else:
9564 case CMD_endif:
9565 case CMD_for:
9566 case CMD_endfor:
9567 case CMD_continue:
9568 case CMD_break:
9569 case CMD_while:
9570 case CMD_endwhile:
9571 case CMD_try:
9572 case CMD_catch:
9573 case CMD_finally:
9574 case CMD_endtry:
9575 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9576 goto erret;
9577 default: break;
9578 }
9579
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009580 // ":legacy return expr" needs to be handled differently.
9581 if (checkforcmd(&start, "return", 4))
9582 ea.cmdidx = CMD_return;
9583 else
9584 ea.cmdidx = CMD_legacy;
9585 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9588 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009589 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009590 {
9591 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009592 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009593 }
9594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009596 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009598 // CMD_var cannot happen, compile_assignment() above would be
9599 // used. Most likely an assignment to a non-existing variable.
9600 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009601 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009603 }
9604
Bram Moolenaar3988f642020-08-27 22:43:03 +02009605 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009606 && ea.cmdidx != CMD_elseif
9607 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009608 && ea.cmdidx != CMD_endif
9609 && ea.cmdidx != CMD_endfor
9610 && ea.cmdidx != CMD_endwhile
9611 && ea.cmdidx != CMD_catch
9612 && ea.cmdidx != CMD_finally
9613 && ea.cmdidx != CMD_endtry)
9614 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009615 emsg(_(e_unreachable_code_after_return));
9616 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009617 }
9618
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009619 p = skipwhite(p);
9620 if (ea.cmdidx != CMD_SIZE
9621 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9622 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009623 if (ea.cmdidx >= 0)
9624 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009625 if ((ea.argt & EX_BANG) && *p == '!')
9626 {
9627 ea.forceit = TRUE;
9628 p = skipwhite(p + 1);
9629 }
9630 }
9631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 switch (ea.cmdidx)
9633 {
9634 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009635 ea.arg = p;
9636 line = compile_nested_function(&ea, &cctx);
9637 break;
9638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009639 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009640 // TODO: should we allow this, e.g. to declare a global
9641 // function?
9642 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 goto erret;
9644
9645 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009646 line = compile_return(p, check_return_type,
9647 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009648 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009649 break;
9650
9651 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009652 emsg(_(e_cannot_use_let_in_vim9_script));
9653 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009654 case CMD_var:
9655 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009656 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009657 case CMD_increment:
9658 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009660 if (line == p)
9661 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662 break;
9663
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009664 case CMD_unlet:
9665 case CMD_unlockvar:
9666 case CMD_lockvar:
9667 line = compile_unletlock(p, &ea, &cctx);
9668 break;
9669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009670 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009671 emsg(_(e_import_can_only_be_used_in_script));
9672 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673 break;
9674
9675 case CMD_if:
9676 line = compile_if(p, &cctx);
9677 break;
9678 case CMD_elseif:
9679 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009680 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009681 break;
9682 case CMD_else:
9683 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009684 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009685 break;
9686 case CMD_endif:
9687 line = compile_endif(p, &cctx);
9688 break;
9689
9690 case CMD_while:
9691 line = compile_while(p, &cctx);
9692 break;
9693 case CMD_endwhile:
9694 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009695 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 break;
9697
9698 case CMD_for:
9699 line = compile_for(p, &cctx);
9700 break;
9701 case CMD_endfor:
9702 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009703 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 break;
9705 case CMD_continue:
9706 line = compile_continue(p, &cctx);
9707 break;
9708 case CMD_break:
9709 line = compile_break(p, &cctx);
9710 break;
9711
9712 case CMD_try:
9713 line = compile_try(p, &cctx);
9714 break;
9715 case CMD_catch:
9716 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009717 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 break;
9719 case CMD_finally:
9720 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009721 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009722 break;
9723 case CMD_endtry:
9724 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009725 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009726 break;
9727 case CMD_throw:
9728 line = compile_throw(p, &cctx);
9729 break;
9730
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009731 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009732 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009733 break;
9734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009735 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009736 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009737 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009738 case CMD_echomsg:
9739 case CMD_echoerr:
9740 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009741 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009742
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009743 case CMD_put:
9744 ea.cmd = cmd;
9745 line = compile_put(p, &ea, &cctx);
9746 break;
9747
Bram Moolenaar4c137212021-04-19 16:48:48 +02009748 case CMD_substitute:
9749 if (cctx.ctx_skip == SKIP_YES)
9750 line = (char_u *)"";
9751 else
9752 {
9753 ea.arg = p;
9754 line = compile_substitute(line, &ea, &cctx);
9755 }
9756 break;
9757
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009758 case CMD_redir:
9759 ea.arg = p;
9760 line = compile_redir(line, &ea, &cctx);
9761 break;
9762
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009763 case CMD_cexpr:
9764 case CMD_lexpr:
9765 case CMD_caddexpr:
9766 case CMD_laddexpr:
9767 case CMD_cgetexpr:
9768 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009769#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009770 ea.arg = p;
9771 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009772#else
9773 ex_ni(&ea);
9774 line = NULL;
9775#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009776 break;
9777
Bram Moolenaar3988f642020-08-27 22:43:03 +02009778 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009779
Bram Moolenaarae616492020-07-28 20:07:27 +02009780 case CMD_append:
9781 case CMD_change:
9782 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009783 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009784 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009785 case CMD_xit:
9786 not_in_vim9(&ea);
9787 goto erret;
9788
Bram Moolenaar002262f2020-07-08 17:47:57 +02009789 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009790 if (cctx.ctx_skip != SKIP_YES)
9791 {
9792 semsg(_(e_invalid_command_str), ea.cmd);
9793 goto erret;
9794 }
9795 // We don't check for a next command here.
9796 line = (char_u *)"";
9797 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009798
Bram Moolenaar20677332021-06-06 17:02:53 +02009799 case CMD_lua:
9800 case CMD_mzscheme:
9801 case CMD_perl:
9802 case CMD_py3:
9803 case CMD_python3:
9804 case CMD_python:
9805 case CMD_pythonx:
9806 case CMD_ruby:
9807 case CMD_tcl:
9808 ea.arg = p;
9809 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009810 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009811 else
9812 // heredoc lines have been concatenated with NL
9813 // characters in get_function_body()
9814 line = compile_script(line, &cctx);
9815 break;
9816
9817 default:
9818 // Not recognized, execute with do_cmdline_cmd().
9819 ea.arg = p;
9820 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 break;
9822 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009823nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009824 if (line == NULL)
9825 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009826 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009827
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009828 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009829 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009831 if (cctx.ctx_type_stack.ga_len < 0)
9832 {
9833 iemsg("Type stack underflow");
9834 goto erret;
9835 }
9836 }
9837
9838 if (cctx.ctx_scope != NULL)
9839 {
9840 if (cctx.ctx_scope->se_type == IF_SCOPE)
9841 emsg(_(e_endif));
9842 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9843 emsg(_(e_endwhile));
9844 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9845 emsg(_(e_endfor));
9846 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009847 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009848 goto erret;
9849 }
9850
Bram Moolenaarefd88552020-06-18 20:50:10 +02009851 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009852 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009853 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9854 ufunc->uf_ret_type = &t_void;
9855 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009857 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009858 goto erret;
9859 }
9860
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009861 // Return void if there is no return at the end.
9862 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009863 }
9864
Bram Moolenaar599410c2021-04-10 14:03:43 +02009865 // When compiled with ":silent!" and there was an error don't consider the
9866 // function compiled.
9867 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009868 {
9869 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9870 + ufunc->uf_dfunc_idx;
9871 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009872 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009873#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009874 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009875 {
9876 dfunc->df_instr_prof = instr->ga_data;
9877 dfunc->df_instr_prof_count = instr->ga_len;
9878 }
9879 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009880#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009881 if (cctx.ctx_compile_type == CT_DEBUG)
9882 {
9883 dfunc->df_instr_debug = instr->ga_data;
9884 dfunc->df_instr_debug_count = instr->ga_len;
9885 }
9886 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009887 {
9888 dfunc->df_instr = instr->ga_data;
9889 dfunc->df_instr_count = instr->ga_len;
9890 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009891 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009892 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009893 if (cctx.ctx_outer_used)
9894 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009895 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897
9898 ret = OK;
9899
9900erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009901 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009902 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009903 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9904 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009905
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009906 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009907 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009908 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009909 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009910
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009911 // If using the last entry in the table and it was added above, we
9912 // might as well remove it.
9913 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009914 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009915 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009916 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009917 ufunc->uf_dfunc_idx = 0;
9918 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009919 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009920
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009921 while (cctx.ctx_scope != NULL)
9922 drop_scope(&cctx);
9923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924 if (errormsg != NULL)
9925 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009926 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009927 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009928 }
9929
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009930 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9931 {
9932 if (ret == OK)
9933 {
9934 emsg(_(e_missing_redir_end));
9935 ret = FAIL;
9936 }
9937 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009938 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009939 }
9940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009942 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009943 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009944 if (do_estack_push)
9945 estack_pop();
9946
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009947 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009948 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009949 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009951 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009952}
9953
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009954 void
9955set_function_type(ufunc_T *ufunc)
9956{
9957 int varargs = ufunc->uf_va_name != NULL;
9958 int argcount = ufunc->uf_args.ga_len;
9959
9960 // Create a type for the function, with the return type and any
9961 // argument types.
9962 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9963 // The type is included in "tt_args".
9964 if (argcount > 0 || varargs)
9965 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009966 if (ufunc->uf_type_list.ga_itemsize == 0)
9967 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009968 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9969 argcount, &ufunc->uf_type_list);
9970 // Add argument types to the function type.
9971 if (func_type_add_arg_types(ufunc->uf_func_type,
9972 argcount + varargs,
9973 &ufunc->uf_type_list) == FAIL)
9974 return;
9975 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9976 ufunc->uf_func_type->tt_min_argcount =
9977 argcount - ufunc->uf_def_args.ga_len;
9978 if (ufunc->uf_arg_types == NULL)
9979 {
9980 int i;
9981
9982 // lambda does not have argument types.
9983 for (i = 0; i < argcount; ++i)
9984 ufunc->uf_func_type->tt_args[i] = &t_any;
9985 }
9986 else
9987 mch_memmove(ufunc->uf_func_type->tt_args,
9988 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9989 if (varargs)
9990 {
9991 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009992 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009993 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9994 }
9995 }
9996 else
9997 // No arguments, can use a predefined type.
9998 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9999 argcount, &ufunc->uf_type_list);
10000}
10001
10002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010003/*
10004 * Delete an instruction, free what it contains.
10005 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010006 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010007delete_instr(isn_T *isn)
10008{
10009 switch (isn->isn_type)
10010 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010011 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010012 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010013 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010014 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010015 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010016 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010017 case ISN_LOADENV:
10018 case ISN_LOADG:
10019 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010020 case ISN_LOADT:
10021 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010022 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010023 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010024 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010025 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010026 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010027 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010028 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010030 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010031 case ISN_STOREW:
10032 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033 vim_free(isn->isn_arg.string);
10034 break;
10035
Bram Moolenaar4c137212021-04-19 16:48:48 +020010036 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010037 {
10038 int idx;
10039 isn_T *list = isn->isn_arg.subs.subs_instr;
10040
10041 vim_free(isn->isn_arg.subs.subs_cmd);
10042 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10043 delete_instr(list + idx);
10044 vim_free(list);
10045 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010046 break;
10047
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010048 case ISN_INSTR:
10049 {
10050 int idx;
10051 isn_T *list = isn->isn_arg.instr;
10052
10053 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10054 delete_instr(list + idx);
10055 vim_free(list);
10056 }
10057 break;
10058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010060 case ISN_STORES:
10061 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 break;
10063
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010064 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010065 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010066 vim_free(isn->isn_arg.unlet.ul_name);
10067 break;
10068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069 case ISN_STOREOPT:
10070 vim_free(isn->isn_arg.storeopt.so_name);
10071 break;
10072
10073 case ISN_PUSHBLOB: // push blob isn_arg.blob
10074 blob_unref(isn->isn_arg.blob);
10075 break;
10076
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010077 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010078#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010079 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010080#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010081 break;
10082
10083 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010084#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010085 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010086#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010087 break;
10088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 case ISN_UCALL:
10090 vim_free(isn->isn_arg.ufunc.cuf_name);
10091 break;
10092
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010093 case ISN_FUNCREF:
10094 {
10095 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10096 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010097 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010098
Bram Moolenaar077a4232020-12-22 18:33:27 +010010099 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10100 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010101 }
10102 break;
10103
10104 case ISN_DCALL:
10105 {
10106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10107 + isn->isn_arg.dfunc.cdf_idx;
10108
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010109 if (dfunc->df_ufunc != NULL
10110 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010111 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010112 }
10113 break;
10114
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010115 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010116 {
10117 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10118 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10119
10120 if (ufunc != NULL)
10121 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010122 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010123 func_ptr_unref(ufunc);
10124 }
10125
10126 vim_free(lambda);
10127 vim_free(isn->isn_arg.newfunc.nf_global);
10128 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010129 break;
10130
Bram Moolenaar5e654232020-09-16 15:22:00 +020010131 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010132 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010133 free_type(isn->isn_arg.type.ct_type);
10134 break;
10135
Bram Moolenaar02194d22020-10-24 23:08:38 +020010136 case ISN_CMDMOD:
10137 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10138 ->cmod_filter_regmatch.regprog);
10139 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10140 break;
10141
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010142 case ISN_LOADSCRIPT:
10143 case ISN_STORESCRIPT:
10144 vim_free(isn->isn_arg.script.scriptref);
10145 break;
10146
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010147 case ISN_TRY:
10148 vim_free(isn->isn_arg.try.try_ref);
10149 break;
10150
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010151 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010152 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010153 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10154 break;
10155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010156 case ISN_2BOOL:
10157 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010158 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010159 case ISN_ADDBLOB:
10160 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010161 case ISN_ANYINDEX:
10162 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010163 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010164 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010165 case ISN_BLOBINDEX:
10166 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010167 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010168 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010169 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010170 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010171 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010172 case ISN_COMPAREANY:
10173 case ISN_COMPAREBLOB:
10174 case ISN_COMPAREBOOL:
10175 case ISN_COMPAREDICT:
10176 case ISN_COMPAREFLOAT:
10177 case ISN_COMPAREFUNC:
10178 case ISN_COMPARELIST:
10179 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010180 case ISN_COMPARESPECIAL:
10181 case ISN_COMPARESTRING:
10182 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010183 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010184 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010185 case ISN_DROP:
10186 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010187 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010188 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010189 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010190 case ISN_EXECCONCAT:
10191 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010192 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010193 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010194 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010195 case ISN_GETITEM:
10196 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010197 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010198 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010199 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010200 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010201 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010202 case ISN_LOADBDICT:
10203 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010204 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010205 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010206 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010207 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010208 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010209 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010210 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010211 case ISN_NEGATENR:
10212 case ISN_NEWDICT:
10213 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010214 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010215 case ISN_OPFLOAT:
10216 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010217 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010218 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010219 case ISN_PROF_END:
10220 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010221 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010222 case ISN_PUSHF:
10223 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010224 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010225 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010226 case ISN_REDIREND:
10227 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010228 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010229 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010230 case ISN_SHUFFLE:
10231 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010233 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010234 case ISN_STORENR:
10235 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010236 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010237 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010238 case ISN_STOREV:
10239 case ISN_STRINDEX:
10240 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010242 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010243 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010244 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010245 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010246 // nothing allocated
10247 break;
10248 }
10249}
10250
10251/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010252 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010253 */
10254 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010255delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010256{
10257 int idx;
10258
10259 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010260 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010261
10262 if (dfunc->df_instr != NULL)
10263 {
10264 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10265 delete_instr(dfunc->df_instr + idx);
10266 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010267 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010268 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010269 if (dfunc->df_instr_debug != NULL)
10270 {
10271 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10272 delete_instr(dfunc->df_instr_debug + idx);
10273 VIM_CLEAR(dfunc->df_instr_debug);
10274 dfunc->df_instr_debug = NULL;
10275 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010276#ifdef FEAT_PROFILE
10277 if (dfunc->df_instr_prof != NULL)
10278 {
10279 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10280 delete_instr(dfunc->df_instr_prof + idx);
10281 VIM_CLEAR(dfunc->df_instr_prof);
10282 dfunc->df_instr_prof = NULL;
10283 }
10284#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010285
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010286 if (mark_deleted)
10287 dfunc->df_deleted = TRUE;
10288 if (dfunc->df_ufunc != NULL)
10289 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010290}
10291
10292/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010293 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010294 * function, unless another user function still uses it.
10295 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010296 */
10297 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010298unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010299{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010300 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010301 {
10302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10303 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010304
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010305 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010306 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010307 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010308 ufunc->uf_dfunc_idx = 0;
10309 if (dfunc->df_ufunc == ufunc)
10310 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010311 }
10312}
10313
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010314/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010315 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010316 */
10317 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010318link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010319{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010320 if (ufunc->uf_dfunc_idx > 0)
10321 {
10322 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10323 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010324
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010325 ++dfunc->df_refcount;
10326 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010327}
10328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010329#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010330/*
10331 * Free all functions defined with ":def".
10332 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010333 void
10334free_def_functions(void)
10335{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010336 int idx;
10337
10338 for (idx = 0; idx < def_functions.ga_len; ++idx)
10339 {
10340 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10341
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010342 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010343 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010344 }
10345
10346 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010347}
10348#endif
10349
10350
10351#endif // FEAT_EVAL