blob: a8e2c121d6cc83261662314e5da2507ceabe0e33 [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".
Bram Moolenaarab36e6a2021-11-30 16:14:49 +0000147 char_u *lhs_end; // end including any type
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200148
149 int lhs_has_index; // has "[expr]" or ".name"
150
151 int lhs_new_local; // create new local variable
152 int lhs_opt_flags; // for when destination is an option
153 int lhs_vimvaridx; // for when destination is a v:var
154
155 lvar_T lhs_local_lvar; // used for existing local destination
156 lvar_T lhs_arg_lvar; // used for argument destination
157 lvar_T *lhs_lvar; // points to destination lvar
158 int lhs_scriptvar_sid;
159 int lhs_scriptvar_idx;
160
161 int lhs_has_type; // type was specified
162 type_T *lhs_type;
163 type_T *lhs_member_type;
164
165 int lhs_append; // used by ISN_REDIREND
166} lhs_T;
167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168/*
169 * Context for compiling lines of Vim script.
170 * Stores info about the local variables and condition stack.
171 */
172struct cctx_S {
173 ufunc_T *ctx_ufunc; // current function
174 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200175 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 garray_T ctx_instr; // generated instructions
177
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200178 int ctx_prev_lnum; // line number below previous command, for
179 // debugging
180
Bram Moolenaare99d4222021-06-13 14:01:26 +0200181 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200185 int ctx_has_closure; // set to one if a closures was created in
186 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 garray_T ctx_imports; // imported items
189
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200190 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100191 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200192 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 cctx_T *ctx_outer; // outer scope for lambda or nested
195 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200196 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200199 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200200
Bram Moolenaar02194d22020-10-24 23:08:38 +0200201 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200202
203 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
204 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205};
206
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100207static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
209/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100210 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100211 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 * If "lvar" is NULL only check if the variable can be found.
213 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100215 static int
216lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217{
218 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100221 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100222 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200223
224 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
226 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100227 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
228 if (STRNCMP(name, lvp->lv_name, len) == 0
229 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200230 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100231 if (lvar != NULL)
232 {
233 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100234 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100235 }
236 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200239
240 // Find local in outer function scope.
241 if (cctx->ctx_outer != NULL)
242 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100243 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200244 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100245 if (lvar != NULL)
246 {
247 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100248 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100249 }
250 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200251 }
252 }
253
Bram Moolenaar709664c2020-12-12 14:33:41 +0100254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255}
256
257/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 * Lookup an argument in the current function and an enclosing function.
259 * Returns the argument index in "idxp"
260 * Returns the argument type in "type"
261 * Sets "gen_load_outer" to TRUE if found in outer scope.
262 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 */
264 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200265arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 char_u *name,
267 size_t len,
268 int *idxp,
269 type_T **type,
270 int *gen_load_outer,
271 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272{
273 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100276 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200277 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200278 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
280 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
281
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200282 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
283 {
284 if (idxp != NULL)
285 {
286 // Arguments are located above the frame pointer. One further
287 // if there is a vararg argument
288 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
289 + STACK_FRAME_SIZE)
290 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
291
292 if (cctx->ctx_ufunc->uf_arg_types != NULL)
293 *type = cctx->ctx_ufunc->uf_arg_types[idx];
294 else
295 *type = &t_any;
296 }
297 return OK;
298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200301 va_name = cctx->ctx_ufunc->uf_va_name;
302 if (va_name != NULL
303 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
304 {
305 if (idxp != NULL)
306 {
307 // varargs is always the last argument
308 *idxp = -STACK_FRAME_SIZE - 1;
309 *type = cctx->ctx_ufunc->uf_va_type;
310 }
311 return OK;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200314 if (cctx->ctx_outer != NULL)
315 {
316 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200317 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200318 == OK)
319 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100320 if (gen_load_outer != NULL)
321 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200322 return OK;
323 }
324 }
325
326 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a script-local variable in the current script, possibly defined in a
331 * block that contains the function "cctx->ctx_ufunc".
332 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100333 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Return NULL when not found.
335 */
336 static sallvar_T *
337find_script_var(char_u *name, size_t len, cctx_T *cctx)
338{
339 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
340 hashitem_T *hi;
341 int cc;
342 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200343 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 ufunc_T *ufunc;
345
346 // Find the list of all script variables with the right name.
347 if (len > 0)
348 {
349 cc = name[len];
350 name[len] = NUL;
351 }
352 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
353 if (len > 0)
354 name[len] = cc;
355 if (HASHITEM_EMPTY(hi))
356 return NULL;
357
358 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200359 if (sav->sav_block_id == 0)
360 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200361 return sav;
362
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200363 if (cctx == NULL)
364 {
365 // Not in a function scope, find variable with block id equal to or
366 // smaller than the current block id.
367 while (sav != NULL)
368 {
369 if (sav->sav_block_id <= si->sn_current_block_id)
370 break;
371 sav = sav->sav_next;
372 }
373 return sav;
374 }
375
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 // Go over the variables with this name and find one that was visible
377 // from the function.
378 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200379 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200380 while (sav != NULL)
381 {
382 int idx;
383
384 // Go over the blocks that this function was defined in. If the
385 // variable block ID matches it was visible to the function.
386 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
387 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
388 return sav;
389 sav = sav->sav_next;
390 }
391
Bram Moolenaar88421d62021-07-24 14:14:52 +0200392 // Not found, assume variable at script level was visible.
393 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200394}
395
396/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100397 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200398 */
399 static int
400script_is_vim9()
401{
402 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
403}
404
405/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200407 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100408 * Returns OK or FAIL.
409 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200410 static int
411script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200413 if (current_sctx.sc_sid <= 0)
414 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200415 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200416 {
417 // Check script variables that were visible where the function was
418 // defined.
419 if (find_script_var(name, len, cctx) != NULL)
420 return OK;
421 }
422 else
423 {
424 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
425 dictitem_T *di;
426 int cc;
427
428 // Check script variables that are currently visible
429 cc = name[len];
430 name[len] = NUL;
431 di = find_var_in_ht(ht, 0, name, TRUE);
432 name[len] = cc;
433 if (di != NULL)
434 return OK;
435 }
436
437 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438}
439
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100441 * Return TRUE if "name" is a local variable, argument, script variable or
442 * imported.
443 */
444 static int
445variable_exists(char_u *name, size_t len, cctx_T *cctx)
446{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100447 return (cctx != NULL
448 && (lookup_local(name, len, NULL, cctx) == OK
449 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200450 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100451 || find_imported(name, len, cctx) != NULL;
452}
453
454/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100455 * Return TRUE if "name" is a local variable, argument, script variable,
456 * imported or function.
457 */
458 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100459item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100460{
461 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100462 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463
464 if (variable_exists(name, len, cctx))
465 return TRUE;
466
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100467 // This is similar to what is in lookup_scriptitem():
468 // Find a function, so that a following "->" works.
469 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
470 // a function call.
471 p = skipwhite(name + len);
472
473 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
474 {
475 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200476 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100477 // Skip "g:" before a function name.
478 is_global = (name[0] == 'g' && name[1] == ':');
479 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
480 }
481 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100482}
483
484/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100485 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200486 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200487 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100488 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100489 * Return FAIL and give an error if it defined.
490 */
491 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100493{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200494 int c = p[len];
495 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200496
Bram Moolenaarda479c72021-04-10 21:01:38 +0200497 // underscore argument is OK
498 if (len == 1 && *p == '_')
499 return OK;
500
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200501 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 {
503 if (is_arg)
504 semsg(_(e_argument_already_declared_in_script_str), p);
505 else
506 semsg(_(e_variable_already_declared_in_script_str), p);
507 return FAIL;
508 }
509
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100511 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100512 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200513 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200514 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200515 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100516 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200517 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200518 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
519 && (!func_is_global(ufunc)
520 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200521 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100522 if (is_arg)
523 semsg(_(e_argument_name_shadows_existing_variable_str), p);
524 else
525 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200527 return FAIL;
528 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100529 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200530 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100531 return OK;
532}
533
Bram Moolenaar65b95452020-07-19 14:03:09 +0200534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535/////////////////////////////////////////////////////////////////////
536// Following generate_ functions expect the caller to call ga_grow().
537
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200538#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
539#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541/*
542 * Generate an instruction without arguments.
543 * Returns a pointer to the new instruction, NULL if failed.
544 */
545 static isn_T *
546generate_instr(cctx_T *cctx, isntype_T isn_type)
547{
548 garray_T *instr = &cctx->ctx_instr;
549 isn_T *isn;
550
Bram Moolenaar080457c2020-03-03 21:53:32 +0100551 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200552 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 return NULL;
554 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
555 isn->isn_type = isn_type;
556 isn->isn_lnum = cctx->ctx_lnum + 1;
557 ++instr->ga_len;
558
559 return isn;
560}
561
562/*
563 * Generate an instruction without arguments.
564 * "drop" will be removed from the stack.
565 * Returns a pointer to the new instruction, NULL if failed.
566 */
567 static isn_T *
568generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
569{
570 garray_T *stack = &cctx->ctx_type_stack;
571
Bram Moolenaar080457c2020-03-03 21:53:32 +0100572 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 stack->ga_len -= drop;
574 return generate_instr(cctx, isn_type);
575}
576
577/*
578 * Generate instruction "isn_type" and put "type" on the type stack.
579 */
580 static isn_T *
581generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
582{
583 isn_T *isn;
584 garray_T *stack = &cctx->ctx_type_stack;
585
586 if ((isn = generate_instr(cctx, isn_type)) == NULL)
587 return NULL;
588
Bram Moolenaar35578162021-08-02 19:10:38 +0200589 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200591 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 ++stack->ga_len;
593
594 return isn;
595}
596
597/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200598 * Generate an ISN_DEBUG instruction.
599 */
600 static isn_T *
601generate_instr_debug(cctx_T *cctx)
602{
603 isn_T *isn;
604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
605 + cctx->ctx_ufunc->uf_dfunc_idx;
606
607 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
608 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200609 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
610 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200611 return isn;
612}
613
614/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200616 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200617 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 */
619 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200620may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621{
622 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200623 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100625 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100627 RETURN_OK_IF_SKIP(cctx);
628 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200629 switch ((*type)->tt_type)
630 {
631 // nothing to be done
632 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200634 // conversion possible
635 case VAR_SPECIAL:
636 case VAR_BOOL:
637 case VAR_NUMBER:
638 case VAR_FLOAT:
639 break;
640
641 // conversion possible (with runtime check)
642 case VAR_ANY:
643 case VAR_UNKNOWN:
644 isntype = ISN_2STRING_ANY;
645 break;
646
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200647 // conversion possible when tolerant
648 case VAR_LIST:
649 if (tolerant)
650 {
651 isntype = ISN_2STRING_ANY;
652 break;
653 }
654 // FALLTHROUGH
655
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200656 // conversion not possible
657 case VAR_VOID:
658 case VAR_BLOB:
659 case VAR_FUNC:
660 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200661 case VAR_DICT:
662 case VAR_JOB:
663 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200664 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200665 to_string_error((*type)->tt_type);
666 return FAIL;
667 }
668
669 *type = &t_string;
670 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200672 isn->isn_arg.tostring.offset = offset;
673 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674
675 return OK;
676}
677
678 static int
679check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
680{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200681 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200683 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 {
685 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200686 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200688 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return FAIL;
690 }
691 return OK;
692}
693
Bram Moolenaar07802042021-09-09 23:01:14 +0200694/*
695 * Generate instruction for "+". For a list this creates a new list.
696 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200697 static int
698generate_add_instr(
699 cctx_T *cctx,
700 vartype_T vartype,
701 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200702 type_T *type2,
703 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200704{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100705 garray_T *stack = &cctx->ctx_type_stack;
706 isn_T *isn = generate_instr_drop(cctx,
707 vartype == VAR_NUMBER ? ISN_OPNR
708 : vartype == VAR_LIST ? ISN_ADDLIST
709 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200710#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100711 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200712#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100713 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200714
715 if (vartype != VAR_LIST && vartype != VAR_BLOB
716 && type1->tt_type != VAR_ANY
717 && type2->tt_type != VAR_ANY
718 && check_number_or_float(
719 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
720 return FAIL;
721
722 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200723 {
724 if (isn->isn_type == ISN_ADDLIST)
725 isn->isn_arg.op.op_type = expr_type;
726 else
727 isn->isn_arg.op.op_type = EXPR_ADD;
728 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100729
730 // When concatenating two lists with different member types the member type
731 // becomes "any".
732 if (vartype == VAR_LIST
733 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
734 && type1->tt_member != type2->tt_member)
735 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
736
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200737 return isn == NULL ? FAIL : OK;
738}
739
740/*
741 * Get the type to use for an instruction for an operation on "type1" and
742 * "type2". If they are matching use a type-specific instruction. Otherwise
743 * fall back to runtime type checking.
744 */
745 static vartype_T
746operator_type(type_T *type1, type_T *type2)
747{
748 if (type1->tt_type == type2->tt_type
749 && (type1->tt_type == VAR_NUMBER
750 || type1->tt_type == VAR_LIST
751#ifdef FEAT_FLOAT
752 || type1->tt_type == VAR_FLOAT
753#endif
754 || type1->tt_type == VAR_BLOB))
755 return type1->tt_type;
756 return VAR_ANY;
757}
758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759/*
760 * Generate an instruction with two arguments. The instruction depends on the
761 * type of the arguments.
762 */
763 static int
764generate_two_op(cctx_T *cctx, char_u *op)
765{
766 garray_T *stack = &cctx->ctx_type_stack;
767 type_T *type1;
768 type_T *type2;
769 vartype_T vartype;
770 isn_T *isn;
771
Bram Moolenaar080457c2020-03-03 21:53:32 +0100772 RETURN_OK_IF_SKIP(cctx);
773
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200774 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
776 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200777 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778
779 switch (*op)
780 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200781 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200782 if (generate_add_instr(cctx, vartype, type1, type2,
783 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 break;
786
787 case '-':
788 case '*':
789 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
790 op) == FAIL)
791 return FAIL;
792 if (vartype == VAR_NUMBER)
793 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
794#ifdef FEAT_FLOAT
795 else if (vartype == VAR_FLOAT)
796 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
797#endif
798 else
799 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
800 if (isn != NULL)
801 isn->isn_arg.op.op_type = *op == '*'
802 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
803 break;
804
Bram Moolenaar4c683752020-04-05 21:38:23 +0200805 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200807 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 && type2->tt_type != VAR_NUMBER))
809 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200810 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 return FAIL;
812 }
813 isn = generate_instr_drop(cctx,
814 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
815 if (isn != NULL)
816 isn->isn_arg.op.op_type = EXPR_REM;
817 break;
818 }
819
820 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200821 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 {
823 type_T *type = &t_any;
824
825#ifdef FEAT_FLOAT
826 // float+number and number+float results in float
827 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
828 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
829 type = &t_float;
830#endif
831 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
832 }
833
834 return OK;
835}
836
837/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200838 * Get the instruction to use for comparing "type1" with "type2"
839 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200841 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100842get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843{
844 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
Bram Moolenaar4c683752020-04-05 21:38:23 +0200846 if (type1 == VAR_UNKNOWN)
847 type1 = VAR_ANY;
848 if (type2 == VAR_UNKNOWN)
849 type2 = VAR_ANY;
850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 if (type1 == type2)
852 {
853 switch (type1)
854 {
855 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
856 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
857 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
858 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
859 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
860 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
861 case VAR_LIST: isntype = ISN_COMPARELIST; break;
862 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
863 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 default: isntype = ISN_COMPAREANY; break;
865 }
866 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200867 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100869 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 isntype = ISN_COMPAREANY;
871
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (isntype == ISN_COMPAREBOOL
874 || isntype == ISN_COMPARESPECIAL
875 || isntype == ISN_COMPARENR
876 || isntype == ISN_COMPAREFLOAT))
877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200878 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100879 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200880 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 }
882 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100883 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
885 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100886 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
887 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 && (type1 == VAR_BLOB || type2 == VAR_BLOB
889 || type1 == VAR_LIST || type2 == VAR_LIST))))
890 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200891 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200893 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895 return isntype;
896}
897
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200898 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200900{
901 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
902 return FAIL;
903 return OK;
904}
905
Bram Moolenaara5565e42020-05-09 15:44:01 +0200906/*
907 * Generate an ISN_COMPARE* instruction with a boolean result.
908 */
909 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100910generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200911{
912 isntype_T isntype;
913 isn_T *isn;
914 garray_T *stack = &cctx->ctx_type_stack;
915 vartype_T type1;
916 vartype_T type2;
917
918 RETURN_OK_IF_SKIP(cctx);
919
920 // Get the known type of the two items on the stack. If they are matching
921 // use a type-specific instruction. Otherwise fall back to runtime type
922 // checking.
923 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
924 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100925 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200926 if (isntype == ISN_DROP)
927 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 if ((isn = generate_instr(cctx, isntype)) == NULL)
930 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100931 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 isn->isn_arg.op.op_ic = ic;
933
934 // takes two arguments, puts one bool back
935 if (stack->ga_len >= 2)
936 {
937 --stack->ga_len;
938 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
939 }
940
941 return OK;
942}
943
944/*
945 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 */
948 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200949generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950{
951 isn_T *isn;
952 garray_T *stack = &cctx->ctx_type_stack;
953
Bram Moolenaar080457c2020-03-03 21:53:32 +0100954 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
956 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200957 isn->isn_arg.tobool.invert = invert;
958 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200961 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962
963 return OK;
964}
965
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200966/*
967 * Generate an ISN_COND2BOOL instruction.
968 */
969 static int
970generate_COND2BOOL(cctx_T *cctx)
971{
972 isn_T *isn;
973 garray_T *stack = &cctx->ctx_type_stack;
974
975 RETURN_OK_IF_SKIP(cctx);
976 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
977 return FAIL;
978
979 // type becomes bool
980 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
981
982 return OK;
983}
984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200986generate_TYPECHECK(
987 cctx_T *cctx,
988 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100989 int offset,
990 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991{
992 isn_T *isn;
993 garray_T *stack = &cctx->ctx_type_stack;
994
Bram Moolenaar080457c2020-03-03 21:53:32 +0100995 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
997 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200998 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100999 isn->isn_arg.type.ct_off = (int8_T)offset;
1000 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001
Bram Moolenaar5e654232020-09-16 15:22:00 +02001002 // type becomes expected
1003 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
1005 return OK;
1006}
1007
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001008 static int
1009generate_SETTYPE(
1010 cctx_T *cctx,
1011 type_T *expected)
1012{
1013 isn_T *isn;
1014
1015 RETURN_OK_IF_SKIP(cctx);
1016 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.type.ct_type = alloc_type(expected);
1019 return OK;
1020}
1021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001023 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1024 * used. Return FALSE if the types will never match.
1025 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001026 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001027use_typecheck(type_T *actual, type_T *expected)
1028{
1029 if (actual->tt_type == VAR_ANY
1030 || actual->tt_type == VAR_UNKNOWN
1031 || (actual->tt_type == VAR_FUNC
1032 && (expected->tt_type == VAR_FUNC
1033 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001034 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1035 && ((actual->tt_member == &t_void)
1036 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001037 return TRUE;
1038 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1039 && actual->tt_type == expected->tt_type)
1040 // This takes care of a nested list or dict.
1041 return use_typecheck(actual->tt_member, expected->tt_member);
1042 return FALSE;
1043}
1044
1045/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001046 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001047 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001048 * - "actual" is a type that can be "expected" type: add a runtime check; or
1049 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001050 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1051 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001052 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001053 static int
1054need_type_where(
1055 type_T *actual,
1056 type_T *expected,
1057 int offset,
1058 where_T where,
1059 cctx_T *cctx,
1060 int silent,
1061 int actual_is_const)
1062{
1063 if (expected == &t_bool && actual != &t_bool
1064 && (actual->tt_flags & TTFLAG_BOOL_OK))
1065 {
1066 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1067 // boolean is OK but requires a conversion.
1068 generate_2BOOL(cctx, FALSE, offset);
1069 return OK;
1070 }
1071
1072 if (check_type(expected, actual, FALSE, where) == OK)
1073 return OK;
1074
1075 // If the actual type can be the expected type add a runtime check.
1076 // If it's a constant a runtime check makes no sense.
1077 if ((!actual_is_const || actual == &t_any)
1078 && use_typecheck(actual, expected))
1079 {
1080 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1081 return OK;
1082 }
1083
1084 if (!silent)
1085 type_mismatch_where(expected, actual, where);
1086 return FAIL;
1087}
1088
Bram Moolenaar351ead02021-01-16 16:07:01 +01001089 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001090need_type(
1091 type_T *actual,
1092 type_T *expected,
1093 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001094 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001095 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001096 int silent,
1097 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001098{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001099 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001100
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001101 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001102 return need_type_where(actual, expected, offset, where,
1103 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001104}
1105
1106/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001107 * Check that the top of the type stack has a type that can be used as a
1108 * condition. Give an error and return FAIL if not.
1109 */
1110 static int
1111bool_on_stack(cctx_T *cctx)
1112{
1113 garray_T *stack = &cctx->ctx_type_stack;
1114 type_T *type;
1115
1116 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1117 if (type == &t_bool)
1118 return OK;
1119
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001120 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001121 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1122 // This requires a runtime type check.
1123 return generate_COND2BOOL(cctx);
1124
Bram Moolenaar351ead02021-01-16 16:07:01 +01001125 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001126}
1127
1128/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 * Generate an ISN_PUSHNR instruction.
1130 */
1131 static int
1132generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1133{
1134 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001135 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136
Bram Moolenaar080457c2020-03-03 21:53:32 +01001137 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.number = number;
1141
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001142 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001143 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001144 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 return OK;
1146}
1147
1148/*
1149 * Generate an ISN_PUSHBOOL instruction.
1150 */
1151 static int
1152generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1153{
1154 isn_T *isn;
1155
Bram Moolenaar080457c2020-03-03 21:53:32 +01001156 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1158 return FAIL;
1159 isn->isn_arg.number = number;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_PUSHSPEC instruction.
1166 */
1167 static int
1168generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = number;
1176
1177 return OK;
1178}
1179
1180#ifdef FEAT_FLOAT
1181/*
1182 * Generate an ISN_PUSHF instruction.
1183 */
1184 static int
1185generate_PUSHF(cctx_T *cctx, float_T fnumber)
1186{
1187 isn_T *isn;
1188
Bram Moolenaar080457c2020-03-03 21:53:32 +01001189 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.fnumber = fnumber;
1193
1194 return OK;
1195}
1196#endif
1197
1198/*
1199 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001200 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 */
1202 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001203generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204{
1205 isn_T *isn;
1206
Bram Moolenaar508b5612021-01-02 18:17:26 +01001207 if (cctx->ctx_skip == SKIP_YES)
1208 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001209 if (str != NULL)
1210 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001211 return OK;
1212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001214 {
1215 if (str != NULL)
1216 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001218 }
1219 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001225 * Generate an ISN_PUSHCHANNEL instruction.
1226 * Consumes "channel".
1227 */
1228 static int
1229generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001234 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.channel = channel;
1237
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_PUSHJOB instruction.
1243 * Consumes "job".
1244 */
1245 static int
1246generate_PUSHJOB(cctx_T *cctx, job_T *job)
1247{
1248 isn_T *isn;
1249
Bram Moolenaar080457c2020-03-03 21:53:32 +01001250 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001251 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001252 return FAIL;
1253 isn->isn_arg.job = job;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 * Generate an ISN_PUSHBLOB instruction.
1260 * Consumes "blob".
1261 */
1262 static int
1263generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1264{
1265 isn_T *isn;
1266
Bram Moolenaar080457c2020-03-03 21:53:32 +01001267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1269 return FAIL;
1270 isn->isn_arg.blob = blob;
1271
1272 return OK;
1273}
1274
1275/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001276 * Generate an ISN_PUSHFUNC instruction with name "name".
1277 * Consumes "name".
1278 */
1279 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001280generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001281{
1282 isn_T *isn;
1283
Bram Moolenaar080457c2020-03-03 21:53:32 +01001284 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001285 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001286 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001287 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001288
1289 return OK;
1290}
1291
1292/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001293 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001294 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1295 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001296 */
1297 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001298generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001302 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1303 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001304 type_T *item_type = &t_any;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001308 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001309 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001310 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 emsg(_(e_listreq));
1312 return FAIL;
1313 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001314 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001315 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1316 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001317 isn->isn_arg.getitem.gi_index = index;
1318 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001319
1320 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001321 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001322 return FAIL;
1323 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1324 ++stack->ga_len;
1325 return OK;
1326}
1327
1328/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001329 * Generate an ISN_SLICE instruction with "count".
1330 */
1331 static int
1332generate_SLICE(cctx_T *cctx, int count)
1333{
1334 isn_T *isn;
1335
1336 RETURN_OK_IF_SKIP(cctx);
1337 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1338 return FAIL;
1339 isn->isn_arg.number = count;
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_CHECKLEN instruction with "min_len".
1345 */
1346 static int
1347generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1348{
1349 isn_T *isn;
1350
1351 RETURN_OK_IF_SKIP(cctx);
1352
1353 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1354 return FAIL;
1355 isn->isn_arg.checklen.cl_min_len = min_len;
1356 isn->isn_arg.checklen.cl_more_OK = more_OK;
1357
1358 return OK;
1359}
1360
1361/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 * Generate an ISN_STORE instruction.
1363 */
1364 static int
1365generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1366{
1367 isn_T *isn;
1368
Bram Moolenaar080457c2020-03-03 21:53:32 +01001369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1371 return FAIL;
1372 if (name != NULL)
1373 isn->isn_arg.string = vim_strsave(name);
1374 else
1375 isn->isn_arg.number = idx;
1376
1377 return OK;
1378}
1379
1380/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001381 * Generate an ISN_STOREOUTER instruction.
1382 */
1383 static int
1384generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1385{
1386 isn_T *isn;
1387
1388 RETURN_OK_IF_SKIP(cctx);
1389 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1390 return FAIL;
1391 isn->isn_arg.outer.outer_idx = idx;
1392 isn->isn_arg.outer.outer_depth = level;
1393
1394 return OK;
1395}
1396
1397/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1399 */
1400 static int
1401generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1407 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001408 isn->isn_arg.storenr.stnr_idx = idx;
1409 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410
1411 return OK;
1412}
1413
1414/*
1415 * Generate an ISN_STOREOPT instruction
1416 */
1417 static int
1418generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1419{
1420 isn_T *isn;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001423 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 return FAIL;
1425 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1426 isn->isn_arg.storeopt.so_flags = opt_flags;
1427
1428 return OK;
1429}
1430
1431/*
1432 * Generate an ISN_LOAD or similar instruction.
1433 */
1434 static int
1435generate_LOAD(
1436 cctx_T *cctx,
1437 isntype_T isn_type,
1438 int idx,
1439 char_u *name,
1440 type_T *type)
1441{
1442 isn_T *isn;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1446 return FAIL;
1447 if (name != NULL)
1448 isn->isn_arg.string = vim_strsave(name);
1449 else
1450 isn->isn_arg.number = idx;
1451
1452 return OK;
1453}
1454
1455/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001456 * Generate an ISN_LOADOUTER instruction
1457 */
1458 static int
1459generate_LOADOUTER(
1460 cctx_T *cctx,
1461 int idx,
1462 int nesting,
1463 type_T *type)
1464{
1465 isn_T *isn;
1466
1467 RETURN_OK_IF_SKIP(cctx);
1468 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1469 return FAIL;
1470 isn->isn_arg.outer.outer_idx = idx;
1471 isn->isn_arg.outer.outer_depth = nesting;
1472
1473 return OK;
1474}
1475
1476/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001477 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001478 */
1479 static int
1480generate_LOADV(
1481 cctx_T *cctx,
1482 char_u *name,
1483 int error)
1484{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001485 int di_flags;
1486 int vidx = find_vim_var(name, &di_flags);
1487 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001488
Bram Moolenaar080457c2020-03-03 21:53:32 +01001489 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001490 if (vidx < 0)
1491 {
1492 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001493 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001494 return FAIL;
1495 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001496 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001497
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001498 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001499}
1500
1501/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001502 * Generate an ISN_UNLET instruction.
1503 */
1504 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001505generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001506{
1507 isn_T *isn;
1508
1509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001510 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001511 return FAIL;
1512 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1513 isn->isn_arg.unlet.ul_forceit = forceit;
1514
1515 return OK;
1516}
1517
1518/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001519 * Generate an ISN_LOCKCONST instruction.
1520 */
1521 static int
1522generate_LOCKCONST(cctx_T *cctx)
1523{
1524 isn_T *isn;
1525
1526 RETURN_OK_IF_SKIP(cctx);
1527 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1528 return FAIL;
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 * Generate an ISN_LOADS instruction.
1534 */
1535 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001536generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001538 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001540 int sid,
1541 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542{
1543 isn_T *isn;
1544
Bram Moolenaar080457c2020-03-03 21:53:32 +01001545 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001546 if (isn_type == ISN_LOADS)
1547 isn = generate_instr_type(cctx, isn_type, type);
1548 else
1549 isn = generate_instr_drop(cctx, isn_type, 1);
1550 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001552 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1553 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554
1555 return OK;
1556}
1557
1558/*
1559 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1560 */
1561 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001562generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 cctx_T *cctx,
1564 isntype_T isn_type,
1565 int sid,
1566 int idx,
1567 type_T *type)
1568{
1569 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001570 scriptref_T *sref;
1571 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572
Bram Moolenaar080457c2020-03-03 21:53:32 +01001573 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 if (isn_type == ISN_LOADSCRIPT)
1575 isn = generate_instr_type(cctx, isn_type, type);
1576 else
1577 isn = generate_instr_drop(cctx, isn_type, 1);
1578 if (isn == NULL)
1579 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001580
1581 // This requires three arguments, which doesn't fit in an instruction, thus
1582 // we need to allocate a struct for this.
1583 sref = ALLOC_ONE(scriptref_T);
1584 if (sref == NULL)
1585 return FAIL;
1586 isn->isn_arg.script.scriptref = sref;
1587 sref->sref_sid = sid;
1588 sref->sref_idx = idx;
1589 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001590 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 return OK;
1592}
1593
1594/*
1595 * Generate an ISN_NEWLIST instruction.
1596 */
1597 static int
1598generate_NEWLIST(cctx_T *cctx, int count)
1599{
1600 isn_T *isn;
1601 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 type_T *type;
1603 type_T *member;
1604
Bram Moolenaar080457c2020-03-03 21:53:32 +01001605 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.number = count;
1609
Bram Moolenaar127542b2020-08-09 17:22:04 +02001610 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001611 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001612 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001613 else
1614 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001615 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1616 cctx->ctx_type_list);
1617 type = get_list_type(member, cctx->ctx_type_list);
1618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 // drop the value types
1620 stack->ga_len -= count;
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001623 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 return FAIL;
1625 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1626 ++stack->ga_len;
1627
1628 return OK;
1629}
1630
1631/*
1632 * Generate an ISN_NEWDICT instruction.
1633 */
1634 static int
1635generate_NEWDICT(cctx_T *cctx, int count)
1636{
1637 isn_T *isn;
1638 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 type_T *type;
1640 type_T *member;
1641
Bram Moolenaar080457c2020-03-03 21:53:32 +01001642 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.number = count;
1646
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001647 if (count == 0)
1648 member = &t_void;
1649 else
1650 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001651 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1652 cctx->ctx_type_list);
1653 type = get_dict_type(member, cctx->ctx_type_list);
1654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 // drop the key and value types
1656 stack->ga_len -= 2 * count;
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001659 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 return FAIL;
1661 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1662 ++stack->ga_len;
1663
1664 return OK;
1665}
1666
1667/*
1668 * Generate an ISN_FUNCREF instruction.
1669 */
1670 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001671generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672{
1673 isn_T *isn;
1674 garray_T *stack = &cctx->ctx_type_stack;
1675
Bram Moolenaar080457c2020-03-03 21:53:32 +01001676 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1678 return FAIL;
Bram Moolenaar38453522021-11-28 22:00:12 +00001679 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1680 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1681 else
1682 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001683 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001685 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001686 // the nested context, including this one.
1687 if (ufunc->uf_flags & FC_CLOSURE)
1688 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1689
Bram Moolenaar35578162021-08-02 19:10:38 +02001690 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001692 ((type_T **)stack->ga_data)[stack->ga_len] =
1693 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 ++stack->ga_len;
1695
1696 return OK;
1697}
1698
1699/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001700 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001701 * "lambda_name" and "func_name" must be in allocated memory and will be
1702 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001703 */
1704 static int
1705generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1706{
1707 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001708
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001709 if (cctx->ctx_skip == SKIP_YES)
1710 {
1711 vim_free(lambda_name);
1712 vim_free(func_name);
1713 return OK;
1714 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001715 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001716 {
1717 vim_free(lambda_name);
1718 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001719 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001720 }
1721 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001722 isn->isn_arg.newfunc.nf_global = func_name;
1723
1724 return OK;
1725}
1726
1727/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001728 * Generate an ISN_DEF instruction: list functions
1729 */
1730 static int
1731generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1732{
1733 isn_T *isn;
1734
1735 RETURN_OK_IF_SKIP(cctx);
1736 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1737 return FAIL;
1738 if (len > 0)
1739 {
1740 isn->isn_arg.string = vim_strnsave(name, len);
1741 if (isn->isn_arg.string == NULL)
1742 return FAIL;
1743 }
1744 return OK;
1745}
1746
1747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 * Generate an ISN_JUMP instruction.
1749 */
1750 static int
1751generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1752{
1753 isn_T *isn;
1754 garray_T *stack = &cctx->ctx_type_stack;
1755
Bram Moolenaar080457c2020-03-03 21:53:32 +01001756 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.jump.jump_when = when;
1760 isn->isn_arg.jump.jump_where = where;
1761
1762 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1763 --stack->ga_len;
1764
1765 return OK;
1766}
1767
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001768/*
1769 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1770 */
1771 static int
1772generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1773{
1774 isn_T *isn;
1775
1776 RETURN_OK_IF_SKIP(cctx);
1777 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1778 return FAIL;
1779 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1780 // jump_where is set later
1781 return OK;
1782}
1783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 static int
1785generate_FOR(cctx_T *cctx, int loop_idx)
1786{
1787 isn_T *isn;
1788 garray_T *stack = &cctx->ctx_type_stack;
1789
Bram Moolenaar080457c2020-03-03 21:53:32 +01001790 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1792 return FAIL;
1793 isn->isn_arg.forloop.for_idx = loop_idx;
1794
Bram Moolenaar35578162021-08-02 19:10:38 +02001795 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 return FAIL;
1797 // type doesn't matter, will be stored next
1798 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1799 ++stack->ga_len;
1800
1801 return OK;
1802}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001803/*
1804 * Generate an ISN_TRYCONT instruction.
1805 */
1806 static int
1807generate_TRYCONT(cctx_T *cctx, int levels, int where)
1808{
1809 isn_T *isn;
1810
1811 RETURN_OK_IF_SKIP(cctx);
1812 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.trycont.tct_levels = levels;
1815 isn->isn_arg.trycont.tct_where = where;
1816
1817 return OK;
1818}
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
1821/*
1822 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001823 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 * Return FAIL if the number of arguments is wrong.
1825 */
1826 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001827generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001831 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001832 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001833 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001834 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835
Bram Moolenaar080457c2020-03-03 21:53:32 +01001836 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001837 argoff = check_internal_func(func_idx, argcount);
1838 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 return FAIL;
1840
Bram Moolenaar389df252020-07-09 21:20:47 +02001841 if (method_call && argoff > 1)
1842 {
1843 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1844 return FAIL;
1845 isn->isn_arg.shuffle.shfl_item = argcount;
1846 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1847 }
1848
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001849 if (argcount > 0)
1850 {
1851 // Check the types of the arguments.
1852 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001853 if (method_call && argoff > 1)
1854 {
1855 int i;
1856
1857 for (i = 0; i < argcount; ++i)
1858 shuffled_argtypes[i] = (i < argoff - 1)
1859 ? argtypes[i + 1]
1860 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1861 argtypes = shuffled_argtypes;
1862 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001863 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1864 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001865 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001866 if (internal_func_is_map(func_idx))
1867 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001868 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1871 return FAIL;
1872 isn->isn_arg.bfunc.cbf_idx = func_idx;
1873 isn->isn_arg.bfunc.cbf_argcount = argcount;
1874
Bram Moolenaar94738d82020-10-21 14:25:07 +02001875 // Drop the argument types and push the return type.
1876 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001877 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 return FAIL;
1879 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001880 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001881 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001883 if (maptype != NULL && maptype->tt_member != NULL
1884 && maptype->tt_member != &t_any)
1885 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001886 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 return OK;
1889}
1890
1891/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001892 * Generate an ISN_LISTAPPEND instruction. Works like add().
1893 * Argument count is already checked.
1894 */
1895 static int
1896generate_LISTAPPEND(cctx_T *cctx)
1897{
1898 garray_T *stack = &cctx->ctx_type_stack;
1899 type_T *list_type;
1900 type_T *item_type;
1901 type_T *expected;
1902
1903 // Caller already checked that list_type is a list.
1904 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1905 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1906 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001907 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001908 return FAIL;
1909
1910 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1911 return FAIL;
1912
1913 --stack->ga_len; // drop the argument
1914 return OK;
1915}
1916
1917/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001918 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1919 * Argument count is already checked.
1920 */
1921 static int
1922generate_BLOBAPPEND(cctx_T *cctx)
1923{
1924 garray_T *stack = &cctx->ctx_type_stack;
1925 type_T *item_type;
1926
1927 // Caller already checked that blob_type is a blob.
1928 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001929 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001930 return FAIL;
1931
1932 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1933 return FAIL;
1934
1935 --stack->ga_len; // drop the argument
1936 return OK;
1937}
1938
1939/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001940 * Return TRUE if "ufunc" should be compiled, taking into account whether
1941 * "profile" indicates profiling is to be done.
1942 */
1943 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001944func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001945{
1946 switch (ufunc->uf_def_status)
1947 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001948 case UF_TO_BE_COMPILED:
1949 return TRUE;
1950
Bram Moolenaarb2049902021-01-24 12:53:53 +01001951 case UF_COMPILED:
1952 {
1953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1954 + ufunc->uf_dfunc_idx;
1955
Bram Moolenaare99d4222021-06-13 14:01:26 +02001956 switch (compile_type)
1957 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001958 case CT_PROFILE:
1959#ifdef FEAT_PROFILE
1960 return dfunc->df_instr_prof == NULL;
1961#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001962 case CT_NONE:
1963 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001964 case CT_DEBUG:
1965 return dfunc->df_instr_debug == NULL;
1966 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001967 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001968
1969 case UF_NOT_COMPILED:
1970 case UF_COMPILE_ERROR:
1971 case UF_COMPILING:
1972 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001973 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001974 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001975}
1976
1977/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 * Generate an ISN_DCALL or ISN_UCALL instruction.
1979 * Return FAIL if the number of arguments is wrong.
1980 */
1981 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001982generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983{
1984 isn_T *isn;
1985 garray_T *stack = &cctx->ctx_type_stack;
1986 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001987 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988
Bram Moolenaar080457c2020-03-03 21:53:32 +01001989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (argcount > regular_args && !has_varargs(ufunc))
1991 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001992 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 return FAIL;
1994 }
1995 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1996 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001997 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return FAIL;
1999 }
2000
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002001 if (ufunc->uf_def_status != UF_NOT_COMPILED
2002 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002003 {
2004 int i;
2005
2006 for (i = 0; i < argcount; ++i)
2007 {
2008 type_T *expected;
2009 type_T *actual;
2010
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002011 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2012 if (actual == &t_special
2013 && i >= regular_args - ufunc->uf_def_args.ga_len)
2014 {
2015 // assume v:none used for default argument value
2016 continue;
2017 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002018 if (i < regular_args)
2019 {
2020 if (ufunc->uf_arg_types == NULL)
2021 continue;
2022 expected = ufunc->uf_arg_types[i];
2023 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002024 else if (ufunc->uf_va_type == NULL
2025 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002026 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002027 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002028 else
2029 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002030 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002031 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002032 {
2033 arg_type_mismatch(expected, actual, i + 1);
2034 return FAIL;
2035 }
2036 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002037 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002038 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002039 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002040 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002041 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002042 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2043 {
2044 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2045 ufunc->uf_name);
2046 return FAIL;
2047 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002050 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002051 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002053 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 {
2055 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2056 isn->isn_arg.dfunc.cdf_argcount = argcount;
2057 }
2058 else
2059 {
2060 // A user function may be deleted and redefined later, can't use the
2061 // ufunc pointer, need to look it up again at runtime.
2062 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2063 isn->isn_arg.ufunc.cuf_argcount = argcount;
2064 }
2065
2066 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002067 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 return FAIL;
2069 // add return value
2070 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2071 ++stack->ga_len;
2072
2073 return OK;
2074}
2075
2076/*
2077 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2078 */
2079 static int
2080generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2081{
2082 isn_T *isn;
2083 garray_T *stack = &cctx->ctx_type_stack;
2084
Bram Moolenaar080457c2020-03-03 21:53:32 +01002085 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2087 return FAIL;
2088 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2089 isn->isn_arg.ufunc.cuf_argcount = argcount;
2090
2091 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002092 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002093 return FAIL;
2094 // add return value
2095 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2096 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097
2098 return OK;
2099}
2100
2101/*
2102 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002103 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 */
2105 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002106generate_PCALL(
2107 cctx_T *cctx,
2108 int argcount,
2109 char_u *name,
2110 type_T *type,
2111 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112{
2113 isn_T *isn;
2114 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002115 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
Bram Moolenaar080457c2020-03-03 21:53:32 +01002117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002118
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002119 if (type->tt_type == VAR_ANY)
2120 ret_type = &t_any;
2121 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002122 {
2123 if (type->tt_argcount != -1)
2124 {
2125 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2126
2127 if (argcount < type->tt_min_argcount - varargs)
2128 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002129 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002130 return FAIL;
2131 }
2132 if (!varargs && argcount > type->tt_argcount)
2133 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002134 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002135 return FAIL;
2136 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002137 if (type->tt_args != NULL)
2138 {
2139 int i;
2140
2141 for (i = 0; i < argcount; ++i)
2142 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002143 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002144 type_T *actual = ((type_T **)stack->ga_data)[
2145 stack->ga_len + offset];
2146 type_T *expected;
2147
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002148 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002149 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002150 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002151 else if (i >= type->tt_min_argcount
2152 && actual == &t_special)
2153 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002154 else
2155 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002156 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002157 cctx, TRUE, FALSE) == FAIL)
2158 {
2159 arg_type_mismatch(expected, actual, i + 1);
2160 return FAIL;
2161 }
2162 }
2163 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002164 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002165 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002166 if (ret_type == &t_unknown)
2167 // return type not known yet, use a runtime check
2168 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002169 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002170 else
2171 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002172 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002173 return FAIL;
2174 }
2175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2177 return FAIL;
2178 isn->isn_arg.pfunc.cpf_top = at_top;
2179 isn->isn_arg.pfunc.cpf_argcount = argcount;
2180
2181 stack->ga_len -= argcount; // drop the arguments
2182
2183 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002184 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002186 // If partial is above the arguments it must be cleared and replaced with
2187 // the return value.
2188 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2189 return FAIL;
2190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 return OK;
2192}
2193
2194/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002195 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 */
2197 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002198generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199{
2200 isn_T *isn;
2201 garray_T *stack = &cctx->ctx_type_stack;
2202 type_T *type;
2203
Bram Moolenaar080457c2020-03-03 21:53:32 +01002204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002205 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002207 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002209 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002211 if (type->tt_type != VAR_DICT && type != &t_any)
2212 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002213 char *tofree;
2214
2215 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2216 name, type_name(type, &tofree));
2217 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002218 return FAIL;
2219 }
2220 // change dict type to dict member type
2221 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002222 {
2223 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2224 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226
2227 return OK;
2228}
2229
2230/*
2231 * Generate an ISN_ECHO instruction.
2232 */
2233 static int
2234generate_ECHO(cctx_T *cctx, int with_white, int count)
2235{
2236 isn_T *isn;
2237
Bram Moolenaar080457c2020-03-03 21:53:32 +01002238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2240 return FAIL;
2241 isn->isn_arg.echo.echo_with_white = with_white;
2242 isn->isn_arg.echo.echo_count = count;
2243
2244 return OK;
2245}
2246
Bram Moolenaarad39c092020-02-26 18:23:43 +01002247/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002248 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002249 */
2250 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002251generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002252{
2253 isn_T *isn;
2254
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002255 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002256 return FAIL;
2257 isn->isn_arg.number = count;
2258
2259 return OK;
2260}
2261
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002262/*
2263 * Generate an ISN_PUT instruction.
2264 */
2265 static int
2266generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2267{
2268 isn_T *isn;
2269
2270 RETURN_OK_IF_SKIP(cctx);
2271 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2272 return FAIL;
2273 isn->isn_arg.put.put_regname = regname;
2274 isn->isn_arg.put.put_lnum = lnum;
2275 return OK;
2276}
2277
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002278/*
2279 * Generate an EXEC instruction that takes a string argument.
2280 * A copy is made of "line".
2281 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002283generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284{
2285 isn_T *isn;
2286
Bram Moolenaar080457c2020-03-03 21:53:32 +01002287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002288 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 return FAIL;
2290 isn->isn_arg.string = vim_strsave(line);
2291 return OK;
2292}
2293
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002294/*
2295 * Generate an EXEC instruction that takes a string argument.
2296 * "str" must be allocated, it is consumed.
2297 */
2298 static int
2299generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2300{
2301 isn_T *isn;
2302
2303 if (cctx->ctx_skip == SKIP_YES)
2304 {
2305 vim_free(str);
2306 return OK;
2307 }
2308 if ((isn = generate_instr(cctx, isntype)) == NULL)
2309 {
2310 vim_free(str);
2311 return FAIL;
2312 }
2313 isn->isn_arg.string = str;
2314 return OK;
2315}
2316
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002317 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002318generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2319{
2320 isn_T *isn;
2321 garray_T *stack = &cctx->ctx_type_stack;
2322
2323 RETURN_OK_IF_SKIP(cctx);
2324 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2325 return FAIL;
2326 isn->isn_arg.string = vim_strsave(line);
2327
Bram Moolenaar35578162021-08-02 19:10:38 +02002328 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002329 return FAIL;
2330 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2331 ++stack->ga_len;
2332
2333 return OK;
2334}
2335
2336 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002337generate_EXECCONCAT(cctx_T *cctx, int count)
2338{
2339 isn_T *isn;
2340
2341 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2342 return FAIL;
2343 isn->isn_arg.number = count;
2344 return OK;
2345}
2346
Bram Moolenaar08597872020-12-10 19:43:40 +01002347/*
2348 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2349 */
2350 static int
2351generate_RANGE(cctx_T *cctx, char_u *range)
2352{
2353 isn_T *isn;
2354 garray_T *stack = &cctx->ctx_type_stack;
2355
2356 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2357 return FAIL;
2358 isn->isn_arg.string = range;
2359
Bram Moolenaar35578162021-08-02 19:10:38 +02002360 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002361 return FAIL;
2362 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2363 ++stack->ga_len;
2364 return OK;
2365}
2366
Bram Moolenaar792f7862020-11-23 08:31:18 +01002367 static int
2368generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2369{
2370 isn_T *isn;
2371
2372 RETURN_OK_IF_SKIP(cctx);
2373 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2374 return FAIL;
2375 isn->isn_arg.unpack.unp_count = var_count;
2376 isn->isn_arg.unpack.unp_semicolon = semicolon;
2377 return OK;
2378}
2379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002381 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002382 */
2383 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002384generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002385{
2386 isn_T *isn;
2387
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002388 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002389 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002390 cctx->ctx_has_cmdmod = TRUE;
2391
2392 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002393 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002394 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2395 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2396 return FAIL;
2397 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002398 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002399 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002400 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002401
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002402 return OK;
2403}
2404
2405 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002406generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002407{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002408 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2409 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002410 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002411 return OK;
2412}
2413
Bram Moolenaarfa984412021-03-25 22:15:28 +01002414 static int
2415misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002416{
2417 garray_T *instr = &cctx->ctx_instr;
2418
Bram Moolenaara91a7132021-03-25 21:12:15 +01002419 if (cctx->ctx_has_cmdmod
2420 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2421 == ISN_CMDMOD)
2422 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002423 emsg(_(e_misplaced_command_modifier));
2424 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002425 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002426 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002427}
2428
2429/*
2430 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002431 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002432 */
2433 static int
2434current_instr_idx(cctx_T *cctx)
2435{
2436 garray_T *instr = &cctx->ctx_instr;
2437 int idx = instr->ga_len;
2438
Bram Moolenaare99d4222021-06-13 14:01:26 +02002439 while (idx > 0)
2440 {
2441 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002442 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002443 {
2444 --idx;
2445 continue;
2446 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002447#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002448 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2449 {
2450 --idx;
2451 continue;
2452 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002453#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002454 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2455 {
2456 --idx;
2457 continue;
2458 }
2459 break;
2460 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002461 return idx;
2462}
2463
Bram Moolenaarf002a412021-01-24 13:34:18 +01002464#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002465 static void
2466may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2467{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002468 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002469 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002470}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002471#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002472
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002473/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002475 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002477 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002478reserve_local(
2479 cctx_T *cctx,
2480 char_u *name,
2481 size_t len,
2482 int isConst,
2483 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002486 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002488 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002490 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002491 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 }
2493
Bram Moolenaar35578162021-08-02 19:10:38 +02002494 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002495 return NULL;
2496 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002497 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002499 // Every local variable uses the next entry on the stack. We could re-use
2500 // the last ones when leaving a scope, but then variables used in a closure
2501 // might get overwritten. To keep things simple do not re-use stack
2502 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002503 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2504 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002505
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002506 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 lvar->lv_const = isConst;
2508 lvar->lv_type = type;
2509
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002510 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002511 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002512 return NULL;
2513 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2514 vim_strsave(lvar->lv_name);
2515 ++dfunc->df_var_names.ga_len;
2516
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002517 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518}
2519
2520/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 * Remove local variables above "new_top".
2522 */
2523 static void
2524unwind_locals(cctx_T *cctx, int new_top)
2525{
2526 if (cctx->ctx_locals.ga_len > new_top)
2527 {
2528 int idx;
2529 lvar_T *lvar;
2530
2531 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2532 {
2533 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2534 vim_free(lvar->lv_name);
2535 }
2536 }
2537 cctx->ctx_locals.ga_len = new_top;
2538}
2539
2540/*
2541 * Free all local variables.
2542 */
2543 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002544free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002545{
2546 unwind_locals(cctx, 0);
2547 ga_clear(&cctx->ctx_locals);
2548}
2549
2550/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002551 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2552 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2553 * error if the variable was defined with :const.
2554 */
2555 static int
2556check_item_writable(svar_T *sv, int check_writable, char_u *name)
2557{
2558 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2559 || (check_writable == ASSIGN_FINAL
2560 && sv->sv_const == ASSIGN_CONST))
2561 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002562 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002563 return FAIL;
2564 }
2565 return OK;
2566}
2567
2568/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002570 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 * Returns the index in "sn_var_vals" if found.
2572 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002573 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 */
2575 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002576get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577{
2578 hashtab_T *ht;
2579 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002580 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002581 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 int idx;
2583
Bram Moolenaare3d46852020-08-29 13:39:17 +02002584 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002586 if (sid == current_sctx.sc_sid)
2587 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002588 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002589
2590 if (sav == NULL)
2591 return -2;
2592 idx = sav->sav_var_vals_idx;
2593 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002594 if (check_item_writable(sv, check_writable, name) == FAIL)
2595 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002596 return idx;
2597 }
2598
2599 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 ht = &SCRIPT_VARS(sid);
2601 di = find_var_in_ht(ht, 0, name, TRUE);
2602 if (di == NULL)
2603 return -2;
2604
2605 // Now find the svar_T index in sn_var_vals.
2606 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2607 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002608 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (sv->sv_tv == &di->di_tv)
2610 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002611 if (check_item_writable(sv, check_writable, name) == FAIL)
2612 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 return idx;
2614 }
2615 }
2616 return -1;
2617}
2618
2619/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002620 * Find "name" in imported items of the current script or in "cctx" if not
2621 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 */
2623 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002624find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 int idx;
2627
Bram Moolenaare3d46852020-08-29 13:39:17 +02002628 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002629 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (cctx != NULL)
2631 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2632 {
2633 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2634 + idx;
2635
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002636 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2637 : STRLEN(import->imp_name) == len
2638 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 return import;
2640 }
2641
Bram Moolenaarefa94442020-08-08 22:16:00 +02002642 return find_imported_in_script(name, len, current_sctx.sc_sid);
2643}
2644
2645 imported_T *
2646find_imported_in_script(char_u *name, size_t len, int sid)
2647{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002648 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002649 int idx;
2650
Bram Moolenaare3d46852020-08-29 13:39:17 +02002651 if (!SCRIPT_ID_VALID(sid))
2652 return NULL;
2653 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2655 {
2656 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2657
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002658 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2659 : STRLEN(import->imp_name) == len
2660 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 return import;
2662 }
2663 return NULL;
2664}
2665
2666/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002667 * Free all imported variables.
2668 */
2669 static void
2670free_imported(cctx_T *cctx)
2671{
2672 int idx;
2673
2674 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2675 {
2676 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2677
2678 vim_free(import->imp_name);
2679 }
2680 ga_clear(&cctx->ctx_imports);
2681}
2682
2683/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002684 * Return a pointer to the next line that isn't empty or only contains a
2685 * comment. Skips over white space.
2686 * Returns NULL if there is none.
2687 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002688 char_u *
2689peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002690{
2691 int lnum = cctx->ctx_lnum;
2692
2693 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2694 {
2695 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002696 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002697
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002698 // ignore NULLs inserted for continuation lines
2699 if (line != NULL)
2700 {
2701 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002702 if (vim9_bad_comment(p))
2703 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002704 if (*p != NUL && !vim9_comment_start(p))
2705 return p;
2706 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002707 }
2708 return NULL;
2709}
2710
2711/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002712 * Called when checking for a following operator at "arg". When the rest of
2713 * the line is empty or only a comment, peek the next line. If there is a next
2714 * line return a pointer to it and set "nextp".
2715 * Otherwise skip over white space.
2716 */
2717 static char_u *
2718may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2719{
2720 char_u *p = skipwhite(arg);
2721
2722 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002723 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002724 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002725 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002726 if (*nextp != NULL)
2727 return *nextp;
2728 }
2729 return p;
2730}
2731
2732/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002733 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002734 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002735 * Returns NULL when at the end.
2736 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002737 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002738next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002739{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002740 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002741
2742 do
2743 {
2744 ++cctx->ctx_lnum;
2745 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002746 {
2747 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002748 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002749 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002750 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002751 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002752 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002753 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002754 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002755 return line;
2756}
2757
2758/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002759 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002760 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002761 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002762 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2763 */
2764 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002765may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002766{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002767 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002768 if (vim9_bad_comment(*arg))
2769 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002770 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002771 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002773
2774 if (next == NULL)
2775 return FAIL;
2776 *arg = skipwhite(next);
2777 }
2778 return OK;
2779}
2780
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002781/*
2782 * Idem, and give an error when failed.
2783 */
2784 static int
2785may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2786{
2787 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2788 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002789 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002790 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002791 return FAIL;
2792 }
2793 return OK;
2794}
2795
2796
Bram Moolenaara5565e42020-05-09 15:44:01 +02002797// Structure passed between the compile_expr* functions to keep track of
2798// constants that have been parsed but for which no code was produced yet. If
2799// possible expressions on these constants are applied at compile time. If
2800// that is not possible, the code to push the constants needs to be generated
2801// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002802// Using 50 should be more than enough of 5 levels of ().
2803#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002804typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002805 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002806 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002807 int pp_is_const; // all generated code was constants, used for a
2808 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002809} ppconst_T;
2810
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002811static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002812static int compile_expr0(char_u **arg, cctx_T *cctx);
2813static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2814
Bram Moolenaara5565e42020-05-09 15:44:01 +02002815/*
2816 * Generate a PUSH instruction for "tv".
2817 * "tv" will be consumed or cleared.
2818 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2819 */
2820 static int
2821generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2822{
2823 if (tv != NULL)
2824 {
2825 switch (tv->v_type)
2826 {
2827 case VAR_UNKNOWN:
2828 break;
2829 case VAR_BOOL:
2830 generate_PUSHBOOL(cctx, tv->vval.v_number);
2831 break;
2832 case VAR_SPECIAL:
2833 generate_PUSHSPEC(cctx, tv->vval.v_number);
2834 break;
2835 case VAR_NUMBER:
2836 generate_PUSHNR(cctx, tv->vval.v_number);
2837 break;
2838#ifdef FEAT_FLOAT
2839 case VAR_FLOAT:
2840 generate_PUSHF(cctx, tv->vval.v_float);
2841 break;
2842#endif
2843 case VAR_BLOB:
2844 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2845 tv->vval.v_blob = NULL;
2846 break;
2847 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002848 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002849 tv->vval.v_string = NULL;
2850 break;
2851 default:
2852 iemsg("constant type not supported");
2853 clear_tv(tv);
2854 return FAIL;
2855 }
2856 tv->v_type = VAR_UNKNOWN;
2857 }
2858 return OK;
2859}
2860
2861/*
2862 * Generate code for any ppconst entries.
2863 */
2864 static int
2865generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2866{
2867 int i;
2868 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002869 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002870
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002871 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002872 for (i = 0; i < ppconst->pp_used; ++i)
2873 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2874 ret = FAIL;
2875 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002876 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002877 return ret;
2878}
2879
2880/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002881 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002882 */
2883 static int
2884check_ppconst_bool(ppconst_T *ppconst)
2885{
2886 if (ppconst->pp_used > 0)
2887 {
2888 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002889 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002890
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002891 return check_typval_type(&t_bool, tv, where);
2892 }
2893 return OK;
2894}
2895
2896/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002897 * Clear ppconst constants. Used when failing.
2898 */
2899 static void
2900clear_ppconst(ppconst_T *ppconst)
2901{
2902 int i;
2903
2904 for (i = 0; i < ppconst->pp_used; ++i)
2905 clear_tv(&ppconst->pp_tv[i]);
2906 ppconst->pp_used = 0;
2907}
2908
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002909/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002910 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002911 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002912 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002913 */
2914 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002915compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002916{
2917 type_T **typep;
2918 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002919 vartype_T vartype;
2920 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002921
Bram Moolenaar261417b2021-05-06 21:04:55 +02002922 // We can index a list, dict and blob. If we don't know the type
2923 // we can use the index value type. If we still don't know use an "ANY"
2924 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002925 typep = ((type_T **)stack->ga_data) + stack->ga_len
2926 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002927 vartype = (*typep)->tt_type;
2928 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002929 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002930 if (*typep == &t_any && idxtype == &t_string)
2931 vartype = VAR_DICT;
2932 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002933 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002934 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002935 return FAIL;
2936 if (is_slice)
2937 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2939 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940 FALSE, FALSE) == FAIL)
2941 return FAIL;
2942 }
2943 }
2944
Bram Moolenaar261417b2021-05-06 21:04:55 +02002945 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002946 {
2947 if (is_slice)
2948 {
2949 emsg(_(e_cannot_slice_dictionary));
2950 return FAIL;
2951 }
2952 if ((*typep)->tt_type == VAR_DICT)
2953 {
2954 *typep = (*typep)->tt_member;
2955 if (*typep == &t_unknown)
2956 // empty dict was used
2957 *typep = &t_any;
2958 }
2959 else
2960 {
2961 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2962 FALSE, FALSE) == FAIL)
2963 return FAIL;
2964 *typep = &t_any;
2965 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002966 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002967 return FAIL;
2968 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2969 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002970 if (keeping_dict != NULL)
2971 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002972 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002973 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002974 {
2975 *typep = &t_string;
2976 if ((is_slice
2977 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2978 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2979 return FAIL;
2980 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002981 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002982 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002983 if (is_slice)
2984 {
2985 *typep = &t_blob;
2986 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2987 return FAIL;
2988 }
2989 else
2990 {
2991 *typep = &t_number;
2992 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2993 return FAIL;
2994 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002995 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002996 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002997 {
2998 if (is_slice)
2999 {
3000 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003001 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003002 2) == FAIL)
3003 return FAIL;
3004 }
3005 else
3006 {
3007 if ((*typep)->tt_type == VAR_LIST)
3008 {
3009 *typep = (*typep)->tt_member;
3010 if (*typep == &t_unknown)
3011 // empty list was used
3012 *typep = &t_any;
3013 }
3014 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003015 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3016 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003017 return FAIL;
3018 }
3019 }
3020 else
3021 {
3022 emsg(_(e_string_list_dict_or_blob_required));
3023 return FAIL;
3024 }
3025 return OK;
3026}
3027
3028/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003029 * Generate an instruction to load script-local variable "name", without the
3030 * leading "s:".
3031 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 */
3033 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003034compile_load_scriptvar(
3035 cctx_T *cctx,
3036 char_u *name, // variable NUL terminated
3037 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003038 char_u **end, // end of variable
3039 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003041 scriptitem_T *si;
3042 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 imported_T *import;
3044
Bram Moolenaare3d46852020-08-29 13:39:17 +02003045 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3046 return FAIL;
3047 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003048 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003049 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003051 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003052 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3053 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 if (idx >= 0)
3056 {
3057 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3058
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003059 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 current_sctx.sc_sid, idx, sv->sv_type);
3061 return OK;
3062 }
3063
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003064 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 if (import != NULL)
3066 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003067 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003068 {
3069 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003070 char_u *exp_name;
3071 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003072 ufunc_T *ufunc;
3073 type_T *type;
3074
3075 // Used "import * as Name", need to lookup the member.
3076 if (*p != '.')
3077 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003078 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003079 return FAIL;
3080 }
3081 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003082 if (VIM_ISWHITE(*p))
3083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003084 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003085 return FAIL;
3086 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003087
Bram Moolenaar1c991142020-07-04 13:15:31 +02003088 // isolate one name
3089 exp_name = p;
3090 while (eval_isnamec(*p))
3091 ++p;
3092 cc = *p;
3093 *p = NUL;
3094
Bram Moolenaaredba7072021-03-13 21:14:18 +01003095 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3096 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003097 *p = cc;
3098 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003099 *end = p;
3100
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003101 if (idx < 0)
3102 {
3103 if (*p == '(' && ufunc != NULL)
3104 {
3105 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3106 return OK;
3107 }
3108 return FAIL;
3109 }
3110
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003111 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3112 import->imp_sid,
3113 idx,
3114 type);
3115 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003116 else if (import->imp_funcname != NULL)
3117 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003118 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003119 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3120 import->imp_sid,
3121 import->imp_var_vals_idx,
3122 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 return OK;
3124 }
3125
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003126 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003127 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 return FAIL;
3129}
3130
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003131 static int
3132generate_funcref(cctx_T *cctx, char_u *name)
3133{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003134 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003135
3136 if (ufunc == NULL)
3137 return FAIL;
3138
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003139 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003140 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3141 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003142 == FAIL)
3143 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003144 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003145}
3146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147/*
3148 * Compile a variable name into a load instruction.
3149 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003150 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 * When "error" is FALSE do not give an error when not found.
3152 */
3153 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003154compile_load(
3155 char_u **arg,
3156 char_u *end_arg,
3157 cctx_T *cctx,
3158 int is_expr,
3159 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160{
3161 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003163 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003165 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166
3167 if (*(*arg + 1) == ':')
3168 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003169 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003170 {
3171 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
Bram Moolenaarfa596382021-04-07 21:58:16 +02003173 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 switch (**arg)
3175 {
3176 case 'g': isn_type = ISN_LOADGDICT; break;
3177 case 'w': isn_type = ISN_LOADWDICT; break;
3178 case 't': isn_type = ISN_LOADTDICT; break;
3179 case 'b': isn_type = ISN_LOADBDICT; break;
3180 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003181 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003182 goto theend;
3183 }
3184 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3185 goto theend;
3186 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 else
3189 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003190 isntype_T isn_type = ISN_DROP;
3191
Bram Moolenaarfa596382021-04-07 21:58:16 +02003192 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3194 if (name == NULL)
3195 return FAIL;
3196
3197 switch (**arg)
3198 {
3199 case 'v': res = generate_LOADV(cctx, name, error);
3200 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003201 case 's': if (is_expr && ASCII_ISUPPER(*name)
3202 && find_func(name, FALSE, cctx) != NULL)
3203 res = generate_funcref(cctx, name);
3204 else
3205 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003206 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003207 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003208 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003209 {
3210 if (is_expr && ASCII_ISUPPER(*name)
3211 && find_func(name, FALSE, cctx) != NULL)
3212 res = generate_funcref(cctx, name);
3213 else
3214 isn_type = ISN_LOADG;
3215 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003216 else
3217 {
3218 isn_type = ISN_LOADAUTO;
3219 vim_free(name);
3220 name = vim_strnsave(*arg, end - *arg);
3221 if (name == NULL)
3222 return FAIL;
3223 }
3224 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003225 case 'w': isn_type = ISN_LOADW; break;
3226 case 't': isn_type = ISN_LOADT; break;
3227 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003228 default: // cannot happen, just in case
3229 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003230 goto theend;
3231 }
3232 if (isn_type != ISN_DROP)
3233 {
3234 // Global, Buffer-local, Window-local and Tabpage-local
3235 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003236 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003237 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 }
3240 }
3241 else
3242 {
3243 size_t len = end - *arg;
3244 int idx;
3245 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003246 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
3248 name = vim_strnsave(*arg, end - *arg);
3249 if (name == NULL)
3250 return FAIL;
3251
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003252 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3253 {
3254 script_autoload(name, FALSE);
3255 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3256 }
3257 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3258 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003260 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003261 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 }
3263 else
3264 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003265 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003266
Bram Moolenaar709664c2020-12-12 14:33:41 +01003267 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003269 type = lvar.lv_type;
3270 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003271 if (lvar.lv_from_outer != 0)
3272 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003273 else
3274 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 }
3276 else
3277 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003278 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003279 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003280 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003281 || find_imported(name, 0, cctx) != NULL)
3282 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003283
Bram Moolenaar0f769812020-09-12 18:32:34 +02003284 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003285 // uppercase letter it can be a user defined function.
3286 // generate_funcref() will fail if the function can't be found.
3287 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003288 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
3290 }
3291 if (gen_load)
3292 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003293 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003294 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003295 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003296 cctx->ctx_outer_used = TRUE;
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 }
3299
3300 *arg = end;
3301
3302theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003303 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003304 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 vim_free(name);
3306 return res;
3307}
3308
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 static void
3310clear_instr_ga(garray_T *gap)
3311{
3312 int idx;
3313
3314 for (idx = 0; idx < gap->ga_len; ++idx)
3315 delete_instr(((isn_T *)gap->ga_data) + idx);
3316 ga_clear(gap);
3317}
3318
3319/*
3320 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3321 * Returns FAIL if compilation fails.
3322 */
3323 static int
3324compile_string(isn_T *isn, cctx_T *cctx)
3325{
3326 char_u *s = isn->isn_arg.string;
3327 garray_T save_ga = cctx->ctx_instr;
3328 int expr_res;
3329 int trailing_error;
3330 int instr_count;
3331 isn_T *instr = NULL;
3332
Bram Moolenaarcd268012021-07-22 19:11:08 +02003333 // Remove the string type from the stack.
3334 --cctx->ctx_type_stack.ga_len;
3335
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003336 // Temporarily reset the list of instructions so that the jump labels are
3337 // correct.
3338 cctx->ctx_instr.ga_len = 0;
3339 cctx->ctx_instr.ga_maxlen = 0;
3340 cctx->ctx_instr.ga_data = NULL;
3341 expr_res = compile_expr0(&s, cctx);
3342 s = skipwhite(s);
3343 trailing_error = *s != NUL;
3344
Bram Moolenaarff652882021-05-16 15:24:49 +02003345 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003346 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003347 {
3348 if (trailing_error)
3349 semsg(_(e_trailing_arg), s);
3350 clear_instr_ga(&cctx->ctx_instr);
3351 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003352 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003353 return FAIL;
3354 }
3355
3356 // Move the generated instructions into the ISN_INSTR instruction, then
3357 // restore the list of instructions.
3358 instr_count = cctx->ctx_instr.ga_len;
3359 instr = cctx->ctx_instr.ga_data;
3360 instr[instr_count].isn_type = ISN_FINISH;
3361
3362 cctx->ctx_instr = save_ga;
3363 vim_free(isn->isn_arg.string);
3364 isn->isn_type = ISN_INSTR;
3365 isn->isn_arg.instr = instr;
3366 return OK;
3367}
3368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369/*
3370 * Compile the argument expressions.
3371 * "arg" points to just after the "(" and is advanced to after the ")"
3372 */
3373 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003374compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003376 char_u *p = *arg;
3377 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003378 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003379 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaare6085c52020-04-12 20:19:16 +02003381 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003383 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3384 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003385 if (*p == ')')
3386 {
3387 *arg = p + 1;
3388 return OK;
3389 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003390 if (must_end)
3391 {
3392 semsg(_(e_missing_comma_before_argument_str), p);
3393 return FAIL;
3394 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003395
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003396 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003397 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return FAIL;
3399 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003400
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003401 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003402 && cctx->ctx_instr.ga_len == instr_count + 1)
3403 {
3404 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3405
3406 // {skip} argument of searchpair() can be compiled if not empty
3407 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3408 compile_string(isn, cctx);
3409 }
3410
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003411 if (*p != ',' && *skipwhite(p) == ',')
3412 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003413 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003414 p = skipwhite(p);
3415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003417 {
3418 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003419 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003420 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003421 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003422 else
3423 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003424 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003425 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003427failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003428 emsg(_(e_missing_close));
3429 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430}
3431
3432/*
3433 * Compile a function call: name(arg1, arg2)
3434 * "arg" points to "name", "arg + varlen" to the "(".
3435 * "argcount_init" is 1 for "value->method()"
3436 * Instructions:
3437 * EVAL arg1
3438 * EVAL arg2
3439 * BCALL / DCALL / UCALL
3440 */
3441 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003442compile_call(
3443 char_u **arg,
3444 size_t varlen,
3445 cctx_T *cctx,
3446 ppconst_T *ppconst,
3447 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448{
3449 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003450 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 int argcount = argcount_init;
3452 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003453 char_u fname_buf[FLEN_FIXED + 1];
3454 char_u *tofree = NULL;
3455 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003456 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003457 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003458 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003459 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003461 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003462 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003463 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003464 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003465 {
3466 char_u *s = skipwhite(*arg + varlen + 1);
3467 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003468 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003469
3470 argvars[0].v_type = VAR_UNKNOWN;
3471 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003472 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003473 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003474 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003475 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003476 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003477 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003478 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003479 {
3480 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3481
3482 *arg = s + 1;
3483 argvars[1].v_type = VAR_UNKNOWN;
3484 tv->v_type = VAR_NUMBER;
3485 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003486 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003487 f_has(argvars, tv);
3488 else
3489 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003490 clear_tv(&argvars[0]);
3491 ++ppconst->pp_used;
3492 return OK;
3493 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003494 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003495 if (!is_has)
3496 {
3497 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3498 return FAIL;
3499 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003500 }
3501
3502 if (generate_ppconst(cctx, ppconst) == FAIL)
3503 return FAIL;
3504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 if (varlen >= sizeof(namebuf))
3506 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003507 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 return FAIL;
3509 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003510 vim_strncpy(namebuf, *arg, varlen);
3511 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003513 // We handle the "skip" argument of searchpair() and searchpairpos()
3514 // differently.
3515 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3516 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3517 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3518 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003521 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523
Bram Moolenaar03290b82020-12-19 16:30:44 +01003524 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003525 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 {
3527 int idx;
3528
3529 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003530 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003532 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003533 if (STRCMP(name, "flatten") == 0)
3534 {
3535 emsg(_(e_cannot_use_flatten_in_vim9_script));
3536 goto theend;
3537 }
3538
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003539 if (STRCMP(name, "add") == 0 && argcount == 2)
3540 {
3541 garray_T *stack = &cctx->ctx_type_stack;
3542 type_T *type = ((type_T **)stack->ga_data)[
3543 stack->ga_len - 2];
3544
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003545 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003546 if (type->tt_type == VAR_LIST)
3547 {
3548 // inline "add(list, item)" so that the type can be checked
3549 res = generate_LISTAPPEND(cctx);
3550 idx = -1;
3551 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003552 else if (type->tt_type == VAR_BLOB)
3553 {
3554 // inline "add(blob, nr)" so that the type can be checked
3555 res = generate_BLOBAPPEND(cctx);
3556 idx = -1;
3557 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003558 }
3559
3560 if (idx >= 0)
3561 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3562 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003563 else
3564 semsg(_(e_unknownfunc), namebuf);
3565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 }
3567
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003568 // An argument or local variable can be a function reference, this
3569 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003570 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003571 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003572 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003573 // If we can find the function by name generate the right call.
3574 // Skip global functions here, a local funcref takes precedence.
3575 ufunc = find_func(name, FALSE, cctx);
3576 if (ufunc != NULL && !func_is_global(ufunc))
3577 {
3578 res = generate_CALL(cctx, ufunc, argcount);
3579 goto theend;
3580 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582
3583 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003584 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003585 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003587 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003588 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003589 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003591 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003592
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003593 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003594 goto theend;
3595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
Bram Moolenaar0f769812020-09-12 18:32:34 +02003597 // If we can find a global function by name generate the right call.
3598 if (ufunc != NULL)
3599 {
3600 res = generate_CALL(cctx, ufunc, argcount);
3601 goto theend;
3602 }
3603
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003604 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003605 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003606 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003607 res = generate_UCALL(cctx, name, argcount);
3608 else
3609 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003610
3611theend:
3612 vim_free(tofree);
3613 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614}
3615
3616// like NAMESPACE_CHAR but with 'a' and 'l'.
3617#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3618
3619/*
3620 * Find the end of a variable or function name. Unlike find_name_end() this
3621 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003622 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 * Return a pointer to just after the name. Equal to "arg" if there is no
3624 * valid name.
3625 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003626 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003627to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628{
3629 char_u *p;
3630
3631 // Quick check for valid starting character.
3632 if (!eval_isnamec1(*arg))
3633 return arg;
3634
3635 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3636 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3637 // and can be used in slice "[n:]".
3638 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003639 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3641 break;
3642 return p;
3643}
3644
3645/*
3646 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003647 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003648 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 */
3650 char_u *
3651to_name_const_end(char_u *arg)
3652{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003653 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 typval_T rettv;
3655
Bram Moolenaar678b2072021-07-26 21:10:11 +02003656 if (STRNCMP(p, "<SNR>", 5) == 0)
3657 p = skipdigits(p + 5);
3658 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 if (p == arg && *arg == '[')
3660 {
3661
3662 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003663 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 p = arg;
3665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return p;
3667}
3668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669/*
3670 * parse a list: [expr, expr]
3671 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003672 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 */
3674 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676{
3677 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003678 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003680 int is_const;
3681 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003683 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003685 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003686 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003687 semsg(_(e_list_end), *arg);
3688 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003689 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003690 if (*p == ',')
3691 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003692 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003693 return FAIL;
3694 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003695 if (*p == ']')
3696 {
3697 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003698 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003699 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003700 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003701 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003702 if (!is_const)
3703 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 ++count;
3705 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003706 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003708 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3709 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003710 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003711 return FAIL;
3712 }
3713 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003714 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 p = skipwhite(p);
3716 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003717 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003719 ppconst->pp_is_const = is_all_const;
3720 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721}
3722
3723/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003724 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003725 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003726 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 */
3728 static int
3729compile_lambda(char_u **arg, cctx_T *cctx)
3730{
Bram Moolenaare462f522020-12-27 14:43:30 +01003731 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 typval_T rettv;
3733 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003734 evalarg_T evalarg;
3735
Bram Moolenaar844fb642021-10-23 13:32:30 +01003736 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 evalarg.eval_flags = EVAL_EVALUATE;
3738 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739
3740 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003741 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3742 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003743 {
3744 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003745 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003746 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003747
Bram Moolenaar65c44152020-12-24 15:14:01 +01003748 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003750 ++ufunc->uf_refcount;
3751 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
Bram Moolenaara9931532021-06-12 15:58:16 +02003753 // Compile it here to get the return type. The return type is optional,
3754 // when it's missing use t_unknown. This is recognized in
3755 // compile_return().
3756 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3757 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003758 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759
Bram Moolenaar648594e2021-07-11 17:55:01 +02003760#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003761 // When the outer function is compiled for profiling, the lambda may be
3762 // called without profiling. Compile it here in the right context.
3763 if (cctx->ctx_compile_type == CT_PROFILE)
3764 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003765#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003766
Bram Moolenaar844fb642021-10-23 13:32:30 +01003767 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3768 // "*arg" may point into it. Point into the original line to avoid a
3769 // dangling pointer.
3770 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003771 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003772 garray_T *gap = &evalarg.eval_tofree_ga;
3773 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003774
3775 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3776 + off;
3777 }
3778
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003779 clear_evalarg(&evalarg, NULL);
3780
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003781 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003782 {
3783 // The return type will now be known.
3784 set_function_type(ufunc);
3785
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003786 // The function reference count will be 1. When the ISN_FUNCREF
3787 // instruction is deleted the reference count is decremented and the
3788 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003789 return generate_FUNCREF(cctx, ufunc);
3790 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003791
3792 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 return FAIL;
3794}
3795
3796/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003797 * Get a lambda and compile it. Uses Vim9 syntax.
3798 */
3799 int
3800get_lambda_tv_and_compile(
3801 char_u **arg,
3802 typval_T *rettv,
3803 int types_optional,
3804 evalarg_T *evalarg)
3805{
3806 int r;
3807 ufunc_T *ufunc;
3808 int save_sc_version = current_sctx.sc_version;
3809
3810 // Get the funcref in "rettv".
3811 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3812 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3813 current_sctx.sc_version = save_sc_version;
3814 if (r != OK)
3815 return r;
3816
3817 // "rettv" will now be a partial referencing the function.
3818 ufunc = rettv->vval.v_partial->pt_func;
3819
3820 // Compile it here to get the return type. The return type is optional,
3821 // when it's missing use t_unknown. This is recognized in
3822 // compile_return().
3823 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3824 ufunc->uf_ret_type = &t_unknown;
3825 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3826
3827 if (ufunc->uf_def_status == UF_COMPILED)
3828 {
3829 // The return type will now be known.
3830 set_function_type(ufunc);
3831 return OK;
3832 }
3833 clear_tv(rettv);
3834 return FAIL;
3835}
3836
3837/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003838 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003840 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 */
3842 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003843compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844{
3845 garray_T *instr = &cctx->ctx_instr;
3846 int count = 0;
3847 dict_T *d = dict_alloc();
3848 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003849 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003850 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003851 int is_const;
3852 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
3854 if (d == NULL)
3855 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003856 if (generate_ppconst(cctx, ppconst) == FAIL)
3857 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003858 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003860 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
Bram Moolenaar23c55272020-06-21 16:58:13 +02003862 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003863 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003864 *arg = NULL;
3865 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003866 }
3867
3868 if (**arg == '}')
3869 break;
3870
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003871 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003873 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003875 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003876 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003877 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003880 if (isn->isn_type == ISN_PUSHNR)
3881 {
3882 char buf[NUMBUFLEN];
3883
3884 // Convert to string at compile time.
3885 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3886 isn->isn_type = ISN_PUSHS;
3887 isn->isn_arg.string = vim_strsave((char_u *)buf);
3888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (isn->isn_type == ISN_PUSHS)
3890 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003891 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003892 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003893 *arg = skipwhite(*arg);
3894 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003895 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003896 emsg(_(e_missing_matching_bracket_after_dict_key));
3897 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003898 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003899 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003901 else
3902 {
3903 // {"name": value},
3904 // {'name': value},
3905 // {name: value} use "name" as a literal key
3906 key = get_literal_key(arg);
3907 if (key == NULL)
3908 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003909 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003910 return FAIL;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912
3913 // Check for duplicate keys, if using string keys.
3914 if (key != NULL)
3915 {
3916 item = dict_find(d, key, -1);
3917 if (item != NULL)
3918 {
3919 semsg(_(e_duplicate_key), key);
3920 goto failret;
3921 }
3922 item = dictitem_alloc(key);
3923 if (item != NULL)
3924 {
3925 item->di_tv.v_type = VAR_UNKNOWN;
3926 item->di_tv.v_lock = 0;
3927 if (dict_add(d, item) == FAIL)
3928 dictitem_free(item);
3929 }
3930 }
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 if (**arg != ':')
3933 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003934 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003935 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003936 else
3937 semsg(_(e_missing_dict_colon), *arg);
3938 return FAIL;
3939 }
3940 whitep = *arg + 1;
3941 if (!IS_WHITE_OR_NUL(*whitep))
3942 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003943 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 return FAIL;
3945 }
3946
Bram Moolenaar23c55272020-06-21 16:58:13 +02003947 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003948 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003949 *arg = NULL;
3950 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003951 }
3952
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003953 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003955 if (!is_const)
3956 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 ++count;
3958
Bram Moolenaar2c330432020-04-13 14:41:35 +02003959 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003960 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003961 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003962 *arg = NULL;
3963 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 if (**arg == '}')
3966 break;
3967 if (**arg != ',')
3968 {
3969 semsg(_(e_missing_dict_comma), *arg);
3970 goto failret;
3971 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003972 if (IS_WHITE_OR_NUL(*whitep))
3973 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003974 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003975 return FAIL;
3976 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003977 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003978 if (!IS_WHITE_OR_NUL(*whitep))
3979 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003980 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003981 return FAIL;
3982 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003983 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 }
3985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 *arg = *arg + 1;
3987
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003988 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003989 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003990 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003991 *arg += STRLEN(*arg);
3992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003994 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 return generate_NEWDICT(cctx, count);
3996
3997failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003998 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003999 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004000 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004001 *arg = (char_u *)"";
4002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 dict_unref(d);
4004 return FAIL;
4005}
4006
4007/*
4008 * Compile "&option".
4009 */
4010 static int
4011compile_get_option(char_u **arg, cctx_T *cctx)
4012{
4013 typval_T rettv;
4014 char_u *start = *arg;
4015 int ret;
4016
4017 // parse the option and get the current value to get the type.
4018 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004019 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 if (ret == OK)
4021 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004022 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004023 char_u *name = vim_strnsave(start, *arg - start);
4024 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4025 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
4027 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4028 vim_free(name);
4029 }
4030 clear_tv(&rettv);
4031
4032 return ret;
4033}
4034
4035/*
4036 * Compile "$VAR".
4037 */
4038 static int
4039compile_get_env(char_u **arg, cctx_T *cctx)
4040{
4041 char_u *start = *arg;
4042 int len;
4043 int ret;
4044 char_u *name;
4045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 ++*arg;
4047 len = get_env_len(arg);
4048 if (len == 0)
4049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004050 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052 }
4053
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004054 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 name = vim_strnsave(start, len + 1);
4056 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4057 vim_free(name);
4058 return ret;
4059}
4060
4061/*
4062 * Compile "@r".
4063 */
4064 static int
4065compile_get_register(char_u **arg, cctx_T *cctx)
4066{
4067 int ret;
4068
4069 ++*arg;
4070 if (**arg == NUL)
4071 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004072 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004075 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 {
4077 emsg_invreg(**arg);
4078 return FAIL;
4079 }
4080 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4081 ++*arg;
4082 return ret;
4083}
4084
4085/*
4086 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004087 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 */
4089 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004090apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004092 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
4094 // this works from end to start
4095 while (p > start)
4096 {
4097 --p;
4098 if (*p == '-' || *p == '+')
4099 {
4100 // only '-' has an effect, for '+' we only check the type
4101#ifdef FEAT_FLOAT
4102 if (rettv->v_type == VAR_FLOAT)
4103 {
4104 if (*p == '-')
4105 rettv->vval.v_float = -rettv->vval.v_float;
4106 }
4107 else
4108#endif
4109 {
4110 varnumber_T val;
4111 int error = FALSE;
4112
4113 // tv_get_number_chk() accepts a string, but we don't want that
4114 // here
4115 if (check_not_string(rettv) == FAIL)
4116 return FAIL;
4117 val = tv_get_number_chk(rettv, &error);
4118 clear_tv(rettv);
4119 if (error)
4120 return FAIL;
4121 if (*p == '-')
4122 val = -val;
4123 rettv->v_type = VAR_NUMBER;
4124 rettv->vval.v_number = val;
4125 }
4126 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004127 else if (numeric_only)
4128 {
4129 ++p;
4130 break;
4131 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004132 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 {
4134 int v = tv2bool(rettv);
4135
4136 // '!' is permissive in the type.
4137 clear_tv(rettv);
4138 rettv->v_type = VAR_BOOL;
4139 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4140 }
4141 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004142 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 return OK;
4144}
4145
4146/*
4147 * Recognize v: variables that are constants and set "rettv".
4148 */
4149 static void
4150get_vim_constant(char_u **arg, typval_T *rettv)
4151{
4152 if (STRNCMP(*arg, "v:true", 6) == 0)
4153 {
4154 rettv->v_type = VAR_BOOL;
4155 rettv->vval.v_number = VVAL_TRUE;
4156 *arg += 6;
4157 }
4158 else if (STRNCMP(*arg, "v:false", 7) == 0)
4159 {
4160 rettv->v_type = VAR_BOOL;
4161 rettv->vval.v_number = VVAL_FALSE;
4162 *arg += 7;
4163 }
4164 else if (STRNCMP(*arg, "v:null", 6) == 0)
4165 {
4166 rettv->v_type = VAR_SPECIAL;
4167 rettv->vval.v_number = VVAL_NULL;
4168 *arg += 6;
4169 }
4170 else if (STRNCMP(*arg, "v:none", 6) == 0)
4171 {
4172 rettv->v_type = VAR_SPECIAL;
4173 rettv->vval.v_number = VVAL_NONE;
4174 *arg += 6;
4175 }
4176}
4177
Bram Moolenaar657137c2021-01-09 15:45:23 +01004178 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004179get_compare_type(char_u *p, int *len, int *type_is)
4180{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004181 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004182 int i;
4183
4184 switch (p[0])
4185 {
4186 case '=': if (p[1] == '=')
4187 type = EXPR_EQUAL;
4188 else if (p[1] == '~')
4189 type = EXPR_MATCH;
4190 break;
4191 case '!': if (p[1] == '=')
4192 type = EXPR_NEQUAL;
4193 else if (p[1] == '~')
4194 type = EXPR_NOMATCH;
4195 break;
4196 case '>': if (p[1] != '=')
4197 {
4198 type = EXPR_GREATER;
4199 *len = 1;
4200 }
4201 else
4202 type = EXPR_GEQUAL;
4203 break;
4204 case '<': if (p[1] != '=')
4205 {
4206 type = EXPR_SMALLER;
4207 *len = 1;
4208 }
4209 else
4210 type = EXPR_SEQUAL;
4211 break;
4212 case 'i': if (p[1] == 's')
4213 {
4214 // "is" and "isnot"; but not a prefix of a name
4215 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4216 *len = 5;
4217 i = p[*len];
4218 if (!isalnum(i) && i != '_')
4219 {
4220 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4221 *type_is = TRUE;
4222 }
4223 }
4224 break;
4225 }
4226 return type;
4227}
4228
4229/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004230 * Skip over an expression, ignoring most errors.
4231 */
4232 static void
4233skip_expr_cctx(char_u **arg, cctx_T *cctx)
4234{
4235 evalarg_T evalarg;
4236
Bram Moolenaar844fb642021-10-23 13:32:30 +01004237 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004238 evalarg.eval_cctx = cctx;
4239 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004240 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004241}
4242
4243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004245 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 */
4247 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004248compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004250 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
4252 // this works from end to start
4253 while (p > start)
4254 {
4255 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004256 while (VIM_ISWHITE(*p))
4257 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 if (*p == '-' || *p == '+')
4259 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004260 int negate = *p == '-';
4261 isn_T *isn;
4262 garray_T *stack = &cctx->ctx_type_stack;
4263 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004265 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4266 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4267 return FAIL;
4268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4270 {
4271 --p;
4272 if (*p == '-')
4273 negate = !negate;
4274 }
4275 // only '-' has an effect, for '+' we only check the type
4276 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004277 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004279 if (isn == NULL)
4280 return FAIL;
4281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004283 else if (numeric_only)
4284 {
4285 ++p;
4286 break;
4287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 else
4289 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004290 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004292 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004294 if (p[-1] == '!')
4295 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004298 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 return FAIL;
4300 }
4301 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004302 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return OK;
4304}
4305
4306/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004307 * Compile "(expression)": recursive!
4308 * Return FAIL/OK.
4309 */
4310 static int
4311compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4312{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004313 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004314 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004315
Bram Moolenaar24156692021-01-14 20:35:49 +01004316 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4317 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004318 if (ppconst->pp_used <= PPSIZE - 10)
4319 {
4320 ret = compile_expr1(arg, cctx, ppconst);
4321 }
4322 else
4323 {
4324 // Not enough space in ppconst, flush constants.
4325 if (generate_ppconst(cctx, ppconst) == FAIL)
4326 return FAIL;
4327 ret = compile_expr0(arg, cctx);
4328 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004329 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4330 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004331 if (**arg == ')')
4332 ++*arg;
4333 else if (ret == OK)
4334 {
4335 emsg(_(e_missing_close));
4336 ret = FAIL;
4337 }
4338 return ret;
4339}
4340
4341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004343 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 */
4345 static int
4346compile_subscript(
4347 char_u **arg,
4348 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004349 char_u *start_leader,
4350 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004351 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004353 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004354 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 for (;;)
4357 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004358 char_u *p = skipwhite(*arg);
4359
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004360 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004361 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004362 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004363
4364 // If a following line starts with "->{" or "->X" advance to that
4365 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004366 // Also if a following line starts with ".x".
4367 if (next != NULL &&
4368 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004369 && (next[2] == '{'
4370 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004371 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004372 {
4373 next = next_line_from_context(cctx, TRUE);
4374 if (next == NULL)
4375 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004376 *arg = next;
4377 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004378 }
4379 }
4380
Bram Moolenaarcd268012021-07-22 19:11:08 +02004381 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4382 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004383 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004385 garray_T *stack = &cctx->ctx_type_stack;
4386 type_T *type;
4387 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004389 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004390 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004391 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004394 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4395
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004396 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004397 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004399 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004401 if (keeping_dict)
4402 {
4403 keeping_dict = FALSE;
4404 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4405 return FAIL;
4406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004408 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004410 char_u *pstart = p;
4411
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004412 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004413 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004414 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 // something->method()
4417 // Apply the '!', '-' and '+' first:
4418 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004419 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004422 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004423 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004424 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004425 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004426 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004427 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004428 garray_T *stack = &cctx->ctx_type_stack;
4429 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004430 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004431 int expr_isn_start = cctx->ctx_instr.ga_len;
4432 int expr_isn_end;
4433 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004434
4435 // Funcref call: list->(Refs[2])(arg)
4436 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004437 //
4438 // Fist compile the function expression.
4439 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004440 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004441
4442 // Remember the next instruction index, where the instructions
4443 // for arguments are being written.
4444 expr_isn_end = cctx->ctx_instr.ga_len;
4445
4446 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004447 if (**arg != '(')
4448 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004449 if (*skipwhite(*arg) == '(')
4450 emsg(_(e_nowhitespace));
4451 else
4452 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004453 return FAIL;
4454 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004455 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004456 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004457 return FAIL;
4458
Bram Moolenaar2927c072021-04-05 19:41:21 +02004459 // Move the instructions for the arguments to before the
4460 // instructions of the expression and move the type of the
4461 // expression after the argument types. This is what ISN_PCALL
4462 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004463 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004464 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4465 if (arg_isn_count > 0)
4466 {
4467 int expr_isn_count = expr_isn_end - expr_isn_start;
4468 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4469
4470 if (isn == NULL)
4471 return FAIL;
4472 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4473 + expr_isn_start,
4474 sizeof(isn_T) * expr_isn_count);
4475 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4476 + expr_isn_start,
4477 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4478 sizeof(isn_T) * arg_isn_count);
4479 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4480 + expr_isn_start + arg_isn_count,
4481 isn, sizeof(isn_T) * expr_isn_count);
4482 vim_free(isn);
4483
4484 type = ((type_T **)stack->ga_data)[type_idx_start];
4485 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4486 ((type_T **)stack->ga_data) + type_idx_start + 1,
4487 sizeof(type_T *)
4488 * (stack->ga_len - type_idx_start - 1));
4489 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4490 }
4491
Bram Moolenaar7e368202020-12-25 21:56:57 +01004492 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004493 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495 }
4496 else
4497 {
4498 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004499 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004500 if (!eval_isnamec1(*p))
4501 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004502 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004503 return FAIL;
4504 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004505 if (ASCII_ISALPHA(*p) && p[1] == ':')
4506 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004507 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 ;
4509 if (*p != '(')
4510 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004511 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 return FAIL;
4513 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return FAIL;
4516 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004517 if (keeping_dict)
4518 {
4519 keeping_dict = FALSE;
4520 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4521 return FAIL;
4522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004524 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004526 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004527
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004528 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004529 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004530 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004531 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004532 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004533 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004534 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004536 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004537 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004538 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004539 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004540 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004541 // missing first index is equal to zero
4542 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004543 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004544 else
4545 {
4546 if (compile_expr0(arg, cctx) == FAIL)
4547 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004548 if (**arg == ':')
4549 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004550 semsg(_(e_white_space_required_before_and_after_str_at_str),
4551 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004552 return FAIL;
4553 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004554 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004555 return FAIL;
4556 *arg = skipwhite(*arg);
4557 }
4558 if (**arg == ':')
4559 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004560 is_slice = TRUE;
4561 ++*arg;
4562 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4563 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004564 semsg(_(e_white_space_required_before_and_after_str_at_str),
4565 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004566 return FAIL;
4567 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004568 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004569 return FAIL;
4570 if (**arg == ']')
4571 // missing second index is equal to end of string
4572 generate_PUSHNR(cctx, -1);
4573 else
4574 {
4575 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004576 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004577 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004578 return FAIL;
4579 *arg = skipwhite(*arg);
4580 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582
4583 if (**arg != ']')
4584 {
4585 emsg(_(e_missbrac));
4586 return FAIL;
4587 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004588 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004590 if (keeping_dict)
4591 {
4592 keeping_dict = FALSE;
4593 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4594 return FAIL;
4595 }
4596 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004599 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004601 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004602 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004603 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004604 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004605
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004606 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004607 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004608 {
4609 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004610 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004611 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004612 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004613 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 while (eval_isnamec(*p))
4615 MB_PTR_ADV(p);
4616 if (p == *arg)
4617 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004618 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 return FAIL;
4620 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004621 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4622 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004623 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004625 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 *arg = p;
4627 }
4628 else
4629 break;
4630 }
4631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004633 // This needs to be done at runtime to be able to check the type.
4634 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
4637 return OK;
4638}
4639
4640/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004641 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4642 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004644 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004645 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646 *
4647 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 */
4649
4650/*
4651 * number number constant
4652 * 0zFFFFFFFF Blob constant
4653 * "string" string constant
4654 * 'string' literal string constant
4655 * &option-name option value
4656 * @r register contents
4657 * identifier variable value
4658 * function() function call
4659 * $VAR environment variable
4660 * (expression) nested expression
4661 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004662 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 *
4664 * Also handle:
4665 * ! in front logical NOT
4666 * - in front unary minus
4667 * + in front unary plus (ignored)
4668 * trailing (arg) funcref/partial call
4669 * trailing [] subscript in String or List
4670 * trailing .name entry in Dictionary
4671 * trailing ->name() method call
4672 */
4673 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004674compile_expr7(
4675 char_u **arg,
4676 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004677 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 char_u *start_leader, *end_leader;
4680 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004681 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004682 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004684 ppconst->pp_is_const = FALSE;
4685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 /*
4687 * Skip '!', '-' and '+' characters. They are handled later.
4688 */
4689 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004690 if (eval_leader(arg, TRUE) == FAIL)
4691 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 end_leader = *arg;
4693
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004694 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 switch (**arg)
4696 {
4697 /*
4698 * Number constant.
4699 */
4700 case '0': // also for blob starting with 0z
4701 case '1':
4702 case '2':
4703 case '3':
4704 case '4':
4705 case '5':
4706 case '6':
4707 case '7':
4708 case '8':
4709 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004710 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004712 // Apply "-" and "+" just before the number now, right to
4713 // left. Matters especially when "->" follows. Stops at
4714 // '!'.
4715 if (apply_leader(rettv, TRUE,
4716 start_leader, &end_leader) == FAIL)
4717 {
4718 clear_tv(rettv);
4719 return FAIL;
4720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 break;
4722
4723 /*
4724 * String constant: "string".
4725 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004726 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return FAIL;
4728 break;
4729
4730 /*
4731 * Literal string constant: 'str''ing'.
4732 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004733 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 return FAIL;
4735 break;
4736
4737 /*
4738 * Constant Vim variable.
4739 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004740 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 ret = NOTDONE;
4742 break;
4743
4744 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 * "true" constant
4746 */
4747 case 't': if (STRNCMP(*arg, "true", 4) == 0
4748 && !eval_isnamec((*arg)[4]))
4749 {
4750 *arg += 4;
4751 rettv->v_type = VAR_BOOL;
4752 rettv->vval.v_number = VVAL_TRUE;
4753 }
4754 else
4755 ret = NOTDONE;
4756 break;
4757
4758 /*
4759 * "false" constant
4760 */
4761 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4762 && !eval_isnamec((*arg)[5]))
4763 {
4764 *arg += 5;
4765 rettv->v_type = VAR_BOOL;
4766 rettv->vval.v_number = VVAL_FALSE;
4767 }
4768 else
4769 ret = NOTDONE;
4770 break;
4771
4772 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004773 * "null" constant
4774 */
4775 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004776 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004777 {
4778 *arg += 4;
4779 rettv->v_type = VAR_SPECIAL;
4780 rettv->vval.v_number = VVAL_NULL;
4781 }
4782 else
4783 ret = NOTDONE;
4784 break;
4785
4786 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 * List: [expr, expr]
4788 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004789 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4790 return FAIL;
4791 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 break;
4793
4794 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 * Dictionary: {'key': val, 'key': val}
4796 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004797 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4798 return FAIL;
4799 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 break;
4801
4802 /*
4803 * Option value: &name
4804 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004805 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4806 return FAIL;
4807 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 break;
4809
4810 /*
4811 * Environment variable: $VAR.
4812 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004813 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4814 return FAIL;
4815 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 break;
4817
4818 /*
4819 * Register contents: @r.
4820 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004821 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4822 return FAIL;
4823 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 break;
4825 /*
4826 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004827 * lambda: (arg, arg) => expr
4828 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004830 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4831 ret = compile_lambda(arg, cctx);
4832 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004833 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 break;
4835
4836 default: ret = NOTDONE;
4837 break;
4838 }
4839 if (ret == FAIL)
4840 return FAIL;
4841
Bram Moolenaar1c747212020-05-09 18:28:34 +02004842 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004844 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004845 clear_tv(rettv);
4846 else
4847 // A constant expression can possibly be handled compile time,
4848 // return the value instead of generating code.
4849 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 }
4851 else if (ret == NOTDONE)
4852 {
4853 char_u *p;
4854 int r;
4855
4856 if (!eval_isnamec1(**arg))
4857 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004858 if (!vim9_bad_comment(*arg))
4859 {
4860 if (ends_excmd(*skipwhite(*arg)))
4861 semsg(_(e_empty_expression_str), *arg);
4862 else
4863 semsg(_(e_name_expected_str), *arg);
4864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 return FAIL;
4866 }
4867
4868 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004869 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004870 if (p - *arg == (size_t)1 && **arg == '_')
4871 {
4872 emsg(_(e_cannot_use_underscore_here));
4873 return FAIL;
4874 }
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004877 {
4878 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004882 if (cctx->ctx_skip != SKIP_YES
4883 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004884 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004885 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 if (r == FAIL)
4888 return FAIL;
4889 }
4890
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004891 // Handle following "[]", ".member", etc.
4892 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004893 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 ppconst) == FAIL)
4895 return FAIL;
4896 if (ppconst->pp_used > 0)
4897 {
4898 // apply the '!', '-' and '+' before the constant
4899 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004900 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 return FAIL;
4902 return OK;
4903 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004904 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004906 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907}
4908
4909/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004910 * Give the "white on both sides" error, taking the operator from "p[len]".
4911 */
4912 void
4913error_white_both(char_u *op, int len)
4914{
4915 char_u buf[10];
4916
4917 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004918 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004919}
4920
4921/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004922 * <type>expr7: runtime type check / conversion
4923 */
4924 static int
4925compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4926{
4927 type_T *want_type = NULL;
4928
4929 // Recognize <type>
4930 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4931 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004932 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004933 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4934 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004935 return FAIL;
4936
4937 if (**arg != '>')
4938 {
4939 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004940 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004941 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004942 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004943 return FAIL;
4944 }
4945 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004946 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004947 return FAIL;
4948 }
4949
4950 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4951 return FAIL;
4952
4953 if (want_type != NULL)
4954 {
4955 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004956 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004957 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004958
Bram Moolenaard1103582020-08-14 22:44:25 +02004959 generate_ppconst(cctx, ppconst);
4960 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004961 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004962 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004963 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004964 return FAIL;
4965 }
4966 }
4967
4968 return OK;
4969}
4970
4971/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 * * number multiplication
4973 * / number division
4974 * % number modulo
4975 */
4976 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004977compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978{
4979 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004980 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004981 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004983 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004984 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 return FAIL;
4986
4987 /*
4988 * Repeat computing, until no "*", "/" or "%" is following.
4989 */
4990 for (;;)
4991 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004992 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 if (*op != '*' && *op != '/' && *op != '%')
4994 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004995 if (next != NULL)
4996 {
4997 *arg = next_line_from_context(cctx, TRUE);
4998 op = skipwhite(*arg);
4999 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005000
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005001 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005003 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005004 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005006 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005007 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005009 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005010 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005012
5013 if (ppconst->pp_used == ppconst_used + 2
5014 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5015 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005016 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005017 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5018 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5019 varnumber_T res = 0;
5020 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005022 // both are numbers: compute the result
5023 switch (*op)
5024 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005025 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005027 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005028 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005029 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005030 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005031 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005032 break;
5033 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005034 if (failed)
5035 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005036 tv1->vval.v_number = res;
5037 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005038 }
5039 else
5040 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005041 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005042 generate_two_op(cctx, op);
5043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 }
5045
5046 return OK;
5047}
5048
5049/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005050 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 * - number subtraction
5052 * .. string concatenation
5053 */
5054 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005055compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056{
5057 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005058 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
5062 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 return FAIL;
5065
5066 /*
5067 * Repeat computing, until no "+", "-" or ".." is following.
5068 */
5069 for (;;)
5070 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005071 op = may_peek_next_line(cctx, *arg, &next);
5072 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005074 if (op[0] == op[1] && *op != '.' && next)
5075 // Finding "++" or "--" on the next line is a separate command.
5076 // But ".." is concatenation.
5077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005079 if (next != NULL)
5080 {
5081 *arg = next_line_from_context(cctx, TRUE);
5082 op = skipwhite(*arg);
5083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005085 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005087 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 }
5090
Bram Moolenaare0de1712020-12-02 17:36:54 +01005091 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005094 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005095 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 return FAIL;
5097
Bram Moolenaara5565e42020-05-09 15:44:01 +02005098 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005099 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5101 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5102 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5103 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5106 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005107
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005108 // concat/subtract/add constant numbers
5109 if (*op == '+')
5110 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5111 else if (*op == '-')
5112 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5113 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005114 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005115 // concatenate constant strings
5116 char_u *s1 = tv1->vval.v_string;
5117 char_u *s2 = tv2->vval.v_string;
5118 size_t len1 = STRLEN(s1);
5119
5120 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5121 if (tv1->vval.v_string == NULL)
5122 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005123 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005124 return FAIL;
5125 }
5126 mch_memmove(tv1->vval.v_string, s1, len1);
5127 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005128 vim_free(s1);
5129 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005130 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 }
5133 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005134 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005135 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005136 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005137 if (*op == '.')
5138 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005139 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5140 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005141 return FAIL;
5142 generate_instr_drop(cctx, ISN_CONCAT, 1);
5143 }
5144 else
5145 generate_two_op(cctx, op);
5146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 }
5148
5149 return OK;
5150}
5151
5152/*
5153 * expr5a == expr5b
5154 * expr5a =~ expr5b
5155 * expr5a != expr5b
5156 * expr5a !~ expr5b
5157 * expr5a > expr5b
5158 * expr5a >= expr5b
5159 * expr5a < expr5b
5160 * expr5a <= expr5b
5161 * expr5a is expr5b
5162 * expr5a isnot expr5b
5163 *
5164 * Produces instructions:
5165 * EVAL expr5a Push result of "expr5a"
5166 * EVAL expr5b Push result of "expr5b"
5167 * COMPARE one of the compare instructions
5168 */
5169 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005170compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005172 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005174 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
5179 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 return FAIL;
5182
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005183 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005184 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185
5186 /*
5187 * If there is a comparative operator, use it.
5188 */
5189 if (type != EXPR_UNKNOWN)
5190 {
5191 int ic = FALSE; // Default: do not ignore case
5192
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005193 if (next != NULL)
5194 {
5195 *arg = next_line_from_context(cctx, TRUE);
5196 p = skipwhite(*arg);
5197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 if (type_is && (p[len] == '?' || p[len] == '#'))
5199 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005200 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 return FAIL;
5202 }
5203 // extra question mark appended: ignore case
5204 if (p[len] == '?')
5205 {
5206 ic = TRUE;
5207 ++len;
5208 }
5209 // extra '#' appended: match case (ignored)
5210 else if (p[len] == '#')
5211 ++len;
5212 // nothing appended: match case
5213
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005214 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005216 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219
5220 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005221 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005222 return FAIL;
5223
Bram Moolenaara5565e42020-05-09 15:44:01 +02005224 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 return FAIL;
5226
Bram Moolenaara5565e42020-05-09 15:44:01 +02005227 if (ppconst->pp_used == ppconst_used + 2)
5228 {
5229 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5230 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5231 int ret;
5232
5233 // Both sides are a constant, compute the result now.
5234 // First check for a valid combination of types, this is more
5235 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005236 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005237 ret = FAIL;
5238 else
5239 {
5240 ret = typval_compare(tv1, tv2, type, ic);
5241 tv1->v_type = VAR_BOOL;
5242 tv1->vval.v_number = tv1->vval.v_number
5243 ? VVAL_TRUE : VVAL_FALSE;
5244 clear_tv(tv2);
5245 --ppconst->pp_used;
5246 }
5247 return ret;
5248 }
5249
5250 generate_ppconst(cctx, ppconst);
5251 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 }
5253
5254 return OK;
5255}
5256
Bram Moolenaar7f141552020-05-09 17:35:53 +02005257static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259/*
5260 * Compile || or &&.
5261 */
5262 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005263compile_and_or(
5264 char_u **arg,
5265 cctx_T *cctx,
5266 char *op,
5267 ppconst_T *ppconst,
5268 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005270 char_u *next;
5271 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 int opchar = *op;
5273
5274 if (p[0] == opchar && p[1] == opchar)
5275 {
5276 garray_T *instr = &cctx->ctx_instr;
5277 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005278 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
5280 /*
5281 * Repeat until there is no following "||" or "&&"
5282 */
5283 ga_init2(&end_ga, sizeof(int), 10);
5284 while (p[0] == opchar && p[1] == opchar)
5285 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005286 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005287 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005288 int start_ctx_lnum = cctx->ctx_lnum;
5289 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005290 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005291 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005292 jumpwhen_T jump_when = opchar == '|'
5293 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005294
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005295 if (next != NULL)
5296 {
5297 *arg = next_line_from_context(cctx, TRUE);
5298 p = skipwhite(*arg);
5299 }
5300
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005301 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5302 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005303 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005304 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005305 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005306 return FAIL;
5307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005309 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005310 SOURCING_LNUM = start_lnum;
5311 save_lnum = cctx->ctx_lnum;
5312 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005313
5314 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005315 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005316 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005317 // Use the last ppconst if possible.
5318 if (ppconst->pp_used > 0)
5319 {
5320 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5321 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005322
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005323 if ((is_true && opchar == '|')
5324 || (!is_true && opchar == '&'))
5325 {
5326 // For "false && expr" and "true || expr" the "expr"
5327 // does not need to be evaluated.
5328 cctx->ctx_skip = SKIP_YES;
5329 clear_tv(tv);
5330 tv->v_type = VAR_BOOL;
5331 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5332 }
5333 else
5334 {
5335 // For "true && expr" and "false || expr" only "expr"
5336 // needs to be evaluated.
5337 --ppconst->pp_used;
5338 jump_when = JUMP_NEVER;
5339 }
5340 }
5341 else
5342 {
5343 // Every part must evaluate to a bool.
5344 status = bool_on_stack(cctx);
5345 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005346 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005347 if (status != FAIL)
5348 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005349 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005350 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 {
5352 ga_clear(&end_ga);
5353 return FAIL;
5354 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005355
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005356 if (jump_when != JUMP_NEVER)
5357 {
5358 if (cctx->ctx_skip != SKIP_YES)
5359 {
5360 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5361 ++end_ga.ga_len;
5362 }
5363 generate_JUMP(cctx, jump_when, 0);
5364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365
5366 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005367 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005368 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005369 {
5370 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005371 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005372 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005373
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005374 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5376 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 {
5378 ga_clear(&end_ga);
5379 return FAIL;
5380 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005381
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005382 // "0 || 1" results in true, "1 && 0" results in false.
5383 if (ppconst->pp_used == const_used + 1)
5384 {
5385 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5386
5387 if (tv->v_type == VAR_NUMBER
5388 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5389 {
5390 tv->vval.v_number = tv->vval.v_number == 1
5391 ? VVAL_TRUE : VVAL_FALSE;
5392 tv->v_type = VAR_BOOL;
5393 }
5394 }
5395
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005396 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005398
5399 if (check_ppconst_bool(ppconst) == FAIL)
5400 {
5401 ga_clear(&end_ga);
5402 return FAIL;
5403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005405 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5406 // Every part must evaluate to a bool.
5407 if (bool_on_stack(cctx) == FAIL)
5408 {
5409 ga_clear(&end_ga);
5410 return FAIL;
5411 }
5412
5413 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005414 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005415 // Fill in the end label in all jumps.
5416 generate_ppconst(cctx, ppconst);
5417 while (end_ga.ga_len > 0)
5418 {
5419 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005420
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005421 --end_ga.ga_len;
5422 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005424 isn->isn_arg.jump.jump_where = instr->ga_len;
5425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005427 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005428
5429 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 }
5431
5432 return OK;
5433}
5434
5435/*
5436 * expr4a && expr4a && expr4a logical AND
5437 *
5438 * Produces instructions:
5439 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005440 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005441 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005443 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 * EVAL expr4c Push result of "expr4c"
5445 * end:
5446 */
5447 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005448compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005450 int ppconst_used = ppconst->pp_used;
5451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 return FAIL;
5455
5456 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005457 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458}
5459
5460/*
5461 * expr3a || expr3b || expr3c logical OR
5462 *
5463 * Produces instructions:
5464 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005465 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005466 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005468 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 * EVAL expr3c Push result of "expr3c"
5470 * end:
5471 */
5472 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005473compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005475 int ppconst_used = ppconst->pp_used;
5476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005478 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479 return FAIL;
5480
5481 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483}
5484
5485/*
5486 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005488 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 * JUMP_IF_FALSE alt jump if false
5490 * EVAL expr1a
5491 * JUMP_ALWAYS end
5492 * alt: EVAL expr1b
5493 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005494 *
5495 * Toplevel expression: expr2 ?? expr1
5496 * Produces instructions:
5497 * EVAL expr2 Push result of "expr2"
5498 * JUMP_AND_KEEP_IF_TRUE end jump if true
5499 * EVAL expr1
5500 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 */
5502 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005503compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504{
5505 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005506 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005507 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508
Bram Moolenaar3988f642020-08-27 22:43:03 +02005509 // Ignore all kinds of errors when not producing code.
5510 if (cctx->ctx_skip == SKIP_YES)
5511 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005512 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005513 return OK;
5514 }
5515
Bram Moolenaar61a89812020-05-07 16:58:17 +02005516 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005517 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518 return FAIL;
5519
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005520 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521 if (*p == '?')
5522 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005523 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 garray_T *instr = &cctx->ctx_instr;
5525 garray_T *stack = &cctx->ctx_type_stack;
5526 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005527 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005529 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005530 int has_const_expr = FALSE;
5531 int const_value = FALSE;
5532 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005534 if (next != NULL)
5535 {
5536 *arg = next_line_from_context(cctx, TRUE);
5537 p = skipwhite(*arg);
5538 }
5539
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005540 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005541 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005542 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005543 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005544 return FAIL;
5545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
Bram Moolenaara5565e42020-05-09 15:44:01 +02005547 if (ppconst->pp_used == ppconst_used + 1)
5548 {
5549 // the condition is a constant, we know whether the ? or the :
5550 // expression is to be evaluated.
5551 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005552 if (op_falsy)
5553 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5554 else
5555 {
5556 int error = FALSE;
5557
5558 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5559 &error);
5560 if (error)
5561 return FAIL;
5562 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005563 cctx->ctx_skip = save_skip == SKIP_YES ||
5564 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5565
5566 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5567 // "left ?? right" and "left" is truthy: produce "left"
5568 generate_ppconst(cctx, ppconst);
5569 else
5570 {
5571 clear_tv(&ppconst->pp_tv[ppconst_used]);
5572 --ppconst->pp_used;
5573 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005574 }
5575 else
5576 {
5577 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005578 if (op_falsy)
5579 end_idx = instr->ga_len;
5580 generate_JUMP(cctx, op_falsy
5581 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5582 if (op_falsy)
5583 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
5586 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005587 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005588 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005589 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005590 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591
Bram Moolenaara5565e42020-05-09 15:44:01 +02005592 if (!has_const_expr)
5593 {
5594 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005596 if (!op_falsy)
5597 {
5598 // remember the type and drop it
5599 --stack->ga_len;
5600 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005602 end_idx = instr->ga_len;
5603 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005604
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005605 // jump here from JUMP_IF_FALSE
5606 isn = ((isn_T *)instr->ga_data) + alt_idx;
5607 isn->isn_arg.jump.jump_where = instr->ga_len;
5608 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005611 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005613 // Check for the ":".
5614 p = may_peek_next_line(cctx, *arg, &next);
5615 if (*p != ':')
5616 {
5617 emsg(_(e_missing_colon));
5618 return FAIL;
5619 }
5620 if (next != NULL)
5621 {
5622 *arg = next_line_from_context(cctx, TRUE);
5623 p = skipwhite(*arg);
5624 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005626 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5627 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005628 semsg(_(e_white_space_required_before_and_after_str_at_str),
5629 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005630 return FAIL;
5631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005633 // evaluate the third expression
5634 if (has_const_expr)
5635 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005636 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005637 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005638 return FAIL;
5639 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5640 return FAIL;
5641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
Bram Moolenaara5565e42020-05-09 15:44:01 +02005643 if (!has_const_expr)
5644 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005645 type_T **typep;
5646
Bram Moolenaara5565e42020-05-09 15:44:01 +02005647 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648
Bram Moolenaara5565e42020-05-09 15:44:01 +02005649 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005650 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5651 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005652
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005653 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005654 isn = ((isn_T *)instr->ga_data) + end_idx;
5655 isn->isn_arg.jump.jump_where = instr->ga_len;
5656 }
5657
5658 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 }
5660 return OK;
5661}
5662
5663/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005664 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005665 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5666 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005667 */
5668 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005669compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005670{
5671 ppconst_T ppconst;
5672
5673 CLEAR_FIELD(ppconst);
5674 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5675 {
5676 clear_ppconst(&ppconst);
5677 return FAIL;
5678 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005679 if (is_const != NULL)
5680 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005681 if (generate_ppconst(cctx, &ppconst) == FAIL)
5682 return FAIL;
5683 return OK;
5684}
5685
5686/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005687 * Toplevel expression.
5688 */
5689 static int
5690compile_expr0(char_u **arg, cctx_T *cctx)
5691{
5692 return compile_expr0_ext(arg, cctx, NULL);
5693}
5694
5695/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005696 * Compile "return [expr]".
5697 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 */
5699 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005700compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701{
5702 char_u *p = arg;
5703 garray_T *stack = &cctx->ctx_type_stack;
5704 type_T *stack_type;
5705
5706 if (*p != NUL && *p != '|' && *p != '\n')
5707 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005708 if (legacy)
5709 {
5710 int save_flags = cmdmod.cmod_flags;
5711
5712 generate_LEGACY_EVAL(cctx, p);
5713 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5714 0, cctx, FALSE, FALSE) == FAIL)
5715 return NULL;
5716 cmdmod.cmod_flags |= CMOD_LEGACY;
5717 (void)skip_expr(&p, NULL);
5718 cmdmod.cmod_flags = save_flags;
5719 }
5720 else
5721 {
5722 // compile return argument into instructions
5723 if (compile_expr0(&p, cctx) == FAIL)
5724 return NULL;
5725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005727 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005728 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005729 // "check_return_type" with uf_ret_type set to &t_unknown is used
5730 // for an inline function without a specified return type. Set the
5731 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005732 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005733 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005734 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5735 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005736 || (!check_return_type
5737 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005738 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005739 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005740 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005741 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005742 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005743 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5744 && stack_type->tt_type != VAR_VOID
5745 && stack_type->tt_type != VAR_UNKNOWN)
5746 {
5747 emsg(_(e_returning_value_in_function_without_return_type));
5748 return NULL;
5749 }
5750 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005751 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005752 return NULL;
5753 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 }
5756 else
5757 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005758 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005759 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005760 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5761 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005763 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 return NULL;
5765 }
5766
5767 // No argument, return zero.
5768 generate_PUSHNR(cctx, 0);
5769 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005770
5771 // Undo any command modifiers.
5772 generate_undo_cmdmods(cctx);
5773
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005774 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 return NULL;
5776
5777 // "return val | endif" is possible
5778 return skipwhite(p);
5779}
5780
5781/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005782 * Get a line from the compilation context, compatible with exarg_T getline().
5783 * Return a pointer to the line in allocated memory.
5784 * Return NULL for end-of-file or some error.
5785 */
5786 static char_u *
5787exarg_getline(
5788 int c UNUSED,
5789 void *cookie,
5790 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005791 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005792{
5793 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005794 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005795
Bram Moolenaar66250c92020-08-20 15:02:42 +02005796 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005797 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005798 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005799 return NULL;
5800 ++cctx->ctx_lnum;
5801 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5802 // Comment lines result in NULL pointers, skip them.
5803 if (p != NULL)
5804 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005805 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005806}
5807
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005808 void
5809fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5810{
5811 eap->getline = exarg_getline;
5812 eap->cookie = cctx;
5813}
5814
Bram Moolenaar04b12692020-05-04 23:24:44 +02005815/*
5816 * Compile a nested :def command.
5817 */
5818 static char_u *
5819compile_nested_function(exarg_T *eap, cctx_T *cctx)
5820{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005821 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005822 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005823 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005824 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005825 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005826 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005827 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005828
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005829 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005830 {
5831 emsg(_(e_cannot_use_bang_with_nested_def));
5832 return NULL;
5833 }
5834
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005835 if (*name_start == '/')
5836 {
5837 name_end = skip_regexp(name_start + 1, '/', TRUE);
5838 if (*name_end == '/')
5839 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005840 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005841 }
5842 if (name_end == name_start || *skipwhite(name_end) != '(')
5843 {
5844 if (!ends_excmd2(name_start, name_end))
5845 {
5846 semsg(_(e_invalid_command_str), eap->cmd);
5847 return NULL;
5848 }
5849
5850 // "def" or "def Name": list functions
5851 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5852 return NULL;
5853 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5854 }
5855
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005856 // Only g:Func() can use a namespace.
5857 if (name_start[1] == ':' && !is_global)
5858 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005859 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005860 return NULL;
5861 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005862 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005863 return NULL;
5864
Bram Moolenaar04b12692020-05-04 23:24:44 +02005865 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005866 fill_exarg_from_cctx(eap, cctx);
5867
Bram Moolenaar04b12692020-05-04 23:24:44 +02005868 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005869 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005870 lambda_name = vim_strsave(get_lambda_name());
5871 if (lambda_name == NULL)
5872 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005873 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005874
Bram Moolenaar822ba242020-05-24 23:00:18 +02005875 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005876 {
5877 r = eap->skip ? OK : FAIL;
5878 goto theend;
5879 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005880
5881 // copy over the block scope IDs before compiling
5882 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5883 {
5884 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5885
5886 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5887 if (ufunc->uf_block_ids != NULL)
5888 {
5889 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5890 sizeof(int) * block_depth);
5891 ufunc->uf_block_depth = block_depth;
5892 }
5893 }
5894
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005895 compile_type = COMPILE_TYPE(ufunc);
5896#ifdef FEAT_PROFILE
5897 // If the outer function is profiled, also compile the nested function for
5898 // profiling.
5899 if (cctx->ctx_compile_type == CT_PROFILE)
5900 compile_type = CT_PROFILE;
5901#endif
5902 if (func_needs_compiling(ufunc, compile_type)
5903 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005904 {
5905 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005906 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005907 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005908
Bram Moolenaar648594e2021-07-11 17:55:01 +02005909#ifdef FEAT_PROFILE
5910 // When the outer function is compiled for profiling, the nested function
5911 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005912 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005913 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5914#endif
5915
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005916 if (is_global)
5917 {
5918 char_u *func_name = vim_strnsave(name_start + 2,
5919 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005920
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005921 if (func_name == NULL)
5922 r = FAIL;
5923 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005924 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005925 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005926 lambda_name = NULL;
5927 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005928 }
5929 else
5930 {
5931 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005932 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005933 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005934
Bram Moolenaareef21022020-08-01 22:16:43 +02005935 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005936 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005937 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005938 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005939 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5940 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005941
5942theend:
5943 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005944 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005945}
5946
5947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 * Return the length of an assignment operator, or zero if there isn't one.
5949 */
5950 int
5951assignment_len(char_u *p, int *heredoc)
5952{
5953 if (*p == '=')
5954 {
5955 if (p[1] == '<' && p[2] == '<')
5956 {
5957 *heredoc = TRUE;
5958 return 3;
5959 }
5960 return 1;
5961 }
5962 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5963 return 2;
5964 if (STRNCMP(p, "..=", 3) == 0)
5965 return 3;
5966 return 0;
5967}
5968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005970 * Generate the load instruction for "name".
5971 */
5972 static void
5973generate_loadvar(
5974 cctx_T *cctx,
5975 assign_dest_T dest,
5976 char_u *name,
5977 lvar_T *lvar,
5978 type_T *type)
5979{
5980 switch (dest)
5981 {
5982 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005983 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5984 break;
5985 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005986 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5987 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5988 else
5989 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005990 break;
5991 case dest_buffer:
5992 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5993 break;
5994 case dest_window:
5995 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5996 break;
5997 case dest_tab:
5998 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5999 break;
6000 case dest_script:
6001 compile_load_scriptvar(cctx,
6002 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6003 break;
6004 case dest_env:
6005 // Include $ in the name here
6006 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6007 break;
6008 case dest_reg:
6009 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6010 break;
6011 case dest_vimvar:
6012 generate_LOADV(cctx, name + 2, TRUE);
6013 break;
6014 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006015 if (lvar->lv_from_outer > 0)
6016 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6017 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006018 else
6019 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6020 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006021 case dest_expr:
6022 // list or dict value should already be on the stack.
6023 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006024 }
6025}
6026
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006027/*
6028 * Skip over "[expr]" or ".member".
6029 * Does not check for any errors.
6030 */
6031 static char_u *
6032skip_index(char_u *start)
6033{
6034 char_u *p = start;
6035
6036 if (*p == '[')
6037 {
6038 p = skipwhite(p + 1);
6039 (void)skip_expr(&p, NULL);
6040 p = skipwhite(p);
6041 if (*p == ']')
6042 return p + 1;
6043 return p;
6044 }
6045 // if (*p == '.')
6046 return to_name_end(p + 1, TRUE);
6047}
6048
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006049 void
6050vim9_declare_error(char_u *name)
6051{
6052 char *scope = "";
6053
6054 switch (*name)
6055 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006056 case 'g': scope = _("global"); break;
6057 case 'b': scope = _("buffer"); break;
6058 case 'w': scope = _("window"); break;
6059 case 't': scope = _("tab"); break;
6060 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006061 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006062 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006063 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006064 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006065 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006066 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006067 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006068 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006069 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006070}
6071
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006072/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006073 * For one assignment figure out the type of destination. Return it in "dest".
6074 * When not recognized "dest" is not set.
6075 * For an option "opt_flags" is set.
6076 * For a v:var "vimvaridx" is set.
6077 * "type" is set to the destination type if known, unchanted otherwise.
6078 * Return FAIL if an error message was given.
6079 */
6080 static int
6081get_var_dest(
6082 char_u *name,
6083 assign_dest_T *dest,
6084 int cmdidx,
6085 int *opt_flags,
6086 int *vimvaridx,
6087 type_T **type,
6088 cctx_T *cctx)
6089{
6090 char_u *p;
6091
6092 if (*name == '&')
6093 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006094 int cc;
6095 long numval;
6096 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006097
6098 *dest = dest_option;
6099 if (cmdidx == CMD_final || cmdidx == CMD_const)
6100 {
6101 emsg(_(e_const_option));
6102 return FAIL;
6103 }
6104 p = name;
6105 p = find_option_end(&p, opt_flags);
6106 if (p == NULL)
6107 {
6108 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006109 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006110 return FAIL;
6111 }
6112 cc = *p;
6113 *p = NUL;
6114 opt_type = get_option_value(skip_option_env_lead(name),
6115 &numval, NULL, *opt_flags);
6116 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006117 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006118 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006119 case gov_unknown:
6120 semsg(_(e_unknown_option), name);
6121 return FAIL;
6122 case gov_string:
6123 case gov_hidden_string:
6124 *type = &t_string;
6125 break;
6126 case gov_bool:
6127 case gov_hidden_bool:
6128 *type = &t_bool;
6129 break;
6130 case gov_number:
6131 case gov_hidden_number:
6132 *type = &t_number;
6133 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006134 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006135 }
6136 else if (*name == '$')
6137 {
6138 *dest = dest_env;
6139 *type = &t_string;
6140 }
6141 else if (*name == '@')
6142 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006143 if (name[1] != '@'
6144 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006145 {
6146 emsg_invreg(name[1]);
6147 return FAIL;
6148 }
6149 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006150 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006151 }
6152 else if (STRNCMP(name, "g:", 2) == 0)
6153 {
6154 *dest = dest_global;
6155 }
6156 else if (STRNCMP(name, "b:", 2) == 0)
6157 {
6158 *dest = dest_buffer;
6159 }
6160 else if (STRNCMP(name, "w:", 2) == 0)
6161 {
6162 *dest = dest_window;
6163 }
6164 else if (STRNCMP(name, "t:", 2) == 0)
6165 {
6166 *dest = dest_tab;
6167 }
6168 else if (STRNCMP(name, "v:", 2) == 0)
6169 {
6170 typval_T *vtv;
6171 int di_flags;
6172
6173 *vimvaridx = find_vim_var(name + 2, &di_flags);
6174 if (*vimvaridx < 0)
6175 {
6176 semsg(_(e_variable_not_found_str), name);
6177 return FAIL;
6178 }
6179 // We use the current value of "sandbox" here, is that OK?
6180 if (var_check_ro(di_flags, name, FALSE))
6181 return FAIL;
6182 *dest = dest_vimvar;
6183 vtv = get_vim_var_tv(*vimvaridx);
6184 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6185 }
6186 return OK;
6187}
6188
6189/*
6190 * Generate a STORE instruction for "dest", not being "dest_local".
6191 * Return FAIL when out of memory.
6192 */
6193 static int
6194generate_store_var(
6195 cctx_T *cctx,
6196 assign_dest_T dest,
6197 int opt_flags,
6198 int vimvaridx,
6199 int scriptvar_idx,
6200 int scriptvar_sid,
6201 type_T *type,
6202 char_u *name)
6203{
6204 switch (dest)
6205 {
6206 case dest_option:
6207 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6208 opt_flags);
6209 case dest_global:
6210 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006211 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6212 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006213 case dest_buffer:
6214 // include b: with the name, easier to execute that way
6215 return generate_STORE(cctx, ISN_STOREB, 0, name);
6216 case dest_window:
6217 // include w: with the name, easier to execute that way
6218 return generate_STORE(cctx, ISN_STOREW, 0, name);
6219 case dest_tab:
6220 // include t: with the name, easier to execute that way
6221 return generate_STORE(cctx, ISN_STORET, 0, name);
6222 case dest_env:
6223 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6224 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006225 return generate_STORE(cctx, ISN_STOREREG,
6226 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006227 case dest_vimvar:
6228 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6229 case dest_script:
6230 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006231 // "s:" may be included in the name.
6232 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006233 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006234 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6235 scriptvar_sid, scriptvar_idx, type);
6236 case dest_local:
6237 case dest_expr:
6238 // cannot happen
6239 break;
6240 }
6241 return FAIL;
6242}
6243
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006245generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6246{
6247 if (lhs->lhs_dest != dest_local)
6248 return generate_store_var(cctx, lhs->lhs_dest,
6249 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6250 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6251 lhs->lhs_type, lhs->lhs_name);
6252
6253 if (lhs->lhs_lvar != NULL)
6254 {
6255 garray_T *instr = &cctx->ctx_instr;
6256 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6257
6258 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6259 // ISN_STORENR
6260 if (lhs->lhs_lvar->lv_from_outer == 0
6261 && instr->ga_len == instr_count + 1
6262 && isn->isn_type == ISN_PUSHNR)
6263 {
6264 varnumber_T val = isn->isn_arg.number;
6265 garray_T *stack = &cctx->ctx_type_stack;
6266
6267 isn->isn_type = ISN_STORENR;
6268 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6269 isn->isn_arg.storenr.stnr_val = val;
6270 if (stack->ga_len > 0)
6271 --stack->ga_len;
6272 }
6273 else if (lhs->lhs_lvar->lv_from_outer > 0)
6274 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6275 lhs->lhs_lvar->lv_from_outer);
6276 else
6277 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6278 }
6279 return OK;
6280}
6281
6282 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283is_decl_command(int cmdidx)
6284{
6285 return cmdidx == CMD_let || cmdidx == CMD_var
6286 || cmdidx == CMD_final || cmdidx == CMD_const;
6287}
6288
6289/*
6290 * Figure out the LHS type and other properties for an assignment or one item
6291 * of ":unlet" with an index.
6292 * Returns OK or FAIL.
6293 */
6294 static int
6295compile_lhs(
6296 char_u *var_start,
6297 lhs_T *lhs,
6298 int cmdidx,
6299 int heredoc,
6300 int oplen,
6301 cctx_T *cctx)
6302{
6303 char_u *var_end;
6304 int is_decl = is_decl_command(cmdidx);
6305
6306 CLEAR_POINTER(lhs);
6307 lhs->lhs_dest = dest_local;
6308 lhs->lhs_vimvaridx = -1;
6309 lhs->lhs_scriptvar_idx = -1;
6310
6311 // "dest_end" is the end of the destination, including "[expr]" or
6312 // ".name".
6313 // "var_end" is the end of the variable/option/etc. name.
6314 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6315 if (*var_start == '@')
6316 var_end = var_start + 2;
6317 else
6318 {
6319 // skip over the leading "&", "&l:", "&g:" and "$"
6320 var_end = skip_option_env_lead(var_start);
6321 var_end = to_name_end(var_end, TRUE);
6322 }
6323
6324 // "a: type" is declaring variable "a" with a type, not dict "a:".
6325 if (is_decl && lhs->lhs_dest_end == var_start + 2
6326 && lhs->lhs_dest_end[-1] == ':')
6327 --lhs->lhs_dest_end;
6328 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6329 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006330 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331
6332 // compute the length of the destination without "[expr]" or ".name"
6333 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006334 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6336 if (lhs->lhs_name == NULL)
6337 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006338
6339 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6340 // Something follows after the variable: "var[idx]" or "var.key".
6341 lhs->lhs_has_index = TRUE;
6342
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343 if (heredoc)
6344 lhs->lhs_type = &t_list_string;
6345 else
6346 lhs->lhs_type = &t_any;
6347
6348 if (cctx->ctx_skip != SKIP_YES)
6349 {
6350 int declare_error = FALSE;
6351
6352 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6353 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6354 &lhs->lhs_type, cctx) == FAIL)
6355 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006356 if (lhs->lhs_dest != dest_local
6357 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006358 {
6359 // Specific kind of variable recognized.
6360 declare_error = is_decl;
6361 }
6362 else
6363 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006365 if (check_reserved_name(lhs->lhs_name) == FAIL)
6366 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006367
6368 if (lookup_local(var_start, lhs->lhs_varlen,
6369 &lhs->lhs_local_lvar, cctx) == OK)
6370 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6371 else
6372 {
6373 CLEAR_FIELD(lhs->lhs_arg_lvar);
6374 if (arg_exists(var_start, lhs->lhs_varlen,
6375 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6376 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6377 {
6378 if (is_decl)
6379 {
6380 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6381 return FAIL;
6382 }
6383 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6384 }
6385 }
6386 if (lhs->lhs_lvar != NULL)
6387 {
6388 if (is_decl)
6389 {
6390 semsg(_(e_variable_already_declared), lhs->lhs_name);
6391 return FAIL;
6392 }
6393 }
6394 else
6395 {
6396 int script_namespace = lhs->lhs_varlen > 1
6397 && STRNCMP(var_start, "s:", 2) == 0;
6398 int script_var = (script_namespace
6399 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006400 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006402 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403 imported_T *import =
6404 find_imported(var_start, lhs->lhs_varlen, cctx);
6405
6406 if (script_namespace || script_var || import != NULL)
6407 {
6408 char_u *rawname = lhs->lhs_name
6409 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6410
6411 if (is_decl)
6412 {
6413 if (script_namespace)
6414 semsg(_(e_cannot_declare_script_variable_in_function),
6415 lhs->lhs_name);
6416 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006417 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 lhs->lhs_name);
6419 return FAIL;
6420 }
6421 else if (cctx->ctx_ufunc->uf_script_ctx_version
6422 == SCRIPT_VERSION_VIM9
6423 && script_namespace
6424 && !script_var && import == NULL)
6425 {
6426 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6427 return FAIL;
6428 }
6429
6430 lhs->lhs_dest = dest_script;
6431
6432 // existing script-local variables should have a type
6433 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6434 if (import != NULL)
6435 lhs->lhs_scriptvar_sid = import->imp_sid;
6436 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6437 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006438 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006439 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006440 lhs->lhs_scriptvar_sid, rawname,
6441 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6442 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 if (lhs->lhs_scriptvar_idx >= 0)
6444 {
6445 scriptitem_T *si = SCRIPT_ITEM(
6446 lhs->lhs_scriptvar_sid);
6447 svar_T *sv =
6448 ((svar_T *)si->sn_var_vals.ga_data)
6449 + lhs->lhs_scriptvar_idx;
6450 lhs->lhs_type = sv->sv_type;
6451 }
6452 }
6453 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006454 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 == FAIL)
6456 return FAIL;
6457 }
6458 }
6459
6460 if (declare_error)
6461 {
6462 vim9_declare_error(lhs->lhs_name);
6463 return FAIL;
6464 }
6465 }
6466
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006467 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006468 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6469 var_end = lhs->lhs_dest_end;
6470
6471 if (lhs->lhs_dest != dest_option)
6472 {
6473 if (is_decl && *var_end == ':')
6474 {
6475 char_u *p;
6476
6477 // parse optional type: "let var: type = expr"
6478 if (!VIM_ISWHITE(var_end[1]))
6479 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006480 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 return FAIL;
6482 }
6483 p = skipwhite(var_end + 1);
6484 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6485 if (lhs->lhs_type == NULL)
6486 return FAIL;
6487 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006488 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 }
6490 else if (lhs->lhs_lvar != NULL)
6491 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6492 }
6493
Bram Moolenaare42939a2021-04-05 17:11:17 +02006494 if (oplen == 3 && !heredoc
6495 && lhs->lhs_dest != dest_global
6496 && !lhs->lhs_has_index
6497 && lhs->lhs_type->tt_type != VAR_STRING
6498 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 {
6500 emsg(_(e_can_only_concatenate_to_string));
6501 return FAIL;
6502 }
6503
6504 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6505 && cctx->ctx_skip != SKIP_YES)
6506 {
6507 if (oplen > 1 && !heredoc)
6508 {
6509 // +=, /=, etc. require an existing variable
6510 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6511 return FAIL;
6512 }
6513 if (!is_decl)
6514 {
6515 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6516 return FAIL;
6517 }
6518
Bram Moolenaar3f327882021-03-17 20:56:38 +01006519 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006520 if ((lhs->lhs_type->tt_type == VAR_FUNC
6521 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006522 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006523 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006524
6525 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6527 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6528 if (lhs->lhs_lvar == NULL)
6529 return FAIL;
6530 lhs->lhs_new_local = TRUE;
6531 }
6532
6533 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006534 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006536 char_u *after = var_start + lhs->lhs_varlen;
6537 char_u *p;
6538
Bram Moolenaar752fc692021-01-04 21:57:11 +01006539 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 if (is_decl)
6541 {
6542 emsg(_(e_cannot_use_index_when_declaring_variable));
6543 return FAIL;
6544 }
6545
Bram Moolenaarc750d912021-11-29 22:02:12 +00006546 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6547 // Only the last index is used below, if there are others
6548 // before it generate code for the expression. Thus for
6549 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6550 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006551 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006552 p = skip_index(after);
6553 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006555 lhs->lhs_varlen_total = p - var_start;
6556 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006557 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006558 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006560 if (after > var_start + lhs->lhs_varlen)
6561 {
6562 lhs->lhs_varlen = after - var_start;
6563 lhs->lhs_dest = dest_expr;
6564 // We don't know the type before evaluating the expression,
6565 // use "any" until then.
6566 lhs->lhs_type = &t_any;
6567 }
6568
6569 if (lhs->lhs_type->tt_member == NULL)
6570 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006571 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006572 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 }
6574 return OK;
6575}
6576
6577/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006578 * Figure out the LHS and check a few errors.
6579 */
6580 static int
6581compile_assign_lhs(
6582 char_u *var_start,
6583 lhs_T *lhs,
6584 int cmdidx,
6585 int is_decl,
6586 int heredoc,
6587 int oplen,
6588 cctx_T *cctx)
6589{
6590 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6591 return FAIL;
6592
6593 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6594 {
6595 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6596 return FAIL;
6597 }
6598 if (!is_decl && lhs->lhs_lvar != NULL
6599 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6600 {
6601 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6602 return FAIL;
6603 }
6604 return OK;
6605}
6606
6607/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006608 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6609 */
6610 static int
6611has_list_index(char_u *idx_start, cctx_T *cctx)
6612{
6613 char_u *p = idx_start;
6614 int save_skip;
6615
6616 if (*p != '[')
6617 return FALSE;
6618
6619 p = skipwhite(p + 1);
6620 if (*p == ':')
6621 return TRUE;
6622
6623 save_skip = cctx->ctx_skip;
6624 cctx->ctx_skip = SKIP_YES;
6625 (void)compile_expr0(&p, cctx);
6626 cctx->ctx_skip = save_skip;
6627 return *skipwhite(p) == ':';
6628}
6629
6630/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006631 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6632 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 */
6634 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006635compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 char_u *var_start,
6637 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006638 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006639 cctx_T *cctx)
6640{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006641 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006642 char_u *p;
6643 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006644 int need_white_before = TRUE;
6645 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646
Bram Moolenaar752fc692021-01-04 21:57:11 +01006647 p = var_start + varlen;
6648 if (*p == '[')
6649 {
6650 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006651 if (*p == ':')
6652 {
6653 // empty first index, push zero
6654 r = generate_PUSHNR(cctx, 0);
6655 need_white_before = FALSE;
6656 }
6657 else
6658 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006659
6660 if (r == OK && *skipwhite(p) == ':')
6661 {
6662 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006663 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006664 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006665 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006666 empty_second = *skipwhite(p + 1) == ']';
6667 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6668 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006669 {
6670 semsg(_(e_white_space_required_before_and_after_str_at_str),
6671 ":", p);
6672 return FAIL;
6673 }
6674 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006675 if (*p == ']')
6676 // empty second index, push "none"
6677 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6678 else
6679 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006680 }
6681
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 if (r == OK && *skipwhite(p) != ']')
6683 {
6684 // this should not happen
6685 emsg(_(e_missbrac));
6686 r = FAIL;
6687 }
6688 }
6689 else // if (*p == '.')
6690 {
6691 char_u *key_end = to_name_end(p + 1, TRUE);
6692 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6693
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006694 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006696 return r;
6697}
6698
6699/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006700 * For a LHS with an index, load the variable to be indexed.
6701 */
6702 static int
6703compile_load_lhs(
6704 lhs_T *lhs,
6705 char_u *var_start,
6706 type_T *rhs_type,
6707 cctx_T *cctx)
6708{
6709 if (lhs->lhs_dest == dest_expr)
6710 {
6711 size_t varlen = lhs->lhs_varlen;
6712 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006713 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006714 char_u *p = var_start;
6715 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006716 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006717
Bram Moolenaare97976b2021-08-01 13:17:17 +02006718 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6719 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006720 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006721 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6722 res = compile_expr0(&p, cctx);
6723 var_start[varlen] = c;
6724 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6725 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006726 {
6727 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006728 if (res != FAIL)
6729 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006730 return FAIL;
6731 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006732
6733 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6734 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6735 // now we can properly check the type
6736 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6737 && rhs_type != &t_void
6738 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6739 FALSE, FALSE) == FAIL)
6740 return FAIL;
6741 }
6742 else
6743 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6744 lhs->lhs_lvar, lhs->lhs_type);
6745 return OK;
6746}
6747
6748/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006749 * Produce code for loading "lhs" and also take care of an index.
6750 * Return OK/FAIL.
6751 */
6752 static int
6753compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6754{
6755 compile_load_lhs(lhs, var_start, NULL, cctx);
6756
6757 if (lhs->lhs_has_index)
6758 {
6759 int range = FALSE;
6760
6761 // Get member from list or dict. First compile the
6762 // index value.
6763 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6764 return FAIL;
6765 if (range)
6766 {
6767 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6768 var_start);
6769 return FAIL;
6770 }
6771
6772 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006773 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006774 return FAIL;
6775 }
6776 return OK;
6777}
6778
6779/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006780 * Assignment to a list or dict member, or ":unlet" for the item, using the
6781 * information in "lhs".
6782 * Returns OK or FAIL.
6783 */
6784 static int
6785compile_assign_unlet(
6786 char_u *var_start,
6787 lhs_T *lhs,
6788 int is_assign,
6789 type_T *rhs_type,
6790 cctx_T *cctx)
6791{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006792 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006793 garray_T *stack = &cctx->ctx_type_stack;
6794 int range = FALSE;
6795
Bram Moolenaar68452172021-04-12 21:21:02 +02006796 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006797 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006798 if (is_assign && range
6799 && lhs->lhs_type->tt_type != VAR_LIST
6800 && lhs->lhs_type != &t_blob
6801 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006802 {
6803 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6804 return FAIL;
6805 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006806
6807 if (lhs->lhs_type == &t_any)
6808 {
6809 // Index on variable of unknown type: check at runtime.
6810 dest_type = VAR_ANY;
6811 }
6812 else
6813 {
6814 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006815 if (dest_type == VAR_DICT && range)
6816 {
6817 emsg(e_cannot_use_range_with_dictionary);
6818 return FAIL;
6819 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006820 if (dest_type == VAR_DICT
6821 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006822 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006823 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006824 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006825 type_T *type;
6826
6827 if (range)
6828 {
6829 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6830 if (need_type(type, &t_number,
6831 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006832 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006833 }
6834 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006835 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006836 && need_type(type, &t_number,
6837 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006838 return FAIL;
6839 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006840 }
6841
6842 // Load the dict or list. On the stack we then have:
6843 // - value (for assignment, not for :unlet)
6844 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006845 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006847 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6848 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006849
Bram Moolenaar68452172021-04-12 21:21:02 +02006850 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6851 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006852 {
6853 if (is_assign)
6854 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006855 if (range)
6856 {
6857 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6858 return FAIL;
6859 }
6860 else
6861 {
6862 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006863
Bram Moolenaar68452172021-04-12 21:21:02 +02006864 if (isn == NULL)
6865 return FAIL;
6866 isn->isn_arg.vartype = dest_type;
6867 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006868 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006869 else if (range)
6870 {
6871 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6872 return FAIL;
6873 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006874 else
6875 {
6876 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6877 return FAIL;
6878 }
6879 }
6880 else
6881 {
6882 emsg(_(e_indexable_type_required));
6883 return FAIL;
6884 }
6885
6886 return OK;
6887}
6888
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006889/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006890 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006891 * "let name"
6892 * "var name = expr"
6893 * "final name = expr"
6894 * "const name = expr"
6895 * "name = expr"
6896 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006897 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006898 * Return NULL for an error.
6899 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 */
6901 static char_u *
6902compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6903{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006904 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006906 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 char_u *ret = NULL;
6908 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006909 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006911 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006913 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 int oplen = 0;
6916 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006917 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006918 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006920 int is_decl = is_decl_command(cmdidx);
6921 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006922 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006924 // Skip over the "var" or "[var, var]" to get to any "=".
6925 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6926 if (p == NULL)
6927 return *arg == '[' ? arg : NULL;
6928
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 sp = p;
6932 p = skipwhite(p);
6933 op = p;
6934 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935
6936 if (var_count > 0 && oplen == 0)
6937 // can be something like "[1, 2]->func()"
6938 return arg;
6939
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006940 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006942 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006943 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006944 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006945 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6946 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006947 if (VIM_ISWHITE(eap->cmd[2]))
6948 {
6949 semsg(_(e_no_white_space_allowed_after_str_str),
6950 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6951 return NULL;
6952 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006953 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6954 oplen = 2;
6955 incdec = TRUE;
6956 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 if (heredoc)
6959 {
6960 list_T *l;
6961 listitem_T *li;
6962
6963 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006964 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006966 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006967 if (l == NULL)
6968 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969
Bram Moolenaar078269b2020-09-21 20:35:55 +02006970 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006972 // Push each line and the create the list.
6973 FOR_ALL_LIST_ITEMS(l, li)
6974 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006975 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006976 li->li_tv.vval.v_string = NULL;
6977 }
6978 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 list_free(l);
6981 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006982 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006986 char_u *wp;
6987
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006988 // for "[var, var] = expr" evaluate the expression here, loop over the
6989 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006990 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006991
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006992 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006993 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006994 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995 if (compile_expr0(&p, cctx) == FAIL)
6996 return NULL;
6997 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006999 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007001 type_T *stacktype;
7002
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007003 stacktype = stack->ga_len == 0 ? &t_void
7004 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007005 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007007 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007008 goto theend;
7009 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007010 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007011 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007012 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02007013 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02007014 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
7015 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007016 if (stacktype->tt_member != NULL)
7017 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007018 }
7019 }
7020
7021 /*
7022 * Loop over variables in "[var, var] = expr".
7023 * For "var = expr" and "let var: type" this is done only once.
7024 */
7025 if (var_count > 0)
7026 var_start = skipwhite(arg + 1); // skip over the "["
7027 else
7028 var_start = arg;
7029 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7030 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007031 int instr_count = -1;
7032 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007033
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007034 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7035 {
7036 // Ignore underscore in "[a, _, b] = list".
7037 if (var_count > 0)
7038 {
7039 var_start = skipwhite(var_start + 2);
7040 continue;
7041 }
7042 emsg(_(e_cannot_use_underscore_here));
7043 goto theend;
7044 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007045 vim_free(lhs.lhs_name);
7046
7047 /*
7048 * Figure out the LHS type and other properties.
7049 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007050 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7051 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007052 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007053 if (heredoc)
7054 {
7055 SOURCING_LNUM = start_lnum;
7056 if (lhs.lhs_has_type
7057 && need_type(&t_list_string, lhs.lhs_type,
7058 -1, 0, cctx, FALSE, FALSE) == FAIL)
7059 goto theend;
7060 }
7061 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007062 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007063 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007064 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007065 if (oplen > 0 && var_count == 0)
7066 {
7067 // skip over the "=" and the expression
7068 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007069 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007070 }
7071 }
7072 else if (oplen > 0)
7073 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007074 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007075 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007076
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007077 // for "+=", "*=", "..=" etc. first load the current value
7078 if (*op != '='
7079 && compile_load_lhs_with_index(&lhs, var_start,
7080 cctx) == FAIL)
7081 goto theend;
7082
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007083 // For "var = expr" evaluate the expression.
7084 if (var_count == 0)
7085 {
7086 int r;
7087
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007088 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007089 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007090 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007091 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007092 r = generate_PUSHNR(cctx, 1);
7093 }
7094 else
7095 {
7096 // Temporarily hide the new local variable here, it is
7097 // not available to this expression.
7098 if (lhs.lhs_new_local)
7099 --cctx->ctx_locals.ga_len;
7100 wp = op + oplen;
7101 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7102 {
7103 if (lhs.lhs_new_local)
7104 ++cctx->ctx_locals.ga_len;
7105 goto theend;
7106 }
7107 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007108 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007109 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007110 if (r == FAIL)
7111 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007112 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007113 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007114 else if (semicolon && var_idx == var_count - 1)
7115 {
7116 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007117 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007118 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7119 goto theend;
7120 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007121 else
7122 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007123 // For "[var, var] = expr" get the "var_idx" item from the
7124 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007125 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007126 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007127 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007128
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007129 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007130 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007131 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007132 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007133 if ((rhs_type->tt_type == VAR_FUNC
7134 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007135 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007136 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007137 goto theend;
7138
Bram Moolenaar752fc692021-01-04 21:57:11 +01007139 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007140 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007141 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007142 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007143 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007144 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007145 }
7146 else
7147 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007148 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007149 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007150 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007151 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007152 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007153 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007154 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007155 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007156 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007157 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007158 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007159 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007160 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007161 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007162 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007163 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007164
Bram Moolenaar77709b12021-04-03 21:01:01 +02007165 // Without operator check type here, otherwise below.
7166 // Use the line number of the assignment.
7167 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007168 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7169 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007170 // If assigning to a list or dict member, use the
7171 // member type. Not for "list[:] =".
7172 if (lhs.lhs_has_index
7173 && !has_list_index(var_start + lhs.lhs_varlen,
7174 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007175 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007176 if (need_type_where(rhs_type, use_type, -1, where,
7177 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007178 goto theend;
7179 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007181 else
7182 {
7183 type_T *lhs_type = lhs.lhs_member_type;
7184
7185 // Special case: assigning to @# can use a number or a
7186 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007187 // Also: can assign a number to a float.
7188 if ((lhs_type == &t_number_or_string
7189 || lhs_type == &t_float)
7190 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007191 lhs_type = &t_number;
7192 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007193 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007194 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007197 else if (cmdidx == CMD_final)
7198 {
7199 emsg(_(e_final_requires_a_value));
7200 goto theend;
7201 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007202 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007204 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007205 goto theend;
7206 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007207 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007208 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007209 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007210 goto theend;
7211 }
7212 else
7213 {
7214 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007215 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007216 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007217 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007218 {
7219 case VAR_BOOL:
7220 generate_PUSHBOOL(cctx, VVAL_FALSE);
7221 break;
7222 case VAR_FLOAT:
7223#ifdef FEAT_FLOAT
7224 generate_PUSHF(cctx, 0.0);
7225#endif
7226 break;
7227 case VAR_STRING:
7228 generate_PUSHS(cctx, NULL);
7229 break;
7230 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007231 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007232 break;
7233 case VAR_FUNC:
7234 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7235 break;
7236 case VAR_LIST:
7237 generate_NEWLIST(cctx, 0);
7238 break;
7239 case VAR_DICT:
7240 generate_NEWDICT(cctx, 0);
7241 break;
7242 case VAR_JOB:
7243 generate_PUSHJOB(cctx, NULL);
7244 break;
7245 case VAR_CHANNEL:
7246 generate_PUSHCHANNEL(cctx, NULL);
7247 break;
7248 case VAR_NUMBER:
7249 case VAR_UNKNOWN:
7250 case VAR_ANY:
7251 case VAR_PARTIAL:
7252 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007253 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007254 case VAR_SPECIAL: // cannot happen
7255 generate_PUSHNR(cctx, 0);
7256 break;
7257 }
7258 }
7259 if (var_count == 0)
7260 end = p;
7261 }
7262
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007263 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007264 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007265 break;
7266
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007267 if (oplen > 0 && *op != '=')
7268 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007269 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007270 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007271
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007272 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007273 {
7274 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7275 goto theend;
7276 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007277 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007278 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007279 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007280 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7281 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007282#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007283 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007284 !(expected == &t_float && (stacktype == &t_number
7285 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007286#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007287 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007288 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007289 goto theend;
7290 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007291
7292 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007293 {
7294 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7295 goto theend;
7296 }
7297 else if (*op == '+')
7298 {
7299 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007300 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007301 lhs.lhs_member_type, stacktype,
7302 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007303 goto theend;
7304 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007305 else if (generate_two_op(cctx, op) == FAIL)
7306 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007309 // Use the line number of the assignment for store instruction.
7310 save_lnum = cctx->ctx_lnum;
7311 cctx->ctx_lnum = start_lnum - 1;
7312
Bram Moolenaar752fc692021-01-04 21:57:11 +01007313 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007314 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007315 // Use the info in "lhs" to store the value at the index in the
7316 // list or dict.
7317 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7318 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007319 {
7320 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007321 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007322 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007323 }
7324 else
7325 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007326 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007327 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007328 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007329 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007330 generate_LOCKCONST(cctx);
7331
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007332 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007333 && (lhs.lhs_type->tt_type == VAR_DICT
7334 || lhs.lhs_type->tt_type == VAR_LIST)
7335 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007336 && !(lhs.lhs_type->tt_member == &t_any
7337 && oplen > 0
7338 && rhs_type != NULL
7339 && rhs_type->tt_type == lhs.lhs_type->tt_type
7340 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007341 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007342 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007343 // also in legacy script. Not for "list<any> = val", then the
7344 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007345 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007346
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007347 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007348 {
7349 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007350 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007351 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007352 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007353 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007354
7355 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007356 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007358
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007359 // For "[var, var] = expr" drop the "expr" value.
7360 // Also for "[var, var; _] = expr".
7361 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007362 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007363 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007364 goto theend;
7365 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007366
Bram Moolenaarb2097502020-07-19 17:17:02 +02007367 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
7369theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007370 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 return ret;
7372}
7373
7374/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007375 * Check for an assignment at "eap->cmd", compile it if found.
7376 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7377 */
7378 static int
7379may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7380{
7381 char_u *pskip;
7382 char_u *p;
7383
7384 // Assuming the command starts with a variable or function name,
7385 // find what follows.
7386 // Skip over "var.member", "var[idx]" and the like.
7387 // Also "&opt = val", "$ENV = val" and "@r = val".
7388 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7389 ? eap->cmd + 1 : eap->cmd;
7390 p = to_name_end(pskip, TRUE);
7391 if (p > eap->cmd && *p != NUL)
7392 {
7393 char_u *var_end;
7394 int oplen;
7395 int heredoc;
7396
7397 if (eap->cmd[0] == '@')
7398 var_end = eap->cmd + 2;
7399 else
7400 var_end = find_name_end(pskip, NULL, NULL,
7401 FNE_CHECK_START | FNE_INCL_BR);
7402 oplen = assignment_len(skipwhite(var_end), &heredoc);
7403 if (oplen > 0)
7404 {
7405 size_t len = p - eap->cmd;
7406
7407 // Recognize an assignment if we recognize the variable
7408 // name:
7409 // "g:var = expr"
7410 // "local = expr" where "local" is a local var.
7411 // "script = expr" where "script" is a script-local var.
7412 // "import = expr" where "import" is an imported var
7413 // "&opt = expr"
7414 // "$ENV = expr"
7415 // "@r = expr"
7416 if (*eap->cmd == '&'
7417 || *eap->cmd == '$'
7418 || *eap->cmd == '@'
7419 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007420 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007421 {
7422 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7423 if (*line == NULL || *line == eap->cmd)
7424 return FAIL;
7425 return OK;
7426 }
7427 }
7428 }
7429
7430 if (*eap->cmd == '[')
7431 {
7432 // [var, var] = expr
7433 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7434 if (*line == NULL)
7435 return FAIL;
7436 if (*line != eap->cmd)
7437 return OK;
7438 }
7439 return NOTDONE;
7440}
7441
7442/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007443 * Check if "name" can be "unlet".
7444 */
7445 int
7446check_vim9_unlet(char_u *name)
7447{
7448 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7449 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007450 // "unlet s:var" is allowed in legacy script.
7451 if (*name == 's' && !script_is_vim9())
7452 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007453 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007454 return FAIL;
7455 }
7456 return OK;
7457}
7458
7459/*
7460 * Callback passed to ex_unletlock().
7461 */
7462 static int
7463compile_unlet(
7464 lval_T *lvp,
7465 char_u *name_end,
7466 exarg_T *eap,
7467 int deep UNUSED,
7468 void *coookie)
7469{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007470 cctx_T *cctx = coookie;
7471 char_u *p = lvp->ll_name;
7472 int cc = *name_end;
7473 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007474
Bram Moolenaar752fc692021-01-04 21:57:11 +01007475 if (cctx->ctx_skip == SKIP_YES)
7476 return OK;
7477
7478 *name_end = NUL;
7479 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007480 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007481 // :unlet $ENV_VAR
7482 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7483 }
7484 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7485 {
7486 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007487
Bram Moolenaar752fc692021-01-04 21:57:11 +01007488 // This is similar to assigning: lookup the list/dict, compile the
7489 // idx/key. Then instead of storing the value unlet the item.
7490 // unlet {list}[idx]
7491 // unlet {dict}[key] dict.key
7492 //
7493 // Figure out the LHS type and other properties.
7494 //
7495 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7496
7497 // : unlet an indexed item
7498 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007499 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007500 iemsg("called compile_lhs() without an index");
7501 ret = FAIL;
7502 }
7503 else
7504 {
7505 // Use the info in "lhs" to unlet the item at the index in the
7506 // list or dict.
7507 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007508 }
7509
Bram Moolenaar752fc692021-01-04 21:57:11 +01007510 vim_free(lhs.lhs_name);
7511 }
7512 else if (check_vim9_unlet(p) == FAIL)
7513 {
7514 ret = FAIL;
7515 }
7516 else
7517 {
7518 // Normal name. Only supports g:, w:, t: and b: namespaces.
7519 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007520 }
7521
Bram Moolenaar752fc692021-01-04 21:57:11 +01007522 *name_end = cc;
7523 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007524}
7525
7526/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007527 * Callback passed to ex_unletlock().
7528 */
7529 static int
7530compile_lock_unlock(
7531 lval_T *lvp,
7532 char_u *name_end,
7533 exarg_T *eap,
7534 int deep UNUSED,
7535 void *coookie)
7536{
7537 cctx_T *cctx = coookie;
7538 int cc = *name_end;
7539 char_u *p = lvp->ll_name;
7540 int ret = OK;
7541 size_t len;
7542 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007543 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007544
7545 if (cctx->ctx_skip == SKIP_YES)
7546 return OK;
7547
7548 // Cannot use :lockvar and :unlockvar on local variables.
7549 if (p[1] != ':')
7550 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007551 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007552
7553 if (lookup_local(p, end - p, NULL, cctx) == OK)
7554 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007555 char_u *s = p;
7556
7557 if (*end != '.' && *end != '[')
7558 {
7559 emsg(_(e_cannot_lock_unlock_local_variable));
7560 return FAIL;
7561 }
7562
7563 // For "d.member" put the local variable on the stack, it will be
7564 // passed to ex_lockvar() indirectly.
7565 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7566 return FAIL;
7567 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007568 }
7569 }
7570
7571 // Checking is done at runtime.
7572 *name_end = NUL;
7573 len = name_end - p + 20;
7574 buf = alloc(len);
7575 if (buf == NULL)
7576 ret = FAIL;
7577 else
7578 {
7579 vim_snprintf((char *)buf, len, "%s %s",
7580 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7581 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007582 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007583
7584 vim_free(buf);
7585 *name_end = cc;
7586 }
7587 return ret;
7588}
7589
7590/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007591 * compile "unlet var", "lock var" and "unlock var"
7592 * "arg" points to "var".
7593 */
7594 static char_u *
7595compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7596{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007597 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7598 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7599 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007600 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7601}
7602
7603/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7605 */
7606 static int
7607compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7608{
7609 garray_T *instr = &cctx->ctx_instr;
7610 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7611
7612 if (endlabel == NULL)
7613 return FAIL;
7614 endlabel->el_next = *el;
7615 *el = endlabel;
7616 endlabel->el_end_label = instr->ga_len;
7617
7618 generate_JUMP(cctx, when, 0);
7619 return OK;
7620}
7621
7622 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007623compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624{
7625 garray_T *instr = &cctx->ctx_instr;
7626
7627 while (*el != NULL)
7628 {
7629 endlabel_T *cur = (*el);
7630 isn_T *isn;
7631
7632 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007633 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 *el = cur->el_next;
7635 vim_free(cur);
7636 }
7637}
7638
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007639 static void
7640compile_free_jump_to_end(endlabel_T **el)
7641{
7642 while (*el != NULL)
7643 {
7644 endlabel_T *cur = (*el);
7645
7646 *el = cur->el_next;
7647 vim_free(cur);
7648 }
7649}
7650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651/*
7652 * Create a new scope and set up the generic items.
7653 */
7654 static scope_T *
7655new_scope(cctx_T *cctx, scopetype_T type)
7656{
7657 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7658
7659 if (scope == NULL)
7660 return NULL;
7661 scope->se_outer = cctx->ctx_scope;
7662 cctx->ctx_scope = scope;
7663 scope->se_type = type;
7664 scope->se_local_count = cctx->ctx_locals.ga_len;
7665 return scope;
7666}
7667
7668/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007669 * Free the current scope and go back to the outer scope.
7670 */
7671 static void
7672drop_scope(cctx_T *cctx)
7673{
7674 scope_T *scope = cctx->ctx_scope;
7675
7676 if (scope == NULL)
7677 {
7678 iemsg("calling drop_scope() without a scope");
7679 return;
7680 }
7681 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007682 switch (scope->se_type)
7683 {
7684 case IF_SCOPE:
7685 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7686 case FOR_SCOPE:
7687 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7688 case WHILE_SCOPE:
7689 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7690 case TRY_SCOPE:
7691 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7692 case NO_SCOPE:
7693 case BLOCK_SCOPE:
7694 break;
7695 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007696 vim_free(scope);
7697}
7698
7699/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 * compile "if expr"
7701 *
7702 * "if expr" Produces instructions:
7703 * EVAL expr Push result of "expr"
7704 * JUMP_IF_FALSE end
7705 * ... body ...
7706 * end:
7707 *
7708 * "if expr | else" Produces instructions:
7709 * EVAL expr Push result of "expr"
7710 * JUMP_IF_FALSE else
7711 * ... body ...
7712 * JUMP_ALWAYS end
7713 * else:
7714 * ... body ...
7715 * end:
7716 *
7717 * "if expr1 | elseif expr2 | else" Produces instructions:
7718 * EVAL expr Push result of "expr"
7719 * JUMP_IF_FALSE elseif
7720 * ... body ...
7721 * JUMP_ALWAYS end
7722 * elseif:
7723 * EVAL expr Push result of "expr"
7724 * JUMP_IF_FALSE else
7725 * ... body ...
7726 * JUMP_ALWAYS end
7727 * else:
7728 * ... body ...
7729 * end:
7730 */
7731 static char_u *
7732compile_if(char_u *arg, cctx_T *cctx)
7733{
7734 char_u *p = arg;
7735 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007736 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007738 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007739 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740
Bram Moolenaara5565e42020-05-09 15:44:01 +02007741 CLEAR_FIELD(ppconst);
7742 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007743 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007744 clear_ppconst(&ppconst);
7745 return NULL;
7746 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007747 if (!ends_excmd2(arg, skipwhite(p)))
7748 {
7749 semsg(_(e_trailing_arg), p);
7750 return NULL;
7751 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007752 if (cctx->ctx_skip == SKIP_YES)
7753 clear_ppconst(&ppconst);
7754 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007755 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007756 int error = FALSE;
7757 int v;
7758
Bram Moolenaara5565e42020-05-09 15:44:01 +02007759 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007760 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007761 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007762 if (error)
7763 return NULL;
7764 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007765 }
7766 else
7767 {
7768 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007769 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007770 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007771 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007772 if (bool_on_stack(cctx) == FAIL)
7773 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775
Bram Moolenaara91a7132021-03-25 21:12:15 +01007776 // CMDMOD_REV must come before the jump
7777 generate_undo_cmdmods(cctx);
7778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 scope = new_scope(cctx, IF_SCOPE);
7780 if (scope == NULL)
7781 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007782 scope->se_skip_save = skip_save;
7783 // "is_had_return" will be reset if any block does not end in :return
7784 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007786 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007787 {
7788 // "where" is set when ":elseif", "else" or ":endif" is found
7789 scope->se_u.se_if.is_if_label = instr->ga_len;
7790 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7791 }
7792 else
7793 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794
Bram Moolenaarced68a02021-01-24 17:53:47 +01007795#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007796 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007797 && skip_save != SKIP_YES)
7798 {
7799 // generated a profile start, need to generate a profile end, since it
7800 // won't be done after returning
7801 cctx->ctx_skip = SKIP_NOT;
7802 generate_instr(cctx, ISN_PROF_END);
7803 cctx->ctx_skip = SKIP_YES;
7804 }
7805#endif
7806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 return p;
7808}
7809
7810 static char_u *
7811compile_elseif(char_u *arg, cctx_T *cctx)
7812{
7813 char_u *p = arg;
7814 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007815 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 isn_T *isn;
7817 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007818 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007819 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820
7821 if (scope == NULL || scope->se_type != IF_SCOPE)
7822 {
7823 emsg(_(e_elseif_without_if));
7824 return NULL;
7825 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007826 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007827 if (!cctx->ctx_had_return)
7828 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829
Bram Moolenaarced68a02021-01-24 17:53:47 +01007830 if (cctx->ctx_skip == SKIP_NOT)
7831 {
7832 // previous block was executed, this one and following will not
7833 cctx->ctx_skip = SKIP_YES;
7834 scope->se_u.se_if.is_seen_skip_not = TRUE;
7835 }
7836 if (scope->se_u.se_if.is_seen_skip_not)
7837 {
7838 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007839 // Do not count the "elseif" for profiling and cmdmod
7840 instr->ga_len = current_instr_idx(cctx);
7841
Bram Moolenaarced68a02021-01-24 17:53:47 +01007842 skip_expr_cctx(&p, cctx);
7843 return p;
7844 }
7845
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007846 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007847 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007848 int moved_cmdmod = FALSE;
7849 int saved_debug = FALSE;
7850 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007851
7852 // Move any CMDMOD instruction to after the jump
7853 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7854 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007855 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007856 return NULL;
7857 ((isn_T *)instr->ga_data)[instr->ga_len] =
7858 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7859 --instr->ga_len;
7860 moved_cmdmod = TRUE;
7861 }
7862
Bram Moolenaar093165c2021-08-22 13:35:31 +02007863 // Remove the already generated ISN_DEBUG, it is written below the
7864 // ISN_FOR instruction.
7865 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7866 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7867 .isn_type == ISN_DEBUG)
7868 {
7869 --instr->ga_len;
7870 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7871 saved_debug = TRUE;
7872 }
7873
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007874 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007876 return NULL;
7877 // previous "if" or "elseif" jumps here
7878 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7879 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007880
Bram Moolenaara91a7132021-03-25 21:12:15 +01007881 if (moved_cmdmod)
7882 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007883
7884 if (saved_debug)
7885 {
7886 // move the debug instruction here
7887 if (GA_GROW_FAILS(instr, 1))
7888 return NULL;
7889 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7890 ++instr->ga_len;
7891 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007894 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007895 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007896 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007897 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007898 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007899#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007900 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007901 // the previous block was skipped, need to profile this line
7902 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007903#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007904 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007905 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007906 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007907 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007908
7909 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007910 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007911 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007912 clear_ppconst(&ppconst);
7913 return NULL;
7914 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007915 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007916 if (!ends_excmd2(arg, skipwhite(p)))
7917 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007918 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007919 semsg(_(e_trailing_arg), p);
7920 return NULL;
7921 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007922 if (scope->se_skip_save == SKIP_YES)
7923 clear_ppconst(&ppconst);
7924 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007925 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007926 int error = FALSE;
7927 int v;
7928
Bram Moolenaarfad27422021-11-30 21:58:19 +00007929 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007930 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7931 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007932 {
7933 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007934 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007935 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007936 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007937 clear_ppconst(&ppconst);
7938 scope->se_u.se_if.is_if_label = -1;
7939 }
7940 else
7941 {
7942 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007943 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007944 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007945 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007946 if (bool_on_stack(cctx) == FAIL)
7947 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948
Bram Moolenaara91a7132021-03-25 21:12:15 +01007949 // CMDMOD_REV must come before the jump
7950 generate_undo_cmdmods(cctx);
7951
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007952 // "where" is set when ":elseif", "else" or ":endif" is found
7953 scope->se_u.se_if.is_if_label = instr->ga_len;
7954 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
7957 return p;
7958}
7959
7960 static char_u *
7961compile_else(char_u *arg, cctx_T *cctx)
7962{
7963 char_u *p = arg;
7964 garray_T *instr = &cctx->ctx_instr;
7965 isn_T *isn;
7966 scope_T *scope = cctx->ctx_scope;
7967
7968 if (scope == NULL || scope->se_type != IF_SCOPE)
7969 {
7970 emsg(_(e_else_without_if));
7971 return NULL;
7972 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007973 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007974 if (!cctx->ctx_had_return)
7975 scope->se_u.se_if.is_had_return = FALSE;
7976 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977
Bram Moolenaarced68a02021-01-24 17:53:47 +01007978#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007979 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007980 {
7981 if (cctx->ctx_skip == SKIP_NOT
7982 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7983 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007984 // the previous block was executed, do not count "else" for
7985 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007986 --instr->ga_len;
7987 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7988 {
7989 // the previous block was not executed, this one will, do count the
7990 // "else" for profiling
7991 cctx->ctx_skip = SKIP_NOT;
7992 generate_instr(cctx, ISN_PROF_END);
7993 generate_instr(cctx, ISN_PROF_START);
7994 cctx->ctx_skip = SKIP_YES;
7995 }
7996 }
7997#endif
7998
7999 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008000 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008001 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008002 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008003 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008004 if (!cctx->ctx_had_return
8005 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8006 JUMP_ALWAYS, cctx) == FAIL)
8007 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008008 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008009
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008010 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008011 {
8012 if (scope->se_u.se_if.is_if_label >= 0)
8013 {
8014 // previous "if" or "elseif" jumps here
8015 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8016 isn->isn_arg.jump.jump_where = instr->ga_len;
8017 scope->se_u.se_if.is_if_label = -1;
8018 }
8019 }
8020
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008021 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008022 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024
8025 return p;
8026}
8027
8028 static char_u *
8029compile_endif(char_u *arg, cctx_T *cctx)
8030{
8031 scope_T *scope = cctx->ctx_scope;
8032 ifscope_T *ifscope;
8033 garray_T *instr = &cctx->ctx_instr;
8034 isn_T *isn;
8035
Bram Moolenaarfa984412021-03-25 22:15:28 +01008036 if (misplaced_cmdmod(cctx))
8037 return NULL;
8038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 if (scope == NULL || scope->se_type != IF_SCOPE)
8040 {
8041 emsg(_(e_endif_without_if));
8042 return NULL;
8043 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008044 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008045 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008046 if (!cctx->ctx_had_return)
8047 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008049 if (scope->se_u.se_if.is_if_label >= 0)
8050 {
8051 // previous "if" or "elseif" jumps here
8052 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8053 isn->isn_arg.jump.jump_where = instr->ga_len;
8054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008056 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008057
8058#ifdef FEAT_PROFILE
8059 // even when skipping we count the endif as executed, unless the block it's
8060 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008061 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008062 && scope->se_skip_save != SKIP_YES)
8063 {
8064 cctx->ctx_skip = SKIP_NOT;
8065 generate_instr(cctx, ISN_PROF_START);
8066 }
8067#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008068 cctx->ctx_skip = scope->se_skip_save;
8069
8070 // If all the blocks end in :return and there is an :else then the
8071 // had_return flag is set.
8072 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008074 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 return arg;
8076}
8077
8078/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008079 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080 *
8081 * Produces instructions:
8082 * PUSHNR -1
8083 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008084 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8086 * - if beyond end, jump to "end"
8087 * - otherwise get item from list and push it
8088 * STORE var Store item in "var"
8089 * ... body ...
8090 * JUMP top Jump back to repeat
8091 * end: DROP Drop the result of "expr"
8092 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008093 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8094 * UNPACK 2 Split item in 2
8095 * STORE var1 Store item in "var1"
8096 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 */
8098 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008099compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008101 char_u *arg;
8102 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008103 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008105 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008106 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008107 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008108 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008111 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008113 lvar_T *loop_lvar; // loop iteration variable
8114 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008116 type_T *item_type = &t_any;
8117 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008118 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119
Bram Moolenaar792f7862020-11-23 08:31:18 +01008120 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008121 if (p == NULL)
8122 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008123 if (var_count == 0)
8124 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008125 else
8126 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127
8128 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008129 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008130 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8131 return NULL;
8132 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008134 if (*p == ':' && wp != p)
8135 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8136 else
8137 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 return NULL;
8139 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008140 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008141 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8142 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008144 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8145 // instruction.
8146 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8147 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8148 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008149 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008150 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008151 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8152 .isn_arg.debug.dbg_break_lnum;
8153 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 scope = new_scope(cctx, FOR_SCOPE);
8156 if (scope == NULL)
8157 return NULL;
8158
Bram Moolenaar792f7862020-11-23 08:31:18 +01008159 // Reserve a variable to store the loop iteration counter and initialize it
8160 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008161 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8162 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008163 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008164 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008165 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008167 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008168 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169
8170 // compile "expr", it remains on the stack until "endfor"
8171 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008172 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008173 {
8174 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008176 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008177 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178
rbtnnbebf0692021-08-21 17:26:50 +02008179 if (cctx->ctx_skip != SKIP_YES)
8180 {
8181 // If we know the type of "var" and it is a not a supported type we can
8182 // give an error now.
8183 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8184 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008185 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008186 {
rbtnnbebf0692021-08-21 17:26:50 +02008187 semsg(_(e_for_loop_on_str_not_supported),
8188 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008189 drop_scope(cctx);
8190 return NULL;
8191 }
rbtnnbebf0692021-08-21 17:26:50 +02008192
8193 if (vartype->tt_type == VAR_STRING)
8194 item_type = &t_string;
8195 else if (vartype->tt_type == VAR_BLOB)
8196 item_type = &t_number;
8197 else if (vartype->tt_type == VAR_LIST
8198 && vartype->tt_member->tt_type != VAR_ANY)
8199 {
8200 if (!var_list)
8201 item_type = vartype->tt_member;
8202 else if (vartype->tt_member->tt_type == VAR_LIST
8203 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008204 item_type = vartype->tt_member->tt_member;
8205 }
8206
8207 // CMDMOD_REV must come before the FOR instruction.
8208 generate_undo_cmdmods(cctx);
8209
8210 // "for_end" is set when ":endfor" is found
8211 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8212
8213 generate_FOR(cctx, loop_lvar->lv_idx);
8214
8215 arg = arg_start;
8216 if (var_list)
8217 {
8218 generate_UNPACK(cctx, var_count, semicolon);
8219 arg = skipwhite(arg + 1); // skip white after '['
8220
8221 // the list item is replaced by a number of items
8222 if (GA_GROW_FAILS(stack, var_count - 1))
8223 {
8224 drop_scope(cctx);
8225 return NULL;
8226 }
8227 --stack->ga_len;
8228 for (idx = 0; idx < var_count; ++idx)
8229 {
8230 ((type_T **)stack->ga_data)[stack->ga_len] =
8231 (semicolon && idx == 0) ? vartype : item_type;
8232 ++stack->ga_len;
8233 }
8234 }
8235
Bram Moolenaar792f7862020-11-23 08:31:18 +01008236 for (idx = 0; idx < var_count; ++idx)
8237 {
rbtnnbebf0692021-08-21 17:26:50 +02008238 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008239 int opt_flags = 0;
8240 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008241 type_T *type = &t_any;
8242 type_T *lhs_type = &t_any;
8243 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008244
rbtnnbebf0692021-08-21 17:26:50 +02008245 p = skip_var_one(arg, FALSE);
8246 varlen = p - arg;
8247 name = vim_strnsave(arg, varlen);
8248 if (name == NULL)
8249 goto failed;
8250 if (*p == ':')
8251 {
8252 p = skipwhite(p + 1);
8253 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8254 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008255
Bram Moolenaarfad27422021-11-30 21:58:19 +00008256 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008257 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008258 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008259 goto failed;
8260 if (dest != dest_local)
8261 {
8262 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008263 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008264 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008265 }
rbtnnbebf0692021-08-21 17:26:50 +02008266 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008267 {
rbtnnbebf0692021-08-21 17:26:50 +02008268 // Assigning to "_": drop the value.
8269 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8270 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008271 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008272 else
rbtnnbebf0692021-08-21 17:26:50 +02008273 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008274 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008275 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008276 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8277 {
8278 semsg(_(e_variable_already_declared), arg);
8279 goto failed;
8280 }
8281
8282 if (STRNCMP(name, "s:", 2) == 0)
8283 {
8284 semsg(_(e_cannot_declare_script_variable_in_function), name);
8285 goto failed;
8286 }
8287
8288 // Reserve a variable to store "var".
8289 where.wt_index = var_list ? idx + 1 : 0;
8290 where.wt_variable = TRUE;
8291 if (lhs_type == &t_any)
8292 lhs_type = item_type;
8293 else if (item_type != &t_unknown
8294 && (item_type == &t_any
8295 ? need_type(item_type, lhs_type,
8296 -1, 0, cctx, FALSE, FALSE)
8297 : check_type(lhs_type, item_type, TRUE, where))
8298 == FAIL)
8299 goto failed;
8300 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8301 if (var_lvar == NULL)
8302 // out of memory or used as an argument
8303 goto failed;
8304
8305 if (semicolon && idx == var_count - 1)
8306 var_lvar->lv_type = vartype;
8307 else
8308 var_lvar->lv_type = item_type;
8309 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8310 }
8311
8312 if (*p == ',' || *p == ';')
8313 ++p;
8314 arg = skipwhite(p);
8315 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008316 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008317
rbtnnbebf0692021-08-21 17:26:50 +02008318 if (cctx->ctx_compile_type == CT_DEBUG)
8319 {
8320 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008321
rbtnnbebf0692021-08-21 17:26:50 +02008322 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8323 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8324 cctx->ctx_prev_lnum = prev_lnum;
8325 generate_instr_debug(cctx);
8326 cctx->ctx_prev_lnum = save_prev_lnum;
8327 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008328 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008329
Bram Moolenaar792f7862020-11-23 08:31:18 +01008330 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008331
8332failed:
8333 vim_free(name);
8334 drop_scope(cctx);
8335 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336}
8337
8338/*
8339 * compile "endfor"
8340 */
8341 static char_u *
8342compile_endfor(char_u *arg, cctx_T *cctx)
8343{
8344 garray_T *instr = &cctx->ctx_instr;
8345 scope_T *scope = cctx->ctx_scope;
8346 forscope_T *forscope;
8347 isn_T *isn;
8348
Bram Moolenaarfa984412021-03-25 22:15:28 +01008349 if (misplaced_cmdmod(cctx))
8350 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 if (scope == NULL || scope->se_type != FOR_SCOPE)
8353 {
8354 emsg(_(e_for));
8355 return NULL;
8356 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008357 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008358 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008359 if (cctx->ctx_skip != SKIP_YES)
8360 {
8361 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008362
rbtnnbebf0692021-08-21 17:26:50 +02008363 // At end of ":for" scope jump back to the FOR instruction.
8364 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008365
rbtnnbebf0692021-08-21 17:26:50 +02008366 // Fill in the "end" label in the FOR statement so it can jump here.
8367 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8368 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008369
rbtnnbebf0692021-08-21 17:26:50 +02008370 // Fill in the "end" label any BREAK statements
8371 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372
rbtnnbebf0692021-08-21 17:26:50 +02008373 // Below the ":for" scope drop the "expr" list from the stack.
8374 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8375 return NULL;
8376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008377
8378 vim_free(scope);
8379
8380 return arg;
8381}
8382
8383/*
8384 * compile "while expr"
8385 *
8386 * Produces instructions:
8387 * top: EVAL expr Push result of "expr"
8388 * JUMP_IF_FALSE end jump if false
8389 * ... body ...
8390 * JUMP top Jump back to repeat
8391 * end:
8392 *
8393 */
8394 static char_u *
8395compile_while(char_u *arg, cctx_T *cctx)
8396{
8397 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008398 scope_T *scope;
8399
8400 scope = new_scope(cctx, WHILE_SCOPE);
8401 if (scope == NULL)
8402 return NULL;
8403
Bram Moolenaara91a7132021-03-25 21:12:15 +01008404 // "endwhile" jumps back here, one before when profiling or using cmdmods
8405 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406
8407 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008408 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008410
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008411 if (!ends_excmd2(arg, skipwhite(p)))
8412 {
8413 semsg(_(e_trailing_arg), p);
8414 return NULL;
8415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416
rbtnnd895b1d2021-08-20 20:54:25 +02008417 if (cctx->ctx_skip != SKIP_YES)
8418 {
8419 if (bool_on_stack(cctx) == FAIL)
8420 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008421
rbtnnd895b1d2021-08-20 20:54:25 +02008422 // CMDMOD_REV must come before the jump
8423 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008424
rbtnnd895b1d2021-08-20 20:54:25 +02008425 // "while_end" is set when ":endwhile" is found
8426 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008428 return FAIL;
8429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430
8431 return p;
8432}
8433
8434/*
8435 * compile "endwhile"
8436 */
8437 static char_u *
8438compile_endwhile(char_u *arg, cctx_T *cctx)
8439{
8440 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008441 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442
Bram Moolenaarfa984412021-03-25 22:15:28 +01008443 if (misplaced_cmdmod(cctx))
8444 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8446 {
8447 emsg(_(e_while));
8448 return NULL;
8449 }
8450 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008451 if (cctx->ctx_skip != SKIP_YES)
8452 {
8453 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008454
Bram Moolenaarf002a412021-01-24 13:34:18 +01008455#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008456 // count the endwhile before jumping
8457 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008458#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008459
rbtnnd895b1d2021-08-20 20:54:25 +02008460 // At end of ":for" scope jump back to the FOR instruction.
8461 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462
rbtnnd895b1d2021-08-20 20:54:25 +02008463 // Fill in the "end" label in the WHILE statement so it can jump here.
8464 // And in any jumps for ":break"
8465 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008466 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008468
8469 vim_free(scope);
8470
8471 return arg;
8472}
8473
8474/*
8475 * compile "continue"
8476 */
8477 static char_u *
8478compile_continue(char_u *arg, cctx_T *cctx)
8479{
8480 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008481 int try_scopes = 0;
8482 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008483
8484 for (;;)
8485 {
8486 if (scope == NULL)
8487 {
8488 emsg(_(e_continue));
8489 return NULL;
8490 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008491 if (scope->se_type == FOR_SCOPE)
8492 {
8493 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008495 }
8496 if (scope->se_type == WHILE_SCOPE)
8497 {
8498 loop_label = scope->se_u.se_while.ws_top_label;
8499 break;
8500 }
8501 if (scope->se_type == TRY_SCOPE)
8502 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 scope = scope->se_outer;
8504 }
8505
Bram Moolenaarc150c092021-02-13 15:02:46 +01008506 if (try_scopes > 0)
8507 // Inside one or more try/catch blocks we first need to jump to the
8508 // "finally" or "endtry" to cleanup.
8509 generate_TRYCONT(cctx, try_scopes, loop_label);
8510 else
8511 // Jump back to the FOR or WHILE instruction.
8512 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 return arg;
8515}
8516
8517/*
8518 * compile "break"
8519 */
8520 static char_u *
8521compile_break(char_u *arg, cctx_T *cctx)
8522{
8523 scope_T *scope = cctx->ctx_scope;
8524 endlabel_T **el;
8525
8526 for (;;)
8527 {
8528 if (scope == NULL)
8529 {
8530 emsg(_(e_break));
8531 return NULL;
8532 }
8533 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8534 break;
8535 scope = scope->se_outer;
8536 }
8537
8538 // Jump to the end of the FOR or WHILE loop.
8539 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008540 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008541 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008542 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8544 return FAIL;
8545
8546 return arg;
8547}
8548
8549/*
8550 * compile "{" start of block
8551 */
8552 static char_u *
8553compile_block(char_u *arg, cctx_T *cctx)
8554{
8555 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8556 return NULL;
8557 return skipwhite(arg + 1);
8558}
8559
8560/*
8561 * compile end of block: drop one scope
8562 */
8563 static void
8564compile_endblock(cctx_T *cctx)
8565{
8566 scope_T *scope = cctx->ctx_scope;
8567
8568 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008569 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 vim_free(scope);
8571}
8572
8573/*
8574 * compile "try"
8575 * Creates a new scope for the try-endtry, pointing to the first catch and
8576 * finally.
8577 * Creates another scope for the "try" block itself.
8578 * TRY instruction sets up exception handling at runtime.
8579 *
8580 * "try"
8581 * TRY -> catch1, -> finally push trystack entry
8582 * ... try block
8583 * "throw {exception}"
8584 * EVAL {exception}
8585 * THROW create exception
8586 * ... try block
8587 * " catch {expr}"
8588 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008589 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008590 * EVAL {expr}
8591 * MATCH
8592 * JUMP nomatch -> catch2
8593 * CATCH remove exception
8594 * ... catch block
8595 * " catch"
8596 * JUMP -> finally
8597 * catch2: CATCH remove exception
8598 * ... catch block
8599 * " finally"
8600 * finally:
8601 * ... finally block
8602 * " endtry"
8603 * ENDTRY pop trystack entry, may rethrow
8604 */
8605 static char_u *
8606compile_try(char_u *arg, cctx_T *cctx)
8607{
8608 garray_T *instr = &cctx->ctx_instr;
8609 scope_T *try_scope;
8610 scope_T *scope;
8611
Bram Moolenaarfa984412021-03-25 22:15:28 +01008612 if (misplaced_cmdmod(cctx))
8613 return NULL;
8614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008615 // scope that holds the jumps that go to catch/finally/endtry
8616 try_scope = new_scope(cctx, TRY_SCOPE);
8617 if (try_scope == NULL)
8618 return NULL;
8619
Bram Moolenaar69f70502021-01-01 16:10:46 +01008620 if (cctx->ctx_skip != SKIP_YES)
8621 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008622 isn_T *isn;
8623
8624 // "try_catch" is set when the first ":catch" is found or when no catch
8625 // is found and ":finally" is found.
8626 // "try_finally" is set when ":finally" is found
8627 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008628 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008629 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8630 return NULL;
8631 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8632 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008633 return NULL;
8634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635
8636 // scope for the try block itself
8637 scope = new_scope(cctx, BLOCK_SCOPE);
8638 if (scope == NULL)
8639 return NULL;
8640
8641 return arg;
8642}
8643
8644/*
8645 * compile "catch {expr}"
8646 */
8647 static char_u *
8648compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8649{
8650 scope_T *scope = cctx->ctx_scope;
8651 garray_T *instr = &cctx->ctx_instr;
8652 char_u *p;
8653 isn_T *isn;
8654
Bram Moolenaarfa984412021-03-25 22:15:28 +01008655 if (misplaced_cmdmod(cctx))
8656 return NULL;
8657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008658 // end block scope from :try or :catch
8659 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8660 compile_endblock(cctx);
8661 scope = cctx->ctx_scope;
8662
8663 // Error if not in a :try scope
8664 if (scope == NULL || scope->se_type != TRY_SCOPE)
8665 {
8666 emsg(_(e_catch));
8667 return NULL;
8668 }
8669
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008670 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008672 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 return NULL;
8674 }
8675
Bram Moolenaar69f70502021-01-01 16:10:46 +01008676 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008678#ifdef FEAT_PROFILE
8679 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008680 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008681 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008682 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008683 .isn_type == ISN_PROF_START)
8684 --instr->ga_len;
8685#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008686 // Jump from end of previous block to :finally or :endtry
8687 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8688 JUMP_ALWAYS, cctx) == FAIL)
8689 return NULL;
8690
8691 // End :try or :catch scope: set value in ISN_TRY instruction
8692 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008693 if (isn->isn_arg.try.try_ref->try_catch == 0)
8694 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008695 if (scope->se_u.se_try.ts_catch_label != 0)
8696 {
8697 // Previous catch without match jumps here
8698 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8699 isn->isn_arg.jump.jump_where = instr->ga_len;
8700 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008701#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008702 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008703 {
8704 // a "throw" that jumps here needs to be counted
8705 generate_instr(cctx, ISN_PROF_END);
8706 // the "catch" is also counted
8707 generate_instr(cctx, ISN_PROF_START);
8708 }
8709#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008710 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008711 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008712 }
8713
8714 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008715 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008716 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008717 scope->se_u.se_try.ts_caught_all = TRUE;
8718 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 }
8720 else
8721 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008722 char_u *end;
8723 char_u *pat;
8724 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008725 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008726 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728 // Push v:exception, push {expr} and MATCH
8729 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8730
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008731 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008732 if (*end != *p)
8733 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008734 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008735 vim_free(tofree);
8736 return FAIL;
8737 }
8738 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008739 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008740 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008741 len = (int)(end - tofree);
8742 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008743 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008744 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008745 if (pat == NULL)
8746 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008747 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008748 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8751 return NULL;
8752
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008753 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8755 return NULL;
8756 }
8757
Bram Moolenaar69f70502021-01-01 16:10:46 +01008758 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008759 return NULL;
8760
8761 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8762 return NULL;
8763 return p;
8764}
8765
8766 static char_u *
8767compile_finally(char_u *arg, cctx_T *cctx)
8768{
8769 scope_T *scope = cctx->ctx_scope;
8770 garray_T *instr = &cctx->ctx_instr;
8771 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008772 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008773
Bram Moolenaarfa984412021-03-25 22:15:28 +01008774 if (misplaced_cmdmod(cctx))
8775 return NULL;
8776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777 // end block scope from :try or :catch
8778 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8779 compile_endblock(cctx);
8780 scope = cctx->ctx_scope;
8781
8782 // Error if not in a :try scope
8783 if (scope == NULL || scope->se_type != TRY_SCOPE)
8784 {
8785 emsg(_(e_finally));
8786 return NULL;
8787 }
8788
rbtnn84934992021-08-07 13:26:53 +02008789 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008790 {
rbtnn84934992021-08-07 13:26:53 +02008791 // End :catch or :finally scope: set value in ISN_TRY instruction
8792 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8793 if (isn->isn_arg.try.try_ref->try_finally != 0)
8794 {
8795 emsg(_(e_finally_dup));
8796 return NULL;
8797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798
rbtnn84934992021-08-07 13:26:53 +02008799 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008800#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008801 if (cctx->ctx_compile_type == CT_PROFILE
8802 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008803 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008804 {
8805 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008806 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008807
8808 // jump to the profile end above it
8809 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008810 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008811 --this_instr;
8812 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008813#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008814
rbtnn84934992021-08-07 13:26:53 +02008815 // Fill in the "end" label in jumps at the end of the blocks.
8816 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008817 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008818
rbtnn84934992021-08-07 13:26:53 +02008819 // If there is no :catch then an exception jumps to :finally.
8820 if (isn->isn_arg.try.try_ref->try_catch == 0)
8821 isn->isn_arg.try.try_ref->try_catch = this_instr;
8822 isn->isn_arg.try.try_ref->try_finally = this_instr;
8823 if (scope->se_u.se_try.ts_catch_label != 0)
8824 {
8825 // Previous catch without match jumps here
8826 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8827 isn->isn_arg.jump.jump_where = this_instr;
8828 scope->se_u.se_try.ts_catch_label = 0;
8829 }
8830 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8831 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833
8834 return arg;
8835}
8836
8837 static char_u *
8838compile_endtry(char_u *arg, cctx_T *cctx)
8839{
8840 scope_T *scope = cctx->ctx_scope;
8841 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008842 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008843
Bram Moolenaarfa984412021-03-25 22:15:28 +01008844 if (misplaced_cmdmod(cctx))
8845 return NULL;
8846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008847 // end block scope from :catch or :finally
8848 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8849 compile_endblock(cctx);
8850 scope = cctx->ctx_scope;
8851
8852 // Error if not in a :try scope
8853 if (scope == NULL || scope->se_type != TRY_SCOPE)
8854 {
8855 if (scope == NULL)
8856 emsg(_(e_no_endtry));
8857 else if (scope->se_type == WHILE_SCOPE)
8858 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008859 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008860 emsg(_(e_endfor));
8861 else
8862 emsg(_(e_endif));
8863 return NULL;
8864 }
8865
Bram Moolenaarc150c092021-02-13 15:02:46 +01008866 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008867 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008868 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008869 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8870 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008871 {
8872 emsg(_(e_missing_catch_or_finally));
8873 return NULL;
8874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008876#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008877 if (cctx->ctx_compile_type == CT_PROFILE
8878 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008879 .isn_type == ISN_PROF_START)
8880 // move the profile start after "endtry" so that it's not counted when
8881 // the exception is rethrown.
8882 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008883#endif
8884
Bram Moolenaar69f70502021-01-01 16:10:46 +01008885 // Fill in the "end" label in jumps at the end of the blocks, if not
8886 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008887 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8888 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889
Bram Moolenaar69f70502021-01-01 16:10:46 +01008890 if (scope->se_u.se_try.ts_catch_label != 0)
8891 {
8892 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008893 isn_T *isn = ((isn_T *)instr->ga_data)
8894 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008895 isn->isn_arg.jump.jump_where = instr->ga_len;
8896 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008897 }
8898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 compile_endblock(cctx);
8900
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008901 if (cctx->ctx_skip != SKIP_YES)
8902 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008903 // End :catch or :finally scope: set instruction index in ISN_TRY
8904 // instruction
8905 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008906 if (cctx->ctx_skip != SKIP_YES
8907 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8908 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008909#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008910 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008911 generate_instr(cctx, ISN_PROF_START);
8912#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008914 return arg;
8915}
8916
8917/*
8918 * compile "throw {expr}"
8919 */
8920 static char_u *
8921compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8922{
8923 char_u *p = skipwhite(arg);
8924
Bram Moolenaara5565e42020-05-09 15:44:01 +02008925 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008927 if (cctx->ctx_skip == SKIP_YES)
8928 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008929 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930 return NULL;
8931 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8932 return NULL;
8933
8934 return p;
8935}
8936
Bram Moolenaarc3235272021-07-10 19:42:03 +02008937 static char_u *
8938compile_eval(char_u *arg, cctx_T *cctx)
8939{
8940 char_u *p = arg;
8941 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008942 long lnum = SOURCING_LNUM;
8943
8944 // find_ex_command() will consider a variable name an expression, assuming
8945 // that something follows on the next line. Check that something actually
8946 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008947 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008948
Bram Moolenaarc3235272021-07-10 19:42:03 +02008949 if (compile_expr0(&p, cctx) == FAIL)
8950 return NULL;
8951
8952 if (name_only && lnum == SOURCING_LNUM)
8953 {
8954 semsg(_(e_expression_without_effect_str), arg);
8955 return NULL;
8956 }
8957
8958 // drop the result
8959 generate_instr_drop(cctx, ISN_DROP, 1);
8960
8961 return skipwhite(p);
8962}
8963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008964/*
8965 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008966 * compile "echomsg expr"
8967 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008968 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008969 * compile "execute expr"
8970 */
8971 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008972compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008973{
8974 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008975 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008976 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008977 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008978 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008979 garray_T *stack = &cctx->ctx_type_stack;
8980 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008981
8982 for (;;)
8983 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008984 if (ends_excmd2(prev, p))
8985 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008986 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008987 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008988 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008989
8990 if (cctx->ctx_skip != SKIP_YES)
8991 {
8992 // check for non-void type
8993 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8994 if (type->tt_type == VAR_VOID)
8995 {
8996 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8997 return NULL;
8998 }
8999 }
9000
Bram Moolenaarad39c092020-02-26 18:23:43 +01009001 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009002 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009003 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009004 }
9005
Bram Moolenaare4984292020-12-13 14:19:25 +01009006 if (count > 0)
9007 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009008 long save_lnum = cctx->ctx_lnum;
9009
9010 // Use the line number where the command started.
9011 cctx->ctx_lnum = start_ctx_lnum;
9012
Bram Moolenaare4984292020-12-13 14:19:25 +01009013 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9014 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9015 else if (cmdidx == CMD_execute)
9016 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9017 else if (cmdidx == CMD_echomsg)
9018 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009019 else if (cmdidx == CMD_echoconsole)
9020 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009021 else
9022 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009023
9024 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 return p;
9027}
9028
9029/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009030 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009031 * instruction to compute it and return OK.
9032 * Otherwise return FAIL, the caller must deal with any range.
9033 */
9034 static int
9035compile_variable_range(exarg_T *eap, cctx_T *cctx)
9036{
9037 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9038 char_u *p = skipdigits(eap->cmd);
9039
9040 if (p == range_end)
9041 return FAIL;
9042 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9043}
9044
9045/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009046 * :put r
9047 * :put ={expr}
9048 */
9049 static char_u *
9050compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9051{
9052 char_u *line = arg;
9053 linenr_T lnum;
9054 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009055 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009056
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009057 eap->regname = *line;
9058
9059 if (eap->regname == '=')
9060 {
9061 char_u *p = line + 1;
9062
9063 if (compile_expr0(&p, cctx) == FAIL)
9064 return NULL;
9065 line = p;
9066 }
9067 else if (eap->regname != NUL)
9068 ++line;
9069
Bram Moolenaar08597872020-12-10 19:43:40 +01009070 if (compile_variable_range(eap, cctx) == OK)
9071 {
9072 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9073 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009074 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009075 {
9076 // Either no range or a number.
9077 // "errormsg" will not be set because the range is ADDR_LINES.
9078 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009079 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009080 return NULL;
9081 if (eap->addr_count == 0)
9082 lnum = -1;
9083 else
9084 lnum = eap->line2;
9085 if (above)
9086 --lnum;
9087 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009088
9089 generate_PUT(cctx, eap->regname, lnum);
9090 return line;
9091}
9092
9093/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009094 * A command that is not compiled, execute with legacy code.
9095 */
9096 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009097compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009098{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009099 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009100 char_u *p;
9101 int has_expr = FALSE;
9102 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009103 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009104 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009105
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009106 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009107 goto theend;
9108
Bram Moolenaare729ce22021-06-06 21:38:09 +02009109 // If there was a prececing command modifier, drop it and include it in the
9110 // EXEC command.
9111 if (cctx->ctx_has_cmdmod)
9112 {
9113 garray_T *instr = &cctx->ctx_instr;
9114 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9115
9116 if (isn->isn_type == ISN_CMDMOD)
9117 {
9118 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9119 ->cmod_filter_regmatch.regprog);
9120 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9121 --instr->ga_len;
9122 cctx->ctx_has_cmdmod = FALSE;
9123 }
9124 }
9125
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009126 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009127 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009128 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009129 int usefilter = FALSE;
9130
9131 has_expr = argt & (EX_XFILE | EX_EXPAND);
9132
9133 // If the command can be followed by a bar, find the bar and truncate
9134 // it, so that the following command can be compiled.
9135 // The '|' is overwritten with a NUL, it is put back below.
9136 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9137 && *eap->arg == '!')
9138 // :w !filter or :r !filter or :r! filter
9139 usefilter = TRUE;
9140 if ((argt & EX_TRLBAR) && !usefilter)
9141 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009142 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009143 separate_nextcmd(eap);
9144 if (eap->nextcmd != NULL)
9145 nextcmd = eap->nextcmd;
9146 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009147 else if (eap->cmdidx == CMD_wincmd)
9148 {
9149 p = eap->arg;
9150 if (*p != NUL)
9151 ++p;
9152 if (*p == 'g' || *p == Ctrl_G)
9153 ++p;
9154 p = skipwhite(p);
9155 if (*p == '|')
9156 {
9157 *p = NUL;
9158 nextcmd = p + 1;
9159 }
9160 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009161 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9162 {
9163 // If there is a trailing '{' read lines until the '}'
9164 p = eap->arg + STRLEN(eap->arg) - 1;
9165 while (p > eap->arg && VIM_ISWHITE(*p))
9166 --p;
9167 if (*p == '{')
9168 {
9169 exarg_T ea;
9170 int flags; // unused
9171 int start_lnum = SOURCING_LNUM;
9172
9173 CLEAR_FIELD(ea);
9174 ea.arg = eap->arg;
9175 fill_exarg_from_cctx(&ea, cctx);
9176 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9177 if (tofree != NULL)
9178 {
9179 *p = NUL;
9180 line = concat_str(line, tofree);
9181 if (line == NULL)
9182 goto theend;
9183 vim_free(tofree);
9184 tofree = line;
9185 SOURCING_LNUM = start_lnum;
9186 }
9187 }
9188 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009189 }
9190
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009191 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9192 {
9193 // expand filename in "syntax include [@group] filename"
9194 has_expr = TRUE;
9195 eap->arg = skipwhite(eap->arg + 7);
9196 if (*eap->arg == '@')
9197 eap->arg = skiptowhite(eap->arg);
9198 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009199
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009200 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9201 && STRLEN(eap->arg) > 4)
9202 {
9203 int delim = *eap->arg;
9204
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009205 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009206 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009207 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009208 }
9209
Bram Moolenaarecac5912021-01-05 19:23:28 +01009210 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009211 cmd_arg = eap->arg;
9212
9213 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009214 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009215 exarg_T nea;
9216
9217 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009218 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009219 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009220 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009221 {
9222 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9223 if (has_expr)
9224 eap->arg = skiptowhite(eap->arg);
9225 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009226 }
9227
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009228 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009229 {
9230 int count = 0;
9231 char_u *start = skipwhite(line);
9232
9233 // :cmd xxx`=expr1`yyy`=expr2`zzz
9234 // PUSHS ":cmd xxx"
9235 // eval expr1
9236 // PUSHS "yyy"
9237 // eval expr2
9238 // PUSHS "zzz"
9239 // EXECCONCAT 5
9240 for (;;)
9241 {
9242 if (p > start)
9243 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009244 char_u *val = vim_strnsave(start, p - start);
9245
9246 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009247 ++count;
9248 }
9249 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009250 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009251 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009252 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009253 ++count;
9254 p = skipwhite(p);
9255 if (*p != '`')
9256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009257 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009258 return NULL;
9259 }
9260 start = p + 1;
9261
9262 p = (char_u *)strstr((char *)start, "`=");
9263 if (p == NULL)
9264 {
9265 if (*skipwhite(start) != NUL)
9266 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009267 char_u *val = vim_strsave(start);
9268
9269 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009270 ++count;
9271 }
9272 break;
9273 }
9274 }
9275 generate_EXECCONCAT(cctx, count);
9276 }
9277 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009278 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009279
9280theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009281 if (*nextcmd != NUL)
9282 {
9283 // the parser expects a pointer to the bar, put it back
9284 --nextcmd;
9285 *nextcmd = '|';
9286 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009287 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009288
9289 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009290}
9291
Bram Moolenaar20677332021-06-06 17:02:53 +02009292/*
9293 * A script command with heredoc, e.g.
9294 * ruby << EOF
9295 * command
9296 * EOF
9297 * Has been turned into one long line with NL characters by
9298 * get_function_body():
9299 * ruby << EOF<NL> command<NL>EOF
9300 */
9301 static char_u *
9302compile_script(char_u *line, cctx_T *cctx)
9303{
9304 if (cctx->ctx_skip != SKIP_YES)
9305 {
9306 isn_T *isn;
9307
9308 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9309 return NULL;
9310 isn->isn_arg.string = vim_strsave(line);
9311 }
9312 return (char_u *)"";
9313}
9314
Bram Moolenaar8238f082021-04-20 21:10:48 +02009315
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009316/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009317 * :s/pat/repl/
9318 */
9319 static char_u *
9320compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9321{
9322 char_u *cmd = eap->arg;
9323 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9324
9325 if (expr != NULL)
9326 {
9327 int delimiter = *cmd++;
9328
9329 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009330 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009331 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9332 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009333 garray_T save_ga = cctx->ctx_instr;
9334 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009335 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009336 int trailing_error;
9337 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009338 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009339 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009340
9341 cmd += 3;
9342 end = skip_substitute(cmd, delimiter);
9343
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009344 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009345 // labels are correct.
9346 cctx->ctx_instr.ga_len = 0;
9347 cctx->ctx_instr.ga_maxlen = 0;
9348 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009349 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009350 if (end[-1] == NUL)
9351 end[-1] = delimiter;
9352 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009353 trailing_error = *cmd != delimiter && *cmd != NUL;
9354
Bram Moolenaar169502f2021-04-21 12:19:35 +02009355 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009356 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009357 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009358 if (trailing_error)
9359 semsg(_(e_trailing_arg), cmd);
9360 clear_instr_ga(&cctx->ctx_instr);
9361 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009362 return NULL;
9363 }
9364
Bram Moolenaar8238f082021-04-20 21:10:48 +02009365 // Move the generated instructions into the ISN_SUBSTITUTE
9366 // instructions, then restore the list of instructions before
9367 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009368 instr_count = cctx->ctx_instr.ga_len;
9369 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009370 instr[instr_count].isn_type = ISN_FINISH;
9371
9372 cctx->ctx_instr = save_ga;
9373 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9374 {
9375 int idx;
9376
9377 for (idx = 0; idx < instr_count; ++idx)
9378 delete_instr(instr + idx);
9379 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009380 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009381 }
9382 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9383 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009384
9385 // skip over flags
9386 if (*end == '&')
9387 ++end;
9388 while (ASCII_ISALPHA(*end) || *end == '#')
9389 ++end;
9390 return end;
9391 }
9392 }
9393
9394 return compile_exec(arg, eap, cctx);
9395}
9396
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009397 static char_u *
9398compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9399{
9400 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009401 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009402
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009403 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009404 {
9405 if (STRNCMP(arg, "END", 3) == 0)
9406 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009407 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009408 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009409 // First load the current variable value.
9410 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9411 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009412 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009413 }
9414
9415 // Gets the redirected text and put it on the stack, then store it
9416 // in the variable.
9417 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9418
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009419 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009420 generate_instr_drop(cctx, ISN_CONCAT, 1);
9421
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009422 if (lhs->lhs_has_index)
9423 {
9424 // Use the info in "lhs" to store the value at the index in the
9425 // list or dict.
9426 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9427 &t_string, cctx) == FAIL)
9428 return NULL;
9429 }
9430 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009431 return NULL;
9432
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009433 VIM_CLEAR(lhs->lhs_name);
9434 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009435 return arg + 3;
9436 }
9437 emsg(_(e_cannot_nest_redir));
9438 return NULL;
9439 }
9440
9441 if (arg[0] == '=' && arg[1] == '>')
9442 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009443 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009444
9445 // redirect to a variable is compiled
9446 arg += 2;
9447 if (*arg == '>')
9448 {
9449 ++arg;
9450 append = TRUE;
9451 }
9452 arg = skipwhite(arg);
9453
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009454 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009455 FALSE, FALSE, 1, cctx) == FAIL)
9456 return NULL;
9457 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009458 lhs->lhs_append = append;
9459 if (lhs->lhs_has_index)
9460 {
9461 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9462 if (lhs->lhs_whole == NULL)
9463 return NULL;
9464 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009465
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009466 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009467 }
9468
9469 // other redirects are handled like at script level
9470 return compile_exec(line, eap, cctx);
9471}
9472
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009473#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009474 static char_u *
9475compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9476{
9477 isn_T *isn;
9478 char_u *p;
9479
9480 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9481 if (isn == NULL)
9482 return NULL;
9483 isn->isn_arg.number = eap->cmdidx;
9484
9485 p = eap->arg;
9486 if (compile_expr0(&p, cctx) == FAIL)
9487 return NULL;
9488
9489 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9490 if (isn == NULL)
9491 return NULL;
9492 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9493 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9494 return NULL;
9495 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9496 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9497 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9498
9499 return p;
9500}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009501#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009502
Bram Moolenaar4c137212021-04-19 16:48:48 +02009503/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009504 * Check if the separator for a :global or :substitute command is OK.
9505 */
9506 int
9507check_global_and_subst(char_u *cmd, char_u *arg)
9508{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009509 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009510 {
9511 semsg(_(e_separator_not_supported_str), arg);
9512 return FAIL;
9513 }
9514 if (VIM_ISWHITE(cmd[1]))
9515 {
9516 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9517 return FAIL;
9518 }
9519 return OK;
9520}
9521
9522
9523/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009524 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009525 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009526 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009527 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009528add_def_function(ufunc_T *ufunc)
9529{
9530 dfunc_T *dfunc;
9531
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009532 if (def_functions.ga_len == 0)
9533 {
9534 // The first position is not used, so that a zero uf_dfunc_idx means it
9535 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009536 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009537 return FAIL;
9538 ++def_functions.ga_len;
9539 }
9540
Bram Moolenaar09689a02020-05-09 22:50:08 +02009541 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009542 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009543 return FAIL;
9544 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9545 CLEAR_POINTER(dfunc);
9546 dfunc->df_idx = def_functions.ga_len;
9547 ufunc->uf_dfunc_idx = dfunc->df_idx;
9548 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009549 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009550 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009551 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009552 ++def_functions.ga_len;
9553 return OK;
9554}
9555
9556/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009557 * After ex_function() has collected all the function lines: parse and compile
9558 * the lines into instructions.
9559 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009560 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9561 * the return statement (used for lambda). When uf_ret_type is already set
9562 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009563 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009564 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009565 * This can be used recursively through compile_lambda(), which may reallocate
9566 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009567 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009569 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009570compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009571 ufunc_T *ufunc,
9572 int check_return_type,
9573 compiletype_T compile_type,
9574 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009575{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009576 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009577 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009578 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009579 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009580 cctx_T cctx;
9581 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009582 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009583 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009584 int ret = FAIL;
9585 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009586 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009587 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009588 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009589 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009590#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009591 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009592#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009593 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009594
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009595 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009596 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009597 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009598 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009599 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9600 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009601 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009602
9603 switch (compile_type)
9604 {
9605 case CT_PROFILE:
9606#ifdef FEAT_PROFILE
9607 instr_dest = dfunc->df_instr_prof; break;
9608#endif
9609 case CT_NONE: instr_dest = dfunc->df_instr; break;
9610 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9611 }
9612 if (instr_dest != NULL)
9613 // Was compiled in this mode before: Free old instructions.
9614 delete_def_function_contents(dfunc, FALSE);
9615 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009616 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009617 else
9618 {
9619 if (add_def_function(ufunc) == FAIL)
9620 return FAIL;
9621 new_def_function = TRUE;
9622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009623
Bram Moolenaar985116a2020-07-12 17:31:09 +02009624 ufunc->uf_def_status = UF_COMPILING;
9625
Bram Moolenaara80faa82020-04-12 19:37:17 +02009626 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009627
Bram Moolenaare99d4222021-06-13 14:01:26 +02009628 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009629 cctx.ctx_ufunc = ufunc;
9630 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009631 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9633 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9634 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9635 cctx.ctx_type_list = &ufunc->uf_type_list;
9636 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9637 instr = &cctx.ctx_instr;
9638
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009639 // Set the context to the function, it may be compiled when called from
9640 // another script. Set the script version to the most modern one.
9641 // The line number will be set in next_line_from_context().
9642 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9644
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009645 // Don't use the flag from ":legacy" here.
9646 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9647
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009648 // Make sure error messages are OK.
9649 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9650 if (do_estack_push)
9651 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009652 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009653
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009654 if (ufunc->uf_def_args.ga_len > 0)
9655 {
9656 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009657 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009658 int i;
9659 char_u *arg;
9660 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009661 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009662
9663 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009664 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009665 for (i = 0; i < count; ++i)
9666 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009667 garray_T *stack = &cctx.ctx_type_stack;
9668 type_T *val_type;
9669 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009670 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009671 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009672 int jump_instr_idx = instr->ga_len;
9673 isn_T *isn;
9674
9675 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9676 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9677 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009678
9679 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009680 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009681
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009682 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009683 r = compile_expr0(&arg, &cctx);
9684
Bram Moolenaar12bce952021-03-11 20:04:04 +01009685 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009686 goto erret;
9687
9688 // If no type specified use the type of the default value.
9689 // Otherwise check that the default value type matches the
9690 // specified type.
9691 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009692 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009693 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009694 {
9695 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009696 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009697 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009698 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009699 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009700 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009701
9702 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009703 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009704
9705 // set instruction index in JUMP_IF_ARG_SET to here
9706 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9707 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009708 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009709
9710 if (did_set_arg_type)
9711 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009712 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009713 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009714
9715 /*
9716 * Loop over all the lines of the function and generate instructions.
9717 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 for (;;)
9719 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009720 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009721 int starts_with_colon = FALSE;
9722 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009723 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009724
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009725 // Bail out on the first error to avoid a flood of errors and report
9726 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009727 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009728 goto erret;
9729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730 if (line != NULL && *line == '|')
9731 // the line continues after a '|'
9732 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009733 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009734 && !(*line == '#' && (line == cctx.ctx_line_start
9735 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009736 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009737 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009738 goto erret;
9739 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009740 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9741 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009742 else
9743 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009744 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009745 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009746 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009747 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009748#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009749 if (cctx.ctx_skip != SKIP_YES)
9750 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009751#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009752 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009753 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009754 // Make a copy, splitting off nextcmd and removing trailing spaces
9755 // may change it.
9756 if (line != NULL)
9757 {
9758 line = vim_strsave(line);
9759 vim_free(line_to_free);
9760 line_to_free = line;
9761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 }
9763
Bram Moolenaara80faa82020-04-12 19:37:17 +02009764 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009765 ea.cmdlinep = &line;
9766 ea.cmd = skipwhite(line);
9767
Bram Moolenaarb2049902021-01-24 12:53:53 +01009768 if (*ea.cmd == '#')
9769 {
9770 // "#" starts a comment
9771 line = (char_u *)"";
9772 continue;
9773 }
9774
Bram Moolenaarf002a412021-01-24 13:34:18 +01009775#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009776 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9777 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009778 {
9779 may_generate_prof_end(&cctx, prof_lnum);
9780
9781 prof_lnum = cctx.ctx_lnum;
9782 generate_instr(&cctx, ISN_PROF_START);
9783 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009784#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009785 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9786 && cctx.ctx_skip != SKIP_YES)
9787 {
9788 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009789 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009790 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009791 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009792
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009793 // Some things can be recognized by the first character.
9794 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009796 case '}':
9797 {
9798 // "}" ends a block scope
9799 scopetype_T stype = cctx.ctx_scope == NULL
9800 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009801
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009802 if (stype == BLOCK_SCOPE)
9803 {
9804 compile_endblock(&cctx);
9805 line = ea.cmd;
9806 }
9807 else
9808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009809 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009810 goto erret;
9811 }
9812 if (line != NULL)
9813 line = skipwhite(ea.cmd + 1);
9814 continue;
9815 }
9816
9817 case '{':
9818 // "{" starts a block scope
9819 // "{'a': 1}->func() is something else
9820 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9821 {
9822 line = compile_block(ea.cmd, &cctx);
9823 continue;
9824 }
9825 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009826 }
9827
9828 /*
9829 * COMMAND MODIFIERS
9830 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009831 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009832 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9833 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834 {
9835 if (errormsg != NULL)
9836 goto erret;
9837 // empty line or comment
9838 line = (char_u *)"";
9839 continue;
9840 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009841 generate_cmdmods(&cctx, &local_cmdmod);
9842 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009843
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009844 // Check if there was a colon after the last command modifier or before
9845 // the current position.
9846 for (p = ea.cmd; p >= line; --p)
9847 {
9848 if (*p == ':')
9849 starts_with_colon = TRUE;
9850 if (p < ea.cmd && !VIM_ISWHITE(*p))
9851 break;
9852 }
9853
Bram Moolenaarce024c32021-06-26 13:00:49 +02009854 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009855 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009856 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009857 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009858 if (checkforcmd(&ea.cmd, "call", 3))
9859 {
9860 if (*ea.cmd == '(')
9861 // not for "call()"
9862 ea.cmd = p;
9863 else
9864 ea.cmd = skipwhite(ea.cmd);
9865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009866
Bram Moolenaarce024c32021-06-26 13:00:49 +02009867 if (!starts_with_colon)
9868 {
9869 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009870
Bram Moolenaarce024c32021-06-26 13:00:49 +02009871 // Check for assignment after command modifiers.
9872 assign = may_compile_assignment(&ea, &line, &cctx);
9873 if (assign == OK)
9874 goto nextline;
9875 if (assign == FAIL)
9876 goto erret;
9877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009878 }
9879
9880 /*
9881 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009882 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009883 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009884 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009886 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009887 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009888 && (*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009889 && (starts_with_colon || !(*cmd == '\''
9890 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009891 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009892 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009893 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009894 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009895 if (!starts_with_colon)
9896 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009897 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009898 goto erret;
9899 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009900 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009901 if (ends_excmd2(line, ea.cmd))
9902 {
9903 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009904 line = skipwhite(line);
9905 while (*line == ':')
9906 ++line;
9907 generate_EXEC(&cctx, ISN_EXECRANGE,
9908 vim_strnsave(line, ea.cmd - line));
9909 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009910 goto nextline;
9911 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009912 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009913 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009914 p = find_ex_command(&ea, NULL,
9915 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009916 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009917
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009918 if (p == NULL)
9919 {
9920 if (cctx.ctx_skip != SKIP_YES)
9921 emsg(_(e_ambiguous_use_of_user_defined_command));
9922 goto erret;
9923 }
9924
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009925 // When using ":legacy cmd" always use compile_exec().
9926 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009927 {
9928 char_u *start = ea.cmd;
9929
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009930 switch (ea.cmdidx)
9931 {
9932 case CMD_if:
9933 case CMD_elseif:
9934 case CMD_else:
9935 case CMD_endif:
9936 case CMD_for:
9937 case CMD_endfor:
9938 case CMD_continue:
9939 case CMD_break:
9940 case CMD_while:
9941 case CMD_endwhile:
9942 case CMD_try:
9943 case CMD_catch:
9944 case CMD_finally:
9945 case CMD_endtry:
9946 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9947 goto erret;
9948 default: break;
9949 }
9950
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009951 // ":legacy return expr" needs to be handled differently.
9952 if (checkforcmd(&start, "return", 4))
9953 ea.cmdidx = CMD_return;
9954 else
9955 ea.cmdidx = CMD_legacy;
9956 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009958 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9959 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009960 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009961 {
9962 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009963 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009964 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009965 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009966 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009967 // CMD_var cannot happen, compile_assignment() above would be
9968 // used. Most likely an assignment to a non-existing variable.
9969 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009970 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 }
9973
Bram Moolenaar3988f642020-08-27 22:43:03 +02009974 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009975 && ea.cmdidx != CMD_elseif
9976 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009977 && ea.cmdidx != CMD_endif
9978 && ea.cmdidx != CMD_endfor
9979 && ea.cmdidx != CMD_endwhile
9980 && ea.cmdidx != CMD_catch
9981 && ea.cmdidx != CMD_finally
9982 && ea.cmdidx != CMD_endtry)
9983 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009984 emsg(_(e_unreachable_code_after_return));
9985 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009986 }
9987
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009988 p = skipwhite(p);
9989 if (ea.cmdidx != CMD_SIZE
9990 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9991 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009992 if (ea.cmdidx >= 0)
9993 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009994 if ((ea.argt & EX_BANG) && *p == '!')
9995 {
9996 ea.forceit = TRUE;
9997 p = skipwhite(p + 1);
9998 }
9999 }
10000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010001 switch (ea.cmdidx)
10002 {
10003 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010004 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010005 ea.arg = p;
10006 line = compile_nested_function(&ea, &cctx);
10007 break;
10008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010009 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010010 line = compile_return(p, check_return_type,
10011 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010012 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010013 break;
10014
10015 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010016 emsg(_(e_cannot_use_let_in_vim9_script));
10017 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010018 case CMD_var:
10019 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010020 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010021 case CMD_increment:
10022 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010023 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010024 if (line == p)
10025 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010026 break;
10027
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010028 case CMD_unlet:
10029 case CMD_unlockvar:
10030 case CMD_lockvar:
10031 line = compile_unletlock(p, &ea, &cctx);
10032 break;
10033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010034 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010035 emsg(_(e_import_can_only_be_used_in_script));
10036 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010037 break;
10038
10039 case CMD_if:
10040 line = compile_if(p, &cctx);
10041 break;
10042 case CMD_elseif:
10043 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010044 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010045 break;
10046 case CMD_else:
10047 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010048 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010049 break;
10050 case CMD_endif:
10051 line = compile_endif(p, &cctx);
10052 break;
10053
10054 case CMD_while:
10055 line = compile_while(p, &cctx);
10056 break;
10057 case CMD_endwhile:
10058 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010059 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010060 break;
10061
10062 case CMD_for:
10063 line = compile_for(p, &cctx);
10064 break;
10065 case CMD_endfor:
10066 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010067 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010068 break;
10069 case CMD_continue:
10070 line = compile_continue(p, &cctx);
10071 break;
10072 case CMD_break:
10073 line = compile_break(p, &cctx);
10074 break;
10075
10076 case CMD_try:
10077 line = compile_try(p, &cctx);
10078 break;
10079 case CMD_catch:
10080 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010081 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010082 break;
10083 case CMD_finally:
10084 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010085 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010086 break;
10087 case CMD_endtry:
10088 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010089 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010090 break;
10091 case CMD_throw:
10092 line = compile_throw(p, &cctx);
10093 break;
10094
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010095 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010096 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010097 break;
10098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010101 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010102 case CMD_echomsg:
10103 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010104 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010105 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010106 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010107
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010108 case CMD_put:
10109 ea.cmd = cmd;
10110 line = compile_put(p, &ea, &cctx);
10111 break;
10112
Bram Moolenaar4c137212021-04-19 16:48:48 +020010113 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010114 if (check_global_and_subst(ea.cmd, p) == FAIL)
10115 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010116 if (cctx.ctx_skip == SKIP_YES)
10117 line = (char_u *)"";
10118 else
10119 {
10120 ea.arg = p;
10121 line = compile_substitute(line, &ea, &cctx);
10122 }
10123 break;
10124
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010125 case CMD_redir:
10126 ea.arg = p;
10127 line = compile_redir(line, &ea, &cctx);
10128 break;
10129
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010130 case CMD_cexpr:
10131 case CMD_lexpr:
10132 case CMD_caddexpr:
10133 case CMD_laddexpr:
10134 case CMD_cgetexpr:
10135 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010136#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010137 ea.arg = p;
10138 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010139#else
10140 ex_ni(&ea);
10141 line = NULL;
10142#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010143 break;
10144
Bram Moolenaarae616492020-07-28 20:07:27 +020010145 case CMD_append:
10146 case CMD_change:
10147 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010148 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010149 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010150 case CMD_xit:
10151 not_in_vim9(&ea);
10152 goto erret;
10153
Bram Moolenaar002262f2020-07-08 17:47:57 +020010154 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010155 if (cctx.ctx_skip != SKIP_YES)
10156 {
10157 semsg(_(e_invalid_command_str), ea.cmd);
10158 goto erret;
10159 }
10160 // We don't check for a next command here.
10161 line = (char_u *)"";
10162 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010163
Bram Moolenaar20677332021-06-06 17:02:53 +020010164 case CMD_lua:
10165 case CMD_mzscheme:
10166 case CMD_perl:
10167 case CMD_py3:
10168 case CMD_python3:
10169 case CMD_python:
10170 case CMD_pythonx:
10171 case CMD_ruby:
10172 case CMD_tcl:
10173 ea.arg = p;
10174 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010175 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010176 else
10177 // heredoc lines have been concatenated with NL
10178 // characters in get_function_body()
10179 line = compile_script(line, &cctx);
10180 break;
10181
Bram Moolenaar7b829262021-10-13 15:04:34 +010010182 case CMD_global:
10183 if (check_global_and_subst(ea.cmd, p) == FAIL)
10184 goto erret;
10185 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010186 default:
10187 // Not recognized, execute with do_cmdline_cmd().
10188 ea.arg = p;
10189 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010190 break;
10191 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010192nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010193 if (line == NULL)
10194 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010195 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010197 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010198 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010200 if (cctx.ctx_type_stack.ga_len < 0)
10201 {
10202 iemsg("Type stack underflow");
10203 goto erret;
10204 }
10205 }
10206
10207 if (cctx.ctx_scope != NULL)
10208 {
10209 if (cctx.ctx_scope->se_type == IF_SCOPE)
10210 emsg(_(e_endif));
10211 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10212 emsg(_(e_endwhile));
10213 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10214 emsg(_(e_endfor));
10215 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010216 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010217 goto erret;
10218 }
10219
Bram Moolenaarefd88552020-06-18 20:50:10 +020010220 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010221 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010222 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10223 ufunc->uf_ret_type = &t_void;
10224 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010226 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010227 goto erret;
10228 }
10229
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010230 // Return void if there is no return at the end.
10231 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232 }
10233
Bram Moolenaar599410c2021-04-10 14:03:43 +020010234 // When compiled with ":silent!" and there was an error don't consider the
10235 // function compiled.
10236 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010237 {
10238 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10239 + ufunc->uf_dfunc_idx;
10240 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010241 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010242#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010243 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010244 {
10245 dfunc->df_instr_prof = instr->ga_data;
10246 dfunc->df_instr_prof_count = instr->ga_len;
10247 }
10248 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010249#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010250 if (cctx.ctx_compile_type == CT_DEBUG)
10251 {
10252 dfunc->df_instr_debug = instr->ga_data;
10253 dfunc->df_instr_debug_count = instr->ga_len;
10254 }
10255 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010256 {
10257 dfunc->df_instr = instr->ga_data;
10258 dfunc->df_instr_count = instr->ga_len;
10259 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010260 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010261 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010262 if (cctx.ctx_outer_used)
10263 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010264 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010266
10267 ret = OK;
10268
10269erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010270 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010271 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010272 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10273 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010274
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010275 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010276 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010277 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010278 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010279
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010280 // If using the last entry in the table and it was added above, we
10281 // might as well remove it.
10282 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010283 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010284 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010285 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010286 ufunc->uf_dfunc_idx = 0;
10287 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010288 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010289
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010290 while (cctx.ctx_scope != NULL)
10291 drop_scope(&cctx);
10292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010293 if (errormsg != NULL)
10294 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010295 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010296 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010297 }
10298
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010299 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10300 {
10301 if (ret == OK)
10302 {
10303 emsg(_(e_missing_redir_end));
10304 ret = FAIL;
10305 }
10306 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010307 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010308 }
10309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010310 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010311 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010312 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010313 if (do_estack_push)
10314 estack_pop();
10315
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010316 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010317 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010318 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010319 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010320 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010321}
10322
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010323 void
10324set_function_type(ufunc_T *ufunc)
10325{
10326 int varargs = ufunc->uf_va_name != NULL;
10327 int argcount = ufunc->uf_args.ga_len;
10328
10329 // Create a type for the function, with the return type and any
10330 // argument types.
10331 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10332 // The type is included in "tt_args".
10333 if (argcount > 0 || varargs)
10334 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010335 if (ufunc->uf_type_list.ga_itemsize == 0)
10336 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010337 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10338 argcount, &ufunc->uf_type_list);
10339 // Add argument types to the function type.
10340 if (func_type_add_arg_types(ufunc->uf_func_type,
10341 argcount + varargs,
10342 &ufunc->uf_type_list) == FAIL)
10343 return;
10344 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10345 ufunc->uf_func_type->tt_min_argcount =
10346 argcount - ufunc->uf_def_args.ga_len;
10347 if (ufunc->uf_arg_types == NULL)
10348 {
10349 int i;
10350
10351 // lambda does not have argument types.
10352 for (i = 0; i < argcount; ++i)
10353 ufunc->uf_func_type->tt_args[i] = &t_any;
10354 }
10355 else
10356 mch_memmove(ufunc->uf_func_type->tt_args,
10357 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10358 if (varargs)
10359 {
10360 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010361 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010362 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10363 }
10364 }
10365 else
10366 // No arguments, can use a predefined type.
10367 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10368 argcount, &ufunc->uf_type_list);
10369}
10370
10371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010372/*
10373 * Delete an instruction, free what it contains.
10374 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010375 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010376delete_instr(isn_T *isn)
10377{
10378 switch (isn->isn_type)
10379 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010380 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010381 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010382 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010383 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010384 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010385 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010386 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010387 case ISN_LOADENV:
10388 case ISN_LOADG:
10389 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010390 case ISN_LOADT:
10391 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010392 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010393 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010394 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010395 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010396 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010397 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010398 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010399 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010400 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010401 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010402 case ISN_STOREW:
10403 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010404 vim_free(isn->isn_arg.string);
10405 break;
10406
Bram Moolenaar4c137212021-04-19 16:48:48 +020010407 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010408 {
10409 int idx;
10410 isn_T *list = isn->isn_arg.subs.subs_instr;
10411
10412 vim_free(isn->isn_arg.subs.subs_cmd);
10413 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10414 delete_instr(list + idx);
10415 vim_free(list);
10416 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010417 break;
10418
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010419 case ISN_INSTR:
10420 {
10421 int idx;
10422 isn_T *list = isn->isn_arg.instr;
10423
10424 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10425 delete_instr(list + idx);
10426 vim_free(list);
10427 }
10428 break;
10429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010430 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010431 case ISN_STORES:
10432 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010433 break;
10434
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010435 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010436 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010437 vim_free(isn->isn_arg.unlet.ul_name);
10438 break;
10439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010440 case ISN_STOREOPT:
10441 vim_free(isn->isn_arg.storeopt.so_name);
10442 break;
10443
10444 case ISN_PUSHBLOB: // push blob isn_arg.blob
10445 blob_unref(isn->isn_arg.blob);
10446 break;
10447
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010448 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010449#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010450 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010451#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010452 break;
10453
10454 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010456 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010457#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010458 break;
10459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010460 case ISN_UCALL:
10461 vim_free(isn->isn_arg.ufunc.cuf_name);
10462 break;
10463
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010464 case ISN_FUNCREF:
10465 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010466 if (isn->isn_arg.funcref.fr_func_name == NULL)
10467 {
10468 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10469 + isn->isn_arg.funcref.fr_dfunc_idx;
10470 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010471
Bram Moolenaar38453522021-11-28 22:00:12 +000010472 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10473 func_ptr_unref(ufunc);
10474 }
10475 else
10476 {
10477 char_u *name = isn->isn_arg.funcref.fr_func_name;
10478
10479 if (name != NULL)
10480 func_unref(name);
10481 vim_free(isn->isn_arg.funcref.fr_func_name);
10482 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010483 }
10484 break;
10485
10486 case ISN_DCALL:
10487 {
10488 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10489 + isn->isn_arg.dfunc.cdf_idx;
10490
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010491 if (dfunc->df_ufunc != NULL
10492 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010493 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010494 }
10495 break;
10496
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010497 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010498 {
10499 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10500 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10501
10502 if (ufunc != NULL)
10503 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010504 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010505 func_ptr_unref(ufunc);
10506 }
10507
10508 vim_free(lambda);
10509 vim_free(isn->isn_arg.newfunc.nf_global);
10510 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010511 break;
10512
Bram Moolenaar5e654232020-09-16 15:22:00 +020010513 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010514 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010515 free_type(isn->isn_arg.type.ct_type);
10516 break;
10517
Bram Moolenaar02194d22020-10-24 23:08:38 +020010518 case ISN_CMDMOD:
10519 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10520 ->cmod_filter_regmatch.regprog);
10521 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10522 break;
10523
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010524 case ISN_LOADSCRIPT:
10525 case ISN_STORESCRIPT:
10526 vim_free(isn->isn_arg.script.scriptref);
10527 break;
10528
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010529 case ISN_TRY:
10530 vim_free(isn->isn_arg.try.try_ref);
10531 break;
10532
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010533 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010534 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010535 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10536 break;
10537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010538 case ISN_2BOOL:
10539 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010540 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010541 case ISN_ADDBLOB:
10542 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010543 case ISN_ANYINDEX:
10544 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010545 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010546 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010547 case ISN_BLOBINDEX:
10548 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010549 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010550 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010551 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010552 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010553 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010554 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010555 case ISN_COMPAREANY:
10556 case ISN_COMPAREBLOB:
10557 case ISN_COMPAREBOOL:
10558 case ISN_COMPAREDICT:
10559 case ISN_COMPAREFLOAT:
10560 case ISN_COMPAREFUNC:
10561 case ISN_COMPARELIST:
10562 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010563 case ISN_COMPARESPECIAL:
10564 case ISN_COMPARESTRING:
10565 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010566 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010567 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010568 case ISN_DROP:
10569 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010570 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010571 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010572 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010573 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010574 case ISN_EXECCONCAT:
10575 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010576 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010577 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010578 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010579 case ISN_GETITEM:
10580 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010581 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010582 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010583 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010584 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010585 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010586 case ISN_LOADBDICT:
10587 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010588 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010589 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010590 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010591 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010592 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010593 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010594 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010595 case ISN_NEGATENR:
10596 case ISN_NEWDICT:
10597 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010598 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010599 case ISN_OPFLOAT:
10600 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010601 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010602 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010603 case ISN_PROF_END:
10604 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010605 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010606 case ISN_PUSHF:
10607 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010608 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010609 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010610 case ISN_REDIREND:
10611 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010612 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010613 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010614 case ISN_SHUFFLE:
10615 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010616 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010617 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010618 case ISN_STORENR:
10619 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010620 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010621 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010622 case ISN_STOREV:
10623 case ISN_STRINDEX:
10624 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010625 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010626 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010627 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010628 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010629 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010630 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010631 // nothing allocated
10632 break;
10633 }
10634}
10635
10636/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010637 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010638 */
10639 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010640delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010641{
10642 int idx;
10643
10644 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010645 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010646
10647 if (dfunc->df_instr != NULL)
10648 {
10649 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10650 delete_instr(dfunc->df_instr + idx);
10651 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010652 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010653 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010654 if (dfunc->df_instr_debug != NULL)
10655 {
10656 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10657 delete_instr(dfunc->df_instr_debug + idx);
10658 VIM_CLEAR(dfunc->df_instr_debug);
10659 dfunc->df_instr_debug = NULL;
10660 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010661#ifdef FEAT_PROFILE
10662 if (dfunc->df_instr_prof != NULL)
10663 {
10664 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10665 delete_instr(dfunc->df_instr_prof + idx);
10666 VIM_CLEAR(dfunc->df_instr_prof);
10667 dfunc->df_instr_prof = NULL;
10668 }
10669#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010670
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010671 if (mark_deleted)
10672 dfunc->df_deleted = TRUE;
10673 if (dfunc->df_ufunc != NULL)
10674 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010675}
10676
10677/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010678 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010679 * function, unless another user function still uses it.
10680 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010681 */
10682 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010683unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010684{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010685 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010686 {
10687 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10688 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010689
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010690 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010691 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010692 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010693 ufunc->uf_dfunc_idx = 0;
10694 if (dfunc->df_ufunc == ufunc)
10695 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010696 }
10697}
10698
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010699/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010700 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010701 */
10702 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010703link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010704{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010705 if (ufunc->uf_dfunc_idx > 0)
10706 {
10707 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10708 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010709
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010710 ++dfunc->df_refcount;
10711 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010712}
10713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010714#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010715/*
10716 * Free all functions defined with ":def".
10717 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010718 void
10719free_def_functions(void)
10720{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010721 int idx;
10722
10723 for (idx = 0; idx < def_functions.ga_len; ++idx)
10724 {
10725 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10726
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010727 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010728 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010729 }
10730
10731 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010732}
10733#endif
10734
10735
10736#endif // FEAT_EVAL