blob: 5ec3b3d3908927aa74603e43c2015d413e1f950b [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;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
358 if (sav->sav_block_id == 0 || cctx == NULL)
359 // variable defined in the script scope or not in a function.
360 return sav;
361
362 // Go over the variables with this name and find one that was visible
363 // from the function.
364 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200365 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200366 while (sav != NULL)
367 {
368 int idx;
369
370 // Go over the blocks that this function was defined in. If the
371 // variable block ID matches it was visible to the function.
372 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
373 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
374 return sav;
375 sav = sav->sav_next;
376 }
377
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 // Not found, assume variable at script level was visible.
379 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200380}
381
382/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100383 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200384 */
385 static int
386script_is_vim9()
387{
388 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
389}
390
391/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200392 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 * Returns OK or FAIL.
395 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200396 static int
397script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200399 if (current_sctx.sc_sid <= 0)
400 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200401 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200402 {
403 // Check script variables that were visible where the function was
404 // defined.
405 if (find_script_var(name, len, cctx) != NULL)
406 return OK;
407 }
408 else
409 {
410 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
411 dictitem_T *di;
412 int cc;
413
414 // Check script variables that are currently visible
415 cc = name[len];
416 name[len] = NUL;
417 di = find_var_in_ht(ht, 0, name, TRUE);
418 name[len] = cc;
419 if (di != NULL)
420 return OK;
421 }
422
423 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424}
425
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100426/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100427 * Return TRUE if "name" is a local variable, argument, script variable or
428 * imported.
429 */
430 static int
431variable_exists(char_u *name, size_t len, cctx_T *cctx)
432{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100433 return (cctx != NULL
434 && (lookup_local(name, len, NULL, cctx) == OK
435 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200436 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100437 || find_imported(name, len, cctx) != NULL;
438}
439
440/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100441 * Return TRUE if "name" is a local variable, argument, script variable,
442 * imported or function.
443 */
444 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100445item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100446{
447 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100448 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100449
450 if (variable_exists(name, len, cctx))
451 return TRUE;
452
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100453 // This is similar to what is in lookup_scriptitem():
454 // Find a function, so that a following "->" works.
455 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
456 // a function call.
457 p = skipwhite(name + len);
458
459 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
460 {
461 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200462 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100463 // Skip "g:" before a function name.
464 is_global = (name[0] == 'g' && name[1] == ':');
465 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
466 }
467 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100468}
469
470/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100471 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200472 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200473 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100474 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100475 * Return FAIL and give an error if it defined.
476 */
477 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100478check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100479{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200480 int c = p[len];
481 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200482
Bram Moolenaarda479c72021-04-10 21:01:38 +0200483 // underscore argument is OK
484 if (len == 1 && *p == '_')
485 return OK;
486
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200487 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100488 {
489 if (is_arg)
490 semsg(_(e_argument_already_declared_in_script_str), p);
491 else
492 semsg(_(e_variable_already_declared_in_script_str), p);
493 return FAIL;
494 }
495
Bram Moolenaarad486a02020-08-01 23:22:18 +0200496 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100497 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100498 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200499 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200500 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200501 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100502 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200503 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200504 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
505 && (!func_is_global(ufunc)
506 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200507 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100508 if (is_arg)
509 semsg(_(e_argument_name_shadows_existing_variable_str), p);
510 else
511 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200512 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200513 return FAIL;
514 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200516 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100517 return OK;
518}
519
Bram Moolenaar65b95452020-07-19 14:03:09 +0200520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521/////////////////////////////////////////////////////////////////////
522// Following generate_ functions expect the caller to call ga_grow().
523
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200524#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
525#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527/*
528 * Generate an instruction without arguments.
529 * Returns a pointer to the new instruction, NULL if failed.
530 */
531 static isn_T *
532generate_instr(cctx_T *cctx, isntype_T isn_type)
533{
534 garray_T *instr = &cctx->ctx_instr;
535 isn_T *isn;
536
Bram Moolenaar080457c2020-03-03 21:53:32 +0100537 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 if (ga_grow(instr, 1) == FAIL)
539 return NULL;
540 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
541 isn->isn_type = isn_type;
542 isn->isn_lnum = cctx->ctx_lnum + 1;
543 ++instr->ga_len;
544
545 return isn;
546}
547
548/*
549 * Generate an instruction without arguments.
550 * "drop" will be removed from the stack.
551 * Returns a pointer to the new instruction, NULL if failed.
552 */
553 static isn_T *
554generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
555{
556 garray_T *stack = &cctx->ctx_type_stack;
557
Bram Moolenaar080457c2020-03-03 21:53:32 +0100558 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 stack->ga_len -= drop;
560 return generate_instr(cctx, isn_type);
561}
562
563/*
564 * Generate instruction "isn_type" and put "type" on the type stack.
565 */
566 static isn_T *
567generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
568{
569 isn_T *isn;
570 garray_T *stack = &cctx->ctx_type_stack;
571
572 if ((isn = generate_instr(cctx, isn_type)) == NULL)
573 return NULL;
574
575 if (ga_grow(stack, 1) == FAIL)
576 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200577 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 ++stack->ga_len;
579
580 return isn;
581}
582
583/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200584 * Generate an ISN_DEBUG instruction.
585 */
586 static isn_T *
587generate_instr_debug(cctx_T *cctx)
588{
589 isn_T *isn;
590 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
591 + cctx->ctx_ufunc->uf_dfunc_idx;
592
593 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
594 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200595 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
596 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 return isn;
598}
599
600/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200602 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200603 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 */
605 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200606may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607{
608 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200609 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100611 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100613 RETURN_OK_IF_SKIP(cctx);
614 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 switch ((*type)->tt_type)
616 {
617 // nothing to be done
618 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200620 // conversion possible
621 case VAR_SPECIAL:
622 case VAR_BOOL:
623 case VAR_NUMBER:
624 case VAR_FLOAT:
625 break;
626
627 // conversion possible (with runtime check)
628 case VAR_ANY:
629 case VAR_UNKNOWN:
630 isntype = ISN_2STRING_ANY;
631 break;
632
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200633 // conversion possible when tolerant
634 case VAR_LIST:
635 if (tolerant)
636 {
637 isntype = ISN_2STRING_ANY;
638 break;
639 }
640 // FALLTHROUGH
641
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200642 // conversion not possible
643 case VAR_VOID:
644 case VAR_BLOB:
645 case VAR_FUNC:
646 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200647 case VAR_DICT:
648 case VAR_JOB:
649 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200650 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200651 to_string_error((*type)->tt_type);
652 return FAIL;
653 }
654
655 *type = &t_string;
656 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200658 isn->isn_arg.tostring.offset = offset;
659 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
661 return OK;
662}
663
664 static int
665check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
666{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200667 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200669 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 {
671 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200672 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200674 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 return FAIL;
676 }
677 return OK;
678}
679
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200680 static int
681generate_add_instr(
682 cctx_T *cctx,
683 vartype_T vartype,
684 type_T *type1,
685 type_T *type2)
686{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100687 garray_T *stack = &cctx->ctx_type_stack;
688 isn_T *isn = generate_instr_drop(cctx,
689 vartype == VAR_NUMBER ? ISN_OPNR
690 : vartype == VAR_LIST ? ISN_ADDLIST
691 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200692#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100693 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200694#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100695 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200696
697 if (vartype != VAR_LIST && vartype != VAR_BLOB
698 && type1->tt_type != VAR_ANY
699 && type2->tt_type != VAR_ANY
700 && check_number_or_float(
701 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
702 return FAIL;
703
704 if (isn != NULL)
705 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706
707 // When concatenating two lists with different member types the member type
708 // becomes "any".
709 if (vartype == VAR_LIST
710 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
711 && type1->tt_member != type2->tt_member)
712 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
713
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200714 return isn == NULL ? FAIL : OK;
715}
716
717/*
718 * Get the type to use for an instruction for an operation on "type1" and
719 * "type2". If they are matching use a type-specific instruction. Otherwise
720 * fall back to runtime type checking.
721 */
722 static vartype_T
723operator_type(type_T *type1, type_T *type2)
724{
725 if (type1->tt_type == type2->tt_type
726 && (type1->tt_type == VAR_NUMBER
727 || type1->tt_type == VAR_LIST
728#ifdef FEAT_FLOAT
729 || type1->tt_type == VAR_FLOAT
730#endif
731 || type1->tt_type == VAR_BLOB))
732 return type1->tt_type;
733 return VAR_ANY;
734}
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736/*
737 * Generate an instruction with two arguments. The instruction depends on the
738 * type of the arguments.
739 */
740 static int
741generate_two_op(cctx_T *cctx, char_u *op)
742{
743 garray_T *stack = &cctx->ctx_type_stack;
744 type_T *type1;
745 type_T *type2;
746 vartype_T vartype;
747 isn_T *isn;
748
Bram Moolenaar080457c2020-03-03 21:53:32 +0100749 RETURN_OK_IF_SKIP(cctx);
750
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200751 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
753 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200754 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755
756 switch (*op)
757 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200758 case '+':
759 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 break;
762
763 case '-':
764 case '*':
765 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
766 op) == FAIL)
767 return FAIL;
768 if (vartype == VAR_NUMBER)
769 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
770#ifdef FEAT_FLOAT
771 else if (vartype == VAR_FLOAT)
772 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
773#endif
774 else
775 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
776 if (isn != NULL)
777 isn->isn_arg.op.op_type = *op == '*'
778 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
779 break;
780
Bram Moolenaar4c683752020-04-05 21:38:23 +0200781 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200783 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 && type2->tt_type != VAR_NUMBER))
785 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200786 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 return FAIL;
788 }
789 isn = generate_instr_drop(cctx,
790 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
791 if (isn != NULL)
792 isn->isn_arg.op.op_type = EXPR_REM;
793 break;
794 }
795
796 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200797 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 {
799 type_T *type = &t_any;
800
801#ifdef FEAT_FLOAT
802 // float+number and number+float results in float
803 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
804 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
805 type = &t_float;
806#endif
807 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
808 }
809
810 return OK;
811}
812
813/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200814 * Get the instruction to use for comparing "type1" with "type2"
815 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200817 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100818get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819{
820 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
Bram Moolenaar4c683752020-04-05 21:38:23 +0200822 if (type1 == VAR_UNKNOWN)
823 type1 = VAR_ANY;
824 if (type2 == VAR_UNKNOWN)
825 type2 = VAR_ANY;
826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 if (type1 == type2)
828 {
829 switch (type1)
830 {
831 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
832 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
833 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
834 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
835 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
836 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
837 case VAR_LIST: isntype = ISN_COMPARELIST; break;
838 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
839 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 default: isntype = ISN_COMPAREANY; break;
841 }
842 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200843 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100845 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 isntype = ISN_COMPAREANY;
847
Bram Moolenaar657137c2021-01-09 15:45:23 +0100848 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 && (isntype == ISN_COMPAREBOOL
850 || isntype == ISN_COMPARESPECIAL
851 || isntype == ISN_COMPARENR
852 || isntype == ISN_COMPAREFLOAT))
853 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200854 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100855 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200856 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 }
858 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100859 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
861 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100862 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
863 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 && (type1 == VAR_BLOB || type2 == VAR_BLOB
865 || type1 == VAR_LIST || type2 == VAR_LIST))))
866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200867 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200871 return isntype;
872}
873
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200874 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100875check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200876{
877 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
878 return FAIL;
879 return OK;
880}
881
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882/*
883 * Generate an ISN_COMPARE* instruction with a boolean result.
884 */
885 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100886generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200887{
888 isntype_T isntype;
889 isn_T *isn;
890 garray_T *stack = &cctx->ctx_type_stack;
891 vartype_T type1;
892 vartype_T type2;
893
894 RETURN_OK_IF_SKIP(cctx);
895
896 // Get the known type of the two items on the stack. If they are matching
897 // use a type-specific instruction. Otherwise fall back to runtime type
898 // checking.
899 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
900 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100901 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200902 if (isntype == ISN_DROP)
903 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904
905 if ((isn = generate_instr(cctx, isntype)) == NULL)
906 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100907 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 isn->isn_arg.op.op_ic = ic;
909
910 // takes two arguments, puts one bool back
911 if (stack->ga_len >= 2)
912 {
913 --stack->ga_len;
914 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
915 }
916
917 return OK;
918}
919
920/*
921 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200922 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 */
924 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200925generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926{
927 isn_T *isn;
928 garray_T *stack = &cctx->ctx_type_stack;
929
Bram Moolenaar080457c2020-03-03 21:53:32 +0100930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
932 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200933 isn->isn_arg.tobool.invert = invert;
934 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200937 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939 return OK;
940}
941
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200942/*
943 * Generate an ISN_COND2BOOL instruction.
944 */
945 static int
946generate_COND2BOOL(cctx_T *cctx)
947{
948 isn_T *isn;
949 garray_T *stack = &cctx->ctx_type_stack;
950
951 RETURN_OK_IF_SKIP(cctx);
952 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
953 return FAIL;
954
955 // type becomes bool
956 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
957
958 return OK;
959}
960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200962generate_TYPECHECK(
963 cctx_T *cctx,
964 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100965 int offset,
966 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967{
968 isn_T *isn;
969 garray_T *stack = &cctx->ctx_type_stack;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
973 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100975 isn->isn_arg.type.ct_off = (int8_T)offset;
976 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977
Bram Moolenaar5e654232020-09-16 15:22:00 +0200978 // type becomes expected
979 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980
981 return OK;
982}
983
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100984 static int
985generate_SETTYPE(
986 cctx_T *cctx,
987 type_T *expected)
988{
989 isn_T *isn;
990
991 RETURN_OK_IF_SKIP(cctx);
992 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
993 return FAIL;
994 isn->isn_arg.type.ct_type = alloc_type(expected);
995 return OK;
996}
997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200999 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1000 * used. Return FALSE if the types will never match.
1001 */
Bram Moolenaar193f6202020-11-16 20:08:35 +01001002 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001003use_typecheck(type_T *actual, type_T *expected)
1004{
1005 if (actual->tt_type == VAR_ANY
1006 || actual->tt_type == VAR_UNKNOWN
1007 || (actual->tt_type == VAR_FUNC
1008 && (expected->tt_type == VAR_FUNC
1009 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001010 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1011 && ((actual->tt_member == &t_void)
1012 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001013 return TRUE;
1014 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1015 && actual->tt_type == expected->tt_type)
1016 // This takes care of a nested list or dict.
1017 return use_typecheck(actual->tt_member, expected->tt_member);
1018 return FALSE;
1019}
1020
1021/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001022 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001023 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001024 * - "actual" is a type that can be "expected" type: add a runtime check; or
1025 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1027 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001028 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001029 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001030need_type(
1031 type_T *actual,
1032 type_T *expected,
1033 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001034 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001035 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001036 int silent,
1037 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001038{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001039 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001040
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001041 if (expected == &t_bool && actual != &t_bool
1042 && (actual->tt_flags & TTFLAG_BOOL_OK))
1043 {
1044 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1045 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001046 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001047 return OK;
1048 }
1049
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001050 where.wt_index = arg_idx;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001051 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001052 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001053
1054 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001055 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001056 if ((!actual_is_const || actual == &t_any)
1057 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001058 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001059 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001060 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001061 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001062
1063 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001064 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001065 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001066}
1067
1068/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001069 * Check that the top of the type stack has a type that can be used as a
1070 * condition. Give an error and return FAIL if not.
1071 */
1072 static int
1073bool_on_stack(cctx_T *cctx)
1074{
1075 garray_T *stack = &cctx->ctx_type_stack;
1076 type_T *type;
1077
1078 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1079 if (type == &t_bool)
1080 return OK;
1081
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001082 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001083 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1084 // This requires a runtime type check.
1085 return generate_COND2BOOL(cctx);
1086
Bram Moolenaar351ead02021-01-16 16:07:01 +01001087 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001088}
1089
1090/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 * Generate an ISN_PUSHNR instruction.
1092 */
1093 static int
1094generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1095{
1096 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001097 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
Bram Moolenaar080457c2020-03-03 21:53:32 +01001099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1101 return FAIL;
1102 isn->isn_arg.number = number;
1103
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001104 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001105 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001106 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 return OK;
1108}
1109
1110/*
1111 * Generate an ISN_PUSHBOOL instruction.
1112 */
1113 static int
1114generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.number = number;
1122
1123 return OK;
1124}
1125
1126/*
1127 * Generate an ISN_PUSHSPEC instruction.
1128 */
1129 static int
1130generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1131{
1132 isn_T *isn;
1133
Bram Moolenaar080457c2020-03-03 21:53:32 +01001134 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1136 return FAIL;
1137 isn->isn_arg.number = number;
1138
1139 return OK;
1140}
1141
1142#ifdef FEAT_FLOAT
1143/*
1144 * Generate an ISN_PUSHF instruction.
1145 */
1146 static int
1147generate_PUSHF(cctx_T *cctx, float_T fnumber)
1148{
1149 isn_T *isn;
1150
Bram Moolenaar080457c2020-03-03 21:53:32 +01001151 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1153 return FAIL;
1154 isn->isn_arg.fnumber = fnumber;
1155
1156 return OK;
1157}
1158#endif
1159
1160/*
1161 * Generate an ISN_PUSHS instruction.
1162 * Consumes "str".
1163 */
1164 static int
1165generate_PUSHS(cctx_T *cctx, char_u *str)
1166{
1167 isn_T *isn;
1168
Bram Moolenaar508b5612021-01-02 18:17:26 +01001169 if (cctx->ctx_skip == SKIP_YES)
1170 {
1171 vim_free(str);
1172 return OK;
1173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.string = str;
1177
1178 return OK;
1179}
1180
1181/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001182 * Generate an ISN_PUSHCHANNEL instruction.
1183 * Consumes "channel".
1184 */
1185 static int
1186generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1187{
1188 isn_T *isn;
1189
Bram Moolenaar080457c2020-03-03 21:53:32 +01001190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001191 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1192 return FAIL;
1193 isn->isn_arg.channel = channel;
1194
1195 return OK;
1196}
1197
1198/*
1199 * Generate an ISN_PUSHJOB instruction.
1200 * Consumes "job".
1201 */
1202 static int
1203generate_PUSHJOB(cctx_T *cctx, job_T *job)
1204{
1205 isn_T *isn;
1206
Bram Moolenaar080457c2020-03-03 21:53:32 +01001207 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001208 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001209 return FAIL;
1210 isn->isn_arg.job = job;
1211
1212 return OK;
1213}
1214
1215/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 * Generate an ISN_PUSHBLOB instruction.
1217 * Consumes "blob".
1218 */
1219 static int
1220generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1221{
1222 isn_T *isn;
1223
Bram Moolenaar080457c2020-03-03 21:53:32 +01001224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1226 return FAIL;
1227 isn->isn_arg.blob = blob;
1228
1229 return OK;
1230}
1231
1232/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001233 * Generate an ISN_PUSHFUNC instruction with name "name".
1234 * Consumes "name".
1235 */
1236 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001237generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001238{
1239 isn_T *isn;
1240
Bram Moolenaar080457c2020-03-03 21:53:32 +01001241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001242 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001243 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001244 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001245
1246 return OK;
1247}
1248
1249/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001250 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001251 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1252 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001253 */
1254 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001255generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001256{
1257 isn_T *isn;
1258 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001259 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1260 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001261 type_T *item_type = &t_any;
1262
1263 RETURN_OK_IF_SKIP(cctx);
1264
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001265 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001266 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001267 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001268 emsg(_(e_listreq));
1269 return FAIL;
1270 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001271 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001272 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1273 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001274 isn->isn_arg.getitem.gi_index = index;
1275 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001276
1277 // add the item type to the type stack
1278 if (ga_grow(stack, 1) == FAIL)
1279 return FAIL;
1280 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1281 ++stack->ga_len;
1282 return OK;
1283}
1284
1285/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001286 * Generate an ISN_SLICE instruction with "count".
1287 */
1288 static int
1289generate_SLICE(cctx_T *cctx, int count)
1290{
1291 isn_T *isn;
1292
1293 RETURN_OK_IF_SKIP(cctx);
1294 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1295 return FAIL;
1296 isn->isn_arg.number = count;
1297 return OK;
1298}
1299
1300/*
1301 * Generate an ISN_CHECKLEN instruction with "min_len".
1302 */
1303 static int
1304generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1305{
1306 isn_T *isn;
1307
1308 RETURN_OK_IF_SKIP(cctx);
1309
1310 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1311 return FAIL;
1312 isn->isn_arg.checklen.cl_min_len = min_len;
1313 isn->isn_arg.checklen.cl_more_OK = more_OK;
1314
1315 return OK;
1316}
1317
1318/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 * Generate an ISN_STORE instruction.
1320 */
1321 static int
1322generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1323{
1324 isn_T *isn;
1325
Bram Moolenaar080457c2020-03-03 21:53:32 +01001326 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1328 return FAIL;
1329 if (name != NULL)
1330 isn->isn_arg.string = vim_strsave(name);
1331 else
1332 isn->isn_arg.number = idx;
1333
1334 return OK;
1335}
1336
1337/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001338 * Generate an ISN_STOREOUTER instruction.
1339 */
1340 static int
1341generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1342{
1343 isn_T *isn;
1344
1345 RETURN_OK_IF_SKIP(cctx);
1346 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1347 return FAIL;
1348 isn->isn_arg.outer.outer_idx = idx;
1349 isn->isn_arg.outer.outer_depth = level;
1350
1351 return OK;
1352}
1353
1354/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1356 */
1357 static int
1358generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1359{
1360 isn_T *isn;
1361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1364 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001365 isn->isn_arg.storenr.stnr_idx = idx;
1366 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367
1368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_STOREOPT instruction
1373 */
1374 static int
1375generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1376{
1377 isn_T *isn;
1378
Bram Moolenaar080457c2020-03-03 21:53:32 +01001379 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001380 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 return FAIL;
1382 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1383 isn->isn_arg.storeopt.so_flags = opt_flags;
1384
1385 return OK;
1386}
1387
1388/*
1389 * Generate an ISN_LOAD or similar instruction.
1390 */
1391 static int
1392generate_LOAD(
1393 cctx_T *cctx,
1394 isntype_T isn_type,
1395 int idx,
1396 char_u *name,
1397 type_T *type)
1398{
1399 isn_T *isn;
1400
Bram Moolenaar080457c2020-03-03 21:53:32 +01001401 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1403 return FAIL;
1404 if (name != NULL)
1405 isn->isn_arg.string = vim_strsave(name);
1406 else
1407 isn->isn_arg.number = idx;
1408
1409 return OK;
1410}
1411
1412/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001413 * Generate an ISN_LOADOUTER instruction
1414 */
1415 static int
1416generate_LOADOUTER(
1417 cctx_T *cctx,
1418 int idx,
1419 int nesting,
1420 type_T *type)
1421{
1422 isn_T *isn;
1423
1424 RETURN_OK_IF_SKIP(cctx);
1425 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.outer.outer_idx = idx;
1428 isn->isn_arg.outer.outer_depth = nesting;
1429
1430 return OK;
1431}
1432
1433/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001434 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001435 */
1436 static int
1437generate_LOADV(
1438 cctx_T *cctx,
1439 char_u *name,
1440 int error)
1441{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001442 int di_flags;
1443 int vidx = find_vim_var(name, &di_flags);
1444 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445
Bram Moolenaar080457c2020-03-03 21:53:32 +01001446 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001447 if (vidx < 0)
1448 {
1449 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001450 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451 return FAIL;
1452 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001453 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001455 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001456}
1457
1458/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001459 * Generate an ISN_UNLET instruction.
1460 */
1461 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001462generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001463{
1464 isn_T *isn;
1465
1466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001467 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001468 return FAIL;
1469 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1470 isn->isn_arg.unlet.ul_forceit = forceit;
1471
1472 return OK;
1473}
1474
1475/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001476 * Generate an ISN_LOCKCONST instruction.
1477 */
1478 static int
1479generate_LOCKCONST(cctx_T *cctx)
1480{
1481 isn_T *isn;
1482
1483 RETURN_OK_IF_SKIP(cctx);
1484 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1485 return FAIL;
1486 return OK;
1487}
1488
1489/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 * Generate an ISN_LOADS instruction.
1491 */
1492 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001497 int sid,
1498 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499{
1500 isn_T *isn;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001503 if (isn_type == ISN_LOADS)
1504 isn = generate_instr_type(cctx, isn_type, type);
1505 else
1506 isn = generate_instr_drop(cctx, isn_type, 1);
1507 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001509 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1510 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
1512 return OK;
1513}
1514
1515/*
1516 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1517 */
1518 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001519generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 cctx_T *cctx,
1521 isntype_T isn_type,
1522 int sid,
1523 int idx,
1524 type_T *type)
1525{
1526 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001527 scriptref_T *sref;
1528 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529
Bram Moolenaar080457c2020-03-03 21:53:32 +01001530 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 if (isn_type == ISN_LOADSCRIPT)
1532 isn = generate_instr_type(cctx, isn_type, type);
1533 else
1534 isn = generate_instr_drop(cctx, isn_type, 1);
1535 if (isn == NULL)
1536 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001537
1538 // This requires three arguments, which doesn't fit in an instruction, thus
1539 // we need to allocate a struct for this.
1540 sref = ALLOC_ONE(scriptref_T);
1541 if (sref == NULL)
1542 return FAIL;
1543 isn->isn_arg.script.scriptref = sref;
1544 sref->sref_sid = sid;
1545 sref->sref_idx = idx;
1546 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001547 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 return OK;
1549}
1550
1551/*
1552 * Generate an ISN_NEWLIST instruction.
1553 */
1554 static int
1555generate_NEWLIST(cctx_T *cctx, int count)
1556{
1557 isn_T *isn;
1558 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 type_T *type;
1560 type_T *member;
1561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1564 return FAIL;
1565 isn->isn_arg.number = count;
1566
Bram Moolenaar127542b2020-08-09 17:22:04 +02001567 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001568 if (count == 0)
1569 member = &t_void;
1570 else
1571 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001572 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1573 cctx->ctx_type_list);
1574 type = get_list_type(member, cctx->ctx_type_list);
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // drop the value types
1577 stack->ga_len -= count;
1578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 // add the list type to the type stack
1580 if (ga_grow(stack, 1) == FAIL)
1581 return FAIL;
1582 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1583 ++stack->ga_len;
1584
1585 return OK;
1586}
1587
1588/*
1589 * Generate an ISN_NEWDICT instruction.
1590 */
1591 static int
1592generate_NEWDICT(cctx_T *cctx, int count)
1593{
1594 isn_T *isn;
1595 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 type_T *type;
1597 type_T *member;
1598
Bram Moolenaar080457c2020-03-03 21:53:32 +01001599 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1601 return FAIL;
1602 isn->isn_arg.number = count;
1603
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001604 if (count == 0)
1605 member = &t_void;
1606 else
1607 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001608 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1609 cctx->ctx_type_list);
1610 type = get_dict_type(member, cctx->ctx_type_list);
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 // drop the key and value types
1613 stack->ga_len -= 2 * count;
1614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 // add the dict type to the type stack
1616 if (ga_grow(stack, 1) == FAIL)
1617 return FAIL;
1618 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1619 ++stack->ga_len;
1620
1621 return OK;
1622}
1623
1624/*
1625 * Generate an ISN_FUNCREF instruction.
1626 */
1627 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001628generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629{
1630 isn_T *isn;
1631 garray_T *stack = &cctx->ctx_type_stack;
1632
Bram Moolenaar080457c2020-03-03 21:53:32 +01001633 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1635 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001636 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001637 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001639 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001640 // the nested context, including this one.
1641 if (ufunc->uf_flags & FC_CLOSURE)
1642 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 if (ga_grow(stack, 1) == FAIL)
1645 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001646 ((type_T **)stack->ga_data)[stack->ga_len] =
1647 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 ++stack->ga_len;
1649
1650 return OK;
1651}
1652
1653/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001654 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001655 * "lambda_name" and "func_name" must be in allocated memory and will be
1656 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001657 */
1658 static int
1659generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1660{
1661 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001662
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001663 if (cctx->ctx_skip == SKIP_YES)
1664 {
1665 vim_free(lambda_name);
1666 vim_free(func_name);
1667 return OK;
1668 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001669 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001670 {
1671 vim_free(lambda_name);
1672 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001673 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001674 }
1675 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001676 isn->isn_arg.newfunc.nf_global = func_name;
1677
1678 return OK;
1679}
1680
1681/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001682 * Generate an ISN_DEF instruction: list functions
1683 */
1684 static int
1685generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1686{
1687 isn_T *isn;
1688
1689 RETURN_OK_IF_SKIP(cctx);
1690 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1691 return FAIL;
1692 if (len > 0)
1693 {
1694 isn->isn_arg.string = vim_strnsave(name, len);
1695 if (isn->isn_arg.string == NULL)
1696 return FAIL;
1697 }
1698 return OK;
1699}
1700
1701/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 * Generate an ISN_JUMP instruction.
1703 */
1704 static int
1705generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1706{
1707 isn_T *isn;
1708 garray_T *stack = &cctx->ctx_type_stack;
1709
Bram Moolenaar080457c2020-03-03 21:53:32 +01001710 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1712 return FAIL;
1713 isn->isn_arg.jump.jump_when = when;
1714 isn->isn_arg.jump.jump_where = where;
1715
1716 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1717 --stack->ga_len;
1718
1719 return OK;
1720}
1721
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001722/*
1723 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1724 */
1725 static int
1726generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1727{
1728 isn_T *isn;
1729
1730 RETURN_OK_IF_SKIP(cctx);
1731 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1732 return FAIL;
1733 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1734 // jump_where is set later
1735 return OK;
1736}
1737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 static int
1739generate_FOR(cctx_T *cctx, int loop_idx)
1740{
1741 isn_T *isn;
1742 garray_T *stack = &cctx->ctx_type_stack;
1743
Bram Moolenaar080457c2020-03-03 21:53:32 +01001744 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1746 return FAIL;
1747 isn->isn_arg.forloop.for_idx = loop_idx;
1748
1749 if (ga_grow(stack, 1) == FAIL)
1750 return FAIL;
1751 // type doesn't matter, will be stored next
1752 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1753 ++stack->ga_len;
1754
1755 return OK;
1756}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001757/*
1758 * Generate an ISN_TRYCONT instruction.
1759 */
1760 static int
1761generate_TRYCONT(cctx_T *cctx, int levels, int where)
1762{
1763 isn_T *isn;
1764
1765 RETURN_OK_IF_SKIP(cctx);
1766 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1767 return FAIL;
1768 isn->isn_arg.trycont.tct_levels = levels;
1769 isn->isn_arg.trycont.tct_where = where;
1770
1771 return OK;
1772}
1773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774
1775/*
1776 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001777 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 * Return FAIL if the number of arguments is wrong.
1779 */
1780 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001781generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782{
1783 isn_T *isn;
1784 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001785 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001786 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001787 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001788 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789
Bram Moolenaar080457c2020-03-03 21:53:32 +01001790 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001791 argoff = check_internal_func(func_idx, argcount);
1792 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 return FAIL;
1794
Bram Moolenaar389df252020-07-09 21:20:47 +02001795 if (method_call && argoff > 1)
1796 {
1797 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1798 return FAIL;
1799 isn->isn_arg.shuffle.shfl_item = argcount;
1800 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1801 }
1802
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001803 if (argcount > 0)
1804 {
1805 // Check the types of the arguments.
1806 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001807 if (method_call && argoff > 1)
1808 {
1809 int i;
1810
1811 for (i = 0; i < argcount; ++i)
1812 shuffled_argtypes[i] = (i < argoff - 1)
1813 ? argtypes[i + 1]
1814 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1815 argtypes = shuffled_argtypes;
1816 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001817 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1818 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001819 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001820 if (internal_func_is_map(func_idx))
1821 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001822 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1825 return FAIL;
1826 isn->isn_arg.bfunc.cbf_idx = func_idx;
1827 isn->isn_arg.bfunc.cbf_argcount = argcount;
1828
Bram Moolenaar94738d82020-10-21 14:25:07 +02001829 // Drop the argument types and push the return type.
1830 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 if (ga_grow(stack, 1) == FAIL)
1832 return FAIL;
1833 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001834 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001835 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001837 if (maptype != NULL && maptype->tt_member != NULL
1838 && maptype->tt_member != &t_any)
1839 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001840 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 return OK;
1843}
1844
1845/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001846 * Generate an ISN_LISTAPPEND instruction. Works like add().
1847 * Argument count is already checked.
1848 */
1849 static int
1850generate_LISTAPPEND(cctx_T *cctx)
1851{
1852 garray_T *stack = &cctx->ctx_type_stack;
1853 type_T *list_type;
1854 type_T *item_type;
1855 type_T *expected;
1856
1857 // Caller already checked that list_type is a list.
1858 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1859 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1860 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001861 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001862 return FAIL;
1863
1864 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1865 return FAIL;
1866
1867 --stack->ga_len; // drop the argument
1868 return OK;
1869}
1870
1871/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001872 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1873 * Argument count is already checked.
1874 */
1875 static int
1876generate_BLOBAPPEND(cctx_T *cctx)
1877{
1878 garray_T *stack = &cctx->ctx_type_stack;
1879 type_T *item_type;
1880
1881 // Caller already checked that blob_type is a blob.
1882 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001883 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001884 return FAIL;
1885
1886 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1887 return FAIL;
1888
1889 --stack->ga_len; // drop the argument
1890 return OK;
1891}
1892
1893/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001894 * Return TRUE if "ufunc" should be compiled, taking into account whether
1895 * "profile" indicates profiling is to be done.
1896 */
1897 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001898func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001899{
1900 switch (ufunc->uf_def_status)
1901 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001902 case UF_TO_BE_COMPILED:
1903 return TRUE;
1904
Bram Moolenaarb2049902021-01-24 12:53:53 +01001905 case UF_COMPILED:
1906 {
1907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1908 + ufunc->uf_dfunc_idx;
1909
Bram Moolenaare99d4222021-06-13 14:01:26 +02001910 switch (compile_type)
1911 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001912 case CT_PROFILE:
1913#ifdef FEAT_PROFILE
1914 return dfunc->df_instr_prof == NULL;
1915#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001916 case CT_NONE:
1917 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001918 case CT_DEBUG:
1919 return dfunc->df_instr_debug == NULL;
1920 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001921 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001922
1923 case UF_NOT_COMPILED:
1924 case UF_COMPILE_ERROR:
1925 case UF_COMPILING:
1926 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001927 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001928 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001929}
1930
1931/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 * Generate an ISN_DCALL or ISN_UCALL instruction.
1933 * Return FAIL if the number of arguments is wrong.
1934 */
1935 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001936generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937{
1938 isn_T *isn;
1939 garray_T *stack = &cctx->ctx_type_stack;
1940 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001941 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942
Bram Moolenaar080457c2020-03-03 21:53:32 +01001943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 if (argcount > regular_args && !has_varargs(ufunc))
1945 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001946 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 return FAIL;
1948 }
1949 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1950 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001951 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 return FAIL;
1953 }
1954
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001955 if (ufunc->uf_def_status != UF_NOT_COMPILED
1956 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001957 {
1958 int i;
1959
1960 for (i = 0; i < argcount; ++i)
1961 {
1962 type_T *expected;
1963 type_T *actual;
1964
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001965 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1966 if (actual == &t_special
1967 && i >= regular_args - ufunc->uf_def_args.ga_len)
1968 {
1969 // assume v:none used for default argument value
1970 continue;
1971 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001972 if (i < regular_args)
1973 {
1974 if (ufunc->uf_arg_types == NULL)
1975 continue;
1976 expected = ufunc->uf_arg_types[i];
1977 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001978 else if (ufunc->uf_va_type == NULL
1979 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001980 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001981 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001982 else
1983 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001984 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001985 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001986 {
1987 arg_type_mismatch(expected, actual, i + 1);
1988 return FAIL;
1989 }
1990 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001991 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001992 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001993 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001994 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001995 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001996 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1997 {
1998 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1999 ufunc->uf_name);
2000 return FAIL;
2001 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002004 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002005 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002007 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 {
2009 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2010 isn->isn_arg.dfunc.cdf_argcount = argcount;
2011 }
2012 else
2013 {
2014 // A user function may be deleted and redefined later, can't use the
2015 // ufunc pointer, need to look it up again at runtime.
2016 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2017 isn->isn_arg.ufunc.cuf_argcount = argcount;
2018 }
2019
2020 stack->ga_len -= argcount; // drop the arguments
2021 if (ga_grow(stack, 1) == FAIL)
2022 return FAIL;
2023 // add return value
2024 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2025 ++stack->ga_len;
2026
2027 return OK;
2028}
2029
2030/*
2031 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2032 */
2033 static int
2034generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2035{
2036 isn_T *isn;
2037 garray_T *stack = &cctx->ctx_type_stack;
2038
Bram Moolenaar080457c2020-03-03 21:53:32 +01002039 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2041 return FAIL;
2042 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2043 isn->isn_arg.ufunc.cuf_argcount = argcount;
2044
2045 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002046 if (ga_grow(stack, 1) == FAIL)
2047 return FAIL;
2048 // add return value
2049 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2050 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051
2052 return OK;
2053}
2054
2055/*
2056 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002057 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 */
2059 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002060generate_PCALL(
2061 cctx_T *cctx,
2062 int argcount,
2063 char_u *name,
2064 type_T *type,
2065 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066{
2067 isn_T *isn;
2068 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002069 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070
Bram Moolenaar080457c2020-03-03 21:53:32 +01002071 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002072
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002073 if (type->tt_type == VAR_ANY)
2074 ret_type = &t_any;
2075 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002076 {
2077 if (type->tt_argcount != -1)
2078 {
2079 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2080
2081 if (argcount < type->tt_min_argcount - varargs)
2082 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002083 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002084 return FAIL;
2085 }
2086 if (!varargs && argcount > type->tt_argcount)
2087 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002088 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002089 return FAIL;
2090 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002091 if (type->tt_args != NULL)
2092 {
2093 int i;
2094
2095 for (i = 0; i < argcount; ++i)
2096 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002097 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002098 type_T *actual = ((type_T **)stack->ga_data)[
2099 stack->ga_len + offset];
2100 type_T *expected;
2101
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002102 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002103 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002104 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002105 else if (i >= type->tt_min_argcount
2106 && actual == &t_special)
2107 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002108 else
2109 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002110 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002111 cctx, TRUE, FALSE) == FAIL)
2112 {
2113 arg_type_mismatch(expected, actual, i + 1);
2114 return FAIL;
2115 }
2116 }
2117 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002118 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002119 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002120 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002121 else
2122 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002123 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002124 return FAIL;
2125 }
2126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2128 return FAIL;
2129 isn->isn_arg.pfunc.cpf_top = at_top;
2130 isn->isn_arg.pfunc.cpf_argcount = argcount;
2131
2132 stack->ga_len -= argcount; // drop the arguments
2133
2134 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002135 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002137 // If partial is above the arguments it must be cleared and replaced with
2138 // the return value.
2139 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2140 return FAIL;
2141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 return OK;
2143}
2144
2145/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002146 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 */
2148 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002149generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150{
2151 isn_T *isn;
2152 garray_T *stack = &cctx->ctx_type_stack;
2153 type_T *type;
2154
Bram Moolenaar080457c2020-03-03 21:53:32 +01002155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002156 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002158 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002160 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002162 if (type->tt_type != VAR_DICT && type != &t_any)
2163 {
2164 emsg(_(e_dictreq));
2165 return FAIL;
2166 }
2167 // change dict type to dict member type
2168 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002169 {
2170 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2171 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173
2174 return OK;
2175}
2176
2177/*
2178 * Generate an ISN_ECHO instruction.
2179 */
2180 static int
2181generate_ECHO(cctx_T *cctx, int with_white, int count)
2182{
2183 isn_T *isn;
2184
Bram Moolenaar080457c2020-03-03 21:53:32 +01002185 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2187 return FAIL;
2188 isn->isn_arg.echo.echo_with_white = with_white;
2189 isn->isn_arg.echo.echo_count = count;
2190
2191 return OK;
2192}
2193
Bram Moolenaarad39c092020-02-26 18:23:43 +01002194/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002195 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002196 */
2197 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002198generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002199{
2200 isn_T *isn;
2201
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002202 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002203 return FAIL;
2204 isn->isn_arg.number = count;
2205
2206 return OK;
2207}
2208
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002209/*
2210 * Generate an ISN_PUT instruction.
2211 */
2212 static int
2213generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2214{
2215 isn_T *isn;
2216
2217 RETURN_OK_IF_SKIP(cctx);
2218 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2219 return FAIL;
2220 isn->isn_arg.put.put_regname = regname;
2221 isn->isn_arg.put.put_lnum = lnum;
2222 return OK;
2223}
2224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 static int
2226generate_EXEC(cctx_T *cctx, char_u *line)
2227{
2228 isn_T *isn;
2229
Bram Moolenaar080457c2020-03-03 21:53:32 +01002230 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2232 return FAIL;
2233 isn->isn_arg.string = vim_strsave(line);
2234 return OK;
2235}
2236
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002237 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002238generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2239{
2240 isn_T *isn;
2241 garray_T *stack = &cctx->ctx_type_stack;
2242
2243 RETURN_OK_IF_SKIP(cctx);
2244 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2245 return FAIL;
2246 isn->isn_arg.string = vim_strsave(line);
2247
2248 if (ga_grow(stack, 1) == FAIL)
2249 return FAIL;
2250 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2251 ++stack->ga_len;
2252
2253 return OK;
2254}
2255
2256 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002257generate_EXECCONCAT(cctx_T *cctx, int count)
2258{
2259 isn_T *isn;
2260
2261 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2262 return FAIL;
2263 isn->isn_arg.number = count;
2264 return OK;
2265}
2266
Bram Moolenaar08597872020-12-10 19:43:40 +01002267/*
2268 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2269 */
2270 static int
2271generate_RANGE(cctx_T *cctx, char_u *range)
2272{
2273 isn_T *isn;
2274 garray_T *stack = &cctx->ctx_type_stack;
2275
2276 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2277 return FAIL;
2278 isn->isn_arg.string = range;
2279
2280 if (ga_grow(stack, 1) == FAIL)
2281 return FAIL;
2282 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2283 ++stack->ga_len;
2284 return OK;
2285}
2286
Bram Moolenaar792f7862020-11-23 08:31:18 +01002287 static int
2288generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2289{
2290 isn_T *isn;
2291
2292 RETURN_OK_IF_SKIP(cctx);
2293 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2294 return FAIL;
2295 isn->isn_arg.unpack.unp_count = var_count;
2296 isn->isn_arg.unpack.unp_semicolon = semicolon;
2297 return OK;
2298}
2299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002301 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002302 */
2303 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002304generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002305{
2306 isn_T *isn;
2307
Bram Moolenaarfa984412021-03-25 22:15:28 +01002308 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002309 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002310 cctx->ctx_has_cmdmod = TRUE;
2311
2312 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002313 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002314 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2315 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2316 return FAIL;
2317 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002318 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002319 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002320 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002321
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002322 return OK;
2323}
2324
2325 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002326generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002327{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002328 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2329 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002330 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002331 return OK;
2332}
2333
Bram Moolenaarfa984412021-03-25 22:15:28 +01002334 static int
2335misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002336{
2337 garray_T *instr = &cctx->ctx_instr;
2338
Bram Moolenaara91a7132021-03-25 21:12:15 +01002339 if (cctx->ctx_has_cmdmod
2340 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2341 == ISN_CMDMOD)
2342 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002343 emsg(_(e_misplaced_command_modifier));
2344 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002345 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002346 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002347}
2348
2349/*
2350 * Get the index of the current instruction.
2351 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2352 */
2353 static int
2354current_instr_idx(cctx_T *cctx)
2355{
2356 garray_T *instr = &cctx->ctx_instr;
2357 int idx = instr->ga_len;
2358
Bram Moolenaare99d4222021-06-13 14:01:26 +02002359 while (idx > 0)
2360 {
2361 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002362 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002363 {
2364 --idx;
2365 continue;
2366 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002367#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002368 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2369 {
2370 --idx;
2371 continue;
2372 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002373#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002374 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2375 {
2376 --idx;
2377 continue;
2378 }
2379 break;
2380 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002381 return idx;
2382}
2383
Bram Moolenaarf002a412021-01-24 13:34:18 +01002384#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002385 static void
2386may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2387{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002388 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002389 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002390}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002391#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002392
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002393/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002395 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002397 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002398reserve_local(
2399 cctx_T *cctx,
2400 char_u *name,
2401 size_t len,
2402 int isConst,
2403 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002406 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002408 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002410 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002411 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 }
2413
2414 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002415 return NULL;
2416 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002417 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002419 // Every local variable uses the next entry on the stack. We could re-use
2420 // the last ones when leaving a scope, but then variables used in a closure
2421 // might get overwritten. To keep things simple do not re-use stack
2422 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002423 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2424 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002425
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002426 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 lvar->lv_const = isConst;
2428 lvar->lv_type = type;
2429
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002430 // Remember the name for debugging.
2431 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2432 return NULL;
2433 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2434 vim_strsave(lvar->lv_name);
2435 ++dfunc->df_var_names.ga_len;
2436
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002437 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438}
2439
2440/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002441 * Remove local variables above "new_top".
2442 */
2443 static void
2444unwind_locals(cctx_T *cctx, int new_top)
2445{
2446 if (cctx->ctx_locals.ga_len > new_top)
2447 {
2448 int idx;
2449 lvar_T *lvar;
2450
2451 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2452 {
2453 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2454 vim_free(lvar->lv_name);
2455 }
2456 }
2457 cctx->ctx_locals.ga_len = new_top;
2458}
2459
2460/*
2461 * Free all local variables.
2462 */
2463 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002465{
2466 unwind_locals(cctx, 0);
2467 ga_clear(&cctx->ctx_locals);
2468}
2469
2470/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002471 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2472 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2473 * error if the variable was defined with :const.
2474 */
2475 static int
2476check_item_writable(svar_T *sv, int check_writable, char_u *name)
2477{
2478 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2479 || (check_writable == ASSIGN_FINAL
2480 && sv->sv_const == ASSIGN_CONST))
2481 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002482 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002483 return FAIL;
2484 }
2485 return OK;
2486}
2487
2488/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002490 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 * Returns the index in "sn_var_vals" if found.
2492 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002493 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 */
2495 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002496get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497{
2498 hashtab_T *ht;
2499 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002500 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002501 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 int idx;
2503
Bram Moolenaare3d46852020-08-29 13:39:17 +02002504 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002506 if (sid == current_sctx.sc_sid)
2507 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002508 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002509
2510 if (sav == NULL)
2511 return -2;
2512 idx = sav->sav_var_vals_idx;
2513 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002514 if (check_item_writable(sv, check_writable, name) == FAIL)
2515 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002516 return idx;
2517 }
2518
2519 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 ht = &SCRIPT_VARS(sid);
2521 di = find_var_in_ht(ht, 0, name, TRUE);
2522 if (di == NULL)
2523 return -2;
2524
2525 // Now find the svar_T index in sn_var_vals.
2526 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2527 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002528 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 if (sv->sv_tv == &di->di_tv)
2530 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002531 if (check_item_writable(sv, check_writable, name) == FAIL)
2532 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 return idx;
2534 }
2535 }
2536 return -1;
2537}
2538
2539/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002540 * Find "name" in imported items of the current script or in "cctx" if not
2541 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 */
2543 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002544find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 int idx;
2547
Bram Moolenaare3d46852020-08-29 13:39:17 +02002548 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002549 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 if (cctx != NULL)
2551 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2552 {
2553 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2554 + idx;
2555
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002556 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2557 : STRLEN(import->imp_name) == len
2558 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 return import;
2560 }
2561
Bram Moolenaarefa94442020-08-08 22:16:00 +02002562 return find_imported_in_script(name, len, current_sctx.sc_sid);
2563}
2564
2565 imported_T *
2566find_imported_in_script(char_u *name, size_t len, int sid)
2567{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002568 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002569 int idx;
2570
Bram Moolenaare3d46852020-08-29 13:39:17 +02002571 if (!SCRIPT_ID_VALID(sid))
2572 return NULL;
2573 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2575 {
2576 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2577
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002578 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2579 : STRLEN(import->imp_name) == len
2580 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 return import;
2582 }
2583 return NULL;
2584}
2585
2586/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002587 * Free all imported variables.
2588 */
2589 static void
2590free_imported(cctx_T *cctx)
2591{
2592 int idx;
2593
2594 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2595 {
2596 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2597
2598 vim_free(import->imp_name);
2599 }
2600 ga_clear(&cctx->ctx_imports);
2601}
2602
2603/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002604 * Return a pointer to the next line that isn't empty or only contains a
2605 * comment. Skips over white space.
2606 * Returns NULL if there is none.
2607 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002608 char_u *
2609peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002610{
2611 int lnum = cctx->ctx_lnum;
2612
2613 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2614 {
2615 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002616 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002617
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002618 // ignore NULLs inserted for continuation lines
2619 if (line != NULL)
2620 {
2621 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002622 if (vim9_bad_comment(p))
2623 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002624 if (*p != NUL && !vim9_comment_start(p))
2625 return p;
2626 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002627 }
2628 return NULL;
2629}
2630
2631/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002632 * Called when checking for a following operator at "arg". When the rest of
2633 * the line is empty or only a comment, peek the next line. If there is a next
2634 * line return a pointer to it and set "nextp".
2635 * Otherwise skip over white space.
2636 */
2637 static char_u *
2638may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2639{
2640 char_u *p = skipwhite(arg);
2641
2642 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002643 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002644 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002645 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002646 if (*nextp != NULL)
2647 return *nextp;
2648 }
2649 return p;
2650}
2651
2652/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002654 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002655 * Returns NULL when at the end.
2656 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002657 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002659{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002660 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002661
2662 do
2663 {
2664 ++cctx->ctx_lnum;
2665 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002666 {
2667 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002668 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002669 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002670 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002671 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002672 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002673 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002674 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 return line;
2676}
2677
2678/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002679 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002680 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002681 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002682 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2683 */
2684 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002685may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002686{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002687 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002688 if (vim9_bad_comment(*arg))
2689 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002690 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002691 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002692 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002693
2694 if (next == NULL)
2695 return FAIL;
2696 *arg = skipwhite(next);
2697 }
2698 return OK;
2699}
2700
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002701/*
2702 * Idem, and give an error when failed.
2703 */
2704 static int
2705may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2706{
2707 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2708 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002709 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002710 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002711 return FAIL;
2712 }
2713 return OK;
2714}
2715
2716
Bram Moolenaara5565e42020-05-09 15:44:01 +02002717// Structure passed between the compile_expr* functions to keep track of
2718// constants that have been parsed but for which no code was produced yet. If
2719// possible expressions on these constants are applied at compile time. If
2720// that is not possible, the code to push the constants needs to be generated
2721// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002722// Using 50 should be more than enough of 5 levels of ().
2723#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002725 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002726 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002727 int pp_is_const; // all generated code was constants, used for a
2728 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002729} ppconst_T;
2730
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002731static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002732static int compile_expr0(char_u **arg, cctx_T *cctx);
2733static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2734
Bram Moolenaara5565e42020-05-09 15:44:01 +02002735/*
2736 * Generate a PUSH instruction for "tv".
2737 * "tv" will be consumed or cleared.
2738 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2739 */
2740 static int
2741generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2742{
2743 if (tv != NULL)
2744 {
2745 switch (tv->v_type)
2746 {
2747 case VAR_UNKNOWN:
2748 break;
2749 case VAR_BOOL:
2750 generate_PUSHBOOL(cctx, tv->vval.v_number);
2751 break;
2752 case VAR_SPECIAL:
2753 generate_PUSHSPEC(cctx, tv->vval.v_number);
2754 break;
2755 case VAR_NUMBER:
2756 generate_PUSHNR(cctx, tv->vval.v_number);
2757 break;
2758#ifdef FEAT_FLOAT
2759 case VAR_FLOAT:
2760 generate_PUSHF(cctx, tv->vval.v_float);
2761 break;
2762#endif
2763 case VAR_BLOB:
2764 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2765 tv->vval.v_blob = NULL;
2766 break;
2767 case VAR_STRING:
2768 generate_PUSHS(cctx, tv->vval.v_string);
2769 tv->vval.v_string = NULL;
2770 break;
2771 default:
2772 iemsg("constant type not supported");
2773 clear_tv(tv);
2774 return FAIL;
2775 }
2776 tv->v_type = VAR_UNKNOWN;
2777 }
2778 return OK;
2779}
2780
2781/*
2782 * Generate code for any ppconst entries.
2783 */
2784 static int
2785generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2786{
2787 int i;
2788 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002789 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002790
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002791 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002792 for (i = 0; i < ppconst->pp_used; ++i)
2793 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2794 ret = FAIL;
2795 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002796 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002797 return ret;
2798}
2799
2800/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002801 * Check that the last item of "ppconst" is a bool.
2802 */
2803 static int
2804check_ppconst_bool(ppconst_T *ppconst)
2805{
2806 if (ppconst->pp_used > 0)
2807 {
2808 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002809 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002810
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002811 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
Bram Moolenaarcd268012021-07-22 19:11:08 +02003250 // Remove the string type from the stack.
3251 --cctx->ctx_type_stack.ga_len;
3252
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003253 // Temporarily reset the list of instructions so that the jump labels are
3254 // correct.
3255 cctx->ctx_instr.ga_len = 0;
3256 cctx->ctx_instr.ga_maxlen = 0;
3257 cctx->ctx_instr.ga_data = NULL;
3258 expr_res = compile_expr0(&s, cctx);
3259 s = skipwhite(s);
3260 trailing_error = *s != NUL;
3261
Bram Moolenaarff652882021-05-16 15:24:49 +02003262 if (expr_res == FAIL || trailing_error
3263 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003264 {
3265 if (trailing_error)
3266 semsg(_(e_trailing_arg), s);
3267 clear_instr_ga(&cctx->ctx_instr);
3268 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003269 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003270 return FAIL;
3271 }
3272
3273 // Move the generated instructions into the ISN_INSTR instruction, then
3274 // restore the list of instructions.
3275 instr_count = cctx->ctx_instr.ga_len;
3276 instr = cctx->ctx_instr.ga_data;
3277 instr[instr_count].isn_type = ISN_FINISH;
3278
3279 cctx->ctx_instr = save_ga;
3280 vim_free(isn->isn_arg.string);
3281 isn->isn_type = ISN_INSTR;
3282 isn->isn_arg.instr = instr;
3283 return OK;
3284}
3285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286/*
3287 * Compile the argument expressions.
3288 * "arg" points to just after the "(" and is advanced to after the ")"
3289 */
3290 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003291compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003293 char_u *p = *arg;
3294 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003295 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003296 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297
Bram Moolenaare6085c52020-04-12 20:19:16 +02003298 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003300 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3301 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003302 if (*p == ')')
3303 {
3304 *arg = p + 1;
3305 return OK;
3306 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003307 if (must_end)
3308 {
3309 semsg(_(e_missing_comma_before_argument_str), p);
3310 return FAIL;
3311 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003312
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003313 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003314 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 return FAIL;
3316 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003317
Bram Moolenaarff652882021-05-16 15:24:49 +02003318 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003319 && cctx->ctx_instr.ga_len == instr_count + 1)
3320 {
3321 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3322
3323 // {skip} argument of searchpair() can be compiled if not empty
3324 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3325 compile_string(isn, cctx);
3326 }
3327
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003328 if (*p != ',' && *skipwhite(p) == ',')
3329 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003330 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003331 p = skipwhite(p);
3332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003334 {
3335 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003336 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003337 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003338 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003339 else
3340 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003341 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003342 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003344failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003345 emsg(_(e_missing_close));
3346 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347}
3348
3349/*
3350 * Compile a function call: name(arg1, arg2)
3351 * "arg" points to "name", "arg + varlen" to the "(".
3352 * "argcount_init" is 1 for "value->method()"
3353 * Instructions:
3354 * EVAL arg1
3355 * EVAL arg2
3356 * BCALL / DCALL / UCALL
3357 */
3358 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003359compile_call(
3360 char_u **arg,
3361 size_t varlen,
3362 cctx_T *cctx,
3363 ppconst_T *ppconst,
3364 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365{
3366 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003367 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 int argcount = argcount_init;
3369 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003370 char_u fname_buf[FLEN_FIXED + 1];
3371 char_u *tofree = NULL;
3372 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003373 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003374 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003375 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003376 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaara5565e42020-05-09 15:44:01 +02003378 // we can evaluate "has('name')" at compile time
3379 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3380 {
3381 char_u *s = skipwhite(*arg + varlen + 1);
3382 typval_T argvars[2];
3383
3384 argvars[0].v_type = VAR_UNKNOWN;
3385 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003386 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003387 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003388 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003389 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003390 if (*s == ')' && argvars[0].v_type == VAR_STRING
3391 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003392 {
3393 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3394
3395 *arg = s + 1;
3396 argvars[1].v_type = VAR_UNKNOWN;
3397 tv->v_type = VAR_NUMBER;
3398 tv->vval.v_number = 0;
3399 f_has(argvars, tv);
3400 clear_tv(&argvars[0]);
3401 ++ppconst->pp_used;
3402 return OK;
3403 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003404 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003405 }
3406
3407 if (generate_ppconst(cctx, ppconst) == FAIL)
3408 return FAIL;
3409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 if (varlen >= sizeof(namebuf))
3411 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003412 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 return FAIL;
3414 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003415 vim_strncpy(namebuf, *arg, varlen);
3416 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003418 // We handle the "skip" argument of searchpair() and searchpairpos()
3419 // differently.
3420 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3421 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3422 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3423 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003426 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003427 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428
Bram Moolenaar03290b82020-12-19 16:30:44 +01003429 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003430 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 {
3432 int idx;
3433
3434 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003435 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003437 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003438 if (STRCMP(name, "flatten") == 0)
3439 {
3440 emsg(_(e_cannot_use_flatten_in_vim9_script));
3441 goto theend;
3442 }
3443
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003444 if (STRCMP(name, "add") == 0 && argcount == 2)
3445 {
3446 garray_T *stack = &cctx->ctx_type_stack;
3447 type_T *type = ((type_T **)stack->ga_data)[
3448 stack->ga_len - 2];
3449
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003450 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003451 if (type->tt_type == VAR_LIST)
3452 {
3453 // inline "add(list, item)" so that the type can be checked
3454 res = generate_LISTAPPEND(cctx);
3455 idx = -1;
3456 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003457 else if (type->tt_type == VAR_BLOB)
3458 {
3459 // inline "add(blob, nr)" so that the type can be checked
3460 res = generate_BLOBAPPEND(cctx);
3461 idx = -1;
3462 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003463 }
3464
3465 if (idx >= 0)
3466 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3467 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003468 else
3469 semsg(_(e_unknownfunc), namebuf);
3470 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 }
3472
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003473 // An argument or local variable can be a function reference, this
3474 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003475 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003476 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003477 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003478 // If we can find the function by name generate the right call.
3479 // Skip global functions here, a local funcref takes precedence.
3480 ufunc = find_func(name, FALSE, cctx);
3481 if (ufunc != NULL && !func_is_global(ufunc))
3482 {
3483 res = generate_CALL(cctx, ufunc, argcount);
3484 goto theend;
3485 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487
3488 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003489 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003490 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003492 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003493 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003494 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003495 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003496 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003497
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003498 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003499 goto theend;
3500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501
Bram Moolenaar0f769812020-09-12 18:32:34 +02003502 // If we can find a global function by name generate the right call.
3503 if (ufunc != NULL)
3504 {
3505 res = generate_CALL(cctx, ufunc, argcount);
3506 goto theend;
3507 }
3508
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003509 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003510 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003511 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003512 res = generate_UCALL(cctx, name, argcount);
3513 else
3514 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003515
3516theend:
3517 vim_free(tofree);
3518 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519}
3520
3521// like NAMESPACE_CHAR but with 'a' and 'l'.
3522#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3523
3524/*
3525 * Find the end of a variable or function name. Unlike find_name_end() this
3526 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003527 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 * Return a pointer to just after the name. Equal to "arg" if there is no
3529 * valid name.
3530 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003531 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003532to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533{
3534 char_u *p;
3535
3536 // Quick check for valid starting character.
3537 if (!eval_isnamec1(*arg))
3538 return arg;
3539
3540 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3541 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3542 // and can be used in slice "[n:]".
3543 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003544 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3546 break;
3547 return p;
3548}
3549
3550/*
3551 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003552 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 */
3554 char_u *
3555to_name_const_end(char_u *arg)
3556{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003557 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 typval_T rettv;
3559
3560 if (p == arg && *arg == '[')
3561 {
3562
3563 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003564 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 p = arg;
3566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 return p;
3568}
3569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570/*
3571 * parse a list: [expr, expr]
3572 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003573 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 */
3575 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003576compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577{
3578 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003579 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003581 int is_const;
3582 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003584 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003586 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003587 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003588 semsg(_(e_list_end), *arg);
3589 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003590 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003591 if (*p == ',')
3592 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003593 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003594 return FAIL;
3595 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003596 if (*p == ']')
3597 {
3598 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003599 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003600 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003601 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003602 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003603 if (!is_const)
3604 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 ++count;
3606 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003607 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003609 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3610 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003611 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003612 return FAIL;
3613 }
3614 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003615 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 p = skipwhite(p);
3617 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003618 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003620 ppconst->pp_is_const = is_all_const;
3621 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622}
3623
3624/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003625 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003626 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003627 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 */
3629 static int
3630compile_lambda(char_u **arg, cctx_T *cctx)
3631{
Bram Moolenaare462f522020-12-27 14:43:30 +01003632 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 typval_T rettv;
3634 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003635 evalarg_T evalarg;
3636
3637 CLEAR_FIELD(evalarg);
3638 evalarg.eval_flags = EVAL_EVALUATE;
3639 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
3641 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003642 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3643 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003644 {
3645 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003646 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003647 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003648
Bram Moolenaar65c44152020-12-24 15:14:01 +01003649 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003651 ++ufunc->uf_refcount;
3652 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
Bram Moolenaara9931532021-06-12 15:58:16 +02003654 // Compile it here to get the return type. The return type is optional,
3655 // when it's missing use t_unknown. This is recognized in
3656 // compile_return().
3657 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3658 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003659 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
Bram Moolenaar648594e2021-07-11 17:55:01 +02003661#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003662 // When the outer function is compiled for profiling, the lambda may be
3663 // called without profiling. Compile it here in the right context.
3664 if (cctx->ctx_compile_type == CT_PROFILE)
3665 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003666#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003667
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003668 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3669 // points into it. Point to the original line to avoid a dangling pointer.
3670 if (evalarg.eval_tofree_cmdline != NULL)
3671 {
3672 size_t off = *arg - evalarg.eval_tofree_cmdline;
3673
3674 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3675 + off;
3676 }
3677
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003678 clear_evalarg(&evalarg, NULL);
3679
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003680 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003681 {
3682 // The return type will now be known.
3683 set_function_type(ufunc);
3684
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003685 // The function reference count will be 1. When the ISN_FUNCREF
3686 // instruction is deleted the reference count is decremented and the
3687 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003688 return generate_FUNCREF(cctx, ufunc);
3689 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003690
3691 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693}
3694
3695/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003696 * Get a lambda and compile it. Uses Vim9 syntax.
3697 */
3698 int
3699get_lambda_tv_and_compile(
3700 char_u **arg,
3701 typval_T *rettv,
3702 int types_optional,
3703 evalarg_T *evalarg)
3704{
3705 int r;
3706 ufunc_T *ufunc;
3707 int save_sc_version = current_sctx.sc_version;
3708
3709 // Get the funcref in "rettv".
3710 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3711 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3712 current_sctx.sc_version = save_sc_version;
3713 if (r != OK)
3714 return r;
3715
3716 // "rettv" will now be a partial referencing the function.
3717 ufunc = rettv->vval.v_partial->pt_func;
3718
3719 // Compile it here to get the return type. The return type is optional,
3720 // when it's missing use t_unknown. This is recognized in
3721 // compile_return().
3722 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3723 ufunc->uf_ret_type = &t_unknown;
3724 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3725
3726 if (ufunc->uf_def_status == UF_COMPILED)
3727 {
3728 // The return type will now be known.
3729 set_function_type(ufunc);
3730 return OK;
3731 }
3732 clear_tv(rettv);
3733 return FAIL;
3734}
3735
3736/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003737 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003739 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 */
3741 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003742compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743{
3744 garray_T *instr = &cctx->ctx_instr;
3745 int count = 0;
3746 dict_T *d = dict_alloc();
3747 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003748 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003749 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003750 int is_const;
3751 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
3753 if (d == NULL)
3754 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003755 if (generate_ppconst(cctx, ppconst) == FAIL)
3756 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003757 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003759 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
Bram Moolenaar23c55272020-06-21 16:58:13 +02003761 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003762 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003763 *arg = NULL;
3764 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003765 }
3766
3767 if (**arg == '}')
3768 break;
3769
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003770 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003772 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003774 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003775 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003776 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003779 if (isn->isn_type == ISN_PUSHNR)
3780 {
3781 char buf[NUMBUFLEN];
3782
3783 // Convert to string at compile time.
3784 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3785 isn->isn_type = ISN_PUSHS;
3786 isn->isn_arg.string = vim_strsave((char_u *)buf);
3787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 if (isn->isn_type == ISN_PUSHS)
3789 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003790 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003791 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003792 *arg = skipwhite(*arg);
3793 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003794 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003795 emsg(_(e_missing_matching_bracket_after_dict_key));
3796 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003797 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003798 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003800 else
3801 {
3802 // {"name": value},
3803 // {'name': value},
3804 // {name: value} use "name" as a literal key
3805 key = get_literal_key(arg);
3806 if (key == NULL)
3807 return FAIL;
3808 if (generate_PUSHS(cctx, key) == FAIL)
3809 return FAIL;
3810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811
3812 // Check for duplicate keys, if using string keys.
3813 if (key != NULL)
3814 {
3815 item = dict_find(d, key, -1);
3816 if (item != NULL)
3817 {
3818 semsg(_(e_duplicate_key), key);
3819 goto failret;
3820 }
3821 item = dictitem_alloc(key);
3822 if (item != NULL)
3823 {
3824 item->di_tv.v_type = VAR_UNKNOWN;
3825 item->di_tv.v_lock = 0;
3826 if (dict_add(d, item) == FAIL)
3827 dictitem_free(item);
3828 }
3829 }
3830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 if (**arg != ':')
3832 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003833 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003834 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003835 else
3836 semsg(_(e_missing_dict_colon), *arg);
3837 return FAIL;
3838 }
3839 whitep = *arg + 1;
3840 if (!IS_WHITE_OR_NUL(*whitep))
3841 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003842 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 return FAIL;
3844 }
3845
Bram Moolenaar23c55272020-06-21 16:58:13 +02003846 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003847 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003848 *arg = NULL;
3849 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003850 }
3851
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003852 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003854 if (!is_const)
3855 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 ++count;
3857
Bram Moolenaar2c330432020-04-13 14:41:35 +02003858 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003859 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003860 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003861 *arg = NULL;
3862 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 if (**arg == '}')
3865 break;
3866 if (**arg != ',')
3867 {
3868 semsg(_(e_missing_dict_comma), *arg);
3869 goto failret;
3870 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003871 if (IS_WHITE_OR_NUL(*whitep))
3872 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003873 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003874 return FAIL;
3875 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003876 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003877 if (!IS_WHITE_OR_NUL(*whitep))
3878 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003879 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003880 return FAIL;
3881 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003882 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 }
3884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 *arg = *arg + 1;
3886
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003887 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003888 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003889 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003890 *arg += STRLEN(*arg);
3891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003893 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 return generate_NEWDICT(cctx, count);
3895
3896failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003897 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003898 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003899 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003900 *arg = (char_u *)"";
3901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 dict_unref(d);
3903 return FAIL;
3904}
3905
3906/*
3907 * Compile "&option".
3908 */
3909 static int
3910compile_get_option(char_u **arg, cctx_T *cctx)
3911{
3912 typval_T rettv;
3913 char_u *start = *arg;
3914 int ret;
3915
3916 // parse the option and get the current value to get the type.
3917 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003918 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 if (ret == OK)
3920 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003921 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003922 char_u *name = vim_strnsave(start, *arg - start);
3923 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3924 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
3926 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3927 vim_free(name);
3928 }
3929 clear_tv(&rettv);
3930
3931 return ret;
3932}
3933
3934/*
3935 * Compile "$VAR".
3936 */
3937 static int
3938compile_get_env(char_u **arg, cctx_T *cctx)
3939{
3940 char_u *start = *arg;
3941 int len;
3942 int ret;
3943 char_u *name;
3944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 ++*arg;
3946 len = get_env_len(arg);
3947 if (len == 0)
3948 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003949 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 return FAIL;
3951 }
3952
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003953 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 name = vim_strnsave(start, len + 1);
3955 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3956 vim_free(name);
3957 return ret;
3958}
3959
3960/*
3961 * Compile "@r".
3962 */
3963 static int
3964compile_get_register(char_u **arg, cctx_T *cctx)
3965{
3966 int ret;
3967
3968 ++*arg;
3969 if (**arg == NUL)
3970 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003971 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
3973 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003974 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 {
3976 emsg_invreg(**arg);
3977 return FAIL;
3978 }
3979 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3980 ++*arg;
3981 return ret;
3982}
3983
3984/*
3985 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003986 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 */
3988 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003989apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003991 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992
3993 // this works from end to start
3994 while (p > start)
3995 {
3996 --p;
3997 if (*p == '-' || *p == '+')
3998 {
3999 // only '-' has an effect, for '+' we only check the type
4000#ifdef FEAT_FLOAT
4001 if (rettv->v_type == VAR_FLOAT)
4002 {
4003 if (*p == '-')
4004 rettv->vval.v_float = -rettv->vval.v_float;
4005 }
4006 else
4007#endif
4008 {
4009 varnumber_T val;
4010 int error = FALSE;
4011
4012 // tv_get_number_chk() accepts a string, but we don't want that
4013 // here
4014 if (check_not_string(rettv) == FAIL)
4015 return FAIL;
4016 val = tv_get_number_chk(rettv, &error);
4017 clear_tv(rettv);
4018 if (error)
4019 return FAIL;
4020 if (*p == '-')
4021 val = -val;
4022 rettv->v_type = VAR_NUMBER;
4023 rettv->vval.v_number = val;
4024 }
4025 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004026 else if (numeric_only)
4027 {
4028 ++p;
4029 break;
4030 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004031 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 {
4033 int v = tv2bool(rettv);
4034
4035 // '!' is permissive in the type.
4036 clear_tv(rettv);
4037 rettv->v_type = VAR_BOOL;
4038 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4039 }
4040 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004041 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 return OK;
4043}
4044
4045/*
4046 * Recognize v: variables that are constants and set "rettv".
4047 */
4048 static void
4049get_vim_constant(char_u **arg, typval_T *rettv)
4050{
4051 if (STRNCMP(*arg, "v:true", 6) == 0)
4052 {
4053 rettv->v_type = VAR_BOOL;
4054 rettv->vval.v_number = VVAL_TRUE;
4055 *arg += 6;
4056 }
4057 else if (STRNCMP(*arg, "v:false", 7) == 0)
4058 {
4059 rettv->v_type = VAR_BOOL;
4060 rettv->vval.v_number = VVAL_FALSE;
4061 *arg += 7;
4062 }
4063 else if (STRNCMP(*arg, "v:null", 6) == 0)
4064 {
4065 rettv->v_type = VAR_SPECIAL;
4066 rettv->vval.v_number = VVAL_NULL;
4067 *arg += 6;
4068 }
4069 else if (STRNCMP(*arg, "v:none", 6) == 0)
4070 {
4071 rettv->v_type = VAR_SPECIAL;
4072 rettv->vval.v_number = VVAL_NONE;
4073 *arg += 6;
4074 }
4075}
4076
Bram Moolenaar657137c2021-01-09 15:45:23 +01004077 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004078get_compare_type(char_u *p, int *len, int *type_is)
4079{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004080 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004081 int i;
4082
4083 switch (p[0])
4084 {
4085 case '=': if (p[1] == '=')
4086 type = EXPR_EQUAL;
4087 else if (p[1] == '~')
4088 type = EXPR_MATCH;
4089 break;
4090 case '!': if (p[1] == '=')
4091 type = EXPR_NEQUAL;
4092 else if (p[1] == '~')
4093 type = EXPR_NOMATCH;
4094 break;
4095 case '>': if (p[1] != '=')
4096 {
4097 type = EXPR_GREATER;
4098 *len = 1;
4099 }
4100 else
4101 type = EXPR_GEQUAL;
4102 break;
4103 case '<': if (p[1] != '=')
4104 {
4105 type = EXPR_SMALLER;
4106 *len = 1;
4107 }
4108 else
4109 type = EXPR_SEQUAL;
4110 break;
4111 case 'i': if (p[1] == 's')
4112 {
4113 // "is" and "isnot"; but not a prefix of a name
4114 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4115 *len = 5;
4116 i = p[*len];
4117 if (!isalnum(i) && i != '_')
4118 {
4119 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4120 *type_is = TRUE;
4121 }
4122 }
4123 break;
4124 }
4125 return type;
4126}
4127
4128/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004129 * Skip over an expression, ignoring most errors.
4130 */
4131 static void
4132skip_expr_cctx(char_u **arg, cctx_T *cctx)
4133{
4134 evalarg_T evalarg;
4135
4136 CLEAR_FIELD(evalarg);
4137 evalarg.eval_cctx = cctx;
4138 skip_expr(arg, &evalarg);
4139}
4140
4141/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004143 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 */
4145 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004146compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004148 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149
4150 // this works from end to start
4151 while (p > start)
4152 {
4153 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004154 while (VIM_ISWHITE(*p))
4155 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 if (*p == '-' || *p == '+')
4157 {
4158 int negate = *p == '-';
4159 isn_T *isn;
4160
4161 // TODO: check type
4162 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4163 {
4164 --p;
4165 if (*p == '-')
4166 negate = !negate;
4167 }
4168 // only '-' has an effect, for '+' we only check the type
4169 if (negate)
4170 isn = generate_instr(cctx, ISN_NEGATENR);
4171 else
4172 isn = generate_instr(cctx, ISN_CHECKNR);
4173 if (isn == NULL)
4174 return FAIL;
4175 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004176 else if (numeric_only)
4177 {
4178 ++p;
4179 break;
4180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 else
4182 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004183 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004185 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004187 if (p[-1] == '!')
4188 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004191 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 return FAIL;
4193 }
4194 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004195 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 return OK;
4197}
4198
4199/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004200 * Compile "(expression)": recursive!
4201 * Return FAIL/OK.
4202 */
4203 static int
4204compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4205{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004206 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004207 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004208
Bram Moolenaar24156692021-01-14 20:35:49 +01004209 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4210 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004211 if (ppconst->pp_used <= PPSIZE - 10)
4212 {
4213 ret = compile_expr1(arg, cctx, ppconst);
4214 }
4215 else
4216 {
4217 // Not enough space in ppconst, flush constants.
4218 if (generate_ppconst(cctx, ppconst) == FAIL)
4219 return FAIL;
4220 ret = compile_expr0(arg, cctx);
4221 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004222 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4223 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004224 if (**arg == ')')
4225 ++*arg;
4226 else if (ret == OK)
4227 {
4228 emsg(_(e_missing_close));
4229 ret = FAIL;
4230 }
4231 return ret;
4232}
4233
4234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004236 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 */
4238 static int
4239compile_subscript(
4240 char_u **arg,
4241 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004242 char_u *start_leader,
4243 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004246 char_u *name_start = *end_leader;
4247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 for (;;)
4249 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004250 char_u *p = skipwhite(*arg);
4251
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004252 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004253 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004254 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004255
4256 // If a following line starts with "->{" or "->X" advance to that
4257 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004258 // Also if a following line starts with ".x".
4259 if (next != NULL &&
4260 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004261 && (next[2] == '{'
4262 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004263 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004264 {
4265 next = next_line_from_context(cctx, TRUE);
4266 if (next == NULL)
4267 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004268 *arg = next;
4269 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004270 }
4271 }
4272
Bram Moolenaarcd268012021-07-22 19:11:08 +02004273 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4274 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004275 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004277 garray_T *stack = &cctx->ctx_type_stack;
4278 type_T *type;
4279 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004281 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004282 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004283 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004286 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4287
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004288 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004289 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004291 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 return FAIL;
4293 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004294 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004296 char_u *pstart = p;
4297
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004298 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004299 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004300 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 // something->method()
4303 // Apply the '!', '-' and '+' first:
4304 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004305 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004308 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004309 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004310 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004311 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004312 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004313 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004314 garray_T *stack = &cctx->ctx_type_stack;
4315 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004316 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004317 int expr_isn_start = cctx->ctx_instr.ga_len;
4318 int expr_isn_end;
4319 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004320
4321 // Funcref call: list->(Refs[2])(arg)
4322 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004323 //
4324 // Fist compile the function expression.
4325 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004326 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004327
4328 // Remember the next instruction index, where the instructions
4329 // for arguments are being written.
4330 expr_isn_end = cctx->ctx_instr.ga_len;
4331
4332 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004333 if (**arg != '(')
4334 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004335 if (*skipwhite(*arg) == '(')
4336 emsg(_(e_nowhitespace));
4337 else
4338 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004339 return FAIL;
4340 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004341 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004342 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004343 return FAIL;
4344
Bram Moolenaar2927c072021-04-05 19:41:21 +02004345 // Move the instructions for the arguments to before the
4346 // instructions of the expression and move the type of the
4347 // expression after the argument types. This is what ISN_PCALL
4348 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004349 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004350 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4351 if (arg_isn_count > 0)
4352 {
4353 int expr_isn_count = expr_isn_end - expr_isn_start;
4354 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4355
4356 if (isn == NULL)
4357 return FAIL;
4358 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4359 + expr_isn_start,
4360 sizeof(isn_T) * expr_isn_count);
4361 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4362 + expr_isn_start,
4363 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4364 sizeof(isn_T) * arg_isn_count);
4365 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4366 + expr_isn_start + arg_isn_count,
4367 isn, sizeof(isn_T) * expr_isn_count);
4368 vim_free(isn);
4369
4370 type = ((type_T **)stack->ga_data)[type_idx_start];
4371 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4372 ((type_T **)stack->ga_data) + type_idx_start + 1,
4373 sizeof(type_T *)
4374 * (stack->ga_len - type_idx_start - 1));
4375 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4376 }
4377
Bram Moolenaar7e368202020-12-25 21:56:57 +01004378 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004379 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 return FAIL;
4381 }
4382 else
4383 {
4384 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004385 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004386 if (!eval_isnamec1(*p))
4387 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004388 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004389 return FAIL;
4390 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004391 if (ASCII_ISALPHA(*p) && p[1] == ':')
4392 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004393 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 ;
4395 if (*p != '(')
4396 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004397 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
4399 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 return FAIL;
4402 }
4403 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004404 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004406 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004407
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004408 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004409 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004410 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004411 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004412 // TODO: more arguments
4413 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004414 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004416 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004417
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004418 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004419 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004420 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004421 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004422 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004423 // missing first index is equal to zero
4424 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004425 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004426 else
4427 {
4428 if (compile_expr0(arg, cctx) == FAIL)
4429 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004430 if (**arg == ':')
4431 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004432 semsg(_(e_white_space_required_before_and_after_str_at_str),
4433 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004434 return FAIL;
4435 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004436 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004437 return FAIL;
4438 *arg = skipwhite(*arg);
4439 }
4440 if (**arg == ':')
4441 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004442 is_slice = TRUE;
4443 ++*arg;
4444 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4445 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004446 semsg(_(e_white_space_required_before_and_after_str_at_str),
4447 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004448 return FAIL;
4449 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004450 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004451 return FAIL;
4452 if (**arg == ']')
4453 // missing second index is equal to end of string
4454 generate_PUSHNR(cctx, -1);
4455 else
4456 {
4457 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004458 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004459 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004460 return FAIL;
4461 *arg = skipwhite(*arg);
4462 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464
4465 if (**arg != ']')
4466 {
4467 emsg(_(e_missbrac));
4468 return FAIL;
4469 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004470 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaare42939a2021-04-05 17:11:17 +02004472 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004473 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004475 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004477 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004478 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004479 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004480 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004481
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004482 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004483 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004484 {
4485 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004486 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004487 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004488 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004489 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 while (eval_isnamec(*p))
4491 MB_PTR_ADV(p);
4492 if (p == *arg)
4493 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004494 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
4496 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004497 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 return FAIL;
4499 *arg = p;
4500 }
4501 else
4502 break;
4503 }
4504
4505 // TODO - see handle_subscript():
4506 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4507 // Don't do this when "Func" is already a partial that was bound
4508 // explicitly (pt_auto is FALSE).
4509
4510 return OK;
4511}
4512
4513/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004514 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4515 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004517 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004518 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004519 *
4520 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 */
4522
4523/*
4524 * number number constant
4525 * 0zFFFFFFFF Blob constant
4526 * "string" string constant
4527 * 'string' literal string constant
4528 * &option-name option value
4529 * @r register contents
4530 * identifier variable value
4531 * function() function call
4532 * $VAR environment variable
4533 * (expression) nested expression
4534 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004535 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 *
4537 * Also handle:
4538 * ! in front logical NOT
4539 * - in front unary minus
4540 * + in front unary plus (ignored)
4541 * trailing (arg) funcref/partial call
4542 * trailing [] subscript in String or List
4543 * trailing .name entry in Dictionary
4544 * trailing ->name() method call
4545 */
4546 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004547compile_expr7(
4548 char_u **arg,
4549 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004550 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 char_u *start_leader, *end_leader;
4553 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004554 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004555 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004557 ppconst->pp_is_const = FALSE;
4558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 /*
4560 * Skip '!', '-' and '+' characters. They are handled later.
4561 */
4562 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004563 if (eval_leader(arg, TRUE) == FAIL)
4564 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 end_leader = *arg;
4566
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004567 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 switch (**arg)
4569 {
4570 /*
4571 * Number constant.
4572 */
4573 case '0': // also for blob starting with 0z
4574 case '1':
4575 case '2':
4576 case '3':
4577 case '4':
4578 case '5':
4579 case '6':
4580 case '7':
4581 case '8':
4582 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004583 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004585 // Apply "-" and "+" just before the number now, right to
4586 // left. Matters especially when "->" follows. Stops at
4587 // '!'.
4588 if (apply_leader(rettv, TRUE,
4589 start_leader, &end_leader) == FAIL)
4590 {
4591 clear_tv(rettv);
4592 return FAIL;
4593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 break;
4595
4596 /*
4597 * String constant: "string".
4598 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004599 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 return FAIL;
4601 break;
4602
4603 /*
4604 * Literal string constant: 'str''ing'.
4605 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004606 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 return FAIL;
4608 break;
4609
4610 /*
4611 * Constant Vim variable.
4612 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004613 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 ret = NOTDONE;
4615 break;
4616
4617 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 * "true" constant
4619 */
4620 case 't': if (STRNCMP(*arg, "true", 4) == 0
4621 && !eval_isnamec((*arg)[4]))
4622 {
4623 *arg += 4;
4624 rettv->v_type = VAR_BOOL;
4625 rettv->vval.v_number = VVAL_TRUE;
4626 }
4627 else
4628 ret = NOTDONE;
4629 break;
4630
4631 /*
4632 * "false" constant
4633 */
4634 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4635 && !eval_isnamec((*arg)[5]))
4636 {
4637 *arg += 5;
4638 rettv->v_type = VAR_BOOL;
4639 rettv->vval.v_number = VVAL_FALSE;
4640 }
4641 else
4642 ret = NOTDONE;
4643 break;
4644
4645 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004646 * "null" constant
4647 */
4648 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004649 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004650 {
4651 *arg += 4;
4652 rettv->v_type = VAR_SPECIAL;
4653 rettv->vval.v_number = VVAL_NULL;
4654 }
4655 else
4656 ret = NOTDONE;
4657 break;
4658
4659 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 * List: [expr, expr]
4661 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004662 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4663 return FAIL;
4664 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 break;
4666
4667 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * Dictionary: {'key': val, 'key': val}
4669 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004670 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4671 return FAIL;
4672 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 break;
4674
4675 /*
4676 * Option value: &name
4677 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004678 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4679 return FAIL;
4680 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 break;
4682
4683 /*
4684 * Environment variable: $VAR.
4685 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004686 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4687 return FAIL;
4688 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 break;
4690
4691 /*
4692 * Register contents: @r.
4693 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004694 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4695 return FAIL;
4696 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 break;
4698 /*
4699 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004700 * lambda: (arg, arg) => expr
4701 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004703 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4704 ret = compile_lambda(arg, cctx);
4705 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004706 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 break;
4708
4709 default: ret = NOTDONE;
4710 break;
4711 }
4712 if (ret == FAIL)
4713 return FAIL;
4714
Bram Moolenaar1c747212020-05-09 18:28:34 +02004715 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004717 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 clear_tv(rettv);
4719 else
4720 // A constant expression can possibly be handled compile time,
4721 // return the value instead of generating code.
4722 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 }
4724 else if (ret == NOTDONE)
4725 {
4726 char_u *p;
4727 int r;
4728
4729 if (!eval_isnamec1(**arg))
4730 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004731 if (!vim9_bad_comment(*arg))
4732 {
4733 if (ends_excmd(*skipwhite(*arg)))
4734 semsg(_(e_empty_expression_str), *arg);
4735 else
4736 semsg(_(e_name_expected_str), *arg);
4737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 return FAIL;
4739 }
4740
4741 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004742 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004743 if (p - *arg == (size_t)1 && **arg == '_')
4744 {
4745 emsg(_(e_cannot_use_underscore_here));
4746 return FAIL;
4747 }
4748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 {
4751 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 {
4755 if (generate_ppconst(cctx, ppconst) == FAIL)
4756 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004757 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 if (r == FAIL)
4760 return FAIL;
4761 }
4762
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004763 // Handle following "[]", ".member", etc.
4764 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004765 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766 ppconst) == FAIL)
4767 return FAIL;
4768 if (ppconst->pp_used > 0)
4769 {
4770 // apply the '!', '-' and '+' before the constant
4771 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004772 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 return FAIL;
4774 return OK;
4775 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004776 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004778 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779}
4780
4781/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004782 * Give the "white on both sides" error, taking the operator from "p[len]".
4783 */
4784 void
4785error_white_both(char_u *op, int len)
4786{
4787 char_u buf[10];
4788
4789 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004790 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004791}
4792
4793/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004794 * <type>expr7: runtime type check / conversion
4795 */
4796 static int
4797compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4798{
4799 type_T *want_type = NULL;
4800
4801 // Recognize <type>
4802 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4803 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004804 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004805 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4806 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004807 return FAIL;
4808
4809 if (**arg != '>')
4810 {
4811 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004812 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004813 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004814 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004815 return FAIL;
4816 }
4817 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004818 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004819 return FAIL;
4820 }
4821
4822 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4823 return FAIL;
4824
4825 if (want_type != NULL)
4826 {
4827 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004828 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004829 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004830
Bram Moolenaard1103582020-08-14 22:44:25 +02004831 generate_ppconst(cctx, ppconst);
4832 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004833 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004834 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004835 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004836 return FAIL;
4837 }
4838 }
4839
4840 return OK;
4841}
4842
4843/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 * * number multiplication
4845 * / number division
4846 * % number modulo
4847 */
4848 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850{
4851 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004852 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004853 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004855 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004856 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 return FAIL;
4858
4859 /*
4860 * Repeat computing, until no "*", "/" or "%" is following.
4861 */
4862 for (;;)
4863 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 if (*op != '*' && *op != '/' && *op != '%')
4866 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004867 if (next != NULL)
4868 {
4869 *arg = next_line_from_context(cctx, TRUE);
4870 op = skipwhite(*arg);
4871 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004872
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004873 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004875 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004876 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004878 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004879 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004881 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004882 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004884
4885 if (ppconst->pp_used == ppconst_used + 2
4886 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4887 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004888 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004889 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4890 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4891 varnumber_T res = 0;
4892 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004894 // both are numbers: compute the result
4895 switch (*op)
4896 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004897 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004898 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004899 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004900 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004901 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004902 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004903 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004904 break;
4905 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004906 if (failed)
4907 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004908 tv1->vval.v_number = res;
4909 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004910 }
4911 else
4912 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004913 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004914 generate_two_op(cctx, op);
4915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 }
4917
4918 return OK;
4919}
4920
4921/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004922 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 * - number subtraction
4924 * .. string concatenation
4925 */
4926 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004927compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928{
4929 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004930 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
4934 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004935 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 return FAIL;
4937
4938 /*
4939 * Repeat computing, until no "+", "-" or ".." is following.
4940 */
4941 for (;;)
4942 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004943 op = may_peek_next_line(cctx, *arg, &next);
4944 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004946 if (op[0] == op[1] && *op != '.' && next)
4947 // Finding "++" or "--" on the next line is a separate command.
4948 // But ".." is concatenation.
4949 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004951 if (next != NULL)
4952 {
4953 *arg = next_line_from_context(cctx, TRUE);
4954 op = skipwhite(*arg);
4955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004957 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004959 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004960 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 }
4962
Bram Moolenaare0de1712020-12-02 17:36:54 +01004963 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004964 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004966 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 return FAIL;
4969
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004971 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004972 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4973 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4974 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4975 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004977 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4978 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004979
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004980 // concat/subtract/add constant numbers
4981 if (*op == '+')
4982 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4983 else if (*op == '-')
4984 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4985 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004986 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004987 // concatenate constant strings
4988 char_u *s1 = tv1->vval.v_string;
4989 char_u *s2 = tv2->vval.v_string;
4990 size_t len1 = STRLEN(s1);
4991
4992 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4993 if (tv1->vval.v_string == NULL)
4994 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004996 return FAIL;
4997 }
4998 mch_memmove(tv1->vval.v_string, s1, len1);
4999 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005000 vim_free(s1);
5001 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005002 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005003 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 }
5005 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005006 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005007 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005008 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005009 if (*op == '.')
5010 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005011 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5012 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005013 return FAIL;
5014 generate_instr_drop(cctx, ISN_CONCAT, 1);
5015 }
5016 else
5017 generate_two_op(cctx, op);
5018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * expr5a == expr5b
5026 * expr5a =~ expr5b
5027 * expr5a != expr5b
5028 * expr5a !~ expr5b
5029 * expr5a > expr5b
5030 * expr5a >= expr5b
5031 * expr5a < expr5b
5032 * expr5a <= expr5b
5033 * expr5a is expr5b
5034 * expr5a isnot expr5b
5035 *
5036 * Produces instructions:
5037 * EVAL expr5a Push result of "expr5a"
5038 * EVAL expr5b Push result of "expr5b"
5039 * COMPARE one of the compare instructions
5040 */
5041 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005042compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005044 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005046 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005049 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050
5051 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005052 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 return FAIL;
5054
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005055 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005056 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057
5058 /*
5059 * If there is a comparative operator, use it.
5060 */
5061 if (type != EXPR_UNKNOWN)
5062 {
5063 int ic = FALSE; // Default: do not ignore case
5064
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005065 if (next != NULL)
5066 {
5067 *arg = next_line_from_context(cctx, TRUE);
5068 p = skipwhite(*arg);
5069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 if (type_is && (p[len] == '?' || p[len] == '#'))
5071 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005072 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 return FAIL;
5074 }
5075 // extra question mark appended: ignore case
5076 if (p[len] == '?')
5077 {
5078 ic = TRUE;
5079 ++len;
5080 }
5081 // extra '#' appended: match case (ignored)
5082 else if (p[len] == '#')
5083 ++len;
5084 // nothing appended: match case
5085
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005086 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005088 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005089 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 }
5091
5092 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005093 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005094 return FAIL;
5095
Bram Moolenaara5565e42020-05-09 15:44:01 +02005096 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 return FAIL;
5098
Bram Moolenaara5565e42020-05-09 15:44:01 +02005099 if (ppconst->pp_used == ppconst_used + 2)
5100 {
5101 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5102 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5103 int ret;
5104
5105 // Both sides are a constant, compute the result now.
5106 // First check for a valid combination of types, this is more
5107 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005108 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 ret = FAIL;
5110 else
5111 {
5112 ret = typval_compare(tv1, tv2, type, ic);
5113 tv1->v_type = VAR_BOOL;
5114 tv1->vval.v_number = tv1->vval.v_number
5115 ? VVAL_TRUE : VVAL_FALSE;
5116 clear_tv(tv2);
5117 --ppconst->pp_used;
5118 }
5119 return ret;
5120 }
5121
5122 generate_ppconst(cctx, ppconst);
5123 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 }
5125
5126 return OK;
5127}
5128
Bram Moolenaar7f141552020-05-09 17:35:53 +02005129static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131/*
5132 * Compile || or &&.
5133 */
5134 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005135compile_and_or(
5136 char_u **arg,
5137 cctx_T *cctx,
5138 char *op,
5139 ppconst_T *ppconst,
5140 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005142 char_u *next;
5143 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 int opchar = *op;
5145
5146 if (p[0] == opchar && p[1] == opchar)
5147 {
5148 garray_T *instr = &cctx->ctx_instr;
5149 garray_T end_ga;
5150
5151 /*
5152 * Repeat until there is no following "||" or "&&"
5153 */
5154 ga_init2(&end_ga, sizeof(int), 10);
5155 while (p[0] == opchar && p[1] == opchar)
5156 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005157 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005158 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005159 int start_ctx_lnum = cctx->ctx_lnum;
5160 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005161 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005162
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005163 if (next != NULL)
5164 {
5165 *arg = next_line_from_context(cctx, TRUE);
5166 p = skipwhite(*arg);
5167 }
5168
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005169 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5170 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005171 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005172 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005173 return FAIL;
5174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005176 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005177 SOURCING_LNUM = start_lnum;
5178 save_lnum = cctx->ctx_lnum;
5179 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005180
5181 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005182 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005183 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005184 // TODO: use ppconst if the value is a constant
5185 generate_ppconst(cctx, ppconst);
5186
5187 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005188 status = bool_on_stack(cctx);
5189 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005190 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005191 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005192 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005193 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 {
5195 ga_clear(&end_ga);
5196 return FAIL;
5197 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5200 ++end_ga.ga_len;
5201 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005202 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203
5204 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005205 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005206 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005207 {
5208 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005209 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005210 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005211
Bram Moolenaara5565e42020-05-09 15:44:01 +02005212 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5213 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 {
5215 ga_clear(&end_ga);
5216 return FAIL;
5217 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005218
5219 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005221
5222 if (check_ppconst_bool(ppconst) == FAIL)
5223 {
5224 ga_clear(&end_ga);
5225 return FAIL;
5226 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005227 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005229 // Every part must evaluate to a bool.
5230 if (bool_on_stack(cctx) == FAIL)
5231 {
5232 ga_clear(&end_ga);
5233 return FAIL;
5234 }
5235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 // Fill in the end label in all jumps.
5237 while (end_ga.ga_len > 0)
5238 {
5239 isn_T *isn;
5240
5241 --end_ga.ga_len;
5242 isn = ((isn_T *)instr->ga_data)
5243 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5244 isn->isn_arg.jump.jump_where = instr->ga_len;
5245 }
5246 ga_clear(&end_ga);
5247 }
5248
5249 return OK;
5250}
5251
5252/*
5253 * expr4a && expr4a && expr4a logical AND
5254 *
5255 * Produces instructions:
5256 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005257 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005258 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005260 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 * EVAL expr4c Push result of "expr4c"
5262 * end:
5263 */
5264 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005265compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 int ppconst_used = ppconst->pp_used;
5268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005270 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 return FAIL;
5272
5273 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005274 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275}
5276
5277/*
5278 * expr3a || expr3b || expr3c logical OR
5279 *
5280 * Produces instructions:
5281 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005282 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005283 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005285 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286 * EVAL expr3c Push result of "expr3c"
5287 * end:
5288 */
5289 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005290compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005292 int ppconst_used = ppconst->pp_used;
5293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005295 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 return FAIL;
5297
5298 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005299 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300}
5301
5302/*
5303 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005305 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 * JUMP_IF_FALSE alt jump if false
5307 * EVAL expr1a
5308 * JUMP_ALWAYS end
5309 * alt: EVAL expr1b
5310 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005311 *
5312 * Toplevel expression: expr2 ?? expr1
5313 * Produces instructions:
5314 * EVAL expr2 Push result of "expr2"
5315 * JUMP_AND_KEEP_IF_TRUE end jump if true
5316 * EVAL expr1
5317 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 */
5319 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005320compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321{
5322 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005323 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005324 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325
Bram Moolenaar3988f642020-08-27 22:43:03 +02005326 // Ignore all kinds of errors when not producing code.
5327 if (cctx->ctx_skip == SKIP_YES)
5328 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005329 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005330 return OK;
5331 }
5332
Bram Moolenaar61a89812020-05-07 16:58:17 +02005333 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005334 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 return FAIL;
5336
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005337 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 if (*p == '?')
5339 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005340 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 garray_T *instr = &cctx->ctx_instr;
5342 garray_T *stack = &cctx->ctx_type_stack;
5343 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005344 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005346 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 int has_const_expr = FALSE;
5348 int const_value = FALSE;
5349 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005351 if (next != NULL)
5352 {
5353 *arg = next_line_from_context(cctx, TRUE);
5354 p = skipwhite(*arg);
5355 }
5356
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005357 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005358 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005359 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005360 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005361 return FAIL;
5362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363
Bram Moolenaara5565e42020-05-09 15:44:01 +02005364 if (ppconst->pp_used == ppconst_used + 1)
5365 {
5366 // the condition is a constant, we know whether the ? or the :
5367 // expression is to be evaluated.
5368 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005369 if (op_falsy)
5370 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5371 else
5372 {
5373 int error = FALSE;
5374
5375 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5376 &error);
5377 if (error)
5378 return FAIL;
5379 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005380 cctx->ctx_skip = save_skip == SKIP_YES ||
5381 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5382
5383 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5384 // "left ?? right" and "left" is truthy: produce "left"
5385 generate_ppconst(cctx, ppconst);
5386 else
5387 {
5388 clear_tv(&ppconst->pp_tv[ppconst_used]);
5389 --ppconst->pp_used;
5390 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005391 }
5392 else
5393 {
5394 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005395 if (op_falsy)
5396 end_idx = instr->ga_len;
5397 generate_JUMP(cctx, op_falsy
5398 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5399 if (op_falsy)
5400 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
5403 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005404 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005405 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005406 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005407 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408
Bram Moolenaara5565e42020-05-09 15:44:01 +02005409 if (!has_const_expr)
5410 {
5411 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005413 if (!op_falsy)
5414 {
5415 // remember the type and drop it
5416 --stack->ga_len;
5417 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005419 end_idx = instr->ga_len;
5420 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005421
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005422 // jump here from JUMP_IF_FALSE
5423 isn = ((isn_T *)instr->ga_data) + alt_idx;
5424 isn->isn_arg.jump.jump_where = instr->ga_len;
5425 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005428 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005430 // Check for the ":".
5431 p = may_peek_next_line(cctx, *arg, &next);
5432 if (*p != ':')
5433 {
5434 emsg(_(e_missing_colon));
5435 return FAIL;
5436 }
5437 if (next != NULL)
5438 {
5439 *arg = next_line_from_context(cctx, TRUE);
5440 p = skipwhite(*arg);
5441 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005442
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005443 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5444 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005445 semsg(_(e_white_space_required_before_and_after_str_at_str),
5446 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005447 return FAIL;
5448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005450 // evaluate the third expression
5451 if (has_const_expr)
5452 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005453 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005454 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005455 return FAIL;
5456 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5457 return FAIL;
5458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459
Bram Moolenaara5565e42020-05-09 15:44:01 +02005460 if (!has_const_expr)
5461 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005462 type_T **typep;
5463
Bram Moolenaara5565e42020-05-09 15:44:01 +02005464 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465
Bram Moolenaara5565e42020-05-09 15:44:01 +02005466 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5468 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005469
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005470 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005471 isn = ((isn_T *)instr->ga_data) + end_idx;
5472 isn->isn_arg.jump.jump_where = instr->ga_len;
5473 }
5474
5475 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 }
5477 return OK;
5478}
5479
5480/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005482 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5483 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 */
5485 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005486compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005487{
5488 ppconst_T ppconst;
5489
5490 CLEAR_FIELD(ppconst);
5491 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5492 {
5493 clear_ppconst(&ppconst);
5494 return FAIL;
5495 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005496 if (is_const != NULL)
5497 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005498 if (generate_ppconst(cctx, &ppconst) == FAIL)
5499 return FAIL;
5500 return OK;
5501}
5502
5503/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005504 * Toplevel expression.
5505 */
5506 static int
5507compile_expr0(char_u **arg, cctx_T *cctx)
5508{
5509 return compile_expr0_ext(arg, cctx, NULL);
5510}
5511
5512/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005513 * Compile "return [expr]".
5514 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 */
5516 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005517compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518{
5519 char_u *p = arg;
5520 garray_T *stack = &cctx->ctx_type_stack;
5521 type_T *stack_type;
5522
5523 if (*p != NUL && *p != '|' && *p != '\n')
5524 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005525 if (legacy)
5526 {
5527 int save_flags = cmdmod.cmod_flags;
5528
5529 generate_LEGACY_EVAL(cctx, p);
5530 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5531 0, cctx, FALSE, FALSE) == FAIL)
5532 return NULL;
5533 cmdmod.cmod_flags |= CMOD_LEGACY;
5534 (void)skip_expr(&p, NULL);
5535 cmdmod.cmod_flags = save_flags;
5536 }
5537 else
5538 {
5539 // compile return argument into instructions
5540 if (compile_expr0(&p, cctx) == FAIL)
5541 return NULL;
5542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005544 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005545 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005546 // "check_return_type" with uf_ret_type set to &t_unknown is used
5547 // for an inline function without a specified return type. Set the
5548 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005549 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005550 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005551 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5552 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005553 || (!check_return_type
5554 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005555 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005556 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005557 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005558 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005559 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005560 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5561 && stack_type->tt_type != VAR_VOID
5562 && stack_type->tt_type != VAR_UNKNOWN)
5563 {
5564 emsg(_(e_returning_value_in_function_without_return_type));
5565 return NULL;
5566 }
5567 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005568 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005569 return NULL;
5570 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572 }
5573 else
5574 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005575 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005576 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005577 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5578 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005580 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 return NULL;
5582 }
5583
5584 // No argument, return zero.
5585 generate_PUSHNR(cctx, 0);
5586 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005587
5588 // Undo any command modifiers.
5589 generate_undo_cmdmods(cctx);
5590
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005591 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 return NULL;
5593
5594 // "return val | endif" is possible
5595 return skipwhite(p);
5596}
5597
5598/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005599 * Get a line from the compilation context, compatible with exarg_T getline().
5600 * Return a pointer to the line in allocated memory.
5601 * Return NULL for end-of-file or some error.
5602 */
5603 static char_u *
5604exarg_getline(
5605 int c UNUSED,
5606 void *cookie,
5607 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005608 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005609{
5610 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005611 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005612
Bram Moolenaar66250c92020-08-20 15:02:42 +02005613 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005614 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005615 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005616 return NULL;
5617 ++cctx->ctx_lnum;
5618 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5619 // Comment lines result in NULL pointers, skip them.
5620 if (p != NULL)
5621 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005622 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005623}
5624
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005625 void
5626fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5627{
5628 eap->getline = exarg_getline;
5629 eap->cookie = cctx;
5630}
5631
Bram Moolenaar04b12692020-05-04 23:24:44 +02005632/*
5633 * Compile a nested :def command.
5634 */
5635 static char_u *
5636compile_nested_function(exarg_T *eap, cctx_T *cctx)
5637{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005638 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005639 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005640 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005641 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005642 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005643 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005644 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005645
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005646 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005647 {
5648 emsg(_(e_cannot_use_bang_with_nested_def));
5649 return NULL;
5650 }
5651
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005652 if (*name_start == '/')
5653 {
5654 name_end = skip_regexp(name_start + 1, '/', TRUE);
5655 if (*name_end == '/')
5656 ++name_end;
5657 eap->nextcmd = check_nextcmd(name_end);
5658 }
5659 if (name_end == name_start || *skipwhite(name_end) != '(')
5660 {
5661 if (!ends_excmd2(name_start, name_end))
5662 {
5663 semsg(_(e_invalid_command_str), eap->cmd);
5664 return NULL;
5665 }
5666
5667 // "def" or "def Name": list functions
5668 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5669 return NULL;
5670 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5671 }
5672
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005673 // Only g:Func() can use a namespace.
5674 if (name_start[1] == ':' && !is_global)
5675 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005676 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005677 return NULL;
5678 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005679 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005680 return NULL;
5681
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005683 fill_exarg_from_cctx(eap, cctx);
5684
Bram Moolenaar04b12692020-05-04 23:24:44 +02005685 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005686 lambda_name = vim_strsave(get_lambda_name());
5687 if (lambda_name == NULL)
5688 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005689 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005690
Bram Moolenaar822ba242020-05-24 23:00:18 +02005691 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005692 {
5693 r = eap->skip ? OK : FAIL;
5694 goto theend;
5695 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005696
5697 // copy over the block scope IDs before compiling
5698 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5699 {
5700 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5701
5702 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5703 if (ufunc->uf_block_ids != NULL)
5704 {
5705 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5706 sizeof(int) * block_depth);
5707 ufunc->uf_block_depth = block_depth;
5708 }
5709 }
5710
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005711 compile_type = COMPILE_TYPE(ufunc);
5712#ifdef FEAT_PROFILE
5713 // If the outer function is profiled, also compile the nested function for
5714 // profiling.
5715 if (cctx->ctx_compile_type == CT_PROFILE)
5716 compile_type = CT_PROFILE;
5717#endif
5718 if (func_needs_compiling(ufunc, compile_type)
5719 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005720 {
5721 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005722 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005723 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005724
Bram Moolenaar648594e2021-07-11 17:55:01 +02005725#ifdef FEAT_PROFILE
5726 // When the outer function is compiled for profiling, the nested function
5727 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005728 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005729 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5730#endif
5731
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005732 if (is_global)
5733 {
5734 char_u *func_name = vim_strnsave(name_start + 2,
5735 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005736
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005737 if (func_name == NULL)
5738 r = FAIL;
5739 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005740 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005741 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005742 lambda_name = NULL;
5743 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005744 }
5745 else
5746 {
5747 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005748 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005749 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005750
Bram Moolenaareef21022020-08-01 22:16:43 +02005751 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005752 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005753 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005754 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005755 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5756 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005757 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005758
5759theend:
5760 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005761 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005762}
5763
5764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 * Return the length of an assignment operator, or zero if there isn't one.
5766 */
5767 int
5768assignment_len(char_u *p, int *heredoc)
5769{
5770 if (*p == '=')
5771 {
5772 if (p[1] == '<' && p[2] == '<')
5773 {
5774 *heredoc = TRUE;
5775 return 3;
5776 }
5777 return 1;
5778 }
5779 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5780 return 2;
5781 if (STRNCMP(p, "..=", 3) == 0)
5782 return 3;
5783 return 0;
5784}
5785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005787 * Generate the load instruction for "name".
5788 */
5789 static void
5790generate_loadvar(
5791 cctx_T *cctx,
5792 assign_dest_T dest,
5793 char_u *name,
5794 lvar_T *lvar,
5795 type_T *type)
5796{
5797 switch (dest)
5798 {
5799 case dest_option:
5800 // TODO: check the option exists
5801 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5802 break;
5803 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005804 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5805 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5806 else
5807 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005808 break;
5809 case dest_buffer:
5810 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5811 break;
5812 case dest_window:
5813 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5814 break;
5815 case dest_tab:
5816 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5817 break;
5818 case dest_script:
5819 compile_load_scriptvar(cctx,
5820 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5821 break;
5822 case dest_env:
5823 // Include $ in the name here
5824 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5825 break;
5826 case dest_reg:
5827 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5828 break;
5829 case dest_vimvar:
5830 generate_LOADV(cctx, name + 2, TRUE);
5831 break;
5832 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005833 if (lvar->lv_from_outer > 0)
5834 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5835 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005836 else
5837 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5838 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005839 case dest_expr:
5840 // list or dict value should already be on the stack.
5841 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005842 }
5843}
5844
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005845/*
5846 * Skip over "[expr]" or ".member".
5847 * Does not check for any errors.
5848 */
5849 static char_u *
5850skip_index(char_u *start)
5851{
5852 char_u *p = start;
5853
5854 if (*p == '[')
5855 {
5856 p = skipwhite(p + 1);
5857 (void)skip_expr(&p, NULL);
5858 p = skipwhite(p);
5859 if (*p == ']')
5860 return p + 1;
5861 return p;
5862 }
5863 // if (*p == '.')
5864 return to_name_end(p + 1, TRUE);
5865}
5866
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005867 void
5868vim9_declare_error(char_u *name)
5869{
5870 char *scope = "";
5871
5872 switch (*name)
5873 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005874 case 'g': scope = _("global"); break;
5875 case 'b': scope = _("buffer"); break;
5876 case 'w': scope = _("window"); break;
5877 case 't': scope = _("tab"); break;
5878 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005879 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005880 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005881 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005882 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005883 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005884 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005885 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005886 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005887 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005888}
5889
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005890/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005891 * For one assignment figure out the type of destination. Return it in "dest".
5892 * When not recognized "dest" is not set.
5893 * For an option "opt_flags" is set.
5894 * For a v:var "vimvaridx" is set.
5895 * "type" is set to the destination type if known, unchanted otherwise.
5896 * Return FAIL if an error message was given.
5897 */
5898 static int
5899get_var_dest(
5900 char_u *name,
5901 assign_dest_T *dest,
5902 int cmdidx,
5903 int *opt_flags,
5904 int *vimvaridx,
5905 type_T **type,
5906 cctx_T *cctx)
5907{
5908 char_u *p;
5909
5910 if (*name == '&')
5911 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005912 int cc;
5913 long numval;
5914 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005915
5916 *dest = dest_option;
5917 if (cmdidx == CMD_final || cmdidx == CMD_const)
5918 {
5919 emsg(_(e_const_option));
5920 return FAIL;
5921 }
5922 p = name;
5923 p = find_option_end(&p, opt_flags);
5924 if (p == NULL)
5925 {
5926 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005927 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005928 return FAIL;
5929 }
5930 cc = *p;
5931 *p = NUL;
5932 opt_type = get_option_value(skip_option_env_lead(name),
5933 &numval, NULL, *opt_flags);
5934 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005935 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005936 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005937 case gov_unknown:
5938 semsg(_(e_unknown_option), name);
5939 return FAIL;
5940 case gov_string:
5941 case gov_hidden_string:
5942 *type = &t_string;
5943 break;
5944 case gov_bool:
5945 case gov_hidden_bool:
5946 *type = &t_bool;
5947 break;
5948 case gov_number:
5949 case gov_hidden_number:
5950 *type = &t_number;
5951 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005952 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005953 }
5954 else if (*name == '$')
5955 {
5956 *dest = dest_env;
5957 *type = &t_string;
5958 }
5959 else if (*name == '@')
5960 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005961 if (name[1] != '@'
5962 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005963 {
5964 emsg_invreg(name[1]);
5965 return FAIL;
5966 }
5967 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005968 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005969 }
5970 else if (STRNCMP(name, "g:", 2) == 0)
5971 {
5972 *dest = dest_global;
5973 }
5974 else if (STRNCMP(name, "b:", 2) == 0)
5975 {
5976 *dest = dest_buffer;
5977 }
5978 else if (STRNCMP(name, "w:", 2) == 0)
5979 {
5980 *dest = dest_window;
5981 }
5982 else if (STRNCMP(name, "t:", 2) == 0)
5983 {
5984 *dest = dest_tab;
5985 }
5986 else if (STRNCMP(name, "v:", 2) == 0)
5987 {
5988 typval_T *vtv;
5989 int di_flags;
5990
5991 *vimvaridx = find_vim_var(name + 2, &di_flags);
5992 if (*vimvaridx < 0)
5993 {
5994 semsg(_(e_variable_not_found_str), name);
5995 return FAIL;
5996 }
5997 // We use the current value of "sandbox" here, is that OK?
5998 if (var_check_ro(di_flags, name, FALSE))
5999 return FAIL;
6000 *dest = dest_vimvar;
6001 vtv = get_vim_var_tv(*vimvaridx);
6002 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6003 }
6004 return OK;
6005}
6006
6007/*
6008 * Generate a STORE instruction for "dest", not being "dest_local".
6009 * Return FAIL when out of memory.
6010 */
6011 static int
6012generate_store_var(
6013 cctx_T *cctx,
6014 assign_dest_T dest,
6015 int opt_flags,
6016 int vimvaridx,
6017 int scriptvar_idx,
6018 int scriptvar_sid,
6019 type_T *type,
6020 char_u *name)
6021{
6022 switch (dest)
6023 {
6024 case dest_option:
6025 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6026 opt_flags);
6027 case dest_global:
6028 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006029 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6030 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006031 case dest_buffer:
6032 // include b: with the name, easier to execute that way
6033 return generate_STORE(cctx, ISN_STOREB, 0, name);
6034 case dest_window:
6035 // include w: with the name, easier to execute that way
6036 return generate_STORE(cctx, ISN_STOREW, 0, name);
6037 case dest_tab:
6038 // include t: with the name, easier to execute that way
6039 return generate_STORE(cctx, ISN_STORET, 0, name);
6040 case dest_env:
6041 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6042 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006043 return generate_STORE(cctx, ISN_STOREREG,
6044 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006045 case dest_vimvar:
6046 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6047 case dest_script:
6048 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006049 // "s:" may be included in the name.
6050 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006051 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006052 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6053 scriptvar_sid, scriptvar_idx, type);
6054 case dest_local:
6055 case dest_expr:
6056 // cannot happen
6057 break;
6058 }
6059 return FAIL;
6060}
6061
Bram Moolenaar752fc692021-01-04 21:57:11 +01006062 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006063generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6064{
6065 if (lhs->lhs_dest != dest_local)
6066 return generate_store_var(cctx, lhs->lhs_dest,
6067 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6068 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6069 lhs->lhs_type, lhs->lhs_name);
6070
6071 if (lhs->lhs_lvar != NULL)
6072 {
6073 garray_T *instr = &cctx->ctx_instr;
6074 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6075
6076 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6077 // ISN_STORENR
6078 if (lhs->lhs_lvar->lv_from_outer == 0
6079 && instr->ga_len == instr_count + 1
6080 && isn->isn_type == ISN_PUSHNR)
6081 {
6082 varnumber_T val = isn->isn_arg.number;
6083 garray_T *stack = &cctx->ctx_type_stack;
6084
6085 isn->isn_type = ISN_STORENR;
6086 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6087 isn->isn_arg.storenr.stnr_val = val;
6088 if (stack->ga_len > 0)
6089 --stack->ga_len;
6090 }
6091 else if (lhs->lhs_lvar->lv_from_outer > 0)
6092 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6093 lhs->lhs_lvar->lv_from_outer);
6094 else
6095 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6096 }
6097 return OK;
6098}
6099
6100 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006101is_decl_command(int cmdidx)
6102{
6103 return cmdidx == CMD_let || cmdidx == CMD_var
6104 || cmdidx == CMD_final || cmdidx == CMD_const;
6105}
6106
6107/*
6108 * Figure out the LHS type and other properties for an assignment or one item
6109 * of ":unlet" with an index.
6110 * Returns OK or FAIL.
6111 */
6112 static int
6113compile_lhs(
6114 char_u *var_start,
6115 lhs_T *lhs,
6116 int cmdidx,
6117 int heredoc,
6118 int oplen,
6119 cctx_T *cctx)
6120{
6121 char_u *var_end;
6122 int is_decl = is_decl_command(cmdidx);
6123
6124 CLEAR_POINTER(lhs);
6125 lhs->lhs_dest = dest_local;
6126 lhs->lhs_vimvaridx = -1;
6127 lhs->lhs_scriptvar_idx = -1;
6128
6129 // "dest_end" is the end of the destination, including "[expr]" or
6130 // ".name".
6131 // "var_end" is the end of the variable/option/etc. name.
6132 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6133 if (*var_start == '@')
6134 var_end = var_start + 2;
6135 else
6136 {
6137 // skip over the leading "&", "&l:", "&g:" and "$"
6138 var_end = skip_option_env_lead(var_start);
6139 var_end = to_name_end(var_end, TRUE);
6140 }
6141
6142 // "a: type" is declaring variable "a" with a type, not dict "a:".
6143 if (is_decl && lhs->lhs_dest_end == var_start + 2
6144 && lhs->lhs_dest_end[-1] == ':')
6145 --lhs->lhs_dest_end;
6146 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6147 --var_end;
6148
6149 // compute the length of the destination without "[expr]" or ".name"
6150 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006151 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006152 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6153 if (lhs->lhs_name == NULL)
6154 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006155
6156 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6157 // Something follows after the variable: "var[idx]" or "var.key".
6158 lhs->lhs_has_index = TRUE;
6159
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160 if (heredoc)
6161 lhs->lhs_type = &t_list_string;
6162 else
6163 lhs->lhs_type = &t_any;
6164
6165 if (cctx->ctx_skip != SKIP_YES)
6166 {
6167 int declare_error = FALSE;
6168
6169 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6170 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6171 &lhs->lhs_type, cctx) == FAIL)
6172 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006173 if (lhs->lhs_dest != dest_local
6174 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 {
6176 // Specific kind of variable recognized.
6177 declare_error = is_decl;
6178 }
6179 else
6180 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006181 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006182 if (check_reserved_name(lhs->lhs_name) == FAIL)
6183 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184
6185 if (lookup_local(var_start, lhs->lhs_varlen,
6186 &lhs->lhs_local_lvar, cctx) == OK)
6187 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6188 else
6189 {
6190 CLEAR_FIELD(lhs->lhs_arg_lvar);
6191 if (arg_exists(var_start, lhs->lhs_varlen,
6192 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6193 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6194 {
6195 if (is_decl)
6196 {
6197 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6198 return FAIL;
6199 }
6200 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6201 }
6202 }
6203 if (lhs->lhs_lvar != NULL)
6204 {
6205 if (is_decl)
6206 {
6207 semsg(_(e_variable_already_declared), lhs->lhs_name);
6208 return FAIL;
6209 }
6210 }
6211 else
6212 {
6213 int script_namespace = lhs->lhs_varlen > 1
6214 && STRNCMP(var_start, "s:", 2) == 0;
6215 int script_var = (script_namespace
6216 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006217 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006219 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006220 imported_T *import =
6221 find_imported(var_start, lhs->lhs_varlen, cctx);
6222
6223 if (script_namespace || script_var || import != NULL)
6224 {
6225 char_u *rawname = lhs->lhs_name
6226 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6227
6228 if (is_decl)
6229 {
6230 if (script_namespace)
6231 semsg(_(e_cannot_declare_script_variable_in_function),
6232 lhs->lhs_name);
6233 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006234 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006235 lhs->lhs_name);
6236 return FAIL;
6237 }
6238 else if (cctx->ctx_ufunc->uf_script_ctx_version
6239 == SCRIPT_VERSION_VIM9
6240 && script_namespace
6241 && !script_var && import == NULL)
6242 {
6243 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6244 return FAIL;
6245 }
6246
6247 lhs->lhs_dest = dest_script;
6248
6249 // existing script-local variables should have a type
6250 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6251 if (import != NULL)
6252 lhs->lhs_scriptvar_sid = import->imp_sid;
6253 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6254 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006255 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006257 lhs->lhs_scriptvar_sid, rawname,
6258 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6259 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006260 if (lhs->lhs_scriptvar_idx >= 0)
6261 {
6262 scriptitem_T *si = SCRIPT_ITEM(
6263 lhs->lhs_scriptvar_sid);
6264 svar_T *sv =
6265 ((svar_T *)si->sn_var_vals.ga_data)
6266 + lhs->lhs_scriptvar_idx;
6267 lhs->lhs_type = sv->sv_type;
6268 }
6269 }
6270 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006271 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 == FAIL)
6273 return FAIL;
6274 }
6275 }
6276
6277 if (declare_error)
6278 {
6279 vim9_declare_error(lhs->lhs_name);
6280 return FAIL;
6281 }
6282 }
6283
6284 // handle "a:name" as a name, not index "name" on "a"
6285 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6286 var_end = lhs->lhs_dest_end;
6287
6288 if (lhs->lhs_dest != dest_option)
6289 {
6290 if (is_decl && *var_end == ':')
6291 {
6292 char_u *p;
6293
6294 // parse optional type: "let var: type = expr"
6295 if (!VIM_ISWHITE(var_end[1]))
6296 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006297 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006298 return FAIL;
6299 }
6300 p = skipwhite(var_end + 1);
6301 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6302 if (lhs->lhs_type == NULL)
6303 return FAIL;
6304 lhs->lhs_has_type = TRUE;
6305 }
6306 else if (lhs->lhs_lvar != NULL)
6307 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6308 }
6309
Bram Moolenaare42939a2021-04-05 17:11:17 +02006310 if (oplen == 3 && !heredoc
6311 && lhs->lhs_dest != dest_global
6312 && !lhs->lhs_has_index
6313 && lhs->lhs_type->tt_type != VAR_STRING
6314 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 {
6316 emsg(_(e_can_only_concatenate_to_string));
6317 return FAIL;
6318 }
6319
6320 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6321 && cctx->ctx_skip != SKIP_YES)
6322 {
6323 if (oplen > 1 && !heredoc)
6324 {
6325 // +=, /=, etc. require an existing variable
6326 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6327 return FAIL;
6328 }
6329 if (!is_decl)
6330 {
6331 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6332 return FAIL;
6333 }
6334
Bram Moolenaar3f327882021-03-17 20:56:38 +01006335 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 if ((lhs->lhs_type->tt_type == VAR_FUNC
6337 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006338 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006340
6341 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006342 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6343 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6344 if (lhs->lhs_lvar == NULL)
6345 return FAIL;
6346 lhs->lhs_new_local = TRUE;
6347 }
6348
6349 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006350 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006351 {
6352 // Something follows after the variable: "var[idx]" or "var.key".
6353 // TODO: should we also handle "->func()" here?
6354 if (is_decl)
6355 {
6356 emsg(_(e_cannot_use_index_when_declaring_variable));
6357 return FAIL;
6358 }
6359
6360 if (var_start[lhs->lhs_varlen] == '['
6361 || var_start[lhs->lhs_varlen] == '.')
6362 {
6363 char_u *after = var_start + lhs->lhs_varlen;
6364 char_u *p;
6365
6366 // Only the last index is used below, if there are others
6367 // before it generate code for the expression. Thus for
6368 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6369 for (;;)
6370 {
6371 p = skip_index(after);
6372 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006373 {
6374 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006376 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006377 after = p;
6378 }
6379 if (after > var_start + lhs->lhs_varlen)
6380 {
6381 lhs->lhs_varlen = after - var_start;
6382 lhs->lhs_dest = dest_expr;
6383 // We don't know the type before evaluating the expression,
6384 // use "any" until then.
6385 lhs->lhs_type = &t_any;
6386 }
6387
Bram Moolenaar752fc692021-01-04 21:57:11 +01006388 if (lhs->lhs_type->tt_member == NULL)
6389 lhs->lhs_member_type = &t_any;
6390 else
6391 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6392 }
6393 else
6394 {
6395 semsg("Not supported yet: %s", var_start);
6396 return FAIL;
6397 }
6398 }
6399 return OK;
6400}
6401
6402/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006403 * Figure out the LHS and check a few errors.
6404 */
6405 static int
6406compile_assign_lhs(
6407 char_u *var_start,
6408 lhs_T *lhs,
6409 int cmdidx,
6410 int is_decl,
6411 int heredoc,
6412 int oplen,
6413 cctx_T *cctx)
6414{
6415 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6416 return FAIL;
6417
6418 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6419 {
6420 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6421 return FAIL;
6422 }
6423 if (!is_decl && lhs->lhs_lvar != NULL
6424 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6425 {
6426 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6427 return FAIL;
6428 }
6429 return OK;
6430}
6431
6432/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006433 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6434 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 */
6436 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006437compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006438 char_u *var_start,
6439 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006440 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 cctx_T *cctx)
6442{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006444 char_u *p;
6445 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006446 int need_white_before = TRUE;
6447 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448
Bram Moolenaar752fc692021-01-04 21:57:11 +01006449 p = var_start + varlen;
6450 if (*p == '[')
6451 {
6452 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006453 if (*p == ':')
6454 {
6455 // empty first index, push zero
6456 r = generate_PUSHNR(cctx, 0);
6457 need_white_before = FALSE;
6458 }
6459 else
6460 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006461
6462 if (r == OK && *skipwhite(p) == ':')
6463 {
6464 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006465 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006466 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006467 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006468 empty_second = *skipwhite(p + 1) == ']';
6469 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6470 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006471 {
6472 semsg(_(e_white_space_required_before_and_after_str_at_str),
6473 ":", p);
6474 return FAIL;
6475 }
6476 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006477 if (*p == ']')
6478 // empty second index, push "none"
6479 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6480 else
6481 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006482 }
6483
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 if (r == OK && *skipwhite(p) != ']')
6485 {
6486 // this should not happen
6487 emsg(_(e_missbrac));
6488 r = FAIL;
6489 }
6490 }
6491 else // if (*p == '.')
6492 {
6493 char_u *key_end = to_name_end(p + 1, TRUE);
6494 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6495
6496 r = generate_PUSHS(cctx, key);
6497 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006498 return r;
6499}
6500
6501/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006502 * For a LHS with an index, load the variable to be indexed.
6503 */
6504 static int
6505compile_load_lhs(
6506 lhs_T *lhs,
6507 char_u *var_start,
6508 type_T *rhs_type,
6509 cctx_T *cctx)
6510{
6511 if (lhs->lhs_dest == dest_expr)
6512 {
6513 size_t varlen = lhs->lhs_varlen;
6514 int c = var_start[varlen];
6515 char_u *p = var_start;
6516 garray_T *stack = &cctx->ctx_type_stack;
6517
6518 // Evaluate "ll[expr]" of "ll[expr][idx]"
6519 var_start[varlen] = NUL;
6520 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6521 {
6522 // this should not happen
6523 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006524 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006525 return FAIL;
6526 }
6527 var_start[varlen] = c;
6528
6529 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6530 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6531 // now we can properly check the type
6532 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6533 && rhs_type != &t_void
6534 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6535 FALSE, FALSE) == FAIL)
6536 return FAIL;
6537 }
6538 else
6539 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6540 lhs->lhs_lvar, lhs->lhs_type);
6541 return OK;
6542}
6543
6544/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006545 * Produce code for loading "lhs" and also take care of an index.
6546 * Return OK/FAIL.
6547 */
6548 static int
6549compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6550{
6551 compile_load_lhs(lhs, var_start, NULL, cctx);
6552
6553 if (lhs->lhs_has_index)
6554 {
6555 int range = FALSE;
6556
6557 // Get member from list or dict. First compile the
6558 // index value.
6559 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6560 return FAIL;
6561 if (range)
6562 {
6563 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6564 var_start);
6565 return FAIL;
6566 }
6567
6568 // Get the member.
6569 if (compile_member(FALSE, cctx) == FAIL)
6570 return FAIL;
6571 }
6572 return OK;
6573}
6574
6575/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006576 * Assignment to a list or dict member, or ":unlet" for the item, using the
6577 * information in "lhs".
6578 * Returns OK or FAIL.
6579 */
6580 static int
6581compile_assign_unlet(
6582 char_u *var_start,
6583 lhs_T *lhs,
6584 int is_assign,
6585 type_T *rhs_type,
6586 cctx_T *cctx)
6587{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006588 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006589 garray_T *stack = &cctx->ctx_type_stack;
6590 int range = FALSE;
6591
Bram Moolenaar68452172021-04-12 21:21:02 +02006592 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006593 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006594 if (is_assign && range && lhs->lhs_type != &t_blob
6595 && lhs->lhs_type != &t_any)
6596 {
6597 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6598 return FAIL;
6599 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006600
6601 if (lhs->lhs_type == &t_any)
6602 {
6603 // Index on variable of unknown type: check at runtime.
6604 dest_type = VAR_ANY;
6605 }
6606 else
6607 {
6608 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006609 if (dest_type == VAR_DICT && range)
6610 {
6611 emsg(e_cannot_use_range_with_dictionary);
6612 return FAIL;
6613 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006614 if (dest_type == VAR_DICT
6615 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006617 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006618 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006619 type_T *type;
6620
6621 if (range)
6622 {
6623 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6624 if (need_type(type, &t_number,
6625 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006626 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006627 }
6628 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006629 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006630 && need_type(type, &t_number,
6631 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006632 return FAIL;
6633 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006634 }
6635
6636 // Load the dict or list. On the stack we then have:
6637 // - value (for assignment, not for :unlet)
6638 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006639 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006641 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6642 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006643
Bram Moolenaar68452172021-04-12 21:21:02 +02006644 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6645 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646 {
6647 if (is_assign)
6648 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006649 if (range)
6650 {
6651 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6652 return FAIL;
6653 }
6654 else
6655 {
6656 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006657
Bram Moolenaar68452172021-04-12 21:21:02 +02006658 if (isn == NULL)
6659 return FAIL;
6660 isn->isn_arg.vartype = dest_type;
6661 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006662 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006663 else if (range)
6664 {
6665 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6666 return FAIL;
6667 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006668 else
6669 {
6670 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6671 return FAIL;
6672 }
6673 }
6674 else
6675 {
6676 emsg(_(e_indexable_type_required));
6677 return FAIL;
6678 }
6679
6680 return OK;
6681}
6682
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006683/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006684 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006685 * "let name"
6686 * "var name = expr"
6687 * "final name = expr"
6688 * "const name = expr"
6689 * "name = expr"
6690 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006691 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006692 * Return NULL for an error.
6693 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 */
6695 static char_u *
6696compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6697{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006698 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006700 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 char_u *ret = NULL;
6702 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006703 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006705 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006707 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 int oplen = 0;
6710 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006711 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006712 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006714 int is_decl = is_decl_command(cmdidx);
6715 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006716 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006718 // Skip over the "var" or "[var, var]" to get to any "=".
6719 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6720 if (p == NULL)
6721 return *arg == '[' ? arg : NULL;
6722
6723 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006725 // TODO: should we allow this, and figure out type inference from list
6726 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006727 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 return NULL;
6729 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006730 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 sp = p;
6733 p = skipwhite(p);
6734 op = p;
6735 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006736
6737 if (var_count > 0 && oplen == 0)
6738 // can be something like "[1, 2]->func()"
6739 return arg;
6740
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006741 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006743 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006744 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006745 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006746 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6747 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006748 if (VIM_ISWHITE(eap->cmd[2]))
6749 {
6750 semsg(_(e_no_white_space_allowed_after_str_str),
6751 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6752 return NULL;
6753 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006754 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6755 oplen = 2;
6756 incdec = TRUE;
6757 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 if (heredoc)
6760 {
6761 list_T *l;
6762 listitem_T *li;
6763
6764 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006765 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006767 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006768 if (l == NULL)
6769 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
Bram Moolenaar078269b2020-09-21 20:35:55 +02006771 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006773 // Push each line and the create the list.
6774 FOR_ALL_LIST_ITEMS(l, li)
6775 {
6776 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6777 li->li_tv.vval.v_string = NULL;
6778 }
6779 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 list_free(l);
6782 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006783 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006785 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006787 char_u *wp;
6788
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006789 // for "[var, var] = expr" evaluate the expression here, loop over the
6790 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006791 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006792
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006793 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006794 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006795 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006796 if (compile_expr0(&p, cctx) == FAIL)
6797 return NULL;
6798 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006800 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006802 type_T *stacktype;
6803
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006804 stacktype = stack->ga_len == 0 ? &t_void
6805 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006808 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 goto theend;
6810 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006811 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006812 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006813 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006814 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006815 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6816 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006817 if (stacktype->tt_member != NULL)
6818 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006819 }
6820 }
6821
6822 /*
6823 * Loop over variables in "[var, var] = expr".
6824 * For "var = expr" and "let var: type" this is done only once.
6825 */
6826 if (var_count > 0)
6827 var_start = skipwhite(arg + 1); // skip over the "["
6828 else
6829 var_start = arg;
6830 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6831 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006832 int instr_count = -1;
6833 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006834
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006835 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6836 {
6837 // Ignore underscore in "[a, _, b] = list".
6838 if (var_count > 0)
6839 {
6840 var_start = skipwhite(var_start + 2);
6841 continue;
6842 }
6843 emsg(_(e_cannot_use_underscore_here));
6844 goto theend;
6845 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 vim_free(lhs.lhs_name);
6847
6848 /*
6849 * Figure out the LHS type and other properties.
6850 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006851 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6852 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006853 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006854 if (!heredoc)
6855 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006856 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006857 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006858 if (oplen > 0 && var_count == 0)
6859 {
6860 // skip over the "=" and the expression
6861 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006862 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006863 }
6864 }
6865 else if (oplen > 0)
6866 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006867 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006868 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006869
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006870 // for "+=", "*=", "..=" etc. first load the current value
6871 if (*op != '='
6872 && compile_load_lhs_with_index(&lhs, var_start,
6873 cctx) == FAIL)
6874 goto theend;
6875
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006876 // For "var = expr" evaluate the expression.
6877 if (var_count == 0)
6878 {
6879 int r;
6880
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006881 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006882 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006883 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006884 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006885 r = generate_PUSHNR(cctx, 1);
6886 }
6887 else
6888 {
6889 // Temporarily hide the new local variable here, it is
6890 // not available to this expression.
6891 if (lhs.lhs_new_local)
6892 --cctx->ctx_locals.ga_len;
6893 wp = op + oplen;
6894 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6895 {
6896 if (lhs.lhs_new_local)
6897 ++cctx->ctx_locals.ga_len;
6898 goto theend;
6899 }
6900 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006901 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006902 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006903 if (r == FAIL)
6904 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006905 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006906 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006907 else if (semicolon && var_idx == var_count - 1)
6908 {
6909 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006910 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006911 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6912 goto theend;
6913 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006914 else
6915 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006916 // For "[var, var] = expr" get the "var_idx" item from the
6917 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006918 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006919 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006920 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006921
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006922 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006923 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006924 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006925 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006926 if ((rhs_type->tt_type == VAR_FUNC
6927 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006928 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006930 goto theend;
6931
Bram Moolenaar752fc692021-01-04 21:57:11 +01006932 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006933 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006934 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006936 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006937 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006938 }
6939 else
6940 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006941 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006942 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006943 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006944 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006945 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006946 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006947 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006948 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006949 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006950 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006951 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006952 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006953 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006954 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006955 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006956
Bram Moolenaar77709b12021-04-03 21:01:01 +02006957 // Without operator check type here, otherwise below.
6958 // Use the line number of the assignment.
6959 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006960 if (lhs.lhs_has_index)
6961 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006962 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006963 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006964 goto theend;
6965 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006966 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006967 else
6968 {
6969 type_T *lhs_type = lhs.lhs_member_type;
6970
6971 // Special case: assigning to @# can use a number or a
6972 // string.
6973 if (lhs_type == &t_number_or_string
6974 && rhs_type->tt_type == VAR_NUMBER)
6975 lhs_type = &t_number;
6976 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006977 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006978 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006981 else if (cmdidx == CMD_final)
6982 {
6983 emsg(_(e_final_requires_a_value));
6984 goto theend;
6985 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006986 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006988 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 goto theend;
6990 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006991 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006992 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006993 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006994 goto theend;
6995 }
6996 else
6997 {
6998 // variables are always initialized
6999 if (ga_grow(instr, 1) == FAIL)
7000 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007001 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007002 {
7003 case VAR_BOOL:
7004 generate_PUSHBOOL(cctx, VVAL_FALSE);
7005 break;
7006 case VAR_FLOAT:
7007#ifdef FEAT_FLOAT
7008 generate_PUSHF(cctx, 0.0);
7009#endif
7010 break;
7011 case VAR_STRING:
7012 generate_PUSHS(cctx, NULL);
7013 break;
7014 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007015 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007016 break;
7017 case VAR_FUNC:
7018 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7019 break;
7020 case VAR_LIST:
7021 generate_NEWLIST(cctx, 0);
7022 break;
7023 case VAR_DICT:
7024 generate_NEWDICT(cctx, 0);
7025 break;
7026 case VAR_JOB:
7027 generate_PUSHJOB(cctx, NULL);
7028 break;
7029 case VAR_CHANNEL:
7030 generate_PUSHCHANNEL(cctx, NULL);
7031 break;
7032 case VAR_NUMBER:
7033 case VAR_UNKNOWN:
7034 case VAR_ANY:
7035 case VAR_PARTIAL:
7036 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007037 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007038 case VAR_SPECIAL: // cannot happen
7039 generate_PUSHNR(cctx, 0);
7040 break;
7041 }
7042 }
7043 if (var_count == 0)
7044 end = p;
7045 }
7046
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007047 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007048 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007049 break;
7050
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007051 if (oplen > 0 && *op != '=')
7052 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007053 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007054 type_T *stacktype;
7055
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007056 if (*op == '.')
7057 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007058 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007059 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007060 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007061 if (
7062#ifdef FEAT_FLOAT
7063 // If variable is float operation with number is OK.
7064 !(expected == &t_float && stacktype == &t_number) &&
7065#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007066 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007067 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007068 goto theend;
7069
7070 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007071 {
7072 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7073 goto theend;
7074 }
7075 else if (*op == '+')
7076 {
7077 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007078 operator_type(lhs.lhs_member_type, stacktype),
7079 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007080 goto theend;
7081 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007082 else if (generate_two_op(cctx, op) == FAIL)
7083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007086 // Use the line number of the assignment for store instruction.
7087 save_lnum = cctx->ctx_lnum;
7088 cctx->ctx_lnum = start_lnum - 1;
7089
Bram Moolenaar752fc692021-01-04 21:57:11 +01007090 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007091 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007092 // Use the info in "lhs" to store the value at the index in the
7093 // list or dict.
7094 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7095 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007096 {
7097 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007098 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007099 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007100 }
7101 else
7102 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007103 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007104 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007105 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007106 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007107 generate_LOCKCONST(cctx);
7108
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007109 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007110 && (lhs.lhs_type->tt_type == VAR_DICT
7111 || lhs.lhs_type->tt_type == VAR_LIST)
7112 && lhs.lhs_type->tt_member != NULL
7113 && lhs.lhs_type->tt_member != &t_any
7114 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007115 // Set the type in the list or dict, so that it can be checked,
7116 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007117 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007118
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007119 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007120 {
7121 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007122 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007123 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007124 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007125 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007126
7127 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007128 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007130
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007131 // For "[var, var] = expr" drop the "expr" value.
7132 // Also for "[var, var; _] = expr".
7133 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007134 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007135 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007136 goto theend;
7137 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007138
Bram Moolenaarb2097502020-07-19 17:17:02 +02007139 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140
7141theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007142 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 return ret;
7144}
7145
7146/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007147 * Check for an assignment at "eap->cmd", compile it if found.
7148 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7149 */
7150 static int
7151may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7152{
7153 char_u *pskip;
7154 char_u *p;
7155
7156 // Assuming the command starts with a variable or function name,
7157 // find what follows.
7158 // Skip over "var.member", "var[idx]" and the like.
7159 // Also "&opt = val", "$ENV = val" and "@r = val".
7160 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7161 ? eap->cmd + 1 : eap->cmd;
7162 p = to_name_end(pskip, TRUE);
7163 if (p > eap->cmd && *p != NUL)
7164 {
7165 char_u *var_end;
7166 int oplen;
7167 int heredoc;
7168
7169 if (eap->cmd[0] == '@')
7170 var_end = eap->cmd + 2;
7171 else
7172 var_end = find_name_end(pskip, NULL, NULL,
7173 FNE_CHECK_START | FNE_INCL_BR);
7174 oplen = assignment_len(skipwhite(var_end), &heredoc);
7175 if (oplen > 0)
7176 {
7177 size_t len = p - eap->cmd;
7178
7179 // Recognize an assignment if we recognize the variable
7180 // name:
7181 // "g:var = expr"
7182 // "local = expr" where "local" is a local var.
7183 // "script = expr" where "script" is a script-local var.
7184 // "import = expr" where "import" is an imported var
7185 // "&opt = expr"
7186 // "$ENV = expr"
7187 // "@r = expr"
7188 if (*eap->cmd == '&'
7189 || *eap->cmd == '$'
7190 || *eap->cmd == '@'
7191 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007192 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007193 {
7194 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7195 if (*line == NULL || *line == eap->cmd)
7196 return FAIL;
7197 return OK;
7198 }
7199 }
7200 }
7201
7202 if (*eap->cmd == '[')
7203 {
7204 // [var, var] = expr
7205 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7206 if (*line == NULL)
7207 return FAIL;
7208 if (*line != eap->cmd)
7209 return OK;
7210 }
7211 return NOTDONE;
7212}
7213
7214/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007215 * Check if "name" can be "unlet".
7216 */
7217 int
7218check_vim9_unlet(char_u *name)
7219{
7220 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7221 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007222 // "unlet s:var" is allowed in legacy script.
7223 if (*name == 's' && !script_is_vim9())
7224 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007225 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007226 return FAIL;
7227 }
7228 return OK;
7229}
7230
7231/*
7232 * Callback passed to ex_unletlock().
7233 */
7234 static int
7235compile_unlet(
7236 lval_T *lvp,
7237 char_u *name_end,
7238 exarg_T *eap,
7239 int deep UNUSED,
7240 void *coookie)
7241{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007242 cctx_T *cctx = coookie;
7243 char_u *p = lvp->ll_name;
7244 int cc = *name_end;
7245 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007246
Bram Moolenaar752fc692021-01-04 21:57:11 +01007247 if (cctx->ctx_skip == SKIP_YES)
7248 return OK;
7249
7250 *name_end = NUL;
7251 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007252 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007253 // :unlet $ENV_VAR
7254 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7255 }
7256 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7257 {
7258 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007259
Bram Moolenaar752fc692021-01-04 21:57:11 +01007260 // This is similar to assigning: lookup the list/dict, compile the
7261 // idx/key. Then instead of storing the value unlet the item.
7262 // unlet {list}[idx]
7263 // unlet {dict}[key] dict.key
7264 //
7265 // Figure out the LHS type and other properties.
7266 //
7267 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7268
7269 // : unlet an indexed item
7270 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007271 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007272 iemsg("called compile_lhs() without an index");
7273 ret = FAIL;
7274 }
7275 else
7276 {
7277 // Use the info in "lhs" to unlet the item at the index in the
7278 // list or dict.
7279 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007280 }
7281
Bram Moolenaar752fc692021-01-04 21:57:11 +01007282 vim_free(lhs.lhs_name);
7283 }
7284 else if (check_vim9_unlet(p) == FAIL)
7285 {
7286 ret = FAIL;
7287 }
7288 else
7289 {
7290 // Normal name. Only supports g:, w:, t: and b: namespaces.
7291 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007292 }
7293
Bram Moolenaar752fc692021-01-04 21:57:11 +01007294 *name_end = cc;
7295 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007296}
7297
7298/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007299 * Callback passed to ex_unletlock().
7300 */
7301 static int
7302compile_lock_unlock(
7303 lval_T *lvp,
7304 char_u *name_end,
7305 exarg_T *eap,
7306 int deep UNUSED,
7307 void *coookie)
7308{
7309 cctx_T *cctx = coookie;
7310 int cc = *name_end;
7311 char_u *p = lvp->ll_name;
7312 int ret = OK;
7313 size_t len;
7314 char_u *buf;
7315
7316 if (cctx->ctx_skip == SKIP_YES)
7317 return OK;
7318
7319 // Cannot use :lockvar and :unlockvar on local variables.
7320 if (p[1] != ':')
7321 {
7322 char_u *end = skip_var_one(p, FALSE);
7323
7324 if (lookup_local(p, end - p, NULL, cctx) == OK)
7325 {
7326 emsg(_(e_cannot_lock_unlock_local_variable));
7327 return FAIL;
7328 }
7329 }
7330
7331 // Checking is done at runtime.
7332 *name_end = NUL;
7333 len = name_end - p + 20;
7334 buf = alloc(len);
7335 if (buf == NULL)
7336 ret = FAIL;
7337 else
7338 {
7339 vim_snprintf((char *)buf, len, "%s %s",
7340 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7341 p);
7342 ret = generate_EXEC(cctx, buf);
7343
7344 vim_free(buf);
7345 *name_end = cc;
7346 }
7347 return ret;
7348}
7349
7350/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007351 * compile "unlet var", "lock var" and "unlock var"
7352 * "arg" points to "var".
7353 */
7354 static char_u *
7355compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7356{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007357 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7358 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7359 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007360 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7361}
7362
7363/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7365 */
7366 static int
7367compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7368{
7369 garray_T *instr = &cctx->ctx_instr;
7370 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7371
7372 if (endlabel == NULL)
7373 return FAIL;
7374 endlabel->el_next = *el;
7375 *el = endlabel;
7376 endlabel->el_end_label = instr->ga_len;
7377
7378 generate_JUMP(cctx, when, 0);
7379 return OK;
7380}
7381
7382 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007383compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384{
7385 garray_T *instr = &cctx->ctx_instr;
7386
7387 while (*el != NULL)
7388 {
7389 endlabel_T *cur = (*el);
7390 isn_T *isn;
7391
7392 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007393 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 *el = cur->el_next;
7395 vim_free(cur);
7396 }
7397}
7398
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007399 static void
7400compile_free_jump_to_end(endlabel_T **el)
7401{
7402 while (*el != NULL)
7403 {
7404 endlabel_T *cur = (*el);
7405
7406 *el = cur->el_next;
7407 vim_free(cur);
7408 }
7409}
7410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411/*
7412 * Create a new scope and set up the generic items.
7413 */
7414 static scope_T *
7415new_scope(cctx_T *cctx, scopetype_T type)
7416{
7417 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7418
7419 if (scope == NULL)
7420 return NULL;
7421 scope->se_outer = cctx->ctx_scope;
7422 cctx->ctx_scope = scope;
7423 scope->se_type = type;
7424 scope->se_local_count = cctx->ctx_locals.ga_len;
7425 return scope;
7426}
7427
7428/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007429 * Free the current scope and go back to the outer scope.
7430 */
7431 static void
7432drop_scope(cctx_T *cctx)
7433{
7434 scope_T *scope = cctx->ctx_scope;
7435
7436 if (scope == NULL)
7437 {
7438 iemsg("calling drop_scope() without a scope");
7439 return;
7440 }
7441 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007442 switch (scope->se_type)
7443 {
7444 case IF_SCOPE:
7445 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7446 case FOR_SCOPE:
7447 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7448 case WHILE_SCOPE:
7449 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7450 case TRY_SCOPE:
7451 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7452 case NO_SCOPE:
7453 case BLOCK_SCOPE:
7454 break;
7455 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007456 vim_free(scope);
7457}
7458
7459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 * compile "if expr"
7461 *
7462 * "if expr" Produces instructions:
7463 * EVAL expr Push result of "expr"
7464 * JUMP_IF_FALSE end
7465 * ... body ...
7466 * end:
7467 *
7468 * "if expr | else" Produces instructions:
7469 * EVAL expr Push result of "expr"
7470 * JUMP_IF_FALSE else
7471 * ... body ...
7472 * JUMP_ALWAYS end
7473 * else:
7474 * ... body ...
7475 * end:
7476 *
7477 * "if expr1 | elseif expr2 | else" Produces instructions:
7478 * EVAL expr Push result of "expr"
7479 * JUMP_IF_FALSE elseif
7480 * ... body ...
7481 * JUMP_ALWAYS end
7482 * elseif:
7483 * EVAL expr Push result of "expr"
7484 * JUMP_IF_FALSE else
7485 * ... body ...
7486 * JUMP_ALWAYS end
7487 * else:
7488 * ... body ...
7489 * end:
7490 */
7491 static char_u *
7492compile_if(char_u *arg, cctx_T *cctx)
7493{
7494 char_u *p = arg;
7495 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007496 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007498 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007499 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500
Bram Moolenaara5565e42020-05-09 15:44:01 +02007501 CLEAR_FIELD(ppconst);
7502 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007503 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007504 clear_ppconst(&ppconst);
7505 return NULL;
7506 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007507 if (!ends_excmd2(arg, skipwhite(p)))
7508 {
7509 semsg(_(e_trailing_arg), p);
7510 return NULL;
7511 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007512 if (cctx->ctx_skip == SKIP_YES)
7513 clear_ppconst(&ppconst);
7514 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007515 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007516 int error = FALSE;
7517 int v;
7518
Bram Moolenaara5565e42020-05-09 15:44:01 +02007519 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007520 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007521 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007522 if (error)
7523 return NULL;
7524 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007525 }
7526 else
7527 {
7528 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007529 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007530 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007531 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007532 if (bool_on_stack(cctx) == FAIL)
7533 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535
Bram Moolenaara91a7132021-03-25 21:12:15 +01007536 // CMDMOD_REV must come before the jump
7537 generate_undo_cmdmods(cctx);
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 scope = new_scope(cctx, IF_SCOPE);
7540 if (scope == NULL)
7541 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007542 scope->se_skip_save = skip_save;
7543 // "is_had_return" will be reset if any block does not end in :return
7544 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007546 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007547 {
7548 // "where" is set when ":elseif", "else" or ":endif" is found
7549 scope->se_u.se_if.is_if_label = instr->ga_len;
7550 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7551 }
7552 else
7553 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554
Bram Moolenaarced68a02021-01-24 17:53:47 +01007555#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007556 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007557 && skip_save != SKIP_YES)
7558 {
7559 // generated a profile start, need to generate a profile end, since it
7560 // won't be done after returning
7561 cctx->ctx_skip = SKIP_NOT;
7562 generate_instr(cctx, ISN_PROF_END);
7563 cctx->ctx_skip = SKIP_YES;
7564 }
7565#endif
7566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 return p;
7568}
7569
7570 static char_u *
7571compile_elseif(char_u *arg, cctx_T *cctx)
7572{
7573 char_u *p = arg;
7574 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007575 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 isn_T *isn;
7577 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007578 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007579 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580
7581 if (scope == NULL || scope->se_type != IF_SCOPE)
7582 {
7583 emsg(_(e_elseif_without_if));
7584 return NULL;
7585 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007586 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007587 if (!cctx->ctx_had_return)
7588 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589
Bram Moolenaarced68a02021-01-24 17:53:47 +01007590 if (cctx->ctx_skip == SKIP_NOT)
7591 {
7592 // previous block was executed, this one and following will not
7593 cctx->ctx_skip = SKIP_YES;
7594 scope->se_u.se_if.is_seen_skip_not = TRUE;
7595 }
7596 if (scope->se_u.se_if.is_seen_skip_not)
7597 {
7598 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007599 // Do not count the "elseif" for profiling and cmdmod
7600 instr->ga_len = current_instr_idx(cctx);
7601
Bram Moolenaarced68a02021-01-24 17:53:47 +01007602 skip_expr_cctx(&p, cctx);
7603 return p;
7604 }
7605
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007606 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007607 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007608 int moved_cmdmod = FALSE;
7609
7610 // Move any CMDMOD instruction to after the jump
7611 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7612 {
7613 if (ga_grow(instr, 1) == FAIL)
7614 return NULL;
7615 ((isn_T *)instr->ga_data)[instr->ga_len] =
7616 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7617 --instr->ga_len;
7618 moved_cmdmod = TRUE;
7619 }
7620
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007621 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007623 return NULL;
7624 // previous "if" or "elseif" jumps here
7625 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7626 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007627 if (moved_cmdmod)
7628 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007631 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007632 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007633 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007634 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007635 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007636#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007637 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007638 {
7639 // the previous block was skipped, need to profile this line
7640 generate_instr(cctx, ISN_PROF_START);
7641 instr_count = instr->ga_len;
7642 }
7643#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007644 if (cctx->ctx_compile_type == CT_DEBUG)
7645 {
7646 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007647 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007648 instr_count = instr->ga_len;
7649 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007650 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007651 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007652 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007653 clear_ppconst(&ppconst);
7654 return NULL;
7655 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007656 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007657 if (!ends_excmd2(arg, skipwhite(p)))
7658 {
7659 semsg(_(e_trailing_arg), p);
7660 return NULL;
7661 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007662 if (scope->se_skip_save == SKIP_YES)
7663 clear_ppconst(&ppconst);
7664 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007665 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007666 int error = FALSE;
7667 int v;
7668
Bram Moolenaar7f141552020-05-09 17:35:53 +02007669 // The expression results in a constant.
7670 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007671 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7672 if (error)
7673 return NULL;
7674 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007675 clear_ppconst(&ppconst);
7676 scope->se_u.se_if.is_if_label = -1;
7677 }
7678 else
7679 {
7680 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007681 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007682 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007683 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007684 if (bool_on_stack(cctx) == FAIL)
7685 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686
Bram Moolenaara91a7132021-03-25 21:12:15 +01007687 // CMDMOD_REV must come before the jump
7688 generate_undo_cmdmods(cctx);
7689
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007690 // "where" is set when ":elseif", "else" or ":endif" is found
7691 scope->se_u.se_if.is_if_label = instr->ga_len;
7692 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
7695 return p;
7696}
7697
7698 static char_u *
7699compile_else(char_u *arg, cctx_T *cctx)
7700{
7701 char_u *p = arg;
7702 garray_T *instr = &cctx->ctx_instr;
7703 isn_T *isn;
7704 scope_T *scope = cctx->ctx_scope;
7705
7706 if (scope == NULL || scope->se_type != IF_SCOPE)
7707 {
7708 emsg(_(e_else_without_if));
7709 return NULL;
7710 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007711 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007712 if (!cctx->ctx_had_return)
7713 scope->se_u.se_if.is_had_return = FALSE;
7714 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715
Bram Moolenaarced68a02021-01-24 17:53:47 +01007716#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007717 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007718 {
7719 if (cctx->ctx_skip == SKIP_NOT
7720 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7721 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007722 // the previous block was executed, do not count "else" for
7723 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007724 --instr->ga_len;
7725 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7726 {
7727 // the previous block was not executed, this one will, do count the
7728 // "else" for profiling
7729 cctx->ctx_skip = SKIP_NOT;
7730 generate_instr(cctx, ISN_PROF_END);
7731 generate_instr(cctx, ISN_PROF_START);
7732 cctx->ctx_skip = SKIP_YES;
7733 }
7734 }
7735#endif
7736
7737 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007738 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007739 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007740 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007741 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007742 if (!cctx->ctx_had_return
7743 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7744 JUMP_ALWAYS, cctx) == FAIL)
7745 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007746 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007747
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007748 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007749 {
7750 if (scope->se_u.se_if.is_if_label >= 0)
7751 {
7752 // previous "if" or "elseif" jumps here
7753 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7754 isn->isn_arg.jump.jump_where = instr->ga_len;
7755 scope->se_u.se_if.is_if_label = -1;
7756 }
7757 }
7758
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007759 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007760 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762
7763 return p;
7764}
7765
7766 static char_u *
7767compile_endif(char_u *arg, cctx_T *cctx)
7768{
7769 scope_T *scope = cctx->ctx_scope;
7770 ifscope_T *ifscope;
7771 garray_T *instr = &cctx->ctx_instr;
7772 isn_T *isn;
7773
Bram Moolenaarfa984412021-03-25 22:15:28 +01007774 if (misplaced_cmdmod(cctx))
7775 return NULL;
7776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 if (scope == NULL || scope->se_type != IF_SCOPE)
7778 {
7779 emsg(_(e_endif_without_if));
7780 return NULL;
7781 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007782 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007783 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007784 if (!cctx->ctx_had_return)
7785 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007787 if (scope->se_u.se_if.is_if_label >= 0)
7788 {
7789 // previous "if" or "elseif" jumps here
7790 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7791 isn->isn_arg.jump.jump_where = instr->ga_len;
7792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007794 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007795
7796#ifdef FEAT_PROFILE
7797 // even when skipping we count the endif as executed, unless the block it's
7798 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007799 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007800 && scope->se_skip_save != SKIP_YES)
7801 {
7802 cctx->ctx_skip = SKIP_NOT;
7803 generate_instr(cctx, ISN_PROF_START);
7804 }
7805#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007806 cctx->ctx_skip = scope->se_skip_save;
7807
7808 // If all the blocks end in :return and there is an :else then the
7809 // had_return flag is set.
7810 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007812 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813 return arg;
7814}
7815
7816/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007817 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 *
7819 * Produces instructions:
7820 * PUSHNR -1
7821 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007822 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7824 * - if beyond end, jump to "end"
7825 * - otherwise get item from list and push it
7826 * STORE var Store item in "var"
7827 * ... body ...
7828 * JUMP top Jump back to repeat
7829 * end: DROP Drop the result of "expr"
7830 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007831 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7832 * UNPACK 2 Split item in 2
7833 * STORE var1 Store item in "var1"
7834 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 */
7836 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007837compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007839 char_u *arg;
7840 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007841 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007843 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007844 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007845 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007846 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007849 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007851 lvar_T *loop_lvar; // loop iteration variable
7852 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007854 type_T *item_type = &t_any;
7855 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007856 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857
Bram Moolenaar792f7862020-11-23 08:31:18 +01007858 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007859 if (p == NULL)
7860 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007861 if (var_count == 0)
7862 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007863 else
7864 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865
7866 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007867 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007868 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7869 return NULL;
7870 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007872 if (*p == ':' && wp != p)
7873 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7874 else
7875 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 return NULL;
7877 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007878 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007879 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7880 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007882 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7883 // instruction.
7884 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7885 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7886 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007887 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007888 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007889 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7890 .isn_arg.debug.dbg_break_lnum;
7891 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 scope = new_scope(cctx, FOR_SCOPE);
7894 if (scope == NULL)
7895 return NULL;
7896
Bram Moolenaar792f7862020-11-23 08:31:18 +01007897 // Reserve a variable to store the loop iteration counter and initialize it
7898 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007899 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7900 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007901 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007902 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007903 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007905 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007906 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007907
7908 // compile "expr", it remains on the stack until "endfor"
7909 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007910 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007911 {
7912 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007914 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007915 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007917 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007918 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007920 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007921 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007923 semsg(_(e_for_loop_on_str_not_supported),
7924 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007925 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 return NULL;
7927 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007928
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007929 if (vartype->tt_type == VAR_STRING)
7930 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007931 else if (vartype->tt_type == VAR_BLOB)
7932 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007933 else if (vartype->tt_type == VAR_LIST
7934 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007935 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007936 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007937 item_type = vartype->tt_member;
7938 else if (vartype->tt_member->tt_type == VAR_LIST
7939 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007940 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007941 item_type = vartype->tt_member->tt_member;
7942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007944 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007945 generate_undo_cmdmods(cctx);
7946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007948 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007949
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007950 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951
Bram Moolenaar792f7862020-11-23 08:31:18 +01007952 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007953 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007954 {
7955 generate_UNPACK(cctx, var_count, semicolon);
7956 arg = skipwhite(arg + 1); // skip white after '['
7957
7958 // the list item is replaced by a number of items
7959 if (ga_grow(stack, var_count - 1) == FAIL)
7960 {
7961 drop_scope(cctx);
7962 return NULL;
7963 }
7964 --stack->ga_len;
7965 for (idx = 0; idx < var_count; ++idx)
7966 {
7967 ((type_T **)stack->ga_data)[stack->ga_len] =
7968 (semicolon && idx == 0) ? vartype : item_type;
7969 ++stack->ga_len;
7970 }
7971 }
7972
7973 for (idx = 0; idx < var_count; ++idx)
7974 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007975 assign_dest_T dest = dest_local;
7976 int opt_flags = 0;
7977 int vimvaridx = -1;
7978 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007979 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02007980 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007981
7982 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007983 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007984 name = vim_strnsave(arg, varlen);
7985 if (name == NULL)
7986 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007987 if (*p == ':')
7988 {
7989 p = skipwhite(p + 1);
7990 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7991 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007992
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007993 // TODO: script var not supported?
7994 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7995 &vimvaridx, &type, cctx) == FAIL)
7996 goto failed;
7997 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007998 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007999 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8000 0, 0, type, name) == FAIL)
8001 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008002 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008003 else if (varlen == 1 && *arg == '_')
8004 {
8005 // Assigning to "_": drop the value.
8006 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8007 goto failed;
8008 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008009 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008010 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008011 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008012 {
8013 semsg(_(e_variable_already_declared), arg);
8014 goto failed;
8015 }
8016
Bram Moolenaarea870692020-12-02 14:24:30 +01008017 if (STRNCMP(name, "s:", 2) == 0)
8018 {
8019 semsg(_(e_cannot_declare_script_variable_in_function), name);
8020 goto failed;
8021 }
8022
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008023 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008024 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008025 where.wt_variable = TRUE;
8026 if (lhs_type == &t_any)
8027 lhs_type = item_type;
8028 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008029 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008030 ? need_type(item_type, lhs_type,
8031 -1, 0, cctx, FALSE, FALSE)
8032 : check_type(lhs_type, item_type, TRUE, where))
8033 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008034 goto failed;
8035 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008036 if (var_lvar == NULL)
8037 // out of memory or used as an argument
8038 goto failed;
8039
8040 if (semicolon && idx == var_count - 1)
8041 var_lvar->lv_type = vartype;
8042 else
8043 var_lvar->lv_type = item_type;
8044 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8045 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008046
8047 if (*p == ',' || *p == ';')
8048 ++p;
8049 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008050 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008051 }
8052
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008053 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008054 {
8055 int save_prev_lnum = cctx->ctx_prev_lnum;
8056
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008057 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008058 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8059 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008060 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008061 cctx->ctx_prev_lnum = save_prev_lnum;
8062 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008063
Bram Moolenaar792f7862020-11-23 08:31:18 +01008064 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008065
8066failed:
8067 vim_free(name);
8068 drop_scope(cctx);
8069 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070}
8071
8072/*
8073 * compile "endfor"
8074 */
8075 static char_u *
8076compile_endfor(char_u *arg, cctx_T *cctx)
8077{
8078 garray_T *instr = &cctx->ctx_instr;
8079 scope_T *scope = cctx->ctx_scope;
8080 forscope_T *forscope;
8081 isn_T *isn;
8082
Bram Moolenaarfa984412021-03-25 22:15:28 +01008083 if (misplaced_cmdmod(cctx))
8084 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086 if (scope == NULL || scope->se_type != FOR_SCOPE)
8087 {
8088 emsg(_(e_for));
8089 return NULL;
8090 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008091 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008093 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094
8095 // At end of ":for" scope jump back to the FOR instruction.
8096 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8097
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008098 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8100 isn->isn_arg.forloop.for_end = instr->ga_len;
8101
8102 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008103 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104
8105 // Below the ":for" scope drop the "expr" list from the stack.
8106 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8107 return NULL;
8108
8109 vim_free(scope);
8110
8111 return arg;
8112}
8113
8114/*
8115 * compile "while expr"
8116 *
8117 * Produces instructions:
8118 * top: EVAL expr Push result of "expr"
8119 * JUMP_IF_FALSE end jump if false
8120 * ... body ...
8121 * JUMP top Jump back to repeat
8122 * end:
8123 *
8124 */
8125 static char_u *
8126compile_while(char_u *arg, cctx_T *cctx)
8127{
8128 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 scope_T *scope;
8130
8131 scope = new_scope(cctx, WHILE_SCOPE);
8132 if (scope == NULL)
8133 return NULL;
8134
Bram Moolenaara91a7132021-03-25 21:12:15 +01008135 // "endwhile" jumps back here, one before when profiling or using cmdmods
8136 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137
8138 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008139 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008141 if (!ends_excmd2(arg, skipwhite(p)))
8142 {
8143 semsg(_(e_trailing_arg), p);
8144 return NULL;
8145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146
Bram Moolenaar13106602020-10-04 16:06:05 +02008147 if (bool_on_stack(cctx) == FAIL)
8148 return FAIL;
8149
Bram Moolenaara91a7132021-03-25 21:12:15 +01008150 // CMDMOD_REV must come before the jump
8151 generate_undo_cmdmods(cctx);
8152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008154 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 JUMP_IF_FALSE, cctx) == FAIL)
8156 return FAIL;
8157
8158 return p;
8159}
8160
8161/*
8162 * compile "endwhile"
8163 */
8164 static char_u *
8165compile_endwhile(char_u *arg, cctx_T *cctx)
8166{
8167 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008168 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169
Bram Moolenaarfa984412021-03-25 22:15:28 +01008170 if (misplaced_cmdmod(cctx))
8171 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8173 {
8174 emsg(_(e_while));
8175 return NULL;
8176 }
8177 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008178 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179
Bram Moolenaarf002a412021-01-24 13:34:18 +01008180#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008181 // count the endwhile before jumping
8182 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008183#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008186 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008187
8188 // Fill in the "end" label in the WHILE statement so it can jump here.
8189 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008190 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8191 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192
8193 vim_free(scope);
8194
8195 return arg;
8196}
8197
8198/*
8199 * compile "continue"
8200 */
8201 static char_u *
8202compile_continue(char_u *arg, cctx_T *cctx)
8203{
8204 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008205 int try_scopes = 0;
8206 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207
8208 for (;;)
8209 {
8210 if (scope == NULL)
8211 {
8212 emsg(_(e_continue));
8213 return NULL;
8214 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008215 if (scope->se_type == FOR_SCOPE)
8216 {
8217 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008219 }
8220 if (scope->se_type == WHILE_SCOPE)
8221 {
8222 loop_label = scope->se_u.se_while.ws_top_label;
8223 break;
8224 }
8225 if (scope->se_type == TRY_SCOPE)
8226 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227 scope = scope->se_outer;
8228 }
8229
Bram Moolenaarc150c092021-02-13 15:02:46 +01008230 if (try_scopes > 0)
8231 // Inside one or more try/catch blocks we first need to jump to the
8232 // "finally" or "endtry" to cleanup.
8233 generate_TRYCONT(cctx, try_scopes, loop_label);
8234 else
8235 // Jump back to the FOR or WHILE instruction.
8236 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238 return arg;
8239}
8240
8241/*
8242 * compile "break"
8243 */
8244 static char_u *
8245compile_break(char_u *arg, cctx_T *cctx)
8246{
8247 scope_T *scope = cctx->ctx_scope;
8248 endlabel_T **el;
8249
8250 for (;;)
8251 {
8252 if (scope == NULL)
8253 {
8254 emsg(_(e_break));
8255 return NULL;
8256 }
8257 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8258 break;
8259 scope = scope->se_outer;
8260 }
8261
8262 // Jump to the end of the FOR or WHILE loop.
8263 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008264 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008266 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8268 return FAIL;
8269
8270 return arg;
8271}
8272
8273/*
8274 * compile "{" start of block
8275 */
8276 static char_u *
8277compile_block(char_u *arg, cctx_T *cctx)
8278{
8279 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8280 return NULL;
8281 return skipwhite(arg + 1);
8282}
8283
8284/*
8285 * compile end of block: drop one scope
8286 */
8287 static void
8288compile_endblock(cctx_T *cctx)
8289{
8290 scope_T *scope = cctx->ctx_scope;
8291
8292 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008293 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294 vim_free(scope);
8295}
8296
8297/*
8298 * compile "try"
8299 * Creates a new scope for the try-endtry, pointing to the first catch and
8300 * finally.
8301 * Creates another scope for the "try" block itself.
8302 * TRY instruction sets up exception handling at runtime.
8303 *
8304 * "try"
8305 * TRY -> catch1, -> finally push trystack entry
8306 * ... try block
8307 * "throw {exception}"
8308 * EVAL {exception}
8309 * THROW create exception
8310 * ... try block
8311 * " catch {expr}"
8312 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008313 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 * EVAL {expr}
8315 * MATCH
8316 * JUMP nomatch -> catch2
8317 * CATCH remove exception
8318 * ... catch block
8319 * " catch"
8320 * JUMP -> finally
8321 * catch2: CATCH remove exception
8322 * ... catch block
8323 * " finally"
8324 * finally:
8325 * ... finally block
8326 * " endtry"
8327 * ENDTRY pop trystack entry, may rethrow
8328 */
8329 static char_u *
8330compile_try(char_u *arg, cctx_T *cctx)
8331{
8332 garray_T *instr = &cctx->ctx_instr;
8333 scope_T *try_scope;
8334 scope_T *scope;
8335
Bram Moolenaarfa984412021-03-25 22:15:28 +01008336 if (misplaced_cmdmod(cctx))
8337 return NULL;
8338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008339 // scope that holds the jumps that go to catch/finally/endtry
8340 try_scope = new_scope(cctx, TRY_SCOPE);
8341 if (try_scope == NULL)
8342 return NULL;
8343
Bram Moolenaar69f70502021-01-01 16:10:46 +01008344 if (cctx->ctx_skip != SKIP_YES)
8345 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008346 isn_T *isn;
8347
8348 // "try_catch" is set when the first ":catch" is found or when no catch
8349 // is found and ":finally" is found.
8350 // "try_finally" is set when ":finally" is found
8351 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008352 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008353 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8354 return NULL;
8355 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8356 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008357 return NULL;
8358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359
8360 // scope for the try block itself
8361 scope = new_scope(cctx, BLOCK_SCOPE);
8362 if (scope == NULL)
8363 return NULL;
8364
8365 return arg;
8366}
8367
8368/*
8369 * compile "catch {expr}"
8370 */
8371 static char_u *
8372compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8373{
8374 scope_T *scope = cctx->ctx_scope;
8375 garray_T *instr = &cctx->ctx_instr;
8376 char_u *p;
8377 isn_T *isn;
8378
Bram Moolenaarfa984412021-03-25 22:15:28 +01008379 if (misplaced_cmdmod(cctx))
8380 return NULL;
8381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 // end block scope from :try or :catch
8383 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8384 compile_endblock(cctx);
8385 scope = cctx->ctx_scope;
8386
8387 // Error if not in a :try scope
8388 if (scope == NULL || scope->se_type != TRY_SCOPE)
8389 {
8390 emsg(_(e_catch));
8391 return NULL;
8392 }
8393
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008394 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008396 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 return NULL;
8398 }
8399
Bram Moolenaar69f70502021-01-01 16:10:46 +01008400 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008402#ifdef FEAT_PROFILE
8403 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008404 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008405 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008406 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008407 .isn_type == ISN_PROF_START)
8408 --instr->ga_len;
8409#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008410 // Jump from end of previous block to :finally or :endtry
8411 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8412 JUMP_ALWAYS, cctx) == FAIL)
8413 return NULL;
8414
8415 // End :try or :catch scope: set value in ISN_TRY instruction
8416 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008417 if (isn->isn_arg.try.try_ref->try_catch == 0)
8418 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008419 if (scope->se_u.se_try.ts_catch_label != 0)
8420 {
8421 // Previous catch without match jumps here
8422 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8423 isn->isn_arg.jump.jump_where = instr->ga_len;
8424 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008425#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008426 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008427 {
8428 // a "throw" that jumps here needs to be counted
8429 generate_instr(cctx, ISN_PROF_END);
8430 // the "catch" is also counted
8431 generate_instr(cctx, ISN_PROF_START);
8432 }
8433#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008434 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008435 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008436 }
8437
8438 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008439 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008441 scope->se_u.se_try.ts_caught_all = TRUE;
8442 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 }
8444 else
8445 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008446 char_u *end;
8447 char_u *pat;
8448 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008449 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008450 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 // Push v:exception, push {expr} and MATCH
8453 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8454
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008455 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008456 if (*end != *p)
8457 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008458 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008459 vim_free(tofree);
8460 return FAIL;
8461 }
8462 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008463 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008464 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008465 len = (int)(end - tofree);
8466 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008467 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008468 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008469 if (pat == NULL)
8470 return FAIL;
8471 if (generate_PUSHS(cctx, pat) == FAIL)
8472 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008474 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8475 return NULL;
8476
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008477 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8479 return NULL;
8480 }
8481
Bram Moolenaar69f70502021-01-01 16:10:46 +01008482 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008483 return NULL;
8484
8485 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8486 return NULL;
8487 return p;
8488}
8489
8490 static char_u *
8491compile_finally(char_u *arg, cctx_T *cctx)
8492{
8493 scope_T *scope = cctx->ctx_scope;
8494 garray_T *instr = &cctx->ctx_instr;
8495 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008496 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008497
Bram Moolenaarfa984412021-03-25 22:15:28 +01008498 if (misplaced_cmdmod(cctx))
8499 return NULL;
8500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008501 // end block scope from :try or :catch
8502 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8503 compile_endblock(cctx);
8504 scope = cctx->ctx_scope;
8505
8506 // Error if not in a :try scope
8507 if (scope == NULL || scope->se_type != TRY_SCOPE)
8508 {
8509 emsg(_(e_finally));
8510 return NULL;
8511 }
8512
8513 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008514 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008515 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008516 {
8517 emsg(_(e_finally_dup));
8518 return NULL;
8519 }
8520
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008521 this_instr = instr->ga_len;
8522#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008523 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008524 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008525 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008526 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008527 // jump to the profile start of the "finally"
8528 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008529
8530 // jump to the profile end above it
8531 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8532 .isn_type == ISN_PROF_END)
8533 --this_instr;
8534 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008535#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008537 // Fill in the "end" label in jumps at the end of the blocks.
8538 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8539 this_instr, cctx);
8540
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008541 // If there is no :catch then an exception jumps to :finally.
8542 if (isn->isn_arg.try.try_ref->try_catch == 0)
8543 isn->isn_arg.try.try_ref->try_catch = this_instr;
8544 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008545 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 {
8547 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008548 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008549 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008550 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008551 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008552 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8553 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008555 // TODO: set index in ts_finally_label jumps
8556
8557 return arg;
8558}
8559
8560 static char_u *
8561compile_endtry(char_u *arg, cctx_T *cctx)
8562{
8563 scope_T *scope = cctx->ctx_scope;
8564 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008565 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008566
Bram Moolenaarfa984412021-03-25 22:15:28 +01008567 if (misplaced_cmdmod(cctx))
8568 return NULL;
8569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 // end block scope from :catch or :finally
8571 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8572 compile_endblock(cctx);
8573 scope = cctx->ctx_scope;
8574
8575 // Error if not in a :try scope
8576 if (scope == NULL || scope->se_type != TRY_SCOPE)
8577 {
8578 if (scope == NULL)
8579 emsg(_(e_no_endtry));
8580 else if (scope->se_type == WHILE_SCOPE)
8581 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008582 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 emsg(_(e_endfor));
8584 else
8585 emsg(_(e_endif));
8586 return NULL;
8587 }
8588
Bram Moolenaarc150c092021-02-13 15:02:46 +01008589 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008590 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008592 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8593 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008594 {
8595 emsg(_(e_missing_catch_or_finally));
8596 return NULL;
8597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008598
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008599#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008600 if (cctx->ctx_compile_type == CT_PROFILE
8601 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008602 .isn_type == ISN_PROF_START)
8603 // move the profile start after "endtry" so that it's not counted when
8604 // the exception is rethrown.
8605 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008606#endif
8607
Bram Moolenaar69f70502021-01-01 16:10:46 +01008608 // Fill in the "end" label in jumps at the end of the blocks, if not
8609 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008610 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8611 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612
Bram Moolenaar69f70502021-01-01 16:10:46 +01008613 if (scope->se_u.se_try.ts_catch_label != 0)
8614 {
8615 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008616 isn_T *isn = ((isn_T *)instr->ga_data)
8617 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008618 isn->isn_arg.jump.jump_where = instr->ga_len;
8619 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008620 }
8621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008622 compile_endblock(cctx);
8623
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008624 if (cctx->ctx_skip != SKIP_YES)
8625 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008626 // End :catch or :finally scope: set instruction index in ISN_TRY
8627 // instruction
8628 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008629 if (cctx->ctx_skip != SKIP_YES
8630 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8631 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008632#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008633 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008634 generate_instr(cctx, ISN_PROF_START);
8635#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637 return arg;
8638}
8639
8640/*
8641 * compile "throw {expr}"
8642 */
8643 static char_u *
8644compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8645{
8646 char_u *p = skipwhite(arg);
8647
Bram Moolenaara5565e42020-05-09 15:44:01 +02008648 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008650 if (cctx->ctx_skip == SKIP_YES)
8651 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008652 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653 return NULL;
8654 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8655 return NULL;
8656
8657 return p;
8658}
8659
Bram Moolenaarc3235272021-07-10 19:42:03 +02008660 static char_u *
8661compile_eval(char_u *arg, cctx_T *cctx)
8662{
8663 char_u *p = arg;
8664 int name_only;
8665 char_u *alias;
8666 long lnum = SOURCING_LNUM;
8667
8668 // find_ex_command() will consider a variable name an expression, assuming
8669 // that something follows on the next line. Check that something actually
8670 // follows, otherwise it's probably a misplaced command.
8671 get_name_len(&p, &alias, FALSE, FALSE);
8672 name_only = ends_excmd2(arg, skipwhite(p));
8673 vim_free(alias);
8674
8675 p = arg;
8676 if (compile_expr0(&p, cctx) == FAIL)
8677 return NULL;
8678
8679 if (name_only && lnum == SOURCING_LNUM)
8680 {
8681 semsg(_(e_expression_without_effect_str), arg);
8682 return NULL;
8683 }
8684
8685 // drop the result
8686 generate_instr_drop(cctx, ISN_DROP, 1);
8687
8688 return skipwhite(p);
8689}
8690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691/*
8692 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008693 * compile "echomsg expr"
8694 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008695 * compile "execute expr"
8696 */
8697 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008698compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008699{
8700 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008701 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008702 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008703 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008704 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008705 garray_T *stack = &cctx->ctx_type_stack;
8706 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008707
8708 for (;;)
8709 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008710 if (ends_excmd2(prev, p))
8711 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008712 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008713 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008714 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008715
8716 if (cctx->ctx_skip != SKIP_YES)
8717 {
8718 // check for non-void type
8719 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8720 if (type->tt_type == VAR_VOID)
8721 {
8722 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8723 return NULL;
8724 }
8725 }
8726
Bram Moolenaarad39c092020-02-26 18:23:43 +01008727 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008728 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008729 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008730 }
8731
Bram Moolenaare4984292020-12-13 14:19:25 +01008732 if (count > 0)
8733 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008734 long save_lnum = cctx->ctx_lnum;
8735
8736 // Use the line number where the command started.
8737 cctx->ctx_lnum = start_ctx_lnum;
8738
Bram Moolenaare4984292020-12-13 14:19:25 +01008739 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8740 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8741 else if (cmdidx == CMD_execute)
8742 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8743 else if (cmdidx == CMD_echomsg)
8744 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8745 else
8746 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008747
8748 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 return p;
8751}
8752
8753/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008754 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008755 * instruction to compute it and return OK.
8756 * Otherwise return FAIL, the caller must deal with any range.
8757 */
8758 static int
8759compile_variable_range(exarg_T *eap, cctx_T *cctx)
8760{
8761 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8762 char_u *p = skipdigits(eap->cmd);
8763
8764 if (p == range_end)
8765 return FAIL;
8766 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8767}
8768
8769/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008770 * :put r
8771 * :put ={expr}
8772 */
8773 static char_u *
8774compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8775{
8776 char_u *line = arg;
8777 linenr_T lnum;
8778 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008779 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008780
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008781 eap->regname = *line;
8782
8783 if (eap->regname == '=')
8784 {
8785 char_u *p = line + 1;
8786
8787 if (compile_expr0(&p, cctx) == FAIL)
8788 return NULL;
8789 line = p;
8790 }
8791 else if (eap->regname != NUL)
8792 ++line;
8793
Bram Moolenaar08597872020-12-10 19:43:40 +01008794 if (compile_variable_range(eap, cctx) == OK)
8795 {
8796 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8797 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008798 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008799 {
8800 // Either no range or a number.
8801 // "errormsg" will not be set because the range is ADDR_LINES.
8802 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008803 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008804 return NULL;
8805 if (eap->addr_count == 0)
8806 lnum = -1;
8807 else
8808 lnum = eap->line2;
8809 if (above)
8810 --lnum;
8811 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008812
8813 generate_PUT(cctx, eap->regname, lnum);
8814 return line;
8815}
8816
8817/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008818 * A command that is not compiled, execute with legacy code.
8819 */
8820 static char_u *
8821compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8822{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008823 char_u *p;
8824 int has_expr = FALSE;
8825 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008826
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008827 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008828 goto theend;
8829
Bram Moolenaare729ce22021-06-06 21:38:09 +02008830 // If there was a prececing command modifier, drop it and include it in the
8831 // EXEC command.
8832 if (cctx->ctx_has_cmdmod)
8833 {
8834 garray_T *instr = &cctx->ctx_instr;
8835 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8836
8837 if (isn->isn_type == ISN_CMDMOD)
8838 {
8839 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8840 ->cmod_filter_regmatch.regprog);
8841 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8842 --instr->ga_len;
8843 cctx->ctx_has_cmdmod = FALSE;
8844 }
8845 }
8846
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008847 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008848 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008849 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008850 int usefilter = FALSE;
8851
8852 has_expr = argt & (EX_XFILE | EX_EXPAND);
8853
8854 // If the command can be followed by a bar, find the bar and truncate
8855 // it, so that the following command can be compiled.
8856 // The '|' is overwritten with a NUL, it is put back below.
8857 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8858 && *eap->arg == '!')
8859 // :w !filter or :r !filter or :r! filter
8860 usefilter = TRUE;
8861 if ((argt & EX_TRLBAR) && !usefilter)
8862 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008863 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008864 separate_nextcmd(eap);
8865 if (eap->nextcmd != NULL)
8866 nextcmd = eap->nextcmd;
8867 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008868 else if (eap->cmdidx == CMD_wincmd)
8869 {
8870 p = eap->arg;
8871 if (*p != NUL)
8872 ++p;
8873 if (*p == 'g' || *p == Ctrl_G)
8874 ++p;
8875 p = skipwhite(p);
8876 if (*p == '|')
8877 {
8878 *p = NUL;
8879 nextcmd = p + 1;
8880 }
8881 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008882 }
8883
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008884 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8885 {
8886 // expand filename in "syntax include [@group] filename"
8887 has_expr = TRUE;
8888 eap->arg = skipwhite(eap->arg + 7);
8889 if (*eap->arg == '@')
8890 eap->arg = skiptowhite(eap->arg);
8891 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008892
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008893 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8894 && STRLEN(eap->arg) > 4)
8895 {
8896 int delim = *eap->arg;
8897
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008898 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008899 if (*p == delim)
8900 {
8901 eap->arg = p + 1;
8902 has_expr = TRUE;
8903 }
8904 }
8905
Bram Moolenaarecac5912021-01-05 19:23:28 +01008906 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8907 {
8908 // TODO: should only expand when appropriate for the command
8909 eap->arg = skiptowhite(eap->arg);
8910 has_expr = TRUE;
8911 }
8912
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008913 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008914 {
8915 int count = 0;
8916 char_u *start = skipwhite(line);
8917
8918 // :cmd xxx`=expr1`yyy`=expr2`zzz
8919 // PUSHS ":cmd xxx"
8920 // eval expr1
8921 // PUSHS "yyy"
8922 // eval expr2
8923 // PUSHS "zzz"
8924 // EXECCONCAT 5
8925 for (;;)
8926 {
8927 if (p > start)
8928 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008929 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008930 ++count;
8931 }
8932 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008933 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008934 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008935 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008936 ++count;
8937 p = skipwhite(p);
8938 if (*p != '`')
8939 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008940 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008941 return NULL;
8942 }
8943 start = p + 1;
8944
8945 p = (char_u *)strstr((char *)start, "`=");
8946 if (p == NULL)
8947 {
8948 if (*skipwhite(start) != NUL)
8949 {
8950 generate_PUSHS(cctx, vim_strsave(start));
8951 ++count;
8952 }
8953 break;
8954 }
8955 }
8956 generate_EXECCONCAT(cctx, count);
8957 }
8958 else
8959 generate_EXEC(cctx, line);
8960
8961theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008962 if (*nextcmd != NUL)
8963 {
8964 // the parser expects a pointer to the bar, put it back
8965 --nextcmd;
8966 *nextcmd = '|';
8967 }
8968
8969 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008970}
8971
Bram Moolenaar20677332021-06-06 17:02:53 +02008972/*
8973 * A script command with heredoc, e.g.
8974 * ruby << EOF
8975 * command
8976 * EOF
8977 * Has been turned into one long line with NL characters by
8978 * get_function_body():
8979 * ruby << EOF<NL> command<NL>EOF
8980 */
8981 static char_u *
8982compile_script(char_u *line, cctx_T *cctx)
8983{
8984 if (cctx->ctx_skip != SKIP_YES)
8985 {
8986 isn_T *isn;
8987
8988 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8989 return NULL;
8990 isn->isn_arg.string = vim_strsave(line);
8991 }
8992 return (char_u *)"";
8993}
8994
Bram Moolenaar8238f082021-04-20 21:10:48 +02008995
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008996/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008997 * :s/pat/repl/
8998 */
8999 static char_u *
9000compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9001{
9002 char_u *cmd = eap->arg;
9003 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9004
9005 if (expr != NULL)
9006 {
9007 int delimiter = *cmd++;
9008
9009 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009010 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009011 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9012 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009013 garray_T save_ga = cctx->ctx_instr;
9014 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009015 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009016 int trailing_error;
9017 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009018 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009019 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009020
9021 cmd += 3;
9022 end = skip_substitute(cmd, delimiter);
9023
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009024 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009025 // labels are correct.
9026 cctx->ctx_instr.ga_len = 0;
9027 cctx->ctx_instr.ga_maxlen = 0;
9028 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009029 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009030 if (end[-1] == NUL)
9031 end[-1] = delimiter;
9032 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009033 trailing_error = *cmd != delimiter && *cmd != NUL;
9034
Bram Moolenaar169502f2021-04-21 12:19:35 +02009035 if (expr_res == FAIL || trailing_error
9036 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009037 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009038 if (trailing_error)
9039 semsg(_(e_trailing_arg), cmd);
9040 clear_instr_ga(&cctx->ctx_instr);
9041 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009042 return NULL;
9043 }
9044
Bram Moolenaar8238f082021-04-20 21:10:48 +02009045 // Move the generated instructions into the ISN_SUBSTITUTE
9046 // instructions, then restore the list of instructions before
9047 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009048 instr_count = cctx->ctx_instr.ga_len;
9049 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009050 instr[instr_count].isn_type = ISN_FINISH;
9051
9052 cctx->ctx_instr = save_ga;
9053 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9054 {
9055 int idx;
9056
9057 for (idx = 0; idx < instr_count; ++idx)
9058 delete_instr(instr + idx);
9059 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009060 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009061 }
9062 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9063 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009064
9065 // skip over flags
9066 if (*end == '&')
9067 ++end;
9068 while (ASCII_ISALPHA(*end) || *end == '#')
9069 ++end;
9070 return end;
9071 }
9072 }
9073
9074 return compile_exec(arg, eap, cctx);
9075}
9076
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009077 static char_u *
9078compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9079{
9080 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009081 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009082
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009083 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009084 {
9085 if (STRNCMP(arg, "END", 3) == 0)
9086 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009087 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009088 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009089 // First load the current variable value.
9090 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9091 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009092 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009093 }
9094
9095 // Gets the redirected text and put it on the stack, then store it
9096 // in the variable.
9097 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9098
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009099 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009100 generate_instr_drop(cctx, ISN_CONCAT, 1);
9101
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009102 if (lhs->lhs_has_index)
9103 {
9104 // Use the info in "lhs" to store the value at the index in the
9105 // list or dict.
9106 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9107 &t_string, cctx) == FAIL)
9108 return NULL;
9109 }
9110 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009111 return NULL;
9112
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009113 VIM_CLEAR(lhs->lhs_name);
9114 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009115 return arg + 3;
9116 }
9117 emsg(_(e_cannot_nest_redir));
9118 return NULL;
9119 }
9120
9121 if (arg[0] == '=' && arg[1] == '>')
9122 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009123 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009124
9125 // redirect to a variable is compiled
9126 arg += 2;
9127 if (*arg == '>')
9128 {
9129 ++arg;
9130 append = TRUE;
9131 }
9132 arg = skipwhite(arg);
9133
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009134 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009135 FALSE, FALSE, 1, cctx) == FAIL)
9136 return NULL;
9137 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009138 lhs->lhs_append = append;
9139 if (lhs->lhs_has_index)
9140 {
9141 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9142 if (lhs->lhs_whole == NULL)
9143 return NULL;
9144 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009145
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009146 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009147 }
9148
9149 // other redirects are handled like at script level
9150 return compile_exec(line, eap, cctx);
9151}
9152
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009153#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009154 static char_u *
9155compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9156{
9157 isn_T *isn;
9158 char_u *p;
9159
9160 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9161 if (isn == NULL)
9162 return NULL;
9163 isn->isn_arg.number = eap->cmdidx;
9164
9165 p = eap->arg;
9166 if (compile_expr0(&p, cctx) == FAIL)
9167 return NULL;
9168
9169 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9170 if (isn == NULL)
9171 return NULL;
9172 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9173 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9174 return NULL;
9175 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9176 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9177 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9178
9179 return p;
9180}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009181#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009182
Bram Moolenaar4c137212021-04-19 16:48:48 +02009183/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009184 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009185 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009186 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009187 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009188add_def_function(ufunc_T *ufunc)
9189{
9190 dfunc_T *dfunc;
9191
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009192 if (def_functions.ga_len == 0)
9193 {
9194 // The first position is not used, so that a zero uf_dfunc_idx means it
9195 // wasn't set.
9196 if (ga_grow(&def_functions, 1) == FAIL)
9197 return FAIL;
9198 ++def_functions.ga_len;
9199 }
9200
Bram Moolenaar09689a02020-05-09 22:50:08 +02009201 // Add the function to "def_functions".
9202 if (ga_grow(&def_functions, 1) == FAIL)
9203 return FAIL;
9204 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9205 CLEAR_POINTER(dfunc);
9206 dfunc->df_idx = def_functions.ga_len;
9207 ufunc->uf_dfunc_idx = dfunc->df_idx;
9208 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009209 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009210 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009211 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009212 ++def_functions.ga_len;
9213 return OK;
9214}
9215
9216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009217 * After ex_function() has collected all the function lines: parse and compile
9218 * the lines into instructions.
9219 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009220 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9221 * the return statement (used for lambda). When uf_ret_type is already set
9222 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009223 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009224 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009225 * This can be used recursively through compile_lambda(), which may reallocate
9226 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009227 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009229 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009230compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009231 ufunc_T *ufunc,
9232 int check_return_type,
9233 compiletype_T compile_type,
9234 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009237 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009239 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009240 cctx_T cctx;
9241 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009242 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009243 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009244 int ret = FAIL;
9245 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009246 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009247 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009248 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009249 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009250#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009251 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009252#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009253 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009255 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009256 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009257 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9260 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009261 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009262
9263 switch (compile_type)
9264 {
9265 case CT_PROFILE:
9266#ifdef FEAT_PROFILE
9267 instr_dest = dfunc->df_instr_prof; break;
9268#endif
9269 case CT_NONE: instr_dest = dfunc->df_instr; break;
9270 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9271 }
9272 if (instr_dest != NULL)
9273 // Was compiled in this mode before: Free old instructions.
9274 delete_def_function_contents(dfunc, FALSE);
9275 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009277 else
9278 {
9279 if (add_def_function(ufunc) == FAIL)
9280 return FAIL;
9281 new_def_function = TRUE;
9282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009283
Bram Moolenaar985116a2020-07-12 17:31:09 +02009284 ufunc->uf_def_status = UF_COMPILING;
9285
Bram Moolenaara80faa82020-04-12 19:37:17 +02009286 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009287
Bram Moolenaare99d4222021-06-13 14:01:26 +02009288 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 cctx.ctx_ufunc = ufunc;
9290 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009291 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009292 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9293 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9294 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9295 cctx.ctx_type_list = &ufunc->uf_type_list;
9296 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9297 instr = &cctx.ctx_instr;
9298
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009299 // Set the context to the function, it may be compiled when called from
9300 // another script. Set the script version to the most modern one.
9301 // The line number will be set in next_line_from_context().
9302 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009303 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9304
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009305 // Don't use the flag from ":legacy" here.
9306 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9307
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009308 // Make sure error messages are OK.
9309 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9310 if (do_estack_push)
9311 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009312 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009314 if (ufunc->uf_def_args.ga_len > 0)
9315 {
9316 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009317 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009318 int i;
9319 char_u *arg;
9320 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009321 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009322
9323 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009324 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009325 for (i = 0; i < count; ++i)
9326 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009327 garray_T *stack = &cctx.ctx_type_stack;
9328 type_T *val_type;
9329 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009330 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009331 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009332 int jump_instr_idx = instr->ga_len;
9333 isn_T *isn;
9334
9335 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9336 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9337 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009338
9339 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009340 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009341
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009342 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009343 r = compile_expr0(&arg, &cctx);
9344
Bram Moolenaar12bce952021-03-11 20:04:04 +01009345 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009346 goto erret;
9347
9348 // If no type specified use the type of the default value.
9349 // Otherwise check that the default value type matches the
9350 // specified type.
9351 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009352 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009353 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009354 {
9355 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009356 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009357 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009358 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009359 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009360 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009361
9362 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009363 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009364
9365 // set instruction index in JUMP_IF_ARG_SET to here
9366 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9367 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009368 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009369
9370 if (did_set_arg_type)
9371 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009372 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009373 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009374
9375 /*
9376 * Loop over all the lines of the function and generate instructions.
9377 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009378 for (;;)
9379 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009380 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009381 int starts_with_colon = FALSE;
9382 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009383 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009384
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009385 // Bail out on the first error to avoid a flood of errors and report
9386 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009387 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009388 goto erret;
9389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009390 if (line != NULL && *line == '|')
9391 // the line continues after a '|'
9392 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009393 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009394 && !(*line == '#' && (line == cctx.ctx_line_start
9395 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009396 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009397 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 goto erret;
9399 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009400 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9401 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009402 else
9403 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009404 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009405 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009406 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009407 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009408#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009409 if (cctx.ctx_skip != SKIP_YES)
9410 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009411#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009412 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009413 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009414 // Make a copy, splitting off nextcmd and removing trailing spaces
9415 // may change it.
9416 if (line != NULL)
9417 {
9418 line = vim_strsave(line);
9419 vim_free(line_to_free);
9420 line_to_free = line;
9421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009422 }
9423
Bram Moolenaara80faa82020-04-12 19:37:17 +02009424 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425 ea.cmdlinep = &line;
9426 ea.cmd = skipwhite(line);
9427
Bram Moolenaarb2049902021-01-24 12:53:53 +01009428 if (*ea.cmd == '#')
9429 {
9430 // "#" starts a comment
9431 line = (char_u *)"";
9432 continue;
9433 }
9434
Bram Moolenaarf002a412021-01-24 13:34:18 +01009435#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009436 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9437 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009438 {
9439 may_generate_prof_end(&cctx, prof_lnum);
9440
9441 prof_lnum = cctx.ctx_lnum;
9442 generate_instr(&cctx, ISN_PROF_START);
9443 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009444#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009445 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9446 && cctx.ctx_skip != SKIP_YES)
9447 {
9448 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009449 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009450 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009451 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009452
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009453 // Some things can be recognized by the first character.
9454 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009456 case '}':
9457 {
9458 // "}" ends a block scope
9459 scopetype_T stype = cctx.ctx_scope == NULL
9460 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009461
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009462 if (stype == BLOCK_SCOPE)
9463 {
9464 compile_endblock(&cctx);
9465 line = ea.cmd;
9466 }
9467 else
9468 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009469 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009470 goto erret;
9471 }
9472 if (line != NULL)
9473 line = skipwhite(ea.cmd + 1);
9474 continue;
9475 }
9476
9477 case '{':
9478 // "{" starts a block scope
9479 // "{'a': 1}->func() is something else
9480 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9481 {
9482 line = compile_block(ea.cmd, &cctx);
9483 continue;
9484 }
9485 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009486 }
9487
9488 /*
9489 * COMMAND MODIFIERS
9490 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009491 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009492 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9493 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009494 {
9495 if (errormsg != NULL)
9496 goto erret;
9497 // empty line or comment
9498 line = (char_u *)"";
9499 continue;
9500 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009501 generate_cmdmods(&cctx, &local_cmdmod);
9502 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009503
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009504 // Check if there was a colon after the last command modifier or before
9505 // the current position.
9506 for (p = ea.cmd; p >= line; --p)
9507 {
9508 if (*p == ':')
9509 starts_with_colon = TRUE;
9510 if (p < ea.cmd && !VIM_ISWHITE(*p))
9511 break;
9512 }
9513
Bram Moolenaarce024c32021-06-26 13:00:49 +02009514 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009515 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009516 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009517 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009518 if (checkforcmd(&ea.cmd, "call", 3))
9519 {
9520 if (*ea.cmd == '(')
9521 // not for "call()"
9522 ea.cmd = p;
9523 else
9524 ea.cmd = skipwhite(ea.cmd);
9525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009526
Bram Moolenaarce024c32021-06-26 13:00:49 +02009527 if (!starts_with_colon)
9528 {
9529 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009530
Bram Moolenaarce024c32021-06-26 13:00:49 +02009531 // Check for assignment after command modifiers.
9532 assign = may_compile_assignment(&ea, &line, &cctx);
9533 if (assign == OK)
9534 goto nextline;
9535 if (assign == FAIL)
9536 goto erret;
9537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009538 }
9539
9540 /*
9541 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009542 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009543 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009544 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009545 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009546 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9547 && (starts_with_colon || !(*cmd == '\''
9548 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009549 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009550 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009551 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009552 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009553 if (!starts_with_colon)
9554 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009555 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009556 goto erret;
9557 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009558 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009559 if (ends_excmd2(line, ea.cmd))
9560 {
9561 // A range without a command: jump to the line.
9562 // TODO: compile to a more efficient command, possibly
9563 // calling parse_cmd_address().
9564 ea.cmdidx = CMD_SIZE;
9565 line = compile_exec(line, &ea, &cctx);
9566 goto nextline;
9567 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009568 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009569 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009570 p = find_ex_command(&ea, NULL,
9571 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009572 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009573
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009574 if (p == NULL)
9575 {
9576 if (cctx.ctx_skip != SKIP_YES)
9577 emsg(_(e_ambiguous_use_of_user_defined_command));
9578 goto erret;
9579 }
9580
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009581 // When using ":legacy cmd" always use compile_exec().
9582 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009583 {
9584 char_u *start = ea.cmd;
9585
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009586 switch (ea.cmdidx)
9587 {
9588 case CMD_if:
9589 case CMD_elseif:
9590 case CMD_else:
9591 case CMD_endif:
9592 case CMD_for:
9593 case CMD_endfor:
9594 case CMD_continue:
9595 case CMD_break:
9596 case CMD_while:
9597 case CMD_endwhile:
9598 case CMD_try:
9599 case CMD_catch:
9600 case CMD_finally:
9601 case CMD_endtry:
9602 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9603 goto erret;
9604 default: break;
9605 }
9606
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009607 // ":legacy return expr" needs to be handled differently.
9608 if (checkforcmd(&start, "return", 4))
9609 ea.cmdidx = CMD_return;
9610 else
9611 ea.cmdidx = CMD_legacy;
9612 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009614 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9615 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009616 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009617 {
9618 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009619 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009620 }
9621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009622 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009623 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009624 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009625 // CMD_var cannot happen, compile_assignment() above would be
9626 // used. Most likely an assignment to a non-existing variable.
9627 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009628 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009630 }
9631
Bram Moolenaar3988f642020-08-27 22:43:03 +02009632 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009633 && ea.cmdidx != CMD_elseif
9634 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009635 && ea.cmdidx != CMD_endif
9636 && ea.cmdidx != CMD_endfor
9637 && ea.cmdidx != CMD_endwhile
9638 && ea.cmdidx != CMD_catch
9639 && ea.cmdidx != CMD_finally
9640 && ea.cmdidx != CMD_endtry)
9641 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009642 emsg(_(e_unreachable_code_after_return));
9643 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009644 }
9645
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009646 p = skipwhite(p);
9647 if (ea.cmdidx != CMD_SIZE
9648 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9649 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009650 if (ea.cmdidx >= 0)
9651 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009652 if ((ea.argt & EX_BANG) && *p == '!')
9653 {
9654 ea.forceit = TRUE;
9655 p = skipwhite(p + 1);
9656 }
9657 }
9658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 switch (ea.cmdidx)
9660 {
9661 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009662 ea.arg = p;
9663 line = compile_nested_function(&ea, &cctx);
9664 break;
9665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009666 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009667 // TODO: should we allow this, e.g. to declare a global
9668 // function?
9669 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009670 goto erret;
9671
9672 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009673 line = compile_return(p, check_return_type,
9674 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009675 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009676 break;
9677
9678 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009679 emsg(_(e_cannot_use_let_in_vim9_script));
9680 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009681 case CMD_var:
9682 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009684 case CMD_increment:
9685 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009686 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009687 if (line == p)
9688 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009689 break;
9690
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009691 case CMD_unlet:
9692 case CMD_unlockvar:
9693 case CMD_lockvar:
9694 line = compile_unletlock(p, &ea, &cctx);
9695 break;
9696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009697 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009698 emsg(_(e_import_can_only_be_used_in_script));
9699 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009700 break;
9701
9702 case CMD_if:
9703 line = compile_if(p, &cctx);
9704 break;
9705 case CMD_elseif:
9706 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009707 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 break;
9709 case CMD_else:
9710 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009711 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009712 break;
9713 case CMD_endif:
9714 line = compile_endif(p, &cctx);
9715 break;
9716
9717 case CMD_while:
9718 line = compile_while(p, &cctx);
9719 break;
9720 case CMD_endwhile:
9721 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009722 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009723 break;
9724
9725 case CMD_for:
9726 line = compile_for(p, &cctx);
9727 break;
9728 case CMD_endfor:
9729 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009730 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009731 break;
9732 case CMD_continue:
9733 line = compile_continue(p, &cctx);
9734 break;
9735 case CMD_break:
9736 line = compile_break(p, &cctx);
9737 break;
9738
9739 case CMD_try:
9740 line = compile_try(p, &cctx);
9741 break;
9742 case CMD_catch:
9743 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009744 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009745 break;
9746 case CMD_finally:
9747 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009748 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 break;
9750 case CMD_endtry:
9751 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009752 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009753 break;
9754 case CMD_throw:
9755 line = compile_throw(p, &cctx);
9756 break;
9757
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009758 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009759 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009760 break;
9761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009763 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009764 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009765 case CMD_echomsg:
9766 case CMD_echoerr:
9767 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009768 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009769
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009770 case CMD_put:
9771 ea.cmd = cmd;
9772 line = compile_put(p, &ea, &cctx);
9773 break;
9774
Bram Moolenaar4c137212021-04-19 16:48:48 +02009775 case CMD_substitute:
9776 if (cctx.ctx_skip == SKIP_YES)
9777 line = (char_u *)"";
9778 else
9779 {
9780 ea.arg = p;
9781 line = compile_substitute(line, &ea, &cctx);
9782 }
9783 break;
9784
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009785 case CMD_redir:
9786 ea.arg = p;
9787 line = compile_redir(line, &ea, &cctx);
9788 break;
9789
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009790 case CMD_cexpr:
9791 case CMD_lexpr:
9792 case CMD_caddexpr:
9793 case CMD_laddexpr:
9794 case CMD_cgetexpr:
9795 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009796#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009797 ea.arg = p;
9798 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009799#else
9800 ex_ni(&ea);
9801 line = NULL;
9802#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009803 break;
9804
Bram Moolenaar3988f642020-08-27 22:43:03 +02009805 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009806
Bram Moolenaarae616492020-07-28 20:07:27 +02009807 case CMD_append:
9808 case CMD_change:
9809 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009810 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009811 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009812 case CMD_xit:
9813 not_in_vim9(&ea);
9814 goto erret;
9815
Bram Moolenaar002262f2020-07-08 17:47:57 +02009816 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009817 if (cctx.ctx_skip != SKIP_YES)
9818 {
9819 semsg(_(e_invalid_command_str), ea.cmd);
9820 goto erret;
9821 }
9822 // We don't check for a next command here.
9823 line = (char_u *)"";
9824 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009825
Bram Moolenaar20677332021-06-06 17:02:53 +02009826 case CMD_lua:
9827 case CMD_mzscheme:
9828 case CMD_perl:
9829 case CMD_py3:
9830 case CMD_python3:
9831 case CMD_python:
9832 case CMD_pythonx:
9833 case CMD_ruby:
9834 case CMD_tcl:
9835 ea.arg = p;
9836 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009837 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009838 else
9839 // heredoc lines have been concatenated with NL
9840 // characters in get_function_body()
9841 line = compile_script(line, &cctx);
9842 break;
9843
9844 default:
9845 // Not recognized, execute with do_cmdline_cmd().
9846 ea.arg = p;
9847 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009848 break;
9849 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009850nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851 if (line == NULL)
9852 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009853 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009854
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009855 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009856 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009858 if (cctx.ctx_type_stack.ga_len < 0)
9859 {
9860 iemsg("Type stack underflow");
9861 goto erret;
9862 }
9863 }
9864
9865 if (cctx.ctx_scope != NULL)
9866 {
9867 if (cctx.ctx_scope->se_type == IF_SCOPE)
9868 emsg(_(e_endif));
9869 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9870 emsg(_(e_endwhile));
9871 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9872 emsg(_(e_endfor));
9873 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009874 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009875 goto erret;
9876 }
9877
Bram Moolenaarefd88552020-06-18 20:50:10 +02009878 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009879 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009880 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9881 ufunc->uf_ret_type = &t_void;
9882 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009883 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009884 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885 goto erret;
9886 }
9887
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009888 // Return void if there is no return at the end.
9889 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009890 }
9891
Bram Moolenaar599410c2021-04-10 14:03:43 +02009892 // When compiled with ":silent!" and there was an error don't consider the
9893 // function compiled.
9894 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009895 {
9896 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9897 + ufunc->uf_dfunc_idx;
9898 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009899 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009900#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009901 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009902 {
9903 dfunc->df_instr_prof = instr->ga_data;
9904 dfunc->df_instr_prof_count = instr->ga_len;
9905 }
9906 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009907#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009908 if (cctx.ctx_compile_type == CT_DEBUG)
9909 {
9910 dfunc->df_instr_debug = instr->ga_data;
9911 dfunc->df_instr_debug_count = instr->ga_len;
9912 }
9913 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009914 {
9915 dfunc->df_instr = instr->ga_data;
9916 dfunc->df_instr_count = instr->ga_len;
9917 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009918 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009919 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009920 if (cctx.ctx_outer_used)
9921 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009922 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924
9925 ret = OK;
9926
9927erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009928 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009929 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009930 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9931 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009932
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009933 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009934 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009935 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009936 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009937
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009938 // If using the last entry in the table and it was added above, we
9939 // might as well remove it.
9940 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009941 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009942 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009943 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009944 ufunc->uf_dfunc_idx = 0;
9945 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009946 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009947
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009948 while (cctx.ctx_scope != NULL)
9949 drop_scope(&cctx);
9950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009951 if (errormsg != NULL)
9952 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009953 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009954 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009955 }
9956
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009957 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9958 {
9959 if (ret == OK)
9960 {
9961 emsg(_(e_missing_redir_end));
9962 ret = FAIL;
9963 }
9964 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009965 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009966 }
9967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009968 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009969 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009970 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009971 if (do_estack_push)
9972 estack_pop();
9973
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009974 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009975 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009976 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009977 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009978 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009979}
9980
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009981 void
9982set_function_type(ufunc_T *ufunc)
9983{
9984 int varargs = ufunc->uf_va_name != NULL;
9985 int argcount = ufunc->uf_args.ga_len;
9986
9987 // Create a type for the function, with the return type and any
9988 // argument types.
9989 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9990 // The type is included in "tt_args".
9991 if (argcount > 0 || varargs)
9992 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009993 if (ufunc->uf_type_list.ga_itemsize == 0)
9994 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009995 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9996 argcount, &ufunc->uf_type_list);
9997 // Add argument types to the function type.
9998 if (func_type_add_arg_types(ufunc->uf_func_type,
9999 argcount + varargs,
10000 &ufunc->uf_type_list) == FAIL)
10001 return;
10002 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10003 ufunc->uf_func_type->tt_min_argcount =
10004 argcount - ufunc->uf_def_args.ga_len;
10005 if (ufunc->uf_arg_types == NULL)
10006 {
10007 int i;
10008
10009 // lambda does not have argument types.
10010 for (i = 0; i < argcount; ++i)
10011 ufunc->uf_func_type->tt_args[i] = &t_any;
10012 }
10013 else
10014 mch_memmove(ufunc->uf_func_type->tt_args,
10015 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10016 if (varargs)
10017 {
10018 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010019 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010020 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10021 }
10022 }
10023 else
10024 // No arguments, can use a predefined type.
10025 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10026 argcount, &ufunc->uf_type_list);
10027}
10028
10029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010030/*
10031 * Delete an instruction, free what it contains.
10032 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010033 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010034delete_instr(isn_T *isn)
10035{
10036 switch (isn->isn_type)
10037 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010038 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010039 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010040 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010041 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010042 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010043 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010044 case ISN_LOADENV:
10045 case ISN_LOADG:
10046 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010047 case ISN_LOADT:
10048 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010049 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010050 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010051 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010052 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010053 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010054 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010055 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010056 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010057 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010058 case ISN_STOREW:
10059 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010060 vim_free(isn->isn_arg.string);
10061 break;
10062
Bram Moolenaar4c137212021-04-19 16:48:48 +020010063 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010064 {
10065 int idx;
10066 isn_T *list = isn->isn_arg.subs.subs_instr;
10067
10068 vim_free(isn->isn_arg.subs.subs_cmd);
10069 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10070 delete_instr(list + idx);
10071 vim_free(list);
10072 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010073 break;
10074
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010075 case ISN_INSTR:
10076 {
10077 int idx;
10078 isn_T *list = isn->isn_arg.instr;
10079
10080 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10081 delete_instr(list + idx);
10082 vim_free(list);
10083 }
10084 break;
10085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010086 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010087 case ISN_STORES:
10088 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 break;
10090
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010091 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010092 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010093 vim_free(isn->isn_arg.unlet.ul_name);
10094 break;
10095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010096 case ISN_STOREOPT:
10097 vim_free(isn->isn_arg.storeopt.so_name);
10098 break;
10099
10100 case ISN_PUSHBLOB: // push blob isn_arg.blob
10101 blob_unref(isn->isn_arg.blob);
10102 break;
10103
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010104 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010106 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010107#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010108 break;
10109
10110 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010111#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010112 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010113#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010114 break;
10115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010116 case ISN_UCALL:
10117 vim_free(isn->isn_arg.ufunc.cuf_name);
10118 break;
10119
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010120 case ISN_FUNCREF:
10121 {
10122 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10123 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010124 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010125
Bram Moolenaar077a4232020-12-22 18:33:27 +010010126 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10127 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010128 }
10129 break;
10130
10131 case ISN_DCALL:
10132 {
10133 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10134 + isn->isn_arg.dfunc.cdf_idx;
10135
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010136 if (dfunc->df_ufunc != NULL
10137 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010138 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010139 }
10140 break;
10141
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010142 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010143 {
10144 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10145 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10146
10147 if (ufunc != NULL)
10148 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010149 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010150 func_ptr_unref(ufunc);
10151 }
10152
10153 vim_free(lambda);
10154 vim_free(isn->isn_arg.newfunc.nf_global);
10155 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010156 break;
10157
Bram Moolenaar5e654232020-09-16 15:22:00 +020010158 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010159 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010160 free_type(isn->isn_arg.type.ct_type);
10161 break;
10162
Bram Moolenaar02194d22020-10-24 23:08:38 +020010163 case ISN_CMDMOD:
10164 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10165 ->cmod_filter_regmatch.regprog);
10166 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10167 break;
10168
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010169 case ISN_LOADSCRIPT:
10170 case ISN_STORESCRIPT:
10171 vim_free(isn->isn_arg.script.scriptref);
10172 break;
10173
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010174 case ISN_TRY:
10175 vim_free(isn->isn_arg.try.try_ref);
10176 break;
10177
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010178 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010179 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010180 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10181 break;
10182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010183 case ISN_2BOOL:
10184 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010185 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010186 case ISN_ADDBLOB:
10187 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010188 case ISN_ANYINDEX:
10189 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010190 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010191 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010192 case ISN_BLOBINDEX:
10193 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010194 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010195 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010196 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010197 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010198 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010199 case ISN_COMPAREANY:
10200 case ISN_COMPAREBLOB:
10201 case ISN_COMPAREBOOL:
10202 case ISN_COMPAREDICT:
10203 case ISN_COMPAREFLOAT:
10204 case ISN_COMPAREFUNC:
10205 case ISN_COMPARELIST:
10206 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010207 case ISN_COMPARESPECIAL:
10208 case ISN_COMPARESTRING:
10209 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010210 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010211 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010212 case ISN_DROP:
10213 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010214 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010215 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010216 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010217 case ISN_EXECCONCAT:
10218 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010219 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010220 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010221 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010222 case ISN_GETITEM:
10223 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010224 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010225 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010226 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010227 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010228 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010229 case ISN_LOADBDICT:
10230 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010231 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010233 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010234 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010235 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010236 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010237 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010238 case ISN_NEGATENR:
10239 case ISN_NEWDICT:
10240 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010242 case ISN_OPFLOAT:
10243 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010244 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010245 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010246 case ISN_PROF_END:
10247 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010248 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010249 case ISN_PUSHF:
10250 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010252 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010253 case ISN_REDIREND:
10254 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010255 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010256 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010257 case ISN_SHUFFLE:
10258 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010259 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010260 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010261 case ISN_STORENR:
10262 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010263 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010264 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010265 case ISN_STOREV:
10266 case ISN_STRINDEX:
10267 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010268 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010269 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010270 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010271 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010272 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010273 // nothing allocated
10274 break;
10275 }
10276}
10277
10278/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010279 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010280 */
10281 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010282delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010283{
10284 int idx;
10285
10286 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010287 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010288
10289 if (dfunc->df_instr != NULL)
10290 {
10291 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10292 delete_instr(dfunc->df_instr + idx);
10293 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010294 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010295 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010296 if (dfunc->df_instr_debug != NULL)
10297 {
10298 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10299 delete_instr(dfunc->df_instr_debug + idx);
10300 VIM_CLEAR(dfunc->df_instr_debug);
10301 dfunc->df_instr_debug = NULL;
10302 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010303#ifdef FEAT_PROFILE
10304 if (dfunc->df_instr_prof != NULL)
10305 {
10306 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10307 delete_instr(dfunc->df_instr_prof + idx);
10308 VIM_CLEAR(dfunc->df_instr_prof);
10309 dfunc->df_instr_prof = NULL;
10310 }
10311#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010312
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010313 if (mark_deleted)
10314 dfunc->df_deleted = TRUE;
10315 if (dfunc->df_ufunc != NULL)
10316 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010317}
10318
10319/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010320 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010321 * function, unless another user function still uses it.
10322 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010323 */
10324 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010325unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010326{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010327 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010328 {
10329 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10330 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010331
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010332 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010333 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010334 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010335 ufunc->uf_dfunc_idx = 0;
10336 if (dfunc->df_ufunc == ufunc)
10337 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010338 }
10339}
10340
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010341/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010342 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010343 */
10344 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010345link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010346{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010347 if (ufunc->uf_dfunc_idx > 0)
10348 {
10349 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10350 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010351
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010352 ++dfunc->df_refcount;
10353 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010354}
10355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010356#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010357/*
10358 * Free all functions defined with ":def".
10359 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010360 void
10361free_def_functions(void)
10362{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010363 int idx;
10364
10365 for (idx = 0; idx < def_functions.ga_len; ++idx)
10366 {
10367 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10368
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010369 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010370 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010371 }
10372
10373 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010374}
10375#endif
10376
10377
10378#endif // FEAT_EVAL