blob: 8e8ae3f98ed477106ed031b25e3787aa96961528 [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 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002480 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002481 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/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002799 * Check that the last item of "ppconst" is a bool.
2800 */
2801 static int
2802check_ppconst_bool(ppconst_T *ppconst)
2803{
2804 if (ppconst->pp_used > 0)
2805 {
2806 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2807 where_T where;
2808
2809 where.wt_index = 0;
2810 where.wt_variable = FALSE;
2811 return check_typval_type(&t_bool, tv, where);
2812 }
2813 return OK;
2814}
2815
2816/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002817 * Clear ppconst constants. Used when failing.
2818 */
2819 static void
2820clear_ppconst(ppconst_T *ppconst)
2821{
2822 int i;
2823
2824 for (i = 0; i < ppconst->pp_used; ++i)
2825 clear_tv(&ppconst->pp_tv[i]);
2826 ppconst->pp_used = 0;
2827}
2828
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002829/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002830 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002831 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002832 */
2833 static int
2834compile_member(int is_slice, cctx_T *cctx)
2835{
2836 type_T **typep;
2837 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002838 vartype_T vartype;
2839 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002840
Bram Moolenaar261417b2021-05-06 21:04:55 +02002841 // We can index a list, dict and blob. If we don't know the type
2842 // we can use the index value type. If we still don't know use an "ANY"
2843 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002844 typep = ((type_T **)stack->ga_data) + stack->ga_len
2845 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002846 vartype = (*typep)->tt_type;
2847 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002848 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002849 if (*typep == &t_any && idxtype == &t_string)
2850 vartype = VAR_DICT;
2851 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002852 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002853 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002854 return FAIL;
2855 if (is_slice)
2856 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002857 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2858 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002859 FALSE, FALSE) == FAIL)
2860 return FAIL;
2861 }
2862 }
2863
Bram Moolenaar261417b2021-05-06 21:04:55 +02002864 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002865 {
2866 if (is_slice)
2867 {
2868 emsg(_(e_cannot_slice_dictionary));
2869 return FAIL;
2870 }
2871 if ((*typep)->tt_type == VAR_DICT)
2872 {
2873 *typep = (*typep)->tt_member;
2874 if (*typep == &t_unknown)
2875 // empty dict was used
2876 *typep = &t_any;
2877 }
2878 else
2879 {
2880 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2881 FALSE, FALSE) == FAIL)
2882 return FAIL;
2883 *typep = &t_any;
2884 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002885 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002886 return FAIL;
2887 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2888 return FAIL;
2889 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002890 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002891 {
2892 *typep = &t_string;
2893 if ((is_slice
2894 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2895 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2896 return FAIL;
2897 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002898 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002899 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002900 if (is_slice)
2901 {
2902 *typep = &t_blob;
2903 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2904 return FAIL;
2905 }
2906 else
2907 {
2908 *typep = &t_number;
2909 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2910 return FAIL;
2911 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002912 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002913 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002914 {
2915 if (is_slice)
2916 {
2917 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002918 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002919 2) == FAIL)
2920 return FAIL;
2921 }
2922 else
2923 {
2924 if ((*typep)->tt_type == VAR_LIST)
2925 {
2926 *typep = (*typep)->tt_member;
2927 if (*typep == &t_unknown)
2928 // empty list was used
2929 *typep = &t_any;
2930 }
2931 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002932 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2933 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 return FAIL;
2935 }
2936 }
2937 else
2938 {
2939 emsg(_(e_string_list_dict_or_blob_required));
2940 return FAIL;
2941 }
2942 return OK;
2943}
2944
2945/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002946 * Generate an instruction to load script-local variable "name", without the
2947 * leading "s:".
2948 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 */
2950 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002951compile_load_scriptvar(
2952 cctx_T *cctx,
2953 char_u *name, // variable NUL terminated
2954 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002955 char_u **end, // end of variable
2956 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002958 scriptitem_T *si;
2959 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 imported_T *import;
2961
Bram Moolenaare3d46852020-08-29 13:39:17 +02002962 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2963 return FAIL;
2964 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002965 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002966 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002968 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002969 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2970 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
2972 if (idx >= 0)
2973 {
2974 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2975
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002976 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 current_sctx.sc_sid, idx, sv->sv_type);
2978 return OK;
2979 }
2980
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002981 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 if (import != NULL)
2983 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002984 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002985 {
2986 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002987 char_u *exp_name;
2988 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002989 ufunc_T *ufunc;
2990 type_T *type;
2991
2992 // Used "import * as Name", need to lookup the member.
2993 if (*p != '.')
2994 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002995 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002996 return FAIL;
2997 }
2998 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002999 if (VIM_ISWHITE(*p))
3000 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003001 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003002 return FAIL;
3003 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003004
Bram Moolenaar1c991142020-07-04 13:15:31 +02003005 // isolate one name
3006 exp_name = p;
3007 while (eval_isnamec(*p))
3008 ++p;
3009 cc = *p;
3010 *p = NUL;
3011
Bram Moolenaaredba7072021-03-13 21:14:18 +01003012 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3013 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003014 *p = cc;
3015 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003016 *end = p;
3017
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003018 if (idx < 0)
3019 {
3020 if (*p == '(' && ufunc != NULL)
3021 {
3022 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3023 return OK;
3024 }
3025 return FAIL;
3026 }
3027
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003028 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3029 import->imp_sid,
3030 idx,
3031 type);
3032 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003033 else if (import->imp_funcname != NULL)
3034 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003035 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003036 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3037 import->imp_sid,
3038 import->imp_var_vals_idx,
3039 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 return OK;
3041 }
3042
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003043 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003044 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 return FAIL;
3046}
3047
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003048 static int
3049generate_funcref(cctx_T *cctx, char_u *name)
3050{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003051 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003052
3053 if (ufunc == NULL)
3054 return FAIL;
3055
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003056 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003057 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3058 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003059 == FAIL)
3060 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003061 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003062}
3063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064/*
3065 * Compile a variable name into a load instruction.
3066 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003067 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 * When "error" is FALSE do not give an error when not found.
3069 */
3070 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003071compile_load(
3072 char_u **arg,
3073 char_u *end_arg,
3074 cctx_T *cctx,
3075 int is_expr,
3076 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077{
3078 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003080 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003082 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
3084 if (*(*arg + 1) == ':')
3085 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003086 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003087 {
3088 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089
Bram Moolenaarfa596382021-04-07 21:58:16 +02003090 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003091 switch (**arg)
3092 {
3093 case 'g': isn_type = ISN_LOADGDICT; break;
3094 case 'w': isn_type = ISN_LOADWDICT; break;
3095 case 't': isn_type = ISN_LOADTDICT; break;
3096 case 'b': isn_type = ISN_LOADBDICT; break;
3097 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003098 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003099 goto theend;
3100 }
3101 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3102 goto theend;
3103 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 else
3106 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 isntype_T isn_type = ISN_DROP;
3108
Bram Moolenaarfa596382021-04-07 21:58:16 +02003109 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003110 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3111 if (name == NULL)
3112 return FAIL;
3113
3114 switch (**arg)
3115 {
3116 case 'v': res = generate_LOADV(cctx, name, error);
3117 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003118 case 's': if (is_expr && ASCII_ISUPPER(*name)
3119 && find_func(name, FALSE, cctx) != NULL)
3120 res = generate_funcref(cctx, name);
3121 else
3122 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003123 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003124 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003125 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003126 {
3127 if (is_expr && ASCII_ISUPPER(*name)
3128 && find_func(name, FALSE, cctx) != NULL)
3129 res = generate_funcref(cctx, name);
3130 else
3131 isn_type = ISN_LOADG;
3132 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003133 else
3134 {
3135 isn_type = ISN_LOADAUTO;
3136 vim_free(name);
3137 name = vim_strnsave(*arg, end - *arg);
3138 if (name == NULL)
3139 return FAIL;
3140 }
3141 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003142 case 'w': isn_type = ISN_LOADW; break;
3143 case 't': isn_type = ISN_LOADT; break;
3144 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003145 default: // cannot happen, just in case
3146 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003147 goto theend;
3148 }
3149 if (isn_type != ISN_DROP)
3150 {
3151 // Global, Buffer-local, Window-local and Tabpage-local
3152 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003153 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003154 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157 }
3158 else
3159 {
3160 size_t len = end - *arg;
3161 int idx;
3162 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003163 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164
3165 name = vim_strnsave(*arg, end - *arg);
3166 if (name == NULL)
3167 return FAIL;
3168
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003169 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3170 {
3171 script_autoload(name, FALSE);
3172 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3173 }
3174 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3175 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003177 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003178 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 }
3180 else
3181 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003182 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003183
Bram Moolenaar709664c2020-12-12 14:33:41 +01003184 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003186 type = lvar.lv_type;
3187 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003188 if (lvar.lv_from_outer != 0)
3189 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003190 else
3191 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 else
3194 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003195 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003196 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003197 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003198 || find_imported(name, 0, cctx) != NULL)
3199 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003200
Bram Moolenaar0f769812020-09-12 18:32:34 +02003201 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003202 // uppercase letter it can be a user defined function.
3203 // generate_funcref() will fail if the function can't be found.
3204 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003205 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 }
3207 }
3208 if (gen_load)
3209 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003210 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003211 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003212 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003213 cctx->ctx_outer_used = TRUE;
3214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 }
3216
3217 *arg = end;
3218
3219theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003220 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003221 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 vim_free(name);
3223 return res;
3224}
3225
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003226 static void
3227clear_instr_ga(garray_T *gap)
3228{
3229 int idx;
3230
3231 for (idx = 0; idx < gap->ga_len; ++idx)
3232 delete_instr(((isn_T *)gap->ga_data) + idx);
3233 ga_clear(gap);
3234}
3235
3236/*
3237 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3238 * Returns FAIL if compilation fails.
3239 */
3240 static int
3241compile_string(isn_T *isn, cctx_T *cctx)
3242{
3243 char_u *s = isn->isn_arg.string;
3244 garray_T save_ga = cctx->ctx_instr;
3245 int expr_res;
3246 int trailing_error;
3247 int instr_count;
3248 isn_T *instr = NULL;
3249
3250 // Temporarily reset the list of instructions so that the jump labels are
3251 // correct.
3252 cctx->ctx_instr.ga_len = 0;
3253 cctx->ctx_instr.ga_maxlen = 0;
3254 cctx->ctx_instr.ga_data = NULL;
3255 expr_res = compile_expr0(&s, cctx);
3256 s = skipwhite(s);
3257 trailing_error = *s != NUL;
3258
Bram Moolenaarff652882021-05-16 15:24:49 +02003259 if (expr_res == FAIL || trailing_error
3260 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003261 {
3262 if (trailing_error)
3263 semsg(_(e_trailing_arg), s);
3264 clear_instr_ga(&cctx->ctx_instr);
3265 cctx->ctx_instr = save_ga;
3266 return FAIL;
3267 }
3268
3269 // Move the generated instructions into the ISN_INSTR instruction, then
3270 // restore the list of instructions.
3271 instr_count = cctx->ctx_instr.ga_len;
3272 instr = cctx->ctx_instr.ga_data;
3273 instr[instr_count].isn_type = ISN_FINISH;
3274
3275 cctx->ctx_instr = save_ga;
3276 vim_free(isn->isn_arg.string);
3277 isn->isn_type = ISN_INSTR;
3278 isn->isn_arg.instr = instr;
3279 return OK;
3280}
3281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282/*
3283 * Compile the argument expressions.
3284 * "arg" points to just after the "(" and is advanced to after the ")"
3285 */
3286 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003287compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003289 char_u *p = *arg;
3290 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003291 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003292 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293
Bram Moolenaare6085c52020-04-12 20:19:16 +02003294 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003296 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3297 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003298 if (*p == ')')
3299 {
3300 *arg = p + 1;
3301 return OK;
3302 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003303 if (must_end)
3304 {
3305 semsg(_(e_missing_comma_before_argument_str), p);
3306 return FAIL;
3307 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003308
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003310 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 return FAIL;
3312 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003313
Bram Moolenaarff652882021-05-16 15:24:49 +02003314 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003315 && cctx->ctx_instr.ga_len == instr_count + 1)
3316 {
3317 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3318
3319 // {skip} argument of searchpair() can be compiled if not empty
3320 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3321 compile_string(isn, cctx);
3322 }
3323
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003324 if (*p != ',' && *skipwhite(p) == ',')
3325 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003326 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003327 p = skipwhite(p);
3328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003330 {
3331 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003332 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003333 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003334 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003335 else
3336 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003337 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003338 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003340failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003341 emsg(_(e_missing_close));
3342 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343}
3344
3345/*
3346 * Compile a function call: name(arg1, arg2)
3347 * "arg" points to "name", "arg + varlen" to the "(".
3348 * "argcount_init" is 1 for "value->method()"
3349 * Instructions:
3350 * EVAL arg1
3351 * EVAL arg2
3352 * BCALL / DCALL / UCALL
3353 */
3354 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003355compile_call(
3356 char_u **arg,
3357 size_t varlen,
3358 cctx_T *cctx,
3359 ppconst_T *ppconst,
3360 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361{
3362 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003363 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 int argcount = argcount_init;
3365 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003366 char_u fname_buf[FLEN_FIXED + 1];
3367 char_u *tofree = NULL;
3368 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003369 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003370 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003371 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003372 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373
Bram Moolenaara5565e42020-05-09 15:44:01 +02003374 // we can evaluate "has('name')" at compile time
3375 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3376 {
3377 char_u *s = skipwhite(*arg + varlen + 1);
3378 typval_T argvars[2];
3379
3380 argvars[0].v_type = VAR_UNKNOWN;
3381 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003383 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003384 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003385 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003386 if (*s == ')' && argvars[0].v_type == VAR_STRING
3387 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003388 {
3389 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3390
3391 *arg = s + 1;
3392 argvars[1].v_type = VAR_UNKNOWN;
3393 tv->v_type = VAR_NUMBER;
3394 tv->vval.v_number = 0;
3395 f_has(argvars, tv);
3396 clear_tv(&argvars[0]);
3397 ++ppconst->pp_used;
3398 return OK;
3399 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003400 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003401 }
3402
3403 if (generate_ppconst(cctx, ppconst) == FAIL)
3404 return FAIL;
3405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 if (varlen >= sizeof(namebuf))
3407 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003408 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 return FAIL;
3410 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003411 vim_strncpy(namebuf, *arg, varlen);
3412 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003414 // We handle the "skip" argument of searchpair() and searchpairpos()
3415 // differently.
3416 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3417 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3418 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3419 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003422 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003423 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424
Bram Moolenaar03290b82020-12-19 16:30:44 +01003425 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003426 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 {
3428 int idx;
3429
3430 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003431 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003433 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003434 if (STRCMP(name, "flatten") == 0)
3435 {
3436 emsg(_(e_cannot_use_flatten_in_vim9_script));
3437 goto theend;
3438 }
3439
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003440 if (STRCMP(name, "add") == 0 && argcount == 2)
3441 {
3442 garray_T *stack = &cctx->ctx_type_stack;
3443 type_T *type = ((type_T **)stack->ga_data)[
3444 stack->ga_len - 2];
3445
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003446 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003447 if (type->tt_type == VAR_LIST)
3448 {
3449 // inline "add(list, item)" so that the type can be checked
3450 res = generate_LISTAPPEND(cctx);
3451 idx = -1;
3452 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003453 else if (type->tt_type == VAR_BLOB)
3454 {
3455 // inline "add(blob, nr)" so that the type can be checked
3456 res = generate_BLOBAPPEND(cctx);
3457 idx = -1;
3458 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003459 }
3460
3461 if (idx >= 0)
3462 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3463 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003464 else
3465 semsg(_(e_unknownfunc), namebuf);
3466 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
3468
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003469 // An argument or local variable can be a function reference, this
3470 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003471 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003472 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003473 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003474 // If we can find the function by name generate the right call.
3475 // Skip global functions here, a local funcref takes precedence.
3476 ufunc = find_func(name, FALSE, cctx);
3477 if (ufunc != NULL && !func_is_global(ufunc))
3478 {
3479 res = generate_CALL(cctx, ufunc, argcount);
3480 goto theend;
3481 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483
3484 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003485 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003486 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003488 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003489 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003490 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003491 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003492 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003493
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003494 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003495 goto theend;
3496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497
Bram Moolenaar0f769812020-09-12 18:32:34 +02003498 // If we can find a global function by name generate the right call.
3499 if (ufunc != NULL)
3500 {
3501 res = generate_CALL(cctx, ufunc, argcount);
3502 goto theend;
3503 }
3504
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003505 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003506 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003507 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003508 res = generate_UCALL(cctx, name, argcount);
3509 else
3510 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003511
3512theend:
3513 vim_free(tofree);
3514 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515}
3516
3517// like NAMESPACE_CHAR but with 'a' and 'l'.
3518#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3519
3520/*
3521 * Find the end of a variable or function name. Unlike find_name_end() this
3522 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003523 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 * Return a pointer to just after the name. Equal to "arg" if there is no
3525 * valid name.
3526 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003527 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003528to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529{
3530 char_u *p;
3531
3532 // Quick check for valid starting character.
3533 if (!eval_isnamec1(*arg))
3534 return arg;
3535
3536 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3537 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3538 // and can be used in slice "[n:]".
3539 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003540 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3542 break;
3543 return p;
3544}
3545
3546/*
3547 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003548 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 */
3550 char_u *
3551to_name_const_end(char_u *arg)
3552{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003553 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 typval_T rettv;
3555
3556 if (p == arg && *arg == '[')
3557 {
3558
3559 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003560 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 p = arg;
3562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 return p;
3564}
3565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566/*
3567 * parse a list: [expr, expr]
3568 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003569 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 */
3571 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003572compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573{
3574 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003575 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003577 int is_const;
3578 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003580 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003582 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003583 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003584 semsg(_(e_list_end), *arg);
3585 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003586 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003587 if (*p == ',')
3588 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003589 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003590 return FAIL;
3591 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003592 if (*p == ']')
3593 {
3594 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003595 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003596 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003597 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003598 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003599 if (!is_const)
3600 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 ++count;
3602 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003603 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003605 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3606 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003607 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003608 return FAIL;
3609 }
3610 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003611 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 p = skipwhite(p);
3613 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003614 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003616 ppconst->pp_is_const = is_all_const;
3617 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618}
3619
3620/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003621 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003622 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003623 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 */
3625 static int
3626compile_lambda(char_u **arg, cctx_T *cctx)
3627{
Bram Moolenaare462f522020-12-27 14:43:30 +01003628 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 typval_T rettv;
3630 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003631 evalarg_T evalarg;
3632
3633 CLEAR_FIELD(evalarg);
3634 evalarg.eval_flags = EVAL_EVALUATE;
3635 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636
3637 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003638 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3639 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003640 {
3641 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003642 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003643 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003644
Bram Moolenaar65c44152020-12-24 15:14:01 +01003645 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003647 ++ufunc->uf_refcount;
3648 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649
Bram Moolenaara9931532021-06-12 15:58:16 +02003650 // Compile it here to get the return type. The return type is optional,
3651 // when it's missing use t_unknown. This is recognized in
3652 // compile_return().
3653 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3654 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003655 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaar648594e2021-07-11 17:55:01 +02003657#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003658 // When the outer function is compiled for profiling, the lambda may be
3659 // called without profiling. Compile it here in the right context.
3660 if (cctx->ctx_compile_type == CT_PROFILE)
3661 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003662#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003663
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003664 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3665 // points into it. Point to the original line to avoid a dangling pointer.
3666 if (evalarg.eval_tofree_cmdline != NULL)
3667 {
3668 size_t off = *arg - evalarg.eval_tofree_cmdline;
3669
3670 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3671 + off;
3672 }
3673
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003674 clear_evalarg(&evalarg, NULL);
3675
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003676 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003677 {
3678 // The return type will now be known.
3679 set_function_type(ufunc);
3680
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003681 // The function reference count will be 1. When the ISN_FUNCREF
3682 // instruction is deleted the reference count is decremented and the
3683 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003684 return generate_FUNCREF(cctx, ufunc);
3685 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003686
3687 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 return FAIL;
3689}
3690
3691/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003692 * Get a lambda and compile it. Uses Vim9 syntax.
3693 */
3694 int
3695get_lambda_tv_and_compile(
3696 char_u **arg,
3697 typval_T *rettv,
3698 int types_optional,
3699 evalarg_T *evalarg)
3700{
3701 int r;
3702 ufunc_T *ufunc;
3703 int save_sc_version = current_sctx.sc_version;
3704
3705 // Get the funcref in "rettv".
3706 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3707 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3708 current_sctx.sc_version = save_sc_version;
3709 if (r != OK)
3710 return r;
3711
3712 // "rettv" will now be a partial referencing the function.
3713 ufunc = rettv->vval.v_partial->pt_func;
3714
3715 // Compile it here to get the return type. The return type is optional,
3716 // when it's missing use t_unknown. This is recognized in
3717 // compile_return().
3718 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3719 ufunc->uf_ret_type = &t_unknown;
3720 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3721
3722 if (ufunc->uf_def_status == UF_COMPILED)
3723 {
3724 // The return type will now be known.
3725 set_function_type(ufunc);
3726 return OK;
3727 }
3728 clear_tv(rettv);
3729 return FAIL;
3730}
3731
3732/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003733 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003735 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 */
3737 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003738compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739{
3740 garray_T *instr = &cctx->ctx_instr;
3741 int count = 0;
3742 dict_T *d = dict_alloc();
3743 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003744 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003745 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003746 int is_const;
3747 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748
3749 if (d == NULL)
3750 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003751 if (generate_ppconst(cctx, ppconst) == FAIL)
3752 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003753 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003755 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756
Bram Moolenaar23c55272020-06-21 16:58:13 +02003757 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003758 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003759 *arg = NULL;
3760 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003761 }
3762
3763 if (**arg == '}')
3764 break;
3765
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003766 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003768 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003770 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003771 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003772 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003775 if (isn->isn_type == ISN_PUSHNR)
3776 {
3777 char buf[NUMBUFLEN];
3778
3779 // Convert to string at compile time.
3780 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3781 isn->isn_type = ISN_PUSHS;
3782 isn->isn_arg.string = vim_strsave((char_u *)buf);
3783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 if (isn->isn_type == ISN_PUSHS)
3785 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003786 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003787 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003788 *arg = skipwhite(*arg);
3789 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003790 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003791 emsg(_(e_missing_matching_bracket_after_dict_key));
3792 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003793 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003794 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003796 else
3797 {
3798 // {"name": value},
3799 // {'name': value},
3800 // {name: value} use "name" as a literal key
3801 key = get_literal_key(arg);
3802 if (key == NULL)
3803 return FAIL;
3804 if (generate_PUSHS(cctx, key) == FAIL)
3805 return FAIL;
3806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 // Check for duplicate keys, if using string keys.
3809 if (key != NULL)
3810 {
3811 item = dict_find(d, key, -1);
3812 if (item != NULL)
3813 {
3814 semsg(_(e_duplicate_key), key);
3815 goto failret;
3816 }
3817 item = dictitem_alloc(key);
3818 if (item != NULL)
3819 {
3820 item->di_tv.v_type = VAR_UNKNOWN;
3821 item->di_tv.v_lock = 0;
3822 if (dict_add(d, item) == FAIL)
3823 dictitem_free(item);
3824 }
3825 }
3826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 if (**arg != ':')
3828 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003829 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003830 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003831 else
3832 semsg(_(e_missing_dict_colon), *arg);
3833 return FAIL;
3834 }
3835 whitep = *arg + 1;
3836 if (!IS_WHITE_OR_NUL(*whitep))
3837 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003838 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 return FAIL;
3840 }
3841
Bram Moolenaar23c55272020-06-21 16:58:13 +02003842 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003843 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003844 *arg = NULL;
3845 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003846 }
3847
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003848 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003850 if (!is_const)
3851 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 ++count;
3853
Bram Moolenaar2c330432020-04-13 14:41:35 +02003854 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003855 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003856 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003857 *arg = NULL;
3858 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 if (**arg == '}')
3861 break;
3862 if (**arg != ',')
3863 {
3864 semsg(_(e_missing_dict_comma), *arg);
3865 goto failret;
3866 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003867 if (IS_WHITE_OR_NUL(*whitep))
3868 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003869 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003870 return FAIL;
3871 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003872 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003873 if (!IS_WHITE_OR_NUL(*whitep))
3874 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003875 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003876 return FAIL;
3877 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003878 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
3880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 *arg = *arg + 1;
3882
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003883 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003884 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003885 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003886 *arg += STRLEN(*arg);
3887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003889 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return generate_NEWDICT(cctx, count);
3891
3892failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003893 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003894 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003895 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003896 *arg = (char_u *)"";
3897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 dict_unref(d);
3899 return FAIL;
3900}
3901
3902/*
3903 * Compile "&option".
3904 */
3905 static int
3906compile_get_option(char_u **arg, cctx_T *cctx)
3907{
3908 typval_T rettv;
3909 char_u *start = *arg;
3910 int ret;
3911
3912 // parse the option and get the current value to get the type.
3913 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003914 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 if (ret == OK)
3916 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003917 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003918 char_u *name = vim_strnsave(start, *arg - start);
3919 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3920 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921
3922 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3923 vim_free(name);
3924 }
3925 clear_tv(&rettv);
3926
3927 return ret;
3928}
3929
3930/*
3931 * Compile "$VAR".
3932 */
3933 static int
3934compile_get_env(char_u **arg, cctx_T *cctx)
3935{
3936 char_u *start = *arg;
3937 int len;
3938 int ret;
3939 char_u *name;
3940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 ++*arg;
3942 len = get_env_len(arg);
3943 if (len == 0)
3944 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003945 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 return FAIL;
3947 }
3948
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003949 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 name = vim_strnsave(start, len + 1);
3951 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3952 vim_free(name);
3953 return ret;
3954}
3955
3956/*
3957 * Compile "@r".
3958 */
3959 static int
3960compile_get_register(char_u **arg, cctx_T *cctx)
3961{
3962 int ret;
3963
3964 ++*arg;
3965 if (**arg == NUL)
3966 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003967 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return FAIL;
3969 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003970 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 {
3972 emsg_invreg(**arg);
3973 return FAIL;
3974 }
3975 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3976 ++*arg;
3977 return ret;
3978}
3979
3980/*
3981 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003982 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 */
3984 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003985apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003987 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988
3989 // this works from end to start
3990 while (p > start)
3991 {
3992 --p;
3993 if (*p == '-' || *p == '+')
3994 {
3995 // only '-' has an effect, for '+' we only check the type
3996#ifdef FEAT_FLOAT
3997 if (rettv->v_type == VAR_FLOAT)
3998 {
3999 if (*p == '-')
4000 rettv->vval.v_float = -rettv->vval.v_float;
4001 }
4002 else
4003#endif
4004 {
4005 varnumber_T val;
4006 int error = FALSE;
4007
4008 // tv_get_number_chk() accepts a string, but we don't want that
4009 // here
4010 if (check_not_string(rettv) == FAIL)
4011 return FAIL;
4012 val = tv_get_number_chk(rettv, &error);
4013 clear_tv(rettv);
4014 if (error)
4015 return FAIL;
4016 if (*p == '-')
4017 val = -val;
4018 rettv->v_type = VAR_NUMBER;
4019 rettv->vval.v_number = val;
4020 }
4021 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004022 else if (numeric_only)
4023 {
4024 ++p;
4025 break;
4026 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004027 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 {
4029 int v = tv2bool(rettv);
4030
4031 // '!' is permissive in the type.
4032 clear_tv(rettv);
4033 rettv->v_type = VAR_BOOL;
4034 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4035 }
4036 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004037 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 return OK;
4039}
4040
4041/*
4042 * Recognize v: variables that are constants and set "rettv".
4043 */
4044 static void
4045get_vim_constant(char_u **arg, typval_T *rettv)
4046{
4047 if (STRNCMP(*arg, "v:true", 6) == 0)
4048 {
4049 rettv->v_type = VAR_BOOL;
4050 rettv->vval.v_number = VVAL_TRUE;
4051 *arg += 6;
4052 }
4053 else if (STRNCMP(*arg, "v:false", 7) == 0)
4054 {
4055 rettv->v_type = VAR_BOOL;
4056 rettv->vval.v_number = VVAL_FALSE;
4057 *arg += 7;
4058 }
4059 else if (STRNCMP(*arg, "v:null", 6) == 0)
4060 {
4061 rettv->v_type = VAR_SPECIAL;
4062 rettv->vval.v_number = VVAL_NULL;
4063 *arg += 6;
4064 }
4065 else if (STRNCMP(*arg, "v:none", 6) == 0)
4066 {
4067 rettv->v_type = VAR_SPECIAL;
4068 rettv->vval.v_number = VVAL_NONE;
4069 *arg += 6;
4070 }
4071}
4072
Bram Moolenaar657137c2021-01-09 15:45:23 +01004073 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004074get_compare_type(char_u *p, int *len, int *type_is)
4075{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004076 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004077 int i;
4078
4079 switch (p[0])
4080 {
4081 case '=': if (p[1] == '=')
4082 type = EXPR_EQUAL;
4083 else if (p[1] == '~')
4084 type = EXPR_MATCH;
4085 break;
4086 case '!': if (p[1] == '=')
4087 type = EXPR_NEQUAL;
4088 else if (p[1] == '~')
4089 type = EXPR_NOMATCH;
4090 break;
4091 case '>': if (p[1] != '=')
4092 {
4093 type = EXPR_GREATER;
4094 *len = 1;
4095 }
4096 else
4097 type = EXPR_GEQUAL;
4098 break;
4099 case '<': if (p[1] != '=')
4100 {
4101 type = EXPR_SMALLER;
4102 *len = 1;
4103 }
4104 else
4105 type = EXPR_SEQUAL;
4106 break;
4107 case 'i': if (p[1] == 's')
4108 {
4109 // "is" and "isnot"; but not a prefix of a name
4110 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4111 *len = 5;
4112 i = p[*len];
4113 if (!isalnum(i) && i != '_')
4114 {
4115 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4116 *type_is = TRUE;
4117 }
4118 }
4119 break;
4120 }
4121 return type;
4122}
4123
4124/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004125 * Skip over an expression, ignoring most errors.
4126 */
4127 static void
4128skip_expr_cctx(char_u **arg, cctx_T *cctx)
4129{
4130 evalarg_T evalarg;
4131
4132 CLEAR_FIELD(evalarg);
4133 evalarg.eval_cctx = cctx;
4134 skip_expr(arg, &evalarg);
4135}
4136
4137/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004139 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 */
4141 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004142compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004144 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145
4146 // this works from end to start
4147 while (p > start)
4148 {
4149 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004150 while (VIM_ISWHITE(*p))
4151 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 if (*p == '-' || *p == '+')
4153 {
4154 int negate = *p == '-';
4155 isn_T *isn;
4156
4157 // TODO: check type
4158 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4159 {
4160 --p;
4161 if (*p == '-')
4162 negate = !negate;
4163 }
4164 // only '-' has an effect, for '+' we only check the type
4165 if (negate)
4166 isn = generate_instr(cctx, ISN_NEGATENR);
4167 else
4168 isn = generate_instr(cctx, ISN_CHECKNR);
4169 if (isn == NULL)
4170 return FAIL;
4171 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004172 else if (numeric_only)
4173 {
4174 ++p;
4175 break;
4176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 else
4178 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004179 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004181 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004183 if (p[-1] == '!')
4184 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004187 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 return FAIL;
4189 }
4190 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004191 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 return OK;
4193}
4194
4195/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004196 * Compile "(expression)": recursive!
4197 * Return FAIL/OK.
4198 */
4199 static int
4200compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4201{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004202 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004203 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004204
Bram Moolenaar24156692021-01-14 20:35:49 +01004205 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4206 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004207 if (ppconst->pp_used <= PPSIZE - 10)
4208 {
4209 ret = compile_expr1(arg, cctx, ppconst);
4210 }
4211 else
4212 {
4213 // Not enough space in ppconst, flush constants.
4214 if (generate_ppconst(cctx, ppconst) == FAIL)
4215 return FAIL;
4216 ret = compile_expr0(arg, cctx);
4217 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004218 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4219 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004220 if (**arg == ')')
4221 ++*arg;
4222 else if (ret == OK)
4223 {
4224 emsg(_(e_missing_close));
4225 ret = FAIL;
4226 }
4227 return ret;
4228}
4229
4230/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004232 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 */
4234 static int
4235compile_subscript(
4236 char_u **arg,
4237 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004238 char_u *start_leader,
4239 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004240 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004242 char_u *name_start = *end_leader;
4243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 for (;;)
4245 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004246 char_u *p = skipwhite(*arg);
4247
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004248 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004249 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004250 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004251
4252 // If a following line starts with "->{" or "->X" advance to that
4253 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004254 // Also if a following line starts with ".x".
4255 if (next != NULL &&
4256 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004257 && (next[2] == '{'
4258 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004259 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004260 {
4261 next = next_line_from_context(cctx, TRUE);
4262 if (next == NULL)
4263 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004264 *arg = next;
4265 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004266 }
4267 }
4268
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004269 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004270 // not a function call.
4271 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004273 garray_T *stack = &cctx->ctx_type_stack;
4274 type_T *type;
4275 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004277 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004278 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004279 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004282 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4283
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004284 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004285 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004287 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 return FAIL;
4289 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004290 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004292 char_u *pstart = p;
4293
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004294 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004296 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 // something->method()
4299 // Apply the '!', '-' and '+' first:
4300 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004301 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004304 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004305 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004306 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004307 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004308 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004309 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004310 garray_T *stack = &cctx->ctx_type_stack;
4311 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004312 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004313 int expr_isn_start = cctx->ctx_instr.ga_len;
4314 int expr_isn_end;
4315 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004316
4317 // Funcref call: list->(Refs[2])(arg)
4318 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004319 //
4320 // Fist compile the function expression.
4321 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004322 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004323
4324 // Remember the next instruction index, where the instructions
4325 // for arguments are being written.
4326 expr_isn_end = cctx->ctx_instr.ga_len;
4327
4328 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004329 if (**arg != '(')
4330 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004331 if (*skipwhite(*arg) == '(')
4332 emsg(_(e_nowhitespace));
4333 else
4334 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004335 return FAIL;
4336 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004337 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004338 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004339 return FAIL;
4340
Bram Moolenaar2927c072021-04-05 19:41:21 +02004341 // Move the instructions for the arguments to before the
4342 // instructions of the expression and move the type of the
4343 // expression after the argument types. This is what ISN_PCALL
4344 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004345 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004346 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4347 if (arg_isn_count > 0)
4348 {
4349 int expr_isn_count = expr_isn_end - expr_isn_start;
4350 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4351
4352 if (isn == NULL)
4353 return FAIL;
4354 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4355 + expr_isn_start,
4356 sizeof(isn_T) * expr_isn_count);
4357 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4358 + expr_isn_start,
4359 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4360 sizeof(isn_T) * arg_isn_count);
4361 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4362 + expr_isn_start + arg_isn_count,
4363 isn, sizeof(isn_T) * expr_isn_count);
4364 vim_free(isn);
4365
4366 type = ((type_T **)stack->ga_data)[type_idx_start];
4367 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4368 ((type_T **)stack->ga_data) + type_idx_start + 1,
4369 sizeof(type_T *)
4370 * (stack->ga_len - type_idx_start - 1));
4371 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4372 }
4373
Bram Moolenaar7e368202020-12-25 21:56:57 +01004374 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004375 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 return FAIL;
4377 }
4378 else
4379 {
4380 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004381 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004382 if (!eval_isnamec1(*p))
4383 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004384 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004385 return FAIL;
4386 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004387 if (ASCII_ISALPHA(*p) && p[1] == ':')
4388 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004389 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 ;
4391 if (*p != '(')
4392 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004393 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 return FAIL;
4395 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 return FAIL;
4398 }
4399 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004400 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004402 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004403
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004404 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004405 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004406 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004407 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004408 // TODO: more arguments
4409 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004410 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004411 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004412 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004413
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004414 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004415 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004416 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004417 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004418 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004419 // missing first index is equal to zero
4420 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004421 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004422 else
4423 {
4424 if (compile_expr0(arg, cctx) == FAIL)
4425 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004426 if (**arg == ':')
4427 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004428 semsg(_(e_white_space_required_before_and_after_str_at_str),
4429 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004430 return FAIL;
4431 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004432 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004433 return FAIL;
4434 *arg = skipwhite(*arg);
4435 }
4436 if (**arg == ':')
4437 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004438 is_slice = TRUE;
4439 ++*arg;
4440 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4441 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004442 semsg(_(e_white_space_required_before_and_after_str_at_str),
4443 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004444 return FAIL;
4445 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004446 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004447 return FAIL;
4448 if (**arg == ']')
4449 // missing second index is equal to end of string
4450 generate_PUSHNR(cctx, -1);
4451 else
4452 {
4453 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004454 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004455 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004456 return FAIL;
4457 *arg = skipwhite(*arg);
4458 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460
4461 if (**arg != ']')
4462 {
4463 emsg(_(e_missbrac));
4464 return FAIL;
4465 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004466 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaare42939a2021-04-05 17:11:17 +02004468 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004469 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004471 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004473 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004474 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004475 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004476 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004477
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004478 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004479 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004480 {
4481 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004482 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004483 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004484 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004485 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 while (eval_isnamec(*p))
4487 MB_PTR_ADV(p);
4488 if (p == *arg)
4489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004490 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 return FAIL;
4492 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004493 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495 *arg = p;
4496 }
4497 else
4498 break;
4499 }
4500
4501 // TODO - see handle_subscript():
4502 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4503 // Don't do this when "Func" is already a partial that was bound
4504 // explicitly (pt_auto is FALSE).
4505
4506 return OK;
4507}
4508
4509/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004510 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4511 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004513 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004514 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004515 *
4516 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 */
4518
4519/*
4520 * number number constant
4521 * 0zFFFFFFFF Blob constant
4522 * "string" string constant
4523 * 'string' literal string constant
4524 * &option-name option value
4525 * @r register contents
4526 * identifier variable value
4527 * function() function call
4528 * $VAR environment variable
4529 * (expression) nested expression
4530 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004531 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 *
4533 * Also handle:
4534 * ! in front logical NOT
4535 * - in front unary minus
4536 * + in front unary plus (ignored)
4537 * trailing (arg) funcref/partial call
4538 * trailing [] subscript in String or List
4539 * trailing .name entry in Dictionary
4540 * trailing ->name() method call
4541 */
4542 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004543compile_expr7(
4544 char_u **arg,
4545 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004546 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 char_u *start_leader, *end_leader;
4549 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004550 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004551 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004553 ppconst->pp_is_const = FALSE;
4554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 /*
4556 * Skip '!', '-' and '+' characters. They are handled later.
4557 */
4558 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004559 if (eval_leader(arg, TRUE) == FAIL)
4560 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 end_leader = *arg;
4562
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004563 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 switch (**arg)
4565 {
4566 /*
4567 * Number constant.
4568 */
4569 case '0': // also for blob starting with 0z
4570 case '1':
4571 case '2':
4572 case '3':
4573 case '4':
4574 case '5':
4575 case '6':
4576 case '7':
4577 case '8':
4578 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004579 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004581 // Apply "-" and "+" just before the number now, right to
4582 // left. Matters especially when "->" follows. Stops at
4583 // '!'.
4584 if (apply_leader(rettv, TRUE,
4585 start_leader, &end_leader) == FAIL)
4586 {
4587 clear_tv(rettv);
4588 return FAIL;
4589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 break;
4591
4592 /*
4593 * String constant: "string".
4594 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004595 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 return FAIL;
4597 break;
4598
4599 /*
4600 * Literal string constant: 'str''ing'.
4601 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004602 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 return FAIL;
4604 break;
4605
4606 /*
4607 * Constant Vim variable.
4608 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004609 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 ret = NOTDONE;
4611 break;
4612
4613 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 * "true" constant
4615 */
4616 case 't': if (STRNCMP(*arg, "true", 4) == 0
4617 && !eval_isnamec((*arg)[4]))
4618 {
4619 *arg += 4;
4620 rettv->v_type = VAR_BOOL;
4621 rettv->vval.v_number = VVAL_TRUE;
4622 }
4623 else
4624 ret = NOTDONE;
4625 break;
4626
4627 /*
4628 * "false" constant
4629 */
4630 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4631 && !eval_isnamec((*arg)[5]))
4632 {
4633 *arg += 5;
4634 rettv->v_type = VAR_BOOL;
4635 rettv->vval.v_number = VVAL_FALSE;
4636 }
4637 else
4638 ret = NOTDONE;
4639 break;
4640
4641 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004642 * "null" constant
4643 */
4644 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004645 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004646 {
4647 *arg += 4;
4648 rettv->v_type = VAR_SPECIAL;
4649 rettv->vval.v_number = VVAL_NULL;
4650 }
4651 else
4652 ret = NOTDONE;
4653 break;
4654
4655 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 * List: [expr, expr]
4657 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004658 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4659 return FAIL;
4660 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 break;
4662
4663 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 * Dictionary: {'key': val, 'key': val}
4665 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004666 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4667 return FAIL;
4668 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 break;
4670
4671 /*
4672 * Option value: &name
4673 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004674 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4675 return FAIL;
4676 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 break;
4678
4679 /*
4680 * Environment variable: $VAR.
4681 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004682 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4683 return FAIL;
4684 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 break;
4686
4687 /*
4688 * Register contents: @r.
4689 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004690 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4691 return FAIL;
4692 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 break;
4694 /*
4695 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004696 * lambda: (arg, arg) => expr
4697 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004699 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4700 ret = compile_lambda(arg, cctx);
4701 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004702 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 break;
4704
4705 default: ret = NOTDONE;
4706 break;
4707 }
4708 if (ret == FAIL)
4709 return FAIL;
4710
Bram Moolenaar1c747212020-05-09 18:28:34 +02004711 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004713 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 clear_tv(rettv);
4715 else
4716 // A constant expression can possibly be handled compile time,
4717 // return the value instead of generating code.
4718 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 }
4720 else if (ret == NOTDONE)
4721 {
4722 char_u *p;
4723 int r;
4724
4725 if (!eval_isnamec1(**arg))
4726 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004727 if (!vim9_bad_comment(*arg))
4728 {
4729 if (ends_excmd(*skipwhite(*arg)))
4730 semsg(_(e_empty_expression_str), *arg);
4731 else
4732 semsg(_(e_name_expected_str), *arg);
4733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 return FAIL;
4735 }
4736
4737 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004738 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004739 if (p - *arg == (size_t)1 && **arg == '_')
4740 {
4741 emsg(_(e_cannot_use_underscore_here));
4742 return FAIL;
4743 }
4744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 {
4747 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 {
4751 if (generate_ppconst(cctx, ppconst) == FAIL)
4752 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004753 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 if (r == FAIL)
4756 return FAIL;
4757 }
4758
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004759 // Handle following "[]", ".member", etc.
4760 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004761 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004762 ppconst) == FAIL)
4763 return FAIL;
4764 if (ppconst->pp_used > 0)
4765 {
4766 // apply the '!', '-' and '+' before the constant
4767 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004768 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004769 return FAIL;
4770 return OK;
4771 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004772 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004774 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775}
4776
4777/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004778 * Give the "white on both sides" error, taking the operator from "p[len]".
4779 */
4780 void
4781error_white_both(char_u *op, int len)
4782{
4783 char_u buf[10];
4784
4785 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004786 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004787}
4788
4789/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004790 * <type>expr7: runtime type check / conversion
4791 */
4792 static int
4793compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4794{
4795 type_T *want_type = NULL;
4796
4797 // Recognize <type>
4798 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4799 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004800 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004801 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4802 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004803 return FAIL;
4804
4805 if (**arg != '>')
4806 {
4807 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004808 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004809 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004810 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004811 return FAIL;
4812 }
4813 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004814 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004815 return FAIL;
4816 }
4817
4818 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4819 return FAIL;
4820
4821 if (want_type != NULL)
4822 {
4823 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004824 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004825 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004826
Bram Moolenaard1103582020-08-14 22:44:25 +02004827 generate_ppconst(cctx, ppconst);
4828 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004829 where.wt_index = 0;
4830 where.wt_variable = FALSE;
4831 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004832 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004833 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004834 return FAIL;
4835 }
4836 }
4837
4838 return OK;
4839}
4840
4841/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 * * number multiplication
4843 * / number division
4844 * % number modulo
4845 */
4846 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004847compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848{
4849 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004850 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004851 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004853 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004854 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 return FAIL;
4856
4857 /*
4858 * Repeat computing, until no "*", "/" or "%" is following.
4859 */
4860 for (;;)
4861 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004862 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 if (*op != '*' && *op != '/' && *op != '%')
4864 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004865 if (next != NULL)
4866 {
4867 *arg = next_line_from_context(cctx, TRUE);
4868 op = skipwhite(*arg);
4869 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004870
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004871 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004873 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004874 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004876 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004877 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004879 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004880 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004882
4883 if (ppconst->pp_used == ppconst_used + 2
4884 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4885 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004886 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004887 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4888 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4889 varnumber_T res = 0;
4890 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004892 // both are numbers: compute the result
4893 switch (*op)
4894 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004895 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004896 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004897 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004898 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004899 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004900 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004901 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004902 break;
4903 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004904 if (failed)
4905 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004906 tv1->vval.v_number = res;
4907 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004908 }
4909 else
4910 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004911 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004912 generate_two_op(cctx, op);
4913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 }
4915
4916 return OK;
4917}
4918
4919/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004920 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 * - number subtraction
4922 * .. string concatenation
4923 */
4924 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004925compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926{
4927 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004928 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931
4932 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004933 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 return FAIL;
4935
4936 /*
4937 * Repeat computing, until no "+", "-" or ".." is following.
4938 */
4939 for (;;)
4940 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004941 op = may_peek_next_line(cctx, *arg, &next);
4942 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004944 if (op[0] == op[1] && *op != '.' && next)
4945 // Finding "++" or "--" on the next line is a separate command.
4946 // But ".." is concatenation.
4947 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004949 if (next != NULL)
4950 {
4951 *arg = next_line_from_context(cctx, TRUE);
4952 op = skipwhite(*arg);
4953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004955 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004957 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004958 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 }
4960
Bram Moolenaare0de1712020-12-02 17:36:54 +01004961 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004962 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004964 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004965 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 return FAIL;
4967
Bram Moolenaara5565e42020-05-09 15:44:01 +02004968 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004969 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4971 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4972 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4973 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004975 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4976 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004977
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004978 // concat/subtract/add constant numbers
4979 if (*op == '+')
4980 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4981 else if (*op == '-')
4982 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4983 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004984 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004985 // concatenate constant strings
4986 char_u *s1 = tv1->vval.v_string;
4987 char_u *s2 = tv2->vval.v_string;
4988 size_t len1 = STRLEN(s1);
4989
4990 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4991 if (tv1->vval.v_string == NULL)
4992 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004993 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004994 return FAIL;
4995 }
4996 mch_memmove(tv1->vval.v_string, s1, len1);
4997 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004998 vim_free(s1);
4999 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005000 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005004 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005005 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005006 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005007 if (*op == '.')
5008 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005009 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5010 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005011 return FAIL;
5012 generate_instr_drop(cctx, ISN_CONCAT, 1);
5013 }
5014 else
5015 generate_two_op(cctx, op);
5016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 }
5018
5019 return OK;
5020}
5021
5022/*
5023 * expr5a == expr5b
5024 * expr5a =~ expr5b
5025 * expr5a != expr5b
5026 * expr5a !~ expr5b
5027 * expr5a > expr5b
5028 * expr5a >= expr5b
5029 * expr5a < expr5b
5030 * expr5a <= expr5b
5031 * expr5a is expr5b
5032 * expr5a isnot expr5b
5033 *
5034 * Produces instructions:
5035 * EVAL expr5a Push result of "expr5a"
5036 * EVAL expr5b Push result of "expr5b"
5037 * COMPARE one of the compare instructions
5038 */
5039 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005040compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005042 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005044 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005047 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048
5049 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005050 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 return FAIL;
5052
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005053 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005054 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
5056 /*
5057 * If there is a comparative operator, use it.
5058 */
5059 if (type != EXPR_UNKNOWN)
5060 {
5061 int ic = FALSE; // Default: do not ignore case
5062
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005063 if (next != NULL)
5064 {
5065 *arg = next_line_from_context(cctx, TRUE);
5066 p = skipwhite(*arg);
5067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 if (type_is && (p[len] == '?' || p[len] == '#'))
5069 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005070 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 return FAIL;
5072 }
5073 // extra question mark appended: ignore case
5074 if (p[len] == '?')
5075 {
5076 ic = TRUE;
5077 ++len;
5078 }
5079 // extra '#' appended: match case (ignored)
5080 else if (p[len] == '#')
5081 ++len;
5082 // nothing appended: match case
5083
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005084 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005086 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005087 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 }
5089
5090 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005091 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 return FAIL;
5093
Bram Moolenaara5565e42020-05-09 15:44:01 +02005094 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 return FAIL;
5096
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097 if (ppconst->pp_used == ppconst_used + 2)
5098 {
5099 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5100 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5101 int ret;
5102
5103 // Both sides are a constant, compute the result now.
5104 // First check for a valid combination of types, this is more
5105 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005106 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005107 ret = FAIL;
5108 else
5109 {
5110 ret = typval_compare(tv1, tv2, type, ic);
5111 tv1->v_type = VAR_BOOL;
5112 tv1->vval.v_number = tv1->vval.v_number
5113 ? VVAL_TRUE : VVAL_FALSE;
5114 clear_tv(tv2);
5115 --ppconst->pp_used;
5116 }
5117 return ret;
5118 }
5119
5120 generate_ppconst(cctx, ppconst);
5121 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 }
5123
5124 return OK;
5125}
5126
Bram Moolenaar7f141552020-05-09 17:35:53 +02005127static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129/*
5130 * Compile || or &&.
5131 */
5132 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133compile_and_or(
5134 char_u **arg,
5135 cctx_T *cctx,
5136 char *op,
5137 ppconst_T *ppconst,
5138 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005140 char_u *next;
5141 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 int opchar = *op;
5143
5144 if (p[0] == opchar && p[1] == opchar)
5145 {
5146 garray_T *instr = &cctx->ctx_instr;
5147 garray_T end_ga;
5148
5149 /*
5150 * Repeat until there is no following "||" or "&&"
5151 */
5152 ga_init2(&end_ga, sizeof(int), 10);
5153 while (p[0] == opchar && p[1] == opchar)
5154 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005155 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005156 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005157 int start_ctx_lnum = cctx->ctx_lnum;
5158 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005159 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005160
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005161 if (next != NULL)
5162 {
5163 *arg = next_line_from_context(cctx, TRUE);
5164 p = skipwhite(*arg);
5165 }
5166
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005167 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5168 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005169 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005170 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005171 return FAIL;
5172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005174 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005175 SOURCING_LNUM = start_lnum;
5176 save_lnum = cctx->ctx_lnum;
5177 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005178
5179 status = check_ppconst_bool(ppconst);
5180 if (status == OK)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005181 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005182 // TODO: use ppconst if the value is a constant
5183 generate_ppconst(cctx, ppconst);
5184
5185 // Every part must evaluate to a bool.
5186 status = (bool_on_stack(cctx));
5187 if (status == OK)
5188 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005189 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005190 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005191 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 {
5193 ga_clear(&end_ga);
5194 return FAIL;
5195 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5198 ++end_ga.ga_len;
5199 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005200 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201
5202 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005203 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005204 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005205 {
5206 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005207 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005208 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005209
Bram Moolenaara5565e42020-05-09 15:44:01 +02005210 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5211 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 {
5213 ga_clear(&end_ga);
5214 return FAIL;
5215 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005216
5217 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005219
5220 if (check_ppconst_bool(ppconst) == FAIL)
5221 {
5222 ga_clear(&end_ga);
5223 return FAIL;
5224 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005227 // Every part must evaluate to a bool.
5228 if (bool_on_stack(cctx) == FAIL)
5229 {
5230 ga_clear(&end_ga);
5231 return FAIL;
5232 }
5233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 // Fill in the end label in all jumps.
5235 while (end_ga.ga_len > 0)
5236 {
5237 isn_T *isn;
5238
5239 --end_ga.ga_len;
5240 isn = ((isn_T *)instr->ga_data)
5241 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5242 isn->isn_arg.jump.jump_where = instr->ga_len;
5243 }
5244 ga_clear(&end_ga);
5245 }
5246
5247 return OK;
5248}
5249
5250/*
5251 * expr4a && expr4a && expr4a logical AND
5252 *
5253 * Produces instructions:
5254 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005255 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005256 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005258 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 * EVAL expr4c Push result of "expr4c"
5260 * end:
5261 */
5262 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005263compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005265 int ppconst_used = ppconst->pp_used;
5266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005268 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 return FAIL;
5270
5271 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005272 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273}
5274
5275/*
5276 * expr3a || expr3b || expr3c logical OR
5277 *
5278 * Produces instructions:
5279 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005280 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005281 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005283 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 * EVAL expr3c Push result of "expr3c"
5285 * end:
5286 */
5287 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005288compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005290 int ppconst_used = ppconst->pp_used;
5291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005293 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 return FAIL;
5295
5296 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298}
5299
5300/*
5301 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005303 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 * JUMP_IF_FALSE alt jump if false
5305 * EVAL expr1a
5306 * JUMP_ALWAYS end
5307 * alt: EVAL expr1b
5308 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005309 *
5310 * Toplevel expression: expr2 ?? expr1
5311 * Produces instructions:
5312 * EVAL expr2 Push result of "expr2"
5313 * JUMP_AND_KEEP_IF_TRUE end jump if true
5314 * EVAL expr1
5315 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 */
5317 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005318compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319{
5320 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005321 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005322 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323
Bram Moolenaar3988f642020-08-27 22:43:03 +02005324 // Ignore all kinds of errors when not producing code.
5325 if (cctx->ctx_skip == SKIP_YES)
5326 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005327 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005328 return OK;
5329 }
5330
Bram Moolenaar61a89812020-05-07 16:58:17 +02005331 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005332 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 return FAIL;
5334
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005335 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 if (*p == '?')
5337 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005338 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 garray_T *instr = &cctx->ctx_instr;
5340 garray_T *stack = &cctx->ctx_type_stack;
5341 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005342 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005344 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005345 int has_const_expr = FALSE;
5346 int const_value = FALSE;
5347 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005349 if (next != NULL)
5350 {
5351 *arg = next_line_from_context(cctx, TRUE);
5352 p = skipwhite(*arg);
5353 }
5354
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005356 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005357 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005358 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005359 return FAIL;
5360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361
Bram Moolenaara5565e42020-05-09 15:44:01 +02005362 if (ppconst->pp_used == ppconst_used + 1)
5363 {
5364 // the condition is a constant, we know whether the ? or the :
5365 // expression is to be evaluated.
5366 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005367 if (op_falsy)
5368 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5369 else
5370 {
5371 int error = FALSE;
5372
5373 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5374 &error);
5375 if (error)
5376 return FAIL;
5377 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005378 cctx->ctx_skip = save_skip == SKIP_YES ||
5379 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5380
5381 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5382 // "left ?? right" and "left" is truthy: produce "left"
5383 generate_ppconst(cctx, ppconst);
5384 else
5385 {
5386 clear_tv(&ppconst->pp_tv[ppconst_used]);
5387 --ppconst->pp_used;
5388 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005389 }
5390 else
5391 {
5392 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005393 if (op_falsy)
5394 end_idx = instr->ga_len;
5395 generate_JUMP(cctx, op_falsy
5396 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5397 if (op_falsy)
5398 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
5401 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005402 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005403 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005404 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005405 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406
Bram Moolenaara5565e42020-05-09 15:44:01 +02005407 if (!has_const_expr)
5408 {
5409 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005411 if (!op_falsy)
5412 {
5413 // remember the type and drop it
5414 --stack->ga_len;
5415 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005417 end_idx = instr->ga_len;
5418 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005419
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005420 // jump here from JUMP_IF_FALSE
5421 isn = ((isn_T *)instr->ga_data) + alt_idx;
5422 isn->isn_arg.jump.jump_where = instr->ga_len;
5423 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005426 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005428 // Check for the ":".
5429 p = may_peek_next_line(cctx, *arg, &next);
5430 if (*p != ':')
5431 {
5432 emsg(_(e_missing_colon));
5433 return FAIL;
5434 }
5435 if (next != NULL)
5436 {
5437 *arg = next_line_from_context(cctx, TRUE);
5438 p = skipwhite(*arg);
5439 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005440
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005441 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5442 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005443 semsg(_(e_white_space_required_before_and_after_str_at_str),
5444 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005445 return FAIL;
5446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005448 // evaluate the third expression
5449 if (has_const_expr)
5450 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005451 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005452 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005453 return FAIL;
5454 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5455 return FAIL;
5456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457
Bram Moolenaara5565e42020-05-09 15:44:01 +02005458 if (!has_const_expr)
5459 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005460 type_T **typep;
5461
Bram Moolenaara5565e42020-05-09 15:44:01 +02005462 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463
Bram Moolenaara5565e42020-05-09 15:44:01 +02005464 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005465 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5466 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005467
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005468 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005469 isn = ((isn_T *)instr->ga_data) + end_idx;
5470 isn->isn_arg.jump.jump_where = instr->ga_len;
5471 }
5472
5473 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 }
5475 return OK;
5476}
5477
5478/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005479 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005480 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5481 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 */
5483 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005484compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005485{
5486 ppconst_T ppconst;
5487
5488 CLEAR_FIELD(ppconst);
5489 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5490 {
5491 clear_ppconst(&ppconst);
5492 return FAIL;
5493 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005494 if (is_const != NULL)
5495 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005496 if (generate_ppconst(cctx, &ppconst) == FAIL)
5497 return FAIL;
5498 return OK;
5499}
5500
5501/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005502 * Toplevel expression.
5503 */
5504 static int
5505compile_expr0(char_u **arg, cctx_T *cctx)
5506{
5507 return compile_expr0_ext(arg, cctx, NULL);
5508}
5509
5510/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005511 * Compile "return [expr]".
5512 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 */
5514 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005515compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516{
5517 char_u *p = arg;
5518 garray_T *stack = &cctx->ctx_type_stack;
5519 type_T *stack_type;
5520
5521 if (*p != NUL && *p != '|' && *p != '\n')
5522 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005523 if (legacy)
5524 {
5525 int save_flags = cmdmod.cmod_flags;
5526
5527 generate_LEGACY_EVAL(cctx, p);
5528 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5529 0, cctx, FALSE, FALSE) == FAIL)
5530 return NULL;
5531 cmdmod.cmod_flags |= CMOD_LEGACY;
5532 (void)skip_expr(&p, NULL);
5533 cmdmod.cmod_flags = save_flags;
5534 }
5535 else
5536 {
5537 // compile return argument into instructions
5538 if (compile_expr0(&p, cctx) == FAIL)
5539 return NULL;
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005542 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005543 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005544 // "check_return_type" with uf_ret_type set to &t_unknown is used
5545 // for an inline function without a specified return type. Set the
5546 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005547 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005548 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005549 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5550 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005551 || (!check_return_type
5552 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005553 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005554 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005555 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005556 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005557 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005558 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5559 && stack_type->tt_type != VAR_VOID
5560 && stack_type->tt_type != VAR_UNKNOWN)
5561 {
5562 emsg(_(e_returning_value_in_function_without_return_type));
5563 return NULL;
5564 }
5565 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005566 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005567 return NULL;
5568 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570 }
5571 else
5572 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005573 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005574 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005575 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5576 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005578 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 return NULL;
5580 }
5581
5582 // No argument, return zero.
5583 generate_PUSHNR(cctx, 0);
5584 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005585
5586 // Undo any command modifiers.
5587 generate_undo_cmdmods(cctx);
5588
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005589 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 return NULL;
5591
5592 // "return val | endif" is possible
5593 return skipwhite(p);
5594}
5595
5596/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005597 * Get a line from the compilation context, compatible with exarg_T getline().
5598 * Return a pointer to the line in allocated memory.
5599 * Return NULL for end-of-file or some error.
5600 */
5601 static char_u *
5602exarg_getline(
5603 int c UNUSED,
5604 void *cookie,
5605 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005606 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005607{
5608 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005609 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005610
Bram Moolenaar66250c92020-08-20 15:02:42 +02005611 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005612 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005613 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005614 return NULL;
5615 ++cctx->ctx_lnum;
5616 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5617 // Comment lines result in NULL pointers, skip them.
5618 if (p != NULL)
5619 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005620 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005621}
5622
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005623 void
5624fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5625{
5626 eap->getline = exarg_getline;
5627 eap->cookie = cctx;
5628}
5629
Bram Moolenaar04b12692020-05-04 23:24:44 +02005630/*
5631 * Compile a nested :def command.
5632 */
5633 static char_u *
5634compile_nested_function(exarg_T *eap, cctx_T *cctx)
5635{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005636 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005637 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005638 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005639 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005640 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005641 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005642 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005643
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005644 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005645 {
5646 emsg(_(e_cannot_use_bang_with_nested_def));
5647 return NULL;
5648 }
5649
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005650 if (*name_start == '/')
5651 {
5652 name_end = skip_regexp(name_start + 1, '/', TRUE);
5653 if (*name_end == '/')
5654 ++name_end;
5655 eap->nextcmd = check_nextcmd(name_end);
5656 }
5657 if (name_end == name_start || *skipwhite(name_end) != '(')
5658 {
5659 if (!ends_excmd2(name_start, name_end))
5660 {
5661 semsg(_(e_invalid_command_str), eap->cmd);
5662 return NULL;
5663 }
5664
5665 // "def" or "def Name": list functions
5666 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5667 return NULL;
5668 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5669 }
5670
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005671 // Only g:Func() can use a namespace.
5672 if (name_start[1] == ':' && !is_global)
5673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005674 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005675 return NULL;
5676 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005677 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005678 return NULL;
5679
Bram Moolenaar04b12692020-05-04 23:24:44 +02005680 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005681 fill_exarg_from_cctx(eap, cctx);
5682
Bram Moolenaar04b12692020-05-04 23:24:44 +02005683 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005684 lambda_name = vim_strsave(get_lambda_name());
5685 if (lambda_name == NULL)
5686 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005687 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005688
Bram Moolenaar822ba242020-05-24 23:00:18 +02005689 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005690 {
5691 r = eap->skip ? OK : FAIL;
5692 goto theend;
5693 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005694
5695 // copy over the block scope IDs before compiling
5696 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5697 {
5698 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5699
5700 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5701 if (ufunc->uf_block_ids != NULL)
5702 {
5703 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5704 sizeof(int) * block_depth);
5705 ufunc->uf_block_depth = block_depth;
5706 }
5707 }
5708
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005709 compile_type = COMPILE_TYPE(ufunc);
5710#ifdef FEAT_PROFILE
5711 // If the outer function is profiled, also compile the nested function for
5712 // profiling.
5713 if (cctx->ctx_compile_type == CT_PROFILE)
5714 compile_type = CT_PROFILE;
5715#endif
5716 if (func_needs_compiling(ufunc, compile_type)
5717 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005718 {
5719 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005720 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005721 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005722
Bram Moolenaar648594e2021-07-11 17:55:01 +02005723#ifdef FEAT_PROFILE
5724 // When the outer function is compiled for profiling, the nested function
5725 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005726 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005727 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5728#endif
5729
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005730 if (is_global)
5731 {
5732 char_u *func_name = vim_strnsave(name_start + 2,
5733 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005734
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005735 if (func_name == NULL)
5736 r = FAIL;
5737 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005738 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005739 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005740 lambda_name = NULL;
5741 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005742 }
5743 else
5744 {
5745 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005746 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005747 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005748
Bram Moolenaareef21022020-08-01 22:16:43 +02005749 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005750 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005751 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005752 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005753 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5754 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005755 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005756
5757theend:
5758 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005759 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005760}
5761
5762/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 * Return the length of an assignment operator, or zero if there isn't one.
5764 */
5765 int
5766assignment_len(char_u *p, int *heredoc)
5767{
5768 if (*p == '=')
5769 {
5770 if (p[1] == '<' && p[2] == '<')
5771 {
5772 *heredoc = TRUE;
5773 return 3;
5774 }
5775 return 1;
5776 }
5777 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5778 return 2;
5779 if (STRNCMP(p, "..=", 3) == 0)
5780 return 3;
5781 return 0;
5782}
5783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005785 * Generate the load instruction for "name".
5786 */
5787 static void
5788generate_loadvar(
5789 cctx_T *cctx,
5790 assign_dest_T dest,
5791 char_u *name,
5792 lvar_T *lvar,
5793 type_T *type)
5794{
5795 switch (dest)
5796 {
5797 case dest_option:
5798 // TODO: check the option exists
5799 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5800 break;
5801 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005802 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5803 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5804 else
5805 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005806 break;
5807 case dest_buffer:
5808 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5809 break;
5810 case dest_window:
5811 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5812 break;
5813 case dest_tab:
5814 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5815 break;
5816 case dest_script:
5817 compile_load_scriptvar(cctx,
5818 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5819 break;
5820 case dest_env:
5821 // Include $ in the name here
5822 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5823 break;
5824 case dest_reg:
5825 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5826 break;
5827 case dest_vimvar:
5828 generate_LOADV(cctx, name + 2, TRUE);
5829 break;
5830 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005831 if (lvar->lv_from_outer > 0)
5832 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5833 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005834 else
5835 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5836 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005837 case dest_expr:
5838 // list or dict value should already be on the stack.
5839 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005840 }
5841}
5842
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005843/*
5844 * Skip over "[expr]" or ".member".
5845 * Does not check for any errors.
5846 */
5847 static char_u *
5848skip_index(char_u *start)
5849{
5850 char_u *p = start;
5851
5852 if (*p == '[')
5853 {
5854 p = skipwhite(p + 1);
5855 (void)skip_expr(&p, NULL);
5856 p = skipwhite(p);
5857 if (*p == ']')
5858 return p + 1;
5859 return p;
5860 }
5861 // if (*p == '.')
5862 return to_name_end(p + 1, TRUE);
5863}
5864
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005865 void
5866vim9_declare_error(char_u *name)
5867{
5868 char *scope = "";
5869
5870 switch (*name)
5871 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005872 case 'g': scope = _("global"); break;
5873 case 'b': scope = _("buffer"); break;
5874 case 'w': scope = _("window"); break;
5875 case 't': scope = _("tab"); break;
5876 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005877 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005878 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005879 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005880 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005881 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005882 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005883 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005884 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005885 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005886}
5887
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005888/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005889 * For one assignment figure out the type of destination. Return it in "dest".
5890 * When not recognized "dest" is not set.
5891 * For an option "opt_flags" is set.
5892 * For a v:var "vimvaridx" is set.
5893 * "type" is set to the destination type if known, unchanted otherwise.
5894 * Return FAIL if an error message was given.
5895 */
5896 static int
5897get_var_dest(
5898 char_u *name,
5899 assign_dest_T *dest,
5900 int cmdidx,
5901 int *opt_flags,
5902 int *vimvaridx,
5903 type_T **type,
5904 cctx_T *cctx)
5905{
5906 char_u *p;
5907
5908 if (*name == '&')
5909 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005910 int cc;
5911 long numval;
5912 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005913
5914 *dest = dest_option;
5915 if (cmdidx == CMD_final || cmdidx == CMD_const)
5916 {
5917 emsg(_(e_const_option));
5918 return FAIL;
5919 }
5920 p = name;
5921 p = find_option_end(&p, opt_flags);
5922 if (p == NULL)
5923 {
5924 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005925 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005926 return FAIL;
5927 }
5928 cc = *p;
5929 *p = NUL;
5930 opt_type = get_option_value(skip_option_env_lead(name),
5931 &numval, NULL, *opt_flags);
5932 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005933 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005934 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005935 case gov_unknown:
5936 semsg(_(e_unknown_option), name);
5937 return FAIL;
5938 case gov_string:
5939 case gov_hidden_string:
5940 *type = &t_string;
5941 break;
5942 case gov_bool:
5943 case gov_hidden_bool:
5944 *type = &t_bool;
5945 break;
5946 case gov_number:
5947 case gov_hidden_number:
5948 *type = &t_number;
5949 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005950 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005951 }
5952 else if (*name == '$')
5953 {
5954 *dest = dest_env;
5955 *type = &t_string;
5956 }
5957 else if (*name == '@')
5958 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005959 if (name[1] != '@'
5960 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005961 {
5962 emsg_invreg(name[1]);
5963 return FAIL;
5964 }
5965 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005966 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005967 }
5968 else if (STRNCMP(name, "g:", 2) == 0)
5969 {
5970 *dest = dest_global;
5971 }
5972 else if (STRNCMP(name, "b:", 2) == 0)
5973 {
5974 *dest = dest_buffer;
5975 }
5976 else if (STRNCMP(name, "w:", 2) == 0)
5977 {
5978 *dest = dest_window;
5979 }
5980 else if (STRNCMP(name, "t:", 2) == 0)
5981 {
5982 *dest = dest_tab;
5983 }
5984 else if (STRNCMP(name, "v:", 2) == 0)
5985 {
5986 typval_T *vtv;
5987 int di_flags;
5988
5989 *vimvaridx = find_vim_var(name + 2, &di_flags);
5990 if (*vimvaridx < 0)
5991 {
5992 semsg(_(e_variable_not_found_str), name);
5993 return FAIL;
5994 }
5995 // We use the current value of "sandbox" here, is that OK?
5996 if (var_check_ro(di_flags, name, FALSE))
5997 return FAIL;
5998 *dest = dest_vimvar;
5999 vtv = get_vim_var_tv(*vimvaridx);
6000 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6001 }
6002 return OK;
6003}
6004
6005/*
6006 * Generate a STORE instruction for "dest", not being "dest_local".
6007 * Return FAIL when out of memory.
6008 */
6009 static int
6010generate_store_var(
6011 cctx_T *cctx,
6012 assign_dest_T dest,
6013 int opt_flags,
6014 int vimvaridx,
6015 int scriptvar_idx,
6016 int scriptvar_sid,
6017 type_T *type,
6018 char_u *name)
6019{
6020 switch (dest)
6021 {
6022 case dest_option:
6023 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6024 opt_flags);
6025 case dest_global:
6026 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006027 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6028 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006029 case dest_buffer:
6030 // include b: with the name, easier to execute that way
6031 return generate_STORE(cctx, ISN_STOREB, 0, name);
6032 case dest_window:
6033 // include w: with the name, easier to execute that way
6034 return generate_STORE(cctx, ISN_STOREW, 0, name);
6035 case dest_tab:
6036 // include t: with the name, easier to execute that way
6037 return generate_STORE(cctx, ISN_STORET, 0, name);
6038 case dest_env:
6039 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6040 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006041 return generate_STORE(cctx, ISN_STOREREG,
6042 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006043 case dest_vimvar:
6044 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6045 case dest_script:
6046 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006047 // "s:" may be included in the name.
6048 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006049 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006050 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6051 scriptvar_sid, scriptvar_idx, type);
6052 case dest_local:
6053 case dest_expr:
6054 // cannot happen
6055 break;
6056 }
6057 return FAIL;
6058}
6059
Bram Moolenaar752fc692021-01-04 21:57:11 +01006060 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006061generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6062{
6063 if (lhs->lhs_dest != dest_local)
6064 return generate_store_var(cctx, lhs->lhs_dest,
6065 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6066 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6067 lhs->lhs_type, lhs->lhs_name);
6068
6069 if (lhs->lhs_lvar != NULL)
6070 {
6071 garray_T *instr = &cctx->ctx_instr;
6072 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6073
6074 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6075 // ISN_STORENR
6076 if (lhs->lhs_lvar->lv_from_outer == 0
6077 && instr->ga_len == instr_count + 1
6078 && isn->isn_type == ISN_PUSHNR)
6079 {
6080 varnumber_T val = isn->isn_arg.number;
6081 garray_T *stack = &cctx->ctx_type_stack;
6082
6083 isn->isn_type = ISN_STORENR;
6084 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6085 isn->isn_arg.storenr.stnr_val = val;
6086 if (stack->ga_len > 0)
6087 --stack->ga_len;
6088 }
6089 else if (lhs->lhs_lvar->lv_from_outer > 0)
6090 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6091 lhs->lhs_lvar->lv_from_outer);
6092 else
6093 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6094 }
6095 return OK;
6096}
6097
6098 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099is_decl_command(int cmdidx)
6100{
6101 return cmdidx == CMD_let || cmdidx == CMD_var
6102 || cmdidx == CMD_final || cmdidx == CMD_const;
6103}
6104
6105/*
6106 * Figure out the LHS type and other properties for an assignment or one item
6107 * of ":unlet" with an index.
6108 * Returns OK or FAIL.
6109 */
6110 static int
6111compile_lhs(
6112 char_u *var_start,
6113 lhs_T *lhs,
6114 int cmdidx,
6115 int heredoc,
6116 int oplen,
6117 cctx_T *cctx)
6118{
6119 char_u *var_end;
6120 int is_decl = is_decl_command(cmdidx);
6121
6122 CLEAR_POINTER(lhs);
6123 lhs->lhs_dest = dest_local;
6124 lhs->lhs_vimvaridx = -1;
6125 lhs->lhs_scriptvar_idx = -1;
6126
6127 // "dest_end" is the end of the destination, including "[expr]" or
6128 // ".name".
6129 // "var_end" is the end of the variable/option/etc. name.
6130 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6131 if (*var_start == '@')
6132 var_end = var_start + 2;
6133 else
6134 {
6135 // skip over the leading "&", "&l:", "&g:" and "$"
6136 var_end = skip_option_env_lead(var_start);
6137 var_end = to_name_end(var_end, TRUE);
6138 }
6139
6140 // "a: type" is declaring variable "a" with a type, not dict "a:".
6141 if (is_decl && lhs->lhs_dest_end == var_start + 2
6142 && lhs->lhs_dest_end[-1] == ':')
6143 --lhs->lhs_dest_end;
6144 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6145 --var_end;
6146
6147 // compute the length of the destination without "[expr]" or ".name"
6148 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006149 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006150 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6151 if (lhs->lhs_name == NULL)
6152 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006153
6154 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6155 // Something follows after the variable: "var[idx]" or "var.key".
6156 lhs->lhs_has_index = TRUE;
6157
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 if (heredoc)
6159 lhs->lhs_type = &t_list_string;
6160 else
6161 lhs->lhs_type = &t_any;
6162
6163 if (cctx->ctx_skip != SKIP_YES)
6164 {
6165 int declare_error = FALSE;
6166
6167 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6168 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6169 &lhs->lhs_type, cctx) == FAIL)
6170 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006171 if (lhs->lhs_dest != dest_local
6172 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006173 {
6174 // Specific kind of variable recognized.
6175 declare_error = is_decl;
6176 }
6177 else
6178 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006179 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006180 if (check_reserved_name(lhs->lhs_name) == FAIL)
6181 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006182
6183 if (lookup_local(var_start, lhs->lhs_varlen,
6184 &lhs->lhs_local_lvar, cctx) == OK)
6185 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6186 else
6187 {
6188 CLEAR_FIELD(lhs->lhs_arg_lvar);
6189 if (arg_exists(var_start, lhs->lhs_varlen,
6190 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6191 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6192 {
6193 if (is_decl)
6194 {
6195 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6196 return FAIL;
6197 }
6198 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6199 }
6200 }
6201 if (lhs->lhs_lvar != NULL)
6202 {
6203 if (is_decl)
6204 {
6205 semsg(_(e_variable_already_declared), lhs->lhs_name);
6206 return FAIL;
6207 }
6208 }
6209 else
6210 {
6211 int script_namespace = lhs->lhs_varlen > 1
6212 && STRNCMP(var_start, "s:", 2) == 0;
6213 int script_var = (script_namespace
6214 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006215 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006217 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 imported_T *import =
6219 find_imported(var_start, lhs->lhs_varlen, cctx);
6220
6221 if (script_namespace || script_var || import != NULL)
6222 {
6223 char_u *rawname = lhs->lhs_name
6224 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6225
6226 if (is_decl)
6227 {
6228 if (script_namespace)
6229 semsg(_(e_cannot_declare_script_variable_in_function),
6230 lhs->lhs_name);
6231 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006232 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 lhs->lhs_name);
6234 return FAIL;
6235 }
6236 else if (cctx->ctx_ufunc->uf_script_ctx_version
6237 == SCRIPT_VERSION_VIM9
6238 && script_namespace
6239 && !script_var && import == NULL)
6240 {
6241 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6242 return FAIL;
6243 }
6244
6245 lhs->lhs_dest = dest_script;
6246
6247 // existing script-local variables should have a type
6248 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6249 if (import != NULL)
6250 lhs->lhs_scriptvar_sid = import->imp_sid;
6251 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6252 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006253 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006254 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006255 lhs->lhs_scriptvar_sid, rawname,
6256 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6257 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006258 if (lhs->lhs_scriptvar_idx >= 0)
6259 {
6260 scriptitem_T *si = SCRIPT_ITEM(
6261 lhs->lhs_scriptvar_sid);
6262 svar_T *sv =
6263 ((svar_T *)si->sn_var_vals.ga_data)
6264 + lhs->lhs_scriptvar_idx;
6265 lhs->lhs_type = sv->sv_type;
6266 }
6267 }
6268 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006269 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 == FAIL)
6271 return FAIL;
6272 }
6273 }
6274
6275 if (declare_error)
6276 {
6277 vim9_declare_error(lhs->lhs_name);
6278 return FAIL;
6279 }
6280 }
6281
6282 // handle "a:name" as a name, not index "name" on "a"
6283 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6284 var_end = lhs->lhs_dest_end;
6285
6286 if (lhs->lhs_dest != dest_option)
6287 {
6288 if (is_decl && *var_end == ':')
6289 {
6290 char_u *p;
6291
6292 // parse optional type: "let var: type = expr"
6293 if (!VIM_ISWHITE(var_end[1]))
6294 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006295 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006296 return FAIL;
6297 }
6298 p = skipwhite(var_end + 1);
6299 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6300 if (lhs->lhs_type == NULL)
6301 return FAIL;
6302 lhs->lhs_has_type = TRUE;
6303 }
6304 else if (lhs->lhs_lvar != NULL)
6305 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6306 }
6307
Bram Moolenaare42939a2021-04-05 17:11:17 +02006308 if (oplen == 3 && !heredoc
6309 && lhs->lhs_dest != dest_global
6310 && !lhs->lhs_has_index
6311 && lhs->lhs_type->tt_type != VAR_STRING
6312 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006313 {
6314 emsg(_(e_can_only_concatenate_to_string));
6315 return FAIL;
6316 }
6317
6318 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6319 && cctx->ctx_skip != SKIP_YES)
6320 {
6321 if (oplen > 1 && !heredoc)
6322 {
6323 // +=, /=, etc. require an existing variable
6324 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6325 return FAIL;
6326 }
6327 if (!is_decl)
6328 {
6329 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6330 return FAIL;
6331 }
6332
Bram Moolenaar3f327882021-03-17 20:56:38 +01006333 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006334 if ((lhs->lhs_type->tt_type == VAR_FUNC
6335 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006336 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006337 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006338
6339 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6341 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6342 if (lhs->lhs_lvar == NULL)
6343 return FAIL;
6344 lhs->lhs_new_local = TRUE;
6345 }
6346
6347 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006348 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006349 {
6350 // Something follows after the variable: "var[idx]" or "var.key".
6351 // TODO: should we also handle "->func()" here?
6352 if (is_decl)
6353 {
6354 emsg(_(e_cannot_use_index_when_declaring_variable));
6355 return FAIL;
6356 }
6357
6358 if (var_start[lhs->lhs_varlen] == '['
6359 || var_start[lhs->lhs_varlen] == '.')
6360 {
6361 char_u *after = var_start + lhs->lhs_varlen;
6362 char_u *p;
6363
6364 // Only the last index is used below, if there are others
6365 // before it generate code for the expression. Thus for
6366 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6367 for (;;)
6368 {
6369 p = skip_index(after);
6370 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006371 {
6372 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006374 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 after = p;
6376 }
6377 if (after > var_start + lhs->lhs_varlen)
6378 {
6379 lhs->lhs_varlen = after - var_start;
6380 lhs->lhs_dest = dest_expr;
6381 // We don't know the type before evaluating the expression,
6382 // use "any" until then.
6383 lhs->lhs_type = &t_any;
6384 }
6385
Bram Moolenaar752fc692021-01-04 21:57:11 +01006386 if (lhs->lhs_type->tt_member == NULL)
6387 lhs->lhs_member_type = &t_any;
6388 else
6389 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6390 }
6391 else
6392 {
6393 semsg("Not supported yet: %s", var_start);
6394 return FAIL;
6395 }
6396 }
6397 return OK;
6398}
6399
6400/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006401 * Figure out the LHS and check a few errors.
6402 */
6403 static int
6404compile_assign_lhs(
6405 char_u *var_start,
6406 lhs_T *lhs,
6407 int cmdidx,
6408 int is_decl,
6409 int heredoc,
6410 int oplen,
6411 cctx_T *cctx)
6412{
6413 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6414 return FAIL;
6415
6416 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6417 {
6418 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6419 return FAIL;
6420 }
6421 if (!is_decl && lhs->lhs_lvar != NULL
6422 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6423 {
6424 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6425 return FAIL;
6426 }
6427 return OK;
6428}
6429
6430/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006431 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6432 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 */
6434 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006435compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006436 char_u *var_start,
6437 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006438 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006439 cctx_T *cctx)
6440{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006442 char_u *p;
6443 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006444 int need_white_before = TRUE;
6445 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006446
Bram Moolenaar752fc692021-01-04 21:57:11 +01006447 p = var_start + varlen;
6448 if (*p == '[')
6449 {
6450 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006451 if (*p == ':')
6452 {
6453 // empty first index, push zero
6454 r = generate_PUSHNR(cctx, 0);
6455 need_white_before = FALSE;
6456 }
6457 else
6458 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006459
6460 if (r == OK && *skipwhite(p) == ':')
6461 {
6462 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006463 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006464 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006465 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006466 empty_second = *skipwhite(p + 1) == ']';
6467 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6468 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006469 {
6470 semsg(_(e_white_space_required_before_and_after_str_at_str),
6471 ":", p);
6472 return FAIL;
6473 }
6474 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006475 if (*p == ']')
6476 // empty second index, push "none"
6477 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6478 else
6479 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006480 }
6481
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 if (r == OK && *skipwhite(p) != ']')
6483 {
6484 // this should not happen
6485 emsg(_(e_missbrac));
6486 r = FAIL;
6487 }
6488 }
6489 else // if (*p == '.')
6490 {
6491 char_u *key_end = to_name_end(p + 1, TRUE);
6492 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6493
6494 r = generate_PUSHS(cctx, key);
6495 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006496 return r;
6497}
6498
6499/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006500 * For a LHS with an index, load the variable to be indexed.
6501 */
6502 static int
6503compile_load_lhs(
6504 lhs_T *lhs,
6505 char_u *var_start,
6506 type_T *rhs_type,
6507 cctx_T *cctx)
6508{
6509 if (lhs->lhs_dest == dest_expr)
6510 {
6511 size_t varlen = lhs->lhs_varlen;
6512 int c = var_start[varlen];
6513 char_u *p = var_start;
6514 garray_T *stack = &cctx->ctx_type_stack;
6515
6516 // Evaluate "ll[expr]" of "ll[expr][idx]"
6517 var_start[varlen] = NUL;
6518 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6519 {
6520 // this should not happen
6521 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006522 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006523 return FAIL;
6524 }
6525 var_start[varlen] = c;
6526
6527 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6528 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6529 // now we can properly check the type
6530 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6531 && rhs_type != &t_void
6532 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6533 FALSE, FALSE) == FAIL)
6534 return FAIL;
6535 }
6536 else
6537 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6538 lhs->lhs_lvar, lhs->lhs_type);
6539 return OK;
6540}
6541
6542/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006543 * Produce code for loading "lhs" and also take care of an index.
6544 * Return OK/FAIL.
6545 */
6546 static int
6547compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6548{
6549 compile_load_lhs(lhs, var_start, NULL, cctx);
6550
6551 if (lhs->lhs_has_index)
6552 {
6553 int range = FALSE;
6554
6555 // Get member from list or dict. First compile the
6556 // index value.
6557 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6558 return FAIL;
6559 if (range)
6560 {
6561 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6562 var_start);
6563 return FAIL;
6564 }
6565
6566 // Get the member.
6567 if (compile_member(FALSE, cctx) == FAIL)
6568 return FAIL;
6569 }
6570 return OK;
6571}
6572
6573/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006574 * Assignment to a list or dict member, or ":unlet" for the item, using the
6575 * information in "lhs".
6576 * Returns OK or FAIL.
6577 */
6578 static int
6579compile_assign_unlet(
6580 char_u *var_start,
6581 lhs_T *lhs,
6582 int is_assign,
6583 type_T *rhs_type,
6584 cctx_T *cctx)
6585{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006586 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006587 garray_T *stack = &cctx->ctx_type_stack;
6588 int range = FALSE;
6589
Bram Moolenaar68452172021-04-12 21:21:02 +02006590 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006591 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006592 if (is_assign && range && lhs->lhs_type != &t_blob
6593 && lhs->lhs_type != &t_any)
6594 {
6595 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6596 return FAIL;
6597 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006598
6599 if (lhs->lhs_type == &t_any)
6600 {
6601 // Index on variable of unknown type: check at runtime.
6602 dest_type = VAR_ANY;
6603 }
6604 else
6605 {
6606 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006607 if (dest_type == VAR_DICT && range)
6608 {
6609 emsg(e_cannot_use_range_with_dictionary);
6610 return FAIL;
6611 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006612 if (dest_type == VAR_DICT
6613 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006615 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006616 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006617 type_T *type;
6618
6619 if (range)
6620 {
6621 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6622 if (need_type(type, &t_number,
6623 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006624 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006625 }
6626 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006627 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006628 && need_type(type, &t_number,
6629 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006630 return FAIL;
6631 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 }
6633
6634 // Load the dict or list. On the stack we then have:
6635 // - value (for assignment, not for :unlet)
6636 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006637 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006638 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006639 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6640 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006641
Bram Moolenaar68452172021-04-12 21:21:02 +02006642 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6643 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006644 {
6645 if (is_assign)
6646 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006647 if (range)
6648 {
6649 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6650 return FAIL;
6651 }
6652 else
6653 {
6654 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006655
Bram Moolenaar68452172021-04-12 21:21:02 +02006656 if (isn == NULL)
6657 return FAIL;
6658 isn->isn_arg.vartype = dest_type;
6659 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006660 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006661 else if (range)
6662 {
6663 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6664 return FAIL;
6665 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 else
6667 {
6668 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6669 return FAIL;
6670 }
6671 }
6672 else
6673 {
6674 emsg(_(e_indexable_type_required));
6675 return FAIL;
6676 }
6677
6678 return OK;
6679}
6680
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006681/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006682 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006683 * "let name"
6684 * "var name = expr"
6685 * "final name = expr"
6686 * "const name = expr"
6687 * "name = expr"
6688 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006689 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690 * Return NULL for an error.
6691 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 */
6693 static char_u *
6694compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6695{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006696 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006698 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 char_u *ret = NULL;
6700 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006701 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006703 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006705 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 int oplen = 0;
6708 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006709 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006710 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006712 int is_decl = is_decl_command(cmdidx);
6713 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006714 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 // Skip over the "var" or "[var, var]" to get to any "=".
6717 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6718 if (p == NULL)
6719 return *arg == '[' ? arg : NULL;
6720
6721 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006723 // TODO: should we allow this, and figure out type inference from list
6724 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006725 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 return NULL;
6727 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006728 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 sp = p;
6731 p = skipwhite(p);
6732 op = p;
6733 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006734
6735 if (var_count > 0 && oplen == 0)
6736 // can be something like "[1, 2]->func()"
6737 return arg;
6738
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006739 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006741 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006742 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006743 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006744 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6745 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006746 if (VIM_ISWHITE(eap->cmd[2]))
6747 {
6748 semsg(_(e_no_white_space_allowed_after_str_str),
6749 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6750 return NULL;
6751 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006752 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6753 oplen = 2;
6754 incdec = TRUE;
6755 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 if (heredoc)
6758 {
6759 list_T *l;
6760 listitem_T *li;
6761
6762 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006763 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006765 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006766 if (l == NULL)
6767 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768
Bram Moolenaar078269b2020-09-21 20:35:55 +02006769 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006771 // Push each line and the create the list.
6772 FOR_ALL_LIST_ITEMS(l, li)
6773 {
6774 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6775 li->li_tv.vval.v_string = NULL;
6776 }
6777 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 list_free(l);
6780 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006781 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006783 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006785 char_u *wp;
6786
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 // for "[var, var] = expr" evaluate the expression here, loop over the
6788 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006789 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006790
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006791 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006792 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006793 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006794 if (compile_expr0(&p, cctx) == FAIL)
6795 return NULL;
6796 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006798 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006800 type_T *stacktype;
6801
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006802 stacktype = stack->ga_len == 0 ? &t_void
6803 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006804 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006806 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006807 goto theend;
6808 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006809 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006810 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006811 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006812 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006813 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6814 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006815 if (stacktype->tt_member != NULL)
6816 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006817 }
6818 }
6819
6820 /*
6821 * Loop over variables in "[var, var] = expr".
6822 * For "var = expr" and "let var: type" this is done only once.
6823 */
6824 if (var_count > 0)
6825 var_start = skipwhite(arg + 1); // skip over the "["
6826 else
6827 var_start = arg;
6828 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6829 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006830 int instr_count = -1;
6831 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006833 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6834 {
6835 // Ignore underscore in "[a, _, b] = list".
6836 if (var_count > 0)
6837 {
6838 var_start = skipwhite(var_start + 2);
6839 continue;
6840 }
6841 emsg(_(e_cannot_use_underscore_here));
6842 goto theend;
6843 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844 vim_free(lhs.lhs_name);
6845
6846 /*
6847 * Figure out the LHS type and other properties.
6848 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006849 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6850 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006851 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006852 if (!heredoc)
6853 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006854 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006855 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006856 if (oplen > 0 && var_count == 0)
6857 {
6858 // skip over the "=" and the expression
6859 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006860 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006861 }
6862 }
6863 else if (oplen > 0)
6864 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006865 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006866 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006867
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006868 // for "+=", "*=", "..=" etc. first load the current value
6869 if (*op != '='
6870 && compile_load_lhs_with_index(&lhs, var_start,
6871 cctx) == FAIL)
6872 goto theend;
6873
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 // For "var = expr" evaluate the expression.
6875 if (var_count == 0)
6876 {
6877 int r;
6878
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006879 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006881 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006882 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006883 r = generate_PUSHNR(cctx, 1);
6884 }
6885 else
6886 {
6887 // Temporarily hide the new local variable here, it is
6888 // not available to this expression.
6889 if (lhs.lhs_new_local)
6890 --cctx->ctx_locals.ga_len;
6891 wp = op + oplen;
6892 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6893 {
6894 if (lhs.lhs_new_local)
6895 ++cctx->ctx_locals.ga_len;
6896 goto theend;
6897 }
6898 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006899 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006900 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006901 if (r == FAIL)
6902 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006903 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006904 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006905 else if (semicolon && var_idx == var_count - 1)
6906 {
6907 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006908 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006909 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6910 goto theend;
6911 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006912 else
6913 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006914 // For "[var, var] = expr" get the "var_idx" item from the
6915 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006916 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006917 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006918 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006919
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006920 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006921 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006922 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006924 if ((rhs_type->tt_type == VAR_FUNC
6925 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006926 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006927 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006928 goto theend;
6929
Bram Moolenaar752fc692021-01-04 21:57:11 +01006930 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006931 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006932 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006933 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006934 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006935 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006936 }
6937 else
6938 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006939 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006940 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006941 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006942 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006943 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006944 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006945 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006946 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006947 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006948 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006949 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006950 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006951 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006952 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006953 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006954
Bram Moolenaar77709b12021-04-03 21:01:01 +02006955 // Without operator check type here, otherwise below.
6956 // Use the line number of the assignment.
6957 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006958 if (lhs.lhs_has_index)
6959 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006960 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006961 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006962 goto theend;
6963 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006964 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006965 else
6966 {
6967 type_T *lhs_type = lhs.lhs_member_type;
6968
6969 // Special case: assigning to @# can use a number or a
6970 // string.
6971 if (lhs_type == &t_number_or_string
6972 && rhs_type->tt_type == VAR_NUMBER)
6973 lhs_type = &t_number;
6974 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006975 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006976 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006979 else if (cmdidx == CMD_final)
6980 {
6981 emsg(_(e_final_requires_a_value));
6982 goto theend;
6983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006986 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006987 goto theend;
6988 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006989 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006990 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006991 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006992 goto theend;
6993 }
6994 else
6995 {
6996 // variables are always initialized
6997 if (ga_grow(instr, 1) == FAIL)
6998 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006999 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007000 {
7001 case VAR_BOOL:
7002 generate_PUSHBOOL(cctx, VVAL_FALSE);
7003 break;
7004 case VAR_FLOAT:
7005#ifdef FEAT_FLOAT
7006 generate_PUSHF(cctx, 0.0);
7007#endif
7008 break;
7009 case VAR_STRING:
7010 generate_PUSHS(cctx, NULL);
7011 break;
7012 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007013 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007014 break;
7015 case VAR_FUNC:
7016 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7017 break;
7018 case VAR_LIST:
7019 generate_NEWLIST(cctx, 0);
7020 break;
7021 case VAR_DICT:
7022 generate_NEWDICT(cctx, 0);
7023 break;
7024 case VAR_JOB:
7025 generate_PUSHJOB(cctx, NULL);
7026 break;
7027 case VAR_CHANNEL:
7028 generate_PUSHCHANNEL(cctx, NULL);
7029 break;
7030 case VAR_NUMBER:
7031 case VAR_UNKNOWN:
7032 case VAR_ANY:
7033 case VAR_PARTIAL:
7034 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007035 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007036 case VAR_SPECIAL: // cannot happen
7037 generate_PUSHNR(cctx, 0);
7038 break;
7039 }
7040 }
7041 if (var_count == 0)
7042 end = p;
7043 }
7044
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007045 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007046 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007047 break;
7048
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007049 if (oplen > 0 && *op != '=')
7050 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007051 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007052 type_T *stacktype;
7053
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007054 if (*op == '.')
7055 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007056 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007057 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007058 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007059 if (
7060#ifdef FEAT_FLOAT
7061 // If variable is float operation with number is OK.
7062 !(expected == &t_float && stacktype == &t_number) &&
7063#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007064 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007065 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007066 goto theend;
7067
7068 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007069 {
7070 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7071 goto theend;
7072 }
7073 else if (*op == '+')
7074 {
7075 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007076 operator_type(lhs.lhs_member_type, stacktype),
7077 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007078 goto theend;
7079 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007080 else if (generate_two_op(cctx, op) == FAIL)
7081 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007084 // Use the line number of the assignment for store instruction.
7085 save_lnum = cctx->ctx_lnum;
7086 cctx->ctx_lnum = start_lnum - 1;
7087
Bram Moolenaar752fc692021-01-04 21:57:11 +01007088 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007089 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007090 // Use the info in "lhs" to store the value at the index in the
7091 // list or dict.
7092 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7093 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007094 {
7095 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007096 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007098 }
7099 else
7100 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007101 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007102 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007103 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007104 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007105 generate_LOCKCONST(cctx);
7106
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007107 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007108 && (lhs.lhs_type->tt_type == VAR_DICT
7109 || lhs.lhs_type->tt_type == VAR_LIST)
7110 && lhs.lhs_type->tt_member != NULL
7111 && lhs.lhs_type->tt_member != &t_any
7112 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007113 // Set the type in the list or dict, so that it can be checked,
7114 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007115 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007117 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007118 {
7119 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007120 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007121 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007122 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007123 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007124
7125 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007126 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007128
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007129 // For "[var, var] = expr" drop the "expr" value.
7130 // Also for "[var, var; _] = expr".
7131 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007132 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007133 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007134 goto theend;
7135 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007136
Bram Moolenaarb2097502020-07-19 17:17:02 +02007137 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007140 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 return ret;
7142}
7143
7144/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007145 * Check for an assignment at "eap->cmd", compile it if found.
7146 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7147 */
7148 static int
7149may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7150{
7151 char_u *pskip;
7152 char_u *p;
7153
7154 // Assuming the command starts with a variable or function name,
7155 // find what follows.
7156 // Skip over "var.member", "var[idx]" and the like.
7157 // Also "&opt = val", "$ENV = val" and "@r = val".
7158 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7159 ? eap->cmd + 1 : eap->cmd;
7160 p = to_name_end(pskip, TRUE);
7161 if (p > eap->cmd && *p != NUL)
7162 {
7163 char_u *var_end;
7164 int oplen;
7165 int heredoc;
7166
7167 if (eap->cmd[0] == '@')
7168 var_end = eap->cmd + 2;
7169 else
7170 var_end = find_name_end(pskip, NULL, NULL,
7171 FNE_CHECK_START | FNE_INCL_BR);
7172 oplen = assignment_len(skipwhite(var_end), &heredoc);
7173 if (oplen > 0)
7174 {
7175 size_t len = p - eap->cmd;
7176
7177 // Recognize an assignment if we recognize the variable
7178 // name:
7179 // "g:var = expr"
7180 // "local = expr" where "local" is a local var.
7181 // "script = expr" where "script" is a script-local var.
7182 // "import = expr" where "import" is an imported var
7183 // "&opt = expr"
7184 // "$ENV = expr"
7185 // "@r = expr"
7186 if (*eap->cmd == '&'
7187 || *eap->cmd == '$'
7188 || *eap->cmd == '@'
7189 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007190 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007191 {
7192 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7193 if (*line == NULL || *line == eap->cmd)
7194 return FAIL;
7195 return OK;
7196 }
7197 }
7198 }
7199
7200 if (*eap->cmd == '[')
7201 {
7202 // [var, var] = expr
7203 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7204 if (*line == NULL)
7205 return FAIL;
7206 if (*line != eap->cmd)
7207 return OK;
7208 }
7209 return NOTDONE;
7210}
7211
7212/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007213 * Check if "name" can be "unlet".
7214 */
7215 int
7216check_vim9_unlet(char_u *name)
7217{
7218 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7219 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007220 // "unlet s:var" is allowed in legacy script.
7221 if (*name == 's' && !script_is_vim9())
7222 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007223 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007224 return FAIL;
7225 }
7226 return OK;
7227}
7228
7229/*
7230 * Callback passed to ex_unletlock().
7231 */
7232 static int
7233compile_unlet(
7234 lval_T *lvp,
7235 char_u *name_end,
7236 exarg_T *eap,
7237 int deep UNUSED,
7238 void *coookie)
7239{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007240 cctx_T *cctx = coookie;
7241 char_u *p = lvp->ll_name;
7242 int cc = *name_end;
7243 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007244
Bram Moolenaar752fc692021-01-04 21:57:11 +01007245 if (cctx->ctx_skip == SKIP_YES)
7246 return OK;
7247
7248 *name_end = NUL;
7249 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007250 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007251 // :unlet $ENV_VAR
7252 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7253 }
7254 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7255 {
7256 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007257
Bram Moolenaar752fc692021-01-04 21:57:11 +01007258 // This is similar to assigning: lookup the list/dict, compile the
7259 // idx/key. Then instead of storing the value unlet the item.
7260 // unlet {list}[idx]
7261 // unlet {dict}[key] dict.key
7262 //
7263 // Figure out the LHS type and other properties.
7264 //
7265 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7266
7267 // : unlet an indexed item
7268 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007269 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007270 iemsg("called compile_lhs() without an index");
7271 ret = FAIL;
7272 }
7273 else
7274 {
7275 // Use the info in "lhs" to unlet the item at the index in the
7276 // list or dict.
7277 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007278 }
7279
Bram Moolenaar752fc692021-01-04 21:57:11 +01007280 vim_free(lhs.lhs_name);
7281 }
7282 else if (check_vim9_unlet(p) == FAIL)
7283 {
7284 ret = FAIL;
7285 }
7286 else
7287 {
7288 // Normal name. Only supports g:, w:, t: and b: namespaces.
7289 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007290 }
7291
Bram Moolenaar752fc692021-01-04 21:57:11 +01007292 *name_end = cc;
7293 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007294}
7295
7296/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007297 * Callback passed to ex_unletlock().
7298 */
7299 static int
7300compile_lock_unlock(
7301 lval_T *lvp,
7302 char_u *name_end,
7303 exarg_T *eap,
7304 int deep UNUSED,
7305 void *coookie)
7306{
7307 cctx_T *cctx = coookie;
7308 int cc = *name_end;
7309 char_u *p = lvp->ll_name;
7310 int ret = OK;
7311 size_t len;
7312 char_u *buf;
7313
7314 if (cctx->ctx_skip == SKIP_YES)
7315 return OK;
7316
7317 // Cannot use :lockvar and :unlockvar on local variables.
7318 if (p[1] != ':')
7319 {
7320 char_u *end = skip_var_one(p, FALSE);
7321
7322 if (lookup_local(p, end - p, NULL, cctx) == OK)
7323 {
7324 emsg(_(e_cannot_lock_unlock_local_variable));
7325 return FAIL;
7326 }
7327 }
7328
7329 // Checking is done at runtime.
7330 *name_end = NUL;
7331 len = name_end - p + 20;
7332 buf = alloc(len);
7333 if (buf == NULL)
7334 ret = FAIL;
7335 else
7336 {
7337 vim_snprintf((char *)buf, len, "%s %s",
7338 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7339 p);
7340 ret = generate_EXEC(cctx, buf);
7341
7342 vim_free(buf);
7343 *name_end = cc;
7344 }
7345 return ret;
7346}
7347
7348/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007349 * compile "unlet var", "lock var" and "unlock var"
7350 * "arg" points to "var".
7351 */
7352 static char_u *
7353compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7354{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007355 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7356 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7357 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007358 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7359}
7360
7361/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7363 */
7364 static int
7365compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7366{
7367 garray_T *instr = &cctx->ctx_instr;
7368 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7369
7370 if (endlabel == NULL)
7371 return FAIL;
7372 endlabel->el_next = *el;
7373 *el = endlabel;
7374 endlabel->el_end_label = instr->ga_len;
7375
7376 generate_JUMP(cctx, when, 0);
7377 return OK;
7378}
7379
7380 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007381compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382{
7383 garray_T *instr = &cctx->ctx_instr;
7384
7385 while (*el != NULL)
7386 {
7387 endlabel_T *cur = (*el);
7388 isn_T *isn;
7389
7390 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007391 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 *el = cur->el_next;
7393 vim_free(cur);
7394 }
7395}
7396
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007397 static void
7398compile_free_jump_to_end(endlabel_T **el)
7399{
7400 while (*el != NULL)
7401 {
7402 endlabel_T *cur = (*el);
7403
7404 *el = cur->el_next;
7405 vim_free(cur);
7406 }
7407}
7408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409/*
7410 * Create a new scope and set up the generic items.
7411 */
7412 static scope_T *
7413new_scope(cctx_T *cctx, scopetype_T type)
7414{
7415 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7416
7417 if (scope == NULL)
7418 return NULL;
7419 scope->se_outer = cctx->ctx_scope;
7420 cctx->ctx_scope = scope;
7421 scope->se_type = type;
7422 scope->se_local_count = cctx->ctx_locals.ga_len;
7423 return scope;
7424}
7425
7426/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007427 * Free the current scope and go back to the outer scope.
7428 */
7429 static void
7430drop_scope(cctx_T *cctx)
7431{
7432 scope_T *scope = cctx->ctx_scope;
7433
7434 if (scope == NULL)
7435 {
7436 iemsg("calling drop_scope() without a scope");
7437 return;
7438 }
7439 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007440 switch (scope->se_type)
7441 {
7442 case IF_SCOPE:
7443 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7444 case FOR_SCOPE:
7445 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7446 case WHILE_SCOPE:
7447 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7448 case TRY_SCOPE:
7449 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7450 case NO_SCOPE:
7451 case BLOCK_SCOPE:
7452 break;
7453 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007454 vim_free(scope);
7455}
7456
7457/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 * compile "if expr"
7459 *
7460 * "if expr" Produces instructions:
7461 * EVAL expr Push result of "expr"
7462 * JUMP_IF_FALSE end
7463 * ... body ...
7464 * end:
7465 *
7466 * "if expr | else" Produces instructions:
7467 * EVAL expr Push result of "expr"
7468 * JUMP_IF_FALSE else
7469 * ... body ...
7470 * JUMP_ALWAYS end
7471 * else:
7472 * ... body ...
7473 * end:
7474 *
7475 * "if expr1 | elseif expr2 | else" Produces instructions:
7476 * EVAL expr Push result of "expr"
7477 * JUMP_IF_FALSE elseif
7478 * ... body ...
7479 * JUMP_ALWAYS end
7480 * elseif:
7481 * EVAL expr Push result of "expr"
7482 * JUMP_IF_FALSE else
7483 * ... body ...
7484 * JUMP_ALWAYS end
7485 * else:
7486 * ... body ...
7487 * end:
7488 */
7489 static char_u *
7490compile_if(char_u *arg, cctx_T *cctx)
7491{
7492 char_u *p = arg;
7493 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007494 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007496 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007497 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498
Bram Moolenaara5565e42020-05-09 15:44:01 +02007499 CLEAR_FIELD(ppconst);
7500 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007501 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007502 clear_ppconst(&ppconst);
7503 return NULL;
7504 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007505 if (!ends_excmd2(arg, skipwhite(p)))
7506 {
7507 semsg(_(e_trailing_arg), p);
7508 return NULL;
7509 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007510 if (cctx->ctx_skip == SKIP_YES)
7511 clear_ppconst(&ppconst);
7512 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007513 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007514 int error = FALSE;
7515 int v;
7516
Bram Moolenaara5565e42020-05-09 15:44:01 +02007517 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007518 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007519 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007520 if (error)
7521 return NULL;
7522 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007523 }
7524 else
7525 {
7526 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007527 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007528 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007529 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007530 if (bool_on_stack(cctx) == FAIL)
7531 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533
Bram Moolenaara91a7132021-03-25 21:12:15 +01007534 // CMDMOD_REV must come before the jump
7535 generate_undo_cmdmods(cctx);
7536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 scope = new_scope(cctx, IF_SCOPE);
7538 if (scope == NULL)
7539 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007540 scope->se_skip_save = skip_save;
7541 // "is_had_return" will be reset if any block does not end in :return
7542 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007544 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007545 {
7546 // "where" is set when ":elseif", "else" or ":endif" is found
7547 scope->se_u.se_if.is_if_label = instr->ga_len;
7548 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7549 }
7550 else
7551 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552
Bram Moolenaarced68a02021-01-24 17:53:47 +01007553#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007554 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007555 && skip_save != SKIP_YES)
7556 {
7557 // generated a profile start, need to generate a profile end, since it
7558 // won't be done after returning
7559 cctx->ctx_skip = SKIP_NOT;
7560 generate_instr(cctx, ISN_PROF_END);
7561 cctx->ctx_skip = SKIP_YES;
7562 }
7563#endif
7564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 return p;
7566}
7567
7568 static char_u *
7569compile_elseif(char_u *arg, cctx_T *cctx)
7570{
7571 char_u *p = arg;
7572 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007573 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 isn_T *isn;
7575 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007576 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007577 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578
7579 if (scope == NULL || scope->se_type != IF_SCOPE)
7580 {
7581 emsg(_(e_elseif_without_if));
7582 return NULL;
7583 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007584 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007585 if (!cctx->ctx_had_return)
7586 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587
Bram Moolenaarced68a02021-01-24 17:53:47 +01007588 if (cctx->ctx_skip == SKIP_NOT)
7589 {
7590 // previous block was executed, this one and following will not
7591 cctx->ctx_skip = SKIP_YES;
7592 scope->se_u.se_if.is_seen_skip_not = TRUE;
7593 }
7594 if (scope->se_u.se_if.is_seen_skip_not)
7595 {
7596 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007597 // Do not count the "elseif" for profiling and cmdmod
7598 instr->ga_len = current_instr_idx(cctx);
7599
Bram Moolenaarced68a02021-01-24 17:53:47 +01007600 skip_expr_cctx(&p, cctx);
7601 return p;
7602 }
7603
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007604 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007605 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007606 int moved_cmdmod = FALSE;
7607
7608 // Move any CMDMOD instruction to after the jump
7609 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7610 {
7611 if (ga_grow(instr, 1) == FAIL)
7612 return NULL;
7613 ((isn_T *)instr->ga_data)[instr->ga_len] =
7614 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7615 --instr->ga_len;
7616 moved_cmdmod = TRUE;
7617 }
7618
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007619 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007621 return NULL;
7622 // previous "if" or "elseif" jumps here
7623 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7624 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007625 if (moved_cmdmod)
7626 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007629 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007630 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007631 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007632 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007633 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007634#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007635 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007636 {
7637 // the previous block was skipped, need to profile this line
7638 generate_instr(cctx, ISN_PROF_START);
7639 instr_count = instr->ga_len;
7640 }
7641#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007642 if (cctx->ctx_compile_type == CT_DEBUG)
7643 {
7644 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007645 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007646 instr_count = instr->ga_len;
7647 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007648 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007649 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007650 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007651 clear_ppconst(&ppconst);
7652 return NULL;
7653 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007654 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007655 if (!ends_excmd2(arg, skipwhite(p)))
7656 {
7657 semsg(_(e_trailing_arg), p);
7658 return NULL;
7659 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007660 if (scope->se_skip_save == SKIP_YES)
7661 clear_ppconst(&ppconst);
7662 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007663 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007664 int error = FALSE;
7665 int v;
7666
Bram Moolenaar7f141552020-05-09 17:35:53 +02007667 // The expression results in a constant.
7668 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007669 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7670 if (error)
7671 return NULL;
7672 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007673 clear_ppconst(&ppconst);
7674 scope->se_u.se_if.is_if_label = -1;
7675 }
7676 else
7677 {
7678 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007679 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007680 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007681 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007682 if (bool_on_stack(cctx) == FAIL)
7683 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684
Bram Moolenaara91a7132021-03-25 21:12:15 +01007685 // CMDMOD_REV must come before the jump
7686 generate_undo_cmdmods(cctx);
7687
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007688 // "where" is set when ":elseif", "else" or ":endif" is found
7689 scope->se_u.se_if.is_if_label = instr->ga_len;
7690 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692
7693 return p;
7694}
7695
7696 static char_u *
7697compile_else(char_u *arg, cctx_T *cctx)
7698{
7699 char_u *p = arg;
7700 garray_T *instr = &cctx->ctx_instr;
7701 isn_T *isn;
7702 scope_T *scope = cctx->ctx_scope;
7703
7704 if (scope == NULL || scope->se_type != IF_SCOPE)
7705 {
7706 emsg(_(e_else_without_if));
7707 return NULL;
7708 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007709 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007710 if (!cctx->ctx_had_return)
7711 scope->se_u.se_if.is_had_return = FALSE;
7712 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713
Bram Moolenaarced68a02021-01-24 17:53:47 +01007714#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007715 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007716 {
7717 if (cctx->ctx_skip == SKIP_NOT
7718 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7719 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007720 // the previous block was executed, do not count "else" for
7721 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007722 --instr->ga_len;
7723 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7724 {
7725 // the previous block was not executed, this one will, do count the
7726 // "else" for profiling
7727 cctx->ctx_skip = SKIP_NOT;
7728 generate_instr(cctx, ISN_PROF_END);
7729 generate_instr(cctx, ISN_PROF_START);
7730 cctx->ctx_skip = SKIP_YES;
7731 }
7732 }
7733#endif
7734
7735 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007736 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007737 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007738 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007739 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007740 if (!cctx->ctx_had_return
7741 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7742 JUMP_ALWAYS, cctx) == FAIL)
7743 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007744 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007745
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007746 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007747 {
7748 if (scope->se_u.se_if.is_if_label >= 0)
7749 {
7750 // previous "if" or "elseif" jumps here
7751 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7752 isn->isn_arg.jump.jump_where = instr->ga_len;
7753 scope->se_u.se_if.is_if_label = -1;
7754 }
7755 }
7756
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007757 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007758 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760
7761 return p;
7762}
7763
7764 static char_u *
7765compile_endif(char_u *arg, cctx_T *cctx)
7766{
7767 scope_T *scope = cctx->ctx_scope;
7768 ifscope_T *ifscope;
7769 garray_T *instr = &cctx->ctx_instr;
7770 isn_T *isn;
7771
Bram Moolenaarfa984412021-03-25 22:15:28 +01007772 if (misplaced_cmdmod(cctx))
7773 return NULL;
7774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 if (scope == NULL || scope->se_type != IF_SCOPE)
7776 {
7777 emsg(_(e_endif_without_if));
7778 return NULL;
7779 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007780 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007781 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007782 if (!cctx->ctx_had_return)
7783 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007785 if (scope->se_u.se_if.is_if_label >= 0)
7786 {
7787 // previous "if" or "elseif" jumps here
7788 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7789 isn->isn_arg.jump.jump_where = instr->ga_len;
7790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007792 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007793
7794#ifdef FEAT_PROFILE
7795 // even when skipping we count the endif as executed, unless the block it's
7796 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007797 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007798 && scope->se_skip_save != SKIP_YES)
7799 {
7800 cctx->ctx_skip = SKIP_NOT;
7801 generate_instr(cctx, ISN_PROF_START);
7802 }
7803#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007804 cctx->ctx_skip = scope->se_skip_save;
7805
7806 // If all the blocks end in :return and there is an :else then the
7807 // had_return flag is set.
7808 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007810 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 return arg;
7812}
7813
7814/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007815 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 *
7817 * Produces instructions:
7818 * PUSHNR -1
7819 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007820 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7822 * - if beyond end, jump to "end"
7823 * - otherwise get item from list and push it
7824 * STORE var Store item in "var"
7825 * ... body ...
7826 * JUMP top Jump back to repeat
7827 * end: DROP Drop the result of "expr"
7828 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007829 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7830 * UNPACK 2 Split item in 2
7831 * STORE var1 Store item in "var1"
7832 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 */
7834 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007835compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007837 char_u *arg;
7838 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007839 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007841 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007842 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007843 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007844 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007847 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007849 lvar_T *loop_lvar; // loop iteration variable
7850 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007852 type_T *item_type = &t_any;
7853 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007854 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855
Bram Moolenaar792f7862020-11-23 08:31:18 +01007856 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007857 if (p == NULL)
7858 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007859 if (var_count == 0)
7860 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007861 else
7862 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863
7864 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007865 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007866 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7867 return NULL;
7868 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007870 if (*p == ':' && wp != p)
7871 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7872 else
7873 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 return NULL;
7875 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007876 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007877 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7878 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007880 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7881 // instruction.
7882 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7883 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7884 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007885 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007886 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007887 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7888 .isn_arg.debug.dbg_break_lnum;
7889 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891 scope = new_scope(cctx, FOR_SCOPE);
7892 if (scope == NULL)
7893 return NULL;
7894
Bram Moolenaar792f7862020-11-23 08:31:18 +01007895 // Reserve a variable to store the loop iteration counter and initialize it
7896 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007897 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7898 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007899 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007900 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007901 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007903 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007904 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905
7906 // compile "expr", it remains on the stack until "endfor"
7907 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007908 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007909 {
7910 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007911 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007912 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007913 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007915 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007916 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007917 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007918 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007919 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007920 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007921 semsg(_(e_for_loop_on_str_not_supported),
7922 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007923 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 return NULL;
7925 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007926
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007927 if (vartype->tt_type == VAR_STRING)
7928 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007929 else if (vartype->tt_type == VAR_BLOB)
7930 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007931 else if (vartype->tt_type == VAR_LIST
7932 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007933 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007934 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007935 item_type = vartype->tt_member;
7936 else if (vartype->tt_member->tt_type == VAR_LIST
7937 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007938 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007939 item_type = vartype->tt_member->tt_member;
7940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007942 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007943 generate_undo_cmdmods(cctx);
7944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007946 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007947
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007948 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949
Bram Moolenaar792f7862020-11-23 08:31:18 +01007950 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007951 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007952 {
7953 generate_UNPACK(cctx, var_count, semicolon);
7954 arg = skipwhite(arg + 1); // skip white after '['
7955
7956 // the list item is replaced by a number of items
7957 if (ga_grow(stack, var_count - 1) == FAIL)
7958 {
7959 drop_scope(cctx);
7960 return NULL;
7961 }
7962 --stack->ga_len;
7963 for (idx = 0; idx < var_count; ++idx)
7964 {
7965 ((type_T **)stack->ga_data)[stack->ga_len] =
7966 (semicolon && idx == 0) ? vartype : item_type;
7967 ++stack->ga_len;
7968 }
7969 }
7970
7971 for (idx = 0; idx < var_count; ++idx)
7972 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007973 assign_dest_T dest = dest_local;
7974 int opt_flags = 0;
7975 int vimvaridx = -1;
7976 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007977 type_T *lhs_type = &t_any;
7978 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007979
7980 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007981 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007982 name = vim_strnsave(arg, varlen);
7983 if (name == NULL)
7984 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007985 if (*p == ':')
7986 {
7987 p = skipwhite(p + 1);
7988 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7989 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007990
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007991 // TODO: script var not supported?
7992 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7993 &vimvaridx, &type, cctx) == FAIL)
7994 goto failed;
7995 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007996 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007997 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7998 0, 0, type, name) == FAIL)
7999 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008000 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008001 else if (varlen == 1 && *arg == '_')
8002 {
8003 // Assigning to "_": drop the value.
8004 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8005 goto failed;
8006 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008007 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008008 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008009 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008010 {
8011 semsg(_(e_variable_already_declared), arg);
8012 goto failed;
8013 }
8014
Bram Moolenaarea870692020-12-02 14:24:30 +01008015 if (STRNCMP(name, "s:", 2) == 0)
8016 {
8017 semsg(_(e_cannot_declare_script_variable_in_function), name);
8018 goto failed;
8019 }
8020
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008021 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008022 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008023 where.wt_variable = TRUE;
8024 if (lhs_type == &t_any)
8025 lhs_type = item_type;
8026 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008027 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008028 ? need_type(item_type, lhs_type,
8029 -1, 0, cctx, FALSE, FALSE)
8030 : check_type(lhs_type, item_type, TRUE, where))
8031 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008032 goto failed;
8033 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008034 if (var_lvar == NULL)
8035 // out of memory or used as an argument
8036 goto failed;
8037
8038 if (semicolon && idx == var_count - 1)
8039 var_lvar->lv_type = vartype;
8040 else
8041 var_lvar->lv_type = item_type;
8042 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8043 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008044
8045 if (*p == ',' || *p == ';')
8046 ++p;
8047 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008048 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008049 }
8050
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008051 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008052 {
8053 int save_prev_lnum = cctx->ctx_prev_lnum;
8054
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008055 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008056 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8057 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008058 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008059 cctx->ctx_prev_lnum = save_prev_lnum;
8060 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008061
Bram Moolenaar792f7862020-11-23 08:31:18 +01008062 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008063
8064failed:
8065 vim_free(name);
8066 drop_scope(cctx);
8067 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068}
8069
8070/*
8071 * compile "endfor"
8072 */
8073 static char_u *
8074compile_endfor(char_u *arg, cctx_T *cctx)
8075{
8076 garray_T *instr = &cctx->ctx_instr;
8077 scope_T *scope = cctx->ctx_scope;
8078 forscope_T *forscope;
8079 isn_T *isn;
8080
Bram Moolenaarfa984412021-03-25 22:15:28 +01008081 if (misplaced_cmdmod(cctx))
8082 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 if (scope == NULL || scope->se_type != FOR_SCOPE)
8085 {
8086 emsg(_(e_for));
8087 return NULL;
8088 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008089 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008091 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092
8093 // At end of ":for" scope jump back to the FOR instruction.
8094 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8095
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008096 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8098 isn->isn_arg.forloop.for_end = instr->ga_len;
8099
8100 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008101 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102
8103 // Below the ":for" scope drop the "expr" list from the stack.
8104 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8105 return NULL;
8106
8107 vim_free(scope);
8108
8109 return arg;
8110}
8111
8112/*
8113 * compile "while expr"
8114 *
8115 * Produces instructions:
8116 * top: EVAL expr Push result of "expr"
8117 * JUMP_IF_FALSE end jump if false
8118 * ... body ...
8119 * JUMP top Jump back to repeat
8120 * end:
8121 *
8122 */
8123 static char_u *
8124compile_while(char_u *arg, cctx_T *cctx)
8125{
8126 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 scope_T *scope;
8128
8129 scope = new_scope(cctx, WHILE_SCOPE);
8130 if (scope == NULL)
8131 return NULL;
8132
Bram Moolenaara91a7132021-03-25 21:12:15 +01008133 // "endwhile" jumps back here, one before when profiling or using cmdmods
8134 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135
8136 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008137 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008139 if (!ends_excmd2(arg, skipwhite(p)))
8140 {
8141 semsg(_(e_trailing_arg), p);
8142 return NULL;
8143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144
Bram Moolenaar13106602020-10-04 16:06:05 +02008145 if (bool_on_stack(cctx) == FAIL)
8146 return FAIL;
8147
Bram Moolenaara91a7132021-03-25 21:12:15 +01008148 // CMDMOD_REV must come before the jump
8149 generate_undo_cmdmods(cctx);
8150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008152 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 JUMP_IF_FALSE, cctx) == FAIL)
8154 return FAIL;
8155
8156 return p;
8157}
8158
8159/*
8160 * compile "endwhile"
8161 */
8162 static char_u *
8163compile_endwhile(char_u *arg, cctx_T *cctx)
8164{
8165 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008166 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167
Bram Moolenaarfa984412021-03-25 22:15:28 +01008168 if (misplaced_cmdmod(cctx))
8169 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8171 {
8172 emsg(_(e_while));
8173 return NULL;
8174 }
8175 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008176 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177
Bram Moolenaarf002a412021-01-24 13:34:18 +01008178#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008179 // count the endwhile before jumping
8180 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008181#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008184 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185
8186 // Fill in the "end" label in the WHILE statement so it can jump here.
8187 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008188 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8189 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190
8191 vim_free(scope);
8192
8193 return arg;
8194}
8195
8196/*
8197 * compile "continue"
8198 */
8199 static char_u *
8200compile_continue(char_u *arg, cctx_T *cctx)
8201{
8202 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008203 int try_scopes = 0;
8204 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205
8206 for (;;)
8207 {
8208 if (scope == NULL)
8209 {
8210 emsg(_(e_continue));
8211 return NULL;
8212 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008213 if (scope->se_type == FOR_SCOPE)
8214 {
8215 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008217 }
8218 if (scope->se_type == WHILE_SCOPE)
8219 {
8220 loop_label = scope->se_u.se_while.ws_top_label;
8221 break;
8222 }
8223 if (scope->se_type == TRY_SCOPE)
8224 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225 scope = scope->se_outer;
8226 }
8227
Bram Moolenaarc150c092021-02-13 15:02:46 +01008228 if (try_scopes > 0)
8229 // Inside one or more try/catch blocks we first need to jump to the
8230 // "finally" or "endtry" to cleanup.
8231 generate_TRYCONT(cctx, try_scopes, loop_label);
8232 else
8233 // Jump back to the FOR or WHILE instruction.
8234 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 return arg;
8237}
8238
8239/*
8240 * compile "break"
8241 */
8242 static char_u *
8243compile_break(char_u *arg, cctx_T *cctx)
8244{
8245 scope_T *scope = cctx->ctx_scope;
8246 endlabel_T **el;
8247
8248 for (;;)
8249 {
8250 if (scope == NULL)
8251 {
8252 emsg(_(e_break));
8253 return NULL;
8254 }
8255 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8256 break;
8257 scope = scope->se_outer;
8258 }
8259
8260 // Jump to the end of the FOR or WHILE loop.
8261 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008262 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008264 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8266 return FAIL;
8267
8268 return arg;
8269}
8270
8271/*
8272 * compile "{" start of block
8273 */
8274 static char_u *
8275compile_block(char_u *arg, cctx_T *cctx)
8276{
8277 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8278 return NULL;
8279 return skipwhite(arg + 1);
8280}
8281
8282/*
8283 * compile end of block: drop one scope
8284 */
8285 static void
8286compile_endblock(cctx_T *cctx)
8287{
8288 scope_T *scope = cctx->ctx_scope;
8289
8290 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008291 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008292 vim_free(scope);
8293}
8294
8295/*
8296 * compile "try"
8297 * Creates a new scope for the try-endtry, pointing to the first catch and
8298 * finally.
8299 * Creates another scope for the "try" block itself.
8300 * TRY instruction sets up exception handling at runtime.
8301 *
8302 * "try"
8303 * TRY -> catch1, -> finally push trystack entry
8304 * ... try block
8305 * "throw {exception}"
8306 * EVAL {exception}
8307 * THROW create exception
8308 * ... try block
8309 * " catch {expr}"
8310 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008311 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 * EVAL {expr}
8313 * MATCH
8314 * JUMP nomatch -> catch2
8315 * CATCH remove exception
8316 * ... catch block
8317 * " catch"
8318 * JUMP -> finally
8319 * catch2: CATCH remove exception
8320 * ... catch block
8321 * " finally"
8322 * finally:
8323 * ... finally block
8324 * " endtry"
8325 * ENDTRY pop trystack entry, may rethrow
8326 */
8327 static char_u *
8328compile_try(char_u *arg, cctx_T *cctx)
8329{
8330 garray_T *instr = &cctx->ctx_instr;
8331 scope_T *try_scope;
8332 scope_T *scope;
8333
Bram Moolenaarfa984412021-03-25 22:15:28 +01008334 if (misplaced_cmdmod(cctx))
8335 return NULL;
8336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337 // scope that holds the jumps that go to catch/finally/endtry
8338 try_scope = new_scope(cctx, TRY_SCOPE);
8339 if (try_scope == NULL)
8340 return NULL;
8341
Bram Moolenaar69f70502021-01-01 16:10:46 +01008342 if (cctx->ctx_skip != SKIP_YES)
8343 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008344 isn_T *isn;
8345
8346 // "try_catch" is set when the first ":catch" is found or when no catch
8347 // is found and ":finally" is found.
8348 // "try_finally" is set when ":finally" is found
8349 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008350 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008351 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8352 return NULL;
8353 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8354 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008355 return NULL;
8356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357
8358 // scope for the try block itself
8359 scope = new_scope(cctx, BLOCK_SCOPE);
8360 if (scope == NULL)
8361 return NULL;
8362
8363 return arg;
8364}
8365
8366/*
8367 * compile "catch {expr}"
8368 */
8369 static char_u *
8370compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8371{
8372 scope_T *scope = cctx->ctx_scope;
8373 garray_T *instr = &cctx->ctx_instr;
8374 char_u *p;
8375 isn_T *isn;
8376
Bram Moolenaarfa984412021-03-25 22:15:28 +01008377 if (misplaced_cmdmod(cctx))
8378 return NULL;
8379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008380 // end block scope from :try or :catch
8381 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8382 compile_endblock(cctx);
8383 scope = cctx->ctx_scope;
8384
8385 // Error if not in a :try scope
8386 if (scope == NULL || scope->se_type != TRY_SCOPE)
8387 {
8388 emsg(_(e_catch));
8389 return NULL;
8390 }
8391
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008392 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008393 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008394 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 return NULL;
8396 }
8397
Bram Moolenaar69f70502021-01-01 16:10:46 +01008398 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008399 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008400#ifdef FEAT_PROFILE
8401 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008402 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008403 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008404 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008405 .isn_type == ISN_PROF_START)
8406 --instr->ga_len;
8407#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008408 // Jump from end of previous block to :finally or :endtry
8409 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8410 JUMP_ALWAYS, cctx) == FAIL)
8411 return NULL;
8412
8413 // End :try or :catch scope: set value in ISN_TRY instruction
8414 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008415 if (isn->isn_arg.try.try_ref->try_catch == 0)
8416 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008417 if (scope->se_u.se_try.ts_catch_label != 0)
8418 {
8419 // Previous catch without match jumps here
8420 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8421 isn->isn_arg.jump.jump_where = instr->ga_len;
8422 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008423#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008424 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008425 {
8426 // a "throw" that jumps here needs to be counted
8427 generate_instr(cctx, ISN_PROF_END);
8428 // the "catch" is also counted
8429 generate_instr(cctx, ISN_PROF_START);
8430 }
8431#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008432 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008433 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 }
8435
8436 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008437 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008439 scope->se_u.se_try.ts_caught_all = TRUE;
8440 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 }
8442 else
8443 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008444 char_u *end;
8445 char_u *pat;
8446 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008447 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008448 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 // Push v:exception, push {expr} and MATCH
8451 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8452
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008453 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008454 if (*end != *p)
8455 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008456 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008457 vim_free(tofree);
8458 return FAIL;
8459 }
8460 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008461 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008462 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008463 len = (int)(end - tofree);
8464 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008465 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008466 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008467 if (pat == NULL)
8468 return FAIL;
8469 if (generate_PUSHS(cctx, pat) == FAIL)
8470 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8473 return NULL;
8474
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008475 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8477 return NULL;
8478 }
8479
Bram Moolenaar69f70502021-01-01 16:10:46 +01008480 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481 return NULL;
8482
8483 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8484 return NULL;
8485 return p;
8486}
8487
8488 static char_u *
8489compile_finally(char_u *arg, cctx_T *cctx)
8490{
8491 scope_T *scope = cctx->ctx_scope;
8492 garray_T *instr = &cctx->ctx_instr;
8493 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008494 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495
Bram Moolenaarfa984412021-03-25 22:15:28 +01008496 if (misplaced_cmdmod(cctx))
8497 return NULL;
8498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 // end block scope from :try or :catch
8500 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8501 compile_endblock(cctx);
8502 scope = cctx->ctx_scope;
8503
8504 // Error if not in a :try scope
8505 if (scope == NULL || scope->se_type != TRY_SCOPE)
8506 {
8507 emsg(_(e_finally));
8508 return NULL;
8509 }
8510
8511 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008512 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008513 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 {
8515 emsg(_(e_finally_dup));
8516 return NULL;
8517 }
8518
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008519 this_instr = instr->ga_len;
8520#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008521 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008522 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008523 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008524 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008525 // jump to the profile start of the "finally"
8526 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008527
8528 // jump to the profile end above it
8529 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8530 .isn_type == ISN_PROF_END)
8531 --this_instr;
8532 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008533#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008535 // Fill in the "end" label in jumps at the end of the blocks.
8536 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8537 this_instr, cctx);
8538
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008539 // If there is no :catch then an exception jumps to :finally.
8540 if (isn->isn_arg.try.try_ref->try_catch == 0)
8541 isn->isn_arg.try.try_ref->try_catch = this_instr;
8542 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008543 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 {
8545 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008546 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008547 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008548 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008549 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008550 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8551 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008553 // TODO: set index in ts_finally_label jumps
8554
8555 return arg;
8556}
8557
8558 static char_u *
8559compile_endtry(char_u *arg, cctx_T *cctx)
8560{
8561 scope_T *scope = cctx->ctx_scope;
8562 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008563 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564
Bram Moolenaarfa984412021-03-25 22:15:28 +01008565 if (misplaced_cmdmod(cctx))
8566 return NULL;
8567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 // end block scope from :catch or :finally
8569 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8570 compile_endblock(cctx);
8571 scope = cctx->ctx_scope;
8572
8573 // Error if not in a :try scope
8574 if (scope == NULL || scope->se_type != TRY_SCOPE)
8575 {
8576 if (scope == NULL)
8577 emsg(_(e_no_endtry));
8578 else if (scope->se_type == WHILE_SCOPE)
8579 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008580 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 emsg(_(e_endfor));
8582 else
8583 emsg(_(e_endif));
8584 return NULL;
8585 }
8586
Bram Moolenaarc150c092021-02-13 15:02:46 +01008587 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008588 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008590 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8591 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008592 {
8593 emsg(_(e_missing_catch_or_finally));
8594 return NULL;
8595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008597#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008598 if (cctx->ctx_compile_type == CT_PROFILE
8599 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008600 .isn_type == ISN_PROF_START)
8601 // move the profile start after "endtry" so that it's not counted when
8602 // the exception is rethrown.
8603 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008604#endif
8605
Bram Moolenaar69f70502021-01-01 16:10:46 +01008606 // Fill in the "end" label in jumps at the end of the blocks, if not
8607 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008608 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8609 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610
Bram Moolenaar69f70502021-01-01 16:10:46 +01008611 if (scope->se_u.se_try.ts_catch_label != 0)
8612 {
8613 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008614 isn_T *isn = ((isn_T *)instr->ga_data)
8615 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008616 isn->isn_arg.jump.jump_where = instr->ga_len;
8617 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008618 }
8619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620 compile_endblock(cctx);
8621
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008622 if (cctx->ctx_skip != SKIP_YES)
8623 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008624 // End :catch or :finally scope: set instruction index in ISN_TRY
8625 // instruction
8626 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008627 if (cctx->ctx_skip != SKIP_YES
8628 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8629 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008630#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008631 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008632 generate_instr(cctx, ISN_PROF_START);
8633#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 return arg;
8636}
8637
8638/*
8639 * compile "throw {expr}"
8640 */
8641 static char_u *
8642compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8643{
8644 char_u *p = skipwhite(arg);
8645
Bram Moolenaara5565e42020-05-09 15:44:01 +02008646 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008648 if (cctx->ctx_skip == SKIP_YES)
8649 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008650 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 return NULL;
8652 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8653 return NULL;
8654
8655 return p;
8656}
8657
Bram Moolenaarc3235272021-07-10 19:42:03 +02008658 static char_u *
8659compile_eval(char_u *arg, cctx_T *cctx)
8660{
8661 char_u *p = arg;
8662 int name_only;
8663 char_u *alias;
8664 long lnum = SOURCING_LNUM;
8665
8666 // find_ex_command() will consider a variable name an expression, assuming
8667 // that something follows on the next line. Check that something actually
8668 // follows, otherwise it's probably a misplaced command.
8669 get_name_len(&p, &alias, FALSE, FALSE);
8670 name_only = ends_excmd2(arg, skipwhite(p));
8671 vim_free(alias);
8672
8673 p = arg;
8674 if (compile_expr0(&p, cctx) == FAIL)
8675 return NULL;
8676
8677 if (name_only && lnum == SOURCING_LNUM)
8678 {
8679 semsg(_(e_expression_without_effect_str), arg);
8680 return NULL;
8681 }
8682
8683 // drop the result
8684 generate_instr_drop(cctx, ISN_DROP, 1);
8685
8686 return skipwhite(p);
8687}
8688
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008689/*
8690 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008691 * compile "echomsg expr"
8692 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008693 * compile "execute expr"
8694 */
8695 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008696compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008697{
8698 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008699 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008700 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008701 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008702 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008703 garray_T *stack = &cctx->ctx_type_stack;
8704 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008705
8706 for (;;)
8707 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008708 if (ends_excmd2(prev, p))
8709 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008710 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008711 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008712 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008713
8714 if (cctx->ctx_skip != SKIP_YES)
8715 {
8716 // check for non-void type
8717 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8718 if (type->tt_type == VAR_VOID)
8719 {
8720 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8721 return NULL;
8722 }
8723 }
8724
Bram Moolenaarad39c092020-02-26 18:23:43 +01008725 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008726 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008727 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008728 }
8729
Bram Moolenaare4984292020-12-13 14:19:25 +01008730 if (count > 0)
8731 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008732 long save_lnum = cctx->ctx_lnum;
8733
8734 // Use the line number where the command started.
8735 cctx->ctx_lnum = start_ctx_lnum;
8736
Bram Moolenaare4984292020-12-13 14:19:25 +01008737 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8738 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8739 else if (cmdidx == CMD_execute)
8740 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8741 else if (cmdidx == CMD_echomsg)
8742 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8743 else
8744 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008745
8746 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008748 return p;
8749}
8750
8751/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008752 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008753 * instruction to compute it and return OK.
8754 * Otherwise return FAIL, the caller must deal with any range.
8755 */
8756 static int
8757compile_variable_range(exarg_T *eap, cctx_T *cctx)
8758{
8759 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8760 char_u *p = skipdigits(eap->cmd);
8761
8762 if (p == range_end)
8763 return FAIL;
8764 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8765}
8766
8767/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008768 * :put r
8769 * :put ={expr}
8770 */
8771 static char_u *
8772compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8773{
8774 char_u *line = arg;
8775 linenr_T lnum;
8776 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008777 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008778
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008779 eap->regname = *line;
8780
8781 if (eap->regname == '=')
8782 {
8783 char_u *p = line + 1;
8784
8785 if (compile_expr0(&p, cctx) == FAIL)
8786 return NULL;
8787 line = p;
8788 }
8789 else if (eap->regname != NUL)
8790 ++line;
8791
Bram Moolenaar08597872020-12-10 19:43:40 +01008792 if (compile_variable_range(eap, cctx) == OK)
8793 {
8794 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8795 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008796 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008797 {
8798 // Either no range or a number.
8799 // "errormsg" will not be set because the range is ADDR_LINES.
8800 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008801 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008802 return NULL;
8803 if (eap->addr_count == 0)
8804 lnum = -1;
8805 else
8806 lnum = eap->line2;
8807 if (above)
8808 --lnum;
8809 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008810
8811 generate_PUT(cctx, eap->regname, lnum);
8812 return line;
8813}
8814
8815/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008816 * A command that is not compiled, execute with legacy code.
8817 */
8818 static char_u *
8819compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8820{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008821 char_u *p;
8822 int has_expr = FALSE;
8823 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008824
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008825 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008826 goto theend;
8827
Bram Moolenaare729ce22021-06-06 21:38:09 +02008828 // If there was a prececing command modifier, drop it and include it in the
8829 // EXEC command.
8830 if (cctx->ctx_has_cmdmod)
8831 {
8832 garray_T *instr = &cctx->ctx_instr;
8833 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8834
8835 if (isn->isn_type == ISN_CMDMOD)
8836 {
8837 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8838 ->cmod_filter_regmatch.regprog);
8839 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8840 --instr->ga_len;
8841 cctx->ctx_has_cmdmod = FALSE;
8842 }
8843 }
8844
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008845 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008846 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008847 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008848 int usefilter = FALSE;
8849
8850 has_expr = argt & (EX_XFILE | EX_EXPAND);
8851
8852 // If the command can be followed by a bar, find the bar and truncate
8853 // it, so that the following command can be compiled.
8854 // The '|' is overwritten with a NUL, it is put back below.
8855 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8856 && *eap->arg == '!')
8857 // :w !filter or :r !filter or :r! filter
8858 usefilter = TRUE;
8859 if ((argt & EX_TRLBAR) && !usefilter)
8860 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008861 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008862 separate_nextcmd(eap);
8863 if (eap->nextcmd != NULL)
8864 nextcmd = eap->nextcmd;
8865 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008866 else if (eap->cmdidx == CMD_wincmd)
8867 {
8868 p = eap->arg;
8869 if (*p != NUL)
8870 ++p;
8871 if (*p == 'g' || *p == Ctrl_G)
8872 ++p;
8873 p = skipwhite(p);
8874 if (*p == '|')
8875 {
8876 *p = NUL;
8877 nextcmd = p + 1;
8878 }
8879 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008880 }
8881
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008882 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8883 {
8884 // expand filename in "syntax include [@group] filename"
8885 has_expr = TRUE;
8886 eap->arg = skipwhite(eap->arg + 7);
8887 if (*eap->arg == '@')
8888 eap->arg = skiptowhite(eap->arg);
8889 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008890
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008891 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8892 && STRLEN(eap->arg) > 4)
8893 {
8894 int delim = *eap->arg;
8895
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008896 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008897 if (*p == delim)
8898 {
8899 eap->arg = p + 1;
8900 has_expr = TRUE;
8901 }
8902 }
8903
Bram Moolenaarecac5912021-01-05 19:23:28 +01008904 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8905 {
8906 // TODO: should only expand when appropriate for the command
8907 eap->arg = skiptowhite(eap->arg);
8908 has_expr = TRUE;
8909 }
8910
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008911 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008912 {
8913 int count = 0;
8914 char_u *start = skipwhite(line);
8915
8916 // :cmd xxx`=expr1`yyy`=expr2`zzz
8917 // PUSHS ":cmd xxx"
8918 // eval expr1
8919 // PUSHS "yyy"
8920 // eval expr2
8921 // PUSHS "zzz"
8922 // EXECCONCAT 5
8923 for (;;)
8924 {
8925 if (p > start)
8926 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008927 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008928 ++count;
8929 }
8930 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008931 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008932 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008933 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008934 ++count;
8935 p = skipwhite(p);
8936 if (*p != '`')
8937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008938 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008939 return NULL;
8940 }
8941 start = p + 1;
8942
8943 p = (char_u *)strstr((char *)start, "`=");
8944 if (p == NULL)
8945 {
8946 if (*skipwhite(start) != NUL)
8947 {
8948 generate_PUSHS(cctx, vim_strsave(start));
8949 ++count;
8950 }
8951 break;
8952 }
8953 }
8954 generate_EXECCONCAT(cctx, count);
8955 }
8956 else
8957 generate_EXEC(cctx, line);
8958
8959theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008960 if (*nextcmd != NUL)
8961 {
8962 // the parser expects a pointer to the bar, put it back
8963 --nextcmd;
8964 *nextcmd = '|';
8965 }
8966
8967 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008968}
8969
Bram Moolenaar20677332021-06-06 17:02:53 +02008970/*
8971 * A script command with heredoc, e.g.
8972 * ruby << EOF
8973 * command
8974 * EOF
8975 * Has been turned into one long line with NL characters by
8976 * get_function_body():
8977 * ruby << EOF<NL> command<NL>EOF
8978 */
8979 static char_u *
8980compile_script(char_u *line, cctx_T *cctx)
8981{
8982 if (cctx->ctx_skip != SKIP_YES)
8983 {
8984 isn_T *isn;
8985
8986 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8987 return NULL;
8988 isn->isn_arg.string = vim_strsave(line);
8989 }
8990 return (char_u *)"";
8991}
8992
Bram Moolenaar8238f082021-04-20 21:10:48 +02008993
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008994/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008995 * :s/pat/repl/
8996 */
8997 static char_u *
8998compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8999{
9000 char_u *cmd = eap->arg;
9001 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9002
9003 if (expr != NULL)
9004 {
9005 int delimiter = *cmd++;
9006
9007 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009008 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009009 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9010 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009011 garray_T save_ga = cctx->ctx_instr;
9012 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009013 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009014 int trailing_error;
9015 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009016 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009017 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009018
9019 cmd += 3;
9020 end = skip_substitute(cmd, delimiter);
9021
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009022 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009023 // labels are correct.
9024 cctx->ctx_instr.ga_len = 0;
9025 cctx->ctx_instr.ga_maxlen = 0;
9026 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009027 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009028 if (end[-1] == NUL)
9029 end[-1] = delimiter;
9030 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009031 trailing_error = *cmd != delimiter && *cmd != NUL;
9032
Bram Moolenaar169502f2021-04-21 12:19:35 +02009033 if (expr_res == FAIL || trailing_error
9034 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009035 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009036 if (trailing_error)
9037 semsg(_(e_trailing_arg), cmd);
9038 clear_instr_ga(&cctx->ctx_instr);
9039 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009040 return NULL;
9041 }
9042
Bram Moolenaar8238f082021-04-20 21:10:48 +02009043 // Move the generated instructions into the ISN_SUBSTITUTE
9044 // instructions, then restore the list of instructions before
9045 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009046 instr_count = cctx->ctx_instr.ga_len;
9047 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009048 instr[instr_count].isn_type = ISN_FINISH;
9049
9050 cctx->ctx_instr = save_ga;
9051 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9052 {
9053 int idx;
9054
9055 for (idx = 0; idx < instr_count; ++idx)
9056 delete_instr(instr + idx);
9057 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009058 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009059 }
9060 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9061 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009062
9063 // skip over flags
9064 if (*end == '&')
9065 ++end;
9066 while (ASCII_ISALPHA(*end) || *end == '#')
9067 ++end;
9068 return end;
9069 }
9070 }
9071
9072 return compile_exec(arg, eap, cctx);
9073}
9074
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009075 static char_u *
9076compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9077{
9078 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009079 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009080
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009081 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009082 {
9083 if (STRNCMP(arg, "END", 3) == 0)
9084 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009085 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009086 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009087 // First load the current variable value.
9088 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9089 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009090 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009091 }
9092
9093 // Gets the redirected text and put it on the stack, then store it
9094 // in the variable.
9095 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9096
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009097 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009098 generate_instr_drop(cctx, ISN_CONCAT, 1);
9099
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009100 if (lhs->lhs_has_index)
9101 {
9102 // Use the info in "lhs" to store the value at the index in the
9103 // list or dict.
9104 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9105 &t_string, cctx) == FAIL)
9106 return NULL;
9107 }
9108 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009109 return NULL;
9110
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009111 VIM_CLEAR(lhs->lhs_name);
9112 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009113 return arg + 3;
9114 }
9115 emsg(_(e_cannot_nest_redir));
9116 return NULL;
9117 }
9118
9119 if (arg[0] == '=' && arg[1] == '>')
9120 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009121 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009122
9123 // redirect to a variable is compiled
9124 arg += 2;
9125 if (*arg == '>')
9126 {
9127 ++arg;
9128 append = TRUE;
9129 }
9130 arg = skipwhite(arg);
9131
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009132 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009133 FALSE, FALSE, 1, cctx) == FAIL)
9134 return NULL;
9135 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009136 lhs->lhs_append = append;
9137 if (lhs->lhs_has_index)
9138 {
9139 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9140 if (lhs->lhs_whole == NULL)
9141 return NULL;
9142 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009143
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009144 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009145 }
9146
9147 // other redirects are handled like at script level
9148 return compile_exec(line, eap, cctx);
9149}
9150
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009151#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009152 static char_u *
9153compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9154{
9155 isn_T *isn;
9156 char_u *p;
9157
9158 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9159 if (isn == NULL)
9160 return NULL;
9161 isn->isn_arg.number = eap->cmdidx;
9162
9163 p = eap->arg;
9164 if (compile_expr0(&p, cctx) == FAIL)
9165 return NULL;
9166
9167 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9168 if (isn == NULL)
9169 return NULL;
9170 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9171 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9172 return NULL;
9173 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9174 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9175 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9176
9177 return p;
9178}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009179#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009180
Bram Moolenaar4c137212021-04-19 16:48:48 +02009181/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009182 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009183 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009184 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009185 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009186add_def_function(ufunc_T *ufunc)
9187{
9188 dfunc_T *dfunc;
9189
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009190 if (def_functions.ga_len == 0)
9191 {
9192 // The first position is not used, so that a zero uf_dfunc_idx means it
9193 // wasn't set.
9194 if (ga_grow(&def_functions, 1) == FAIL)
9195 return FAIL;
9196 ++def_functions.ga_len;
9197 }
9198
Bram Moolenaar09689a02020-05-09 22:50:08 +02009199 // Add the function to "def_functions".
9200 if (ga_grow(&def_functions, 1) == FAIL)
9201 return FAIL;
9202 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9203 CLEAR_POINTER(dfunc);
9204 dfunc->df_idx = def_functions.ga_len;
9205 ufunc->uf_dfunc_idx = dfunc->df_idx;
9206 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009207 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009208 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009209 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009210 ++def_functions.ga_len;
9211 return OK;
9212}
9213
9214/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009215 * After ex_function() has collected all the function lines: parse and compile
9216 * the lines into instructions.
9217 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009218 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9219 * the return statement (used for lambda). When uf_ret_type is already set
9220 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009221 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009222 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009223 * This can be used recursively through compile_lambda(), which may reallocate
9224 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009225 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009226 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009227 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009228compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009229 ufunc_T *ufunc,
9230 int check_return_type,
9231 compiletype_T compile_type,
9232 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009233{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009234 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009235 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009237 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 cctx_T cctx;
9239 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009240 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009241 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 int ret = FAIL;
9243 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009244 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009245 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009246 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009247 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009248#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009249 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009250#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009251 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009253 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009254 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009255 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009256 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009257 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9258 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009259 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009260
9261 switch (compile_type)
9262 {
9263 case CT_PROFILE:
9264#ifdef FEAT_PROFILE
9265 instr_dest = dfunc->df_instr_prof; break;
9266#endif
9267 case CT_NONE: instr_dest = dfunc->df_instr; break;
9268 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9269 }
9270 if (instr_dest != NULL)
9271 // Was compiled in this mode before: Free old instructions.
9272 delete_def_function_contents(dfunc, FALSE);
9273 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009274 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009275 else
9276 {
9277 if (add_def_function(ufunc) == FAIL)
9278 return FAIL;
9279 new_def_function = TRUE;
9280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281
Bram Moolenaar985116a2020-07-12 17:31:09 +02009282 ufunc->uf_def_status = UF_COMPILING;
9283
Bram Moolenaara80faa82020-04-12 19:37:17 +02009284 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009285
Bram Moolenaare99d4222021-06-13 14:01:26 +02009286 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009287 cctx.ctx_ufunc = ufunc;
9288 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009289 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9291 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9292 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9293 cctx.ctx_type_list = &ufunc->uf_type_list;
9294 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9295 instr = &cctx.ctx_instr;
9296
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009297 // Set the context to the function, it may be compiled when called from
9298 // another script. Set the script version to the most modern one.
9299 // The line number will be set in next_line_from_context().
9300 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009301 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9302
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009303 // Don't use the flag from ":legacy" here.
9304 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9305
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009306 // Make sure error messages are OK.
9307 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9308 if (do_estack_push)
9309 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009310 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009311
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009312 if (ufunc->uf_def_args.ga_len > 0)
9313 {
9314 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009315 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009316 int i;
9317 char_u *arg;
9318 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009319 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009320
9321 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009322 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009323 for (i = 0; i < count; ++i)
9324 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009325 garray_T *stack = &cctx.ctx_type_stack;
9326 type_T *val_type;
9327 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009328 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009329 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009330 int jump_instr_idx = instr->ga_len;
9331 isn_T *isn;
9332
9333 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9334 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9335 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009336
9337 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009338 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009339
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009340 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009341 r = compile_expr0(&arg, &cctx);
9342
Bram Moolenaar12bce952021-03-11 20:04:04 +01009343 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009344 goto erret;
9345
9346 // If no type specified use the type of the default value.
9347 // Otherwise check that the default value type matches the
9348 // specified type.
9349 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009350 where.wt_index = arg_idx + 1;
9351 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009352 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009353 {
9354 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009355 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009356 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009357 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009358 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009359 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009360
9361 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009362 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009363
9364 // set instruction index in JUMP_IF_ARG_SET to here
9365 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9366 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009367 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009368
9369 if (did_set_arg_type)
9370 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009371 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009372 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009373
9374 /*
9375 * Loop over all the lines of the function and generate instructions.
9376 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009377 for (;;)
9378 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009379 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009380 int starts_with_colon = FALSE;
9381 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009382 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009383
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009384 // Bail out on the first error to avoid a flood of errors and report
9385 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009386 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009387 goto erret;
9388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009389 if (line != NULL && *line == '|')
9390 // the line continues after a '|'
9391 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009392 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009393 && !(*line == '#' && (line == cctx.ctx_line_start
9394 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009395 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009396 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009397 goto erret;
9398 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009399 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9400 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009401 else
9402 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009403 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009404 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009405 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009406 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009407#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009408 if (cctx.ctx_skip != SKIP_YES)
9409 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009410#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009411 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009412 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009413 // Make a copy, splitting off nextcmd and removing trailing spaces
9414 // may change it.
9415 if (line != NULL)
9416 {
9417 line = vim_strsave(line);
9418 vim_free(line_to_free);
9419 line_to_free = line;
9420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009421 }
9422
Bram Moolenaara80faa82020-04-12 19:37:17 +02009423 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009424 ea.cmdlinep = &line;
9425 ea.cmd = skipwhite(line);
9426
Bram Moolenaarb2049902021-01-24 12:53:53 +01009427 if (*ea.cmd == '#')
9428 {
9429 // "#" starts a comment
9430 line = (char_u *)"";
9431 continue;
9432 }
9433
Bram Moolenaarf002a412021-01-24 13:34:18 +01009434#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009435 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9436 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009437 {
9438 may_generate_prof_end(&cctx, prof_lnum);
9439
9440 prof_lnum = cctx.ctx_lnum;
9441 generate_instr(&cctx, ISN_PROF_START);
9442 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009443#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009444 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9445 && cctx.ctx_skip != SKIP_YES)
9446 {
9447 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009448 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009449 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009450 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009451
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009452 // Some things can be recognized by the first character.
9453 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009454 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009455 case '}':
9456 {
9457 // "}" ends a block scope
9458 scopetype_T stype = cctx.ctx_scope == NULL
9459 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009460
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009461 if (stype == BLOCK_SCOPE)
9462 {
9463 compile_endblock(&cctx);
9464 line = ea.cmd;
9465 }
9466 else
9467 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009468 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009469 goto erret;
9470 }
9471 if (line != NULL)
9472 line = skipwhite(ea.cmd + 1);
9473 continue;
9474 }
9475
9476 case '{':
9477 // "{" starts a block scope
9478 // "{'a': 1}->func() is something else
9479 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9480 {
9481 line = compile_block(ea.cmd, &cctx);
9482 continue;
9483 }
9484 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009485 }
9486
9487 /*
9488 * COMMAND MODIFIERS
9489 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009490 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009491 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9492 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009493 {
9494 if (errormsg != NULL)
9495 goto erret;
9496 // empty line or comment
9497 line = (char_u *)"";
9498 continue;
9499 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009500 generate_cmdmods(&cctx, &local_cmdmod);
9501 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009502
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009503 // Check if there was a colon after the last command modifier or before
9504 // the current position.
9505 for (p = ea.cmd; p >= line; --p)
9506 {
9507 if (*p == ':')
9508 starts_with_colon = TRUE;
9509 if (p < ea.cmd && !VIM_ISWHITE(*p))
9510 break;
9511 }
9512
Bram Moolenaarce024c32021-06-26 13:00:49 +02009513 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009514 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009515 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009516 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009517 if (checkforcmd(&ea.cmd, "call", 3))
9518 {
9519 if (*ea.cmd == '(')
9520 // not for "call()"
9521 ea.cmd = p;
9522 else
9523 ea.cmd = skipwhite(ea.cmd);
9524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009525
Bram Moolenaarce024c32021-06-26 13:00:49 +02009526 if (!starts_with_colon)
9527 {
9528 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009529
Bram Moolenaarce024c32021-06-26 13:00:49 +02009530 // Check for assignment after command modifiers.
9531 assign = may_compile_assignment(&ea, &line, &cctx);
9532 if (assign == OK)
9533 goto nextline;
9534 if (assign == FAIL)
9535 goto erret;
9536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009537 }
9538
9539 /*
9540 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009541 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009542 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009543 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009544 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009545 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9546 && (starts_with_colon || !(*cmd == '\''
9547 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009548 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009549 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009550 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009551 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009552 if (!starts_with_colon)
9553 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009554 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009555 goto erret;
9556 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009557 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009558 if (ends_excmd2(line, ea.cmd))
9559 {
9560 // A range without a command: jump to the line.
9561 // TODO: compile to a more efficient command, possibly
9562 // calling parse_cmd_address().
9563 ea.cmdidx = CMD_SIZE;
9564 line = compile_exec(line, &ea, &cctx);
9565 goto nextline;
9566 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009567 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009568 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009569 p = find_ex_command(&ea, NULL,
9570 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009571 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009572
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009573 if (p == NULL)
9574 {
9575 if (cctx.ctx_skip != SKIP_YES)
9576 emsg(_(e_ambiguous_use_of_user_defined_command));
9577 goto erret;
9578 }
9579
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009580 // When using ":legacy cmd" always use compile_exec().
9581 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009582 {
9583 char_u *start = ea.cmd;
9584
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009585 switch (ea.cmdidx)
9586 {
9587 case CMD_if:
9588 case CMD_elseif:
9589 case CMD_else:
9590 case CMD_endif:
9591 case CMD_for:
9592 case CMD_endfor:
9593 case CMD_continue:
9594 case CMD_break:
9595 case CMD_while:
9596 case CMD_endwhile:
9597 case CMD_try:
9598 case CMD_catch:
9599 case CMD_finally:
9600 case CMD_endtry:
9601 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9602 goto erret;
9603 default: break;
9604 }
9605
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009606 // ":legacy return expr" needs to be handled differently.
9607 if (checkforcmd(&start, "return", 4))
9608 ea.cmdidx = CMD_return;
9609 else
9610 ea.cmdidx = CMD_legacy;
9611 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9614 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009615 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009616 {
9617 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009618 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009619 }
9620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009621 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009622 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009623 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009624 // CMD_var cannot happen, compile_assignment() above would be
9625 // used. Most likely an assignment to a non-existing variable.
9626 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009627 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009629 }
9630
Bram Moolenaar3988f642020-08-27 22:43:03 +02009631 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009632 && ea.cmdidx != CMD_elseif
9633 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009634 && ea.cmdidx != CMD_endif
9635 && ea.cmdidx != CMD_endfor
9636 && ea.cmdidx != CMD_endwhile
9637 && ea.cmdidx != CMD_catch
9638 && ea.cmdidx != CMD_finally
9639 && ea.cmdidx != CMD_endtry)
9640 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009641 emsg(_(e_unreachable_code_after_return));
9642 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009643 }
9644
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009645 p = skipwhite(p);
9646 if (ea.cmdidx != CMD_SIZE
9647 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9648 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009649 if (ea.cmdidx >= 0)
9650 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009651 if ((ea.argt & EX_BANG) && *p == '!')
9652 {
9653 ea.forceit = TRUE;
9654 p = skipwhite(p + 1);
9655 }
9656 }
9657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 switch (ea.cmdidx)
9659 {
9660 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009661 ea.arg = p;
9662 line = compile_nested_function(&ea, &cctx);
9663 break;
9664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009665 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009666 // TODO: should we allow this, e.g. to declare a global
9667 // function?
9668 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009669 goto erret;
9670
9671 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009672 line = compile_return(p, check_return_type,
9673 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009674 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009675 break;
9676
9677 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009678 emsg(_(e_cannot_use_let_in_vim9_script));
9679 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009680 case CMD_var:
9681 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009682 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009683 case CMD_increment:
9684 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009685 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009686 if (line == p)
9687 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009688 break;
9689
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009690 case CMD_unlet:
9691 case CMD_unlockvar:
9692 case CMD_lockvar:
9693 line = compile_unletlock(p, &ea, &cctx);
9694 break;
9695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009697 emsg(_(e_import_can_only_be_used_in_script));
9698 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009699 break;
9700
9701 case CMD_if:
9702 line = compile_if(p, &cctx);
9703 break;
9704 case CMD_elseif:
9705 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009706 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009707 break;
9708 case CMD_else:
9709 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009710 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 break;
9712 case CMD_endif:
9713 line = compile_endif(p, &cctx);
9714 break;
9715
9716 case CMD_while:
9717 line = compile_while(p, &cctx);
9718 break;
9719 case CMD_endwhile:
9720 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009721 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009722 break;
9723
9724 case CMD_for:
9725 line = compile_for(p, &cctx);
9726 break;
9727 case CMD_endfor:
9728 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009729 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730 break;
9731 case CMD_continue:
9732 line = compile_continue(p, &cctx);
9733 break;
9734 case CMD_break:
9735 line = compile_break(p, &cctx);
9736 break;
9737
9738 case CMD_try:
9739 line = compile_try(p, &cctx);
9740 break;
9741 case CMD_catch:
9742 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009743 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009744 break;
9745 case CMD_finally:
9746 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009747 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009748 break;
9749 case CMD_endtry:
9750 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009751 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009752 break;
9753 case CMD_throw:
9754 line = compile_throw(p, &cctx);
9755 break;
9756
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009757 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009758 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009759 break;
9760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009761 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009763 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009764 case CMD_echomsg:
9765 case CMD_echoerr:
9766 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009767 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009769 case CMD_put:
9770 ea.cmd = cmd;
9771 line = compile_put(p, &ea, &cctx);
9772 break;
9773
Bram Moolenaar4c137212021-04-19 16:48:48 +02009774 case CMD_substitute:
9775 if (cctx.ctx_skip == SKIP_YES)
9776 line = (char_u *)"";
9777 else
9778 {
9779 ea.arg = p;
9780 line = compile_substitute(line, &ea, &cctx);
9781 }
9782 break;
9783
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009784 case CMD_redir:
9785 ea.arg = p;
9786 line = compile_redir(line, &ea, &cctx);
9787 break;
9788
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009789 case CMD_cexpr:
9790 case CMD_lexpr:
9791 case CMD_caddexpr:
9792 case CMD_laddexpr:
9793 case CMD_cgetexpr:
9794 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009795#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009796 ea.arg = p;
9797 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009798#else
9799 ex_ni(&ea);
9800 line = NULL;
9801#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009802 break;
9803
Bram Moolenaar3988f642020-08-27 22:43:03 +02009804 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009805
Bram Moolenaarae616492020-07-28 20:07:27 +02009806 case CMD_append:
9807 case CMD_change:
9808 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009809 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009810 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009811 case CMD_xit:
9812 not_in_vim9(&ea);
9813 goto erret;
9814
Bram Moolenaar002262f2020-07-08 17:47:57 +02009815 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009816 if (cctx.ctx_skip != SKIP_YES)
9817 {
9818 semsg(_(e_invalid_command_str), ea.cmd);
9819 goto erret;
9820 }
9821 // We don't check for a next command here.
9822 line = (char_u *)"";
9823 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009824
Bram Moolenaar20677332021-06-06 17:02:53 +02009825 case CMD_lua:
9826 case CMD_mzscheme:
9827 case CMD_perl:
9828 case CMD_py3:
9829 case CMD_python3:
9830 case CMD_python:
9831 case CMD_pythonx:
9832 case CMD_ruby:
9833 case CMD_tcl:
9834 ea.arg = p;
9835 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009836 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009837 else
9838 // heredoc lines have been concatenated with NL
9839 // characters in get_function_body()
9840 line = compile_script(line, &cctx);
9841 break;
9842
9843 default:
9844 // Not recognized, execute with do_cmdline_cmd().
9845 ea.arg = p;
9846 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009847 break;
9848 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009849nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009850 if (line == NULL)
9851 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009852 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009853
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009854 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009855 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009857 if (cctx.ctx_type_stack.ga_len < 0)
9858 {
9859 iemsg("Type stack underflow");
9860 goto erret;
9861 }
9862 }
9863
9864 if (cctx.ctx_scope != NULL)
9865 {
9866 if (cctx.ctx_scope->se_type == IF_SCOPE)
9867 emsg(_(e_endif));
9868 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9869 emsg(_(e_endwhile));
9870 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9871 emsg(_(e_endfor));
9872 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009873 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009874 goto erret;
9875 }
9876
Bram Moolenaarefd88552020-06-18 20:50:10 +02009877 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009878 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009879 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9880 ufunc->uf_ret_type = &t_void;
9881 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009883 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884 goto erret;
9885 }
9886
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009887 // Return void if there is no return at the end.
9888 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009889 }
9890
Bram Moolenaar599410c2021-04-10 14:03:43 +02009891 // When compiled with ":silent!" and there was an error don't consider the
9892 // function compiled.
9893 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009894 {
9895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9896 + ufunc->uf_dfunc_idx;
9897 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009898 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009899#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009900 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009901 {
9902 dfunc->df_instr_prof = instr->ga_data;
9903 dfunc->df_instr_prof_count = instr->ga_len;
9904 }
9905 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009906#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009907 if (cctx.ctx_compile_type == CT_DEBUG)
9908 {
9909 dfunc->df_instr_debug = instr->ga_data;
9910 dfunc->df_instr_debug_count = instr->ga_len;
9911 }
9912 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009913 {
9914 dfunc->df_instr = instr->ga_data;
9915 dfunc->df_instr_count = instr->ga_len;
9916 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009917 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009918 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009919 if (cctx.ctx_outer_used)
9920 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009921 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009923
9924 ret = OK;
9925
9926erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009927 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009928 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009929 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9930 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009931
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009932 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009933 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009934 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009935 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009936
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009937 // If using the last entry in the table and it was added above, we
9938 // might as well remove it.
9939 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009940 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009941 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009942 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009943 ufunc->uf_dfunc_idx = 0;
9944 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009945 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009946
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009947 while (cctx.ctx_scope != NULL)
9948 drop_scope(&cctx);
9949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 if (errormsg != NULL)
9951 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009952 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009953 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954 }
9955
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009956 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9957 {
9958 if (ret == OK)
9959 {
9960 emsg(_(e_missing_redir_end));
9961 ret = FAIL;
9962 }
9963 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009964 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009965 }
9966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009968 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009969 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009970 if (do_estack_push)
9971 estack_pop();
9972
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009973 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009974 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009975 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009976 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009977 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009978}
9979
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009980 void
9981set_function_type(ufunc_T *ufunc)
9982{
9983 int varargs = ufunc->uf_va_name != NULL;
9984 int argcount = ufunc->uf_args.ga_len;
9985
9986 // Create a type for the function, with the return type and any
9987 // argument types.
9988 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9989 // The type is included in "tt_args".
9990 if (argcount > 0 || varargs)
9991 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009992 if (ufunc->uf_type_list.ga_itemsize == 0)
9993 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009994 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9995 argcount, &ufunc->uf_type_list);
9996 // Add argument types to the function type.
9997 if (func_type_add_arg_types(ufunc->uf_func_type,
9998 argcount + varargs,
9999 &ufunc->uf_type_list) == FAIL)
10000 return;
10001 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10002 ufunc->uf_func_type->tt_min_argcount =
10003 argcount - ufunc->uf_def_args.ga_len;
10004 if (ufunc->uf_arg_types == NULL)
10005 {
10006 int i;
10007
10008 // lambda does not have argument types.
10009 for (i = 0; i < argcount; ++i)
10010 ufunc->uf_func_type->tt_args[i] = &t_any;
10011 }
10012 else
10013 mch_memmove(ufunc->uf_func_type->tt_args,
10014 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10015 if (varargs)
10016 {
10017 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010018 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010019 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10020 }
10021 }
10022 else
10023 // No arguments, can use a predefined type.
10024 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10025 argcount, &ufunc->uf_type_list);
10026}
10027
10028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029/*
10030 * Delete an instruction, free what it contains.
10031 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010032 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033delete_instr(isn_T *isn)
10034{
10035 switch (isn->isn_type)
10036 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010037 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010038 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010039 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010040 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010041 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010042 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010043 case ISN_LOADENV:
10044 case ISN_LOADG:
10045 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010046 case ISN_LOADT:
10047 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010049 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010051 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010052 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010053 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010054 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010055 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010056 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010057 case ISN_STOREW:
10058 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 vim_free(isn->isn_arg.string);
10060 break;
10061
Bram Moolenaar4c137212021-04-19 16:48:48 +020010062 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010063 {
10064 int idx;
10065 isn_T *list = isn->isn_arg.subs.subs_instr;
10066
10067 vim_free(isn->isn_arg.subs.subs_cmd);
10068 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10069 delete_instr(list + idx);
10070 vim_free(list);
10071 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010072 break;
10073
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010074 case ISN_INSTR:
10075 {
10076 int idx;
10077 isn_T *list = isn->isn_arg.instr;
10078
10079 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10080 delete_instr(list + idx);
10081 vim_free(list);
10082 }
10083 break;
10084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010085 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010086 case ISN_STORES:
10087 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010088 break;
10089
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010090 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010091 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010092 vim_free(isn->isn_arg.unlet.ul_name);
10093 break;
10094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010095 case ISN_STOREOPT:
10096 vim_free(isn->isn_arg.storeopt.so_name);
10097 break;
10098
10099 case ISN_PUSHBLOB: // push blob isn_arg.blob
10100 blob_unref(isn->isn_arg.blob);
10101 break;
10102
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010103 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010104#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010105 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010106#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010107 break;
10108
10109 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010111 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010112#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010113 break;
10114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010115 case ISN_UCALL:
10116 vim_free(isn->isn_arg.ufunc.cuf_name);
10117 break;
10118
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010119 case ISN_FUNCREF:
10120 {
10121 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10122 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010123 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010124
Bram Moolenaar077a4232020-12-22 18:33:27 +010010125 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10126 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010127 }
10128 break;
10129
10130 case ISN_DCALL:
10131 {
10132 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10133 + isn->isn_arg.dfunc.cdf_idx;
10134
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010135 if (dfunc->df_ufunc != NULL
10136 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010137 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010138 }
10139 break;
10140
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010141 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010142 {
10143 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10144 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10145
10146 if (ufunc != NULL)
10147 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010148 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010149 func_ptr_unref(ufunc);
10150 }
10151
10152 vim_free(lambda);
10153 vim_free(isn->isn_arg.newfunc.nf_global);
10154 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010155 break;
10156
Bram Moolenaar5e654232020-09-16 15:22:00 +020010157 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010158 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010159 free_type(isn->isn_arg.type.ct_type);
10160 break;
10161
Bram Moolenaar02194d22020-10-24 23:08:38 +020010162 case ISN_CMDMOD:
10163 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10164 ->cmod_filter_regmatch.regprog);
10165 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10166 break;
10167
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010168 case ISN_LOADSCRIPT:
10169 case ISN_STORESCRIPT:
10170 vim_free(isn->isn_arg.script.scriptref);
10171 break;
10172
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010173 case ISN_TRY:
10174 vim_free(isn->isn_arg.try.try_ref);
10175 break;
10176
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010177 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010178 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010179 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10180 break;
10181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010182 case ISN_2BOOL:
10183 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010184 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010185 case ISN_ADDBLOB:
10186 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010187 case ISN_ANYINDEX:
10188 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010189 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010190 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010191 case ISN_BLOBINDEX:
10192 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010193 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010194 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010195 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010197 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010198 case ISN_COMPAREANY:
10199 case ISN_COMPAREBLOB:
10200 case ISN_COMPAREBOOL:
10201 case ISN_COMPAREDICT:
10202 case ISN_COMPAREFLOAT:
10203 case ISN_COMPAREFUNC:
10204 case ISN_COMPARELIST:
10205 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010206 case ISN_COMPARESPECIAL:
10207 case ISN_COMPARESTRING:
10208 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010209 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010210 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010211 case ISN_DROP:
10212 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010213 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010214 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010215 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010216 case ISN_EXECCONCAT:
10217 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010218 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010219 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010220 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010221 case ISN_GETITEM:
10222 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010223 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010224 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010225 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010226 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010227 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010228 case ISN_LOADBDICT:
10229 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010230 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010231 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010232 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010233 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010234 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010235 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010236 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010237 case ISN_NEGATENR:
10238 case ISN_NEWDICT:
10239 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010240 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010241 case ISN_OPFLOAT:
10242 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010243 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010244 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010245 case ISN_PROF_END:
10246 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010247 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 case ISN_PUSHF:
10249 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010250 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010251 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010252 case ISN_REDIREND:
10253 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010255 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010256 case ISN_SHUFFLE:
10257 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010259 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010260 case ISN_STORENR:
10261 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010262 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010263 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010264 case ISN_STOREV:
10265 case ISN_STRINDEX:
10266 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010267 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010268 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010269 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010270 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010271 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272 // nothing allocated
10273 break;
10274 }
10275}
10276
10277/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010278 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010279 */
10280 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010281delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010282{
10283 int idx;
10284
10285 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010286 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010287
10288 if (dfunc->df_instr != NULL)
10289 {
10290 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10291 delete_instr(dfunc->df_instr + idx);
10292 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010293 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010294 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010295 if (dfunc->df_instr_debug != NULL)
10296 {
10297 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10298 delete_instr(dfunc->df_instr_debug + idx);
10299 VIM_CLEAR(dfunc->df_instr_debug);
10300 dfunc->df_instr_debug = NULL;
10301 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010302#ifdef FEAT_PROFILE
10303 if (dfunc->df_instr_prof != NULL)
10304 {
10305 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10306 delete_instr(dfunc->df_instr_prof + idx);
10307 VIM_CLEAR(dfunc->df_instr_prof);
10308 dfunc->df_instr_prof = NULL;
10309 }
10310#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010311
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010312 if (mark_deleted)
10313 dfunc->df_deleted = TRUE;
10314 if (dfunc->df_ufunc != NULL)
10315 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010316}
10317
10318/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010319 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010320 * function, unless another user function still uses it.
10321 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010322 */
10323 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010324unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010325{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010326 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010327 {
10328 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10329 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010330
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010331 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010332 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010333 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010334 ufunc->uf_dfunc_idx = 0;
10335 if (dfunc->df_ufunc == ufunc)
10336 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010337 }
10338}
10339
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010340/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010341 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010342 */
10343 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010344link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010345{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010346 if (ufunc->uf_dfunc_idx > 0)
10347 {
10348 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10349 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010350
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010351 ++dfunc->df_refcount;
10352 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010353}
10354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010355#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010356/*
10357 * Free all functions defined with ":def".
10358 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010359 void
10360free_def_functions(void)
10361{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010362 int idx;
10363
10364 for (idx = 0; idx < def_functions.ga_len; ++idx)
10365 {
10366 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10367
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010368 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010369 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010370 }
10371
10372 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373}
10374#endif
10375
10376
10377#endif // FEAT_EVAL