blob: 956ce44eb01621916f8516d7b1e4d973a8d9dfaf [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 Moolenaare0de1712020-12-02 17:36:54 +01003674 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003676 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 */
3678 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003679compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680{
3681 garray_T *instr = &cctx->ctx_instr;
3682 int count = 0;
3683 dict_T *d = dict_alloc();
3684 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003685 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003686 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003687 int is_const;
3688 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689
3690 if (d == NULL)
3691 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003692 if (generate_ppconst(cctx, ppconst) == FAIL)
3693 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003694 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003696 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697
Bram Moolenaar23c55272020-06-21 16:58:13 +02003698 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003699 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003700 *arg = NULL;
3701 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003702 }
3703
3704 if (**arg == '}')
3705 break;
3706
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003707 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003709 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003711 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003712 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003713 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003716 if (isn->isn_type == ISN_PUSHNR)
3717 {
3718 char buf[NUMBUFLEN];
3719
3720 // Convert to string at compile time.
3721 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3722 isn->isn_type = ISN_PUSHS;
3723 isn->isn_arg.string = vim_strsave((char_u *)buf);
3724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 if (isn->isn_type == ISN_PUSHS)
3726 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003727 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003728 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003729 *arg = skipwhite(*arg);
3730 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003731 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003732 emsg(_(e_missing_matching_bracket_after_dict_key));
3733 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003734 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003735 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003737 else
3738 {
3739 // {"name": value},
3740 // {'name': value},
3741 // {name: value} use "name" as a literal key
3742 key = get_literal_key(arg);
3743 if (key == NULL)
3744 return FAIL;
3745 if (generate_PUSHS(cctx, key) == FAIL)
3746 return FAIL;
3747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748
3749 // Check for duplicate keys, if using string keys.
3750 if (key != NULL)
3751 {
3752 item = dict_find(d, key, -1);
3753 if (item != NULL)
3754 {
3755 semsg(_(e_duplicate_key), key);
3756 goto failret;
3757 }
3758 item = dictitem_alloc(key);
3759 if (item != NULL)
3760 {
3761 item->di_tv.v_type = VAR_UNKNOWN;
3762 item->di_tv.v_lock = 0;
3763 if (dict_add(d, item) == FAIL)
3764 dictitem_free(item);
3765 }
3766 }
3767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 if (**arg != ':')
3769 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003770 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003771 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003772 else
3773 semsg(_(e_missing_dict_colon), *arg);
3774 return FAIL;
3775 }
3776 whitep = *arg + 1;
3777 if (!IS_WHITE_OR_NUL(*whitep))
3778 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003779 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 return FAIL;
3781 }
3782
Bram Moolenaar23c55272020-06-21 16:58:13 +02003783 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003784 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003785 *arg = NULL;
3786 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003787 }
3788
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003789 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003791 if (!is_const)
3792 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 ++count;
3794
Bram Moolenaar2c330432020-04-13 14:41:35 +02003795 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003796 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003797 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003798 *arg = NULL;
3799 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 if (**arg == '}')
3802 break;
3803 if (**arg != ',')
3804 {
3805 semsg(_(e_missing_dict_comma), *arg);
3806 goto failret;
3807 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003808 if (IS_WHITE_OR_NUL(*whitep))
3809 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003810 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003811 return FAIL;
3812 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003813 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003814 if (!IS_WHITE_OR_NUL(*whitep))
3815 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003816 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003817 return FAIL;
3818 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003819 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 }
3821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 *arg = *arg + 1;
3823
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003824 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003825 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003826 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003827 *arg += STRLEN(*arg);
3828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003830 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 return generate_NEWDICT(cctx, count);
3832
3833failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003834 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003835 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003836 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003837 *arg = (char_u *)"";
3838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 dict_unref(d);
3840 return FAIL;
3841}
3842
3843/*
3844 * Compile "&option".
3845 */
3846 static int
3847compile_get_option(char_u **arg, cctx_T *cctx)
3848{
3849 typval_T rettv;
3850 char_u *start = *arg;
3851 int ret;
3852
3853 // parse the option and get the current value to get the type.
3854 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003855 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 if (ret == OK)
3857 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003858 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003859 char_u *name = vim_strnsave(start, *arg - start);
3860 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3861 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862
3863 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3864 vim_free(name);
3865 }
3866 clear_tv(&rettv);
3867
3868 return ret;
3869}
3870
3871/*
3872 * Compile "$VAR".
3873 */
3874 static int
3875compile_get_env(char_u **arg, cctx_T *cctx)
3876{
3877 char_u *start = *arg;
3878 int len;
3879 int ret;
3880 char_u *name;
3881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 ++*arg;
3883 len = get_env_len(arg);
3884 if (len == 0)
3885 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003886 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 return FAIL;
3888 }
3889
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003890 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 name = vim_strnsave(start, len + 1);
3892 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3893 vim_free(name);
3894 return ret;
3895}
3896
3897/*
3898 * Compile "@r".
3899 */
3900 static int
3901compile_get_register(char_u **arg, cctx_T *cctx)
3902{
3903 int ret;
3904
3905 ++*arg;
3906 if (**arg == NUL)
3907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003908 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 return FAIL;
3910 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003911 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
3913 emsg_invreg(**arg);
3914 return FAIL;
3915 }
3916 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3917 ++*arg;
3918 return ret;
3919}
3920
3921/*
3922 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003923 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 */
3925 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003926apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003928 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929
3930 // this works from end to start
3931 while (p > start)
3932 {
3933 --p;
3934 if (*p == '-' || *p == '+')
3935 {
3936 // only '-' has an effect, for '+' we only check the type
3937#ifdef FEAT_FLOAT
3938 if (rettv->v_type == VAR_FLOAT)
3939 {
3940 if (*p == '-')
3941 rettv->vval.v_float = -rettv->vval.v_float;
3942 }
3943 else
3944#endif
3945 {
3946 varnumber_T val;
3947 int error = FALSE;
3948
3949 // tv_get_number_chk() accepts a string, but we don't want that
3950 // here
3951 if (check_not_string(rettv) == FAIL)
3952 return FAIL;
3953 val = tv_get_number_chk(rettv, &error);
3954 clear_tv(rettv);
3955 if (error)
3956 return FAIL;
3957 if (*p == '-')
3958 val = -val;
3959 rettv->v_type = VAR_NUMBER;
3960 rettv->vval.v_number = val;
3961 }
3962 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003963 else if (numeric_only)
3964 {
3965 ++p;
3966 break;
3967 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003968 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
3970 int v = tv2bool(rettv);
3971
3972 // '!' is permissive in the type.
3973 clear_tv(rettv);
3974 rettv->v_type = VAR_BOOL;
3975 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3976 }
3977 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003978 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return OK;
3980}
3981
3982/*
3983 * Recognize v: variables that are constants and set "rettv".
3984 */
3985 static void
3986get_vim_constant(char_u **arg, typval_T *rettv)
3987{
3988 if (STRNCMP(*arg, "v:true", 6) == 0)
3989 {
3990 rettv->v_type = VAR_BOOL;
3991 rettv->vval.v_number = VVAL_TRUE;
3992 *arg += 6;
3993 }
3994 else if (STRNCMP(*arg, "v:false", 7) == 0)
3995 {
3996 rettv->v_type = VAR_BOOL;
3997 rettv->vval.v_number = VVAL_FALSE;
3998 *arg += 7;
3999 }
4000 else if (STRNCMP(*arg, "v:null", 6) == 0)
4001 {
4002 rettv->v_type = VAR_SPECIAL;
4003 rettv->vval.v_number = VVAL_NULL;
4004 *arg += 6;
4005 }
4006 else if (STRNCMP(*arg, "v:none", 6) == 0)
4007 {
4008 rettv->v_type = VAR_SPECIAL;
4009 rettv->vval.v_number = VVAL_NONE;
4010 *arg += 6;
4011 }
4012}
4013
Bram Moolenaar657137c2021-01-09 15:45:23 +01004014 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004015get_compare_type(char_u *p, int *len, int *type_is)
4016{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004017 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004018 int i;
4019
4020 switch (p[0])
4021 {
4022 case '=': if (p[1] == '=')
4023 type = EXPR_EQUAL;
4024 else if (p[1] == '~')
4025 type = EXPR_MATCH;
4026 break;
4027 case '!': if (p[1] == '=')
4028 type = EXPR_NEQUAL;
4029 else if (p[1] == '~')
4030 type = EXPR_NOMATCH;
4031 break;
4032 case '>': if (p[1] != '=')
4033 {
4034 type = EXPR_GREATER;
4035 *len = 1;
4036 }
4037 else
4038 type = EXPR_GEQUAL;
4039 break;
4040 case '<': if (p[1] != '=')
4041 {
4042 type = EXPR_SMALLER;
4043 *len = 1;
4044 }
4045 else
4046 type = EXPR_SEQUAL;
4047 break;
4048 case 'i': if (p[1] == 's')
4049 {
4050 // "is" and "isnot"; but not a prefix of a name
4051 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4052 *len = 5;
4053 i = p[*len];
4054 if (!isalnum(i) && i != '_')
4055 {
4056 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4057 *type_is = TRUE;
4058 }
4059 }
4060 break;
4061 }
4062 return type;
4063}
4064
4065/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004066 * Skip over an expression, ignoring most errors.
4067 */
4068 static void
4069skip_expr_cctx(char_u **arg, cctx_T *cctx)
4070{
4071 evalarg_T evalarg;
4072
4073 CLEAR_FIELD(evalarg);
4074 evalarg.eval_cctx = cctx;
4075 skip_expr(arg, &evalarg);
4076}
4077
4078/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004080 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 */
4082 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004083compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004085 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086
4087 // this works from end to start
4088 while (p > start)
4089 {
4090 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004091 while (VIM_ISWHITE(*p))
4092 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 if (*p == '-' || *p == '+')
4094 {
4095 int negate = *p == '-';
4096 isn_T *isn;
4097
4098 // TODO: check type
4099 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4100 {
4101 --p;
4102 if (*p == '-')
4103 negate = !negate;
4104 }
4105 // only '-' has an effect, for '+' we only check the type
4106 if (negate)
4107 isn = generate_instr(cctx, ISN_NEGATENR);
4108 else
4109 isn = generate_instr(cctx, ISN_CHECKNR);
4110 if (isn == NULL)
4111 return FAIL;
4112 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004113 else if (numeric_only)
4114 {
4115 ++p;
4116 break;
4117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 else
4119 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004120 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004122 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004124 if (p[-1] == '!')
4125 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004128 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
4130 }
4131 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004132 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 return OK;
4134}
4135
4136/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004137 * Compile "(expression)": recursive!
4138 * Return FAIL/OK.
4139 */
4140 static int
4141compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4142{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004143 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004144 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004145
Bram Moolenaar24156692021-01-14 20:35:49 +01004146 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4147 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004148 if (ppconst->pp_used <= PPSIZE - 10)
4149 {
4150 ret = compile_expr1(arg, cctx, ppconst);
4151 }
4152 else
4153 {
4154 // Not enough space in ppconst, flush constants.
4155 if (generate_ppconst(cctx, ppconst) == FAIL)
4156 return FAIL;
4157 ret = compile_expr0(arg, cctx);
4158 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004159 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4160 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004161 if (**arg == ')')
4162 ++*arg;
4163 else if (ret == OK)
4164 {
4165 emsg(_(e_missing_close));
4166 ret = FAIL;
4167 }
4168 return ret;
4169}
4170
4171/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004173 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 */
4175 static int
4176compile_subscript(
4177 char_u **arg,
4178 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004179 char_u *start_leader,
4180 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004181 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004183 char_u *name_start = *end_leader;
4184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 for (;;)
4186 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004187 char_u *p = skipwhite(*arg);
4188
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004189 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004190 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004191 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004192
4193 // If a following line starts with "->{" or "->X" advance to that
4194 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004195 // Also if a following line starts with ".x".
4196 if (next != NULL &&
4197 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004198 && (next[2] == '{'
4199 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004200 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004201 {
4202 next = next_line_from_context(cctx, TRUE);
4203 if (next == NULL)
4204 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004205 *arg = next;
4206 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004207 }
4208 }
4209
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004210 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004211 // not a function call.
4212 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004214 garray_T *stack = &cctx->ctx_type_stack;
4215 type_T *type;
4216 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004218 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004219 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004220 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004223 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4224
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004225 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004226 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004228 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 return FAIL;
4230 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004231 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004233 char_u *pstart = p;
4234
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004235 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004237 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 // something->method()
4240 // Apply the '!', '-' and '+' first:
4241 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004242 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004245 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004246 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004247 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004248 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004249 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004250 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004251 garray_T *stack = &cctx->ctx_type_stack;
4252 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004253 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004254 int expr_isn_start = cctx->ctx_instr.ga_len;
4255 int expr_isn_end;
4256 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004257
4258 // Funcref call: list->(Refs[2])(arg)
4259 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004260 //
4261 // Fist compile the function expression.
4262 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004263 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004264
4265 // Remember the next instruction index, where the instructions
4266 // for arguments are being written.
4267 expr_isn_end = cctx->ctx_instr.ga_len;
4268
4269 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004270 if (**arg != '(')
4271 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004272 if (*skipwhite(*arg) == '(')
4273 emsg(_(e_nowhitespace));
4274 else
4275 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004276 return FAIL;
4277 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004278 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004279 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004280 return FAIL;
4281
Bram Moolenaar2927c072021-04-05 19:41:21 +02004282 // Move the instructions for the arguments to before the
4283 // instructions of the expression and move the type of the
4284 // expression after the argument types. This is what ISN_PCALL
4285 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004286 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004287 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4288 if (arg_isn_count > 0)
4289 {
4290 int expr_isn_count = expr_isn_end - expr_isn_start;
4291 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4292
4293 if (isn == NULL)
4294 return FAIL;
4295 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4296 + expr_isn_start,
4297 sizeof(isn_T) * expr_isn_count);
4298 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4299 + expr_isn_start,
4300 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4301 sizeof(isn_T) * arg_isn_count);
4302 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4303 + expr_isn_start + arg_isn_count,
4304 isn, sizeof(isn_T) * expr_isn_count);
4305 vim_free(isn);
4306
4307 type = ((type_T **)stack->ga_data)[type_idx_start];
4308 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4309 ((type_T **)stack->ga_data) + type_idx_start + 1,
4310 sizeof(type_T *)
4311 * (stack->ga_len - type_idx_start - 1));
4312 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4313 }
4314
Bram Moolenaar7e368202020-12-25 21:56:57 +01004315 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4316 if (generate_PCALL(cctx, argcount,
4317 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 return FAIL;
4319 }
4320 else
4321 {
4322 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004323 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004324 if (!eval_isnamec1(*p))
4325 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004326 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004327 return FAIL;
4328 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004329 if (ASCII_ISALPHA(*p) && p[1] == ':')
4330 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004331 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 ;
4333 if (*p != '(')
4334 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004335 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 return FAIL;
4337 }
4338 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 return FAIL;
4341 }
4342 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004343 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004346
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004347 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004348 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004349 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004350 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004351 // TODO: more arguments
4352 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004353 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004355 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004357 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004358 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004359 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004360 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004361 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004362 // missing first index is equal to zero
4363 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004364 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004365 else
4366 {
4367 if (compile_expr0(arg, cctx) == FAIL)
4368 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004369 if (**arg == ':')
4370 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004371 semsg(_(e_white_space_required_before_and_after_str_at_str),
4372 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004373 return FAIL;
4374 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004375 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004376 return FAIL;
4377 *arg = skipwhite(*arg);
4378 }
4379 if (**arg == ':')
4380 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004381 is_slice = TRUE;
4382 ++*arg;
4383 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4384 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004385 semsg(_(e_white_space_required_before_and_after_str_at_str),
4386 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004387 return FAIL;
4388 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004389 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004390 return FAIL;
4391 if (**arg == ']')
4392 // missing second index is equal to end of string
4393 generate_PUSHNR(cctx, -1);
4394 else
4395 {
4396 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004397 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004398 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004399 return FAIL;
4400 *arg = skipwhite(*arg);
4401 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403
4404 if (**arg != ']')
4405 {
4406 emsg(_(e_missbrac));
4407 return FAIL;
4408 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004409 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
Bram Moolenaare42939a2021-04-05 17:11:17 +02004411 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004412 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004414 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004416 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004417 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004418 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004419 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004420
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004421 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004422 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004423 {
4424 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004425 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004426 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004427 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004428 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 while (eval_isnamec(*p))
4430 MB_PTR_ADV(p);
4431 if (p == *arg)
4432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004433 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 return FAIL;
4435 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 return FAIL;
4438 *arg = p;
4439 }
4440 else
4441 break;
4442 }
4443
4444 // TODO - see handle_subscript():
4445 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4446 // Don't do this when "Func" is already a partial that was bound
4447 // explicitly (pt_auto is FALSE).
4448
4449 return OK;
4450}
4451
4452/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004453 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4454 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004456 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004457 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004458 *
4459 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 */
4461
4462/*
4463 * number number constant
4464 * 0zFFFFFFFF Blob constant
4465 * "string" string constant
4466 * 'string' literal string constant
4467 * &option-name option value
4468 * @r register contents
4469 * identifier variable value
4470 * function() function call
4471 * $VAR environment variable
4472 * (expression) nested expression
4473 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004474 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 *
4476 * Also handle:
4477 * ! in front logical NOT
4478 * - in front unary minus
4479 * + in front unary plus (ignored)
4480 * trailing (arg) funcref/partial call
4481 * trailing [] subscript in String or List
4482 * trailing .name entry in Dictionary
4483 * trailing ->name() method call
4484 */
4485 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004486compile_expr7(
4487 char_u **arg,
4488 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004489 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 char_u *start_leader, *end_leader;
4492 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004493 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004494 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004496 ppconst->pp_is_const = FALSE;
4497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 /*
4499 * Skip '!', '-' and '+' characters. They are handled later.
4500 */
4501 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004502 if (eval_leader(arg, TRUE) == FAIL)
4503 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 end_leader = *arg;
4505
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004506 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 switch (**arg)
4508 {
4509 /*
4510 * Number constant.
4511 */
4512 case '0': // also for blob starting with 0z
4513 case '1':
4514 case '2':
4515 case '3':
4516 case '4':
4517 case '5':
4518 case '6':
4519 case '7':
4520 case '8':
4521 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004522 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004524 // Apply "-" and "+" just before the number now, right to
4525 // left. Matters especially when "->" follows. Stops at
4526 // '!'.
4527 if (apply_leader(rettv, TRUE,
4528 start_leader, &end_leader) == FAIL)
4529 {
4530 clear_tv(rettv);
4531 return FAIL;
4532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 break;
4534
4535 /*
4536 * String constant: "string".
4537 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004538 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540 break;
4541
4542 /*
4543 * Literal string constant: 'str''ing'.
4544 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004545 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 return FAIL;
4547 break;
4548
4549 /*
4550 * Constant Vim variable.
4551 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004552 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 ret = NOTDONE;
4554 break;
4555
4556 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 * "true" constant
4558 */
4559 case 't': if (STRNCMP(*arg, "true", 4) == 0
4560 && !eval_isnamec((*arg)[4]))
4561 {
4562 *arg += 4;
4563 rettv->v_type = VAR_BOOL;
4564 rettv->vval.v_number = VVAL_TRUE;
4565 }
4566 else
4567 ret = NOTDONE;
4568 break;
4569
4570 /*
4571 * "false" constant
4572 */
4573 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4574 && !eval_isnamec((*arg)[5]))
4575 {
4576 *arg += 5;
4577 rettv->v_type = VAR_BOOL;
4578 rettv->vval.v_number = VVAL_FALSE;
4579 }
4580 else
4581 ret = NOTDONE;
4582 break;
4583
4584 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004585 * "null" constant
4586 */
4587 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004588 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004589 {
4590 *arg += 4;
4591 rettv->v_type = VAR_SPECIAL;
4592 rettv->vval.v_number = VVAL_NULL;
4593 }
4594 else
4595 ret = NOTDONE;
4596 break;
4597
4598 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 * List: [expr, expr]
4600 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004601 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4602 return FAIL;
4603 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 break;
4605
4606 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 * Dictionary: {'key': val, 'key': val}
4608 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004609 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4610 return FAIL;
4611 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 break;
4613
4614 /*
4615 * Option value: &name
4616 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004617 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4618 return FAIL;
4619 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 break;
4621
4622 /*
4623 * Environment variable: $VAR.
4624 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004625 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4626 return FAIL;
4627 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 break;
4629
4630 /*
4631 * Register contents: @r.
4632 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004633 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4634 return FAIL;
4635 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 break;
4637 /*
4638 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004639 * lambda: (arg, arg) => expr
4640 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004642 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4643 ret = compile_lambda(arg, cctx);
4644 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004645 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 break;
4647
4648 default: ret = NOTDONE;
4649 break;
4650 }
4651 if (ret == FAIL)
4652 return FAIL;
4653
Bram Moolenaar1c747212020-05-09 18:28:34 +02004654 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004656 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 clear_tv(rettv);
4658 else
4659 // A constant expression can possibly be handled compile time,
4660 // return the value instead of generating code.
4661 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 }
4663 else if (ret == NOTDONE)
4664 {
4665 char_u *p;
4666 int r;
4667
4668 if (!eval_isnamec1(**arg))
4669 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004670 if (!vim9_bad_comment(*arg))
4671 {
4672 if (ends_excmd(*skipwhite(*arg)))
4673 semsg(_(e_empty_expression_str), *arg);
4674 else
4675 semsg(_(e_name_expected_str), *arg);
4676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 return FAIL;
4678 }
4679
4680 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004681 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004682 if (p - *arg == (size_t)1 && **arg == '_')
4683 {
4684 emsg(_(e_cannot_use_underscore_here));
4685 return FAIL;
4686 }
4687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004689 {
4690 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 {
4694 if (generate_ppconst(cctx, ppconst) == FAIL)
4695 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004696 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 if (r == FAIL)
4699 return FAIL;
4700 }
4701
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004702 // Handle following "[]", ".member", etc.
4703 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004704 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004705 ppconst) == FAIL)
4706 return FAIL;
4707 if (ppconst->pp_used > 0)
4708 {
4709 // apply the '!', '-' and '+' before the constant
4710 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004711 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004712 return FAIL;
4713 return OK;
4714 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004715 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004717 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718}
4719
4720/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004721 * Give the "white on both sides" error, taking the operator from "p[len]".
4722 */
4723 void
4724error_white_both(char_u *op, int len)
4725{
4726 char_u buf[10];
4727
4728 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004729 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004730}
4731
4732/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004733 * <type>expr7: runtime type check / conversion
4734 */
4735 static int
4736compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4737{
4738 type_T *want_type = NULL;
4739
4740 // Recognize <type>
4741 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4742 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004743 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004744 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4745 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004746 return FAIL;
4747
4748 if (**arg != '>')
4749 {
4750 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004751 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004752 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004753 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004754 return FAIL;
4755 }
4756 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004757 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004758 return FAIL;
4759 }
4760
4761 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4762 return FAIL;
4763
4764 if (want_type != NULL)
4765 {
4766 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004767 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004768 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004769
Bram Moolenaard1103582020-08-14 22:44:25 +02004770 generate_ppconst(cctx, ppconst);
4771 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004772 where.wt_index = 0;
4773 where.wt_variable = FALSE;
4774 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004775 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004776 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004777 return FAIL;
4778 }
4779 }
4780
4781 return OK;
4782}
4783
4784/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 * * number multiplication
4786 * / number division
4787 * % number modulo
4788 */
4789 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791{
4792 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004794 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004796 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004797 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 return FAIL;
4799
4800 /*
4801 * Repeat computing, until no "*", "/" or "%" is following.
4802 */
4803 for (;;)
4804 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004805 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 if (*op != '*' && *op != '/' && *op != '%')
4807 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004808 if (next != NULL)
4809 {
4810 *arg = next_line_from_context(cctx, TRUE);
4811 op = skipwhite(*arg);
4812 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004813
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004814 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004816 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004817 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004819 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004820 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004822 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004823 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004825
4826 if (ppconst->pp_used == ppconst_used + 2
4827 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4828 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004829 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004830 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4831 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4832 varnumber_T res = 0;
4833 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004835 // both are numbers: compute the result
4836 switch (*op)
4837 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004838 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004839 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004840 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004841 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004842 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004843 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004844 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004845 break;
4846 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004847 if (failed)
4848 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004849 tv1->vval.v_number = res;
4850 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004851 }
4852 else
4853 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004854 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004855 generate_two_op(cctx, op);
4856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
4858
4859 return OK;
4860}
4861
4862/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004863 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 * - number subtraction
4865 * .. string concatenation
4866 */
4867 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004868compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869{
4870 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004871 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874
4875 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004876 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return FAIL;
4878
4879 /*
4880 * Repeat computing, until no "+", "-" or ".." is following.
4881 */
4882 for (;;)
4883 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004884 op = may_peek_next_line(cctx, *arg, &next);
4885 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004887 if (op[0] == op[1] && *op != '.' && next)
4888 // Finding "++" or "--" on the next line is a separate command.
4889 // But ".." is concatenation.
4890 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004892 if (next != NULL)
4893 {
4894 *arg = next_line_from_context(cctx, TRUE);
4895 op = skipwhite(*arg);
4896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004898 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004900 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004901 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 }
4903
Bram Moolenaare0de1712020-12-02 17:36:54 +01004904 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004905 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004907 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 return FAIL;
4910
Bram Moolenaara5565e42020-05-09 15:44:01 +02004911 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004912 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004913 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4914 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4915 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4916 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004918 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4919 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004920
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004921 // concat/subtract/add constant numbers
4922 if (*op == '+')
4923 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4924 else if (*op == '-')
4925 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4926 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004928 // concatenate constant strings
4929 char_u *s1 = tv1->vval.v_string;
4930 char_u *s2 = tv2->vval.v_string;
4931 size_t len1 = STRLEN(s1);
4932
4933 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4934 if (tv1->vval.v_string == NULL)
4935 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004936 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004937 return FAIL;
4938 }
4939 mch_memmove(tv1->vval.v_string, s1, len1);
4940 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004941 vim_free(s1);
4942 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004943 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004944 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
4946 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004947 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004948 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004949 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004950 if (*op == '.')
4951 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004952 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4953 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004954 return FAIL;
4955 generate_instr_drop(cctx, ISN_CONCAT, 1);
4956 }
4957 else
4958 generate_two_op(cctx, op);
4959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 }
4961
4962 return OK;
4963}
4964
4965/*
4966 * expr5a == expr5b
4967 * expr5a =~ expr5b
4968 * expr5a != expr5b
4969 * expr5a !~ expr5b
4970 * expr5a > expr5b
4971 * expr5a >= expr5b
4972 * expr5a < expr5b
4973 * expr5a <= expr5b
4974 * expr5a is expr5b
4975 * expr5a isnot expr5b
4976 *
4977 * Produces instructions:
4978 * EVAL expr5a Push result of "expr5a"
4979 * EVAL expr5b Push result of "expr5b"
4980 * COMPARE one of the compare instructions
4981 */
4982 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004983compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004985 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004987 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991
4992 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004993 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 return FAIL;
4995
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004996 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004997 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998
4999 /*
5000 * If there is a comparative operator, use it.
5001 */
5002 if (type != EXPR_UNKNOWN)
5003 {
5004 int ic = FALSE; // Default: do not ignore case
5005
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005006 if (next != NULL)
5007 {
5008 *arg = next_line_from_context(cctx, TRUE);
5009 p = skipwhite(*arg);
5010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 if (type_is && (p[len] == '?' || p[len] == '#'))
5012 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005013 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 return FAIL;
5015 }
5016 // extra question mark appended: ignore case
5017 if (p[len] == '?')
5018 {
5019 ic = TRUE;
5020 ++len;
5021 }
5022 // extra '#' appended: match case (ignored)
5023 else if (p[len] == '#')
5024 ++len;
5025 // nothing appended: match case
5026
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005027 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005029 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005030 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 }
5032
5033 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005034 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005035 return FAIL;
5036
Bram Moolenaara5565e42020-05-09 15:44:01 +02005037 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 return FAIL;
5039
Bram Moolenaara5565e42020-05-09 15:44:01 +02005040 if (ppconst->pp_used == ppconst_used + 2)
5041 {
5042 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5043 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5044 int ret;
5045
5046 // Both sides are a constant, compute the result now.
5047 // First check for a valid combination of types, this is more
5048 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005049 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005050 ret = FAIL;
5051 else
5052 {
5053 ret = typval_compare(tv1, tv2, type, ic);
5054 tv1->v_type = VAR_BOOL;
5055 tv1->vval.v_number = tv1->vval.v_number
5056 ? VVAL_TRUE : VVAL_FALSE;
5057 clear_tv(tv2);
5058 --ppconst->pp_used;
5059 }
5060 return ret;
5061 }
5062
5063 generate_ppconst(cctx, ppconst);
5064 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 }
5066
5067 return OK;
5068}
5069
Bram Moolenaar7f141552020-05-09 17:35:53 +02005070static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072/*
5073 * Compile || or &&.
5074 */
5075 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076compile_and_or(
5077 char_u **arg,
5078 cctx_T *cctx,
5079 char *op,
5080 ppconst_T *ppconst,
5081 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005083 char_u *next;
5084 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 int opchar = *op;
5086
5087 if (p[0] == opchar && p[1] == opchar)
5088 {
5089 garray_T *instr = &cctx->ctx_instr;
5090 garray_T end_ga;
5091
5092 /*
5093 * Repeat until there is no following "||" or "&&"
5094 */
5095 ga_init2(&end_ga, sizeof(int), 10);
5096 while (p[0] == opchar && p[1] == opchar)
5097 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005098 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005099 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005100 int start_ctx_lnum = cctx->ctx_lnum;
5101 int save_lnum;
5102
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005103 if (next != NULL)
5104 {
5105 *arg = next_line_from_context(cctx, TRUE);
5106 p = skipwhite(*arg);
5107 }
5108
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005109 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5110 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005111 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005112 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005113 return FAIL;
5114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005116 // TODO: use ppconst if the value is a constant and check
5117 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005118 generate_ppconst(cctx, ppconst);
5119
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005120 // Every part must evaluate to a bool.
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005121 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005122 SOURCING_LNUM = start_lnum;
5123 save_lnum = cctx->ctx_lnum;
5124 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005125 if (bool_on_stack(cctx) == FAIL)
5126 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005127 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005128 ga_clear(&end_ga);
5129 return FAIL;
5130 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005131 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 if (ga_grow(&end_ga, 1) == FAIL)
5134 {
5135 ga_clear(&end_ga);
5136 return FAIL;
5137 }
5138 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5139 ++end_ga.ga_len;
5140 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005141 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142
5143 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005144 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005145 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005146 {
5147 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005148 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005149 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005150
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5152 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 {
5154 ga_clear(&end_ga);
5155 return FAIL;
5156 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005157
5158 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005160 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005162 // Every part must evaluate to a bool.
5163 if (bool_on_stack(cctx) == FAIL)
5164 {
5165 ga_clear(&end_ga);
5166 return FAIL;
5167 }
5168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 // Fill in the end label in all jumps.
5170 while (end_ga.ga_len > 0)
5171 {
5172 isn_T *isn;
5173
5174 --end_ga.ga_len;
5175 isn = ((isn_T *)instr->ga_data)
5176 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5177 isn->isn_arg.jump.jump_where = instr->ga_len;
5178 }
5179 ga_clear(&end_ga);
5180 }
5181
5182 return OK;
5183}
5184
5185/*
5186 * expr4a && expr4a && expr4a logical AND
5187 *
5188 * Produces instructions:
5189 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005190 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005191 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005193 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 * EVAL expr4c Push result of "expr4c"
5195 * end:
5196 */
5197 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005198compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005200 int ppconst_used = ppconst->pp_used;
5201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005203 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 return FAIL;
5205
5206 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005207 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208}
5209
5210/*
5211 * expr3a || expr3b || expr3c logical OR
5212 *
5213 * Produces instructions:
5214 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005215 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005216 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005218 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 * EVAL expr3c Push result of "expr3c"
5220 * end:
5221 */
5222 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005223compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 int ppconst_used = ppconst->pp_used;
5226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005228 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 return FAIL;
5230
5231 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005232 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233}
5234
5235/*
5236 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005238 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 * JUMP_IF_FALSE alt jump if false
5240 * EVAL expr1a
5241 * JUMP_ALWAYS end
5242 * alt: EVAL expr1b
5243 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005244 *
5245 * Toplevel expression: expr2 ?? expr1
5246 * Produces instructions:
5247 * EVAL expr2 Push result of "expr2"
5248 * JUMP_AND_KEEP_IF_TRUE end jump if true
5249 * EVAL expr1
5250 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 */
5252 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005253compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254{
5255 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005256 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005257 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258
Bram Moolenaar3988f642020-08-27 22:43:03 +02005259 // Ignore all kinds of errors when not producing code.
5260 if (cctx->ctx_skip == SKIP_YES)
5261 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005262 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005263 return OK;
5264 }
5265
Bram Moolenaar61a89812020-05-07 16:58:17 +02005266 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 return FAIL;
5269
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005270 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 if (*p == '?')
5272 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005273 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 garray_T *instr = &cctx->ctx_instr;
5275 garray_T *stack = &cctx->ctx_type_stack;
5276 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005277 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005279 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005280 int has_const_expr = FALSE;
5281 int const_value = FALSE;
5282 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005284 if (next != NULL)
5285 {
5286 *arg = next_line_from_context(cctx, TRUE);
5287 p = skipwhite(*arg);
5288 }
5289
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005290 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005291 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005292 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005293 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005294 return FAIL;
5295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297 if (ppconst->pp_used == ppconst_used + 1)
5298 {
5299 // the condition is a constant, we know whether the ? or the :
5300 // expression is to be evaluated.
5301 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005302 if (op_falsy)
5303 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5304 else
5305 {
5306 int error = FALSE;
5307
5308 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5309 &error);
5310 if (error)
5311 return FAIL;
5312 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005313 cctx->ctx_skip = save_skip == SKIP_YES ||
5314 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5315
5316 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5317 // "left ?? right" and "left" is truthy: produce "left"
5318 generate_ppconst(cctx, ppconst);
5319 else
5320 {
5321 clear_tv(&ppconst->pp_tv[ppconst_used]);
5322 --ppconst->pp_used;
5323 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005324 }
5325 else
5326 {
5327 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005328 if (op_falsy)
5329 end_idx = instr->ga_len;
5330 generate_JUMP(cctx, op_falsy
5331 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5332 if (op_falsy)
5333 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
5336 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005337 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005338 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005339 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005340 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342 if (!has_const_expr)
5343 {
5344 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005346 if (!op_falsy)
5347 {
5348 // remember the type and drop it
5349 --stack->ga_len;
5350 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005352 end_idx = instr->ga_len;
5353 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005354
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005355 // jump here from JUMP_IF_FALSE
5356 isn = ((isn_T *)instr->ga_data) + alt_idx;
5357 isn->isn_arg.jump.jump_where = instr->ga_len;
5358 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005361 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005363 // Check for the ":".
5364 p = may_peek_next_line(cctx, *arg, &next);
5365 if (*p != ':')
5366 {
5367 emsg(_(e_missing_colon));
5368 return FAIL;
5369 }
5370 if (next != NULL)
5371 {
5372 *arg = next_line_from_context(cctx, TRUE);
5373 p = skipwhite(*arg);
5374 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005375
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005376 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5377 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005378 semsg(_(e_white_space_required_before_and_after_str_at_str),
5379 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005380 return FAIL;
5381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005383 // evaluate the third expression
5384 if (has_const_expr)
5385 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005386 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005387 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005388 return FAIL;
5389 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5390 return FAIL;
5391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392
Bram Moolenaara5565e42020-05-09 15:44:01 +02005393 if (!has_const_expr)
5394 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005395 type_T **typep;
5396
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005400 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5401 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005403 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005404 isn = ((isn_T *)instr->ga_data) + end_idx;
5405 isn->isn_arg.jump.jump_where = instr->ga_len;
5406 }
5407
5408 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 }
5410 return OK;
5411}
5412
5413/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005414 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005415 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5416 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005417 */
5418 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005419compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005420{
5421 ppconst_T ppconst;
5422
5423 CLEAR_FIELD(ppconst);
5424 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5425 {
5426 clear_ppconst(&ppconst);
5427 return FAIL;
5428 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005429 if (is_const != NULL)
5430 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005431 if (generate_ppconst(cctx, &ppconst) == FAIL)
5432 return FAIL;
5433 return OK;
5434}
5435
5436/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005437 * Toplevel expression.
5438 */
5439 static int
5440compile_expr0(char_u **arg, cctx_T *cctx)
5441{
5442 return compile_expr0_ext(arg, cctx, NULL);
5443}
5444
5445/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005446 * Compile "return [expr]".
5447 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 */
5449 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005450compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451{
5452 char_u *p = arg;
5453 garray_T *stack = &cctx->ctx_type_stack;
5454 type_T *stack_type;
5455
5456 if (*p != NUL && *p != '|' && *p != '\n')
5457 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005458 if (legacy)
5459 {
5460 int save_flags = cmdmod.cmod_flags;
5461
5462 generate_LEGACY_EVAL(cctx, p);
5463 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5464 0, cctx, FALSE, FALSE) == FAIL)
5465 return NULL;
5466 cmdmod.cmod_flags |= CMOD_LEGACY;
5467 (void)skip_expr(&p, NULL);
5468 cmdmod.cmod_flags = save_flags;
5469 }
5470 else
5471 {
5472 // compile return argument into instructions
5473 if (compile_expr0(&p, cctx) == FAIL)
5474 return NULL;
5475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005477 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005478 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005479 // "check_return_type" with uf_ret_type set to &t_unknown is used
5480 // for an inline function without a specified return type. Set the
5481 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005482 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005483 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005484 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5485 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005486 || (!check_return_type
5487 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005488 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005489 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005490 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005491 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005492 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005493 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5494 && stack_type->tt_type != VAR_VOID
5495 && stack_type->tt_type != VAR_UNKNOWN)
5496 {
5497 emsg(_(e_returning_value_in_function_without_return_type));
5498 return NULL;
5499 }
5500 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005501 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005502 return NULL;
5503 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505 }
5506 else
5507 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005508 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005509 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005510 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5511 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005513 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514 return NULL;
5515 }
5516
5517 // No argument, return zero.
5518 generate_PUSHNR(cctx, 0);
5519 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005520
5521 // Undo any command modifiers.
5522 generate_undo_cmdmods(cctx);
5523
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005524 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 return NULL;
5526
5527 // "return val | endif" is possible
5528 return skipwhite(p);
5529}
5530
5531/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005532 * Get a line from the compilation context, compatible with exarg_T getline().
5533 * Return a pointer to the line in allocated memory.
5534 * Return NULL for end-of-file or some error.
5535 */
5536 static char_u *
5537exarg_getline(
5538 int c UNUSED,
5539 void *cookie,
5540 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005541 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005542{
5543 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005544 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005545
Bram Moolenaar66250c92020-08-20 15:02:42 +02005546 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005547 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005548 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005549 return NULL;
5550 ++cctx->ctx_lnum;
5551 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5552 // Comment lines result in NULL pointers, skip them.
5553 if (p != NULL)
5554 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005555 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005556}
5557
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005558 void
5559fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5560{
5561 eap->getline = exarg_getline;
5562 eap->cookie = cctx;
5563}
5564
Bram Moolenaar04b12692020-05-04 23:24:44 +02005565/*
5566 * Compile a nested :def command.
5567 */
5568 static char_u *
5569compile_nested_function(exarg_T *eap, cctx_T *cctx)
5570{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005571 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005572 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005573 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005574 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005575 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005576 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005577 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005578
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005579 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005580 {
5581 emsg(_(e_cannot_use_bang_with_nested_def));
5582 return NULL;
5583 }
5584
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005585 if (*name_start == '/')
5586 {
5587 name_end = skip_regexp(name_start + 1, '/', TRUE);
5588 if (*name_end == '/')
5589 ++name_end;
5590 eap->nextcmd = check_nextcmd(name_end);
5591 }
5592 if (name_end == name_start || *skipwhite(name_end) != '(')
5593 {
5594 if (!ends_excmd2(name_start, name_end))
5595 {
5596 semsg(_(e_invalid_command_str), eap->cmd);
5597 return NULL;
5598 }
5599
5600 // "def" or "def Name": list functions
5601 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5602 return NULL;
5603 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5604 }
5605
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005606 // Only g:Func() can use a namespace.
5607 if (name_start[1] == ':' && !is_global)
5608 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005609 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005610 return NULL;
5611 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005612 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005613 return NULL;
5614
Bram Moolenaar04b12692020-05-04 23:24:44 +02005615 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005616 fill_exarg_from_cctx(eap, cctx);
5617
Bram Moolenaar04b12692020-05-04 23:24:44 +02005618 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005619 lambda_name = vim_strsave(get_lambda_name());
5620 if (lambda_name == NULL)
5621 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005622 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005623
Bram Moolenaar822ba242020-05-24 23:00:18 +02005624 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005625 {
5626 r = eap->skip ? OK : FAIL;
5627 goto theend;
5628 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005629
5630 // copy over the block scope IDs before compiling
5631 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5632 {
5633 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5634
5635 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5636 if (ufunc->uf_block_ids != NULL)
5637 {
5638 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5639 sizeof(int) * block_depth);
5640 ufunc->uf_block_depth = block_depth;
5641 }
5642 }
5643
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005644 compile_type = COMPILE_TYPE(ufunc);
5645#ifdef FEAT_PROFILE
5646 // If the outer function is profiled, also compile the nested function for
5647 // profiling.
5648 if (cctx->ctx_compile_type == CT_PROFILE)
5649 compile_type = CT_PROFILE;
5650#endif
5651 if (func_needs_compiling(ufunc, compile_type)
5652 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005653 {
5654 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005655 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005656 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005657
Bram Moolenaar648594e2021-07-11 17:55:01 +02005658#ifdef FEAT_PROFILE
5659 // When the outer function is compiled for profiling, the nested function
5660 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005661 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005662 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5663#endif
5664
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005665 if (is_global)
5666 {
5667 char_u *func_name = vim_strnsave(name_start + 2,
5668 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005669
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005670 if (func_name == NULL)
5671 r = FAIL;
5672 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005673 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005674 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005675 lambda_name = NULL;
5676 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005677 }
5678 else
5679 {
5680 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005681 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005682 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005683
Bram Moolenaareef21022020-08-01 22:16:43 +02005684 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005685 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005686 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005687 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005688 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5689 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005690 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005691
5692theend:
5693 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005694 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005695}
5696
5697/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 * Return the length of an assignment operator, or zero if there isn't one.
5699 */
5700 int
5701assignment_len(char_u *p, int *heredoc)
5702{
5703 if (*p == '=')
5704 {
5705 if (p[1] == '<' && p[2] == '<')
5706 {
5707 *heredoc = TRUE;
5708 return 3;
5709 }
5710 return 1;
5711 }
5712 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5713 return 2;
5714 if (STRNCMP(p, "..=", 3) == 0)
5715 return 3;
5716 return 0;
5717}
5718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005720 * Generate the load instruction for "name".
5721 */
5722 static void
5723generate_loadvar(
5724 cctx_T *cctx,
5725 assign_dest_T dest,
5726 char_u *name,
5727 lvar_T *lvar,
5728 type_T *type)
5729{
5730 switch (dest)
5731 {
5732 case dest_option:
5733 // TODO: check the option exists
5734 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5735 break;
5736 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005737 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5738 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5739 else
5740 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005741 break;
5742 case dest_buffer:
5743 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5744 break;
5745 case dest_window:
5746 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5747 break;
5748 case dest_tab:
5749 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5750 break;
5751 case dest_script:
5752 compile_load_scriptvar(cctx,
5753 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5754 break;
5755 case dest_env:
5756 // Include $ in the name here
5757 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5758 break;
5759 case dest_reg:
5760 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5761 break;
5762 case dest_vimvar:
5763 generate_LOADV(cctx, name + 2, TRUE);
5764 break;
5765 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005766 if (lvar->lv_from_outer > 0)
5767 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5768 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005769 else
5770 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5771 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005772 case dest_expr:
5773 // list or dict value should already be on the stack.
5774 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005775 }
5776}
5777
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005778/*
5779 * Skip over "[expr]" or ".member".
5780 * Does not check for any errors.
5781 */
5782 static char_u *
5783skip_index(char_u *start)
5784{
5785 char_u *p = start;
5786
5787 if (*p == '[')
5788 {
5789 p = skipwhite(p + 1);
5790 (void)skip_expr(&p, NULL);
5791 p = skipwhite(p);
5792 if (*p == ']')
5793 return p + 1;
5794 return p;
5795 }
5796 // if (*p == '.')
5797 return to_name_end(p + 1, TRUE);
5798}
5799
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005800 void
5801vim9_declare_error(char_u *name)
5802{
5803 char *scope = "";
5804
5805 switch (*name)
5806 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005807 case 'g': scope = _("global"); break;
5808 case 'b': scope = _("buffer"); break;
5809 case 'w': scope = _("window"); break;
5810 case 't': scope = _("tab"); break;
5811 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005812 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005813 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005814 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005815 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005816 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005817 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005818 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005819 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005820 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005821}
5822
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005823/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005824 * For one assignment figure out the type of destination. Return it in "dest".
5825 * When not recognized "dest" is not set.
5826 * For an option "opt_flags" is set.
5827 * For a v:var "vimvaridx" is set.
5828 * "type" is set to the destination type if known, unchanted otherwise.
5829 * Return FAIL if an error message was given.
5830 */
5831 static int
5832get_var_dest(
5833 char_u *name,
5834 assign_dest_T *dest,
5835 int cmdidx,
5836 int *opt_flags,
5837 int *vimvaridx,
5838 type_T **type,
5839 cctx_T *cctx)
5840{
5841 char_u *p;
5842
5843 if (*name == '&')
5844 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005845 int cc;
5846 long numval;
5847 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005848
5849 *dest = dest_option;
5850 if (cmdidx == CMD_final || cmdidx == CMD_const)
5851 {
5852 emsg(_(e_const_option));
5853 return FAIL;
5854 }
5855 p = name;
5856 p = find_option_end(&p, opt_flags);
5857 if (p == NULL)
5858 {
5859 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005860 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005861 return FAIL;
5862 }
5863 cc = *p;
5864 *p = NUL;
5865 opt_type = get_option_value(skip_option_env_lead(name),
5866 &numval, NULL, *opt_flags);
5867 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005868 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005869 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005870 case gov_unknown:
5871 semsg(_(e_unknown_option), name);
5872 return FAIL;
5873 case gov_string:
5874 case gov_hidden_string:
5875 *type = &t_string;
5876 break;
5877 case gov_bool:
5878 case gov_hidden_bool:
5879 *type = &t_bool;
5880 break;
5881 case gov_number:
5882 case gov_hidden_number:
5883 *type = &t_number;
5884 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005885 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005886 }
5887 else if (*name == '$')
5888 {
5889 *dest = dest_env;
5890 *type = &t_string;
5891 }
5892 else if (*name == '@')
5893 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005894 if (name[1] != '@'
5895 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005896 {
5897 emsg_invreg(name[1]);
5898 return FAIL;
5899 }
5900 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005901 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005902 }
5903 else if (STRNCMP(name, "g:", 2) == 0)
5904 {
5905 *dest = dest_global;
5906 }
5907 else if (STRNCMP(name, "b:", 2) == 0)
5908 {
5909 *dest = dest_buffer;
5910 }
5911 else if (STRNCMP(name, "w:", 2) == 0)
5912 {
5913 *dest = dest_window;
5914 }
5915 else if (STRNCMP(name, "t:", 2) == 0)
5916 {
5917 *dest = dest_tab;
5918 }
5919 else if (STRNCMP(name, "v:", 2) == 0)
5920 {
5921 typval_T *vtv;
5922 int di_flags;
5923
5924 *vimvaridx = find_vim_var(name + 2, &di_flags);
5925 if (*vimvaridx < 0)
5926 {
5927 semsg(_(e_variable_not_found_str), name);
5928 return FAIL;
5929 }
5930 // We use the current value of "sandbox" here, is that OK?
5931 if (var_check_ro(di_flags, name, FALSE))
5932 return FAIL;
5933 *dest = dest_vimvar;
5934 vtv = get_vim_var_tv(*vimvaridx);
5935 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5936 }
5937 return OK;
5938}
5939
5940/*
5941 * Generate a STORE instruction for "dest", not being "dest_local".
5942 * Return FAIL when out of memory.
5943 */
5944 static int
5945generate_store_var(
5946 cctx_T *cctx,
5947 assign_dest_T dest,
5948 int opt_flags,
5949 int vimvaridx,
5950 int scriptvar_idx,
5951 int scriptvar_sid,
5952 type_T *type,
5953 char_u *name)
5954{
5955 switch (dest)
5956 {
5957 case dest_option:
5958 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5959 opt_flags);
5960 case dest_global:
5961 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005962 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5963 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005964 case dest_buffer:
5965 // include b: with the name, easier to execute that way
5966 return generate_STORE(cctx, ISN_STOREB, 0, name);
5967 case dest_window:
5968 // include w: with the name, easier to execute that way
5969 return generate_STORE(cctx, ISN_STOREW, 0, name);
5970 case dest_tab:
5971 // include t: with the name, easier to execute that way
5972 return generate_STORE(cctx, ISN_STORET, 0, name);
5973 case dest_env:
5974 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5975 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005976 return generate_STORE(cctx, ISN_STOREREG,
5977 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005978 case dest_vimvar:
5979 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5980 case dest_script:
5981 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005982 // "s:" may be included in the name.
5983 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005984 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005985 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5986 scriptvar_sid, scriptvar_idx, type);
5987 case dest_local:
5988 case dest_expr:
5989 // cannot happen
5990 break;
5991 }
5992 return FAIL;
5993}
5994
Bram Moolenaar752fc692021-01-04 21:57:11 +01005995 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005996generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5997{
5998 if (lhs->lhs_dest != dest_local)
5999 return generate_store_var(cctx, lhs->lhs_dest,
6000 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6001 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6002 lhs->lhs_type, lhs->lhs_name);
6003
6004 if (lhs->lhs_lvar != NULL)
6005 {
6006 garray_T *instr = &cctx->ctx_instr;
6007 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6008
6009 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6010 // ISN_STORENR
6011 if (lhs->lhs_lvar->lv_from_outer == 0
6012 && instr->ga_len == instr_count + 1
6013 && isn->isn_type == ISN_PUSHNR)
6014 {
6015 varnumber_T val = isn->isn_arg.number;
6016 garray_T *stack = &cctx->ctx_type_stack;
6017
6018 isn->isn_type = ISN_STORENR;
6019 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6020 isn->isn_arg.storenr.stnr_val = val;
6021 if (stack->ga_len > 0)
6022 --stack->ga_len;
6023 }
6024 else if (lhs->lhs_lvar->lv_from_outer > 0)
6025 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6026 lhs->lhs_lvar->lv_from_outer);
6027 else
6028 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6029 }
6030 return OK;
6031}
6032
6033 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006034is_decl_command(int cmdidx)
6035{
6036 return cmdidx == CMD_let || cmdidx == CMD_var
6037 || cmdidx == CMD_final || cmdidx == CMD_const;
6038}
6039
6040/*
6041 * Figure out the LHS type and other properties for an assignment or one item
6042 * of ":unlet" with an index.
6043 * Returns OK or FAIL.
6044 */
6045 static int
6046compile_lhs(
6047 char_u *var_start,
6048 lhs_T *lhs,
6049 int cmdidx,
6050 int heredoc,
6051 int oplen,
6052 cctx_T *cctx)
6053{
6054 char_u *var_end;
6055 int is_decl = is_decl_command(cmdidx);
6056
6057 CLEAR_POINTER(lhs);
6058 lhs->lhs_dest = dest_local;
6059 lhs->lhs_vimvaridx = -1;
6060 lhs->lhs_scriptvar_idx = -1;
6061
6062 // "dest_end" is the end of the destination, including "[expr]" or
6063 // ".name".
6064 // "var_end" is the end of the variable/option/etc. name.
6065 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6066 if (*var_start == '@')
6067 var_end = var_start + 2;
6068 else
6069 {
6070 // skip over the leading "&", "&l:", "&g:" and "$"
6071 var_end = skip_option_env_lead(var_start);
6072 var_end = to_name_end(var_end, TRUE);
6073 }
6074
6075 // "a: type" is declaring variable "a" with a type, not dict "a:".
6076 if (is_decl && lhs->lhs_dest_end == var_start + 2
6077 && lhs->lhs_dest_end[-1] == ':')
6078 --lhs->lhs_dest_end;
6079 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6080 --var_end;
6081
6082 // compute the length of the destination without "[expr]" or ".name"
6083 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006084 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006085 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6086 if (lhs->lhs_name == NULL)
6087 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006088
6089 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6090 // Something follows after the variable: "var[idx]" or "var.key".
6091 lhs->lhs_has_index = TRUE;
6092
Bram Moolenaar752fc692021-01-04 21:57:11 +01006093 if (heredoc)
6094 lhs->lhs_type = &t_list_string;
6095 else
6096 lhs->lhs_type = &t_any;
6097
6098 if (cctx->ctx_skip != SKIP_YES)
6099 {
6100 int declare_error = FALSE;
6101
6102 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6103 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6104 &lhs->lhs_type, cctx) == FAIL)
6105 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006106 if (lhs->lhs_dest != dest_local
6107 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006108 {
6109 // Specific kind of variable recognized.
6110 declare_error = is_decl;
6111 }
6112 else
6113 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006114 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006115 if (check_reserved_name(lhs->lhs_name) == FAIL)
6116 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006117
6118 if (lookup_local(var_start, lhs->lhs_varlen,
6119 &lhs->lhs_local_lvar, cctx) == OK)
6120 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6121 else
6122 {
6123 CLEAR_FIELD(lhs->lhs_arg_lvar);
6124 if (arg_exists(var_start, lhs->lhs_varlen,
6125 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6126 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6127 {
6128 if (is_decl)
6129 {
6130 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6131 return FAIL;
6132 }
6133 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6134 }
6135 }
6136 if (lhs->lhs_lvar != NULL)
6137 {
6138 if (is_decl)
6139 {
6140 semsg(_(e_variable_already_declared), lhs->lhs_name);
6141 return FAIL;
6142 }
6143 }
6144 else
6145 {
6146 int script_namespace = lhs->lhs_varlen > 1
6147 && STRNCMP(var_start, "s:", 2) == 0;
6148 int script_var = (script_namespace
6149 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006150 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006152 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 imported_T *import =
6154 find_imported(var_start, lhs->lhs_varlen, cctx);
6155
6156 if (script_namespace || script_var || import != NULL)
6157 {
6158 char_u *rawname = lhs->lhs_name
6159 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6160
6161 if (is_decl)
6162 {
6163 if (script_namespace)
6164 semsg(_(e_cannot_declare_script_variable_in_function),
6165 lhs->lhs_name);
6166 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006167 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006168 lhs->lhs_name);
6169 return FAIL;
6170 }
6171 else if (cctx->ctx_ufunc->uf_script_ctx_version
6172 == SCRIPT_VERSION_VIM9
6173 && script_namespace
6174 && !script_var && import == NULL)
6175 {
6176 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6177 return FAIL;
6178 }
6179
6180 lhs->lhs_dest = dest_script;
6181
6182 // existing script-local variables should have a type
6183 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6184 if (import != NULL)
6185 lhs->lhs_scriptvar_sid = import->imp_sid;
6186 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6187 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006188 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006190 lhs->lhs_scriptvar_sid, rawname,
6191 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6192 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006193 if (lhs->lhs_scriptvar_idx >= 0)
6194 {
6195 scriptitem_T *si = SCRIPT_ITEM(
6196 lhs->lhs_scriptvar_sid);
6197 svar_T *sv =
6198 ((svar_T *)si->sn_var_vals.ga_data)
6199 + lhs->lhs_scriptvar_idx;
6200 lhs->lhs_type = sv->sv_type;
6201 }
6202 }
6203 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006204 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205 == FAIL)
6206 return FAIL;
6207 }
6208 }
6209
6210 if (declare_error)
6211 {
6212 vim9_declare_error(lhs->lhs_name);
6213 return FAIL;
6214 }
6215 }
6216
6217 // handle "a:name" as a name, not index "name" on "a"
6218 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6219 var_end = lhs->lhs_dest_end;
6220
6221 if (lhs->lhs_dest != dest_option)
6222 {
6223 if (is_decl && *var_end == ':')
6224 {
6225 char_u *p;
6226
6227 // parse optional type: "let var: type = expr"
6228 if (!VIM_ISWHITE(var_end[1]))
6229 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006230 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 return FAIL;
6232 }
6233 p = skipwhite(var_end + 1);
6234 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6235 if (lhs->lhs_type == NULL)
6236 return FAIL;
6237 lhs->lhs_has_type = TRUE;
6238 }
6239 else if (lhs->lhs_lvar != NULL)
6240 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6241 }
6242
Bram Moolenaare42939a2021-04-05 17:11:17 +02006243 if (oplen == 3 && !heredoc
6244 && lhs->lhs_dest != dest_global
6245 && !lhs->lhs_has_index
6246 && lhs->lhs_type->tt_type != VAR_STRING
6247 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 {
6249 emsg(_(e_can_only_concatenate_to_string));
6250 return FAIL;
6251 }
6252
6253 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6254 && cctx->ctx_skip != SKIP_YES)
6255 {
6256 if (oplen > 1 && !heredoc)
6257 {
6258 // +=, /=, etc. require an existing variable
6259 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6260 return FAIL;
6261 }
6262 if (!is_decl)
6263 {
6264 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6265 return FAIL;
6266 }
6267
Bram Moolenaar3f327882021-03-17 20:56:38 +01006268 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269 if ((lhs->lhs_type->tt_type == VAR_FUNC
6270 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006271 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006273
6274 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006275 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6276 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6277 if (lhs->lhs_lvar == NULL)
6278 return FAIL;
6279 lhs->lhs_new_local = TRUE;
6280 }
6281
6282 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006283 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 {
6285 // Something follows after the variable: "var[idx]" or "var.key".
6286 // TODO: should we also handle "->func()" here?
6287 if (is_decl)
6288 {
6289 emsg(_(e_cannot_use_index_when_declaring_variable));
6290 return FAIL;
6291 }
6292
6293 if (var_start[lhs->lhs_varlen] == '['
6294 || var_start[lhs->lhs_varlen] == '.')
6295 {
6296 char_u *after = var_start + lhs->lhs_varlen;
6297 char_u *p;
6298
6299 // Only the last index is used below, if there are others
6300 // before it generate code for the expression. Thus for
6301 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6302 for (;;)
6303 {
6304 p = skip_index(after);
6305 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006306 {
6307 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006309 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006310 after = p;
6311 }
6312 if (after > var_start + lhs->lhs_varlen)
6313 {
6314 lhs->lhs_varlen = after - var_start;
6315 lhs->lhs_dest = dest_expr;
6316 // We don't know the type before evaluating the expression,
6317 // use "any" until then.
6318 lhs->lhs_type = &t_any;
6319 }
6320
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 if (lhs->lhs_type->tt_member == NULL)
6322 lhs->lhs_member_type = &t_any;
6323 else
6324 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6325 }
6326 else
6327 {
6328 semsg("Not supported yet: %s", var_start);
6329 return FAIL;
6330 }
6331 }
6332 return OK;
6333}
6334
6335/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006336 * Figure out the LHS and check a few errors.
6337 */
6338 static int
6339compile_assign_lhs(
6340 char_u *var_start,
6341 lhs_T *lhs,
6342 int cmdidx,
6343 int is_decl,
6344 int heredoc,
6345 int oplen,
6346 cctx_T *cctx)
6347{
6348 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6349 return FAIL;
6350
6351 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6352 {
6353 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6354 return FAIL;
6355 }
6356 if (!is_decl && lhs->lhs_lvar != NULL
6357 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6358 {
6359 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6360 return FAIL;
6361 }
6362 return OK;
6363}
6364
6365/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006366 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6367 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006368 */
6369 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006370compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006371 char_u *var_start,
6372 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006373 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006374 cctx_T *cctx)
6375{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006376 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006377 char_u *p;
6378 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006379 int need_white_before = TRUE;
6380 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381
Bram Moolenaar752fc692021-01-04 21:57:11 +01006382 p = var_start + varlen;
6383 if (*p == '[')
6384 {
6385 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006386 if (*p == ':')
6387 {
6388 // empty first index, push zero
6389 r = generate_PUSHNR(cctx, 0);
6390 need_white_before = FALSE;
6391 }
6392 else
6393 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006394
6395 if (r == OK && *skipwhite(p) == ':')
6396 {
6397 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006398 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006399 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006400 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006401 empty_second = *skipwhite(p + 1) == ']';
6402 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6403 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006404 {
6405 semsg(_(e_white_space_required_before_and_after_str_at_str),
6406 ":", p);
6407 return FAIL;
6408 }
6409 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006410 if (*p == ']')
6411 // empty second index, push "none"
6412 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6413 else
6414 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006415 }
6416
Bram Moolenaar752fc692021-01-04 21:57:11 +01006417 if (r == OK && *skipwhite(p) != ']')
6418 {
6419 // this should not happen
6420 emsg(_(e_missbrac));
6421 r = FAIL;
6422 }
6423 }
6424 else // if (*p == '.')
6425 {
6426 char_u *key_end = to_name_end(p + 1, TRUE);
6427 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6428
6429 r = generate_PUSHS(cctx, key);
6430 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006431 return r;
6432}
6433
6434/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006435 * For a LHS with an index, load the variable to be indexed.
6436 */
6437 static int
6438compile_load_lhs(
6439 lhs_T *lhs,
6440 char_u *var_start,
6441 type_T *rhs_type,
6442 cctx_T *cctx)
6443{
6444 if (lhs->lhs_dest == dest_expr)
6445 {
6446 size_t varlen = lhs->lhs_varlen;
6447 int c = var_start[varlen];
6448 char_u *p = var_start;
6449 garray_T *stack = &cctx->ctx_type_stack;
6450
6451 // Evaluate "ll[expr]" of "ll[expr][idx]"
6452 var_start[varlen] = NUL;
6453 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6454 {
6455 // this should not happen
6456 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006457 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006458 return FAIL;
6459 }
6460 var_start[varlen] = c;
6461
6462 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6463 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6464 // now we can properly check the type
6465 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6466 && rhs_type != &t_void
6467 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6468 FALSE, FALSE) == FAIL)
6469 return FAIL;
6470 }
6471 else
6472 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6473 lhs->lhs_lvar, lhs->lhs_type);
6474 return OK;
6475}
6476
6477/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006478 * Produce code for loading "lhs" and also take care of an index.
6479 * Return OK/FAIL.
6480 */
6481 static int
6482compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6483{
6484 compile_load_lhs(lhs, var_start, NULL, cctx);
6485
6486 if (lhs->lhs_has_index)
6487 {
6488 int range = FALSE;
6489
6490 // Get member from list or dict. First compile the
6491 // index value.
6492 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6493 return FAIL;
6494 if (range)
6495 {
6496 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6497 var_start);
6498 return FAIL;
6499 }
6500
6501 // Get the member.
6502 if (compile_member(FALSE, cctx) == FAIL)
6503 return FAIL;
6504 }
6505 return OK;
6506}
6507
6508/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006509 * Assignment to a list or dict member, or ":unlet" for the item, using the
6510 * information in "lhs".
6511 * Returns OK or FAIL.
6512 */
6513 static int
6514compile_assign_unlet(
6515 char_u *var_start,
6516 lhs_T *lhs,
6517 int is_assign,
6518 type_T *rhs_type,
6519 cctx_T *cctx)
6520{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006521 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006522 garray_T *stack = &cctx->ctx_type_stack;
6523 int range = FALSE;
6524
Bram Moolenaar68452172021-04-12 21:21:02 +02006525 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006527 if (is_assign && range && lhs->lhs_type != &t_blob
6528 && lhs->lhs_type != &t_any)
6529 {
6530 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6531 return FAIL;
6532 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006533
6534 if (lhs->lhs_type == &t_any)
6535 {
6536 // Index on variable of unknown type: check at runtime.
6537 dest_type = VAR_ANY;
6538 }
6539 else
6540 {
6541 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006542 if (dest_type == VAR_DICT && range)
6543 {
6544 emsg(e_cannot_use_range_with_dictionary);
6545 return FAIL;
6546 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006547 if (dest_type == VAR_DICT
6548 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006549 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006550 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006551 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006552 type_T *type;
6553
6554 if (range)
6555 {
6556 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6557 if (need_type(type, &t_number,
6558 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006559 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006560 }
6561 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6562 if ((dest_type != VAR_BLOB || type != &t_special)
6563 && need_type(type, &t_number,
6564 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006565 return FAIL;
6566 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006567 }
6568
6569 // Load the dict or list. On the stack we then have:
6570 // - value (for assignment, not for :unlet)
6571 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006572 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006574 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6575 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576
Bram Moolenaar68452172021-04-12 21:21:02 +02006577 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6578 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006579 {
6580 if (is_assign)
6581 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006582 if (range)
6583 {
6584 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6585 return FAIL;
6586 }
6587 else
6588 {
6589 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590
Bram Moolenaar68452172021-04-12 21:21:02 +02006591 if (isn == NULL)
6592 return FAIL;
6593 isn->isn_arg.vartype = dest_type;
6594 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006596 else if (range)
6597 {
6598 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6599 return FAIL;
6600 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006601 else
6602 {
6603 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6604 return FAIL;
6605 }
6606 }
6607 else
6608 {
6609 emsg(_(e_indexable_type_required));
6610 return FAIL;
6611 }
6612
6613 return OK;
6614}
6615
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006616/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006617 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006618 * "let name"
6619 * "var name = expr"
6620 * "final name = expr"
6621 * "const name = expr"
6622 * "name = expr"
6623 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006624 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006625 * Return NULL for an error.
6626 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 */
6628 static char_u *
6629compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6630{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006631 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006633 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 char_u *ret = NULL;
6635 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006638 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006640 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 int oplen = 0;
6643 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006644 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006645 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006647 int is_decl = is_decl_command(cmdidx);
6648 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006649 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006651 // Skip over the "var" or "[var, var]" to get to any "=".
6652 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6653 if (p == NULL)
6654 return *arg == '[' ? arg : NULL;
6655
6656 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006658 // TODO: should we allow this, and figure out type inference from list
6659 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006660 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 return NULL;
6662 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006663 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 sp = p;
6666 p = skipwhite(p);
6667 op = p;
6668 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006669
6670 if (var_count > 0 && oplen == 0)
6671 // can be something like "[1, 2]->func()"
6672 return arg;
6673
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006674 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006676 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006677 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006678 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006679 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6680 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006681 if (VIM_ISWHITE(eap->cmd[2]))
6682 {
6683 semsg(_(e_no_white_space_allowed_after_str_str),
6684 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6685 return NULL;
6686 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006687 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6688 oplen = 2;
6689 incdec = TRUE;
6690 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 if (heredoc)
6693 {
6694 list_T *l;
6695 listitem_T *li;
6696
6697 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006698 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006700 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006701 if (l == NULL)
6702 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703
Bram Moolenaar078269b2020-09-21 20:35:55 +02006704 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006706 // Push each line and the create the list.
6707 FOR_ALL_LIST_ITEMS(l, li)
6708 {
6709 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6710 li->li_tv.vval.v_string = NULL;
6711 }
6712 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 list_free(l);
6715 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006718 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006720 char_u *wp;
6721
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006722 // for "[var, var] = expr" evaluate the expression here, loop over the
6723 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006724 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006725
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006726 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006727 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006728 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006729 if (compile_expr0(&p, cctx) == FAIL)
6730 return NULL;
6731 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006733 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006735 type_T *stacktype;
6736
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006737 stacktype = stack->ga_len == 0 ? &t_void
6738 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006739 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006741 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006742 goto theend;
6743 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006744 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006745 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006746 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006747 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006748 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6749 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006750 if (stacktype->tt_member != NULL)
6751 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 }
6753 }
6754
6755 /*
6756 * Loop over variables in "[var, var] = expr".
6757 * For "var = expr" and "let var: type" this is done only once.
6758 */
6759 if (var_count > 0)
6760 var_start = skipwhite(arg + 1); // skip over the "["
6761 else
6762 var_start = arg;
6763 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6764 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006765 int instr_count = -1;
6766 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006767
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006768 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6769 {
6770 // Ignore underscore in "[a, _, b] = list".
6771 if (var_count > 0)
6772 {
6773 var_start = skipwhite(var_start + 2);
6774 continue;
6775 }
6776 emsg(_(e_cannot_use_underscore_here));
6777 goto theend;
6778 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006779 vim_free(lhs.lhs_name);
6780
6781 /*
6782 * Figure out the LHS type and other properties.
6783 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006784 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6785 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006786 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 if (!heredoc)
6788 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006789 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006790 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006791 if (oplen > 0 && var_count == 0)
6792 {
6793 // skip over the "=" and the expression
6794 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006795 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006796 }
6797 }
6798 else if (oplen > 0)
6799 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006800 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006801 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006802
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006803 // for "+=", "*=", "..=" etc. first load the current value
6804 if (*op != '='
6805 && compile_load_lhs_with_index(&lhs, var_start,
6806 cctx) == FAIL)
6807 goto theend;
6808
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 // For "var = expr" evaluate the expression.
6810 if (var_count == 0)
6811 {
6812 int r;
6813
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006814 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006815 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006816 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006817 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006818 r = generate_PUSHNR(cctx, 1);
6819 }
6820 else
6821 {
6822 // Temporarily hide the new local variable here, it is
6823 // not available to this expression.
6824 if (lhs.lhs_new_local)
6825 --cctx->ctx_locals.ga_len;
6826 wp = op + oplen;
6827 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6828 {
6829 if (lhs.lhs_new_local)
6830 ++cctx->ctx_locals.ga_len;
6831 goto theend;
6832 }
6833 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006835 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006836 if (r == FAIL)
6837 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006838 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006839 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006840 else if (semicolon && var_idx == var_count - 1)
6841 {
6842 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006843 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006844 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6845 goto theend;
6846 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006847 else
6848 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006849 // For "[var, var] = expr" get the "var_idx" item from the
6850 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006851 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006852 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006853 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006854
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006855 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006856 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006858 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006859 if ((rhs_type->tt_type == VAR_FUNC
6860 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006861 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006862 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006863 goto theend;
6864
Bram Moolenaar752fc692021-01-04 21:57:11 +01006865 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006866 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006867 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006868 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006869 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006870 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 }
6872 else
6873 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006874 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006875 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006876 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006877 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006878 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006879 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006880 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006881 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006882 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006883 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006884 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006885 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006886 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006887 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006888 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006889
Bram Moolenaar77709b12021-04-03 21:01:01 +02006890 // Without operator check type here, otherwise below.
6891 // Use the line number of the assignment.
6892 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 if (lhs.lhs_has_index)
6894 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006895 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006896 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006897 goto theend;
6898 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006899 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006900 else
6901 {
6902 type_T *lhs_type = lhs.lhs_member_type;
6903
6904 // Special case: assigning to @# can use a number or a
6905 // string.
6906 if (lhs_type == &t_number_or_string
6907 && rhs_type->tt_type == VAR_NUMBER)
6908 lhs_type = &t_number;
6909 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006910 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006911 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006914 else if (cmdidx == CMD_final)
6915 {
6916 emsg(_(e_final_requires_a_value));
6917 goto theend;
6918 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006919 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006921 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006922 goto theend;
6923 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006924 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006925 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006926 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006927 goto theend;
6928 }
6929 else
6930 {
6931 // variables are always initialized
6932 if (ga_grow(instr, 1) == FAIL)
6933 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006934 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 {
6936 case VAR_BOOL:
6937 generate_PUSHBOOL(cctx, VVAL_FALSE);
6938 break;
6939 case VAR_FLOAT:
6940#ifdef FEAT_FLOAT
6941 generate_PUSHF(cctx, 0.0);
6942#endif
6943 break;
6944 case VAR_STRING:
6945 generate_PUSHS(cctx, NULL);
6946 break;
6947 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006948 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006949 break;
6950 case VAR_FUNC:
6951 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6952 break;
6953 case VAR_LIST:
6954 generate_NEWLIST(cctx, 0);
6955 break;
6956 case VAR_DICT:
6957 generate_NEWDICT(cctx, 0);
6958 break;
6959 case VAR_JOB:
6960 generate_PUSHJOB(cctx, NULL);
6961 break;
6962 case VAR_CHANNEL:
6963 generate_PUSHCHANNEL(cctx, NULL);
6964 break;
6965 case VAR_NUMBER:
6966 case VAR_UNKNOWN:
6967 case VAR_ANY:
6968 case VAR_PARTIAL:
6969 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006970 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971 case VAR_SPECIAL: // cannot happen
6972 generate_PUSHNR(cctx, 0);
6973 break;
6974 }
6975 }
6976 if (var_count == 0)
6977 end = p;
6978 }
6979
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006980 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006981 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006982 break;
6983
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 if (oplen > 0 && *op != '=')
6985 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006986 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006987 type_T *stacktype;
6988
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 if (*op == '.')
6990 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006991 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006992 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006993 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006994 if (
6995#ifdef FEAT_FLOAT
6996 // If variable is float operation with number is OK.
6997 !(expected == &t_float && stacktype == &t_number) &&
6998#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006999 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007000 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007001 goto theend;
7002
7003 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007004 {
7005 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7006 goto theend;
7007 }
7008 else if (*op == '+')
7009 {
7010 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007011 operator_type(lhs.lhs_member_type, stacktype),
7012 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007013 goto theend;
7014 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007015 else if (generate_two_op(cctx, op) == FAIL)
7016 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007019 // Use the line number of the assignment for store instruction.
7020 save_lnum = cctx->ctx_lnum;
7021 cctx->ctx_lnum = start_lnum - 1;
7022
Bram Moolenaar752fc692021-01-04 21:57:11 +01007023 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007024 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007025 // Use the info in "lhs" to store the value at the index in the
7026 // list or dict.
7027 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7028 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007029 {
7030 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007031 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007032 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007033 }
7034 else
7035 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007036 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007037 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007038 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007039 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007040 generate_LOCKCONST(cctx);
7041
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007042 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007043 && (lhs.lhs_type->tt_type == VAR_DICT
7044 || lhs.lhs_type->tt_type == VAR_LIST)
7045 && lhs.lhs_type->tt_member != NULL
7046 && lhs.lhs_type->tt_member != &t_any
7047 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007048 // Set the type in the list or dict, so that it can be checked,
7049 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007050 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007051
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007052 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007053 {
7054 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007055 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007056 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007057 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007058 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007059
7060 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007061 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007063
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007064 // For "[var, var] = expr" drop the "expr" value.
7065 // Also for "[var, var; _] = expr".
7066 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007067 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007068 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007069 goto theend;
7070 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007071
Bram Moolenaarb2097502020-07-19 17:17:02 +02007072 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073
7074theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007075 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 return ret;
7077}
7078
7079/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007080 * Check for an assignment at "eap->cmd", compile it if found.
7081 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7082 */
7083 static int
7084may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7085{
7086 char_u *pskip;
7087 char_u *p;
7088
7089 // Assuming the command starts with a variable or function name,
7090 // find what follows.
7091 // Skip over "var.member", "var[idx]" and the like.
7092 // Also "&opt = val", "$ENV = val" and "@r = val".
7093 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7094 ? eap->cmd + 1 : eap->cmd;
7095 p = to_name_end(pskip, TRUE);
7096 if (p > eap->cmd && *p != NUL)
7097 {
7098 char_u *var_end;
7099 int oplen;
7100 int heredoc;
7101
7102 if (eap->cmd[0] == '@')
7103 var_end = eap->cmd + 2;
7104 else
7105 var_end = find_name_end(pskip, NULL, NULL,
7106 FNE_CHECK_START | FNE_INCL_BR);
7107 oplen = assignment_len(skipwhite(var_end), &heredoc);
7108 if (oplen > 0)
7109 {
7110 size_t len = p - eap->cmd;
7111
7112 // Recognize an assignment if we recognize the variable
7113 // name:
7114 // "g:var = expr"
7115 // "local = expr" where "local" is a local var.
7116 // "script = expr" where "script" is a script-local var.
7117 // "import = expr" where "import" is an imported var
7118 // "&opt = expr"
7119 // "$ENV = expr"
7120 // "@r = expr"
7121 if (*eap->cmd == '&'
7122 || *eap->cmd == '$'
7123 || *eap->cmd == '@'
7124 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007125 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007126 {
7127 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7128 if (*line == NULL || *line == eap->cmd)
7129 return FAIL;
7130 return OK;
7131 }
7132 }
7133 }
7134
7135 if (*eap->cmd == '[')
7136 {
7137 // [var, var] = expr
7138 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7139 if (*line == NULL)
7140 return FAIL;
7141 if (*line != eap->cmd)
7142 return OK;
7143 }
7144 return NOTDONE;
7145}
7146
7147/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007148 * Check if "name" can be "unlet".
7149 */
7150 int
7151check_vim9_unlet(char_u *name)
7152{
7153 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7154 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007155 // "unlet s:var" is allowed in legacy script.
7156 if (*name == 's' && !script_is_vim9())
7157 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007158 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007159 return FAIL;
7160 }
7161 return OK;
7162}
7163
7164/*
7165 * Callback passed to ex_unletlock().
7166 */
7167 static int
7168compile_unlet(
7169 lval_T *lvp,
7170 char_u *name_end,
7171 exarg_T *eap,
7172 int deep UNUSED,
7173 void *coookie)
7174{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007175 cctx_T *cctx = coookie;
7176 char_u *p = lvp->ll_name;
7177 int cc = *name_end;
7178 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007179
Bram Moolenaar752fc692021-01-04 21:57:11 +01007180 if (cctx->ctx_skip == SKIP_YES)
7181 return OK;
7182
7183 *name_end = NUL;
7184 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007185 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007186 // :unlet $ENV_VAR
7187 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7188 }
7189 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7190 {
7191 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007192
Bram Moolenaar752fc692021-01-04 21:57:11 +01007193 // This is similar to assigning: lookup the list/dict, compile the
7194 // idx/key. Then instead of storing the value unlet the item.
7195 // unlet {list}[idx]
7196 // unlet {dict}[key] dict.key
7197 //
7198 // Figure out the LHS type and other properties.
7199 //
7200 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7201
7202 // : unlet an indexed item
7203 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007204 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007205 iemsg("called compile_lhs() without an index");
7206 ret = FAIL;
7207 }
7208 else
7209 {
7210 // Use the info in "lhs" to unlet the item at the index in the
7211 // list or dict.
7212 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007213 }
7214
Bram Moolenaar752fc692021-01-04 21:57:11 +01007215 vim_free(lhs.lhs_name);
7216 }
7217 else if (check_vim9_unlet(p) == FAIL)
7218 {
7219 ret = FAIL;
7220 }
7221 else
7222 {
7223 // Normal name. Only supports g:, w:, t: and b: namespaces.
7224 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007225 }
7226
Bram Moolenaar752fc692021-01-04 21:57:11 +01007227 *name_end = cc;
7228 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007229}
7230
7231/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007232 * Callback passed to ex_unletlock().
7233 */
7234 static int
7235compile_lock_unlock(
7236 lval_T *lvp,
7237 char_u *name_end,
7238 exarg_T *eap,
7239 int deep UNUSED,
7240 void *coookie)
7241{
7242 cctx_T *cctx = coookie;
7243 int cc = *name_end;
7244 char_u *p = lvp->ll_name;
7245 int ret = OK;
7246 size_t len;
7247 char_u *buf;
7248
7249 if (cctx->ctx_skip == SKIP_YES)
7250 return OK;
7251
7252 // Cannot use :lockvar and :unlockvar on local variables.
7253 if (p[1] != ':')
7254 {
7255 char_u *end = skip_var_one(p, FALSE);
7256
7257 if (lookup_local(p, end - p, NULL, cctx) == OK)
7258 {
7259 emsg(_(e_cannot_lock_unlock_local_variable));
7260 return FAIL;
7261 }
7262 }
7263
7264 // Checking is done at runtime.
7265 *name_end = NUL;
7266 len = name_end - p + 20;
7267 buf = alloc(len);
7268 if (buf == NULL)
7269 ret = FAIL;
7270 else
7271 {
7272 vim_snprintf((char *)buf, len, "%s %s",
7273 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7274 p);
7275 ret = generate_EXEC(cctx, buf);
7276
7277 vim_free(buf);
7278 *name_end = cc;
7279 }
7280 return ret;
7281}
7282
7283/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007284 * compile "unlet var", "lock var" and "unlock var"
7285 * "arg" points to "var".
7286 */
7287 static char_u *
7288compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7289{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007290 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7291 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7292 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007293 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7294}
7295
7296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 * Compile an :import command.
7298 */
7299 static char_u *
7300compile_import(char_u *arg, cctx_T *cctx)
7301{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007302 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303}
7304
7305/*
7306 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7307 */
7308 static int
7309compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7310{
7311 garray_T *instr = &cctx->ctx_instr;
7312 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7313
7314 if (endlabel == NULL)
7315 return FAIL;
7316 endlabel->el_next = *el;
7317 *el = endlabel;
7318 endlabel->el_end_label = instr->ga_len;
7319
7320 generate_JUMP(cctx, when, 0);
7321 return OK;
7322}
7323
7324 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007325compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326{
7327 garray_T *instr = &cctx->ctx_instr;
7328
7329 while (*el != NULL)
7330 {
7331 endlabel_T *cur = (*el);
7332 isn_T *isn;
7333
7334 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007335 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 *el = cur->el_next;
7337 vim_free(cur);
7338 }
7339}
7340
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007341 static void
7342compile_free_jump_to_end(endlabel_T **el)
7343{
7344 while (*el != NULL)
7345 {
7346 endlabel_T *cur = (*el);
7347
7348 *el = cur->el_next;
7349 vim_free(cur);
7350 }
7351}
7352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353/*
7354 * Create a new scope and set up the generic items.
7355 */
7356 static scope_T *
7357new_scope(cctx_T *cctx, scopetype_T type)
7358{
7359 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7360
7361 if (scope == NULL)
7362 return NULL;
7363 scope->se_outer = cctx->ctx_scope;
7364 cctx->ctx_scope = scope;
7365 scope->se_type = type;
7366 scope->se_local_count = cctx->ctx_locals.ga_len;
7367 return scope;
7368}
7369
7370/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007371 * Free the current scope and go back to the outer scope.
7372 */
7373 static void
7374drop_scope(cctx_T *cctx)
7375{
7376 scope_T *scope = cctx->ctx_scope;
7377
7378 if (scope == NULL)
7379 {
7380 iemsg("calling drop_scope() without a scope");
7381 return;
7382 }
7383 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007384 switch (scope->se_type)
7385 {
7386 case IF_SCOPE:
7387 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7388 case FOR_SCOPE:
7389 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7390 case WHILE_SCOPE:
7391 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7392 case TRY_SCOPE:
7393 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7394 case NO_SCOPE:
7395 case BLOCK_SCOPE:
7396 break;
7397 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007398 vim_free(scope);
7399}
7400
7401/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 * compile "if expr"
7403 *
7404 * "if expr" Produces instructions:
7405 * EVAL expr Push result of "expr"
7406 * JUMP_IF_FALSE end
7407 * ... body ...
7408 * end:
7409 *
7410 * "if expr | else" Produces instructions:
7411 * EVAL expr Push result of "expr"
7412 * JUMP_IF_FALSE else
7413 * ... body ...
7414 * JUMP_ALWAYS end
7415 * else:
7416 * ... body ...
7417 * end:
7418 *
7419 * "if expr1 | elseif expr2 | else" Produces instructions:
7420 * EVAL expr Push result of "expr"
7421 * JUMP_IF_FALSE elseif
7422 * ... body ...
7423 * JUMP_ALWAYS end
7424 * elseif:
7425 * EVAL expr Push result of "expr"
7426 * JUMP_IF_FALSE else
7427 * ... body ...
7428 * JUMP_ALWAYS end
7429 * else:
7430 * ... body ...
7431 * end:
7432 */
7433 static char_u *
7434compile_if(char_u *arg, cctx_T *cctx)
7435{
7436 char_u *p = arg;
7437 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007438 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007440 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007441 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442
Bram Moolenaara5565e42020-05-09 15:44:01 +02007443 CLEAR_FIELD(ppconst);
7444 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007445 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007446 clear_ppconst(&ppconst);
7447 return NULL;
7448 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007449 if (!ends_excmd2(arg, skipwhite(p)))
7450 {
7451 semsg(_(e_trailing_arg), p);
7452 return NULL;
7453 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007454 if (cctx->ctx_skip == SKIP_YES)
7455 clear_ppconst(&ppconst);
7456 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007457 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007458 int error = FALSE;
7459 int v;
7460
Bram Moolenaara5565e42020-05-09 15:44:01 +02007461 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007462 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007463 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007464 if (error)
7465 return NULL;
7466 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007467 }
7468 else
7469 {
7470 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007471 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007472 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007473 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007474 if (bool_on_stack(cctx) == FAIL)
7475 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477
Bram Moolenaara91a7132021-03-25 21:12:15 +01007478 // CMDMOD_REV must come before the jump
7479 generate_undo_cmdmods(cctx);
7480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 scope = new_scope(cctx, IF_SCOPE);
7482 if (scope == NULL)
7483 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007484 scope->se_skip_save = skip_save;
7485 // "is_had_return" will be reset if any block does not end in :return
7486 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007488 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007489 {
7490 // "where" is set when ":elseif", "else" or ":endif" is found
7491 scope->se_u.se_if.is_if_label = instr->ga_len;
7492 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7493 }
7494 else
7495 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496
Bram Moolenaarced68a02021-01-24 17:53:47 +01007497#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007498 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007499 && skip_save != SKIP_YES)
7500 {
7501 // generated a profile start, need to generate a profile end, since it
7502 // won't be done after returning
7503 cctx->ctx_skip = SKIP_NOT;
7504 generate_instr(cctx, ISN_PROF_END);
7505 cctx->ctx_skip = SKIP_YES;
7506 }
7507#endif
7508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 return p;
7510}
7511
7512 static char_u *
7513compile_elseif(char_u *arg, cctx_T *cctx)
7514{
7515 char_u *p = arg;
7516 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007517 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 isn_T *isn;
7519 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007520 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007521 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522
7523 if (scope == NULL || scope->se_type != IF_SCOPE)
7524 {
7525 emsg(_(e_elseif_without_if));
7526 return NULL;
7527 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007528 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007529 if (!cctx->ctx_had_return)
7530 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531
Bram Moolenaarced68a02021-01-24 17:53:47 +01007532 if (cctx->ctx_skip == SKIP_NOT)
7533 {
7534 // previous block was executed, this one and following will not
7535 cctx->ctx_skip = SKIP_YES;
7536 scope->se_u.se_if.is_seen_skip_not = TRUE;
7537 }
7538 if (scope->se_u.se_if.is_seen_skip_not)
7539 {
7540 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007541 // Do not count the "elseif" for profiling and cmdmod
7542 instr->ga_len = current_instr_idx(cctx);
7543
Bram Moolenaarced68a02021-01-24 17:53:47 +01007544 skip_expr_cctx(&p, cctx);
7545 return p;
7546 }
7547
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007548 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007549 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007550 int moved_cmdmod = FALSE;
7551
7552 // Move any CMDMOD instruction to after the jump
7553 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7554 {
7555 if (ga_grow(instr, 1) == FAIL)
7556 return NULL;
7557 ((isn_T *)instr->ga_data)[instr->ga_len] =
7558 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7559 --instr->ga_len;
7560 moved_cmdmod = TRUE;
7561 }
7562
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007563 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007565 return NULL;
7566 // previous "if" or "elseif" jumps here
7567 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7568 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007569 if (moved_cmdmod)
7570 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007573 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007574 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007575 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007576 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007577 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007578#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007579 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007580 {
7581 // the previous block was skipped, need to profile this line
7582 generate_instr(cctx, ISN_PROF_START);
7583 instr_count = instr->ga_len;
7584 }
7585#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007586 if (cctx->ctx_compile_type == CT_DEBUG)
7587 {
7588 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007589 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007590 instr_count = instr->ga_len;
7591 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007592 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007593 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007594 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007595 clear_ppconst(&ppconst);
7596 return NULL;
7597 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007598 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007599 if (!ends_excmd2(arg, skipwhite(p)))
7600 {
7601 semsg(_(e_trailing_arg), p);
7602 return NULL;
7603 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007604 if (scope->se_skip_save == SKIP_YES)
7605 clear_ppconst(&ppconst);
7606 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007607 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007608 int error = FALSE;
7609 int v;
7610
Bram Moolenaar7f141552020-05-09 17:35:53 +02007611 // The expression results in a constant.
7612 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007613 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7614 if (error)
7615 return NULL;
7616 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007617 clear_ppconst(&ppconst);
7618 scope->se_u.se_if.is_if_label = -1;
7619 }
7620 else
7621 {
7622 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007623 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007624 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007625 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007626 if (bool_on_stack(cctx) == FAIL)
7627 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628
Bram Moolenaara91a7132021-03-25 21:12:15 +01007629 // CMDMOD_REV must come before the jump
7630 generate_undo_cmdmods(cctx);
7631
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007632 // "where" is set when ":elseif", "else" or ":endif" is found
7633 scope->se_u.se_if.is_if_label = instr->ga_len;
7634 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636
7637 return p;
7638}
7639
7640 static char_u *
7641compile_else(char_u *arg, cctx_T *cctx)
7642{
7643 char_u *p = arg;
7644 garray_T *instr = &cctx->ctx_instr;
7645 isn_T *isn;
7646 scope_T *scope = cctx->ctx_scope;
7647
7648 if (scope == NULL || scope->se_type != IF_SCOPE)
7649 {
7650 emsg(_(e_else_without_if));
7651 return NULL;
7652 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007653 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007654 if (!cctx->ctx_had_return)
7655 scope->se_u.se_if.is_had_return = FALSE;
7656 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657
Bram Moolenaarced68a02021-01-24 17:53:47 +01007658#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007659 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007660 {
7661 if (cctx->ctx_skip == SKIP_NOT
7662 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7663 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007664 // the previous block was executed, do not count "else" for
7665 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007666 --instr->ga_len;
7667 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7668 {
7669 // the previous block was not executed, this one will, do count the
7670 // "else" for profiling
7671 cctx->ctx_skip = SKIP_NOT;
7672 generate_instr(cctx, ISN_PROF_END);
7673 generate_instr(cctx, ISN_PROF_START);
7674 cctx->ctx_skip = SKIP_YES;
7675 }
7676 }
7677#endif
7678
7679 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007680 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007681 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007682 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007683 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007684 if (!cctx->ctx_had_return
7685 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7686 JUMP_ALWAYS, cctx) == FAIL)
7687 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007688 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007689
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007690 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007691 {
7692 if (scope->se_u.se_if.is_if_label >= 0)
7693 {
7694 // previous "if" or "elseif" jumps here
7695 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7696 isn->isn_arg.jump.jump_where = instr->ga_len;
7697 scope->se_u.se_if.is_if_label = -1;
7698 }
7699 }
7700
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007701 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007702 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704
7705 return p;
7706}
7707
7708 static char_u *
7709compile_endif(char_u *arg, cctx_T *cctx)
7710{
7711 scope_T *scope = cctx->ctx_scope;
7712 ifscope_T *ifscope;
7713 garray_T *instr = &cctx->ctx_instr;
7714 isn_T *isn;
7715
Bram Moolenaarfa984412021-03-25 22:15:28 +01007716 if (misplaced_cmdmod(cctx))
7717 return NULL;
7718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 if (scope == NULL || scope->se_type != IF_SCOPE)
7720 {
7721 emsg(_(e_endif_without_if));
7722 return NULL;
7723 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007724 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007725 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007726 if (!cctx->ctx_had_return)
7727 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007729 if (scope->se_u.se_if.is_if_label >= 0)
7730 {
7731 // previous "if" or "elseif" jumps here
7732 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7733 isn->isn_arg.jump.jump_where = instr->ga_len;
7734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007736 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007737
7738#ifdef FEAT_PROFILE
7739 // even when skipping we count the endif as executed, unless the block it's
7740 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007741 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007742 && scope->se_skip_save != SKIP_YES)
7743 {
7744 cctx->ctx_skip = SKIP_NOT;
7745 generate_instr(cctx, ISN_PROF_START);
7746 }
7747#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007748 cctx->ctx_skip = scope->se_skip_save;
7749
7750 // If all the blocks end in :return and there is an :else then the
7751 // had_return flag is set.
7752 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007754 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 return arg;
7756}
7757
7758/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007759 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 *
7761 * Produces instructions:
7762 * PUSHNR -1
7763 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007764 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7766 * - if beyond end, jump to "end"
7767 * - otherwise get item from list and push it
7768 * STORE var Store item in "var"
7769 * ... body ...
7770 * JUMP top Jump back to repeat
7771 * end: DROP Drop the result of "expr"
7772 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007773 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7774 * UNPACK 2 Split item in 2
7775 * STORE var1 Store item in "var1"
7776 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 */
7778 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007779compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007781 char_u *arg;
7782 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007783 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007785 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007786 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007787 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007788 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007791 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007793 lvar_T *loop_lvar; // loop iteration variable
7794 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007796 type_T *item_type = &t_any;
7797 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007798 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799
Bram Moolenaar792f7862020-11-23 08:31:18 +01007800 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007801 if (p == NULL)
7802 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007803 if (var_count == 0)
7804 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007805 else
7806 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807
7808 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007809 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007810 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7811 return NULL;
7812 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007814 if (*p == ':' && wp != p)
7815 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7816 else
7817 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 return NULL;
7819 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007820 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007821 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7822 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007824 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7825 // instruction.
7826 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7827 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7828 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007829 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007830 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007831 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7832 .isn_arg.debug.dbg_break_lnum;
7833 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 scope = new_scope(cctx, FOR_SCOPE);
7836 if (scope == NULL)
7837 return NULL;
7838
Bram Moolenaar792f7862020-11-23 08:31:18 +01007839 // Reserve a variable to store the loop iteration counter and initialize it
7840 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007841 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7842 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007843 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007844 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007845 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007847 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007848 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849
7850 // compile "expr", it remains on the stack until "endfor"
7851 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007852 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007853 {
7854 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007856 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007857 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007859 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007860 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007862 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007863 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007865 semsg(_(e_for_loop_on_str_not_supported),
7866 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007867 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 return NULL;
7869 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007870
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007871 if (vartype->tt_type == VAR_STRING)
7872 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007873 else if (vartype->tt_type == VAR_BLOB)
7874 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007875 else if (vartype->tt_type == VAR_LIST
7876 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007877 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007878 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007879 item_type = vartype->tt_member;
7880 else if (vartype->tt_member->tt_type == VAR_LIST
7881 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007882 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007883 item_type = vartype->tt_member->tt_member;
7884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007886 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007887 generate_undo_cmdmods(cctx);
7888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007890 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007891
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007892 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893
Bram Moolenaar792f7862020-11-23 08:31:18 +01007894 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007895 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007896 {
7897 generate_UNPACK(cctx, var_count, semicolon);
7898 arg = skipwhite(arg + 1); // skip white after '['
7899
7900 // the list item is replaced by a number of items
7901 if (ga_grow(stack, var_count - 1) == FAIL)
7902 {
7903 drop_scope(cctx);
7904 return NULL;
7905 }
7906 --stack->ga_len;
7907 for (idx = 0; idx < var_count; ++idx)
7908 {
7909 ((type_T **)stack->ga_data)[stack->ga_len] =
7910 (semicolon && idx == 0) ? vartype : item_type;
7911 ++stack->ga_len;
7912 }
7913 }
7914
7915 for (idx = 0; idx < var_count; ++idx)
7916 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007917 assign_dest_T dest = dest_local;
7918 int opt_flags = 0;
7919 int vimvaridx = -1;
7920 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007921 type_T *lhs_type = &t_any;
7922 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007923
7924 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007925 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007926 name = vim_strnsave(arg, varlen);
7927 if (name == NULL)
7928 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007929 if (*p == ':')
7930 {
7931 p = skipwhite(p + 1);
7932 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7933 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007934
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007935 // TODO: script var not supported?
7936 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7937 &vimvaridx, &type, cctx) == FAIL)
7938 goto failed;
7939 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007940 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007941 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7942 0, 0, type, name) == FAIL)
7943 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007944 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007945 else if (varlen == 1 && *arg == '_')
7946 {
7947 // Assigning to "_": drop the value.
7948 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7949 goto failed;
7950 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007951 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007952 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007953 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007954 {
7955 semsg(_(e_variable_already_declared), arg);
7956 goto failed;
7957 }
7958
Bram Moolenaarea870692020-12-02 14:24:30 +01007959 if (STRNCMP(name, "s:", 2) == 0)
7960 {
7961 semsg(_(e_cannot_declare_script_variable_in_function), name);
7962 goto failed;
7963 }
7964
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007965 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007966 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007967 where.wt_variable = TRUE;
7968 if (lhs_type == &t_any)
7969 lhs_type = item_type;
7970 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02007971 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02007972 ? need_type(item_type, lhs_type,
7973 -1, 0, cctx, FALSE, FALSE)
7974 : check_type(lhs_type, item_type, TRUE, where))
7975 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007976 goto failed;
7977 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007978 if (var_lvar == NULL)
7979 // out of memory or used as an argument
7980 goto failed;
7981
7982 if (semicolon && idx == var_count - 1)
7983 var_lvar->lv_type = vartype;
7984 else
7985 var_lvar->lv_type = item_type;
7986 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7987 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007988
7989 if (*p == ',' || *p == ';')
7990 ++p;
7991 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007992 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007993 }
7994
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007995 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007996 {
7997 int save_prev_lnum = cctx->ctx_prev_lnum;
7998
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007999 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008000 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8001 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008002 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008003 cctx->ctx_prev_lnum = save_prev_lnum;
8004 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008005
Bram Moolenaar792f7862020-11-23 08:31:18 +01008006 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008007
8008failed:
8009 vim_free(name);
8010 drop_scope(cctx);
8011 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012}
8013
8014/*
8015 * compile "endfor"
8016 */
8017 static char_u *
8018compile_endfor(char_u *arg, cctx_T *cctx)
8019{
8020 garray_T *instr = &cctx->ctx_instr;
8021 scope_T *scope = cctx->ctx_scope;
8022 forscope_T *forscope;
8023 isn_T *isn;
8024
Bram Moolenaarfa984412021-03-25 22:15:28 +01008025 if (misplaced_cmdmod(cctx))
8026 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 if (scope == NULL || scope->se_type != FOR_SCOPE)
8029 {
8030 emsg(_(e_for));
8031 return NULL;
8032 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008033 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008035 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036
8037 // At end of ":for" scope jump back to the FOR instruction.
8038 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8039
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008040 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8042 isn->isn_arg.forloop.for_end = instr->ga_len;
8043
8044 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008045 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046
8047 // Below the ":for" scope drop the "expr" list from the stack.
8048 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8049 return NULL;
8050
8051 vim_free(scope);
8052
8053 return arg;
8054}
8055
8056/*
8057 * compile "while expr"
8058 *
8059 * Produces instructions:
8060 * top: EVAL expr Push result of "expr"
8061 * JUMP_IF_FALSE end jump if false
8062 * ... body ...
8063 * JUMP top Jump back to repeat
8064 * end:
8065 *
8066 */
8067 static char_u *
8068compile_while(char_u *arg, cctx_T *cctx)
8069{
8070 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 scope_T *scope;
8072
8073 scope = new_scope(cctx, WHILE_SCOPE);
8074 if (scope == NULL)
8075 return NULL;
8076
Bram Moolenaara91a7132021-03-25 21:12:15 +01008077 // "endwhile" jumps back here, one before when profiling or using cmdmods
8078 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079
8080 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008081 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008083 if (!ends_excmd2(arg, skipwhite(p)))
8084 {
8085 semsg(_(e_trailing_arg), p);
8086 return NULL;
8087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088
Bram Moolenaar13106602020-10-04 16:06:05 +02008089 if (bool_on_stack(cctx) == FAIL)
8090 return FAIL;
8091
Bram Moolenaara91a7132021-03-25 21:12:15 +01008092 // CMDMOD_REV must come before the jump
8093 generate_undo_cmdmods(cctx);
8094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008096 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 JUMP_IF_FALSE, cctx) == FAIL)
8098 return FAIL;
8099
8100 return p;
8101}
8102
8103/*
8104 * compile "endwhile"
8105 */
8106 static char_u *
8107compile_endwhile(char_u *arg, cctx_T *cctx)
8108{
8109 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008110 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111
Bram Moolenaarfa984412021-03-25 22:15:28 +01008112 if (misplaced_cmdmod(cctx))
8113 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8115 {
8116 emsg(_(e_while));
8117 return NULL;
8118 }
8119 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008120 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121
Bram Moolenaarf002a412021-01-24 13:34:18 +01008122#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008123 // count the endwhile before jumping
8124 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008125#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008128 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129
8130 // Fill in the "end" label in the WHILE statement so it can jump here.
8131 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008132 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8133 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134
8135 vim_free(scope);
8136
8137 return arg;
8138}
8139
8140/*
8141 * compile "continue"
8142 */
8143 static char_u *
8144compile_continue(char_u *arg, cctx_T *cctx)
8145{
8146 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008147 int try_scopes = 0;
8148 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149
8150 for (;;)
8151 {
8152 if (scope == NULL)
8153 {
8154 emsg(_(e_continue));
8155 return NULL;
8156 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008157 if (scope->se_type == FOR_SCOPE)
8158 {
8159 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008161 }
8162 if (scope->se_type == WHILE_SCOPE)
8163 {
8164 loop_label = scope->se_u.se_while.ws_top_label;
8165 break;
8166 }
8167 if (scope->se_type == TRY_SCOPE)
8168 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169 scope = scope->se_outer;
8170 }
8171
Bram Moolenaarc150c092021-02-13 15:02:46 +01008172 if (try_scopes > 0)
8173 // Inside one or more try/catch blocks we first need to jump to the
8174 // "finally" or "endtry" to cleanup.
8175 generate_TRYCONT(cctx, try_scopes, loop_label);
8176 else
8177 // Jump back to the FOR or WHILE instruction.
8178 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 return arg;
8181}
8182
8183/*
8184 * compile "break"
8185 */
8186 static char_u *
8187compile_break(char_u *arg, cctx_T *cctx)
8188{
8189 scope_T *scope = cctx->ctx_scope;
8190 endlabel_T **el;
8191
8192 for (;;)
8193 {
8194 if (scope == NULL)
8195 {
8196 emsg(_(e_break));
8197 return NULL;
8198 }
8199 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8200 break;
8201 scope = scope->se_outer;
8202 }
8203
8204 // Jump to the end of the FOR or WHILE loop.
8205 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008206 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008208 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8210 return FAIL;
8211
8212 return arg;
8213}
8214
8215/*
8216 * compile "{" start of block
8217 */
8218 static char_u *
8219compile_block(char_u *arg, cctx_T *cctx)
8220{
8221 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8222 return NULL;
8223 return skipwhite(arg + 1);
8224}
8225
8226/*
8227 * compile end of block: drop one scope
8228 */
8229 static void
8230compile_endblock(cctx_T *cctx)
8231{
8232 scope_T *scope = cctx->ctx_scope;
8233
8234 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008235 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 vim_free(scope);
8237}
8238
8239/*
8240 * compile "try"
8241 * Creates a new scope for the try-endtry, pointing to the first catch and
8242 * finally.
8243 * Creates another scope for the "try" block itself.
8244 * TRY instruction sets up exception handling at runtime.
8245 *
8246 * "try"
8247 * TRY -> catch1, -> finally push trystack entry
8248 * ... try block
8249 * "throw {exception}"
8250 * EVAL {exception}
8251 * THROW create exception
8252 * ... try block
8253 * " catch {expr}"
8254 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008255 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 * EVAL {expr}
8257 * MATCH
8258 * JUMP nomatch -> catch2
8259 * CATCH remove exception
8260 * ... catch block
8261 * " catch"
8262 * JUMP -> finally
8263 * catch2: CATCH remove exception
8264 * ... catch block
8265 * " finally"
8266 * finally:
8267 * ... finally block
8268 * " endtry"
8269 * ENDTRY pop trystack entry, may rethrow
8270 */
8271 static char_u *
8272compile_try(char_u *arg, cctx_T *cctx)
8273{
8274 garray_T *instr = &cctx->ctx_instr;
8275 scope_T *try_scope;
8276 scope_T *scope;
8277
Bram Moolenaarfa984412021-03-25 22:15:28 +01008278 if (misplaced_cmdmod(cctx))
8279 return NULL;
8280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008281 // scope that holds the jumps that go to catch/finally/endtry
8282 try_scope = new_scope(cctx, TRY_SCOPE);
8283 if (try_scope == NULL)
8284 return NULL;
8285
Bram Moolenaar69f70502021-01-01 16:10:46 +01008286 if (cctx->ctx_skip != SKIP_YES)
8287 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008288 isn_T *isn;
8289
8290 // "try_catch" is set when the first ":catch" is found or when no catch
8291 // is found and ":finally" is found.
8292 // "try_finally" is set when ":finally" is found
8293 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008294 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008295 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8296 return NULL;
8297 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8298 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008299 return NULL;
8300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301
8302 // scope for the try block itself
8303 scope = new_scope(cctx, BLOCK_SCOPE);
8304 if (scope == NULL)
8305 return NULL;
8306
8307 return arg;
8308}
8309
8310/*
8311 * compile "catch {expr}"
8312 */
8313 static char_u *
8314compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8315{
8316 scope_T *scope = cctx->ctx_scope;
8317 garray_T *instr = &cctx->ctx_instr;
8318 char_u *p;
8319 isn_T *isn;
8320
Bram Moolenaarfa984412021-03-25 22:15:28 +01008321 if (misplaced_cmdmod(cctx))
8322 return NULL;
8323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008324 // end block scope from :try or :catch
8325 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8326 compile_endblock(cctx);
8327 scope = cctx->ctx_scope;
8328
8329 // Error if not in a :try scope
8330 if (scope == NULL || scope->se_type != TRY_SCOPE)
8331 {
8332 emsg(_(e_catch));
8333 return NULL;
8334 }
8335
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008336 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008338 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008339 return NULL;
8340 }
8341
Bram Moolenaar69f70502021-01-01 16:10:46 +01008342 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008343 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008344#ifdef FEAT_PROFILE
8345 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008346 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008347 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008348 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008349 .isn_type == ISN_PROF_START)
8350 --instr->ga_len;
8351#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008352 // Jump from end of previous block to :finally or :endtry
8353 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8354 JUMP_ALWAYS, cctx) == FAIL)
8355 return NULL;
8356
8357 // End :try or :catch scope: set value in ISN_TRY instruction
8358 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008359 if (isn->isn_arg.try.try_ref->try_catch == 0)
8360 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008361 if (scope->se_u.se_try.ts_catch_label != 0)
8362 {
8363 // Previous catch without match jumps here
8364 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8365 isn->isn_arg.jump.jump_where = instr->ga_len;
8366 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008367#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008368 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008369 {
8370 // a "throw" that jumps here needs to be counted
8371 generate_instr(cctx, ISN_PROF_END);
8372 // the "catch" is also counted
8373 generate_instr(cctx, ISN_PROF_START);
8374 }
8375#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008376 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008377 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378 }
8379
8380 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008381 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008383 scope->se_u.se_try.ts_caught_all = TRUE;
8384 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385 }
8386 else
8387 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008388 char_u *end;
8389 char_u *pat;
8390 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008391 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008392 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 // Push v:exception, push {expr} and MATCH
8395 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8396
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008397 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008398 if (*end != *p)
8399 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008400 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008401 vim_free(tofree);
8402 return FAIL;
8403 }
8404 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008405 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008406 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008407 len = (int)(end - tofree);
8408 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008409 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008410 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008411 if (pat == NULL)
8412 return FAIL;
8413 if (generate_PUSHS(cctx, pat) == FAIL)
8414 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8417 return NULL;
8418
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008419 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8421 return NULL;
8422 }
8423
Bram Moolenaar69f70502021-01-01 16:10:46 +01008424 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 return NULL;
8426
8427 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8428 return NULL;
8429 return p;
8430}
8431
8432 static char_u *
8433compile_finally(char_u *arg, cctx_T *cctx)
8434{
8435 scope_T *scope = cctx->ctx_scope;
8436 garray_T *instr = &cctx->ctx_instr;
8437 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008438 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439
Bram Moolenaarfa984412021-03-25 22:15:28 +01008440 if (misplaced_cmdmod(cctx))
8441 return NULL;
8442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 // end block scope from :try or :catch
8444 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8445 compile_endblock(cctx);
8446 scope = cctx->ctx_scope;
8447
8448 // Error if not in a :try scope
8449 if (scope == NULL || scope->se_type != TRY_SCOPE)
8450 {
8451 emsg(_(e_finally));
8452 return NULL;
8453 }
8454
8455 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008456 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008457 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458 {
8459 emsg(_(e_finally_dup));
8460 return NULL;
8461 }
8462
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008463 this_instr = instr->ga_len;
8464#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008465 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008466 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008467 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008468 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008469 // jump to the profile start of the "finally"
8470 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008471
8472 // jump to the profile end above it
8473 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8474 .isn_type == ISN_PROF_END)
8475 --this_instr;
8476 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008477#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008479 // Fill in the "end" label in jumps at the end of the blocks.
8480 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8481 this_instr, cctx);
8482
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008483 // If there is no :catch then an exception jumps to :finally.
8484 if (isn->isn_arg.try.try_ref->try_catch == 0)
8485 isn->isn_arg.try.try_ref->try_catch = this_instr;
8486 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008487 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 {
8489 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008490 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008491 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008492 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008494 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8495 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008497 // TODO: set index in ts_finally_label jumps
8498
8499 return arg;
8500}
8501
8502 static char_u *
8503compile_endtry(char_u *arg, cctx_T *cctx)
8504{
8505 scope_T *scope = cctx->ctx_scope;
8506 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008507 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508
Bram Moolenaarfa984412021-03-25 22:15:28 +01008509 if (misplaced_cmdmod(cctx))
8510 return NULL;
8511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 // end block scope from :catch or :finally
8513 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8514 compile_endblock(cctx);
8515 scope = cctx->ctx_scope;
8516
8517 // Error if not in a :try scope
8518 if (scope == NULL || scope->se_type != TRY_SCOPE)
8519 {
8520 if (scope == NULL)
8521 emsg(_(e_no_endtry));
8522 else if (scope->se_type == WHILE_SCOPE)
8523 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008524 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 emsg(_(e_endfor));
8526 else
8527 emsg(_(e_endif));
8528 return NULL;
8529 }
8530
Bram Moolenaarc150c092021-02-13 15:02:46 +01008531 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008532 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008534 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8535 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008536 {
8537 emsg(_(e_missing_catch_or_finally));
8538 return NULL;
8539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008541#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008542 if (cctx->ctx_compile_type == CT_PROFILE
8543 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008544 .isn_type == ISN_PROF_START)
8545 // move the profile start after "endtry" so that it's not counted when
8546 // the exception is rethrown.
8547 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008548#endif
8549
Bram Moolenaar69f70502021-01-01 16:10:46 +01008550 // Fill in the "end" label in jumps at the end of the blocks, if not
8551 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008552 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8553 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554
Bram Moolenaar69f70502021-01-01 16:10:46 +01008555 if (scope->se_u.se_try.ts_catch_label != 0)
8556 {
8557 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008558 isn_T *isn = ((isn_T *)instr->ga_data)
8559 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008560 isn->isn_arg.jump.jump_where = instr->ga_len;
8561 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008562 }
8563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 compile_endblock(cctx);
8565
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008566 if (cctx->ctx_skip != SKIP_YES)
8567 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008568 // End :catch or :finally scope: set instruction index in ISN_TRY
8569 // instruction
8570 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008571 if (cctx->ctx_skip != SKIP_YES
8572 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8573 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008574#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008575 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008576 generate_instr(cctx, ISN_PROF_START);
8577#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 return arg;
8580}
8581
8582/*
8583 * compile "throw {expr}"
8584 */
8585 static char_u *
8586compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8587{
8588 char_u *p = skipwhite(arg);
8589
Bram Moolenaara5565e42020-05-09 15:44:01 +02008590 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008592 if (cctx->ctx_skip == SKIP_YES)
8593 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008594 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008595 return NULL;
8596 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8597 return NULL;
8598
8599 return p;
8600}
8601
Bram Moolenaarc3235272021-07-10 19:42:03 +02008602 static char_u *
8603compile_eval(char_u *arg, cctx_T *cctx)
8604{
8605 char_u *p = arg;
8606 int name_only;
8607 char_u *alias;
8608 long lnum = SOURCING_LNUM;
8609
8610 // find_ex_command() will consider a variable name an expression, assuming
8611 // that something follows on the next line. Check that something actually
8612 // follows, otherwise it's probably a misplaced command.
8613 get_name_len(&p, &alias, FALSE, FALSE);
8614 name_only = ends_excmd2(arg, skipwhite(p));
8615 vim_free(alias);
8616
8617 p = arg;
8618 if (compile_expr0(&p, cctx) == FAIL)
8619 return NULL;
8620
8621 if (name_only && lnum == SOURCING_LNUM)
8622 {
8623 semsg(_(e_expression_without_effect_str), arg);
8624 return NULL;
8625 }
8626
8627 // drop the result
8628 generate_instr_drop(cctx, ISN_DROP, 1);
8629
8630 return skipwhite(p);
8631}
8632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008633/*
8634 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008635 * compile "echomsg expr"
8636 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008637 * compile "execute expr"
8638 */
8639 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008640compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008641{
8642 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008643 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008644 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008645 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008646 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008647 garray_T *stack = &cctx->ctx_type_stack;
8648 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008649
8650 for (;;)
8651 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008652 if (ends_excmd2(prev, p))
8653 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008654 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008655 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008656 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008657
8658 if (cctx->ctx_skip != SKIP_YES)
8659 {
8660 // check for non-void type
8661 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8662 if (type->tt_type == VAR_VOID)
8663 {
8664 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8665 return NULL;
8666 }
8667 }
8668
Bram Moolenaarad39c092020-02-26 18:23:43 +01008669 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008670 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008671 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008672 }
8673
Bram Moolenaare4984292020-12-13 14:19:25 +01008674 if (count > 0)
8675 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008676 long save_lnum = cctx->ctx_lnum;
8677
8678 // Use the line number where the command started.
8679 cctx->ctx_lnum = start_ctx_lnum;
8680
Bram Moolenaare4984292020-12-13 14:19:25 +01008681 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8682 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8683 else if (cmdidx == CMD_execute)
8684 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8685 else if (cmdidx == CMD_echomsg)
8686 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8687 else
8688 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008689
8690 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 return p;
8693}
8694
8695/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008696 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008697 * instruction to compute it and return OK.
8698 * Otherwise return FAIL, the caller must deal with any range.
8699 */
8700 static int
8701compile_variable_range(exarg_T *eap, cctx_T *cctx)
8702{
8703 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8704 char_u *p = skipdigits(eap->cmd);
8705
8706 if (p == range_end)
8707 return FAIL;
8708 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8709}
8710
8711/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008712 * :put r
8713 * :put ={expr}
8714 */
8715 static char_u *
8716compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8717{
8718 char_u *line = arg;
8719 linenr_T lnum;
8720 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008721 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008722
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008723 eap->regname = *line;
8724
8725 if (eap->regname == '=')
8726 {
8727 char_u *p = line + 1;
8728
8729 if (compile_expr0(&p, cctx) == FAIL)
8730 return NULL;
8731 line = p;
8732 }
8733 else if (eap->regname != NUL)
8734 ++line;
8735
Bram Moolenaar08597872020-12-10 19:43:40 +01008736 if (compile_variable_range(eap, cctx) == OK)
8737 {
8738 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8739 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008740 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008741 {
8742 // Either no range or a number.
8743 // "errormsg" will not be set because the range is ADDR_LINES.
8744 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008745 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008746 return NULL;
8747 if (eap->addr_count == 0)
8748 lnum = -1;
8749 else
8750 lnum = eap->line2;
8751 if (above)
8752 --lnum;
8753 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008754
8755 generate_PUT(cctx, eap->regname, lnum);
8756 return line;
8757}
8758
8759/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008760 * A command that is not compiled, execute with legacy code.
8761 */
8762 static char_u *
8763compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8764{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008765 char_u *p;
8766 int has_expr = FALSE;
8767 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008768
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008769 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008770 goto theend;
8771
Bram Moolenaare729ce22021-06-06 21:38:09 +02008772 // If there was a prececing command modifier, drop it and include it in the
8773 // EXEC command.
8774 if (cctx->ctx_has_cmdmod)
8775 {
8776 garray_T *instr = &cctx->ctx_instr;
8777 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8778
8779 if (isn->isn_type == ISN_CMDMOD)
8780 {
8781 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8782 ->cmod_filter_regmatch.regprog);
8783 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8784 --instr->ga_len;
8785 cctx->ctx_has_cmdmod = FALSE;
8786 }
8787 }
8788
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008789 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008790 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008791 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008792 int usefilter = FALSE;
8793
8794 has_expr = argt & (EX_XFILE | EX_EXPAND);
8795
8796 // If the command can be followed by a bar, find the bar and truncate
8797 // it, so that the following command can be compiled.
8798 // The '|' is overwritten with a NUL, it is put back below.
8799 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8800 && *eap->arg == '!')
8801 // :w !filter or :r !filter or :r! filter
8802 usefilter = TRUE;
8803 if ((argt & EX_TRLBAR) && !usefilter)
8804 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008805 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008806 separate_nextcmd(eap);
8807 if (eap->nextcmd != NULL)
8808 nextcmd = eap->nextcmd;
8809 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008810 else if (eap->cmdidx == CMD_wincmd)
8811 {
8812 p = eap->arg;
8813 if (*p != NUL)
8814 ++p;
8815 if (*p == 'g' || *p == Ctrl_G)
8816 ++p;
8817 p = skipwhite(p);
8818 if (*p == '|')
8819 {
8820 *p = NUL;
8821 nextcmd = p + 1;
8822 }
8823 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008824 }
8825
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008826 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8827 {
8828 // expand filename in "syntax include [@group] filename"
8829 has_expr = TRUE;
8830 eap->arg = skipwhite(eap->arg + 7);
8831 if (*eap->arg == '@')
8832 eap->arg = skiptowhite(eap->arg);
8833 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008834
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008835 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8836 && STRLEN(eap->arg) > 4)
8837 {
8838 int delim = *eap->arg;
8839
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008840 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008841 if (*p == delim)
8842 {
8843 eap->arg = p + 1;
8844 has_expr = TRUE;
8845 }
8846 }
8847
Bram Moolenaarecac5912021-01-05 19:23:28 +01008848 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8849 {
8850 // TODO: should only expand when appropriate for the command
8851 eap->arg = skiptowhite(eap->arg);
8852 has_expr = TRUE;
8853 }
8854
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008855 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008856 {
8857 int count = 0;
8858 char_u *start = skipwhite(line);
8859
8860 // :cmd xxx`=expr1`yyy`=expr2`zzz
8861 // PUSHS ":cmd xxx"
8862 // eval expr1
8863 // PUSHS "yyy"
8864 // eval expr2
8865 // PUSHS "zzz"
8866 // EXECCONCAT 5
8867 for (;;)
8868 {
8869 if (p > start)
8870 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008871 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008872 ++count;
8873 }
8874 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008875 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008876 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008877 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008878 ++count;
8879 p = skipwhite(p);
8880 if (*p != '`')
8881 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008882 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008883 return NULL;
8884 }
8885 start = p + 1;
8886
8887 p = (char_u *)strstr((char *)start, "`=");
8888 if (p == NULL)
8889 {
8890 if (*skipwhite(start) != NUL)
8891 {
8892 generate_PUSHS(cctx, vim_strsave(start));
8893 ++count;
8894 }
8895 break;
8896 }
8897 }
8898 generate_EXECCONCAT(cctx, count);
8899 }
8900 else
8901 generate_EXEC(cctx, line);
8902
8903theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008904 if (*nextcmd != NUL)
8905 {
8906 // the parser expects a pointer to the bar, put it back
8907 --nextcmd;
8908 *nextcmd = '|';
8909 }
8910
8911 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008912}
8913
Bram Moolenaar20677332021-06-06 17:02:53 +02008914/*
8915 * A script command with heredoc, e.g.
8916 * ruby << EOF
8917 * command
8918 * EOF
8919 * Has been turned into one long line with NL characters by
8920 * get_function_body():
8921 * ruby << EOF<NL> command<NL>EOF
8922 */
8923 static char_u *
8924compile_script(char_u *line, cctx_T *cctx)
8925{
8926 if (cctx->ctx_skip != SKIP_YES)
8927 {
8928 isn_T *isn;
8929
8930 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8931 return NULL;
8932 isn->isn_arg.string = vim_strsave(line);
8933 }
8934 return (char_u *)"";
8935}
8936
Bram Moolenaar8238f082021-04-20 21:10:48 +02008937
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008938/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008939 * :s/pat/repl/
8940 */
8941 static char_u *
8942compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8943{
8944 char_u *cmd = eap->arg;
8945 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8946
8947 if (expr != NULL)
8948 {
8949 int delimiter = *cmd++;
8950
8951 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008952 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008953 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8954 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008955 garray_T save_ga = cctx->ctx_instr;
8956 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008957 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008958 int trailing_error;
8959 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008960 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008961 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008962
8963 cmd += 3;
8964 end = skip_substitute(cmd, delimiter);
8965
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008966 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008967 // labels are correct.
8968 cctx->ctx_instr.ga_len = 0;
8969 cctx->ctx_instr.ga_maxlen = 0;
8970 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008971 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008972 if (end[-1] == NUL)
8973 end[-1] = delimiter;
8974 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008975 trailing_error = *cmd != delimiter && *cmd != NUL;
8976
Bram Moolenaar169502f2021-04-21 12:19:35 +02008977 if (expr_res == FAIL || trailing_error
8978 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008979 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008980 if (trailing_error)
8981 semsg(_(e_trailing_arg), cmd);
8982 clear_instr_ga(&cctx->ctx_instr);
8983 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008984 return NULL;
8985 }
8986
Bram Moolenaar8238f082021-04-20 21:10:48 +02008987 // Move the generated instructions into the ISN_SUBSTITUTE
8988 // instructions, then restore the list of instructions before
8989 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008990 instr_count = cctx->ctx_instr.ga_len;
8991 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008992 instr[instr_count].isn_type = ISN_FINISH;
8993
8994 cctx->ctx_instr = save_ga;
8995 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8996 {
8997 int idx;
8998
8999 for (idx = 0; idx < instr_count; ++idx)
9000 delete_instr(instr + idx);
9001 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009002 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009003 }
9004 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9005 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009006
9007 // skip over flags
9008 if (*end == '&')
9009 ++end;
9010 while (ASCII_ISALPHA(*end) || *end == '#')
9011 ++end;
9012 return end;
9013 }
9014 }
9015
9016 return compile_exec(arg, eap, cctx);
9017}
9018
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009019 static char_u *
9020compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9021{
9022 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009023 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009024
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009025 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009026 {
9027 if (STRNCMP(arg, "END", 3) == 0)
9028 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009029 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009030 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009031 // First load the current variable value.
9032 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9033 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009034 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009035 }
9036
9037 // Gets the redirected text and put it on the stack, then store it
9038 // in the variable.
9039 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9040
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009041 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009042 generate_instr_drop(cctx, ISN_CONCAT, 1);
9043
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009044 if (lhs->lhs_has_index)
9045 {
9046 // Use the info in "lhs" to store the value at the index in the
9047 // list or dict.
9048 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9049 &t_string, cctx) == FAIL)
9050 return NULL;
9051 }
9052 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009053 return NULL;
9054
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009055 VIM_CLEAR(lhs->lhs_name);
9056 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009057 return arg + 3;
9058 }
9059 emsg(_(e_cannot_nest_redir));
9060 return NULL;
9061 }
9062
9063 if (arg[0] == '=' && arg[1] == '>')
9064 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009065 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009066
9067 // redirect to a variable is compiled
9068 arg += 2;
9069 if (*arg == '>')
9070 {
9071 ++arg;
9072 append = TRUE;
9073 }
9074 arg = skipwhite(arg);
9075
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009076 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009077 FALSE, FALSE, 1, cctx) == FAIL)
9078 return NULL;
9079 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009080 lhs->lhs_append = append;
9081 if (lhs->lhs_has_index)
9082 {
9083 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9084 if (lhs->lhs_whole == NULL)
9085 return NULL;
9086 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009087
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009088 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009089 }
9090
9091 // other redirects are handled like at script level
9092 return compile_exec(line, eap, cctx);
9093}
9094
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009095#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009096 static char_u *
9097compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9098{
9099 isn_T *isn;
9100 char_u *p;
9101
9102 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9103 if (isn == NULL)
9104 return NULL;
9105 isn->isn_arg.number = eap->cmdidx;
9106
9107 p = eap->arg;
9108 if (compile_expr0(&p, cctx) == FAIL)
9109 return NULL;
9110
9111 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9112 if (isn == NULL)
9113 return NULL;
9114 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9115 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9116 return NULL;
9117 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9118 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9119 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9120
9121 return p;
9122}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009123#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009124
Bram Moolenaar4c137212021-04-19 16:48:48 +02009125/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009126 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009127 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009128 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009129 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009130add_def_function(ufunc_T *ufunc)
9131{
9132 dfunc_T *dfunc;
9133
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009134 if (def_functions.ga_len == 0)
9135 {
9136 // The first position is not used, so that a zero uf_dfunc_idx means it
9137 // wasn't set.
9138 if (ga_grow(&def_functions, 1) == FAIL)
9139 return FAIL;
9140 ++def_functions.ga_len;
9141 }
9142
Bram Moolenaar09689a02020-05-09 22:50:08 +02009143 // Add the function to "def_functions".
9144 if (ga_grow(&def_functions, 1) == FAIL)
9145 return FAIL;
9146 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9147 CLEAR_POINTER(dfunc);
9148 dfunc->df_idx = def_functions.ga_len;
9149 ufunc->uf_dfunc_idx = dfunc->df_idx;
9150 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009151 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009152 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009153 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009154 ++def_functions.ga_len;
9155 return OK;
9156}
9157
9158/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009159 * After ex_function() has collected all the function lines: parse and compile
9160 * the lines into instructions.
9161 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009162 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9163 * the return statement (used for lambda). When uf_ret_type is already set
9164 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009165 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009166 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009167 * This can be used recursively through compile_lambda(), which may reallocate
9168 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009169 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009170 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009171 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009172compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009173 ufunc_T *ufunc,
9174 int check_return_type,
9175 compiletype_T compile_type,
9176 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009178 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009179 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009180 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009181 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009182 cctx_T cctx;
9183 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009184 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009185 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009186 int ret = FAIL;
9187 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009188 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009189 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009190 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009191 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009192#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009193 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009194#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009195 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009196
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009197 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009198 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009199 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009200 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009201 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9202 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009203 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009204
9205 switch (compile_type)
9206 {
9207 case CT_PROFILE:
9208#ifdef FEAT_PROFILE
9209 instr_dest = dfunc->df_instr_prof; break;
9210#endif
9211 case CT_NONE: instr_dest = dfunc->df_instr; break;
9212 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9213 }
9214 if (instr_dest != NULL)
9215 // Was compiled in this mode before: Free old instructions.
9216 delete_def_function_contents(dfunc, FALSE);
9217 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009218 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009219 else
9220 {
9221 if (add_def_function(ufunc) == FAIL)
9222 return FAIL;
9223 new_def_function = TRUE;
9224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225
Bram Moolenaar985116a2020-07-12 17:31:09 +02009226 ufunc->uf_def_status = UF_COMPILING;
9227
Bram Moolenaara80faa82020-04-12 19:37:17 +02009228 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009229
Bram Moolenaare99d4222021-06-13 14:01:26 +02009230 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009231 cctx.ctx_ufunc = ufunc;
9232 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009233 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009234 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9235 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9236 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9237 cctx.ctx_type_list = &ufunc->uf_type_list;
9238 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9239 instr = &cctx.ctx_instr;
9240
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009241 // Set the context to the function, it may be compiled when called from
9242 // another script. Set the script version to the most modern one.
9243 // The line number will be set in next_line_from_context().
9244 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009245 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9246
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009247 // Don't use the flag from ":legacy" here.
9248 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9249
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009250 // Make sure error messages are OK.
9251 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9252 if (do_estack_push)
9253 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009254 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009255
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009256 if (ufunc->uf_def_args.ga_len > 0)
9257 {
9258 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009259 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009260 int i;
9261 char_u *arg;
9262 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009263 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009264
9265 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009266 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009267 for (i = 0; i < count; ++i)
9268 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009269 garray_T *stack = &cctx.ctx_type_stack;
9270 type_T *val_type;
9271 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009272 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009273 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009274 int jump_instr_idx = instr->ga_len;
9275 isn_T *isn;
9276
9277 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9278 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9279 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009280
9281 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009282 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009283
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009284 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009285 r = compile_expr0(&arg, &cctx);
9286
Bram Moolenaar12bce952021-03-11 20:04:04 +01009287 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009288 goto erret;
9289
9290 // If no type specified use the type of the default value.
9291 // Otherwise check that the default value type matches the
9292 // specified type.
9293 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009294 where.wt_index = arg_idx + 1;
9295 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009296 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009297 {
9298 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009299 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009300 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009301 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009302 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009303 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009304
9305 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009306 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009307
9308 // set instruction index in JUMP_IF_ARG_SET to here
9309 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9310 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009311 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009312
9313 if (did_set_arg_type)
9314 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009315 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009316 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009317
9318 /*
9319 * Loop over all the lines of the function and generate instructions.
9320 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009321 for (;;)
9322 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009323 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009324 int starts_with_colon = FALSE;
9325 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009326 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009327
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009328 // Bail out on the first error to avoid a flood of errors and report
9329 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009330 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009331 goto erret;
9332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009333 if (line != NULL && *line == '|')
9334 // the line continues after a '|'
9335 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009336 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009337 && !(*line == '#' && (line == cctx.ctx_line_start
9338 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009339 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009340 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009341 goto erret;
9342 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009343 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9344 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009345 else
9346 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009347 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009348 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009349 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009350 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009351#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009352 if (cctx.ctx_skip != SKIP_YES)
9353 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009354#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009355 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009356 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009357 // Make a copy, splitting off nextcmd and removing trailing spaces
9358 // may change it.
9359 if (line != NULL)
9360 {
9361 line = vim_strsave(line);
9362 vim_free(line_to_free);
9363 line_to_free = line;
9364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009365 }
9366
Bram Moolenaara80faa82020-04-12 19:37:17 +02009367 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009368 ea.cmdlinep = &line;
9369 ea.cmd = skipwhite(line);
9370
Bram Moolenaarb2049902021-01-24 12:53:53 +01009371 if (*ea.cmd == '#')
9372 {
9373 // "#" starts a comment
9374 line = (char_u *)"";
9375 continue;
9376 }
9377
Bram Moolenaarf002a412021-01-24 13:34:18 +01009378#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009379 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9380 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009381 {
9382 may_generate_prof_end(&cctx, prof_lnum);
9383
9384 prof_lnum = cctx.ctx_lnum;
9385 generate_instr(&cctx, ISN_PROF_START);
9386 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009387#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009388 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9389 && cctx.ctx_skip != SKIP_YES)
9390 {
9391 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009392 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009393 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009394 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009395
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009396 // Some things can be recognized by the first character.
9397 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009399 case '}':
9400 {
9401 // "}" ends a block scope
9402 scopetype_T stype = cctx.ctx_scope == NULL
9403 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009404
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009405 if (stype == BLOCK_SCOPE)
9406 {
9407 compile_endblock(&cctx);
9408 line = ea.cmd;
9409 }
9410 else
9411 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009412 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009413 goto erret;
9414 }
9415 if (line != NULL)
9416 line = skipwhite(ea.cmd + 1);
9417 continue;
9418 }
9419
9420 case '{':
9421 // "{" starts a block scope
9422 // "{'a': 1}->func() is something else
9423 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9424 {
9425 line = compile_block(ea.cmd, &cctx);
9426 continue;
9427 }
9428 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009429 }
9430
9431 /*
9432 * COMMAND MODIFIERS
9433 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009434 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009435 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9436 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009437 {
9438 if (errormsg != NULL)
9439 goto erret;
9440 // empty line or comment
9441 line = (char_u *)"";
9442 continue;
9443 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009444 generate_cmdmods(&cctx, &local_cmdmod);
9445 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009447 // Check if there was a colon after the last command modifier or before
9448 // the current position.
9449 for (p = ea.cmd; p >= line; --p)
9450 {
9451 if (*p == ':')
9452 starts_with_colon = TRUE;
9453 if (p < ea.cmd && !VIM_ISWHITE(*p))
9454 break;
9455 }
9456
Bram Moolenaarce024c32021-06-26 13:00:49 +02009457 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009458 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009459 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009460 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009461 if (checkforcmd(&ea.cmd, "call", 3))
9462 {
9463 if (*ea.cmd == '(')
9464 // not for "call()"
9465 ea.cmd = p;
9466 else
9467 ea.cmd = skipwhite(ea.cmd);
9468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009469
Bram Moolenaarce024c32021-06-26 13:00:49 +02009470 if (!starts_with_colon)
9471 {
9472 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009473
Bram Moolenaarce024c32021-06-26 13:00:49 +02009474 // Check for assignment after command modifiers.
9475 assign = may_compile_assignment(&ea, &line, &cctx);
9476 if (assign == OK)
9477 goto nextline;
9478 if (assign == FAIL)
9479 goto erret;
9480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009481 }
9482
9483 /*
9484 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009485 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009486 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009487 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009488 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009489 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9490 && (starts_with_colon || !(*cmd == '\''
9491 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009492 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009493 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009494 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009495 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009496 if (!starts_with_colon)
9497 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009498 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009499 goto erret;
9500 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009501 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009502 if (ends_excmd2(line, ea.cmd))
9503 {
9504 // A range without a command: jump to the line.
9505 // TODO: compile to a more efficient command, possibly
9506 // calling parse_cmd_address().
9507 ea.cmdidx = CMD_SIZE;
9508 line = compile_exec(line, &ea, &cctx);
9509 goto nextline;
9510 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009511 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009512 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009513 p = find_ex_command(&ea, NULL, starts_with_colon
9514 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009515
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009516 if (p == NULL)
9517 {
9518 if (cctx.ctx_skip != SKIP_YES)
9519 emsg(_(e_ambiguous_use_of_user_defined_command));
9520 goto erret;
9521 }
9522
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009523 // When using ":legacy cmd" always use compile_exec().
9524 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009525 {
9526 char_u *start = ea.cmd;
9527
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009528 switch (ea.cmdidx)
9529 {
9530 case CMD_if:
9531 case CMD_elseif:
9532 case CMD_else:
9533 case CMD_endif:
9534 case CMD_for:
9535 case CMD_endfor:
9536 case CMD_continue:
9537 case CMD_break:
9538 case CMD_while:
9539 case CMD_endwhile:
9540 case CMD_try:
9541 case CMD_catch:
9542 case CMD_finally:
9543 case CMD_endtry:
9544 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9545 goto erret;
9546 default: break;
9547 }
9548
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009549 // ":legacy return expr" needs to be handled differently.
9550 if (checkforcmd(&start, "return", 4))
9551 ea.cmdidx = CMD_return;
9552 else
9553 ea.cmdidx = CMD_legacy;
9554 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009556 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9557 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009558 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009559 {
9560 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009561 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009562 }
9563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009564 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009565 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009566 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009567 // CMD_var cannot happen, compile_assignment() above would be
9568 // used. Most likely an assignment to a non-existing variable.
9569 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009570 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009572 }
9573
Bram Moolenaar3988f642020-08-27 22:43:03 +02009574 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009575 && ea.cmdidx != CMD_elseif
9576 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009577 && ea.cmdidx != CMD_endif
9578 && ea.cmdidx != CMD_endfor
9579 && ea.cmdidx != CMD_endwhile
9580 && ea.cmdidx != CMD_catch
9581 && ea.cmdidx != CMD_finally
9582 && ea.cmdidx != CMD_endtry)
9583 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009584 emsg(_(e_unreachable_code_after_return));
9585 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009586 }
9587
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009588 p = skipwhite(p);
9589 if (ea.cmdidx != CMD_SIZE
9590 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9591 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009592 if (ea.cmdidx >= 0)
9593 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009594 if ((ea.argt & EX_BANG) && *p == '!')
9595 {
9596 ea.forceit = TRUE;
9597 p = skipwhite(p + 1);
9598 }
9599 }
9600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601 switch (ea.cmdidx)
9602 {
9603 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009604 ea.arg = p;
9605 line = compile_nested_function(&ea, &cctx);
9606 break;
9607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009608 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009609 // TODO: should we allow this, e.g. to declare a global
9610 // function?
9611 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009612 goto erret;
9613
9614 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009615 line = compile_return(p, check_return_type,
9616 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009617 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 break;
9619
9620 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009621 emsg(_(e_cannot_use_let_in_vim9_script));
9622 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009623 case CMD_var:
9624 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009625 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009626 case CMD_increment:
9627 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009628 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009629 if (line == p)
9630 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009631 break;
9632
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009633 case CMD_unlet:
9634 case CMD_unlockvar:
9635 case CMD_lockvar:
9636 line = compile_unletlock(p, &ea, &cctx);
9637 break;
9638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009639 case CMD_import:
9640 line = compile_import(p, &cctx);
9641 break;
9642
9643 case CMD_if:
9644 line = compile_if(p, &cctx);
9645 break;
9646 case CMD_elseif:
9647 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009648 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009649 break;
9650 case CMD_else:
9651 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009652 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009653 break;
9654 case CMD_endif:
9655 line = compile_endif(p, &cctx);
9656 break;
9657
9658 case CMD_while:
9659 line = compile_while(p, &cctx);
9660 break;
9661 case CMD_endwhile:
9662 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009663 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009664 break;
9665
9666 case CMD_for:
9667 line = compile_for(p, &cctx);
9668 break;
9669 case CMD_endfor:
9670 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009671 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672 break;
9673 case CMD_continue:
9674 line = compile_continue(p, &cctx);
9675 break;
9676 case CMD_break:
9677 line = compile_break(p, &cctx);
9678 break;
9679
9680 case CMD_try:
9681 line = compile_try(p, &cctx);
9682 break;
9683 case CMD_catch:
9684 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009685 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009686 break;
9687 case CMD_finally:
9688 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009689 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 break;
9691 case CMD_endtry:
9692 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009693 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009694 break;
9695 case CMD_throw:
9696 line = compile_throw(p, &cctx);
9697 break;
9698
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009699 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009700 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009701 break;
9702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009705 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009706 case CMD_echomsg:
9707 case CMD_echoerr:
9708 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009709 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009710
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009711 case CMD_put:
9712 ea.cmd = cmd;
9713 line = compile_put(p, &ea, &cctx);
9714 break;
9715
Bram Moolenaar4c137212021-04-19 16:48:48 +02009716 case CMD_substitute:
9717 if (cctx.ctx_skip == SKIP_YES)
9718 line = (char_u *)"";
9719 else
9720 {
9721 ea.arg = p;
9722 line = compile_substitute(line, &ea, &cctx);
9723 }
9724 break;
9725
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009726 case CMD_redir:
9727 ea.arg = p;
9728 line = compile_redir(line, &ea, &cctx);
9729 break;
9730
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009731 case CMD_cexpr:
9732 case CMD_lexpr:
9733 case CMD_caddexpr:
9734 case CMD_laddexpr:
9735 case CMD_cgetexpr:
9736 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009737#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009738 ea.arg = p;
9739 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009740#else
9741 ex_ni(&ea);
9742 line = NULL;
9743#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009744 break;
9745
Bram Moolenaar3988f642020-08-27 22:43:03 +02009746 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009747
Bram Moolenaarae616492020-07-28 20:07:27 +02009748 case CMD_append:
9749 case CMD_change:
9750 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009751 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009752 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009753 case CMD_xit:
9754 not_in_vim9(&ea);
9755 goto erret;
9756
Bram Moolenaar002262f2020-07-08 17:47:57 +02009757 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009758 if (cctx.ctx_skip != SKIP_YES)
9759 {
9760 semsg(_(e_invalid_command_str), ea.cmd);
9761 goto erret;
9762 }
9763 // We don't check for a next command here.
9764 line = (char_u *)"";
9765 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009766
Bram Moolenaar20677332021-06-06 17:02:53 +02009767 case CMD_lua:
9768 case CMD_mzscheme:
9769 case CMD_perl:
9770 case CMD_py3:
9771 case CMD_python3:
9772 case CMD_python:
9773 case CMD_pythonx:
9774 case CMD_ruby:
9775 case CMD_tcl:
9776 ea.arg = p;
9777 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009778 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009779 else
9780 // heredoc lines have been concatenated with NL
9781 // characters in get_function_body()
9782 line = compile_script(line, &cctx);
9783 break;
9784
9785 default:
9786 // Not recognized, execute with do_cmdline_cmd().
9787 ea.arg = p;
9788 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789 break;
9790 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009791nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009792 if (line == NULL)
9793 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009794 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009796 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009797 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 if (cctx.ctx_type_stack.ga_len < 0)
9800 {
9801 iemsg("Type stack underflow");
9802 goto erret;
9803 }
9804 }
9805
9806 if (cctx.ctx_scope != NULL)
9807 {
9808 if (cctx.ctx_scope->se_type == IF_SCOPE)
9809 emsg(_(e_endif));
9810 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9811 emsg(_(e_endwhile));
9812 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9813 emsg(_(e_endfor));
9814 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009815 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009816 goto erret;
9817 }
9818
Bram Moolenaarefd88552020-06-18 20:50:10 +02009819 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009820 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009821 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9822 ufunc->uf_ret_type = &t_void;
9823 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009824 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009825 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009826 goto erret;
9827 }
9828
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009829 // Return void if there is no return at the end.
9830 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009831 }
9832
Bram Moolenaar599410c2021-04-10 14:03:43 +02009833 // When compiled with ":silent!" and there was an error don't consider the
9834 // function compiled.
9835 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009836 {
9837 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9838 + ufunc->uf_dfunc_idx;
9839 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009840 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009841#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009842 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009843 {
9844 dfunc->df_instr_prof = instr->ga_data;
9845 dfunc->df_instr_prof_count = instr->ga_len;
9846 }
9847 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009848#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009849 if (cctx.ctx_compile_type == CT_DEBUG)
9850 {
9851 dfunc->df_instr_debug = instr->ga_data;
9852 dfunc->df_instr_debug_count = instr->ga_len;
9853 }
9854 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009855 {
9856 dfunc->df_instr = instr->ga_data;
9857 dfunc->df_instr_count = instr->ga_len;
9858 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009859 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009860 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009861 if (cctx.ctx_outer_used)
9862 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009863 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865
9866 ret = OK;
9867
9868erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009869 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009870 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009871 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9872 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009873
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009874 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009875 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009876 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009877 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009878
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009879 // If using the last entry in the table and it was added above, we
9880 // might as well remove it.
9881 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009882 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009883 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009884 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009885 ufunc->uf_dfunc_idx = 0;
9886 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009887 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009888
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009889 while (cctx.ctx_scope != NULL)
9890 drop_scope(&cctx);
9891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009892 if (errormsg != NULL)
9893 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009894 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009895 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009896 }
9897
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009898 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9899 {
9900 if (ret == OK)
9901 {
9902 emsg(_(e_missing_redir_end));
9903 ret = FAIL;
9904 }
9905 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009906 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009907 }
9908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009909 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009910 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009911 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009912 if (do_estack_push)
9913 estack_pop();
9914
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009915 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009916 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009917 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009918 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009919 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009920}
9921
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009922 void
9923set_function_type(ufunc_T *ufunc)
9924{
9925 int varargs = ufunc->uf_va_name != NULL;
9926 int argcount = ufunc->uf_args.ga_len;
9927
9928 // Create a type for the function, with the return type and any
9929 // argument types.
9930 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9931 // The type is included in "tt_args".
9932 if (argcount > 0 || varargs)
9933 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009934 if (ufunc->uf_type_list.ga_itemsize == 0)
9935 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009936 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9937 argcount, &ufunc->uf_type_list);
9938 // Add argument types to the function type.
9939 if (func_type_add_arg_types(ufunc->uf_func_type,
9940 argcount + varargs,
9941 &ufunc->uf_type_list) == FAIL)
9942 return;
9943 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9944 ufunc->uf_func_type->tt_min_argcount =
9945 argcount - ufunc->uf_def_args.ga_len;
9946 if (ufunc->uf_arg_types == NULL)
9947 {
9948 int i;
9949
9950 // lambda does not have argument types.
9951 for (i = 0; i < argcount; ++i)
9952 ufunc->uf_func_type->tt_args[i] = &t_any;
9953 }
9954 else
9955 mch_memmove(ufunc->uf_func_type->tt_args,
9956 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9957 if (varargs)
9958 {
9959 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009960 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009961 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9962 }
9963 }
9964 else
9965 // No arguments, can use a predefined type.
9966 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9967 argcount, &ufunc->uf_type_list);
9968}
9969
9970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971/*
9972 * Delete an instruction, free what it contains.
9973 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009974 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009975delete_instr(isn_T *isn)
9976{
9977 switch (isn->isn_type)
9978 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009979 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009980 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009981 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009982 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009983 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009984 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009985 case ISN_LOADENV:
9986 case ISN_LOADG:
9987 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009988 case ISN_LOADT:
9989 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009990 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009991 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009992 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009993 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009994 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009995 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009996 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009998 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009999 case ISN_STOREW:
10000 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010001 vim_free(isn->isn_arg.string);
10002 break;
10003
Bram Moolenaar4c137212021-04-19 16:48:48 +020010004 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010005 {
10006 int idx;
10007 isn_T *list = isn->isn_arg.subs.subs_instr;
10008
10009 vim_free(isn->isn_arg.subs.subs_cmd);
10010 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10011 delete_instr(list + idx);
10012 vim_free(list);
10013 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010014 break;
10015
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010016 case ISN_INSTR:
10017 {
10018 int idx;
10019 isn_T *list = isn->isn_arg.instr;
10020
10021 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10022 delete_instr(list + idx);
10023 vim_free(list);
10024 }
10025 break;
10026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010027 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010028 case ISN_STORES:
10029 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010030 break;
10031
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010032 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010033 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010034 vim_free(isn->isn_arg.unlet.ul_name);
10035 break;
10036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010037 case ISN_STOREOPT:
10038 vim_free(isn->isn_arg.storeopt.so_name);
10039 break;
10040
10041 case ISN_PUSHBLOB: // push blob isn_arg.blob
10042 blob_unref(isn->isn_arg.blob);
10043 break;
10044
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010045 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010046#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010047 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010048#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010049 break;
10050
10051 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010052#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010053 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010054#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010055 break;
10056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010057 case ISN_UCALL:
10058 vim_free(isn->isn_arg.ufunc.cuf_name);
10059 break;
10060
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010061 case ISN_FUNCREF:
10062 {
10063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10064 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010065 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010066
Bram Moolenaar077a4232020-12-22 18:33:27 +010010067 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10068 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010069 }
10070 break;
10071
10072 case ISN_DCALL:
10073 {
10074 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10075 + isn->isn_arg.dfunc.cdf_idx;
10076
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010077 if (dfunc->df_ufunc != NULL
10078 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010079 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010080 }
10081 break;
10082
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010083 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010084 {
10085 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10086 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10087
10088 if (ufunc != NULL)
10089 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010090 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010091 func_ptr_unref(ufunc);
10092 }
10093
10094 vim_free(lambda);
10095 vim_free(isn->isn_arg.newfunc.nf_global);
10096 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010097 break;
10098
Bram Moolenaar5e654232020-09-16 15:22:00 +020010099 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010100 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010101 free_type(isn->isn_arg.type.ct_type);
10102 break;
10103
Bram Moolenaar02194d22020-10-24 23:08:38 +020010104 case ISN_CMDMOD:
10105 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10106 ->cmod_filter_regmatch.regprog);
10107 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10108 break;
10109
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010110 case ISN_LOADSCRIPT:
10111 case ISN_STORESCRIPT:
10112 vim_free(isn->isn_arg.script.scriptref);
10113 break;
10114
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010115 case ISN_TRY:
10116 vim_free(isn->isn_arg.try.try_ref);
10117 break;
10118
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010119 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010120 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010121 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10122 break;
10123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010124 case ISN_2BOOL:
10125 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010126 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010127 case ISN_ADDBLOB:
10128 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010129 case ISN_ANYINDEX:
10130 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010131 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010132 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010133 case ISN_BLOBINDEX:
10134 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010135 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010136 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010137 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010138 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010139 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010140 case ISN_COMPAREANY:
10141 case ISN_COMPAREBLOB:
10142 case ISN_COMPAREBOOL:
10143 case ISN_COMPAREDICT:
10144 case ISN_COMPAREFLOAT:
10145 case ISN_COMPAREFUNC:
10146 case ISN_COMPARELIST:
10147 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010148 case ISN_COMPARESPECIAL:
10149 case ISN_COMPARESTRING:
10150 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010151 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010152 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010153 case ISN_DROP:
10154 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010155 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010156 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010157 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010158 case ISN_EXECCONCAT:
10159 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010160 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010161 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010162 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010163 case ISN_GETITEM:
10164 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010165 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010166 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010167 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010168 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010169 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010170 case ISN_LOADBDICT:
10171 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010172 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010173 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010174 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010175 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010176 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010177 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010178 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010179 case ISN_NEGATENR:
10180 case ISN_NEWDICT:
10181 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010182 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010183 case ISN_OPFLOAT:
10184 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010185 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010186 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010187 case ISN_PROF_END:
10188 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010189 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010190 case ISN_PUSHF:
10191 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010192 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010193 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010194 case ISN_REDIREND:
10195 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010197 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010198 case ISN_SHUFFLE:
10199 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010200 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010201 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010202 case ISN_STORENR:
10203 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010204 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010205 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010206 case ISN_STOREV:
10207 case ISN_STRINDEX:
10208 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010210 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010211 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010212 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010213 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010214 // nothing allocated
10215 break;
10216 }
10217}
10218
10219/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010220 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010221 */
10222 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010223delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010224{
10225 int idx;
10226
10227 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010228 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010229
10230 if (dfunc->df_instr != NULL)
10231 {
10232 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10233 delete_instr(dfunc->df_instr + idx);
10234 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010235 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010236 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010237 if (dfunc->df_instr_debug != NULL)
10238 {
10239 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10240 delete_instr(dfunc->df_instr_debug + idx);
10241 VIM_CLEAR(dfunc->df_instr_debug);
10242 dfunc->df_instr_debug = NULL;
10243 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010244#ifdef FEAT_PROFILE
10245 if (dfunc->df_instr_prof != NULL)
10246 {
10247 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10248 delete_instr(dfunc->df_instr_prof + idx);
10249 VIM_CLEAR(dfunc->df_instr_prof);
10250 dfunc->df_instr_prof = NULL;
10251 }
10252#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010253
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010254 if (mark_deleted)
10255 dfunc->df_deleted = TRUE;
10256 if (dfunc->df_ufunc != NULL)
10257 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010258}
10259
10260/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010261 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010262 * function, unless another user function still uses it.
10263 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 */
10265 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010266unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010267{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010268 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010269 {
10270 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10271 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010273 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010274 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010275 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010276 ufunc->uf_dfunc_idx = 0;
10277 if (dfunc->df_ufunc == ufunc)
10278 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279 }
10280}
10281
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010282/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010283 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010284 */
10285 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010286link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010287{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010288 if (ufunc->uf_dfunc_idx > 0)
10289 {
10290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10291 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010292
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010293 ++dfunc->df_refcount;
10294 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010295}
10296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010297#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010298/*
10299 * Free all functions defined with ":def".
10300 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010301 void
10302free_def_functions(void)
10303{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010304 int idx;
10305
10306 for (idx = 0; idx < def_functions.ga_len; ++idx)
10307 {
10308 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10309
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010310 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010311 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010312 }
10313
10314 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010315}
10316#endif
10317
10318
10319#endif // FEAT_EVAL