blob: c27a1cc34afb58f9e309cf8a02a1401cb05a99f6 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
342 ufunc_T *ufunc;
343
344 // Find the list of all script variables with the right name.
345 if (len > 0)
346 {
347 cc = name[len];
348 name[len] = NUL;
349 }
350 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
351 if (len > 0)
352 name[len] = cc;
353 if (HASHITEM_EMPTY(hi))
354 return NULL;
355
356 sav = HI2SAV(hi);
357 if (sav->sav_block_id == 0 || cctx == NULL)
358 // variable defined in the script scope or not in a function.
359 return sav;
360
361 // Go over the variables with this name and find one that was visible
362 // from the function.
363 ufunc = cctx->ctx_ufunc;
364 while (sav != NULL)
365 {
366 int idx;
367
368 // Go over the blocks that this function was defined in. If the
369 // variable block ID matches it was visible to the function.
370 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
371 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
372 return sav;
373 sav = sav->sav_next;
374 }
375
376 return NULL;
377}
378
379/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100380 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200381 */
382 static int
383script_is_vim9()
384{
385 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
386}
387
388/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 * Returns OK or FAIL.
392 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200393 static int
394script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200396 if (current_sctx.sc_sid <= 0)
397 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200398 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200399 {
400 // Check script variables that were visible where the function was
401 // defined.
402 if (find_script_var(name, len, cctx) != NULL)
403 return OK;
404 }
405 else
406 {
407 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
408 dictitem_T *di;
409 int cc;
410
411 // Check script variables that are currently visible
412 cc = name[len];
413 name[len] = NUL;
414 di = find_var_in_ht(ht, 0, name, TRUE);
415 name[len] = cc;
416 if (di != NULL)
417 return OK;
418 }
419
420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421}
422
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100423/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100424 * Return TRUE if "name" is a local variable, argument, script variable or
425 * imported.
426 */
427 static int
428variable_exists(char_u *name, size_t len, cctx_T *cctx)
429{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 return (cctx != NULL
431 && (lookup_local(name, len, NULL, cctx) == OK
432 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200433 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100434 || find_imported(name, len, cctx) != NULL;
435}
436
437/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438 * Return TRUE if "name" is a local variable, argument, script variable,
439 * imported or function.
440 */
441 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100442item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100443{
444 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100445 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100446
447 if (variable_exists(name, len, cctx))
448 return TRUE;
449
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100450 // This is similar to what is in lookup_scriptitem():
451 // Find a function, so that a following "->" works.
452 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
453 // a function call.
454 p = skipwhite(name + len);
455
456 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
457 {
458 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200459 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460 // Skip "g:" before a function name.
461 is_global = (name[0] == 'g' && name[1] == ':');
462 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
463 }
464 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100465}
466
467/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100468 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200469 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200470 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100471 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100472 * Return FAIL and give an error if it defined.
473 */
474 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100475check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100476{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200477 int c = p[len];
478 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200479
Bram Moolenaarda479c72021-04-10 21:01:38 +0200480 // underscore argument is OK
481 if (len == 1 && *p == '_')
482 return OK;
483
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200484 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100485 {
486 if (is_arg)
487 semsg(_(e_argument_already_declared_in_script_str), p);
488 else
489 semsg(_(e_variable_already_declared_in_script_str), p);
490 return FAIL;
491 }
492
Bram Moolenaarad486a02020-08-01 23:22:18 +0200493 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100494 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100495 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200496 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100499 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200500 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200501 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
502 && (!func_is_global(ufunc)
503 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200504 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100505 if (is_arg)
506 semsg(_(e_argument_name_shadows_existing_variable_str), p);
507 else
508 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200509 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200510 return FAIL;
511 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100512 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100514 return OK;
515}
516
Bram Moolenaar65b95452020-07-19 14:03:09 +0200517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518/////////////////////////////////////////////////////////////////////
519// Following generate_ functions expect the caller to call ga_grow().
520
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200521#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
522#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524/*
525 * Generate an instruction without arguments.
526 * Returns a pointer to the new instruction, NULL if failed.
527 */
528 static isn_T *
529generate_instr(cctx_T *cctx, isntype_T isn_type)
530{
531 garray_T *instr = &cctx->ctx_instr;
532 isn_T *isn;
533
Bram Moolenaar080457c2020-03-03 21:53:32 +0100534 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 if (ga_grow(instr, 1) == FAIL)
536 return NULL;
537 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
538 isn->isn_type = isn_type;
539 isn->isn_lnum = cctx->ctx_lnum + 1;
540 ++instr->ga_len;
541
542 return isn;
543}
544
545/*
546 * Generate an instruction without arguments.
547 * "drop" will be removed from the stack.
548 * Returns a pointer to the new instruction, NULL if failed.
549 */
550 static isn_T *
551generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
552{
553 garray_T *stack = &cctx->ctx_type_stack;
554
Bram Moolenaar080457c2020-03-03 21:53:32 +0100555 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 stack->ga_len -= drop;
557 return generate_instr(cctx, isn_type);
558}
559
560/*
561 * Generate instruction "isn_type" and put "type" on the type stack.
562 */
563 static isn_T *
564generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
565{
566 isn_T *isn;
567 garray_T *stack = &cctx->ctx_type_stack;
568
569 if ((isn = generate_instr(cctx, isn_type)) == NULL)
570 return NULL;
571
572 if (ga_grow(stack, 1) == FAIL)
573 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200574 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 ++stack->ga_len;
576
577 return isn;
578}
579
580/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200581 * Generate an ISN_DEBUG instruction.
582 */
583 static isn_T *
584generate_instr_debug(cctx_T *cctx)
585{
586 isn_T *isn;
587 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
588 + cctx->ctx_ufunc->uf_dfunc_idx;
589
590 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
591 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200592 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
593 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200594 return isn;
595}
596
597/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200599 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200600 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 */
602 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200603may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604{
605 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200606 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100608 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100610 RETURN_OK_IF_SKIP(cctx);
611 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200612 switch ((*type)->tt_type)
613 {
614 // nothing to be done
615 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200617 // conversion possible
618 case VAR_SPECIAL:
619 case VAR_BOOL:
620 case VAR_NUMBER:
621 case VAR_FLOAT:
622 break;
623
624 // conversion possible (with runtime check)
625 case VAR_ANY:
626 case VAR_UNKNOWN:
627 isntype = ISN_2STRING_ANY;
628 break;
629
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200630 // conversion possible when tolerant
631 case VAR_LIST:
632 if (tolerant)
633 {
634 isntype = ISN_2STRING_ANY;
635 break;
636 }
637 // FALLTHROUGH
638
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200639 // conversion not possible
640 case VAR_VOID:
641 case VAR_BLOB:
642 case VAR_FUNC:
643 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200644 case VAR_DICT:
645 case VAR_JOB:
646 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200647 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200648 to_string_error((*type)->tt_type);
649 return FAIL;
650 }
651
652 *type = &t_string;
653 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200655 isn->isn_arg.tostring.offset = offset;
656 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 return OK;
659}
660
661 static int
662check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
663{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200664 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200666 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 {
668 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200669 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200671 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
673 }
674 return OK;
675}
676
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200677 static int
678generate_add_instr(
679 cctx_T *cctx,
680 vartype_T vartype,
681 type_T *type1,
682 type_T *type2)
683{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100684 garray_T *stack = &cctx->ctx_type_stack;
685 isn_T *isn = generate_instr_drop(cctx,
686 vartype == VAR_NUMBER ? ISN_OPNR
687 : vartype == VAR_LIST ? ISN_ADDLIST
688 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200689#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100690 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200691#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100692 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693
694 if (vartype != VAR_LIST && vartype != VAR_BLOB
695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
699 return FAIL;
700
701 if (isn != NULL)
702 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100703
704 // When concatenating two lists with different member types the member type
705 // becomes "any".
706 if (vartype == VAR_LIST
707 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
708 && type1->tt_member != type2->tt_member)
709 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
710
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711 return isn == NULL ? FAIL : OK;
712}
713
714/*
715 * Get the type to use for an instruction for an operation on "type1" and
716 * "type2". If they are matching use a type-specific instruction. Otherwise
717 * fall back to runtime type checking.
718 */
719 static vartype_T
720operator_type(type_T *type1, type_T *type2)
721{
722 if (type1->tt_type == type2->tt_type
723 && (type1->tt_type == VAR_NUMBER
724 || type1->tt_type == VAR_LIST
725#ifdef FEAT_FLOAT
726 || type1->tt_type == VAR_FLOAT
727#endif
728 || type1->tt_type == VAR_BLOB))
729 return type1->tt_type;
730 return VAR_ANY;
731}
732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733/*
734 * Generate an instruction with two arguments. The instruction depends on the
735 * type of the arguments.
736 */
737 static int
738generate_two_op(cctx_T *cctx, char_u *op)
739{
740 garray_T *stack = &cctx->ctx_type_stack;
741 type_T *type1;
742 type_T *type2;
743 vartype_T vartype;
744 isn_T *isn;
745
Bram Moolenaar080457c2020-03-03 21:53:32 +0100746 RETURN_OK_IF_SKIP(cctx);
747
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200748 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
750 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200751 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
753 switch (*op)
754 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200755 case '+':
756 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 break;
759
760 case '-':
761 case '*':
762 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
763 op) == FAIL)
764 return FAIL;
765 if (vartype == VAR_NUMBER)
766 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
767#ifdef FEAT_FLOAT
768 else if (vartype == VAR_FLOAT)
769 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
770#endif
771 else
772 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
773 if (isn != NULL)
774 isn->isn_arg.op.op_type = *op == '*'
775 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
776 break;
777
Bram Moolenaar4c683752020-04-05 21:38:23 +0200778 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200780 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 && type2->tt_type != VAR_NUMBER))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 return FAIL;
785 }
786 isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
788 if (isn != NULL)
789 isn->isn_arg.op.op_type = EXPR_REM;
790 break;
791 }
792
793 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 {
796 type_T *type = &t_any;
797
798#ifdef FEAT_FLOAT
799 // float+number and number+float results in float
800 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
801 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
802 type = &t_float;
803#endif
804 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
805 }
806
807 return OK;
808}
809
810/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 * Get the instruction to use for comparing "type1" with "type2"
812 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200814 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100815get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816{
817 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818
Bram Moolenaar4c683752020-04-05 21:38:23 +0200819 if (type1 == VAR_UNKNOWN)
820 type1 = VAR_ANY;
821 if (type2 == VAR_UNKNOWN)
822 type2 = VAR_ANY;
823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 if (type1 == type2)
825 {
826 switch (type1)
827 {
828 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
829 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
830 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
831 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
832 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
833 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
834 case VAR_LIST: isntype = ISN_COMPARELIST; break;
835 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
836 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 default: isntype = ISN_COMPAREANY; break;
838 }
839 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200840 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100842 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 isntype = ISN_COMPAREANY;
844
Bram Moolenaar657137c2021-01-09 15:45:23 +0100845 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 && (isntype == ISN_COMPAREBOOL
847 || isntype == ISN_COMPARESPECIAL
848 || isntype == ISN_COMPARENR
849 || isntype == ISN_COMPAREFLOAT))
850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200851 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 }
855 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100856 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
858 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100859 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
860 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 && (type1 == VAR_BLOB || type2 == VAR_BLOB
862 || type1 == VAR_LIST || type2 == VAR_LIST))))
863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200864 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200866 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200868 return isntype;
869}
870
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200871 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200873{
874 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
875 return FAIL;
876 return OK;
877}
878
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879/*
880 * Generate an ISN_COMPARE* instruction with a boolean result.
881 */
882 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100883generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884{
885 isntype_T isntype;
886 isn_T *isn;
887 garray_T *stack = &cctx->ctx_type_stack;
888 vartype_T type1;
889 vartype_T type2;
890
891 RETURN_OK_IF_SKIP(cctx);
892
893 // Get the known type of the two items on the stack. If they are matching
894 // use a type-specific instruction. Otherwise fall back to runtime type
895 // checking.
896 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
897 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100898 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200899 if (isntype == ISN_DROP)
900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
902 if ((isn = generate_instr(cctx, isntype)) == NULL)
903 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100904 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 isn->isn_arg.op.op_ic = ic;
906
907 // takes two arguments, puts one bool back
908 if (stack->ga_len >= 2)
909 {
910 --stack->ga_len;
911 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
912 }
913
914 return OK;
915}
916
917/*
918 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200919 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 */
921 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200922generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923{
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926
Bram Moolenaar080457c2020-03-03 21:53:32 +0100927 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
929 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200930 isn->isn_arg.tobool.invert = invert;
931 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200934 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 return OK;
937}
938
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200939/*
940 * Generate an ISN_COND2BOOL instruction.
941 */
942 static int
943generate_COND2BOOL(cctx_T *cctx)
944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
950 return FAIL;
951
952 // type becomes bool
953 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
954
955 return OK;
956}
957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200959generate_TYPECHECK(
960 cctx_T *cctx,
961 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100962 int offset,
963 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964{
965 isn_T *isn;
966 garray_T *stack = &cctx->ctx_type_stack;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
970 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100972 isn->isn_arg.type.ct_off = (int8_T)offset;
973 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975 // type becomes expected
976 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977
978 return OK;
979}
980
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100981 static int
982generate_SETTYPE(
983 cctx_T *cctx,
984 type_T *expected)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
989 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
990 return FAIL;
991 isn->isn_arg.type.ct_type = alloc_type(expected);
992 return OK;
993}
994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200996 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
997 * used. Return FALSE if the types will never match.
998 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100999 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001000use_typecheck(type_T *actual, type_T *expected)
1001{
1002 if (actual->tt_type == VAR_ANY
1003 || actual->tt_type == VAR_UNKNOWN
1004 || (actual->tt_type == VAR_FUNC
1005 && (expected->tt_type == VAR_FUNC
1006 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001007 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1008 && ((actual->tt_member == &t_void)
1009 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001010 return TRUE;
1011 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1012 && actual->tt_type == expected->tt_type)
1013 // This takes care of a nested list or dict.
1014 return use_typecheck(actual->tt_member, expected->tt_member);
1015 return FALSE;
1016}
1017
1018/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001019 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001020 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001021 * - "actual" is a type that can be "expected" type: add a runtime check; or
1022 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001023 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1024 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001025 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001026 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001027need_type(
1028 type_T *actual,
1029 type_T *expected,
1030 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001031 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001032 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001033 int silent,
1034 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001036 where_T where;
1037
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001038 if (expected == &t_bool && actual != &t_bool
1039 && (actual->tt_flags & TTFLAG_BOOL_OK))
1040 {
1041 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1042 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001043 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001044 return OK;
1045 }
1046
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001047 where.wt_index = arg_idx;
1048 where.wt_variable = FALSE;
1049 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001050 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001051
1052 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001053 // If it's a constant a runtime check makes no sense.
1054 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001055 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001056 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001057 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001058 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001059
1060 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001061 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001062 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001063}
1064
1065/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001066 * Check that the top of the type stack has a type that can be used as a
1067 * condition. Give an error and return FAIL if not.
1068 */
1069 static int
1070bool_on_stack(cctx_T *cctx)
1071{
1072 garray_T *stack = &cctx->ctx_type_stack;
1073 type_T *type;
1074
1075 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1076 if (type == &t_bool)
1077 return OK;
1078
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001079 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001080 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1081 // This requires a runtime type check.
1082 return generate_COND2BOOL(cctx);
1083
Bram Moolenaar351ead02021-01-16 16:07:01 +01001084 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001085}
1086
1087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Generate an ISN_PUSHNR instruction.
1089 */
1090 static int
1091generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1092{
1093 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001094 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.number = number;
1100
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001101 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001102 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001103 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_PUSHBOOL instruction.
1109 */
1110 static int
1111generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.number = number;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHSPEC instruction.
1125 */
1126 static int
1127generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139#ifdef FEAT_FLOAT
1140/*
1141 * Generate an ISN_PUSHF instruction.
1142 */
1143 static int
1144generate_PUSHF(cctx_T *cctx, float_T fnumber)
1145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1150 return FAIL;
1151 isn->isn_arg.fnumber = fnumber;
1152
1153 return OK;
1154}
1155#endif
1156
1157/*
1158 * Generate an ISN_PUSHS instruction.
1159 * Consumes "str".
1160 */
1161 static int
1162generate_PUSHS(cctx_T *cctx, char_u *str)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar508b5612021-01-02 18:17:26 +01001166 if (cctx->ctx_skip == SKIP_YES)
1167 {
1168 vim_free(str);
1169 return OK;
1170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1172 return FAIL;
1173 isn->isn_arg.string = str;
1174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001179 * Generate an ISN_PUSHCHANNEL instruction.
1180 * Consumes "channel".
1181 */
1182 static int
1183generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1184{
1185 isn_T *isn;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001188 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.channel = channel;
1191
1192 return OK;
1193}
1194
1195/*
1196 * Generate an ISN_PUSHJOB instruction.
1197 * Consumes "job".
1198 */
1199 static int
1200generate_PUSHJOB(cctx_T *cctx, job_T *job)
1201{
1202 isn_T *isn;
1203
Bram Moolenaar080457c2020-03-03 21:53:32 +01001204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001205 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001206 return FAIL;
1207 isn->isn_arg.job = job;
1208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 * Generate an ISN_PUSHBLOB instruction.
1214 * Consumes "blob".
1215 */
1216 static int
1217generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1218{
1219 isn_T *isn;
1220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1223 return FAIL;
1224 isn->isn_arg.blob = blob;
1225
1226 return OK;
1227}
1228
1229/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001230 * Generate an ISN_PUSHFUNC instruction with name "name".
1231 * Consumes "name".
1232 */
1233 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001234generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235{
1236 isn_T *isn;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001239 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001240 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001241 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001242
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001247 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001248 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1249 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001250 */
1251 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001252generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001253{
1254 isn_T *isn;
1255 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001256 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1257 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001258 type_T *item_type = &t_any;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001262 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001263 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001264 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001265 emsg(_(e_listreq));
1266 return FAIL;
1267 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001268 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001269 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1270 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001271 isn->isn_arg.getitem.gi_index = index;
1272 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273
1274 // add the item type to the type stack
1275 if (ga_grow(stack, 1) == FAIL)
1276 return FAIL;
1277 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1278 ++stack->ga_len;
1279 return OK;
1280}
1281
1282/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001283 * Generate an ISN_SLICE instruction with "count".
1284 */
1285 static int
1286generate_SLICE(cctx_T *cctx, int count)
1287{
1288 isn_T *isn;
1289
1290 RETURN_OK_IF_SKIP(cctx);
1291 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1292 return FAIL;
1293 isn->isn_arg.number = count;
1294 return OK;
1295}
1296
1297/*
1298 * Generate an ISN_CHECKLEN instruction with "min_len".
1299 */
1300 static int
1301generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306
1307 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.checklen.cl_min_len = min_len;
1310 isn->isn_arg.checklen.cl_more_OK = more_OK;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Generate an ISN_STORE instruction.
1317 */
1318 static int
1319generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1320{
1321 isn_T *isn;
1322
Bram Moolenaar080457c2020-03-03 21:53:32 +01001323 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1325 return FAIL;
1326 if (name != NULL)
1327 isn->isn_arg.string = vim_strsave(name);
1328 else
1329 isn->isn_arg.number = idx;
1330
1331 return OK;
1332}
1333
1334/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001335 * Generate an ISN_STOREOUTER instruction.
1336 */
1337 static int
1338generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1339{
1340 isn_T *isn;
1341
1342 RETURN_OK_IF_SKIP(cctx);
1343 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.outer.outer_idx = idx;
1346 isn->isn_arg.outer.outer_depth = level;
1347
1348 return OK;
1349}
1350
1351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1353 */
1354 static int
1355generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1356{
1357 isn_T *isn;
1358
Bram Moolenaar080457c2020-03-03 21:53:32 +01001359 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1361 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001362 isn->isn_arg.storenr.stnr_idx = idx;
1363 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_STOREOPT instruction
1370 */
1371 static int
1372generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1373{
1374 isn_T *isn;
1375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001377 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 return FAIL;
1379 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1380 isn->isn_arg.storeopt.so_flags = opt_flags;
1381
1382 return OK;
1383}
1384
1385/*
1386 * Generate an ISN_LOAD or similar instruction.
1387 */
1388 static int
1389generate_LOAD(
1390 cctx_T *cctx,
1391 isntype_T isn_type,
1392 int idx,
1393 char_u *name,
1394 type_T *type)
1395{
1396 isn_T *isn;
1397
Bram Moolenaar080457c2020-03-03 21:53:32 +01001398 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1400 return FAIL;
1401 if (name != NULL)
1402 isn->isn_arg.string = vim_strsave(name);
1403 else
1404 isn->isn_arg.number = idx;
1405
1406 return OK;
1407}
1408
1409/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001410 * Generate an ISN_LOADOUTER instruction
1411 */
1412 static int
1413generate_LOADOUTER(
1414 cctx_T *cctx,
1415 int idx,
1416 int nesting,
1417 type_T *type)
1418{
1419 isn_T *isn;
1420
1421 RETURN_OK_IF_SKIP(cctx);
1422 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1423 return FAIL;
1424 isn->isn_arg.outer.outer_idx = idx;
1425 isn->isn_arg.outer.outer_depth = nesting;
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001431 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001432 */
1433 static int
1434generate_LOADV(
1435 cctx_T *cctx,
1436 char_u *name,
1437 int error)
1438{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001439 int di_flags;
1440 int vidx = find_vim_var(name, &di_flags);
1441 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444 if (vidx < 0)
1445 {
1446 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001447 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 return FAIL;
1449 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001450 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001452 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453}
1454
1455/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001456 * Generate an ISN_UNLET instruction.
1457 */
1458 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001459generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001460{
1461 isn_T *isn;
1462
1463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001464 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 return FAIL;
1466 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1467 isn->isn_arg.unlet.ul_forceit = forceit;
1468
1469 return OK;
1470}
1471
1472/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001473 * Generate an ISN_LOCKCONST instruction.
1474 */
1475 static int
1476generate_LOCKCONST(cctx_T *cctx)
1477{
1478 isn_T *isn;
1479
1480 RETURN_OK_IF_SKIP(cctx);
1481 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1482 return FAIL;
1483 return OK;
1484}
1485
1486/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 * Generate an ISN_LOADS instruction.
1488 */
1489 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001490generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001494 int sid,
1495 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498
Bram Moolenaar080457c2020-03-03 21:53:32 +01001499 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001500 if (isn_type == ISN_LOADS)
1501 isn = generate_instr_type(cctx, isn_type, type);
1502 else
1503 isn = generate_instr_drop(cctx, isn_type, 1);
1504 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1507 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
1509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1514 */
1515 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 cctx_T *cctx,
1518 isntype_T isn_type,
1519 int sid,
1520 int idx,
1521 type_T *type)
1522{
1523 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001524 scriptref_T *sref;
1525 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
Bram Moolenaar080457c2020-03-03 21:53:32 +01001527 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (isn_type == ISN_LOADSCRIPT)
1529 isn = generate_instr_type(cctx, isn_type, type);
1530 else
1531 isn = generate_instr_drop(cctx, isn_type, 1);
1532 if (isn == NULL)
1533 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001534
1535 // This requires three arguments, which doesn't fit in an instruction, thus
1536 // we need to allocate a struct for this.
1537 sref = ALLOC_ONE(scriptref_T);
1538 if (sref == NULL)
1539 return FAIL;
1540 isn->isn_arg.script.scriptref = sref;
1541 sref->sref_sid = sid;
1542 sref->sref_idx = idx;
1543 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001544 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_NEWLIST instruction.
1550 */
1551 static int
1552generate_NEWLIST(cctx_T *cctx, int count)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 type_T *type;
1557 type_T *member;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.number = count;
1563
Bram Moolenaar127542b2020-08-09 17:22:04 +02001564 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001565 if (count == 0)
1566 member = &t_void;
1567 else
1568 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001569 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1570 cctx->ctx_type_list);
1571 type = get_list_type(member, cctx->ctx_type_list);
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 // drop the value types
1574 stack->ga_len -= count;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // add the list type to the type stack
1577 if (ga_grow(stack, 1) == FAIL)
1578 return FAIL;
1579 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_NEWDICT instruction.
1587 */
1588 static int
1589generate_NEWDICT(cctx_T *cctx, int count)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 type_T *type;
1594 type_T *member;
1595
Bram Moolenaar080457c2020-03-03 21:53:32 +01001596 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1598 return FAIL;
1599 isn->isn_arg.number = count;
1600
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001601 if (count == 0)
1602 member = &t_void;
1603 else
1604 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001605 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1606 cctx->ctx_type_list);
1607 type = get_dict_type(member, cctx->ctx_type_list);
1608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 // drop the key and value types
1610 stack->ga_len -= 2 * count;
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 // add the dict type to the type stack
1613 if (ga_grow(stack, 1) == FAIL)
1614 return FAIL;
1615 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1616 ++stack->ga_len;
1617
1618 return OK;
1619}
1620
1621/*
1622 * Generate an ISN_FUNCREF instruction.
1623 */
1624 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001625generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626{
1627 isn_T *isn;
1628 garray_T *stack = &cctx->ctx_type_stack;
1629
Bram Moolenaar080457c2020-03-03 21:53:32 +01001630 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1632 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001633 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001634 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001636 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001637 // the nested context, including this one.
1638 if (ufunc->uf_flags & FC_CLOSURE)
1639 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if (ga_grow(stack, 1) == FAIL)
1642 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001643 ((type_T **)stack->ga_data)[stack->ga_len] =
1644 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 ++stack->ga_len;
1646
1647 return OK;
1648}
1649
1650/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001651 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001652 * "lambda_name" and "func_name" must be in allocated memory and will be
1653 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001654 */
1655 static int
1656generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1657{
1658 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001659
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001660 if (cctx->ctx_skip == SKIP_YES)
1661 {
1662 vim_free(lambda_name);
1663 vim_free(func_name);
1664 return OK;
1665 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001666 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001667 {
1668 vim_free(lambda_name);
1669 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001670 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001671 }
1672 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001673 isn->isn_arg.newfunc.nf_global = func_name;
1674
1675 return OK;
1676}
1677
1678/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001679 * Generate an ISN_DEF instruction: list functions
1680 */
1681 static int
1682generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1683{
1684 isn_T *isn;
1685
1686 RETURN_OK_IF_SKIP(cctx);
1687 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1688 return FAIL;
1689 if (len > 0)
1690 {
1691 isn->isn_arg.string = vim_strnsave(name, len);
1692 if (isn->isn_arg.string == NULL)
1693 return FAIL;
1694 }
1695 return OK;
1696}
1697
1698/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 * Generate an ISN_JUMP instruction.
1700 */
1701 static int
1702generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1703{
1704 isn_T *isn;
1705 garray_T *stack = &cctx->ctx_type_stack;
1706
Bram Moolenaar080457c2020-03-03 21:53:32 +01001707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.jump.jump_when = when;
1711 isn->isn_arg.jump.jump_where = where;
1712
1713 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1714 --stack->ga_len;
1715
1716 return OK;
1717}
1718
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001719/*
1720 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1721 */
1722 static int
1723generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1724{
1725 isn_T *isn;
1726
1727 RETURN_OK_IF_SKIP(cctx);
1728 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1729 return FAIL;
1730 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1731 // jump_where is set later
1732 return OK;
1733}
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 static int
1736generate_FOR(cctx_T *cctx, int loop_idx)
1737{
1738 isn_T *isn;
1739 garray_T *stack = &cctx->ctx_type_stack;
1740
Bram Moolenaar080457c2020-03-03 21:53:32 +01001741 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1743 return FAIL;
1744 isn->isn_arg.forloop.for_idx = loop_idx;
1745
1746 if (ga_grow(stack, 1) == FAIL)
1747 return FAIL;
1748 // type doesn't matter, will be stored next
1749 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1750 ++stack->ga_len;
1751
1752 return OK;
1753}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001754/*
1755 * Generate an ISN_TRYCONT instruction.
1756 */
1757 static int
1758generate_TRYCONT(cctx_T *cctx, int levels, int where)
1759{
1760 isn_T *isn;
1761
1762 RETURN_OK_IF_SKIP(cctx);
1763 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.trycont.tct_levels = levels;
1766 isn->isn_arg.trycont.tct_where = where;
1767
1768 return OK;
1769}
1770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772/*
1773 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001774 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 * Return FAIL if the number of arguments is wrong.
1776 */
1777 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001778generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779{
1780 isn_T *isn;
1781 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001782 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001783 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001784 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar080457c2020-03-03 21:53:32 +01001786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001787 argoff = check_internal_func(func_idx, argcount);
1788 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 return FAIL;
1790
Bram Moolenaar389df252020-07-09 21:20:47 +02001791 if (method_call && argoff > 1)
1792 {
1793 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1794 return FAIL;
1795 isn->isn_arg.shuffle.shfl_item = argcount;
1796 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1797 }
1798
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001799 if (argcount > 0)
1800 {
1801 // Check the types of the arguments.
1802 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001803 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1804 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001805 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001806 if (internal_func_is_map(func_idx))
1807 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001808 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1811 return FAIL;
1812 isn->isn_arg.bfunc.cbf_idx = func_idx;
1813 isn->isn_arg.bfunc.cbf_argcount = argcount;
1814
Bram Moolenaar94738d82020-10-21 14:25:07 +02001815 // Drop the argument types and push the return type.
1816 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if (ga_grow(stack, 1) == FAIL)
1818 return FAIL;
1819 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001820 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001821 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001823 if (maptype != NULL && maptype->tt_member != NULL
1824 && maptype->tt_member != &t_any)
1825 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001826 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 return OK;
1829}
1830
1831/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001832 * Generate an ISN_LISTAPPEND instruction. Works like add().
1833 * Argument count is already checked.
1834 */
1835 static int
1836generate_LISTAPPEND(cctx_T *cctx)
1837{
1838 garray_T *stack = &cctx->ctx_type_stack;
1839 type_T *list_type;
1840 type_T *item_type;
1841 type_T *expected;
1842
1843 // Caller already checked that list_type is a list.
1844 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1845 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1846 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001847 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001848 return FAIL;
1849
1850 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1851 return FAIL;
1852
1853 --stack->ga_len; // drop the argument
1854 return OK;
1855}
1856
1857/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001858 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1859 * Argument count is already checked.
1860 */
1861 static int
1862generate_BLOBAPPEND(cctx_T *cctx)
1863{
1864 garray_T *stack = &cctx->ctx_type_stack;
1865 type_T *item_type;
1866
1867 // Caller already checked that blob_type is a blob.
1868 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001869 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001870 return FAIL;
1871
1872 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1873 return FAIL;
1874
1875 --stack->ga_len; // drop the argument
1876 return OK;
1877}
1878
1879/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001880 * Return TRUE if "ufunc" should be compiled, taking into account whether
1881 * "profile" indicates profiling is to be done.
1882 */
1883 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001884func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001885{
1886 switch (ufunc->uf_def_status)
1887 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001888 case UF_TO_BE_COMPILED:
1889 return TRUE;
1890
Bram Moolenaarb2049902021-01-24 12:53:53 +01001891 case UF_COMPILED:
1892 {
1893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1894 + ufunc->uf_dfunc_idx;
1895
Bram Moolenaare99d4222021-06-13 14:01:26 +02001896 switch (compile_type)
1897 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001898 case CT_PROFILE:
1899#ifdef FEAT_PROFILE
1900 return dfunc->df_instr_prof == NULL;
1901#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001902 case CT_NONE:
1903 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001904 case CT_DEBUG:
1905 return dfunc->df_instr_debug == NULL;
1906 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001907 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001908
1909 case UF_NOT_COMPILED:
1910 case UF_COMPILE_ERROR:
1911 case UF_COMPILING:
1912 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001913 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001914 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001915}
1916
1917/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 * Generate an ISN_DCALL or ISN_UCALL instruction.
1919 * Return FAIL if the number of arguments is wrong.
1920 */
1921 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001922generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923{
1924 isn_T *isn;
1925 garray_T *stack = &cctx->ctx_type_stack;
1926 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001927 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928
Bram Moolenaar080457c2020-03-03 21:53:32 +01001929 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 if (argcount > regular_args && !has_varargs(ufunc))
1931 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001932 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 return FAIL;
1934 }
1935 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1936 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001937 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 return FAIL;
1939 }
1940
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001941 if (ufunc->uf_def_status != UF_NOT_COMPILED
1942 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001943 {
1944 int i;
1945
1946 for (i = 0; i < argcount; ++i)
1947 {
1948 type_T *expected;
1949 type_T *actual;
1950
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001951 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1952 if (actual == &t_special
1953 && i >= regular_args - ufunc->uf_def_args.ga_len)
1954 {
1955 // assume v:none used for default argument value
1956 continue;
1957 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001958 if (i < regular_args)
1959 {
1960 if (ufunc->uf_arg_types == NULL)
1961 continue;
1962 expected = ufunc->uf_arg_types[i];
1963 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001964 else if (ufunc->uf_va_type == NULL
1965 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001966 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001967 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001968 else
1969 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001970 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001971 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001972 {
1973 arg_type_mismatch(expected, actual, i + 1);
1974 return FAIL;
1975 }
1976 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001977 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001978 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001979 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001980 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001981 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001982 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1983 {
1984 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1985 ufunc->uf_name);
1986 return FAIL;
1987 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001990 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001991 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001993 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 {
1995 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1996 isn->isn_arg.dfunc.cdf_argcount = argcount;
1997 }
1998 else
1999 {
2000 // A user function may be deleted and redefined later, can't use the
2001 // ufunc pointer, need to look it up again at runtime.
2002 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2003 isn->isn_arg.ufunc.cuf_argcount = argcount;
2004 }
2005
2006 stack->ga_len -= argcount; // drop the arguments
2007 if (ga_grow(stack, 1) == FAIL)
2008 return FAIL;
2009 // add return value
2010 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2011 ++stack->ga_len;
2012
2013 return OK;
2014}
2015
2016/*
2017 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2018 */
2019 static int
2020generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2021{
2022 isn_T *isn;
2023 garray_T *stack = &cctx->ctx_type_stack;
2024
Bram Moolenaar080457c2020-03-03 21:53:32 +01002025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2027 return FAIL;
2028 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2029 isn->isn_arg.ufunc.cuf_argcount = argcount;
2030
2031 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002032 if (ga_grow(stack, 1) == FAIL)
2033 return FAIL;
2034 // add return value
2035 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2036 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 return OK;
2039}
2040
2041/*
2042 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002043 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 */
2045 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002046generate_PCALL(
2047 cctx_T *cctx,
2048 int argcount,
2049 char_u *name,
2050 type_T *type,
2051 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052{
2053 isn_T *isn;
2054 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002055 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056
Bram Moolenaar080457c2020-03-03 21:53:32 +01002057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002058
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002059 if (type->tt_type == VAR_ANY)
2060 ret_type = &t_any;
2061 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002062 {
2063 if (type->tt_argcount != -1)
2064 {
2065 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2066
2067 if (argcount < type->tt_min_argcount - varargs)
2068 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002069 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002070 return FAIL;
2071 }
2072 if (!varargs && argcount > type->tt_argcount)
2073 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002074 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002075 return FAIL;
2076 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002077 if (type->tt_args != NULL)
2078 {
2079 int i;
2080
2081 for (i = 0; i < argcount; ++i)
2082 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002083 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002084 type_T *actual = ((type_T **)stack->ga_data)[
2085 stack->ga_len + offset];
2086 type_T *expected;
2087
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002088 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002089 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002090 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002091 else if (i >= type->tt_min_argcount
2092 && actual == &t_special)
2093 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002094 else
2095 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002096 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002097 cctx, TRUE, FALSE) == FAIL)
2098 {
2099 arg_type_mismatch(expected, actual, i + 1);
2100 return FAIL;
2101 }
2102 }
2103 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002104 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002106 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002107 else
2108 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002109 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002110 return FAIL;
2111 }
2112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2114 return FAIL;
2115 isn->isn_arg.pfunc.cpf_top = at_top;
2116 isn->isn_arg.pfunc.cpf_argcount = argcount;
2117
2118 stack->ga_len -= argcount; // drop the arguments
2119
2120 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002121 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002123 // If partial is above the arguments it must be cleared and replaced with
2124 // the return value.
2125 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2126 return FAIL;
2127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 return OK;
2129}
2130
2131/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002132 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 */
2134 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002135generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136{
2137 isn_T *isn;
2138 garray_T *stack = &cctx->ctx_type_stack;
2139 type_T *type;
2140
Bram Moolenaar080457c2020-03-03 21:53:32 +01002141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002142 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002144 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002146 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002148 if (type->tt_type != VAR_DICT && type != &t_any)
2149 {
2150 emsg(_(e_dictreq));
2151 return FAIL;
2152 }
2153 // change dict type to dict member type
2154 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002155 {
2156 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2157 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
2160 return OK;
2161}
2162
2163/*
2164 * Generate an ISN_ECHO instruction.
2165 */
2166 static int
2167generate_ECHO(cctx_T *cctx, int with_white, int count)
2168{
2169 isn_T *isn;
2170
Bram Moolenaar080457c2020-03-03 21:53:32 +01002171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2173 return FAIL;
2174 isn->isn_arg.echo.echo_with_white = with_white;
2175 isn->isn_arg.echo.echo_count = count;
2176
2177 return OK;
2178}
2179
Bram Moolenaarad39c092020-02-26 18:23:43 +01002180/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002181 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002182 */
2183 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002184generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002185{
2186 isn_T *isn;
2187
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002188 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002189 return FAIL;
2190 isn->isn_arg.number = count;
2191
2192 return OK;
2193}
2194
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002195/*
2196 * Generate an ISN_PUT instruction.
2197 */
2198 static int
2199generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2200{
2201 isn_T *isn;
2202
2203 RETURN_OK_IF_SKIP(cctx);
2204 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2205 return FAIL;
2206 isn->isn_arg.put.put_regname = regname;
2207 isn->isn_arg.put.put_lnum = lnum;
2208 return OK;
2209}
2210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 static int
2212generate_EXEC(cctx_T *cctx, char_u *line)
2213{
2214 isn_T *isn;
2215
Bram Moolenaar080457c2020-03-03 21:53:32 +01002216 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2218 return FAIL;
2219 isn->isn_arg.string = vim_strsave(line);
2220 return OK;
2221}
2222
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002223 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002224generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2225{
2226 isn_T *isn;
2227 garray_T *stack = &cctx->ctx_type_stack;
2228
2229 RETURN_OK_IF_SKIP(cctx);
2230 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2231 return FAIL;
2232 isn->isn_arg.string = vim_strsave(line);
2233
2234 if (ga_grow(stack, 1) == FAIL)
2235 return FAIL;
2236 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2237 ++stack->ga_len;
2238
2239 return OK;
2240}
2241
2242 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002243generate_EXECCONCAT(cctx_T *cctx, int count)
2244{
2245 isn_T *isn;
2246
2247 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2248 return FAIL;
2249 isn->isn_arg.number = count;
2250 return OK;
2251}
2252
Bram Moolenaar08597872020-12-10 19:43:40 +01002253/*
2254 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2255 */
2256 static int
2257generate_RANGE(cctx_T *cctx, char_u *range)
2258{
2259 isn_T *isn;
2260 garray_T *stack = &cctx->ctx_type_stack;
2261
2262 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2263 return FAIL;
2264 isn->isn_arg.string = range;
2265
2266 if (ga_grow(stack, 1) == FAIL)
2267 return FAIL;
2268 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2269 ++stack->ga_len;
2270 return OK;
2271}
2272
Bram Moolenaar792f7862020-11-23 08:31:18 +01002273 static int
2274generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2275{
2276 isn_T *isn;
2277
2278 RETURN_OK_IF_SKIP(cctx);
2279 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2280 return FAIL;
2281 isn->isn_arg.unpack.unp_count = var_count;
2282 isn->isn_arg.unpack.unp_semicolon = semicolon;
2283 return OK;
2284}
2285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002287 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002288 */
2289 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002290generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002291{
2292 isn_T *isn;
2293
Bram Moolenaarfa984412021-03-25 22:15:28 +01002294 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002295 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002296 cctx->ctx_has_cmdmod = TRUE;
2297
2298 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002299 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002300 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2301 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2302 return FAIL;
2303 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002304 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002305 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002306 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002307
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002308 return OK;
2309}
2310
2311 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002312generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002313{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002314 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2315 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002316 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002317 return OK;
2318}
2319
Bram Moolenaarfa984412021-03-25 22:15:28 +01002320 static int
2321misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002322{
2323 garray_T *instr = &cctx->ctx_instr;
2324
Bram Moolenaara91a7132021-03-25 21:12:15 +01002325 if (cctx->ctx_has_cmdmod
2326 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2327 == ISN_CMDMOD)
2328 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002329 emsg(_(e_misplaced_command_modifier));
2330 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002331 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002332 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002333}
2334
2335/*
2336 * Get the index of the current instruction.
2337 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2338 */
2339 static int
2340current_instr_idx(cctx_T *cctx)
2341{
2342 garray_T *instr = &cctx->ctx_instr;
2343 int idx = instr->ga_len;
2344
Bram Moolenaare99d4222021-06-13 14:01:26 +02002345 while (idx > 0)
2346 {
2347 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002348 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002349 {
2350 --idx;
2351 continue;
2352 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002353#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002354 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2355 {
2356 --idx;
2357 continue;
2358 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002359#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002360 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2361 {
2362 --idx;
2363 continue;
2364 }
2365 break;
2366 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002367 return idx;
2368}
2369
Bram Moolenaarf002a412021-01-24 13:34:18 +01002370#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002371 static void
2372may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2373{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002374 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002375 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002376}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002377#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002378
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002381 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002383 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002384reserve_local(
2385 cctx_T *cctx,
2386 char_u *name,
2387 size_t len,
2388 int isConst,
2389 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002392 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002394 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002396 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002397 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 }
2399
2400 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002401 return NULL;
2402 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002403 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002405 // Every local variable uses the next entry on the stack. We could re-use
2406 // the last ones when leaving a scope, but then variables used in a closure
2407 // might get overwritten. To keep things simple do not re-use stack
2408 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002409 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2410 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002411
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002412 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 lvar->lv_const = isConst;
2414 lvar->lv_type = type;
2415
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002416 // Remember the name for debugging.
2417 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2418 return NULL;
2419 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2420 vim_strsave(lvar->lv_name);
2421 ++dfunc->df_var_names.ga_len;
2422
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002423 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424}
2425
2426/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002427 * Remove local variables above "new_top".
2428 */
2429 static void
2430unwind_locals(cctx_T *cctx, int new_top)
2431{
2432 if (cctx->ctx_locals.ga_len > new_top)
2433 {
2434 int idx;
2435 lvar_T *lvar;
2436
2437 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2438 {
2439 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2440 vim_free(lvar->lv_name);
2441 }
2442 }
2443 cctx->ctx_locals.ga_len = new_top;
2444}
2445
2446/*
2447 * Free all local variables.
2448 */
2449 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002451{
2452 unwind_locals(cctx, 0);
2453 ga_clear(&cctx->ctx_locals);
2454}
2455
2456/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002457 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2458 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2459 * error if the variable was defined with :const.
2460 */
2461 static int
2462check_item_writable(svar_T *sv, int check_writable, char_u *name)
2463{
2464 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2465 || (check_writable == ASSIGN_FINAL
2466 && sv->sv_const == ASSIGN_CONST))
2467 {
2468 semsg(_(e_readonlyvar), name);
2469 return FAIL;
2470 }
2471 return OK;
2472}
2473
2474/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002476 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 * Returns the index in "sn_var_vals" if found.
2478 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002479 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 */
2481 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002482get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483{
2484 hashtab_T *ht;
2485 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002486 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002487 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 int idx;
2489
Bram Moolenaare3d46852020-08-29 13:39:17 +02002490 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002492 if (sid == current_sctx.sc_sid)
2493 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002494 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002495
2496 if (sav == NULL)
2497 return -2;
2498 idx = sav->sav_var_vals_idx;
2499 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002500 if (check_item_writable(sv, check_writable, name) == FAIL)
2501 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 return idx;
2503 }
2504
2505 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 ht = &SCRIPT_VARS(sid);
2507 di = find_var_in_ht(ht, 0, name, TRUE);
2508 if (di == NULL)
2509 return -2;
2510
2511 // Now find the svar_T index in sn_var_vals.
2512 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2513 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 if (sv->sv_tv == &di->di_tv)
2516 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002517 if (check_item_writable(sv, check_writable, name) == FAIL)
2518 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 return idx;
2520 }
2521 }
2522 return -1;
2523}
2524
2525/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002526 * Find "name" in imported items of the current script or in "cctx" if not
2527 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 */
2529 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002530find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int idx;
2533
Bram Moolenaare3d46852020-08-29 13:39:17 +02002534 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002535 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 if (cctx != NULL)
2537 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2538 {
2539 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2540 + idx;
2541
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002542 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2543 : STRLEN(import->imp_name) == len
2544 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 return import;
2546 }
2547
Bram Moolenaarefa94442020-08-08 22:16:00 +02002548 return find_imported_in_script(name, len, current_sctx.sc_sid);
2549}
2550
2551 imported_T *
2552find_imported_in_script(char_u *name, size_t len, int sid)
2553{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002554 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002555 int idx;
2556
Bram Moolenaare3d46852020-08-29 13:39:17 +02002557 if (!SCRIPT_ID_VALID(sid))
2558 return NULL;
2559 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2561 {
2562 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2563
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002564 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2565 : STRLEN(import->imp_name) == len
2566 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 return import;
2568 }
2569 return NULL;
2570}
2571
2572/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002573 * Free all imported variables.
2574 */
2575 static void
2576free_imported(cctx_T *cctx)
2577{
2578 int idx;
2579
2580 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2581 {
2582 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2583
2584 vim_free(import->imp_name);
2585 }
2586 ga_clear(&cctx->ctx_imports);
2587}
2588
2589/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002590 * Return a pointer to the next line that isn't empty or only contains a
2591 * comment. Skips over white space.
2592 * Returns NULL if there is none.
2593 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002594 char_u *
2595peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002596{
2597 int lnum = cctx->ctx_lnum;
2598
2599 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2600 {
2601 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002602 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002603
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002604 // ignore NULLs inserted for continuation lines
2605 if (line != NULL)
2606 {
2607 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002608 if (vim9_bad_comment(p))
2609 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002610 if (*p != NUL && !vim9_comment_start(p))
2611 return p;
2612 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002613 }
2614 return NULL;
2615}
2616
2617/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002618 * Called when checking for a following operator at "arg". When the rest of
2619 * the line is empty or only a comment, peek the next line. If there is a next
2620 * line return a pointer to it and set "nextp".
2621 * Otherwise skip over white space.
2622 */
2623 static char_u *
2624may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2625{
2626 char_u *p = skipwhite(arg);
2627
2628 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002629 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002630 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002631 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002632 if (*nextp != NULL)
2633 return *nextp;
2634 }
2635 return p;
2636}
2637
2638/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002639 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002640 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002641 * Returns NULL when at the end.
2642 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002643 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002644next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002645{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002646 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647
2648 do
2649 {
2650 ++cctx->ctx_lnum;
2651 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002652 {
2653 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002654 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002655 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002657 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002658 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002660 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002661 return line;
2662}
2663
2664/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002665 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002666 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002667 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002668 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2669 */
2670 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002671may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002672{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002673 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002674 if (vim9_bad_comment(*arg))
2675 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002676 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002677 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002678 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002679
2680 if (next == NULL)
2681 return FAIL;
2682 *arg = skipwhite(next);
2683 }
2684 return OK;
2685}
2686
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002687/*
2688 * Idem, and give an error when failed.
2689 */
2690 static int
2691may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2692{
2693 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2694 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002695 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002696 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002697 return FAIL;
2698 }
2699 return OK;
2700}
2701
2702
Bram Moolenaara5565e42020-05-09 15:44:01 +02002703// Structure passed between the compile_expr* functions to keep track of
2704// constants that have been parsed but for which no code was produced yet. If
2705// possible expressions on these constants are applied at compile time. If
2706// that is not possible, the code to push the constants needs to be generated
2707// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002708// Using 50 should be more than enough of 5 levels of ().
2709#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002710typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002711 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002713 int pp_is_const; // all generated code was constants, used for a
2714 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715} ppconst_T;
2716
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002717static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002718static int compile_expr0(char_u **arg, cctx_T *cctx);
2719static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2720
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721/*
2722 * Generate a PUSH instruction for "tv".
2723 * "tv" will be consumed or cleared.
2724 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2725 */
2726 static int
2727generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2728{
2729 if (tv != NULL)
2730 {
2731 switch (tv->v_type)
2732 {
2733 case VAR_UNKNOWN:
2734 break;
2735 case VAR_BOOL:
2736 generate_PUSHBOOL(cctx, tv->vval.v_number);
2737 break;
2738 case VAR_SPECIAL:
2739 generate_PUSHSPEC(cctx, tv->vval.v_number);
2740 break;
2741 case VAR_NUMBER:
2742 generate_PUSHNR(cctx, tv->vval.v_number);
2743 break;
2744#ifdef FEAT_FLOAT
2745 case VAR_FLOAT:
2746 generate_PUSHF(cctx, tv->vval.v_float);
2747 break;
2748#endif
2749 case VAR_BLOB:
2750 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2751 tv->vval.v_blob = NULL;
2752 break;
2753 case VAR_STRING:
2754 generate_PUSHS(cctx, tv->vval.v_string);
2755 tv->vval.v_string = NULL;
2756 break;
2757 default:
2758 iemsg("constant type not supported");
2759 clear_tv(tv);
2760 return FAIL;
2761 }
2762 tv->v_type = VAR_UNKNOWN;
2763 }
2764 return OK;
2765}
2766
2767/*
2768 * Generate code for any ppconst entries.
2769 */
2770 static int
2771generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2772{
2773 int i;
2774 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002775 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002776
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002777 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778 for (i = 0; i < ppconst->pp_used; ++i)
2779 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2780 ret = FAIL;
2781 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002782 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002783 return ret;
2784}
2785
2786/*
2787 * Clear ppconst constants. Used when failing.
2788 */
2789 static void
2790clear_ppconst(ppconst_T *ppconst)
2791{
2792 int i;
2793
2794 for (i = 0; i < ppconst->pp_used; ++i)
2795 clear_tv(&ppconst->pp_tv[i]);
2796 ppconst->pp_used = 0;
2797}
2798
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002799/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002800 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002801 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002802 */
2803 static int
2804compile_member(int is_slice, cctx_T *cctx)
2805{
2806 type_T **typep;
2807 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002808 vartype_T vartype;
2809 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002810
Bram Moolenaar261417b2021-05-06 21:04:55 +02002811 // We can index a list, dict and blob. If we don't know the type
2812 // we can use the index value type. If we still don't know use an "ANY"
2813 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002814 typep = ((type_T **)stack->ga_data) + stack->ga_len
2815 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002816 vartype = (*typep)->tt_type;
2817 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002818 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002819 if (*typep == &t_any && idxtype == &t_string)
2820 vartype = VAR_DICT;
2821 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002822 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002823 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002824 return FAIL;
2825 if (is_slice)
2826 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002827 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2828 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002829 FALSE, FALSE) == FAIL)
2830 return FAIL;
2831 }
2832 }
2833
Bram Moolenaar261417b2021-05-06 21:04:55 +02002834 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002835 {
2836 if (is_slice)
2837 {
2838 emsg(_(e_cannot_slice_dictionary));
2839 return FAIL;
2840 }
2841 if ((*typep)->tt_type == VAR_DICT)
2842 {
2843 *typep = (*typep)->tt_member;
2844 if (*typep == &t_unknown)
2845 // empty dict was used
2846 *typep = &t_any;
2847 }
2848 else
2849 {
2850 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2851 FALSE, FALSE) == FAIL)
2852 return FAIL;
2853 *typep = &t_any;
2854 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002855 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856 return FAIL;
2857 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2858 return FAIL;
2859 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002860 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002861 {
2862 *typep = &t_string;
2863 if ((is_slice
2864 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2865 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2866 return FAIL;
2867 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002868 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002870 if (is_slice)
2871 {
2872 *typep = &t_blob;
2873 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2874 return FAIL;
2875 }
2876 else
2877 {
2878 *typep = &t_number;
2879 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2880 return FAIL;
2881 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002882 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002883 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002884 {
2885 if (is_slice)
2886 {
2887 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002888 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002889 2) == FAIL)
2890 return FAIL;
2891 }
2892 else
2893 {
2894 if ((*typep)->tt_type == VAR_LIST)
2895 {
2896 *typep = (*typep)->tt_member;
2897 if (*typep == &t_unknown)
2898 // empty list was used
2899 *typep = &t_any;
2900 }
2901 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002902 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2903 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 return FAIL;
2905 }
2906 }
2907 else
2908 {
2909 emsg(_(e_string_list_dict_or_blob_required));
2910 return FAIL;
2911 }
2912 return OK;
2913}
2914
2915/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002916 * Generate an instruction to load script-local variable "name", without the
2917 * leading "s:".
2918 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 */
2920 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002921compile_load_scriptvar(
2922 cctx_T *cctx,
2923 char_u *name, // variable NUL terminated
2924 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002925 char_u **end, // end of variable
2926 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002928 scriptitem_T *si;
2929 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 imported_T *import;
2931
Bram Moolenaare3d46852020-08-29 13:39:17 +02002932 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2933 return FAIL;
2934 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002935 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002936 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002938 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002939 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2940 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
2942 if (idx >= 0)
2943 {
2944 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2945
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002946 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 current_sctx.sc_sid, idx, sv->sv_type);
2948 return OK;
2949 }
2950
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002951 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 if (import != NULL)
2953 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002954 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002955 {
2956 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002957 char_u *exp_name;
2958 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002959 ufunc_T *ufunc;
2960 type_T *type;
2961
2962 // Used "import * as Name", need to lookup the member.
2963 if (*p != '.')
2964 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002965 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002966 return FAIL;
2967 }
2968 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002969 if (VIM_ISWHITE(*p))
2970 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002971 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002972 return FAIL;
2973 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002974
Bram Moolenaar1c991142020-07-04 13:15:31 +02002975 // isolate one name
2976 exp_name = p;
2977 while (eval_isnamec(*p))
2978 ++p;
2979 cc = *p;
2980 *p = NUL;
2981
Bram Moolenaaredba7072021-03-13 21:14:18 +01002982 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2983 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002984 *p = cc;
2985 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002986 *end = p;
2987
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002988 if (idx < 0)
2989 {
2990 if (*p == '(' && ufunc != NULL)
2991 {
2992 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2993 return OK;
2994 }
2995 return FAIL;
2996 }
2997
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002998 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2999 import->imp_sid,
3000 idx,
3001 type);
3002 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003003 else if (import->imp_funcname != NULL)
3004 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003006 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3007 import->imp_sid,
3008 import->imp_var_vals_idx,
3009 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 return OK;
3011 }
3012
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003013 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003014 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 return FAIL;
3016}
3017
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003018 static int
3019generate_funcref(cctx_T *cctx, char_u *name)
3020{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003021 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003022
3023 if (ufunc == NULL)
3024 return FAIL;
3025
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003026 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003027 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3028 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003029 == FAIL)
3030 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003031 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003032}
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034/*
3035 * Compile a variable name into a load instruction.
3036 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003037 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 * When "error" is FALSE do not give an error when not found.
3039 */
3040 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003041compile_load(
3042 char_u **arg,
3043 char_u *end_arg,
3044 cctx_T *cctx,
3045 int is_expr,
3046 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047{
3048 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003049 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003050 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003052 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053
3054 if (*(*arg + 1) == ':')
3055 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003056 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003057 {
3058 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
Bram Moolenaarfa596382021-04-07 21:58:16 +02003060 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003061 switch (**arg)
3062 {
3063 case 'g': isn_type = ISN_LOADGDICT; break;
3064 case 'w': isn_type = ISN_LOADWDICT; break;
3065 case 't': isn_type = ISN_LOADTDICT; break;
3066 case 'b': isn_type = ISN_LOADBDICT; break;
3067 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003068 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003069 goto theend;
3070 }
3071 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3072 goto theend;
3073 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 else
3076 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 isntype_T isn_type = ISN_DROP;
3078
Bram Moolenaarfa596382021-04-07 21:58:16 +02003079 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003080 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3081 if (name == NULL)
3082 return FAIL;
3083
3084 switch (**arg)
3085 {
3086 case 'v': res = generate_LOADV(cctx, name, error);
3087 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003088 case 's': if (is_expr && ASCII_ISUPPER(*name)
3089 && find_func(name, FALSE, cctx) != NULL)
3090 res = generate_funcref(cctx, name);
3091 else
3092 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003093 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003094 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003095 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003096 {
3097 if (is_expr && ASCII_ISUPPER(*name)
3098 && find_func(name, FALSE, cctx) != NULL)
3099 res = generate_funcref(cctx, name);
3100 else
3101 isn_type = ISN_LOADG;
3102 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003103 else
3104 {
3105 isn_type = ISN_LOADAUTO;
3106 vim_free(name);
3107 name = vim_strnsave(*arg, end - *arg);
3108 if (name == NULL)
3109 return FAIL;
3110 }
3111 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 case 'w': isn_type = ISN_LOADW; break;
3113 case 't': isn_type = ISN_LOADT; break;
3114 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003115 default: // cannot happen, just in case
3116 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003117 goto theend;
3118 }
3119 if (isn_type != ISN_DROP)
3120 {
3121 // Global, Buffer-local, Window-local and Tabpage-local
3122 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003123 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003124 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 }
3127 }
3128 else
3129 {
3130 size_t len = end - *arg;
3131 int idx;
3132 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003133 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
3135 name = vim_strnsave(*arg, end - *arg);
3136 if (name == NULL)
3137 return FAIL;
3138
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003139 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3140 {
3141 script_autoload(name, FALSE);
3142 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3143 }
3144 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3145 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003147 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003148 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 else
3151 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003152 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003153
Bram Moolenaar709664c2020-12-12 14:33:41 +01003154 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003156 type = lvar.lv_type;
3157 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003158 if (lvar.lv_from_outer != 0)
3159 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003160 else
3161 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163 else
3164 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003165 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003166 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003167 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003168 || find_imported(name, 0, cctx) != NULL)
3169 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003170
Bram Moolenaar0f769812020-09-12 18:32:34 +02003171 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003172 // uppercase letter it can be a user defined function.
3173 // generate_funcref() will fail if the function can't be found.
3174 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003175 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 }
3178 if (gen_load)
3179 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003180 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003181 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003182 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003183 cctx->ctx_outer_used = TRUE;
3184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 }
3186
3187 *arg = end;
3188
3189theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003190 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003191 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 vim_free(name);
3193 return res;
3194}
3195
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003196 static void
3197clear_instr_ga(garray_T *gap)
3198{
3199 int idx;
3200
3201 for (idx = 0; idx < gap->ga_len; ++idx)
3202 delete_instr(((isn_T *)gap->ga_data) + idx);
3203 ga_clear(gap);
3204}
3205
3206/*
3207 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3208 * Returns FAIL if compilation fails.
3209 */
3210 static int
3211compile_string(isn_T *isn, cctx_T *cctx)
3212{
3213 char_u *s = isn->isn_arg.string;
3214 garray_T save_ga = cctx->ctx_instr;
3215 int expr_res;
3216 int trailing_error;
3217 int instr_count;
3218 isn_T *instr = NULL;
3219
3220 // Temporarily reset the list of instructions so that the jump labels are
3221 // correct.
3222 cctx->ctx_instr.ga_len = 0;
3223 cctx->ctx_instr.ga_maxlen = 0;
3224 cctx->ctx_instr.ga_data = NULL;
3225 expr_res = compile_expr0(&s, cctx);
3226 s = skipwhite(s);
3227 trailing_error = *s != NUL;
3228
Bram Moolenaarff652882021-05-16 15:24:49 +02003229 if (expr_res == FAIL || trailing_error
3230 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003231 {
3232 if (trailing_error)
3233 semsg(_(e_trailing_arg), s);
3234 clear_instr_ga(&cctx->ctx_instr);
3235 cctx->ctx_instr = save_ga;
3236 return FAIL;
3237 }
3238
3239 // Move the generated instructions into the ISN_INSTR instruction, then
3240 // restore the list of instructions.
3241 instr_count = cctx->ctx_instr.ga_len;
3242 instr = cctx->ctx_instr.ga_data;
3243 instr[instr_count].isn_type = ISN_FINISH;
3244
3245 cctx->ctx_instr = save_ga;
3246 vim_free(isn->isn_arg.string);
3247 isn->isn_type = ISN_INSTR;
3248 isn->isn_arg.instr = instr;
3249 return OK;
3250}
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252/*
3253 * Compile the argument expressions.
3254 * "arg" points to just after the "(" and is advanced to after the ")"
3255 */
3256 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003257compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003259 char_u *p = *arg;
3260 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003261 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003262 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263
Bram Moolenaare6085c52020-04-12 20:19:16 +02003264 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003266 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3267 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003268 if (*p == ')')
3269 {
3270 *arg = p + 1;
3271 return OK;
3272 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003273 if (must_end)
3274 {
3275 semsg(_(e_missing_comma_before_argument_str), p);
3276 return FAIL;
3277 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003278
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003279 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003280 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 return FAIL;
3282 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003283
Bram Moolenaarff652882021-05-16 15:24:49 +02003284 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003285 && cctx->ctx_instr.ga_len == instr_count + 1)
3286 {
3287 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3288
3289 // {skip} argument of searchpair() can be compiled if not empty
3290 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3291 compile_string(isn, cctx);
3292 }
3293
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003294 if (*p != ',' && *skipwhite(p) == ',')
3295 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003296 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003297 p = skipwhite(p);
3298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003300 {
3301 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003302 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003303 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003304 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003305 else
3306 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003307 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003308 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003310failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003311 emsg(_(e_missing_close));
3312 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313}
3314
3315/*
3316 * Compile a function call: name(arg1, arg2)
3317 * "arg" points to "name", "arg + varlen" to the "(".
3318 * "argcount_init" is 1 for "value->method()"
3319 * Instructions:
3320 * EVAL arg1
3321 * EVAL arg2
3322 * BCALL / DCALL / UCALL
3323 */
3324 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003325compile_call(
3326 char_u **arg,
3327 size_t varlen,
3328 cctx_T *cctx,
3329 ppconst_T *ppconst,
3330 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331{
3332 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003333 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 int argcount = argcount_init;
3335 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003336 char_u fname_buf[FLEN_FIXED + 1];
3337 char_u *tofree = NULL;
3338 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003339 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003340 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003341 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003342 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343
Bram Moolenaara5565e42020-05-09 15:44:01 +02003344 // we can evaluate "has('name')" at compile time
3345 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3346 {
3347 char_u *s = skipwhite(*arg + varlen + 1);
3348 typval_T argvars[2];
3349
3350 argvars[0].v_type = VAR_UNKNOWN;
3351 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003352 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003353 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003354 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003355 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003356 if (*s == ')' && argvars[0].v_type == VAR_STRING
3357 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003358 {
3359 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3360
3361 *arg = s + 1;
3362 argvars[1].v_type = VAR_UNKNOWN;
3363 tv->v_type = VAR_NUMBER;
3364 tv->vval.v_number = 0;
3365 f_has(argvars, tv);
3366 clear_tv(&argvars[0]);
3367 ++ppconst->pp_used;
3368 return OK;
3369 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003370 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003371 }
3372
3373 if (generate_ppconst(cctx, ppconst) == FAIL)
3374 return FAIL;
3375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (varlen >= sizeof(namebuf))
3377 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003378 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 return FAIL;
3380 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003381 vim_strncpy(namebuf, *arg, varlen);
3382 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003384 // We handle the "skip" argument of searchpair() and searchpairpos()
3385 // differently.
3386 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3387 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3388 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3389 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003392 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003393 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394
Bram Moolenaar03290b82020-12-19 16:30:44 +01003395 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003396 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
3398 int idx;
3399
3400 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003401 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003403 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003404 if (STRCMP(name, "flatten") == 0)
3405 {
3406 emsg(_(e_cannot_use_flatten_in_vim9_script));
3407 goto theend;
3408 }
3409
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003410 if (STRCMP(name, "add") == 0 && argcount == 2)
3411 {
3412 garray_T *stack = &cctx->ctx_type_stack;
3413 type_T *type = ((type_T **)stack->ga_data)[
3414 stack->ga_len - 2];
3415
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003416 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003417 if (type->tt_type == VAR_LIST)
3418 {
3419 // inline "add(list, item)" so that the type can be checked
3420 res = generate_LISTAPPEND(cctx);
3421 idx = -1;
3422 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003423 else if (type->tt_type == VAR_BLOB)
3424 {
3425 // inline "add(blob, nr)" so that the type can be checked
3426 res = generate_BLOBAPPEND(cctx);
3427 idx = -1;
3428 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003429 }
3430
3431 if (idx >= 0)
3432 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3433 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003434 else
3435 semsg(_(e_unknownfunc), namebuf);
3436 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003439 // An argument or local variable can be a function reference, this
3440 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003441 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003442 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003443 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003444 // If we can find the function by name generate the right call.
3445 // Skip global functions here, a local funcref takes precedence.
3446 ufunc = find_func(name, FALSE, cctx);
3447 if (ufunc != NULL && !func_is_global(ufunc))
3448 {
3449 res = generate_CALL(cctx, ufunc, argcount);
3450 goto theend;
3451 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
3454 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003455 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003456 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003458 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003459 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003460 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003462 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003463
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003464 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003465 goto theend;
3466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
Bram Moolenaar0f769812020-09-12 18:32:34 +02003468 // If we can find a global function by name generate the right call.
3469 if (ufunc != NULL)
3470 {
3471 res = generate_CALL(cctx, ufunc, argcount);
3472 goto theend;
3473 }
3474
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003475 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003476 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003477 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003478 res = generate_UCALL(cctx, name, argcount);
3479 else
3480 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003481
3482theend:
3483 vim_free(tofree);
3484 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485}
3486
3487// like NAMESPACE_CHAR but with 'a' and 'l'.
3488#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3489
3490/*
3491 * Find the end of a variable or function name. Unlike find_name_end() this
3492 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003493 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 * Return a pointer to just after the name. Equal to "arg" if there is no
3495 * valid name.
3496 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003497 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003498to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499{
3500 char_u *p;
3501
3502 // Quick check for valid starting character.
3503 if (!eval_isnamec1(*arg))
3504 return arg;
3505
3506 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3507 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3508 // and can be used in slice "[n:]".
3509 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003510 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3512 break;
3513 return p;
3514}
3515
3516/*
3517 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003518 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 */
3520 char_u *
3521to_name_const_end(char_u *arg)
3522{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003523 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 typval_T rettv;
3525
3526 if (p == arg && *arg == '[')
3527 {
3528
3529 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003530 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 p = arg;
3532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 return p;
3534}
3535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536/*
3537 * parse a list: [expr, expr]
3538 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003539 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 */
3541 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003542compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543{
3544 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003545 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003547 int is_const;
3548 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003550 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003552 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003553 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003554 semsg(_(e_list_end), *arg);
3555 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003556 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003557 if (*p == ',')
3558 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003559 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003560 return FAIL;
3561 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 if (*p == ']')
3563 {
3564 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003565 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003566 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003567 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003568 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003569 if (!is_const)
3570 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 ++count;
3572 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003573 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003575 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3576 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003577 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003578 return FAIL;
3579 }
3580 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003581 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 p = skipwhite(p);
3583 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003584 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003586 ppconst->pp_is_const = is_all_const;
3587 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588}
3589
3590/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003591 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003592 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003593 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 */
3595 static int
3596compile_lambda(char_u **arg, cctx_T *cctx)
3597{
Bram Moolenaare462f522020-12-27 14:43:30 +01003598 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 typval_T rettv;
3600 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003601 evalarg_T evalarg;
3602
3603 CLEAR_FIELD(evalarg);
3604 evalarg.eval_flags = EVAL_EVALUATE;
3605 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606
3607 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003608 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3609 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003610 {
3611 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003612 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003613 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003614
Bram Moolenaar65c44152020-12-24 15:14:01 +01003615 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003617 ++ufunc->uf_refcount;
3618 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaara9931532021-06-12 15:58:16 +02003620 // Compile it here to get the return type. The return type is optional,
3621 // when it's missing use t_unknown. This is recognized in
3622 // compile_return().
3623 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3624 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003625 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003627 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3628 // points into it. Point to the original line to avoid a dangling pointer.
3629 if (evalarg.eval_tofree_cmdline != NULL)
3630 {
3631 size_t off = *arg - evalarg.eval_tofree_cmdline;
3632
3633 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3634 + off;
3635 }
3636
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003637 clear_evalarg(&evalarg, NULL);
3638
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003639 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003640 {
3641 // The return type will now be known.
3642 set_function_type(ufunc);
3643
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003644 // The function reference count will be 1. When the ISN_FUNCREF
3645 // instruction is deleted the reference count is decremented and the
3646 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003647 return generate_FUNCREF(cctx, ufunc);
3648 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003649
3650 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 return FAIL;
3652}
3653
3654/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003655 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003657 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 */
3659 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003660compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661{
3662 garray_T *instr = &cctx->ctx_instr;
3663 int count = 0;
3664 dict_T *d = dict_alloc();
3665 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003666 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003667 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003668 int is_const;
3669 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670
3671 if (d == NULL)
3672 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003673 if (generate_ppconst(cctx, ppconst) == FAIL)
3674 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003675 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003677 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678
Bram Moolenaar23c55272020-06-21 16:58:13 +02003679 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003680 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003681 *arg = NULL;
3682 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003683 }
3684
3685 if (**arg == '}')
3686 break;
3687
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003688 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003690 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003692 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003693 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003694 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003697 if (isn->isn_type == ISN_PUSHNR)
3698 {
3699 char buf[NUMBUFLEN];
3700
3701 // Convert to string at compile time.
3702 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3703 isn->isn_type = ISN_PUSHS;
3704 isn->isn_arg.string = vim_strsave((char_u *)buf);
3705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 if (isn->isn_type == ISN_PUSHS)
3707 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003708 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003709 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003710 *arg = skipwhite(*arg);
3711 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003712 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003713 emsg(_(e_missing_matching_bracket_after_dict_key));
3714 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003715 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003716 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003718 else
3719 {
3720 // {"name": value},
3721 // {'name': value},
3722 // {name: value} use "name" as a literal key
3723 key = get_literal_key(arg);
3724 if (key == NULL)
3725 return FAIL;
3726 if (generate_PUSHS(cctx, key) == FAIL)
3727 return FAIL;
3728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729
3730 // Check for duplicate keys, if using string keys.
3731 if (key != NULL)
3732 {
3733 item = dict_find(d, key, -1);
3734 if (item != NULL)
3735 {
3736 semsg(_(e_duplicate_key), key);
3737 goto failret;
3738 }
3739 item = dictitem_alloc(key);
3740 if (item != NULL)
3741 {
3742 item->di_tv.v_type = VAR_UNKNOWN;
3743 item->di_tv.v_lock = 0;
3744 if (dict_add(d, item) == FAIL)
3745 dictitem_free(item);
3746 }
3747 }
3748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 if (**arg != ':')
3750 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003751 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003752 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003753 else
3754 semsg(_(e_missing_dict_colon), *arg);
3755 return FAIL;
3756 }
3757 whitep = *arg + 1;
3758 if (!IS_WHITE_OR_NUL(*whitep))
3759 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003760 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 return FAIL;
3762 }
3763
Bram Moolenaar23c55272020-06-21 16:58:13 +02003764 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003765 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003766 *arg = NULL;
3767 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003768 }
3769
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003770 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003772 if (!is_const)
3773 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 ++count;
3775
Bram Moolenaar2c330432020-04-13 14:41:35 +02003776 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003777 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003778 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003779 *arg = NULL;
3780 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 if (**arg == '}')
3783 break;
3784 if (**arg != ',')
3785 {
3786 semsg(_(e_missing_dict_comma), *arg);
3787 goto failret;
3788 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003789 if (IS_WHITE_OR_NUL(*whitep))
3790 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003791 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003792 return FAIL;
3793 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003794 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003795 if (!IS_WHITE_OR_NUL(*whitep))
3796 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003797 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003798 return FAIL;
3799 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003800 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 }
3802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 *arg = *arg + 1;
3804
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003805 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003806 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003807 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003808 *arg += STRLEN(*arg);
3809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003811 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 return generate_NEWDICT(cctx, count);
3813
3814failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003815 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003816 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003817 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003818 *arg = (char_u *)"";
3819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 dict_unref(d);
3821 return FAIL;
3822}
3823
3824/*
3825 * Compile "&option".
3826 */
3827 static int
3828compile_get_option(char_u **arg, cctx_T *cctx)
3829{
3830 typval_T rettv;
3831 char_u *start = *arg;
3832 int ret;
3833
3834 // parse the option and get the current value to get the type.
3835 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003836 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 if (ret == OK)
3838 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003839 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003840 char_u *name = vim_strnsave(start, *arg - start);
3841 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3842 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
3844 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3845 vim_free(name);
3846 }
3847 clear_tv(&rettv);
3848
3849 return ret;
3850}
3851
3852/*
3853 * Compile "$VAR".
3854 */
3855 static int
3856compile_get_env(char_u **arg, cctx_T *cctx)
3857{
3858 char_u *start = *arg;
3859 int len;
3860 int ret;
3861 char_u *name;
3862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 ++*arg;
3864 len = get_env_len(arg);
3865 if (len == 0)
3866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003867 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 return FAIL;
3869 }
3870
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003871 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 name = vim_strnsave(start, len + 1);
3873 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3874 vim_free(name);
3875 return ret;
3876}
3877
3878/*
3879 * Compile "@r".
3880 */
3881 static int
3882compile_get_register(char_u **arg, cctx_T *cctx)
3883{
3884 int ret;
3885
3886 ++*arg;
3887 if (**arg == NUL)
3888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003889 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return FAIL;
3891 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003892 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 {
3894 emsg_invreg(**arg);
3895 return FAIL;
3896 }
3897 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3898 ++*arg;
3899 return ret;
3900}
3901
3902/*
3903 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003904 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 */
3906 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003907apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003909 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910
3911 // this works from end to start
3912 while (p > start)
3913 {
3914 --p;
3915 if (*p == '-' || *p == '+')
3916 {
3917 // only '-' has an effect, for '+' we only check the type
3918#ifdef FEAT_FLOAT
3919 if (rettv->v_type == VAR_FLOAT)
3920 {
3921 if (*p == '-')
3922 rettv->vval.v_float = -rettv->vval.v_float;
3923 }
3924 else
3925#endif
3926 {
3927 varnumber_T val;
3928 int error = FALSE;
3929
3930 // tv_get_number_chk() accepts a string, but we don't want that
3931 // here
3932 if (check_not_string(rettv) == FAIL)
3933 return FAIL;
3934 val = tv_get_number_chk(rettv, &error);
3935 clear_tv(rettv);
3936 if (error)
3937 return FAIL;
3938 if (*p == '-')
3939 val = -val;
3940 rettv->v_type = VAR_NUMBER;
3941 rettv->vval.v_number = val;
3942 }
3943 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003944 else if (numeric_only)
3945 {
3946 ++p;
3947 break;
3948 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003949 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 {
3951 int v = tv2bool(rettv);
3952
3953 // '!' is permissive in the type.
3954 clear_tv(rettv);
3955 rettv->v_type = VAR_BOOL;
3956 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3957 }
3958 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003959 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 return OK;
3961}
3962
3963/*
3964 * Recognize v: variables that are constants and set "rettv".
3965 */
3966 static void
3967get_vim_constant(char_u **arg, typval_T *rettv)
3968{
3969 if (STRNCMP(*arg, "v:true", 6) == 0)
3970 {
3971 rettv->v_type = VAR_BOOL;
3972 rettv->vval.v_number = VVAL_TRUE;
3973 *arg += 6;
3974 }
3975 else if (STRNCMP(*arg, "v:false", 7) == 0)
3976 {
3977 rettv->v_type = VAR_BOOL;
3978 rettv->vval.v_number = VVAL_FALSE;
3979 *arg += 7;
3980 }
3981 else if (STRNCMP(*arg, "v:null", 6) == 0)
3982 {
3983 rettv->v_type = VAR_SPECIAL;
3984 rettv->vval.v_number = VVAL_NULL;
3985 *arg += 6;
3986 }
3987 else if (STRNCMP(*arg, "v:none", 6) == 0)
3988 {
3989 rettv->v_type = VAR_SPECIAL;
3990 rettv->vval.v_number = VVAL_NONE;
3991 *arg += 6;
3992 }
3993}
3994
Bram Moolenaar657137c2021-01-09 15:45:23 +01003995 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003996get_compare_type(char_u *p, int *len, int *type_is)
3997{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003998 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003999 int i;
4000
4001 switch (p[0])
4002 {
4003 case '=': if (p[1] == '=')
4004 type = EXPR_EQUAL;
4005 else if (p[1] == '~')
4006 type = EXPR_MATCH;
4007 break;
4008 case '!': if (p[1] == '=')
4009 type = EXPR_NEQUAL;
4010 else if (p[1] == '~')
4011 type = EXPR_NOMATCH;
4012 break;
4013 case '>': if (p[1] != '=')
4014 {
4015 type = EXPR_GREATER;
4016 *len = 1;
4017 }
4018 else
4019 type = EXPR_GEQUAL;
4020 break;
4021 case '<': if (p[1] != '=')
4022 {
4023 type = EXPR_SMALLER;
4024 *len = 1;
4025 }
4026 else
4027 type = EXPR_SEQUAL;
4028 break;
4029 case 'i': if (p[1] == 's')
4030 {
4031 // "is" and "isnot"; but not a prefix of a name
4032 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4033 *len = 5;
4034 i = p[*len];
4035 if (!isalnum(i) && i != '_')
4036 {
4037 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4038 *type_is = TRUE;
4039 }
4040 }
4041 break;
4042 }
4043 return type;
4044}
4045
4046/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004047 * Skip over an expression, ignoring most errors.
4048 */
4049 static void
4050skip_expr_cctx(char_u **arg, cctx_T *cctx)
4051{
4052 evalarg_T evalarg;
4053
4054 CLEAR_FIELD(evalarg);
4055 evalarg.eval_cctx = cctx;
4056 skip_expr(arg, &evalarg);
4057}
4058
4059/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 */
4063 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004064compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004066 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067
4068 // this works from end to start
4069 while (p > start)
4070 {
4071 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004072 while (VIM_ISWHITE(*p))
4073 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 if (*p == '-' || *p == '+')
4075 {
4076 int negate = *p == '-';
4077 isn_T *isn;
4078
4079 // TODO: check type
4080 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4081 {
4082 --p;
4083 if (*p == '-')
4084 negate = !negate;
4085 }
4086 // only '-' has an effect, for '+' we only check the type
4087 if (negate)
4088 isn = generate_instr(cctx, ISN_NEGATENR);
4089 else
4090 isn = generate_instr(cctx, ISN_CHECKNR);
4091 if (isn == NULL)
4092 return FAIL;
4093 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004094 else if (numeric_only)
4095 {
4096 ++p;
4097 break;
4098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 else
4100 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004101 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004103 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004105 if (p[-1] == '!')
4106 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004109 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 return FAIL;
4111 }
4112 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004113 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 return OK;
4115}
4116
4117/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004118 * Compile "(expression)": recursive!
4119 * Return FAIL/OK.
4120 */
4121 static int
4122compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4123{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004124 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004125 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004126
Bram Moolenaar24156692021-01-14 20:35:49 +01004127 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4128 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004129 if (ppconst->pp_used <= PPSIZE - 10)
4130 {
4131 ret = compile_expr1(arg, cctx, ppconst);
4132 }
4133 else
4134 {
4135 // Not enough space in ppconst, flush constants.
4136 if (generate_ppconst(cctx, ppconst) == FAIL)
4137 return FAIL;
4138 ret = compile_expr0(arg, cctx);
4139 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004140 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4141 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004142 if (**arg == ')')
4143 ++*arg;
4144 else if (ret == OK)
4145 {
4146 emsg(_(e_missing_close));
4147 ret = FAIL;
4148 }
4149 return ret;
4150}
4151
4152/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004154 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 */
4156 static int
4157compile_subscript(
4158 char_u **arg,
4159 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004160 char_u *start_leader,
4161 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004162 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004164 char_u *name_start = *end_leader;
4165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 for (;;)
4167 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004168 char_u *p = skipwhite(*arg);
4169
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004170 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004171 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004172 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004173
4174 // If a following line starts with "->{" or "->X" advance to that
4175 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004176 // Also if a following line starts with ".x".
4177 if (next != NULL &&
4178 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004179 && (next[2] == '{'
4180 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004181 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004182 {
4183 next = next_line_from_context(cctx, TRUE);
4184 if (next == NULL)
4185 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004186 *arg = next;
4187 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004188 }
4189 }
4190
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004191 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004192 // not a function call.
4193 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004195 garray_T *stack = &cctx->ctx_type_stack;
4196 type_T *type;
4197 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004200 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004201 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004204 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4205
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004206 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004207 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004209 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 return FAIL;
4211 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004212 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004214 char_u *pstart = p;
4215
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004218 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 // something->method()
4221 // Apply the '!', '-' and '+' first:
4222 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004223 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004226 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004227 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004228 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004229 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004230 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004231 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004232 garray_T *stack = &cctx->ctx_type_stack;
4233 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004234 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004235 int expr_isn_start = cctx->ctx_instr.ga_len;
4236 int expr_isn_end;
4237 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004238
4239 // Funcref call: list->(Refs[2])(arg)
4240 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004241 //
4242 // Fist compile the function expression.
4243 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004244 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004245
4246 // Remember the next instruction index, where the instructions
4247 // for arguments are being written.
4248 expr_isn_end = cctx->ctx_instr.ga_len;
4249
4250 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004251 if (**arg != '(')
4252 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004253 if (*skipwhite(*arg) == '(')
4254 emsg(_(e_nowhitespace));
4255 else
4256 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004257 return FAIL;
4258 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004259 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004260 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004261 return FAIL;
4262
Bram Moolenaar2927c072021-04-05 19:41:21 +02004263 // Move the instructions for the arguments to before the
4264 // instructions of the expression and move the type of the
4265 // expression after the argument types. This is what ISN_PCALL
4266 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004267 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004268 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4269 if (arg_isn_count > 0)
4270 {
4271 int expr_isn_count = expr_isn_end - expr_isn_start;
4272 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4273
4274 if (isn == NULL)
4275 return FAIL;
4276 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4277 + expr_isn_start,
4278 sizeof(isn_T) * expr_isn_count);
4279 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4280 + expr_isn_start,
4281 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4282 sizeof(isn_T) * arg_isn_count);
4283 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4284 + expr_isn_start + arg_isn_count,
4285 isn, sizeof(isn_T) * expr_isn_count);
4286 vim_free(isn);
4287
4288 type = ((type_T **)stack->ga_data)[type_idx_start];
4289 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4290 ((type_T **)stack->ga_data) + type_idx_start + 1,
4291 sizeof(type_T *)
4292 * (stack->ga_len - type_idx_start - 1));
4293 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4294 }
4295
Bram Moolenaar7e368202020-12-25 21:56:57 +01004296 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4297 if (generate_PCALL(cctx, argcount,
4298 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 return FAIL;
4300 }
4301 else
4302 {
4303 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004304 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004305 if (!eval_isnamec1(*p))
4306 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004307 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004308 return FAIL;
4309 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004310 if (ASCII_ISALPHA(*p) && p[1] == ':')
4311 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004312 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 ;
4314 if (*p != '(')
4315 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004316 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 return FAIL;
4318 }
4319 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 return FAIL;
4322 }
4323 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004324 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004326 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004327
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004328 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004329 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004330 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004331 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004332 // TODO: more arguments
4333 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004334 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004335 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004336 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004337
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004338 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004339 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004340 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004341 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004342 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004343 // missing first index is equal to zero
4344 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004345 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004346 else
4347 {
4348 if (compile_expr0(arg, cctx) == FAIL)
4349 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004350 if (**arg == ':')
4351 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004352 semsg(_(e_white_space_required_before_and_after_str_at_str),
4353 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004354 return FAIL;
4355 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004356 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004357 return FAIL;
4358 *arg = skipwhite(*arg);
4359 }
4360 if (**arg == ':')
4361 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004362 is_slice = TRUE;
4363 ++*arg;
4364 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4365 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004366 semsg(_(e_white_space_required_before_and_after_str_at_str),
4367 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004368 return FAIL;
4369 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004370 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004371 return FAIL;
4372 if (**arg == ']')
4373 // missing second index is equal to end of string
4374 generate_PUSHNR(cctx, -1);
4375 else
4376 {
4377 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004378 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004379 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004380 return FAIL;
4381 *arg = skipwhite(*arg);
4382 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
4385 if (**arg != ']')
4386 {
4387 emsg(_(e_missbrac));
4388 return FAIL;
4389 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004390 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
Bram Moolenaare42939a2021-04-05 17:11:17 +02004392 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004393 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004395 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004397 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004398 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004399 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004400 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004401
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004402 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004403 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004404 {
4405 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004406 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004407 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004408 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004409 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 while (eval_isnamec(*p))
4411 MB_PTR_ADV(p);
4412 if (p == *arg)
4413 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004414 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 return FAIL;
4416 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004417 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 return FAIL;
4419 *arg = p;
4420 }
4421 else
4422 break;
4423 }
4424
4425 // TODO - see handle_subscript():
4426 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4427 // Don't do this when "Func" is already a partial that was bound
4428 // explicitly (pt_auto is FALSE).
4429
4430 return OK;
4431}
4432
4433/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004434 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4435 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004437 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004438 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004439 *
4440 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 */
4442
4443/*
4444 * number number constant
4445 * 0zFFFFFFFF Blob constant
4446 * "string" string constant
4447 * 'string' literal string constant
4448 * &option-name option value
4449 * @r register contents
4450 * identifier variable value
4451 * function() function call
4452 * $VAR environment variable
4453 * (expression) nested expression
4454 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004455 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 *
4457 * Also handle:
4458 * ! in front logical NOT
4459 * - in front unary minus
4460 * + in front unary plus (ignored)
4461 * trailing (arg) funcref/partial call
4462 * trailing [] subscript in String or List
4463 * trailing .name entry in Dictionary
4464 * trailing ->name() method call
4465 */
4466 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004467compile_expr7(
4468 char_u **arg,
4469 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004470 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 char_u *start_leader, *end_leader;
4473 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004474 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004475 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004477 ppconst->pp_is_const = FALSE;
4478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 /*
4480 * Skip '!', '-' and '+' characters. They are handled later.
4481 */
4482 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004483 if (eval_leader(arg, TRUE) == FAIL)
4484 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 end_leader = *arg;
4486
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004487 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 switch (**arg)
4489 {
4490 /*
4491 * Number constant.
4492 */
4493 case '0': // also for blob starting with 0z
4494 case '1':
4495 case '2':
4496 case '3':
4497 case '4':
4498 case '5':
4499 case '6':
4500 case '7':
4501 case '8':
4502 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004503 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004505 // Apply "-" and "+" just before the number now, right to
4506 // left. Matters especially when "->" follows. Stops at
4507 // '!'.
4508 if (apply_leader(rettv, TRUE,
4509 start_leader, &end_leader) == FAIL)
4510 {
4511 clear_tv(rettv);
4512 return FAIL;
4513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 break;
4515
4516 /*
4517 * String constant: "string".
4518 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004519 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 return FAIL;
4521 break;
4522
4523 /*
4524 * Literal string constant: 'str''ing'.
4525 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004526 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return FAIL;
4528 break;
4529
4530 /*
4531 * Constant Vim variable.
4532 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004533 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 ret = NOTDONE;
4535 break;
4536
4537 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004538 * "true" constant
4539 */
4540 case 't': if (STRNCMP(*arg, "true", 4) == 0
4541 && !eval_isnamec((*arg)[4]))
4542 {
4543 *arg += 4;
4544 rettv->v_type = VAR_BOOL;
4545 rettv->vval.v_number = VVAL_TRUE;
4546 }
4547 else
4548 ret = NOTDONE;
4549 break;
4550
4551 /*
4552 * "false" constant
4553 */
4554 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4555 && !eval_isnamec((*arg)[5]))
4556 {
4557 *arg += 5;
4558 rettv->v_type = VAR_BOOL;
4559 rettv->vval.v_number = VVAL_FALSE;
4560 }
4561 else
4562 ret = NOTDONE;
4563 break;
4564
4565 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004566 * "null" constant
4567 */
4568 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004569 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004570 {
4571 *arg += 4;
4572 rettv->v_type = VAR_SPECIAL;
4573 rettv->vval.v_number = VVAL_NULL;
4574 }
4575 else
4576 ret = NOTDONE;
4577 break;
4578
4579 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 * List: [expr, expr]
4581 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004582 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4583 return FAIL;
4584 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 break;
4586
4587 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 * Dictionary: {'key': val, 'key': val}
4589 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004590 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4591 return FAIL;
4592 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 break;
4594
4595 /*
4596 * Option value: &name
4597 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004598 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4599 return FAIL;
4600 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 break;
4602
4603 /*
4604 * Environment variable: $VAR.
4605 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004606 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4607 return FAIL;
4608 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 break;
4610
4611 /*
4612 * Register contents: @r.
4613 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004614 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4615 return FAIL;
4616 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 break;
4618 /*
4619 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004620 * lambda: (arg, arg) => expr
4621 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004623 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4624 ret = compile_lambda(arg, cctx);
4625 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004626 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 break;
4628
4629 default: ret = NOTDONE;
4630 break;
4631 }
4632 if (ret == FAIL)
4633 return FAIL;
4634
Bram Moolenaar1c747212020-05-09 18:28:34 +02004635 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004637 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638 clear_tv(rettv);
4639 else
4640 // A constant expression can possibly be handled compile time,
4641 // return the value instead of generating code.
4642 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 }
4644 else if (ret == NOTDONE)
4645 {
4646 char_u *p;
4647 int r;
4648
4649 if (!eval_isnamec1(**arg))
4650 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004651 if (!vim9_bad_comment(*arg))
4652 {
4653 if (ends_excmd(*skipwhite(*arg)))
4654 semsg(_(e_empty_expression_str), *arg);
4655 else
4656 semsg(_(e_name_expected_str), *arg);
4657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 return FAIL;
4659 }
4660
4661 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004662 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004663 if (p - *arg == (size_t)1 && **arg == '_')
4664 {
4665 emsg(_(e_cannot_use_underscore_here));
4666 return FAIL;
4667 }
4668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 {
4671 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674 {
4675 if (generate_ppconst(cctx, ppconst) == FAIL)
4676 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004677 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004678 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 if (r == FAIL)
4680 return FAIL;
4681 }
4682
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004683 // Handle following "[]", ".member", etc.
4684 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004685 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 ppconst) == FAIL)
4687 return FAIL;
4688 if (ppconst->pp_used > 0)
4689 {
4690 // apply the '!', '-' and '+' before the constant
4691 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004692 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 return FAIL;
4694 return OK;
4695 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004696 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004698 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699}
4700
4701/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004702 * Give the "white on both sides" error, taking the operator from "p[len]".
4703 */
4704 void
4705error_white_both(char_u *op, int len)
4706{
4707 char_u buf[10];
4708
4709 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004710 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004711}
4712
4713/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004714 * <type>expr7: runtime type check / conversion
4715 */
4716 static int
4717compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4718{
4719 type_T *want_type = NULL;
4720
4721 // Recognize <type>
4722 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4723 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004724 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004725 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4726 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004727 return FAIL;
4728
4729 if (**arg != '>')
4730 {
4731 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004732 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004733 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004734 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004735 return FAIL;
4736 }
4737 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004738 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004739 return FAIL;
4740 }
4741
4742 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4743 return FAIL;
4744
4745 if (want_type != NULL)
4746 {
4747 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004748 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004749 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004750
Bram Moolenaard1103582020-08-14 22:44:25 +02004751 generate_ppconst(cctx, ppconst);
4752 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004753 where.wt_index = 0;
4754 where.wt_variable = FALSE;
4755 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004756 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004757 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004758 return FAIL;
4759 }
4760 }
4761
4762 return OK;
4763}
4764
4765/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 * * number multiplication
4767 * / number division
4768 * % number modulo
4769 */
4770 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772{
4773 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004774 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004775 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004777 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004778 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 return FAIL;
4780
4781 /*
4782 * Repeat computing, until no "*", "/" or "%" is following.
4783 */
4784 for (;;)
4785 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004786 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 if (*op != '*' && *op != '/' && *op != '%')
4788 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004789 if (next != NULL)
4790 {
4791 *arg = next_line_from_context(cctx, TRUE);
4792 op = skipwhite(*arg);
4793 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004794
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004795 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004797 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004798 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004800 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004801 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004803 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004804 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004806
4807 if (ppconst->pp_used == ppconst_used + 2
4808 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4809 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004810 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004811 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4812 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4813 varnumber_T res = 0;
4814 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004816 // both are numbers: compute the result
4817 switch (*op)
4818 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004819 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004820 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004821 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004822 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004823 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004824 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004825 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004826 break;
4827 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004828 if (failed)
4829 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004830 tv1->vval.v_number = res;
4831 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004832 }
4833 else
4834 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004835 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004836 generate_two_op(cctx, op);
4837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 }
4839
4840 return OK;
4841}
4842
4843/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004844 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 * - number subtraction
4846 * .. string concatenation
4847 */
4848 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849compile_expr5(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 Moolenaar8a7d6542020-01-26 15:56:19 +01004853 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855
4856 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004857 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 return FAIL;
4859
4860 /*
4861 * Repeat computing, until no "+", "-" or ".." is following.
4862 */
4863 for (;;)
4864 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004865 op = may_peek_next_line(cctx, *arg, &next);
4866 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004868 if (op[0] == op[1] && *op != '.' && next)
4869 // Finding "++" or "--" on the next line is a separate command.
4870 // But ".." is concatenation.
4871 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004873 if (next != NULL)
4874 {
4875 *arg = next_line_from_context(cctx, TRUE);
4876 op = skipwhite(*arg);
4877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004879 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004881 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004882 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 }
4884
Bram Moolenaare0de1712020-12-02 17:36:54 +01004885 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004886 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004888 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004889 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 return FAIL;
4891
Bram Moolenaara5565e42020-05-09 15:44:01 +02004892 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004893 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4895 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4896 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4897 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4900 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004901
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004902 // concat/subtract/add constant numbers
4903 if (*op == '+')
4904 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4905 else if (*op == '-')
4906 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4907 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004908 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004909 // concatenate constant strings
4910 char_u *s1 = tv1->vval.v_string;
4911 char_u *s2 = tv2->vval.v_string;
4912 size_t len1 = STRLEN(s1);
4913
4914 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4915 if (tv1->vval.v_string == NULL)
4916 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004918 return FAIL;
4919 }
4920 mch_memmove(tv1->vval.v_string, s1, len1);
4921 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004922 vim_free(s1);
4923 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004924 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004925 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 }
4927 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004928 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004930 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004931 if (*op == '.')
4932 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004933 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4934 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004935 return FAIL;
4936 generate_instr_drop(cctx, ISN_CONCAT, 1);
4937 }
4938 else
4939 generate_two_op(cctx, op);
4940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 }
4942
4943 return OK;
4944}
4945
4946/*
4947 * expr5a == expr5b
4948 * expr5a =~ expr5b
4949 * expr5a != expr5b
4950 * expr5a !~ expr5b
4951 * expr5a > expr5b
4952 * expr5a >= expr5b
4953 * expr5a < expr5b
4954 * expr5a <= expr5b
4955 * expr5a is expr5b
4956 * expr5a isnot expr5b
4957 *
4958 * Produces instructions:
4959 * EVAL expr5a Push result of "expr5a"
4960 * EVAL expr5b Push result of "expr5b"
4961 * COMPARE one of the compare instructions
4962 */
4963 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004964compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004966 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004968 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972
4973 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 return FAIL;
4976
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004977 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004978 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979
4980 /*
4981 * If there is a comparative operator, use it.
4982 */
4983 if (type != EXPR_UNKNOWN)
4984 {
4985 int ic = FALSE; // Default: do not ignore case
4986
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004987 if (next != NULL)
4988 {
4989 *arg = next_line_from_context(cctx, TRUE);
4990 p = skipwhite(*arg);
4991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 if (type_is && (p[len] == '?' || p[len] == '#'))
4993 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004994 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 return FAIL;
4996 }
4997 // extra question mark appended: ignore case
4998 if (p[len] == '?')
4999 {
5000 ic = TRUE;
5001 ++len;
5002 }
5003 // extra '#' appended: match case (ignored)
5004 else if (p[len] == '#')
5005 ++len;
5006 // nothing appended: match case
5007
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005008 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005010 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005011 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 }
5013
5014 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005015 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005016 return FAIL;
5017
Bram Moolenaara5565e42020-05-09 15:44:01 +02005018 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 return FAIL;
5020
Bram Moolenaara5565e42020-05-09 15:44:01 +02005021 if (ppconst->pp_used == ppconst_used + 2)
5022 {
5023 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5024 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5025 int ret;
5026
5027 // Both sides are a constant, compute the result now.
5028 // First check for a valid combination of types, this is more
5029 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005030 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 ret = FAIL;
5032 else
5033 {
5034 ret = typval_compare(tv1, tv2, type, ic);
5035 tv1->v_type = VAR_BOOL;
5036 tv1->vval.v_number = tv1->vval.v_number
5037 ? VVAL_TRUE : VVAL_FALSE;
5038 clear_tv(tv2);
5039 --ppconst->pp_used;
5040 }
5041 return ret;
5042 }
5043
5044 generate_ppconst(cctx, ppconst);
5045 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 }
5047
5048 return OK;
5049}
5050
Bram Moolenaar7f141552020-05-09 17:35:53 +02005051static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053/*
5054 * Compile || or &&.
5055 */
5056 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005057compile_and_or(
5058 char_u **arg,
5059 cctx_T *cctx,
5060 char *op,
5061 ppconst_T *ppconst,
5062 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005064 char_u *next;
5065 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 int opchar = *op;
5067
5068 if (p[0] == opchar && p[1] == opchar)
5069 {
5070 garray_T *instr = &cctx->ctx_instr;
5071 garray_T end_ga;
5072
5073 /*
5074 * Repeat until there is no following "||" or "&&"
5075 */
5076 ga_init2(&end_ga, sizeof(int), 10);
5077 while (p[0] == opchar && p[1] == opchar)
5078 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005079 long start_lnum = SOURCING_LNUM;
5080 int start_ctx_lnum = cctx->ctx_lnum;
5081 int save_lnum;
5082
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005083 if (next != NULL)
5084 {
5085 *arg = next_line_from_context(cctx, TRUE);
5086 p = skipwhite(*arg);
5087 }
5088
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005089 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5090 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005091 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005092 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005093 return FAIL;
5094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005096 // TODO: use ppconst if the value is a constant and check
5097 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005098 generate_ppconst(cctx, ppconst);
5099
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005100 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005101 SOURCING_LNUM = start_lnum;
5102 save_lnum = cctx->ctx_lnum;
5103 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005104 if (bool_on_stack(cctx) == FAIL)
5105 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005106 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005107 ga_clear(&end_ga);
5108 return FAIL;
5109 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005110 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 if (ga_grow(&end_ga, 1) == FAIL)
5113 {
5114 ga_clear(&end_ga);
5115 return FAIL;
5116 }
5117 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5118 ++end_ga.ga_len;
5119 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005120 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121
5122 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005123 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005124 {
5125 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005126 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005127 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005128
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5130 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 {
5132 ga_clear(&end_ga);
5133 return FAIL;
5134 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005135
5136 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005140 // Every part must evaluate to a bool.
5141 if (bool_on_stack(cctx) == FAIL)
5142 {
5143 ga_clear(&end_ga);
5144 return FAIL;
5145 }
5146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 // Fill in the end label in all jumps.
5148 while (end_ga.ga_len > 0)
5149 {
5150 isn_T *isn;
5151
5152 --end_ga.ga_len;
5153 isn = ((isn_T *)instr->ga_data)
5154 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5155 isn->isn_arg.jump.jump_where = instr->ga_len;
5156 }
5157 ga_clear(&end_ga);
5158 }
5159
5160 return OK;
5161}
5162
5163/*
5164 * expr4a && expr4a && expr4a logical AND
5165 *
5166 * Produces instructions:
5167 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005168 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005169 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005171 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 * EVAL expr4c Push result of "expr4c"
5173 * end:
5174 */
5175 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005178 int ppconst_used = ppconst->pp_used;
5179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005181 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 return FAIL;
5183
5184 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005185 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186}
5187
5188/*
5189 * expr3a || expr3b || expr3c logical OR
5190 *
5191 * Produces instructions:
5192 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005193 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005194 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005196 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 * EVAL expr3c Push result of "expr3c"
5198 * end:
5199 */
5200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005203 int ppconst_used = ppconst->pp_used;
5204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005206 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 return FAIL;
5208
5209 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005210 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211}
5212
5213/*
5214 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005216 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 * JUMP_IF_FALSE alt jump if false
5218 * EVAL expr1a
5219 * JUMP_ALWAYS end
5220 * alt: EVAL expr1b
5221 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005222 *
5223 * Toplevel expression: expr2 ?? expr1
5224 * Produces instructions:
5225 * EVAL expr2 Push result of "expr2"
5226 * JUMP_AND_KEEP_IF_TRUE end jump if true
5227 * EVAL expr1
5228 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 */
5230 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005231compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232{
5233 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005234 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005235 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236
Bram Moolenaar3988f642020-08-27 22:43:03 +02005237 // Ignore all kinds of errors when not producing code.
5238 if (cctx->ctx_skip == SKIP_YES)
5239 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005240 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005241 return OK;
5242 }
5243
Bram Moolenaar61a89812020-05-07 16:58:17 +02005244 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005245 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 return FAIL;
5247
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005248 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 if (*p == '?')
5250 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005251 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 garray_T *instr = &cctx->ctx_instr;
5253 garray_T *stack = &cctx->ctx_type_stack;
5254 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005255 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005257 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005258 int has_const_expr = FALSE;
5259 int const_value = FALSE;
5260 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005262 if (next != NULL)
5263 {
5264 *arg = next_line_from_context(cctx, TRUE);
5265 p = skipwhite(*arg);
5266 }
5267
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005268 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005269 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005270 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005271 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005272 return FAIL;
5273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274
Bram Moolenaara5565e42020-05-09 15:44:01 +02005275 if (ppconst->pp_used == ppconst_used + 1)
5276 {
5277 // the condition is a constant, we know whether the ? or the :
5278 // expression is to be evaluated.
5279 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005280 if (op_falsy)
5281 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5282 else
5283 {
5284 int error = FALSE;
5285
5286 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5287 &error);
5288 if (error)
5289 return FAIL;
5290 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005291 cctx->ctx_skip = save_skip == SKIP_YES ||
5292 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5293
5294 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5295 // "left ?? right" and "left" is truthy: produce "left"
5296 generate_ppconst(cctx, ppconst);
5297 else
5298 {
5299 clear_tv(&ppconst->pp_tv[ppconst_used]);
5300 --ppconst->pp_used;
5301 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005302 }
5303 else
5304 {
5305 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005306 if (op_falsy)
5307 end_idx = instr->ga_len;
5308 generate_JUMP(cctx, op_falsy
5309 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5310 if (op_falsy)
5311 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313
5314 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005315 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005316 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005317 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005318 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319
Bram Moolenaara5565e42020-05-09 15:44:01 +02005320 if (!has_const_expr)
5321 {
5322 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005324 if (!op_falsy)
5325 {
5326 // remember the type and drop it
5327 --stack->ga_len;
5328 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005330 end_idx = instr->ga_len;
5331 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005332
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005333 // jump here from JUMP_IF_FALSE
5334 isn = ((isn_T *)instr->ga_data) + alt_idx;
5335 isn->isn_arg.jump.jump_where = instr->ga_len;
5336 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005339 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005341 // Check for the ":".
5342 p = may_peek_next_line(cctx, *arg, &next);
5343 if (*p != ':')
5344 {
5345 emsg(_(e_missing_colon));
5346 return FAIL;
5347 }
5348 if (next != NULL)
5349 {
5350 *arg = next_line_from_context(cctx, TRUE);
5351 p = skipwhite(*arg);
5352 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005353
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005354 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5355 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005356 semsg(_(e_white_space_required_before_and_after_str_at_str),
5357 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005358 return FAIL;
5359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005361 // evaluate the third expression
5362 if (has_const_expr)
5363 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005364 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005365 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005366 return FAIL;
5367 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5368 return FAIL;
5369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
Bram Moolenaara5565e42020-05-09 15:44:01 +02005371 if (!has_const_expr)
5372 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005373 type_T **typep;
5374
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376
Bram Moolenaara5565e42020-05-09 15:44:01 +02005377 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005378 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5379 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005380
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005381 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005382 isn = ((isn_T *)instr->ga_data) + end_idx;
5383 isn->isn_arg.jump.jump_where = instr->ga_len;
5384 }
5385
5386 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 }
5388 return OK;
5389}
5390
5391/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005392 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005393 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5394 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005395 */
5396 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005397compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005398{
5399 ppconst_T ppconst;
5400
5401 CLEAR_FIELD(ppconst);
5402 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5403 {
5404 clear_ppconst(&ppconst);
5405 return FAIL;
5406 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005407 if (is_const != NULL)
5408 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005409 if (generate_ppconst(cctx, &ppconst) == FAIL)
5410 return FAIL;
5411 return OK;
5412}
5413
5414/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005415 * Toplevel expression.
5416 */
5417 static int
5418compile_expr0(char_u **arg, cctx_T *cctx)
5419{
5420 return compile_expr0_ext(arg, cctx, NULL);
5421}
5422
5423/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005424 * Compile "return [expr]".
5425 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 */
5427 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005428compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429{
5430 char_u *p = arg;
5431 garray_T *stack = &cctx->ctx_type_stack;
5432 type_T *stack_type;
5433
5434 if (*p != NUL && *p != '|' && *p != '\n')
5435 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005436 if (legacy)
5437 {
5438 int save_flags = cmdmod.cmod_flags;
5439
5440 generate_LEGACY_EVAL(cctx, p);
5441 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5442 0, cctx, FALSE, FALSE) == FAIL)
5443 return NULL;
5444 cmdmod.cmod_flags |= CMOD_LEGACY;
5445 (void)skip_expr(&p, NULL);
5446 cmdmod.cmod_flags = save_flags;
5447 }
5448 else
5449 {
5450 // compile return argument into instructions
5451 if (compile_expr0(&p, cctx) == FAIL)
5452 return NULL;
5453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005455 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005456 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005457 // "check_return_type" with uf_ret_type set to &t_unknown is used
5458 // for an inline function without a specified return type. Set the
5459 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005460 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005461 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005462 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5463 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005464 || (!check_return_type
5465 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005466 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005467 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005468 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005469 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005470 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005471 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5472 && stack_type->tt_type != VAR_VOID
5473 && stack_type->tt_type != VAR_UNKNOWN)
5474 {
5475 emsg(_(e_returning_value_in_function_without_return_type));
5476 return NULL;
5477 }
5478 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005479 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005480 return NULL;
5481 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 }
5484 else
5485 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005486 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005487 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005488 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5489 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005491 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 return NULL;
5493 }
5494
5495 // No argument, return zero.
5496 generate_PUSHNR(cctx, 0);
5497 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005498
5499 // Undo any command modifiers.
5500 generate_undo_cmdmods(cctx);
5501
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005502 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 return NULL;
5504
5505 // "return val | endif" is possible
5506 return skipwhite(p);
5507}
5508
5509/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005510 * Get a line from the compilation context, compatible with exarg_T getline().
5511 * Return a pointer to the line in allocated memory.
5512 * Return NULL for end-of-file or some error.
5513 */
5514 static char_u *
5515exarg_getline(
5516 int c UNUSED,
5517 void *cookie,
5518 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005519 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005520{
5521 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005522 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005523
Bram Moolenaar66250c92020-08-20 15:02:42 +02005524 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005525 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005526 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005527 return NULL;
5528 ++cctx->ctx_lnum;
5529 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5530 // Comment lines result in NULL pointers, skip them.
5531 if (p != NULL)
5532 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005533 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005534}
5535
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005536 void
5537fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5538{
5539 eap->getline = exarg_getline;
5540 eap->cookie = cctx;
5541}
5542
Bram Moolenaar04b12692020-05-04 23:24:44 +02005543/*
5544 * Compile a nested :def command.
5545 */
5546 static char_u *
5547compile_nested_function(exarg_T *eap, cctx_T *cctx)
5548{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005549 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005550 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005551 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005552 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005553 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005554 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005555
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005556 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005557 {
5558 emsg(_(e_cannot_use_bang_with_nested_def));
5559 return NULL;
5560 }
5561
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005562 if (*name_start == '/')
5563 {
5564 name_end = skip_regexp(name_start + 1, '/', TRUE);
5565 if (*name_end == '/')
5566 ++name_end;
5567 eap->nextcmd = check_nextcmd(name_end);
5568 }
5569 if (name_end == name_start || *skipwhite(name_end) != '(')
5570 {
5571 if (!ends_excmd2(name_start, name_end))
5572 {
5573 semsg(_(e_invalid_command_str), eap->cmd);
5574 return NULL;
5575 }
5576
5577 // "def" or "def Name": list functions
5578 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5579 return NULL;
5580 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5581 }
5582
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005583 // Only g:Func() can use a namespace.
5584 if (name_start[1] == ':' && !is_global)
5585 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005586 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005587 return NULL;
5588 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005589 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005590 return NULL;
5591
Bram Moolenaar04b12692020-05-04 23:24:44 +02005592 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005593 fill_exarg_from_cctx(eap, cctx);
5594
Bram Moolenaar04b12692020-05-04 23:24:44 +02005595 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005596 lambda_name = vim_strsave(get_lambda_name());
5597 if (lambda_name == NULL)
5598 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005599 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005600
Bram Moolenaar822ba242020-05-24 23:00:18 +02005601 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005602 {
5603 r = eap->skip ? OK : FAIL;
5604 goto theend;
5605 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005606
5607 // copy over the block scope IDs before compiling
5608 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5609 {
5610 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5611
5612 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5613 if (ufunc->uf_block_ids != NULL)
5614 {
5615 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5616 sizeof(int) * block_depth);
5617 ufunc->uf_block_depth = block_depth;
5618 }
5619 }
5620
Bram Moolenaare99d4222021-06-13 14:01:26 +02005621 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5622 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005623 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005624 {
5625 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005626 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005627 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005628
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005629 if (is_global)
5630 {
5631 char_u *func_name = vim_strnsave(name_start + 2,
5632 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005633
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005634 if (func_name == NULL)
5635 r = FAIL;
5636 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005637 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005638 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005639 lambda_name = NULL;
5640 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005641 }
5642 else
5643 {
5644 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005645 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005646 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005647
Bram Moolenaareef21022020-08-01 22:16:43 +02005648 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005649 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005650 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005651 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005652 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5653 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005654 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005655
5656theend:
5657 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005658 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005659}
5660
5661/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005662 * Return the length of an assignment operator, or zero if there isn't one.
5663 */
5664 int
5665assignment_len(char_u *p, int *heredoc)
5666{
5667 if (*p == '=')
5668 {
5669 if (p[1] == '<' && p[2] == '<')
5670 {
5671 *heredoc = TRUE;
5672 return 3;
5673 }
5674 return 1;
5675 }
5676 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5677 return 2;
5678 if (STRNCMP(p, "..=", 3) == 0)
5679 return 3;
5680 return 0;
5681}
5682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005684 * Generate the load instruction for "name".
5685 */
5686 static void
5687generate_loadvar(
5688 cctx_T *cctx,
5689 assign_dest_T dest,
5690 char_u *name,
5691 lvar_T *lvar,
5692 type_T *type)
5693{
5694 switch (dest)
5695 {
5696 case dest_option:
5697 // TODO: check the option exists
5698 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5699 break;
5700 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005701 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5702 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5703 else
5704 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005705 break;
5706 case dest_buffer:
5707 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5708 break;
5709 case dest_window:
5710 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5711 break;
5712 case dest_tab:
5713 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5714 break;
5715 case dest_script:
5716 compile_load_scriptvar(cctx,
5717 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5718 break;
5719 case dest_env:
5720 // Include $ in the name here
5721 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5722 break;
5723 case dest_reg:
5724 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5725 break;
5726 case dest_vimvar:
5727 generate_LOADV(cctx, name + 2, TRUE);
5728 break;
5729 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005730 if (lvar->lv_from_outer > 0)
5731 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5732 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 else
5734 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5735 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005736 case dest_expr:
5737 // list or dict value should already be on the stack.
5738 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005739 }
5740}
5741
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005742/*
5743 * Skip over "[expr]" or ".member".
5744 * Does not check for any errors.
5745 */
5746 static char_u *
5747skip_index(char_u *start)
5748{
5749 char_u *p = start;
5750
5751 if (*p == '[')
5752 {
5753 p = skipwhite(p + 1);
5754 (void)skip_expr(&p, NULL);
5755 p = skipwhite(p);
5756 if (*p == ']')
5757 return p + 1;
5758 return p;
5759 }
5760 // if (*p == '.')
5761 return to_name_end(p + 1, TRUE);
5762}
5763
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005764 void
5765vim9_declare_error(char_u *name)
5766{
5767 char *scope = "";
5768
5769 switch (*name)
5770 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005771 case 'g': scope = _("global"); break;
5772 case 'b': scope = _("buffer"); break;
5773 case 'w': scope = _("window"); break;
5774 case 't': scope = _("tab"); break;
5775 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005776 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005777 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005778 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005779 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005780 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005781 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005782 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005783 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005784 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005785}
5786
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005787/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005788 * For one assignment figure out the type of destination. Return it in "dest".
5789 * When not recognized "dest" is not set.
5790 * For an option "opt_flags" is set.
5791 * For a v:var "vimvaridx" is set.
5792 * "type" is set to the destination type if known, unchanted otherwise.
5793 * Return FAIL if an error message was given.
5794 */
5795 static int
5796get_var_dest(
5797 char_u *name,
5798 assign_dest_T *dest,
5799 int cmdidx,
5800 int *opt_flags,
5801 int *vimvaridx,
5802 type_T **type,
5803 cctx_T *cctx)
5804{
5805 char_u *p;
5806
5807 if (*name == '&')
5808 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005809 int cc;
5810 long numval;
5811 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005812
5813 *dest = dest_option;
5814 if (cmdidx == CMD_final || cmdidx == CMD_const)
5815 {
5816 emsg(_(e_const_option));
5817 return FAIL;
5818 }
5819 p = name;
5820 p = find_option_end(&p, opt_flags);
5821 if (p == NULL)
5822 {
5823 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005824 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005825 return FAIL;
5826 }
5827 cc = *p;
5828 *p = NUL;
5829 opt_type = get_option_value(skip_option_env_lead(name),
5830 &numval, NULL, *opt_flags);
5831 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005832 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005833 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005834 case gov_unknown:
5835 semsg(_(e_unknown_option), name);
5836 return FAIL;
5837 case gov_string:
5838 case gov_hidden_string:
5839 *type = &t_string;
5840 break;
5841 case gov_bool:
5842 case gov_hidden_bool:
5843 *type = &t_bool;
5844 break;
5845 case gov_number:
5846 case gov_hidden_number:
5847 *type = &t_number;
5848 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005849 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005850 }
5851 else if (*name == '$')
5852 {
5853 *dest = dest_env;
5854 *type = &t_string;
5855 }
5856 else if (*name == '@')
5857 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005858 if (name[1] != '@'
5859 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005860 {
5861 emsg_invreg(name[1]);
5862 return FAIL;
5863 }
5864 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005865 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005866 }
5867 else if (STRNCMP(name, "g:", 2) == 0)
5868 {
5869 *dest = dest_global;
5870 }
5871 else if (STRNCMP(name, "b:", 2) == 0)
5872 {
5873 *dest = dest_buffer;
5874 }
5875 else if (STRNCMP(name, "w:", 2) == 0)
5876 {
5877 *dest = dest_window;
5878 }
5879 else if (STRNCMP(name, "t:", 2) == 0)
5880 {
5881 *dest = dest_tab;
5882 }
5883 else if (STRNCMP(name, "v:", 2) == 0)
5884 {
5885 typval_T *vtv;
5886 int di_flags;
5887
5888 *vimvaridx = find_vim_var(name + 2, &di_flags);
5889 if (*vimvaridx < 0)
5890 {
5891 semsg(_(e_variable_not_found_str), name);
5892 return FAIL;
5893 }
5894 // We use the current value of "sandbox" here, is that OK?
5895 if (var_check_ro(di_flags, name, FALSE))
5896 return FAIL;
5897 *dest = dest_vimvar;
5898 vtv = get_vim_var_tv(*vimvaridx);
5899 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5900 }
5901 return OK;
5902}
5903
5904/*
5905 * Generate a STORE instruction for "dest", not being "dest_local".
5906 * Return FAIL when out of memory.
5907 */
5908 static int
5909generate_store_var(
5910 cctx_T *cctx,
5911 assign_dest_T dest,
5912 int opt_flags,
5913 int vimvaridx,
5914 int scriptvar_idx,
5915 int scriptvar_sid,
5916 type_T *type,
5917 char_u *name)
5918{
5919 switch (dest)
5920 {
5921 case dest_option:
5922 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5923 opt_flags);
5924 case dest_global:
5925 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005926 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5927 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005928 case dest_buffer:
5929 // include b: with the name, easier to execute that way
5930 return generate_STORE(cctx, ISN_STOREB, 0, name);
5931 case dest_window:
5932 // include w: with the name, easier to execute that way
5933 return generate_STORE(cctx, ISN_STOREW, 0, name);
5934 case dest_tab:
5935 // include t: with the name, easier to execute that way
5936 return generate_STORE(cctx, ISN_STORET, 0, name);
5937 case dest_env:
5938 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5939 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005940 return generate_STORE(cctx, ISN_STOREREG,
5941 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005942 case dest_vimvar:
5943 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5944 case dest_script:
5945 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005946 // "s:" may be included in the name.
5947 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005948 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005949 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5950 scriptvar_sid, scriptvar_idx, type);
5951 case dest_local:
5952 case dest_expr:
5953 // cannot happen
5954 break;
5955 }
5956 return FAIL;
5957}
5958
Bram Moolenaar752fc692021-01-04 21:57:11 +01005959 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005960generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5961{
5962 if (lhs->lhs_dest != dest_local)
5963 return generate_store_var(cctx, lhs->lhs_dest,
5964 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5965 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5966 lhs->lhs_type, lhs->lhs_name);
5967
5968 if (lhs->lhs_lvar != NULL)
5969 {
5970 garray_T *instr = &cctx->ctx_instr;
5971 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5972
5973 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5974 // ISN_STORENR
5975 if (lhs->lhs_lvar->lv_from_outer == 0
5976 && instr->ga_len == instr_count + 1
5977 && isn->isn_type == ISN_PUSHNR)
5978 {
5979 varnumber_T val = isn->isn_arg.number;
5980 garray_T *stack = &cctx->ctx_type_stack;
5981
5982 isn->isn_type = ISN_STORENR;
5983 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5984 isn->isn_arg.storenr.stnr_val = val;
5985 if (stack->ga_len > 0)
5986 --stack->ga_len;
5987 }
5988 else if (lhs->lhs_lvar->lv_from_outer > 0)
5989 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5990 lhs->lhs_lvar->lv_from_outer);
5991 else
5992 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5993 }
5994 return OK;
5995}
5996
5997 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005998is_decl_command(int cmdidx)
5999{
6000 return cmdidx == CMD_let || cmdidx == CMD_var
6001 || cmdidx == CMD_final || cmdidx == CMD_const;
6002}
6003
6004/*
6005 * Figure out the LHS type and other properties for an assignment or one item
6006 * of ":unlet" with an index.
6007 * Returns OK or FAIL.
6008 */
6009 static int
6010compile_lhs(
6011 char_u *var_start,
6012 lhs_T *lhs,
6013 int cmdidx,
6014 int heredoc,
6015 int oplen,
6016 cctx_T *cctx)
6017{
6018 char_u *var_end;
6019 int is_decl = is_decl_command(cmdidx);
6020
6021 CLEAR_POINTER(lhs);
6022 lhs->lhs_dest = dest_local;
6023 lhs->lhs_vimvaridx = -1;
6024 lhs->lhs_scriptvar_idx = -1;
6025
6026 // "dest_end" is the end of the destination, including "[expr]" or
6027 // ".name".
6028 // "var_end" is the end of the variable/option/etc. name.
6029 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6030 if (*var_start == '@')
6031 var_end = var_start + 2;
6032 else
6033 {
6034 // skip over the leading "&", "&l:", "&g:" and "$"
6035 var_end = skip_option_env_lead(var_start);
6036 var_end = to_name_end(var_end, TRUE);
6037 }
6038
6039 // "a: type" is declaring variable "a" with a type, not dict "a:".
6040 if (is_decl && lhs->lhs_dest_end == var_start + 2
6041 && lhs->lhs_dest_end[-1] == ':')
6042 --lhs->lhs_dest_end;
6043 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6044 --var_end;
6045
6046 // compute the length of the destination without "[expr]" or ".name"
6047 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006048 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006049 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6050 if (lhs->lhs_name == NULL)
6051 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006052
6053 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6054 // Something follows after the variable: "var[idx]" or "var.key".
6055 lhs->lhs_has_index = TRUE;
6056
Bram Moolenaar752fc692021-01-04 21:57:11 +01006057 if (heredoc)
6058 lhs->lhs_type = &t_list_string;
6059 else
6060 lhs->lhs_type = &t_any;
6061
6062 if (cctx->ctx_skip != SKIP_YES)
6063 {
6064 int declare_error = FALSE;
6065
6066 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6067 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6068 &lhs->lhs_type, cctx) == FAIL)
6069 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006070 if (lhs->lhs_dest != dest_local
6071 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 {
6073 // Specific kind of variable recognized.
6074 declare_error = is_decl;
6075 }
6076 else
6077 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006079 if (check_reserved_name(lhs->lhs_name) == FAIL)
6080 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081
6082 if (lookup_local(var_start, lhs->lhs_varlen,
6083 &lhs->lhs_local_lvar, cctx) == OK)
6084 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6085 else
6086 {
6087 CLEAR_FIELD(lhs->lhs_arg_lvar);
6088 if (arg_exists(var_start, lhs->lhs_varlen,
6089 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6090 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6091 {
6092 if (is_decl)
6093 {
6094 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6095 return FAIL;
6096 }
6097 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6098 }
6099 }
6100 if (lhs->lhs_lvar != NULL)
6101 {
6102 if (is_decl)
6103 {
6104 semsg(_(e_variable_already_declared), lhs->lhs_name);
6105 return FAIL;
6106 }
6107 }
6108 else
6109 {
6110 int script_namespace = lhs->lhs_varlen > 1
6111 && STRNCMP(var_start, "s:", 2) == 0;
6112 int script_var = (script_namespace
6113 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006114 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006116 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006117 imported_T *import =
6118 find_imported(var_start, lhs->lhs_varlen, cctx);
6119
6120 if (script_namespace || script_var || import != NULL)
6121 {
6122 char_u *rawname = lhs->lhs_name
6123 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6124
6125 if (is_decl)
6126 {
6127 if (script_namespace)
6128 semsg(_(e_cannot_declare_script_variable_in_function),
6129 lhs->lhs_name);
6130 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006131 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 lhs->lhs_name);
6133 return FAIL;
6134 }
6135 else if (cctx->ctx_ufunc->uf_script_ctx_version
6136 == SCRIPT_VERSION_VIM9
6137 && script_namespace
6138 && !script_var && import == NULL)
6139 {
6140 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6141 return FAIL;
6142 }
6143
6144 lhs->lhs_dest = dest_script;
6145
6146 // existing script-local variables should have a type
6147 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6148 if (import != NULL)
6149 lhs->lhs_scriptvar_sid = import->imp_sid;
6150 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6151 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006152 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006154 lhs->lhs_scriptvar_sid, rawname,
6155 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6156 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if (lhs->lhs_scriptvar_idx >= 0)
6158 {
6159 scriptitem_T *si = SCRIPT_ITEM(
6160 lhs->lhs_scriptvar_sid);
6161 svar_T *sv =
6162 ((svar_T *)si->sn_var_vals.ga_data)
6163 + lhs->lhs_scriptvar_idx;
6164 lhs->lhs_type = sv->sv_type;
6165 }
6166 }
6167 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006168 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 == FAIL)
6170 return FAIL;
6171 }
6172 }
6173
6174 if (declare_error)
6175 {
6176 vim9_declare_error(lhs->lhs_name);
6177 return FAIL;
6178 }
6179 }
6180
6181 // handle "a:name" as a name, not index "name" on "a"
6182 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6183 var_end = lhs->lhs_dest_end;
6184
6185 if (lhs->lhs_dest != dest_option)
6186 {
6187 if (is_decl && *var_end == ':')
6188 {
6189 char_u *p;
6190
6191 // parse optional type: "let var: type = expr"
6192 if (!VIM_ISWHITE(var_end[1]))
6193 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006194 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006195 return FAIL;
6196 }
6197 p = skipwhite(var_end + 1);
6198 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6199 if (lhs->lhs_type == NULL)
6200 return FAIL;
6201 lhs->lhs_has_type = TRUE;
6202 }
6203 else if (lhs->lhs_lvar != NULL)
6204 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6205 }
6206
Bram Moolenaare42939a2021-04-05 17:11:17 +02006207 if (oplen == 3 && !heredoc
6208 && lhs->lhs_dest != dest_global
6209 && !lhs->lhs_has_index
6210 && lhs->lhs_type->tt_type != VAR_STRING
6211 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006212 {
6213 emsg(_(e_can_only_concatenate_to_string));
6214 return FAIL;
6215 }
6216
6217 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6218 && cctx->ctx_skip != SKIP_YES)
6219 {
6220 if (oplen > 1 && !heredoc)
6221 {
6222 // +=, /=, etc. require an existing variable
6223 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6224 return FAIL;
6225 }
6226 if (!is_decl)
6227 {
6228 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6229 return FAIL;
6230 }
6231
Bram Moolenaar3f327882021-03-17 20:56:38 +01006232 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 if ((lhs->lhs_type->tt_type == VAR_FUNC
6234 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006235 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006236 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006237
6238 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6240 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6241 if (lhs->lhs_lvar == NULL)
6242 return FAIL;
6243 lhs->lhs_new_local = TRUE;
6244 }
6245
6246 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006247 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 {
6249 // Something follows after the variable: "var[idx]" or "var.key".
6250 // TODO: should we also handle "->func()" here?
6251 if (is_decl)
6252 {
6253 emsg(_(e_cannot_use_index_when_declaring_variable));
6254 return FAIL;
6255 }
6256
6257 if (var_start[lhs->lhs_varlen] == '['
6258 || var_start[lhs->lhs_varlen] == '.')
6259 {
6260 char_u *after = var_start + lhs->lhs_varlen;
6261 char_u *p;
6262
6263 // Only the last index is used below, if there are others
6264 // before it generate code for the expression. Thus for
6265 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6266 for (;;)
6267 {
6268 p = skip_index(after);
6269 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006270 {
6271 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006273 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006274 after = p;
6275 }
6276 if (after > var_start + lhs->lhs_varlen)
6277 {
6278 lhs->lhs_varlen = after - var_start;
6279 lhs->lhs_dest = dest_expr;
6280 // We don't know the type before evaluating the expression,
6281 // use "any" until then.
6282 lhs->lhs_type = &t_any;
6283 }
6284
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 if (lhs->lhs_type->tt_member == NULL)
6286 lhs->lhs_member_type = &t_any;
6287 else
6288 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6289 }
6290 else
6291 {
6292 semsg("Not supported yet: %s", var_start);
6293 return FAIL;
6294 }
6295 }
6296 return OK;
6297}
6298
6299/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006300 * Figure out the LHS and check a few errors.
6301 */
6302 static int
6303compile_assign_lhs(
6304 char_u *var_start,
6305 lhs_T *lhs,
6306 int cmdidx,
6307 int is_decl,
6308 int heredoc,
6309 int oplen,
6310 cctx_T *cctx)
6311{
6312 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6313 return FAIL;
6314
6315 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6316 {
6317 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6318 return FAIL;
6319 }
6320 if (!is_decl && lhs->lhs_lvar != NULL
6321 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6322 {
6323 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6324 return FAIL;
6325 }
6326 return OK;
6327}
6328
6329/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006330 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6331 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006332 */
6333 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006334compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 char_u *var_start,
6336 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006337 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 cctx_T *cctx)
6339{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006341 char_u *p;
6342 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006343 int need_white_before = TRUE;
6344 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006345
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 p = var_start + varlen;
6347 if (*p == '[')
6348 {
6349 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006350 if (*p == ':')
6351 {
6352 // empty first index, push zero
6353 r = generate_PUSHNR(cctx, 0);
6354 need_white_before = FALSE;
6355 }
6356 else
6357 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006358
6359 if (r == OK && *skipwhite(p) == ':')
6360 {
6361 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006362 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006363 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006364 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006365 empty_second = *skipwhite(p + 1) == ']';
6366 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6367 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006368 {
6369 semsg(_(e_white_space_required_before_and_after_str_at_str),
6370 ":", p);
6371 return FAIL;
6372 }
6373 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006374 if (*p == ']')
6375 // empty second index, push "none"
6376 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6377 else
6378 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006379 }
6380
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 if (r == OK && *skipwhite(p) != ']')
6382 {
6383 // this should not happen
6384 emsg(_(e_missbrac));
6385 r = FAIL;
6386 }
6387 }
6388 else // if (*p == '.')
6389 {
6390 char_u *key_end = to_name_end(p + 1, TRUE);
6391 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6392
6393 r = generate_PUSHS(cctx, key);
6394 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006395 return r;
6396}
6397
6398/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006399 * For a LHS with an index, load the variable to be indexed.
6400 */
6401 static int
6402compile_load_lhs(
6403 lhs_T *lhs,
6404 char_u *var_start,
6405 type_T *rhs_type,
6406 cctx_T *cctx)
6407{
6408 if (lhs->lhs_dest == dest_expr)
6409 {
6410 size_t varlen = lhs->lhs_varlen;
6411 int c = var_start[varlen];
6412 char_u *p = var_start;
6413 garray_T *stack = &cctx->ctx_type_stack;
6414
6415 // Evaluate "ll[expr]" of "ll[expr][idx]"
6416 var_start[varlen] = NUL;
6417 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6418 {
6419 // this should not happen
6420 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006421 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006422 return FAIL;
6423 }
6424 var_start[varlen] = c;
6425
6426 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6427 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6428 // now we can properly check the type
6429 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6430 && rhs_type != &t_void
6431 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6432 FALSE, FALSE) == FAIL)
6433 return FAIL;
6434 }
6435 else
6436 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6437 lhs->lhs_lvar, lhs->lhs_type);
6438 return OK;
6439}
6440
6441/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006442 * Produce code for loading "lhs" and also take care of an index.
6443 * Return OK/FAIL.
6444 */
6445 static int
6446compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6447{
6448 compile_load_lhs(lhs, var_start, NULL, cctx);
6449
6450 if (lhs->lhs_has_index)
6451 {
6452 int range = FALSE;
6453
6454 // Get member from list or dict. First compile the
6455 // index value.
6456 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6457 return FAIL;
6458 if (range)
6459 {
6460 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6461 var_start);
6462 return FAIL;
6463 }
6464
6465 // Get the member.
6466 if (compile_member(FALSE, cctx) == FAIL)
6467 return FAIL;
6468 }
6469 return OK;
6470}
6471
6472/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006473 * Assignment to a list or dict member, or ":unlet" for the item, using the
6474 * information in "lhs".
6475 * Returns OK or FAIL.
6476 */
6477 static int
6478compile_assign_unlet(
6479 char_u *var_start,
6480 lhs_T *lhs,
6481 int is_assign,
6482 type_T *rhs_type,
6483 cctx_T *cctx)
6484{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006485 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006486 garray_T *stack = &cctx->ctx_type_stack;
6487 int range = FALSE;
6488
Bram Moolenaar68452172021-04-12 21:21:02 +02006489 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006491 if (is_assign && range && lhs->lhs_type != &t_blob
6492 && lhs->lhs_type != &t_any)
6493 {
6494 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6495 return FAIL;
6496 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497
6498 if (lhs->lhs_type == &t_any)
6499 {
6500 // Index on variable of unknown type: check at runtime.
6501 dest_type = VAR_ANY;
6502 }
6503 else
6504 {
6505 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006506 if (dest_type == VAR_DICT && range)
6507 {
6508 emsg(e_cannot_use_range_with_dictionary);
6509 return FAIL;
6510 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006511 if (dest_type == VAR_DICT
6512 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006514 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006515 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006516 type_T *type;
6517
6518 if (range)
6519 {
6520 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6521 if (need_type(type, &t_number,
6522 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006523 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006524 }
6525 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6526 if ((dest_type != VAR_BLOB || type != &t_special)
6527 && need_type(type, &t_number,
6528 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006529 return FAIL;
6530 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006531 }
6532
6533 // Load the dict or list. On the stack we then have:
6534 // - value (for assignment, not for :unlet)
6535 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006536 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006538 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6539 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540
Bram Moolenaar68452172021-04-12 21:21:02 +02006541 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6542 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006543 {
6544 if (is_assign)
6545 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006546 if (range)
6547 {
6548 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6549 return FAIL;
6550 }
6551 else
6552 {
6553 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554
Bram Moolenaar68452172021-04-12 21:21:02 +02006555 if (isn == NULL)
6556 return FAIL;
6557 isn->isn_arg.vartype = dest_type;
6558 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006560 else if (range)
6561 {
6562 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6563 return FAIL;
6564 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006565 else
6566 {
6567 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6568 return FAIL;
6569 }
6570 }
6571 else
6572 {
6573 emsg(_(e_indexable_type_required));
6574 return FAIL;
6575 }
6576
6577 return OK;
6578}
6579
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006580/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006581 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006582 * "let name"
6583 * "var name = expr"
6584 * "final name = expr"
6585 * "const name = expr"
6586 * "name = expr"
6587 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006588 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006589 * Return NULL for an error.
6590 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 */
6592 static char_u *
6593compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6594{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006595 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006597 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 char_u *ret = NULL;
6599 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006600 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006602 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006604 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 int oplen = 0;
6607 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006608 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006609 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 int is_decl = is_decl_command(cmdidx);
6612 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006613 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006615 // Skip over the "var" or "[var, var]" to get to any "=".
6616 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6617 if (p == NULL)
6618 return *arg == '[' ? arg : NULL;
6619
6620 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006622 // TODO: should we allow this, and figure out type inference from list
6623 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006624 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 return NULL;
6626 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006627 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 sp = p;
6630 p = skipwhite(p);
6631 op = p;
6632 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006633
6634 if (var_count > 0 && oplen == 0)
6635 // can be something like "[1, 2]->func()"
6636 return arg;
6637
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006638 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006640 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006641 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006642 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006643 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6644 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006645 if (VIM_ISWHITE(eap->cmd[2]))
6646 {
6647 semsg(_(e_no_white_space_allowed_after_str_str),
6648 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6649 return NULL;
6650 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006651 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6652 oplen = 2;
6653 incdec = TRUE;
6654 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 if (heredoc)
6657 {
6658 list_T *l;
6659 listitem_T *li;
6660
6661 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006662 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006664 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006665 if (l == NULL)
6666 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667
Bram Moolenaar078269b2020-09-21 20:35:55 +02006668 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006670 // Push each line and the create the list.
6671 FOR_ALL_LIST_ITEMS(l, li)
6672 {
6673 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6674 li->li_tv.vval.v_string = NULL;
6675 }
6676 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 list_free(l);
6679 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006682 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006684 char_u *wp;
6685
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006686 // for "[var, var] = expr" evaluate the expression here, loop over the
6687 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006688 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006689
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006690 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006691 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006692 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006693 if (compile_expr0(&p, cctx) == FAIL)
6694 return NULL;
6695 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006697 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006699 type_T *stacktype;
6700
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006701 stacktype = stack->ga_len == 0 ? &t_void
6702 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006703 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006705 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006706 goto theend;
6707 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006708 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006709 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006710 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006711 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006712 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6713 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006714 if (stacktype->tt_member != NULL)
6715 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 }
6717 }
6718
6719 /*
6720 * Loop over variables in "[var, var] = expr".
6721 * For "var = expr" and "let var: type" this is done only once.
6722 */
6723 if (var_count > 0)
6724 var_start = skipwhite(arg + 1); // skip over the "["
6725 else
6726 var_start = arg;
6727 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6728 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006729 int instr_count = -1;
6730 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006731
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006732 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6733 {
6734 // Ignore underscore in "[a, _, b] = list".
6735 if (var_count > 0)
6736 {
6737 var_start = skipwhite(var_start + 2);
6738 continue;
6739 }
6740 emsg(_(e_cannot_use_underscore_here));
6741 goto theend;
6742 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006743 vim_free(lhs.lhs_name);
6744
6745 /*
6746 * Figure out the LHS type and other properties.
6747 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006748 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6749 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006750 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006751 if (!heredoc)
6752 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006753 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006754 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006755 if (oplen > 0 && var_count == 0)
6756 {
6757 // skip over the "=" and the expression
6758 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006759 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006760 }
6761 }
6762 else if (oplen > 0)
6763 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006764 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006765 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006766
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006767 // for "+=", "*=", "..=" etc. first load the current value
6768 if (*op != '='
6769 && compile_load_lhs_with_index(&lhs, var_start,
6770 cctx) == FAIL)
6771 goto theend;
6772
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006773 // For "var = expr" evaluate the expression.
6774 if (var_count == 0)
6775 {
6776 int r;
6777
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006778 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006779 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006780 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006781 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006782 r = generate_PUSHNR(cctx, 1);
6783 }
6784 else
6785 {
6786 // Temporarily hide the new local variable here, it is
6787 // not available to this expression.
6788 if (lhs.lhs_new_local)
6789 --cctx->ctx_locals.ga_len;
6790 wp = op + oplen;
6791 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6792 {
6793 if (lhs.lhs_new_local)
6794 ++cctx->ctx_locals.ga_len;
6795 goto theend;
6796 }
6797 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006798 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006799 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006800 if (r == FAIL)
6801 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006802 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006803 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006804 else if (semicolon && var_idx == var_count - 1)
6805 {
6806 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006807 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006808 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6809 goto theend;
6810 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006811 else
6812 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006813 // For "[var, var] = expr" get the "var_idx" item from the
6814 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006815 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006816 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006817 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006818
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006819 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006820 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006821 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006822 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006823 if ((rhs_type->tt_type == VAR_FUNC
6824 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006825 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006826 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006827 goto theend;
6828
Bram Moolenaar752fc692021-01-04 21:57:11 +01006829 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006830 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006831 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006833 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006834 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006835 }
6836 else
6837 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006838 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006839 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006840 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006841 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006842 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006843 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006844 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006845 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006846 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006847 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006848 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006849 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006850 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006851 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006852 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006853
Bram Moolenaar77709b12021-04-03 21:01:01 +02006854 // Without operator check type here, otherwise below.
6855 // Use the line number of the assignment.
6856 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 if (lhs.lhs_has_index)
6858 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006859 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006860 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006861 goto theend;
6862 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006863 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006864 else
6865 {
6866 type_T *lhs_type = lhs.lhs_member_type;
6867
6868 // Special case: assigning to @# can use a number or a
6869 // string.
6870 if (lhs_type == &t_number_or_string
6871 && rhs_type->tt_type == VAR_NUMBER)
6872 lhs_type = &t_number;
6873 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006874 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006875 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006878 else if (cmdidx == CMD_final)
6879 {
6880 emsg(_(e_final_requires_a_value));
6881 goto theend;
6882 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006883 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006885 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006886 goto theend;
6887 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006888 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006889 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006890 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006891 goto theend;
6892 }
6893 else
6894 {
6895 // variables are always initialized
6896 if (ga_grow(instr, 1) == FAIL)
6897 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006898 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006899 {
6900 case VAR_BOOL:
6901 generate_PUSHBOOL(cctx, VVAL_FALSE);
6902 break;
6903 case VAR_FLOAT:
6904#ifdef FEAT_FLOAT
6905 generate_PUSHF(cctx, 0.0);
6906#endif
6907 break;
6908 case VAR_STRING:
6909 generate_PUSHS(cctx, NULL);
6910 break;
6911 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006912 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006913 break;
6914 case VAR_FUNC:
6915 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6916 break;
6917 case VAR_LIST:
6918 generate_NEWLIST(cctx, 0);
6919 break;
6920 case VAR_DICT:
6921 generate_NEWDICT(cctx, 0);
6922 break;
6923 case VAR_JOB:
6924 generate_PUSHJOB(cctx, NULL);
6925 break;
6926 case VAR_CHANNEL:
6927 generate_PUSHCHANNEL(cctx, NULL);
6928 break;
6929 case VAR_NUMBER:
6930 case VAR_UNKNOWN:
6931 case VAR_ANY:
6932 case VAR_PARTIAL:
6933 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006934 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 case VAR_SPECIAL: // cannot happen
6936 generate_PUSHNR(cctx, 0);
6937 break;
6938 }
6939 }
6940 if (var_count == 0)
6941 end = p;
6942 }
6943
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006944 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006945 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006946 break;
6947
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006948 if (oplen > 0 && *op != '=')
6949 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006950 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006951 type_T *stacktype;
6952
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006953 if (*op == '.')
6954 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006955 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006956 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006958 if (
6959#ifdef FEAT_FLOAT
6960 // If variable is float operation with number is OK.
6961 !(expected == &t_float && stacktype == &t_number) &&
6962#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006963 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006964 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006965 goto theend;
6966
6967 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006968 {
6969 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6970 goto theend;
6971 }
6972 else if (*op == '+')
6973 {
6974 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 operator_type(lhs.lhs_member_type, stacktype),
6976 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006977 goto theend;
6978 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006979 else if (generate_two_op(cctx, op) == FAIL)
6980 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006983 // Use the line number of the assignment for store instruction.
6984 save_lnum = cctx->ctx_lnum;
6985 cctx->ctx_lnum = start_lnum - 1;
6986
Bram Moolenaar752fc692021-01-04 21:57:11 +01006987 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006988 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006989 // Use the info in "lhs" to store the value at the index in the
6990 // list or dict.
6991 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6992 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006993 {
6994 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006996 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006997 }
6998 else
6999 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007000 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007001 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007002 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007003 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007004 generate_LOCKCONST(cctx);
7005
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007006 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007007 && (lhs.lhs_type->tt_type == VAR_DICT
7008 || lhs.lhs_type->tt_type == VAR_LIST)
7009 && lhs.lhs_type->tt_member != NULL
7010 && lhs.lhs_type->tt_member != &t_any
7011 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007012 // Set the type in the list or dict, so that it can be checked,
7013 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007014 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007015
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007016 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007017 {
7018 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007019 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007020 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007021 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007022 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007023
7024 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007025 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007027
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007028 // For "[var, var] = expr" drop the "expr" value.
7029 // Also for "[var, var; _] = expr".
7030 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007031 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007032 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007033 goto theend;
7034 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035
Bram Moolenaarb2097502020-07-19 17:17:02 +02007036 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037
7038theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007039 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 return ret;
7041}
7042
7043/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007044 * Check for an assignment at "eap->cmd", compile it if found.
7045 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7046 */
7047 static int
7048may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7049{
7050 char_u *pskip;
7051 char_u *p;
7052
7053 // Assuming the command starts with a variable or function name,
7054 // find what follows.
7055 // Skip over "var.member", "var[idx]" and the like.
7056 // Also "&opt = val", "$ENV = val" and "@r = val".
7057 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7058 ? eap->cmd + 1 : eap->cmd;
7059 p = to_name_end(pskip, TRUE);
7060 if (p > eap->cmd && *p != NUL)
7061 {
7062 char_u *var_end;
7063 int oplen;
7064 int heredoc;
7065
7066 if (eap->cmd[0] == '@')
7067 var_end = eap->cmd + 2;
7068 else
7069 var_end = find_name_end(pskip, NULL, NULL,
7070 FNE_CHECK_START | FNE_INCL_BR);
7071 oplen = assignment_len(skipwhite(var_end), &heredoc);
7072 if (oplen > 0)
7073 {
7074 size_t len = p - eap->cmd;
7075
7076 // Recognize an assignment if we recognize the variable
7077 // name:
7078 // "g:var = expr"
7079 // "local = expr" where "local" is a local var.
7080 // "script = expr" where "script" is a script-local var.
7081 // "import = expr" where "import" is an imported var
7082 // "&opt = expr"
7083 // "$ENV = expr"
7084 // "@r = expr"
7085 if (*eap->cmd == '&'
7086 || *eap->cmd == '$'
7087 || *eap->cmd == '@'
7088 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007089 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007090 {
7091 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7092 if (*line == NULL || *line == eap->cmd)
7093 return FAIL;
7094 return OK;
7095 }
7096 }
7097 }
7098
7099 if (*eap->cmd == '[')
7100 {
7101 // [var, var] = expr
7102 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7103 if (*line == NULL)
7104 return FAIL;
7105 if (*line != eap->cmd)
7106 return OK;
7107 }
7108 return NOTDONE;
7109}
7110
7111/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007112 * Check if "name" can be "unlet".
7113 */
7114 int
7115check_vim9_unlet(char_u *name)
7116{
7117 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7118 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007119 // "unlet s:var" is allowed in legacy script.
7120 if (*name == 's' && !script_is_vim9())
7121 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007122 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007123 return FAIL;
7124 }
7125 return OK;
7126}
7127
7128/*
7129 * Callback passed to ex_unletlock().
7130 */
7131 static int
7132compile_unlet(
7133 lval_T *lvp,
7134 char_u *name_end,
7135 exarg_T *eap,
7136 int deep UNUSED,
7137 void *coookie)
7138{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007139 cctx_T *cctx = coookie;
7140 char_u *p = lvp->ll_name;
7141 int cc = *name_end;
7142 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007143
Bram Moolenaar752fc692021-01-04 21:57:11 +01007144 if (cctx->ctx_skip == SKIP_YES)
7145 return OK;
7146
7147 *name_end = NUL;
7148 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007149 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007150 // :unlet $ENV_VAR
7151 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7152 }
7153 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7154 {
7155 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007156
Bram Moolenaar752fc692021-01-04 21:57:11 +01007157 // This is similar to assigning: lookup the list/dict, compile the
7158 // idx/key. Then instead of storing the value unlet the item.
7159 // unlet {list}[idx]
7160 // unlet {dict}[key] dict.key
7161 //
7162 // Figure out the LHS type and other properties.
7163 //
7164 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7165
7166 // : unlet an indexed item
7167 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007168 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007169 iemsg("called compile_lhs() without an index");
7170 ret = FAIL;
7171 }
7172 else
7173 {
7174 // Use the info in "lhs" to unlet the item at the index in the
7175 // list or dict.
7176 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007177 }
7178
Bram Moolenaar752fc692021-01-04 21:57:11 +01007179 vim_free(lhs.lhs_name);
7180 }
7181 else if (check_vim9_unlet(p) == FAIL)
7182 {
7183 ret = FAIL;
7184 }
7185 else
7186 {
7187 // Normal name. Only supports g:, w:, t: and b: namespaces.
7188 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007189 }
7190
Bram Moolenaar752fc692021-01-04 21:57:11 +01007191 *name_end = cc;
7192 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007193}
7194
7195/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007196 * Callback passed to ex_unletlock().
7197 */
7198 static int
7199compile_lock_unlock(
7200 lval_T *lvp,
7201 char_u *name_end,
7202 exarg_T *eap,
7203 int deep UNUSED,
7204 void *coookie)
7205{
7206 cctx_T *cctx = coookie;
7207 int cc = *name_end;
7208 char_u *p = lvp->ll_name;
7209 int ret = OK;
7210 size_t len;
7211 char_u *buf;
7212
7213 if (cctx->ctx_skip == SKIP_YES)
7214 return OK;
7215
7216 // Cannot use :lockvar and :unlockvar on local variables.
7217 if (p[1] != ':')
7218 {
7219 char_u *end = skip_var_one(p, FALSE);
7220
7221 if (lookup_local(p, end - p, NULL, cctx) == OK)
7222 {
7223 emsg(_(e_cannot_lock_unlock_local_variable));
7224 return FAIL;
7225 }
7226 }
7227
7228 // Checking is done at runtime.
7229 *name_end = NUL;
7230 len = name_end - p + 20;
7231 buf = alloc(len);
7232 if (buf == NULL)
7233 ret = FAIL;
7234 else
7235 {
7236 vim_snprintf((char *)buf, len, "%s %s",
7237 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7238 p);
7239 ret = generate_EXEC(cctx, buf);
7240
7241 vim_free(buf);
7242 *name_end = cc;
7243 }
7244 return ret;
7245}
7246
7247/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007248 * compile "unlet var", "lock var" and "unlock var"
7249 * "arg" points to "var".
7250 */
7251 static char_u *
7252compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7253{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007254 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7255 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7256 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007257 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7258}
7259
7260/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 * Compile an :import command.
7262 */
7263 static char_u *
7264compile_import(char_u *arg, cctx_T *cctx)
7265{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007266 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267}
7268
7269/*
7270 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7271 */
7272 static int
7273compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7274{
7275 garray_T *instr = &cctx->ctx_instr;
7276 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7277
7278 if (endlabel == NULL)
7279 return FAIL;
7280 endlabel->el_next = *el;
7281 *el = endlabel;
7282 endlabel->el_end_label = instr->ga_len;
7283
7284 generate_JUMP(cctx, when, 0);
7285 return OK;
7286}
7287
7288 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007289compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290{
7291 garray_T *instr = &cctx->ctx_instr;
7292
7293 while (*el != NULL)
7294 {
7295 endlabel_T *cur = (*el);
7296 isn_T *isn;
7297
7298 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007299 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300 *el = cur->el_next;
7301 vim_free(cur);
7302 }
7303}
7304
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007305 static void
7306compile_free_jump_to_end(endlabel_T **el)
7307{
7308 while (*el != NULL)
7309 {
7310 endlabel_T *cur = (*el);
7311
7312 *el = cur->el_next;
7313 vim_free(cur);
7314 }
7315}
7316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317/*
7318 * Create a new scope and set up the generic items.
7319 */
7320 static scope_T *
7321new_scope(cctx_T *cctx, scopetype_T type)
7322{
7323 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7324
7325 if (scope == NULL)
7326 return NULL;
7327 scope->se_outer = cctx->ctx_scope;
7328 cctx->ctx_scope = scope;
7329 scope->se_type = type;
7330 scope->se_local_count = cctx->ctx_locals.ga_len;
7331 return scope;
7332}
7333
7334/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007335 * Free the current scope and go back to the outer scope.
7336 */
7337 static void
7338drop_scope(cctx_T *cctx)
7339{
7340 scope_T *scope = cctx->ctx_scope;
7341
7342 if (scope == NULL)
7343 {
7344 iemsg("calling drop_scope() without a scope");
7345 return;
7346 }
7347 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007348 switch (scope->se_type)
7349 {
7350 case IF_SCOPE:
7351 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7352 case FOR_SCOPE:
7353 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7354 case WHILE_SCOPE:
7355 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7356 case TRY_SCOPE:
7357 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7358 case NO_SCOPE:
7359 case BLOCK_SCOPE:
7360 break;
7361 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007362 vim_free(scope);
7363}
7364
7365/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 * compile "if expr"
7367 *
7368 * "if expr" Produces instructions:
7369 * EVAL expr Push result of "expr"
7370 * JUMP_IF_FALSE end
7371 * ... body ...
7372 * end:
7373 *
7374 * "if expr | else" Produces instructions:
7375 * EVAL expr Push result of "expr"
7376 * JUMP_IF_FALSE else
7377 * ... body ...
7378 * JUMP_ALWAYS end
7379 * else:
7380 * ... body ...
7381 * end:
7382 *
7383 * "if expr1 | elseif expr2 | else" Produces instructions:
7384 * EVAL expr Push result of "expr"
7385 * JUMP_IF_FALSE elseif
7386 * ... body ...
7387 * JUMP_ALWAYS end
7388 * elseif:
7389 * EVAL expr Push result of "expr"
7390 * JUMP_IF_FALSE else
7391 * ... body ...
7392 * JUMP_ALWAYS end
7393 * else:
7394 * ... body ...
7395 * end:
7396 */
7397 static char_u *
7398compile_if(char_u *arg, cctx_T *cctx)
7399{
7400 char_u *p = arg;
7401 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007402 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007404 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007405 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406
Bram Moolenaara5565e42020-05-09 15:44:01 +02007407 CLEAR_FIELD(ppconst);
7408 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007409 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007410 clear_ppconst(&ppconst);
7411 return NULL;
7412 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007413 if (!ends_excmd2(arg, skipwhite(p)))
7414 {
7415 semsg(_(e_trailing_arg), p);
7416 return NULL;
7417 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007418 if (cctx->ctx_skip == SKIP_YES)
7419 clear_ppconst(&ppconst);
7420 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007421 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007422 int error = FALSE;
7423 int v;
7424
Bram Moolenaara5565e42020-05-09 15:44:01 +02007425 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007426 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007427 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007428 if (error)
7429 return NULL;
7430 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007431 }
7432 else
7433 {
7434 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007435 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007436 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007437 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007438 if (bool_on_stack(cctx) == FAIL)
7439 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441
Bram Moolenaara91a7132021-03-25 21:12:15 +01007442 // CMDMOD_REV must come before the jump
7443 generate_undo_cmdmods(cctx);
7444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 scope = new_scope(cctx, IF_SCOPE);
7446 if (scope == NULL)
7447 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007448 scope->se_skip_save = skip_save;
7449 // "is_had_return" will be reset if any block does not end in :return
7450 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007452 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007453 {
7454 // "where" is set when ":elseif", "else" or ":endif" is found
7455 scope->se_u.se_if.is_if_label = instr->ga_len;
7456 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7457 }
7458 else
7459 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460
Bram Moolenaarced68a02021-01-24 17:53:47 +01007461#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007462 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007463 && skip_save != SKIP_YES)
7464 {
7465 // generated a profile start, need to generate a profile end, since it
7466 // won't be done after returning
7467 cctx->ctx_skip = SKIP_NOT;
7468 generate_instr(cctx, ISN_PROF_END);
7469 cctx->ctx_skip = SKIP_YES;
7470 }
7471#endif
7472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 return p;
7474}
7475
7476 static char_u *
7477compile_elseif(char_u *arg, cctx_T *cctx)
7478{
7479 char_u *p = arg;
7480 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007481 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 isn_T *isn;
7483 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007484 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007485 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486
7487 if (scope == NULL || scope->se_type != IF_SCOPE)
7488 {
7489 emsg(_(e_elseif_without_if));
7490 return NULL;
7491 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007492 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007493 if (!cctx->ctx_had_return)
7494 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495
Bram Moolenaarced68a02021-01-24 17:53:47 +01007496 if (cctx->ctx_skip == SKIP_NOT)
7497 {
7498 // previous block was executed, this one and following will not
7499 cctx->ctx_skip = SKIP_YES;
7500 scope->se_u.se_if.is_seen_skip_not = TRUE;
7501 }
7502 if (scope->se_u.se_if.is_seen_skip_not)
7503 {
7504 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007505 // Do not count the "elseif" for profiling and cmdmod
7506 instr->ga_len = current_instr_idx(cctx);
7507
Bram Moolenaarced68a02021-01-24 17:53:47 +01007508 skip_expr_cctx(&p, cctx);
7509 return p;
7510 }
7511
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007512 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007513 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007514 int moved_cmdmod = FALSE;
7515
7516 // Move any CMDMOD instruction to after the jump
7517 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7518 {
7519 if (ga_grow(instr, 1) == FAIL)
7520 return NULL;
7521 ((isn_T *)instr->ga_data)[instr->ga_len] =
7522 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7523 --instr->ga_len;
7524 moved_cmdmod = TRUE;
7525 }
7526
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007527 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007529 return NULL;
7530 // previous "if" or "elseif" jumps here
7531 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7532 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007533 if (moved_cmdmod)
7534 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007537 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007538 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007539 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007540 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007541 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007542#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007543 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007544 {
7545 // the previous block was skipped, need to profile this line
7546 generate_instr(cctx, ISN_PROF_START);
7547 instr_count = instr->ga_len;
7548 }
7549#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007550 if (cctx->ctx_compile_type == CT_DEBUG)
7551 {
7552 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007553 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007554 instr_count = instr->ga_len;
7555 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007556 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007557 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007558 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007559 clear_ppconst(&ppconst);
7560 return NULL;
7561 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007562 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007563 if (!ends_excmd2(arg, skipwhite(p)))
7564 {
7565 semsg(_(e_trailing_arg), p);
7566 return NULL;
7567 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007568 if (scope->se_skip_save == SKIP_YES)
7569 clear_ppconst(&ppconst);
7570 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007571 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007572 int error = FALSE;
7573 int v;
7574
Bram Moolenaar7f141552020-05-09 17:35:53 +02007575 // The expression results in a constant.
7576 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007577 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7578 if (error)
7579 return NULL;
7580 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007581 clear_ppconst(&ppconst);
7582 scope->se_u.se_if.is_if_label = -1;
7583 }
7584 else
7585 {
7586 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007587 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007588 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007589 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007590 if (bool_on_stack(cctx) == FAIL)
7591 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592
Bram Moolenaara91a7132021-03-25 21:12:15 +01007593 // CMDMOD_REV must come before the jump
7594 generate_undo_cmdmods(cctx);
7595
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007596 // "where" is set when ":elseif", "else" or ":endif" is found
7597 scope->se_u.se_if.is_if_label = instr->ga_len;
7598 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600
7601 return p;
7602}
7603
7604 static char_u *
7605compile_else(char_u *arg, cctx_T *cctx)
7606{
7607 char_u *p = arg;
7608 garray_T *instr = &cctx->ctx_instr;
7609 isn_T *isn;
7610 scope_T *scope = cctx->ctx_scope;
7611
7612 if (scope == NULL || scope->se_type != IF_SCOPE)
7613 {
7614 emsg(_(e_else_without_if));
7615 return NULL;
7616 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007617 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007618 if (!cctx->ctx_had_return)
7619 scope->se_u.se_if.is_had_return = FALSE;
7620 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621
Bram Moolenaarced68a02021-01-24 17:53:47 +01007622#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007623 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007624 {
7625 if (cctx->ctx_skip == SKIP_NOT
7626 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7627 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007628 // the previous block was executed, do not count "else" for
7629 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007630 --instr->ga_len;
7631 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7632 {
7633 // the previous block was not executed, this one will, do count the
7634 // "else" for profiling
7635 cctx->ctx_skip = SKIP_NOT;
7636 generate_instr(cctx, ISN_PROF_END);
7637 generate_instr(cctx, ISN_PROF_START);
7638 cctx->ctx_skip = SKIP_YES;
7639 }
7640 }
7641#endif
7642
7643 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007644 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007645 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007646 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007647 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007648 if (!cctx->ctx_had_return
7649 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7650 JUMP_ALWAYS, cctx) == FAIL)
7651 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007652 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007653
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007654 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007655 {
7656 if (scope->se_u.se_if.is_if_label >= 0)
7657 {
7658 // previous "if" or "elseif" jumps here
7659 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7660 isn->isn_arg.jump.jump_where = instr->ga_len;
7661 scope->se_u.se_if.is_if_label = -1;
7662 }
7663 }
7664
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007665 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007666 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668
7669 return p;
7670}
7671
7672 static char_u *
7673compile_endif(char_u *arg, cctx_T *cctx)
7674{
7675 scope_T *scope = cctx->ctx_scope;
7676 ifscope_T *ifscope;
7677 garray_T *instr = &cctx->ctx_instr;
7678 isn_T *isn;
7679
Bram Moolenaarfa984412021-03-25 22:15:28 +01007680 if (misplaced_cmdmod(cctx))
7681 return NULL;
7682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 if (scope == NULL || scope->se_type != IF_SCOPE)
7684 {
7685 emsg(_(e_endif_without_if));
7686 return NULL;
7687 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007688 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007689 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007690 if (!cctx->ctx_had_return)
7691 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007693 if (scope->se_u.se_if.is_if_label >= 0)
7694 {
7695 // previous "if" or "elseif" jumps here
7696 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7697 isn->isn_arg.jump.jump_where = instr->ga_len;
7698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007700 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007701
7702#ifdef FEAT_PROFILE
7703 // even when skipping we count the endif as executed, unless the block it's
7704 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007705 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007706 && scope->se_skip_save != SKIP_YES)
7707 {
7708 cctx->ctx_skip = SKIP_NOT;
7709 generate_instr(cctx, ISN_PROF_START);
7710 }
7711#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007712 cctx->ctx_skip = scope->se_skip_save;
7713
7714 // If all the blocks end in :return and there is an :else then the
7715 // had_return flag is set.
7716 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007718 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 return arg;
7720}
7721
7722/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007723 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 *
7725 * Produces instructions:
7726 * PUSHNR -1
7727 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007728 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7730 * - if beyond end, jump to "end"
7731 * - otherwise get item from list and push it
7732 * STORE var Store item in "var"
7733 * ... body ...
7734 * JUMP top Jump back to repeat
7735 * end: DROP Drop the result of "expr"
7736 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007737 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7738 * UNPACK 2 Split item in 2
7739 * STORE var1 Store item in "var1"
7740 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 */
7742 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007743compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007745 char_u *arg;
7746 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007747 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007749 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007750 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007751 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007752 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007755 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007757 lvar_T *loop_lvar; // loop iteration variable
7758 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007760 type_T *item_type = &t_any;
7761 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007762 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763
Bram Moolenaar792f7862020-11-23 08:31:18 +01007764 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007765 if (p == NULL)
7766 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007767 if (var_count == 0)
7768 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007769 else
7770 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771
7772 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007773 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007774 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7775 return NULL;
7776 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007778 if (*p == ':' && wp != p)
7779 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7780 else
7781 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782 return NULL;
7783 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007784 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007785 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7786 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007788 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7789 // instruction.
7790 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7791 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7792 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007793 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007794 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007795 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7796 .isn_arg.debug.dbg_break_lnum;
7797 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 scope = new_scope(cctx, FOR_SCOPE);
7800 if (scope == NULL)
7801 return NULL;
7802
Bram Moolenaar792f7862020-11-23 08:31:18 +01007803 // Reserve a variable to store the loop iteration counter and initialize it
7804 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007805 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7806 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007807 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007808 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007809 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007811 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007812 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813
7814 // compile "expr", it remains on the stack until "endfor"
7815 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007816 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007817 {
7818 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007820 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007821 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007823 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007824 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007826 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007827 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007829 semsg(_(e_for_loop_on_str_not_supported),
7830 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007831 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 return NULL;
7833 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007834
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007835 if (vartype->tt_type == VAR_STRING)
7836 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007837 else if (vartype->tt_type == VAR_BLOB)
7838 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007839 else if (vartype->tt_type == VAR_LIST
7840 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007841 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007842 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007843 item_type = vartype->tt_member;
7844 else if (vartype->tt_member->tt_type == VAR_LIST
7845 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007846 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007847 item_type = vartype->tt_member->tt_member;
7848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007850 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007851 generate_undo_cmdmods(cctx);
7852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007854 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007855
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007856 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857
Bram Moolenaar792f7862020-11-23 08:31:18 +01007858 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007859 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007860 {
7861 generate_UNPACK(cctx, var_count, semicolon);
7862 arg = skipwhite(arg + 1); // skip white after '['
7863
7864 // the list item is replaced by a number of items
7865 if (ga_grow(stack, var_count - 1) == FAIL)
7866 {
7867 drop_scope(cctx);
7868 return NULL;
7869 }
7870 --stack->ga_len;
7871 for (idx = 0; idx < var_count; ++idx)
7872 {
7873 ((type_T **)stack->ga_data)[stack->ga_len] =
7874 (semicolon && idx == 0) ? vartype : item_type;
7875 ++stack->ga_len;
7876 }
7877 }
7878
7879 for (idx = 0; idx < var_count; ++idx)
7880 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007881 assign_dest_T dest = dest_local;
7882 int opt_flags = 0;
7883 int vimvaridx = -1;
7884 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007885 type_T *lhs_type = &t_any;
7886 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007887
7888 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007889 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007890 name = vim_strnsave(arg, varlen);
7891 if (name == NULL)
7892 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007893 if (*p == ':')
7894 {
7895 p = skipwhite(p + 1);
7896 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7897 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007898
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007899 // TODO: script var not supported?
7900 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7901 &vimvaridx, &type, cctx) == FAIL)
7902 goto failed;
7903 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007904 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007905 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7906 0, 0, type, name) == FAIL)
7907 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007908 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007909 else if (varlen == 1 && *arg == '_')
7910 {
7911 // Assigning to "_": drop the value.
7912 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7913 goto failed;
7914 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007915 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007916 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007917 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007918 {
7919 semsg(_(e_variable_already_declared), arg);
7920 goto failed;
7921 }
7922
Bram Moolenaarea870692020-12-02 14:24:30 +01007923 if (STRNCMP(name, "s:", 2) == 0)
7924 {
7925 semsg(_(e_cannot_declare_script_variable_in_function), name);
7926 goto failed;
7927 }
7928
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007929 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007930 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007931 where.wt_variable = TRUE;
7932 if (lhs_type == &t_any)
7933 lhs_type = item_type;
7934 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02007935 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02007936 ? need_type(item_type, lhs_type,
7937 -1, 0, cctx, FALSE, FALSE)
7938 : check_type(lhs_type, item_type, TRUE, where))
7939 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007940 goto failed;
7941 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007942 if (var_lvar == NULL)
7943 // out of memory or used as an argument
7944 goto failed;
7945
7946 if (semicolon && idx == var_count - 1)
7947 var_lvar->lv_type = vartype;
7948 else
7949 var_lvar->lv_type = item_type;
7950 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7951 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007952
7953 if (*p == ',' || *p == ';')
7954 ++p;
7955 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007956 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007957 }
7958
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007959 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007960 {
7961 int save_prev_lnum = cctx->ctx_prev_lnum;
7962
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007963 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007964 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
7965 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007966 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007967 cctx->ctx_prev_lnum = save_prev_lnum;
7968 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007969
Bram Moolenaar792f7862020-11-23 08:31:18 +01007970 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007971
7972failed:
7973 vim_free(name);
7974 drop_scope(cctx);
7975 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976}
7977
7978/*
7979 * compile "endfor"
7980 */
7981 static char_u *
7982compile_endfor(char_u *arg, cctx_T *cctx)
7983{
7984 garray_T *instr = &cctx->ctx_instr;
7985 scope_T *scope = cctx->ctx_scope;
7986 forscope_T *forscope;
7987 isn_T *isn;
7988
Bram Moolenaarfa984412021-03-25 22:15:28 +01007989 if (misplaced_cmdmod(cctx))
7990 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007992 if (scope == NULL || scope->se_type != FOR_SCOPE)
7993 {
7994 emsg(_(e_for));
7995 return NULL;
7996 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007997 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007999 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000
8001 // At end of ":for" scope jump back to the FOR instruction.
8002 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8003
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008004 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8006 isn->isn_arg.forloop.for_end = instr->ga_len;
8007
8008 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008009 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010
8011 // Below the ":for" scope drop the "expr" list from the stack.
8012 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8013 return NULL;
8014
8015 vim_free(scope);
8016
8017 return arg;
8018}
8019
8020/*
8021 * compile "while expr"
8022 *
8023 * Produces instructions:
8024 * top: EVAL expr Push result of "expr"
8025 * JUMP_IF_FALSE end jump if false
8026 * ... body ...
8027 * JUMP top Jump back to repeat
8028 * end:
8029 *
8030 */
8031 static char_u *
8032compile_while(char_u *arg, cctx_T *cctx)
8033{
8034 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 scope_T *scope;
8036
8037 scope = new_scope(cctx, WHILE_SCOPE);
8038 if (scope == NULL)
8039 return NULL;
8040
Bram Moolenaara91a7132021-03-25 21:12:15 +01008041 // "endwhile" jumps back here, one before when profiling or using cmdmods
8042 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043
8044 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008045 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008047 if (!ends_excmd2(arg, skipwhite(p)))
8048 {
8049 semsg(_(e_trailing_arg), p);
8050 return NULL;
8051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052
Bram Moolenaar13106602020-10-04 16:06:05 +02008053 if (bool_on_stack(cctx) == FAIL)
8054 return FAIL;
8055
Bram Moolenaara91a7132021-03-25 21:12:15 +01008056 // CMDMOD_REV must come before the jump
8057 generate_undo_cmdmods(cctx);
8058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008060 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061 JUMP_IF_FALSE, cctx) == FAIL)
8062 return FAIL;
8063
8064 return p;
8065}
8066
8067/*
8068 * compile "endwhile"
8069 */
8070 static char_u *
8071compile_endwhile(char_u *arg, cctx_T *cctx)
8072{
8073 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008074 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075
Bram Moolenaarfa984412021-03-25 22:15:28 +01008076 if (misplaced_cmdmod(cctx))
8077 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008078 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8079 {
8080 emsg(_(e_while));
8081 return NULL;
8082 }
8083 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008084 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085
Bram Moolenaarf002a412021-01-24 13:34:18 +01008086#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008087 // count the endwhile before jumping
8088 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008089#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008092 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093
8094 // Fill in the "end" label in the WHILE statement so it can jump here.
8095 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008096 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8097 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098
8099 vim_free(scope);
8100
8101 return arg;
8102}
8103
8104/*
8105 * compile "continue"
8106 */
8107 static char_u *
8108compile_continue(char_u *arg, cctx_T *cctx)
8109{
8110 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008111 int try_scopes = 0;
8112 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113
8114 for (;;)
8115 {
8116 if (scope == NULL)
8117 {
8118 emsg(_(e_continue));
8119 return NULL;
8120 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008121 if (scope->se_type == FOR_SCOPE)
8122 {
8123 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008125 }
8126 if (scope->se_type == WHILE_SCOPE)
8127 {
8128 loop_label = scope->se_u.se_while.ws_top_label;
8129 break;
8130 }
8131 if (scope->se_type == TRY_SCOPE)
8132 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 scope = scope->se_outer;
8134 }
8135
Bram Moolenaarc150c092021-02-13 15:02:46 +01008136 if (try_scopes > 0)
8137 // Inside one or more try/catch blocks we first need to jump to the
8138 // "finally" or "endtry" to cleanup.
8139 generate_TRYCONT(cctx, try_scopes, loop_label);
8140 else
8141 // Jump back to the FOR or WHILE instruction.
8142 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 return arg;
8145}
8146
8147/*
8148 * compile "break"
8149 */
8150 static char_u *
8151compile_break(char_u *arg, cctx_T *cctx)
8152{
8153 scope_T *scope = cctx->ctx_scope;
8154 endlabel_T **el;
8155
8156 for (;;)
8157 {
8158 if (scope == NULL)
8159 {
8160 emsg(_(e_break));
8161 return NULL;
8162 }
8163 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8164 break;
8165 scope = scope->se_outer;
8166 }
8167
8168 // Jump to the end of the FOR or WHILE loop.
8169 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008170 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008172 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8174 return FAIL;
8175
8176 return arg;
8177}
8178
8179/*
8180 * compile "{" start of block
8181 */
8182 static char_u *
8183compile_block(char_u *arg, cctx_T *cctx)
8184{
8185 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8186 return NULL;
8187 return skipwhite(arg + 1);
8188}
8189
8190/*
8191 * compile end of block: drop one scope
8192 */
8193 static void
8194compile_endblock(cctx_T *cctx)
8195{
8196 scope_T *scope = cctx->ctx_scope;
8197
8198 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008199 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 vim_free(scope);
8201}
8202
8203/*
8204 * compile "try"
8205 * Creates a new scope for the try-endtry, pointing to the first catch and
8206 * finally.
8207 * Creates another scope for the "try" block itself.
8208 * TRY instruction sets up exception handling at runtime.
8209 *
8210 * "try"
8211 * TRY -> catch1, -> finally push trystack entry
8212 * ... try block
8213 * "throw {exception}"
8214 * EVAL {exception}
8215 * THROW create exception
8216 * ... try block
8217 * " catch {expr}"
8218 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008219 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220 * EVAL {expr}
8221 * MATCH
8222 * JUMP nomatch -> catch2
8223 * CATCH remove exception
8224 * ... catch block
8225 * " catch"
8226 * JUMP -> finally
8227 * catch2: CATCH remove exception
8228 * ... catch block
8229 * " finally"
8230 * finally:
8231 * ... finally block
8232 * " endtry"
8233 * ENDTRY pop trystack entry, may rethrow
8234 */
8235 static char_u *
8236compile_try(char_u *arg, cctx_T *cctx)
8237{
8238 garray_T *instr = &cctx->ctx_instr;
8239 scope_T *try_scope;
8240 scope_T *scope;
8241
Bram Moolenaarfa984412021-03-25 22:15:28 +01008242 if (misplaced_cmdmod(cctx))
8243 return NULL;
8244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008245 // scope that holds the jumps that go to catch/finally/endtry
8246 try_scope = new_scope(cctx, TRY_SCOPE);
8247 if (try_scope == NULL)
8248 return NULL;
8249
Bram Moolenaar69f70502021-01-01 16:10:46 +01008250 if (cctx->ctx_skip != SKIP_YES)
8251 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008252 isn_T *isn;
8253
8254 // "try_catch" is set when the first ":catch" is found or when no catch
8255 // is found and ":finally" is found.
8256 // "try_finally" is set when ":finally" is found
8257 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008258 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008259 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8260 return NULL;
8261 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8262 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008263 return NULL;
8264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265
8266 // scope for the try block itself
8267 scope = new_scope(cctx, BLOCK_SCOPE);
8268 if (scope == NULL)
8269 return NULL;
8270
8271 return arg;
8272}
8273
8274/*
8275 * compile "catch {expr}"
8276 */
8277 static char_u *
8278compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8279{
8280 scope_T *scope = cctx->ctx_scope;
8281 garray_T *instr = &cctx->ctx_instr;
8282 char_u *p;
8283 isn_T *isn;
8284
Bram Moolenaarfa984412021-03-25 22:15:28 +01008285 if (misplaced_cmdmod(cctx))
8286 return NULL;
8287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008288 // end block scope from :try or :catch
8289 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8290 compile_endblock(cctx);
8291 scope = cctx->ctx_scope;
8292
8293 // Error if not in a :try scope
8294 if (scope == NULL || scope->se_type != TRY_SCOPE)
8295 {
8296 emsg(_(e_catch));
8297 return NULL;
8298 }
8299
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008300 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008302 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 return NULL;
8304 }
8305
Bram Moolenaar69f70502021-01-01 16:10:46 +01008306 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008308#ifdef FEAT_PROFILE
8309 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008310 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008311 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008312 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008313 .isn_type == ISN_PROF_START)
8314 --instr->ga_len;
8315#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008316 // Jump from end of previous block to :finally or :endtry
8317 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8318 JUMP_ALWAYS, cctx) == FAIL)
8319 return NULL;
8320
8321 // End :try or :catch scope: set value in ISN_TRY instruction
8322 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008323 if (isn->isn_arg.try.try_ref->try_catch == 0)
8324 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008325 if (scope->se_u.se_try.ts_catch_label != 0)
8326 {
8327 // Previous catch without match jumps here
8328 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8329 isn->isn_arg.jump.jump_where = instr->ga_len;
8330 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008331#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008332 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008333 {
8334 // a "throw" that jumps here needs to be counted
8335 generate_instr(cctx, ISN_PROF_END);
8336 // the "catch" is also counted
8337 generate_instr(cctx, ISN_PROF_START);
8338 }
8339#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008340 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008341 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008342 }
8343
8344 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008345 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008347 scope->se_u.se_try.ts_caught_all = TRUE;
8348 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 }
8350 else
8351 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008352 char_u *end;
8353 char_u *pat;
8354 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008355 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008356 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008358 // Push v:exception, push {expr} and MATCH
8359 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8360
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008361 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008362 if (*end != *p)
8363 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008364 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008365 vim_free(tofree);
8366 return FAIL;
8367 }
8368 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008369 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008370 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008371 len = (int)(end - tofree);
8372 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008373 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008374 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008375 if (pat == NULL)
8376 return FAIL;
8377 if (generate_PUSHS(cctx, pat) == FAIL)
8378 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008380 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8381 return NULL;
8382
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008383 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8385 return NULL;
8386 }
8387
Bram Moolenaar69f70502021-01-01 16:10:46 +01008388 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389 return NULL;
8390
8391 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8392 return NULL;
8393 return p;
8394}
8395
8396 static char_u *
8397compile_finally(char_u *arg, cctx_T *cctx)
8398{
8399 scope_T *scope = cctx->ctx_scope;
8400 garray_T *instr = &cctx->ctx_instr;
8401 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008402 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008403
Bram Moolenaarfa984412021-03-25 22:15:28 +01008404 if (misplaced_cmdmod(cctx))
8405 return NULL;
8406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 // end block scope from :try or :catch
8408 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8409 compile_endblock(cctx);
8410 scope = cctx->ctx_scope;
8411
8412 // Error if not in a :try scope
8413 if (scope == NULL || scope->se_type != TRY_SCOPE)
8414 {
8415 emsg(_(e_finally));
8416 return NULL;
8417 }
8418
8419 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008420 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008421 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 {
8423 emsg(_(e_finally_dup));
8424 return NULL;
8425 }
8426
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008427 this_instr = instr->ga_len;
8428#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008429 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008430 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008431 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008432 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008433 // jump to the profile start of the "finally"
8434 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008435
8436 // jump to the profile end above it
8437 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8438 .isn_type == ISN_PROF_END)
8439 --this_instr;
8440 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008441#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008443 // Fill in the "end" label in jumps at the end of the blocks.
8444 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8445 this_instr, cctx);
8446
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008447 // If there is no :catch then an exception jumps to :finally.
8448 if (isn->isn_arg.try.try_ref->try_catch == 0)
8449 isn->isn_arg.try.try_ref->try_catch = this_instr;
8450 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008451 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 {
8453 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008454 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008455 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008456 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008458 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8459 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008461 // TODO: set index in ts_finally_label jumps
8462
8463 return arg;
8464}
8465
8466 static char_u *
8467compile_endtry(char_u *arg, cctx_T *cctx)
8468{
8469 scope_T *scope = cctx->ctx_scope;
8470 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008471 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472
Bram Moolenaarfa984412021-03-25 22:15:28 +01008473 if (misplaced_cmdmod(cctx))
8474 return NULL;
8475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 // end block scope from :catch or :finally
8477 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8478 compile_endblock(cctx);
8479 scope = cctx->ctx_scope;
8480
8481 // Error if not in a :try scope
8482 if (scope == NULL || scope->se_type != TRY_SCOPE)
8483 {
8484 if (scope == NULL)
8485 emsg(_(e_no_endtry));
8486 else if (scope->se_type == WHILE_SCOPE)
8487 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008488 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008489 emsg(_(e_endfor));
8490 else
8491 emsg(_(e_endif));
8492 return NULL;
8493 }
8494
Bram Moolenaarc150c092021-02-13 15:02:46 +01008495 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008496 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008497 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008498 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8499 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008500 {
8501 emsg(_(e_missing_catch_or_finally));
8502 return NULL;
8503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008505#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008506 if (cctx->ctx_compile_type == CT_PROFILE
8507 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008508 .isn_type == ISN_PROF_START)
8509 // move the profile start after "endtry" so that it's not counted when
8510 // the exception is rethrown.
8511 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008512#endif
8513
Bram Moolenaar69f70502021-01-01 16:10:46 +01008514 // Fill in the "end" label in jumps at the end of the blocks, if not
8515 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008516 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8517 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008518
Bram Moolenaar69f70502021-01-01 16:10:46 +01008519 if (scope->se_u.se_try.ts_catch_label != 0)
8520 {
8521 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008522 isn_T *isn = ((isn_T *)instr->ga_data)
8523 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008524 isn->isn_arg.jump.jump_where = instr->ga_len;
8525 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008526 }
8527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 compile_endblock(cctx);
8529
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008530 if (cctx->ctx_skip != SKIP_YES)
8531 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008532 // End :catch or :finally scope: set instruction index in ISN_TRY
8533 // instruction
8534 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008535 if (cctx->ctx_skip != SKIP_YES
8536 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8537 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008538#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008539 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008540 generate_instr(cctx, ISN_PROF_START);
8541#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 return arg;
8544}
8545
8546/*
8547 * compile "throw {expr}"
8548 */
8549 static char_u *
8550compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8551{
8552 char_u *p = skipwhite(arg);
8553
Bram Moolenaara5565e42020-05-09 15:44:01 +02008554 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008555 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008556 if (cctx->ctx_skip == SKIP_YES)
8557 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008558 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 return NULL;
8560 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8561 return NULL;
8562
8563 return p;
8564}
8565
Bram Moolenaarc3235272021-07-10 19:42:03 +02008566 static char_u *
8567compile_eval(char_u *arg, cctx_T *cctx)
8568{
8569 char_u *p = arg;
8570 int name_only;
8571 char_u *alias;
8572 long lnum = SOURCING_LNUM;
8573
8574 // find_ex_command() will consider a variable name an expression, assuming
8575 // that something follows on the next line. Check that something actually
8576 // follows, otherwise it's probably a misplaced command.
8577 get_name_len(&p, &alias, FALSE, FALSE);
8578 name_only = ends_excmd2(arg, skipwhite(p));
8579 vim_free(alias);
8580
8581 p = arg;
8582 if (compile_expr0(&p, cctx) == FAIL)
8583 return NULL;
8584
8585 if (name_only && lnum == SOURCING_LNUM)
8586 {
8587 semsg(_(e_expression_without_effect_str), arg);
8588 return NULL;
8589 }
8590
8591 // drop the result
8592 generate_instr_drop(cctx, ISN_DROP, 1);
8593
8594 return skipwhite(p);
8595}
8596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597/*
8598 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008599 * compile "echomsg expr"
8600 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008601 * compile "execute expr"
8602 */
8603 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008604compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008605{
8606 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008607 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008608 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008609 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008610 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008611 garray_T *stack = &cctx->ctx_type_stack;
8612 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008613
8614 for (;;)
8615 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008616 if (ends_excmd2(prev, p))
8617 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008618 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008619 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008620 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008621
8622 if (cctx->ctx_skip != SKIP_YES)
8623 {
8624 // check for non-void type
8625 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8626 if (type->tt_type == VAR_VOID)
8627 {
8628 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8629 return NULL;
8630 }
8631 }
8632
Bram Moolenaarad39c092020-02-26 18:23:43 +01008633 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008634 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008635 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008636 }
8637
Bram Moolenaare4984292020-12-13 14:19:25 +01008638 if (count > 0)
8639 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008640 long save_lnum = cctx->ctx_lnum;
8641
8642 // Use the line number where the command started.
8643 cctx->ctx_lnum = start_ctx_lnum;
8644
Bram Moolenaare4984292020-12-13 14:19:25 +01008645 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8646 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8647 else if (cmdidx == CMD_execute)
8648 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8649 else if (cmdidx == CMD_echomsg)
8650 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8651 else
8652 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008653
8654 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656 return p;
8657}
8658
8659/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008660 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008661 * instruction to compute it and return OK.
8662 * Otherwise return FAIL, the caller must deal with any range.
8663 */
8664 static int
8665compile_variable_range(exarg_T *eap, cctx_T *cctx)
8666{
8667 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8668 char_u *p = skipdigits(eap->cmd);
8669
8670 if (p == range_end)
8671 return FAIL;
8672 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8673}
8674
8675/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008676 * :put r
8677 * :put ={expr}
8678 */
8679 static char_u *
8680compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8681{
8682 char_u *line = arg;
8683 linenr_T lnum;
8684 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008685 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008686
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008687 eap->regname = *line;
8688
8689 if (eap->regname == '=')
8690 {
8691 char_u *p = line + 1;
8692
8693 if (compile_expr0(&p, cctx) == FAIL)
8694 return NULL;
8695 line = p;
8696 }
8697 else if (eap->regname != NUL)
8698 ++line;
8699
Bram Moolenaar08597872020-12-10 19:43:40 +01008700 if (compile_variable_range(eap, cctx) == OK)
8701 {
8702 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8703 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008704 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008705 {
8706 // Either no range or a number.
8707 // "errormsg" will not be set because the range is ADDR_LINES.
8708 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008709 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008710 return NULL;
8711 if (eap->addr_count == 0)
8712 lnum = -1;
8713 else
8714 lnum = eap->line2;
8715 if (above)
8716 --lnum;
8717 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008718
8719 generate_PUT(cctx, eap->regname, lnum);
8720 return line;
8721}
8722
8723/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008724 * A command that is not compiled, execute with legacy code.
8725 */
8726 static char_u *
8727compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8728{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008729 char_u *p;
8730 int has_expr = FALSE;
8731 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008732
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008733 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008734 goto theend;
8735
Bram Moolenaare729ce22021-06-06 21:38:09 +02008736 // If there was a prececing command modifier, drop it and include it in the
8737 // EXEC command.
8738 if (cctx->ctx_has_cmdmod)
8739 {
8740 garray_T *instr = &cctx->ctx_instr;
8741 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8742
8743 if (isn->isn_type == ISN_CMDMOD)
8744 {
8745 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8746 ->cmod_filter_regmatch.regprog);
8747 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8748 --instr->ga_len;
8749 cctx->ctx_has_cmdmod = FALSE;
8750 }
8751 }
8752
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008753 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008754 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008755 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008756 int usefilter = FALSE;
8757
8758 has_expr = argt & (EX_XFILE | EX_EXPAND);
8759
8760 // If the command can be followed by a bar, find the bar and truncate
8761 // it, so that the following command can be compiled.
8762 // The '|' is overwritten with a NUL, it is put back below.
8763 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8764 && *eap->arg == '!')
8765 // :w !filter or :r !filter or :r! filter
8766 usefilter = TRUE;
8767 if ((argt & EX_TRLBAR) && !usefilter)
8768 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008769 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008770 separate_nextcmd(eap);
8771 if (eap->nextcmd != NULL)
8772 nextcmd = eap->nextcmd;
8773 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008774 else if (eap->cmdidx == CMD_wincmd)
8775 {
8776 p = eap->arg;
8777 if (*p != NUL)
8778 ++p;
8779 if (*p == 'g' || *p == Ctrl_G)
8780 ++p;
8781 p = skipwhite(p);
8782 if (*p == '|')
8783 {
8784 *p = NUL;
8785 nextcmd = p + 1;
8786 }
8787 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008788 }
8789
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008790 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8791 {
8792 // expand filename in "syntax include [@group] filename"
8793 has_expr = TRUE;
8794 eap->arg = skipwhite(eap->arg + 7);
8795 if (*eap->arg == '@')
8796 eap->arg = skiptowhite(eap->arg);
8797 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008798
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008799 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8800 && STRLEN(eap->arg) > 4)
8801 {
8802 int delim = *eap->arg;
8803
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008804 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008805 if (*p == delim)
8806 {
8807 eap->arg = p + 1;
8808 has_expr = TRUE;
8809 }
8810 }
8811
Bram Moolenaarecac5912021-01-05 19:23:28 +01008812 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8813 {
8814 // TODO: should only expand when appropriate for the command
8815 eap->arg = skiptowhite(eap->arg);
8816 has_expr = TRUE;
8817 }
8818
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008819 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008820 {
8821 int count = 0;
8822 char_u *start = skipwhite(line);
8823
8824 // :cmd xxx`=expr1`yyy`=expr2`zzz
8825 // PUSHS ":cmd xxx"
8826 // eval expr1
8827 // PUSHS "yyy"
8828 // eval expr2
8829 // PUSHS "zzz"
8830 // EXECCONCAT 5
8831 for (;;)
8832 {
8833 if (p > start)
8834 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008835 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008836 ++count;
8837 }
8838 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008839 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008840 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008841 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008842 ++count;
8843 p = skipwhite(p);
8844 if (*p != '`')
8845 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008846 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008847 return NULL;
8848 }
8849 start = p + 1;
8850
8851 p = (char_u *)strstr((char *)start, "`=");
8852 if (p == NULL)
8853 {
8854 if (*skipwhite(start) != NUL)
8855 {
8856 generate_PUSHS(cctx, vim_strsave(start));
8857 ++count;
8858 }
8859 break;
8860 }
8861 }
8862 generate_EXECCONCAT(cctx, count);
8863 }
8864 else
8865 generate_EXEC(cctx, line);
8866
8867theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008868 if (*nextcmd != NUL)
8869 {
8870 // the parser expects a pointer to the bar, put it back
8871 --nextcmd;
8872 *nextcmd = '|';
8873 }
8874
8875 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008876}
8877
Bram Moolenaar20677332021-06-06 17:02:53 +02008878/*
8879 * A script command with heredoc, e.g.
8880 * ruby << EOF
8881 * command
8882 * EOF
8883 * Has been turned into one long line with NL characters by
8884 * get_function_body():
8885 * ruby << EOF<NL> command<NL>EOF
8886 */
8887 static char_u *
8888compile_script(char_u *line, cctx_T *cctx)
8889{
8890 if (cctx->ctx_skip != SKIP_YES)
8891 {
8892 isn_T *isn;
8893
8894 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8895 return NULL;
8896 isn->isn_arg.string = vim_strsave(line);
8897 }
8898 return (char_u *)"";
8899}
8900
Bram Moolenaar8238f082021-04-20 21:10:48 +02008901
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008902/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008903 * :s/pat/repl/
8904 */
8905 static char_u *
8906compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8907{
8908 char_u *cmd = eap->arg;
8909 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8910
8911 if (expr != NULL)
8912 {
8913 int delimiter = *cmd++;
8914
8915 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008916 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008917 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8918 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008919 garray_T save_ga = cctx->ctx_instr;
8920 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008921 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008922 int trailing_error;
8923 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008924 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008925 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008926
8927 cmd += 3;
8928 end = skip_substitute(cmd, delimiter);
8929
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008930 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008931 // labels are correct.
8932 cctx->ctx_instr.ga_len = 0;
8933 cctx->ctx_instr.ga_maxlen = 0;
8934 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008935 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008936 if (end[-1] == NUL)
8937 end[-1] = delimiter;
8938 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008939 trailing_error = *cmd != delimiter && *cmd != NUL;
8940
Bram Moolenaar169502f2021-04-21 12:19:35 +02008941 if (expr_res == FAIL || trailing_error
8942 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008943 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008944 if (trailing_error)
8945 semsg(_(e_trailing_arg), cmd);
8946 clear_instr_ga(&cctx->ctx_instr);
8947 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008948 return NULL;
8949 }
8950
Bram Moolenaar8238f082021-04-20 21:10:48 +02008951 // Move the generated instructions into the ISN_SUBSTITUTE
8952 // instructions, then restore the list of instructions before
8953 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008954 instr_count = cctx->ctx_instr.ga_len;
8955 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008956 instr[instr_count].isn_type = ISN_FINISH;
8957
8958 cctx->ctx_instr = save_ga;
8959 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8960 {
8961 int idx;
8962
8963 for (idx = 0; idx < instr_count; ++idx)
8964 delete_instr(instr + idx);
8965 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008966 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008967 }
8968 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8969 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008970
8971 // skip over flags
8972 if (*end == '&')
8973 ++end;
8974 while (ASCII_ISALPHA(*end) || *end == '#')
8975 ++end;
8976 return end;
8977 }
8978 }
8979
8980 return compile_exec(arg, eap, cctx);
8981}
8982
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008983 static char_u *
8984compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8985{
8986 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008987 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008988
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008989 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008990 {
8991 if (STRNCMP(arg, "END", 3) == 0)
8992 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008993 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008994 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008995 // First load the current variable value.
8996 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8997 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008998 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008999 }
9000
9001 // Gets the redirected text and put it on the stack, then store it
9002 // in the variable.
9003 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9004
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009005 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009006 generate_instr_drop(cctx, ISN_CONCAT, 1);
9007
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009008 if (lhs->lhs_has_index)
9009 {
9010 // Use the info in "lhs" to store the value at the index in the
9011 // list or dict.
9012 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9013 &t_string, cctx) == FAIL)
9014 return NULL;
9015 }
9016 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009017 return NULL;
9018
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009019 VIM_CLEAR(lhs->lhs_name);
9020 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009021 return arg + 3;
9022 }
9023 emsg(_(e_cannot_nest_redir));
9024 return NULL;
9025 }
9026
9027 if (arg[0] == '=' && arg[1] == '>')
9028 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009029 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009030
9031 // redirect to a variable is compiled
9032 arg += 2;
9033 if (*arg == '>')
9034 {
9035 ++arg;
9036 append = TRUE;
9037 }
9038 arg = skipwhite(arg);
9039
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009040 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009041 FALSE, FALSE, 1, cctx) == FAIL)
9042 return NULL;
9043 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009044 lhs->lhs_append = append;
9045 if (lhs->lhs_has_index)
9046 {
9047 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9048 if (lhs->lhs_whole == NULL)
9049 return NULL;
9050 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009051
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009052 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009053 }
9054
9055 // other redirects are handled like at script level
9056 return compile_exec(line, eap, cctx);
9057}
9058
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009059#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009060 static char_u *
9061compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9062{
9063 isn_T *isn;
9064 char_u *p;
9065
9066 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9067 if (isn == NULL)
9068 return NULL;
9069 isn->isn_arg.number = eap->cmdidx;
9070
9071 p = eap->arg;
9072 if (compile_expr0(&p, cctx) == FAIL)
9073 return NULL;
9074
9075 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9076 if (isn == NULL)
9077 return NULL;
9078 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9079 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9080 return NULL;
9081 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9082 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9083 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9084
9085 return p;
9086}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009087#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009088
Bram Moolenaar4c137212021-04-19 16:48:48 +02009089/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009090 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009091 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009092 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009093 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009094add_def_function(ufunc_T *ufunc)
9095{
9096 dfunc_T *dfunc;
9097
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009098 if (def_functions.ga_len == 0)
9099 {
9100 // The first position is not used, so that a zero uf_dfunc_idx means it
9101 // wasn't set.
9102 if (ga_grow(&def_functions, 1) == FAIL)
9103 return FAIL;
9104 ++def_functions.ga_len;
9105 }
9106
Bram Moolenaar09689a02020-05-09 22:50:08 +02009107 // Add the function to "def_functions".
9108 if (ga_grow(&def_functions, 1) == FAIL)
9109 return FAIL;
9110 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9111 CLEAR_POINTER(dfunc);
9112 dfunc->df_idx = def_functions.ga_len;
9113 ufunc->uf_dfunc_idx = dfunc->df_idx;
9114 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009115 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009116 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009117 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009118 ++def_functions.ga_len;
9119 return OK;
9120}
9121
9122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 * After ex_function() has collected all the function lines: parse and compile
9124 * the lines into instructions.
9125 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009126 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9127 * the return statement (used for lambda). When uf_ret_type is already set
9128 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009129 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009130 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009131 * This can be used recursively through compile_lambda(), which may reallocate
9132 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009133 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009134 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009135 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009136compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009137 ufunc_T *ufunc,
9138 int check_return_type,
9139 compiletype_T compile_type,
9140 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009141{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009142 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009143 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009144 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009145 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009146 cctx_T cctx;
9147 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009148 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009149 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009150 int ret = FAIL;
9151 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009152 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009153 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009154 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009155 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009156#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009157 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009158#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009159 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009160
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009161 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009162 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009163 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009164 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009165 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9166 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009167 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009168
9169 switch (compile_type)
9170 {
9171 case CT_PROFILE:
9172#ifdef FEAT_PROFILE
9173 instr_dest = dfunc->df_instr_prof; break;
9174#endif
9175 case CT_NONE: instr_dest = dfunc->df_instr; break;
9176 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9177 }
9178 if (instr_dest != NULL)
9179 // Was compiled in this mode before: Free old instructions.
9180 delete_def_function_contents(dfunc, FALSE);
9181 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009182 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009183 else
9184 {
9185 if (add_def_function(ufunc) == FAIL)
9186 return FAIL;
9187 new_def_function = TRUE;
9188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009189
Bram Moolenaar985116a2020-07-12 17:31:09 +02009190 ufunc->uf_def_status = UF_COMPILING;
9191
Bram Moolenaara80faa82020-04-12 19:37:17 +02009192 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009193
Bram Moolenaare99d4222021-06-13 14:01:26 +02009194 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009195 cctx.ctx_ufunc = ufunc;
9196 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009197 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009198 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9199 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9200 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9201 cctx.ctx_type_list = &ufunc->uf_type_list;
9202 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9203 instr = &cctx.ctx_instr;
9204
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009205 // Set the context to the function, it may be compiled when called from
9206 // another script. Set the script version to the most modern one.
9207 // The line number will be set in next_line_from_context().
9208 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9210
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009211 // Don't use the flag from ":legacy" here.
9212 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9213
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009214 // Make sure error messages are OK.
9215 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9216 if (do_estack_push)
9217 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009218 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009219
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009220 if (ufunc->uf_def_args.ga_len > 0)
9221 {
9222 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009223 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009224 int i;
9225 char_u *arg;
9226 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009227 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009228
9229 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009230 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009231 for (i = 0; i < count; ++i)
9232 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009233 garray_T *stack = &cctx.ctx_type_stack;
9234 type_T *val_type;
9235 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009236 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009237 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009238 int jump_instr_idx = instr->ga_len;
9239 isn_T *isn;
9240
9241 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9242 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9243 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009244
9245 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009246 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009247
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009248 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009249 r = compile_expr0(&arg, &cctx);
9250
Bram Moolenaar12bce952021-03-11 20:04:04 +01009251 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009252 goto erret;
9253
9254 // If no type specified use the type of the default value.
9255 // Otherwise check that the default value type matches the
9256 // specified type.
9257 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009258 where.wt_index = arg_idx + 1;
9259 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009260 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009261 {
9262 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009263 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009264 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009265 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009266 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009267 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009268
9269 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009270 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009271
9272 // set instruction index in JUMP_IF_ARG_SET to here
9273 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9274 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009275 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009276
9277 if (did_set_arg_type)
9278 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009279 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009280 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009281
9282 /*
9283 * Loop over all the lines of the function and generate instructions.
9284 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285 for (;;)
9286 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009287 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009288 int starts_with_colon = FALSE;
9289 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009290 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009291
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009292 // Bail out on the first error to avoid a flood of errors and report
9293 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009294 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009295 goto erret;
9296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009297 if (line != NULL && *line == '|')
9298 // the line continues after a '|'
9299 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009300 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009301 && !(*line == '#' && (line == cctx.ctx_line_start
9302 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009303 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009304 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009305 goto erret;
9306 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009307 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9308 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009309 else
9310 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009311 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009312 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009313 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009314 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009315#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009316 if (cctx.ctx_skip != SKIP_YES)
9317 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009318#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009319 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009320 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009321 // Make a copy, splitting off nextcmd and removing trailing spaces
9322 // may change it.
9323 if (line != NULL)
9324 {
9325 line = vim_strsave(line);
9326 vim_free(line_to_free);
9327 line_to_free = line;
9328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 }
9330
Bram Moolenaara80faa82020-04-12 19:37:17 +02009331 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009332 ea.cmdlinep = &line;
9333 ea.cmd = skipwhite(line);
9334
Bram Moolenaarb2049902021-01-24 12:53:53 +01009335 if (*ea.cmd == '#')
9336 {
9337 // "#" starts a comment
9338 line = (char_u *)"";
9339 continue;
9340 }
9341
Bram Moolenaarf002a412021-01-24 13:34:18 +01009342#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009343 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9344 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009345 {
9346 may_generate_prof_end(&cctx, prof_lnum);
9347
9348 prof_lnum = cctx.ctx_lnum;
9349 generate_instr(&cctx, ISN_PROF_START);
9350 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009351#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009352 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9353 && cctx.ctx_skip != SKIP_YES)
9354 {
9355 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009356 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009357 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009358 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009359
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009360 // Some things can be recognized by the first character.
9361 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009363 case '}':
9364 {
9365 // "}" ends a block scope
9366 scopetype_T stype = cctx.ctx_scope == NULL
9367 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009368
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009369 if (stype == BLOCK_SCOPE)
9370 {
9371 compile_endblock(&cctx);
9372 line = ea.cmd;
9373 }
9374 else
9375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009376 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009377 goto erret;
9378 }
9379 if (line != NULL)
9380 line = skipwhite(ea.cmd + 1);
9381 continue;
9382 }
9383
9384 case '{':
9385 // "{" starts a block scope
9386 // "{'a': 1}->func() is something else
9387 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9388 {
9389 line = compile_block(ea.cmd, &cctx);
9390 continue;
9391 }
9392 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009393 }
9394
9395 /*
9396 * COMMAND MODIFIERS
9397 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009398 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009399 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9400 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009401 {
9402 if (errormsg != NULL)
9403 goto erret;
9404 // empty line or comment
9405 line = (char_u *)"";
9406 continue;
9407 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009408 generate_cmdmods(&cctx, &local_cmdmod);
9409 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009410
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009411 // Check if there was a colon after the last command modifier or before
9412 // the current position.
9413 for (p = ea.cmd; p >= line; --p)
9414 {
9415 if (*p == ':')
9416 starts_with_colon = TRUE;
9417 if (p < ea.cmd && !VIM_ISWHITE(*p))
9418 break;
9419 }
9420
Bram Moolenaarce024c32021-06-26 13:00:49 +02009421 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009422 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009423 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009424 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009425 if (checkforcmd(&ea.cmd, "call", 3))
9426 {
9427 if (*ea.cmd == '(')
9428 // not for "call()"
9429 ea.cmd = p;
9430 else
9431 ea.cmd = skipwhite(ea.cmd);
9432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009433
Bram Moolenaarce024c32021-06-26 13:00:49 +02009434 if (!starts_with_colon)
9435 {
9436 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009437
Bram Moolenaarce024c32021-06-26 13:00:49 +02009438 // Check for assignment after command modifiers.
9439 assign = may_compile_assignment(&ea, &line, &cctx);
9440 if (assign == OK)
9441 goto nextline;
9442 if (assign == FAIL)
9443 goto erret;
9444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009445 }
9446
9447 /*
9448 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009449 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009450 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009451 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009452 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009453 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9454 && (starts_with_colon || !(*cmd == '\''
9455 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009456 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009457 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009458 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009459 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009460 if (!starts_with_colon)
9461 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009462 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009463 goto erret;
9464 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009465 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009466 if (ends_excmd2(line, ea.cmd))
9467 {
9468 // A range without a command: jump to the line.
9469 // TODO: compile to a more efficient command, possibly
9470 // calling parse_cmd_address().
9471 ea.cmdidx = CMD_SIZE;
9472 line = compile_exec(line, &ea, &cctx);
9473 goto nextline;
9474 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009475 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009476 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009477 p = find_ex_command(&ea, NULL, starts_with_colon
9478 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009479
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009480 if (p == NULL)
9481 {
9482 if (cctx.ctx_skip != SKIP_YES)
9483 emsg(_(e_ambiguous_use_of_user_defined_command));
9484 goto erret;
9485 }
9486
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009487 // When using ":legacy cmd" always use compile_exec().
9488 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009489 {
9490 char_u *start = ea.cmd;
9491
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009492 switch (ea.cmdidx)
9493 {
9494 case CMD_if:
9495 case CMD_elseif:
9496 case CMD_else:
9497 case CMD_endif:
9498 case CMD_for:
9499 case CMD_endfor:
9500 case CMD_continue:
9501 case CMD_break:
9502 case CMD_while:
9503 case CMD_endwhile:
9504 case CMD_try:
9505 case CMD_catch:
9506 case CMD_finally:
9507 case CMD_endtry:
9508 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9509 goto erret;
9510 default: break;
9511 }
9512
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009513 // ":legacy return expr" needs to be handled differently.
9514 if (checkforcmd(&start, "return", 4))
9515 ea.cmdidx = CMD_return;
9516 else
9517 ea.cmdidx = CMD_legacy;
9518 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009520 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9521 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009522 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009523 {
9524 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009525 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009526 }
9527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009528 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009529 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009530 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009531 // CMD_var cannot happen, compile_assignment() above would be
9532 // used. Most likely an assignment to a non-existing variable.
9533 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009534 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009536 }
9537
Bram Moolenaar3988f642020-08-27 22:43:03 +02009538 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009539 && ea.cmdidx != CMD_elseif
9540 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009541 && ea.cmdidx != CMD_endif
9542 && ea.cmdidx != CMD_endfor
9543 && ea.cmdidx != CMD_endwhile
9544 && ea.cmdidx != CMD_catch
9545 && ea.cmdidx != CMD_finally
9546 && ea.cmdidx != CMD_endtry)
9547 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009548 emsg(_(e_unreachable_code_after_return));
9549 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009550 }
9551
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009552 p = skipwhite(p);
9553 if (ea.cmdidx != CMD_SIZE
9554 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9555 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009556 if (ea.cmdidx >= 0)
9557 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009558 if ((ea.argt & EX_BANG) && *p == '!')
9559 {
9560 ea.forceit = TRUE;
9561 p = skipwhite(p + 1);
9562 }
9563 }
9564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009565 switch (ea.cmdidx)
9566 {
9567 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009568 ea.arg = p;
9569 line = compile_nested_function(&ea, &cctx);
9570 break;
9571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009572 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009573 // TODO: should we allow this, e.g. to declare a global
9574 // function?
9575 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009576 goto erret;
9577
9578 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009579 line = compile_return(p, check_return_type,
9580 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009581 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009582 break;
9583
9584 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009585 emsg(_(e_cannot_use_let_in_vim9_script));
9586 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009587 case CMD_var:
9588 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009589 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009590 case CMD_increment:
9591 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009592 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009593 if (line == p)
9594 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 break;
9596
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009597 case CMD_unlet:
9598 case CMD_unlockvar:
9599 case CMD_lockvar:
9600 line = compile_unletlock(p, &ea, &cctx);
9601 break;
9602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009603 case CMD_import:
9604 line = compile_import(p, &cctx);
9605 break;
9606
9607 case CMD_if:
9608 line = compile_if(p, &cctx);
9609 break;
9610 case CMD_elseif:
9611 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009612 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613 break;
9614 case CMD_else:
9615 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009616 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009617 break;
9618 case CMD_endif:
9619 line = compile_endif(p, &cctx);
9620 break;
9621
9622 case CMD_while:
9623 line = compile_while(p, &cctx);
9624 break;
9625 case CMD_endwhile:
9626 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009627 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009628 break;
9629
9630 case CMD_for:
9631 line = compile_for(p, &cctx);
9632 break;
9633 case CMD_endfor:
9634 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009635 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 break;
9637 case CMD_continue:
9638 line = compile_continue(p, &cctx);
9639 break;
9640 case CMD_break:
9641 line = compile_break(p, &cctx);
9642 break;
9643
9644 case CMD_try:
9645 line = compile_try(p, &cctx);
9646 break;
9647 case CMD_catch:
9648 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009649 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009650 break;
9651 case CMD_finally:
9652 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009653 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009654 break;
9655 case CMD_endtry:
9656 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009657 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 break;
9659 case CMD_throw:
9660 line = compile_throw(p, &cctx);
9661 break;
9662
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009663 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009664 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009665 break;
9666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009667 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009668 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009669 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009670 case CMD_echomsg:
9671 case CMD_echoerr:
9672 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009673 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009674
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009675 case CMD_put:
9676 ea.cmd = cmd;
9677 line = compile_put(p, &ea, &cctx);
9678 break;
9679
Bram Moolenaar4c137212021-04-19 16:48:48 +02009680 case CMD_substitute:
9681 if (cctx.ctx_skip == SKIP_YES)
9682 line = (char_u *)"";
9683 else
9684 {
9685 ea.arg = p;
9686 line = compile_substitute(line, &ea, &cctx);
9687 }
9688 break;
9689
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009690 case CMD_redir:
9691 ea.arg = p;
9692 line = compile_redir(line, &ea, &cctx);
9693 break;
9694
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009695 case CMD_cexpr:
9696 case CMD_lexpr:
9697 case CMD_caddexpr:
9698 case CMD_laddexpr:
9699 case CMD_cgetexpr:
9700 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009701#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009702 ea.arg = p;
9703 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009704#else
9705 ex_ni(&ea);
9706 line = NULL;
9707#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009708 break;
9709
Bram Moolenaar3988f642020-08-27 22:43:03 +02009710 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009711
Bram Moolenaarae616492020-07-28 20:07:27 +02009712 case CMD_append:
9713 case CMD_change:
9714 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009715 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009716 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009717 case CMD_xit:
9718 not_in_vim9(&ea);
9719 goto erret;
9720
Bram Moolenaar002262f2020-07-08 17:47:57 +02009721 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009722 if (cctx.ctx_skip != SKIP_YES)
9723 {
9724 semsg(_(e_invalid_command_str), ea.cmd);
9725 goto erret;
9726 }
9727 // We don't check for a next command here.
9728 line = (char_u *)"";
9729 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009730
Bram Moolenaar20677332021-06-06 17:02:53 +02009731 case CMD_lua:
9732 case CMD_mzscheme:
9733 case CMD_perl:
9734 case CMD_py3:
9735 case CMD_python3:
9736 case CMD_python:
9737 case CMD_pythonx:
9738 case CMD_ruby:
9739 case CMD_tcl:
9740 ea.arg = p;
9741 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009742 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009743 else
9744 // heredoc lines have been concatenated with NL
9745 // characters in get_function_body()
9746 line = compile_script(line, &cctx);
9747 break;
9748
9749 default:
9750 // Not recognized, execute with do_cmdline_cmd().
9751 ea.arg = p;
9752 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009753 break;
9754 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009755nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009756 if (line == NULL)
9757 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009758 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009760 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009761 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009763 if (cctx.ctx_type_stack.ga_len < 0)
9764 {
9765 iemsg("Type stack underflow");
9766 goto erret;
9767 }
9768 }
9769
9770 if (cctx.ctx_scope != NULL)
9771 {
9772 if (cctx.ctx_scope->se_type == IF_SCOPE)
9773 emsg(_(e_endif));
9774 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9775 emsg(_(e_endwhile));
9776 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9777 emsg(_(e_endfor));
9778 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009779 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009780 goto erret;
9781 }
9782
Bram Moolenaarefd88552020-06-18 20:50:10 +02009783 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009784 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009785 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9786 ufunc->uf_ret_type = &t_void;
9787 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009789 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 goto erret;
9791 }
9792
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009793 // Return void if there is no return at the end.
9794 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 }
9796
Bram Moolenaar599410c2021-04-10 14:03:43 +02009797 // When compiled with ":silent!" and there was an error don't consider the
9798 // function compiled.
9799 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009800 {
9801 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9802 + ufunc->uf_dfunc_idx;
9803 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009804 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009805#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009806 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009807 {
9808 dfunc->df_instr_prof = instr->ga_data;
9809 dfunc->df_instr_prof_count = instr->ga_len;
9810 }
9811 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009812#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009813 if (cctx.ctx_compile_type == CT_DEBUG)
9814 {
9815 dfunc->df_instr_debug = instr->ga_data;
9816 dfunc->df_instr_debug_count = instr->ga_len;
9817 }
9818 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009819 {
9820 dfunc->df_instr = instr->ga_data;
9821 dfunc->df_instr_count = instr->ga_len;
9822 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009823 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009824 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009825 if (cctx.ctx_outer_used)
9826 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009827 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009829
9830 ret = OK;
9831
9832erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009833 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009835 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9836 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009837
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009838 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009839 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009840 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009841 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009842
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009843 // If using the last entry in the table and it was added above, we
9844 // might as well remove it.
9845 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009846 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009847 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009848 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009849 ufunc->uf_dfunc_idx = 0;
9850 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009851 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009852
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009853 while (cctx.ctx_scope != NULL)
9854 drop_scope(&cctx);
9855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 if (errormsg != NULL)
9857 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009858 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009859 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009860 }
9861
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009862 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9863 {
9864 if (ret == OK)
9865 {
9866 emsg(_(e_missing_redir_end));
9867 ret = FAIL;
9868 }
9869 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009870 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009871 }
9872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009873 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009874 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009875 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009876 if (do_estack_push)
9877 estack_pop();
9878
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009879 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009880 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009881 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009883 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884}
9885
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009886 void
9887set_function_type(ufunc_T *ufunc)
9888{
9889 int varargs = ufunc->uf_va_name != NULL;
9890 int argcount = ufunc->uf_args.ga_len;
9891
9892 // Create a type for the function, with the return type and any
9893 // argument types.
9894 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9895 // The type is included in "tt_args".
9896 if (argcount > 0 || varargs)
9897 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009898 if (ufunc->uf_type_list.ga_itemsize == 0)
9899 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009900 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9901 argcount, &ufunc->uf_type_list);
9902 // Add argument types to the function type.
9903 if (func_type_add_arg_types(ufunc->uf_func_type,
9904 argcount + varargs,
9905 &ufunc->uf_type_list) == FAIL)
9906 return;
9907 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9908 ufunc->uf_func_type->tt_min_argcount =
9909 argcount - ufunc->uf_def_args.ga_len;
9910 if (ufunc->uf_arg_types == NULL)
9911 {
9912 int i;
9913
9914 // lambda does not have argument types.
9915 for (i = 0; i < argcount; ++i)
9916 ufunc->uf_func_type->tt_args[i] = &t_any;
9917 }
9918 else
9919 mch_memmove(ufunc->uf_func_type->tt_args,
9920 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9921 if (varargs)
9922 {
9923 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009924 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009925 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9926 }
9927 }
9928 else
9929 // No arguments, can use a predefined type.
9930 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9931 argcount, &ufunc->uf_type_list);
9932}
9933
9934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009935/*
9936 * Delete an instruction, free what it contains.
9937 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009938 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009939delete_instr(isn_T *isn)
9940{
9941 switch (isn->isn_type)
9942 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009943 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009944 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009945 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009946 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009947 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009948 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009949 case ISN_LOADENV:
9950 case ISN_LOADG:
9951 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009952 case ISN_LOADT:
9953 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009955 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009956 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009957 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009958 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009959 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009960 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009962 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009963 case ISN_STOREW:
9964 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009965 vim_free(isn->isn_arg.string);
9966 break;
9967
Bram Moolenaar4c137212021-04-19 16:48:48 +02009968 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009969 {
9970 int idx;
9971 isn_T *list = isn->isn_arg.subs.subs_instr;
9972
9973 vim_free(isn->isn_arg.subs.subs_cmd);
9974 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9975 delete_instr(list + idx);
9976 vim_free(list);
9977 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009978 break;
9979
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009980 case ISN_INSTR:
9981 {
9982 int idx;
9983 isn_T *list = isn->isn_arg.instr;
9984
9985 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9986 delete_instr(list + idx);
9987 vim_free(list);
9988 }
9989 break;
9990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009991 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009992 case ISN_STORES:
9993 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009994 break;
9995
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009996 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009997 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009998 vim_free(isn->isn_arg.unlet.ul_name);
9999 break;
10000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010001 case ISN_STOREOPT:
10002 vim_free(isn->isn_arg.storeopt.so_name);
10003 break;
10004
10005 case ISN_PUSHBLOB: // push blob isn_arg.blob
10006 blob_unref(isn->isn_arg.blob);
10007 break;
10008
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010009 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010010#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010011 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010012#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010013 break;
10014
10015 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010016#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010017 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010018#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010019 break;
10020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021 case ISN_UCALL:
10022 vim_free(isn->isn_arg.ufunc.cuf_name);
10023 break;
10024
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010025 case ISN_FUNCREF:
10026 {
10027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10028 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010029 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010030
Bram Moolenaar077a4232020-12-22 18:33:27 +010010031 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10032 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010033 }
10034 break;
10035
10036 case ISN_DCALL:
10037 {
10038 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10039 + isn->isn_arg.dfunc.cdf_idx;
10040
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010041 if (dfunc->df_ufunc != NULL
10042 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010043 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010044 }
10045 break;
10046
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010047 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010048 {
10049 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10050 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10051
10052 if (ufunc != NULL)
10053 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010054 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010055 func_ptr_unref(ufunc);
10056 }
10057
10058 vim_free(lambda);
10059 vim_free(isn->isn_arg.newfunc.nf_global);
10060 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010061 break;
10062
Bram Moolenaar5e654232020-09-16 15:22:00 +020010063 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010064 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010065 free_type(isn->isn_arg.type.ct_type);
10066 break;
10067
Bram Moolenaar02194d22020-10-24 23:08:38 +020010068 case ISN_CMDMOD:
10069 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10070 ->cmod_filter_regmatch.regprog);
10071 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10072 break;
10073
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010074 case ISN_LOADSCRIPT:
10075 case ISN_STORESCRIPT:
10076 vim_free(isn->isn_arg.script.scriptref);
10077 break;
10078
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010079 case ISN_TRY:
10080 vim_free(isn->isn_arg.try.try_ref);
10081 break;
10082
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010083 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010084 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010085 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10086 break;
10087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010088 case ISN_2BOOL:
10089 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010090 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010091 case ISN_ADDBLOB:
10092 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010093 case ISN_ANYINDEX:
10094 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010095 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010096 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010097 case ISN_BLOBINDEX:
10098 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010100 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010101 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010103 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010104 case ISN_COMPAREANY:
10105 case ISN_COMPAREBLOB:
10106 case ISN_COMPAREBOOL:
10107 case ISN_COMPAREDICT:
10108 case ISN_COMPAREFLOAT:
10109 case ISN_COMPAREFUNC:
10110 case ISN_COMPARELIST:
10111 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010112 case ISN_COMPARESPECIAL:
10113 case ISN_COMPARESTRING:
10114 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010115 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010116 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117 case ISN_DROP:
10118 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010119 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010120 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010121 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010122 case ISN_EXECCONCAT:
10123 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010124 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010125 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010127 case ISN_GETITEM:
10128 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010129 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010130 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010131 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010132 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010133 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010134 case ISN_LOADBDICT:
10135 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010136 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010137 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010138 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010139 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010140 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010141 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010142 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010143 case ISN_NEGATENR:
10144 case ISN_NEWDICT:
10145 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010146 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010147 case ISN_OPFLOAT:
10148 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010149 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010150 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010151 case ISN_PROF_END:
10152 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010153 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 case ISN_PUSHF:
10155 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010156 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010157 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010158 case ISN_REDIREND:
10159 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010160 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010161 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010162 case ISN_SHUFFLE:
10163 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010164 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010165 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010166 case ISN_STORENR:
10167 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010168 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010169 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010170 case ISN_STOREV:
10171 case ISN_STRINDEX:
10172 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010173 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010174 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010175 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010176 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010177 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010178 // nothing allocated
10179 break;
10180 }
10181}
10182
10183/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010184 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010185 */
10186 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010187delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010188{
10189 int idx;
10190
10191 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010192 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010193
10194 if (dfunc->df_instr != NULL)
10195 {
10196 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10197 delete_instr(dfunc->df_instr + idx);
10198 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010199 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010200 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010201 if (dfunc->df_instr_debug != NULL)
10202 {
10203 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10204 delete_instr(dfunc->df_instr_debug + idx);
10205 VIM_CLEAR(dfunc->df_instr_debug);
10206 dfunc->df_instr_debug = NULL;
10207 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010208#ifdef FEAT_PROFILE
10209 if (dfunc->df_instr_prof != NULL)
10210 {
10211 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10212 delete_instr(dfunc->df_instr_prof + idx);
10213 VIM_CLEAR(dfunc->df_instr_prof);
10214 dfunc->df_instr_prof = NULL;
10215 }
10216#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010217
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010218 if (mark_deleted)
10219 dfunc->df_deleted = TRUE;
10220 if (dfunc->df_ufunc != NULL)
10221 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010222}
10223
10224/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010225 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010226 * function, unless another user function still uses it.
10227 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010228 */
10229 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010230unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010231{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010232 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010233 {
10234 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10235 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010237 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010238 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010239 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010240 ufunc->uf_dfunc_idx = 0;
10241 if (dfunc->df_ufunc == ufunc)
10242 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010243 }
10244}
10245
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010246/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010247 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010248 */
10249 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010250link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010251{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010252 if (ufunc->uf_dfunc_idx > 0)
10253 {
10254 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10255 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010256
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010257 ++dfunc->df_refcount;
10258 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010259}
10260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010261#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010262/*
10263 * Free all functions defined with ":def".
10264 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010265 void
10266free_def_functions(void)
10267{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010268 int idx;
10269
10270 for (idx = 0; idx < def_functions.ga_len; ++idx)
10271 {
10272 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10273
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010274 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010275 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010276 }
10277
10278 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279}
10280#endif
10281
10282
10283#endif // FEAT_EVAL