blob: 0d64f0134d279403bc586f8ea586d31cc6885b16 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200358 if (sav->sav_block_id == 0)
359 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 return sav;
361
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200362 if (cctx == NULL)
363 {
364 // Not in a function scope, find variable with block id equal to or
365 // smaller than the current block id.
366 while (sav != NULL)
367 {
368 if (sav->sav_block_id <= si->sn_current_block_id)
369 break;
370 sav = sav->sav_next;
371 }
372 return sav;
373 }
374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 // Go over the variables with this name and find one that was visible
376 // from the function.
377 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 while (sav != NULL)
380 {
381 int idx;
382
383 // Go over the blocks that this function was defined in. If the
384 // variable block ID matches it was visible to the function.
385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
387 return sav;
388 sav = sav->sav_next;
389 }
390
Bram Moolenaar88421d62021-07-24 14:14:52 +0200391 // Not found, assume variable at script level was visible.
392 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393}
394
395/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100396 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200397 */
398 static int
399script_is_vim9()
400{
401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
402}
403
404/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200405 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 * Returns OK or FAIL.
408 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200409 static int
410script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200412 if (current_sctx.sc_sid <= 0)
413 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200414 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200415 {
416 // Check script variables that were visible where the function was
417 // defined.
418 if (find_script_var(name, len, cctx) != NULL)
419 return OK;
420 }
421 else
422 {
423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
424 dictitem_T *di;
425 int cc;
426
427 // Check script variables that are currently visible
428 cc = name[len];
429 name[len] = NUL;
430 di = find_var_in_ht(ht, 0, name, TRUE);
431 name[len] = cc;
432 if (di != NULL)
433 return OK;
434 }
435
436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437}
438
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100439/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100440 * Return TRUE if "name" is a local variable, argument, script variable or
441 * imported.
442 */
443 static int
444variable_exists(char_u *name, size_t len, cctx_T *cctx)
445{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100446 return (cctx != NULL
447 && (lookup_local(name, len, NULL, cctx) == OK
448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200449 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100450 || find_imported(name, len, cctx) != NULL;
451}
452
453/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100454 * Return TRUE if "name" is a local variable, argument, script variable,
455 * imported or function.
456 */
457 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100459{
460 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100461 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462
463 if (variable_exists(name, len, cctx))
464 return TRUE;
465
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100466 // This is similar to what is in lookup_scriptitem():
467 // Find a function, so that a following "->" works.
468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
469 // a function call.
470 p = skipwhite(name + len);
471
472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
473 {
474 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200475 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100476 // Skip "g:" before a function name.
477 is_global = (name[0] == 'g' && name[1] == ':');
478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
479 }
480 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100481}
482
483/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100484 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200485 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200486 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100487 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100488 * Return FAIL and give an error if it defined.
489 */
490 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100492{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200493 int c = p[len];
494 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495
Bram Moolenaarda479c72021-04-10 21:01:38 +0200496 // underscore argument is OK
497 if (len == 1 && *p == '_')
498 return OK;
499
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200500 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 {
502 if (is_arg)
503 semsg(_(e_argument_already_declared_in_script_str), p);
504 else
505 semsg(_(e_variable_already_declared_in_script_str), p);
506 return FAIL;
507 }
508
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100510 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100511 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
518 && (!func_is_global(ufunc)
519 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200520 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100521 if (is_arg)
522 semsg(_(e_argument_name_shadows_existing_variable_str), p);
523 else
524 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200525 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 return FAIL;
527 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100528 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200529 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 return OK;
531}
532
Bram Moolenaar65b95452020-07-19 14:03:09 +0200533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534/////////////////////////////////////////////////////////////////////
535// Following generate_ functions expect the caller to call ga_grow().
536
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200537#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
538#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540/*
541 * Generate an instruction without arguments.
542 * Returns a pointer to the new instruction, NULL if failed.
543 */
544 static isn_T *
545generate_instr(cctx_T *cctx, isntype_T isn_type)
546{
547 garray_T *instr = &cctx->ctx_instr;
548 isn_T *isn;
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200551 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return NULL;
553 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
554 isn->isn_type = isn_type;
555 isn->isn_lnum = cctx->ctx_lnum + 1;
556 ++instr->ga_len;
557
558 return isn;
559}
560
561/*
562 * Generate an instruction without arguments.
563 * "drop" will be removed from the stack.
564 * Returns a pointer to the new instruction, NULL if failed.
565 */
566 static isn_T *
567generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
568{
569 garray_T *stack = &cctx->ctx_type_stack;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 stack->ga_len -= drop;
573 return generate_instr(cctx, isn_type);
574}
575
576/*
577 * Generate instruction "isn_type" and put "type" on the type stack.
578 */
579 static isn_T *
580generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 if ((isn = generate_instr(cctx, isn_type)) == NULL)
586 return NULL;
587
Bram Moolenaar35578162021-08-02 19:10:38 +0200588 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 ++stack->ga_len;
592
593 return isn;
594}
595
596/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 * Generate an ISN_DEBUG instruction.
598 */
599 static isn_T *
600generate_instr_debug(cctx_T *cctx)
601{
602 isn_T *isn;
603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
604 + cctx->ctx_ufunc->uf_dfunc_idx;
605
606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
607 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200610 return isn;
611}
612
613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200616 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 */
618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200619may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620{
621 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200622 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100624 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 RETURN_OK_IF_SKIP(cctx);
627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 switch ((*type)->tt_type)
629 {
630 // nothing to be done
631 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200633 // conversion possible
634 case VAR_SPECIAL:
635 case VAR_BOOL:
636 case VAR_NUMBER:
637 case VAR_FLOAT:
638 break;
639
640 // conversion possible (with runtime check)
641 case VAR_ANY:
642 case VAR_UNKNOWN:
643 isntype = ISN_2STRING_ANY;
644 break;
645
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200646 // conversion possible when tolerant
647 case VAR_LIST:
648 if (tolerant)
649 {
650 isntype = ISN_2STRING_ANY;
651 break;
652 }
653 // FALLTHROUGH
654
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200655 // conversion not possible
656 case VAR_VOID:
657 case VAR_BLOB:
658 case VAR_FUNC:
659 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200660 case VAR_DICT:
661 case VAR_JOB:
662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200663 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200664 to_string_error((*type)->tt_type);
665 return FAIL;
666 }
667
668 *type = &t_string;
669 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200671 isn->isn_arg.tostring.offset = offset;
672 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 return OK;
675}
676
677 static int
678check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
679{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 {
684 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200685 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 return FAIL;
689 }
690 return OK;
691}
692
Bram Moolenaar07802042021-09-09 23:01:14 +0200693/*
694 * Generate instruction for "+". For a list this creates a new list.
695 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200696 static int
697generate_add_instr(
698 cctx_T *cctx,
699 vartype_T vartype,
700 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200701 type_T *type2,
702 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200703{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100704 garray_T *stack = &cctx->ctx_type_stack;
705 isn_T *isn = generate_instr_drop(cctx,
706 vartype == VAR_NUMBER ? ISN_OPNR
707 : vartype == VAR_LIST ? ISN_ADDLIST
708 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100710 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100712 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200713
714 if (vartype != VAR_LIST && vartype != VAR_BLOB
715 && type1->tt_type != VAR_ANY
716 && type2->tt_type != VAR_ANY
717 && check_number_or_float(
718 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
719 return FAIL;
720
721 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200722 {
723 if (isn->isn_type == ISN_ADDLIST)
724 isn->isn_arg.op.op_type = expr_type;
725 else
726 isn->isn_arg.op.op_type = EXPR_ADD;
727 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100728
729 // When concatenating two lists with different member types the member type
730 // becomes "any".
731 if (vartype == VAR_LIST
732 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
733 && type1->tt_member != type2->tt_member)
734 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
735
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200736 return isn == NULL ? FAIL : OK;
737}
738
739/*
740 * Get the type to use for an instruction for an operation on "type1" and
741 * "type2". If they are matching use a type-specific instruction. Otherwise
742 * fall back to runtime type checking.
743 */
744 static vartype_T
745operator_type(type_T *type1, type_T *type2)
746{
747 if (type1->tt_type == type2->tt_type
748 && (type1->tt_type == VAR_NUMBER
749 || type1->tt_type == VAR_LIST
750#ifdef FEAT_FLOAT
751 || type1->tt_type == VAR_FLOAT
752#endif
753 || type1->tt_type == VAR_BLOB))
754 return type1->tt_type;
755 return VAR_ANY;
756}
757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758/*
759 * Generate an instruction with two arguments. The instruction depends on the
760 * type of the arguments.
761 */
762 static int
763generate_two_op(cctx_T *cctx, char_u *op)
764{
765 garray_T *stack = &cctx->ctx_type_stack;
766 type_T *type1;
767 type_T *type2;
768 vartype_T vartype;
769 isn_T *isn;
770
Bram Moolenaar080457c2020-03-03 21:53:32 +0100771 RETURN_OK_IF_SKIP(cctx);
772
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200773 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
775 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200776 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
778 switch (*op)
779 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200780 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200781 if (generate_add_instr(cctx, vartype, type1, type2,
782 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 break;
785
786 case '-':
787 case '*':
788 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
789 op) == FAIL)
790 return FAIL;
791 if (vartype == VAR_NUMBER)
792 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
793#ifdef FEAT_FLOAT
794 else if (vartype == VAR_FLOAT)
795 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
796#endif
797 else
798 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
799 if (isn != NULL)
800 isn->isn_arg.op.op_type = *op == '*'
801 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
802 break;
803
Bram Moolenaar4c683752020-04-05 21:38:23 +0200804 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200806 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 && type2->tt_type != VAR_NUMBER))
808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200809 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 return FAIL;
811 }
812 isn = generate_instr_drop(cctx,
813 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
814 if (isn != NULL)
815 isn->isn_arg.op.op_type = EXPR_REM;
816 break;
817 }
818
819 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200820 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 {
822 type_T *type = &t_any;
823
824#ifdef FEAT_FLOAT
825 // float+number and number+float results in float
826 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
827 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
828 type = &t_float;
829#endif
830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
831 }
832
833 return OK;
834}
835
836/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200837 * Get the instruction to use for comparing "type1" with "type2"
838 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200840 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100841get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842{
843 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844
Bram Moolenaar4c683752020-04-05 21:38:23 +0200845 if (type1 == VAR_UNKNOWN)
846 type1 = VAR_ANY;
847 if (type2 == VAR_UNKNOWN)
848 type2 = VAR_ANY;
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 if (type1 == type2)
851 {
852 switch (type1)
853 {
854 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
855 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
856 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
857 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
858 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
859 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
860 case VAR_LIST: isntype = ISN_COMPARELIST; break;
861 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
862 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 default: isntype = ISN_COMPAREANY; break;
864 }
865 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200866 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100868 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 isntype = ISN_COMPAREANY;
870
Bram Moolenaar657137c2021-01-09 15:45:23 +0100871 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 && (isntype == ISN_COMPAREBOOL
873 || isntype == ISN_COMPARESPECIAL
874 || isntype == ISN_COMPARENR
875 || isntype == ISN_COMPAREFLOAT))
876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200877 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100878 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 }
881 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100882 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
884 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100885 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
886 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 && (type1 == VAR_BLOB || type2 == VAR_BLOB
888 || type1 == VAR_LIST || type2 == VAR_LIST))))
889 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200890 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200892 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200894 return isntype;
895}
896
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200897 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100898check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200899{
900 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
901 return FAIL;
902 return OK;
903}
904
Bram Moolenaara5565e42020-05-09 15:44:01 +0200905/*
906 * Generate an ISN_COMPARE* instruction with a boolean result.
907 */
908 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100909generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200910{
911 isntype_T isntype;
912 isn_T *isn;
913 garray_T *stack = &cctx->ctx_type_stack;
914 vartype_T type1;
915 vartype_T type2;
916
917 RETURN_OK_IF_SKIP(cctx);
918
919 // Get the known type of the two items on the stack. If they are matching
920 // use a type-specific instruction. Otherwise fall back to runtime type
921 // checking.
922 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
923 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100924 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200925 if (isntype == ISN_DROP)
926 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if ((isn = generate_instr(cctx, isntype)) == NULL)
929 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100930 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 isn->isn_arg.op.op_ic = ic;
932
933 // takes two arguments, puts one bool back
934 if (stack->ga_len >= 2)
935 {
936 --stack->ga_len;
937 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
938 }
939
940 return OK;
941}
942
943/*
944 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200945 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 */
947 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200948generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949{
950 isn_T *isn;
951 garray_T *stack = &cctx->ctx_type_stack;
952
Bram Moolenaar080457c2020-03-03 21:53:32 +0100953 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
955 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200956 isn->isn_arg.tobool.invert = invert;
957 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958
959 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200960 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961
962 return OK;
963}
964
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200965/*
966 * Generate an ISN_COND2BOOL instruction.
967 */
968 static int
969generate_COND2BOOL(cctx_T *cctx)
970{
971 isn_T *isn;
972 garray_T *stack = &cctx->ctx_type_stack;
973
974 RETURN_OK_IF_SKIP(cctx);
975 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
976 return FAIL;
977
978 // type becomes bool
979 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
980
981 return OK;
982}
983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200985generate_TYPECHECK(
986 cctx_T *cctx,
987 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100988 int offset,
989 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990{
991 isn_T *isn;
992 garray_T *stack = &cctx->ctx_type_stack;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
996 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200997 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100998 isn->isn_arg.type.ct_off = (int8_T)offset;
999 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000
Bram Moolenaar5e654232020-09-16 15:22:00 +02001001 // type becomes expected
1002 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
1004 return OK;
1005}
1006
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001007 static int
1008generate_SETTYPE(
1009 cctx_T *cctx,
1010 type_T *expected)
1011{
1012 isn_T *isn;
1013
1014 RETURN_OK_IF_SKIP(cctx);
1015 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1016 return FAIL;
1017 isn->isn_arg.type.ct_type = alloc_type(expected);
1018 return OK;
1019}
1020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001022 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1023 * used. Return FALSE if the types will never match.
1024 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001025 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026use_typecheck(type_T *actual, type_T *expected)
1027{
1028 if (actual->tt_type == VAR_ANY
1029 || actual->tt_type == VAR_UNKNOWN
1030 || (actual->tt_type == VAR_FUNC
1031 && (expected->tt_type == VAR_FUNC
1032 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001033 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1034 && ((actual->tt_member == &t_void)
1035 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001036 return TRUE;
1037 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1038 && actual->tt_type == expected->tt_type)
1039 // This takes care of a nested list or dict.
1040 return use_typecheck(actual->tt_member, expected->tt_member);
1041 return FALSE;
1042}
1043
1044/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001045 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001046 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001047 * - "actual" is a type that can be "expected" type: add a runtime check; or
1048 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001049 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1050 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001052 static int
1053need_type_where(
1054 type_T *actual,
1055 type_T *expected,
1056 int offset,
1057 where_T where,
1058 cctx_T *cctx,
1059 int silent,
1060 int actual_is_const)
1061{
1062 if (expected == &t_bool && actual != &t_bool
1063 && (actual->tt_flags & TTFLAG_BOOL_OK))
1064 {
1065 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1066 // boolean is OK but requires a conversion.
1067 generate_2BOOL(cctx, FALSE, offset);
1068 return OK;
1069 }
1070
1071 if (check_type(expected, actual, FALSE, where) == OK)
1072 return OK;
1073
1074 // If the actual type can be the expected type add a runtime check.
1075 // If it's a constant a runtime check makes no sense.
1076 if ((!actual_is_const || actual == &t_any)
1077 && use_typecheck(actual, expected))
1078 {
1079 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1080 return OK;
1081 }
1082
1083 if (!silent)
1084 type_mismatch_where(expected, actual, where);
1085 return FAIL;
1086}
1087
Bram Moolenaar351ead02021-01-16 16:07:01 +01001088 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001089need_type(
1090 type_T *actual,
1091 type_T *expected,
1092 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001093 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001094 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001095 int silent,
1096 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001097{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001098 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001099
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001100 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001101 return need_type_where(actual, expected, offset, where,
1102 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001103}
1104
1105/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001106 * Check that the top of the type stack has a type that can be used as a
1107 * condition. Give an error and return FAIL if not.
1108 */
1109 static int
1110bool_on_stack(cctx_T *cctx)
1111{
1112 garray_T *stack = &cctx->ctx_type_stack;
1113 type_T *type;
1114
1115 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1116 if (type == &t_bool)
1117 return OK;
1118
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001119 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001120 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1121 // This requires a runtime type check.
1122 return generate_COND2BOOL(cctx);
1123
Bram Moolenaar351ead02021-01-16 16:07:01 +01001124 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001125}
1126
1127/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 * Generate an ISN_PUSHNR instruction.
1129 */
1130 static int
1131generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1132{
1133 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001134 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.number = number;
1140
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001141 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001142 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001143 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_PUSHBOOL instruction.
1149 */
1150 static int
1151generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.number = number;
1159
1160 return OK;
1161}
1162
1163/*
1164 * Generate an ISN_PUSHSPEC instruction.
1165 */
1166 static int
1167generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1168{
1169 isn_T *isn;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1173 return FAIL;
1174 isn->isn_arg.number = number;
1175
1176 return OK;
1177}
1178
1179#ifdef FEAT_FLOAT
1180/*
1181 * Generate an ISN_PUSHF instruction.
1182 */
1183 static int
1184generate_PUSHF(cctx_T *cctx, float_T fnumber)
1185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.fnumber = fnumber;
1192
1193 return OK;
1194}
1195#endif
1196
1197/*
1198 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001199 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 */
1201 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001202generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203{
1204 isn_T *isn;
1205
Bram Moolenaar508b5612021-01-02 18:17:26 +01001206 if (cctx->ctx_skip == SKIP_YES)
1207 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001208 if (str != NULL)
1209 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001210 return OK;
1211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001213 {
1214 if (str != NULL)
1215 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001217 }
1218 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
1220 return OK;
1221}
1222
1223/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001224 * Generate an ISN_PUSHCHANNEL instruction.
1225 * Consumes "channel".
1226 */
1227 static int
1228generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1229{
1230 isn_T *isn;
1231
Bram Moolenaar080457c2020-03-03 21:53:32 +01001232 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001233 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.channel = channel;
1236
1237 return OK;
1238}
1239
1240/*
1241 * Generate an ISN_PUSHJOB instruction.
1242 * Consumes "job".
1243 */
1244 static int
1245generate_PUSHJOB(cctx_T *cctx, job_T *job)
1246{
1247 isn_T *isn;
1248
Bram Moolenaar080457c2020-03-03 21:53:32 +01001249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001250 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001251 return FAIL;
1252 isn->isn_arg.job = job;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_PUSHBLOB instruction.
1259 * Consumes "blob".
1260 */
1261 static int
1262generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1263{
1264 isn_T *isn;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.blob = blob;
1270
1271 return OK;
1272}
1273
1274/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001275 * Generate an ISN_PUSHFUNC instruction with name "name".
1276 * Consumes "name".
1277 */
1278 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001279generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001284 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001285 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001286 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001287
1288 return OK;
1289}
1290
1291/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001292 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001293 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1294 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001295 */
1296 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001297generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001301 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1302 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001303 type_T *item_type = &t_any;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001307 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001309 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001310 emsg(_(e_listreq));
1311 return FAIL;
1312 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001313 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001314 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1315 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001316 isn->isn_arg.getitem.gi_index = index;
1317 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001318
1319 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001320 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001321 return FAIL;
1322 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1323 ++stack->ga_len;
1324 return OK;
1325}
1326
1327/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001328 * Generate an ISN_SLICE instruction with "count".
1329 */
1330 static int
1331generate_SLICE(cctx_T *cctx, int count)
1332{
1333 isn_T *isn;
1334
1335 RETURN_OK_IF_SKIP(cctx);
1336 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.number = count;
1339 return OK;
1340}
1341
1342/*
1343 * Generate an ISN_CHECKLEN instruction with "min_len".
1344 */
1345 static int
1346generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1347{
1348 isn_T *isn;
1349
1350 RETURN_OK_IF_SKIP(cctx);
1351
1352 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1353 return FAIL;
1354 isn->isn_arg.checklen.cl_min_len = min_len;
1355 isn->isn_arg.checklen.cl_more_OK = more_OK;
1356
1357 return OK;
1358}
1359
1360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 * Generate an ISN_STORE instruction.
1362 */
1363 static int
1364generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1365{
1366 isn_T *isn;
1367
Bram Moolenaar080457c2020-03-03 21:53:32 +01001368 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1370 return FAIL;
1371 if (name != NULL)
1372 isn->isn_arg.string = vim_strsave(name);
1373 else
1374 isn->isn_arg.number = idx;
1375
1376 return OK;
1377}
1378
1379/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001380 * Generate an ISN_STOREOUTER instruction.
1381 */
1382 static int
1383generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1384{
1385 isn_T *isn;
1386
1387 RETURN_OK_IF_SKIP(cctx);
1388 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1389 return FAIL;
1390 isn->isn_arg.outer.outer_idx = idx;
1391 isn->isn_arg.outer.outer_depth = level;
1392
1393 return OK;
1394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1398 */
1399 static int
1400generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1401{
1402 isn_T *isn;
1403
Bram Moolenaar080457c2020-03-03 21:53:32 +01001404 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1406 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001407 isn->isn_arg.storenr.stnr_idx = idx;
1408 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 return OK;
1411}
1412
1413/*
1414 * Generate an ISN_STOREOPT instruction
1415 */
1416 static int
1417generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1418{
1419 isn_T *isn;
1420
Bram Moolenaar080457c2020-03-03 21:53:32 +01001421 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001422 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 return FAIL;
1424 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1425 isn->isn_arg.storeopt.so_flags = opt_flags;
1426
1427 return OK;
1428}
1429
1430/*
1431 * Generate an ISN_LOAD or similar instruction.
1432 */
1433 static int
1434generate_LOAD(
1435 cctx_T *cctx,
1436 isntype_T isn_type,
1437 int idx,
1438 char_u *name,
1439 type_T *type)
1440{
1441 isn_T *isn;
1442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1445 return FAIL;
1446 if (name != NULL)
1447 isn->isn_arg.string = vim_strsave(name);
1448 else
1449 isn->isn_arg.number = idx;
1450
1451 return OK;
1452}
1453
1454/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001455 * Generate an ISN_LOADOUTER instruction
1456 */
1457 static int
1458generate_LOADOUTER(
1459 cctx_T *cctx,
1460 int idx,
1461 int nesting,
1462 type_T *type)
1463{
1464 isn_T *isn;
1465
1466 RETURN_OK_IF_SKIP(cctx);
1467 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1468 return FAIL;
1469 isn->isn_arg.outer.outer_idx = idx;
1470 isn->isn_arg.outer.outer_depth = nesting;
1471
1472 return OK;
1473}
1474
1475/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001476 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001477 */
1478 static int
1479generate_LOADV(
1480 cctx_T *cctx,
1481 char_u *name,
1482 int error)
1483{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001484 int di_flags;
1485 int vidx = find_vim_var(name, &di_flags);
1486 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001487
Bram Moolenaar080457c2020-03-03 21:53:32 +01001488 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001489 if (vidx < 0)
1490 {
1491 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001492 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 return FAIL;
1494 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001495 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001496
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001497 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001498}
1499
1500/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001501 * Generate an ISN_UNLET instruction.
1502 */
1503 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001504generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001505{
1506 isn_T *isn;
1507
1508 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001510 return FAIL;
1511 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1512 isn->isn_arg.unlet.ul_forceit = forceit;
1513
1514 return OK;
1515}
1516
1517/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001518 * Generate an ISN_LOCKCONST instruction.
1519 */
1520 static int
1521generate_LOCKCONST(cctx_T *cctx)
1522{
1523 isn_T *isn;
1524
1525 RETURN_OK_IF_SKIP(cctx);
1526 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1527 return FAIL;
1528 return OK;
1529}
1530
1531/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 * Generate an ISN_LOADS instruction.
1533 */
1534 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001535generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001537 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001539 int sid,
1540 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541{
1542 isn_T *isn;
1543
Bram Moolenaar080457c2020-03-03 21:53:32 +01001544 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001545 if (isn_type == ISN_LOADS)
1546 isn = generate_instr_type(cctx, isn_type, type);
1547 else
1548 isn = generate_instr_drop(cctx, isn_type, 1);
1549 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001551 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1552 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553
1554 return OK;
1555}
1556
1557/*
1558 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1559 */
1560 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001561generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 cctx_T *cctx,
1563 isntype_T isn_type,
1564 int sid,
1565 int idx,
1566 type_T *type)
1567{
1568 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001569 scriptref_T *sref;
1570 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if (isn_type == ISN_LOADSCRIPT)
1574 isn = generate_instr_type(cctx, isn_type, type);
1575 else
1576 isn = generate_instr_drop(cctx, isn_type, 1);
1577 if (isn == NULL)
1578 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001579
1580 // This requires three arguments, which doesn't fit in an instruction, thus
1581 // we need to allocate a struct for this.
1582 sref = ALLOC_ONE(scriptref_T);
1583 if (sref == NULL)
1584 return FAIL;
1585 isn->isn_arg.script.scriptref = sref;
1586 sref->sref_sid = sid;
1587 sref->sref_idx = idx;
1588 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001589 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return OK;
1591}
1592
1593/*
1594 * Generate an ISN_NEWLIST instruction.
1595 */
1596 static int
1597generate_NEWLIST(cctx_T *cctx, int count)
1598{
1599 isn_T *isn;
1600 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 type_T *type;
1602 type_T *member;
1603
Bram Moolenaar080457c2020-03-03 21:53:32 +01001604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1606 return FAIL;
1607 isn->isn_arg.number = count;
1608
Bram Moolenaar127542b2020-08-09 17:22:04 +02001609 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001610 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001611 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001612 else
1613 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001614 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1615 cctx->ctx_type_list);
1616 type = get_list_type(member, cctx->ctx_type_list);
1617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 // drop the value types
1619 stack->ga_len -= count;
1620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001622 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 return FAIL;
1624 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_NEWDICT instruction.
1632 */
1633 static int
1634generate_NEWDICT(cctx_T *cctx, int count)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 type_T *type;
1639 type_T *member;
1640
Bram Moolenaar080457c2020-03-03 21:53:32 +01001641 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1643 return FAIL;
1644 isn->isn_arg.number = count;
1645
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001646 if (count == 0)
1647 member = &t_void;
1648 else
1649 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001650 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1651 cctx->ctx_type_list);
1652 type = get_dict_type(member, cctx->ctx_type_list);
1653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 // drop the key and value types
1655 stack->ga_len -= 2 * count;
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001658 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 return FAIL;
1660 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1661 ++stack->ga_len;
1662
1663 return OK;
1664}
1665
1666/*
1667 * Generate an ISN_FUNCREF instruction.
1668 */
1669 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001670generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671{
1672 isn_T *isn;
1673 garray_T *stack = &cctx->ctx_type_stack;
1674
Bram Moolenaar080457c2020-03-03 21:53:32 +01001675 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1677 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001678 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001679 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001681 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001682 // the nested context, including this one.
1683 if (ufunc->uf_flags & FC_CLOSURE)
1684 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1685
Bram Moolenaar35578162021-08-02 19:10:38 +02001686 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001688 ((type_T **)stack->ga_data)[stack->ga_len] =
1689 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 ++stack->ga_len;
1691
1692 return OK;
1693}
1694
1695/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001696 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001697 * "lambda_name" and "func_name" must be in allocated memory and will be
1698 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001699 */
1700 static int
1701generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1702{
1703 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001704
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001705 if (cctx->ctx_skip == SKIP_YES)
1706 {
1707 vim_free(lambda_name);
1708 vim_free(func_name);
1709 return OK;
1710 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001711 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001712 {
1713 vim_free(lambda_name);
1714 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001715 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001716 }
1717 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001718 isn->isn_arg.newfunc.nf_global = func_name;
1719
1720 return OK;
1721}
1722
1723/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001724 * Generate an ISN_DEF instruction: list functions
1725 */
1726 static int
1727generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1728{
1729 isn_T *isn;
1730
1731 RETURN_OK_IF_SKIP(cctx);
1732 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1733 return FAIL;
1734 if (len > 0)
1735 {
1736 isn->isn_arg.string = vim_strnsave(name, len);
1737 if (isn->isn_arg.string == NULL)
1738 return FAIL;
1739 }
1740 return OK;
1741}
1742
1743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 * Generate an ISN_JUMP instruction.
1745 */
1746 static int
1747generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1748{
1749 isn_T *isn;
1750 garray_T *stack = &cctx->ctx_type_stack;
1751
Bram Moolenaar080457c2020-03-03 21:53:32 +01001752 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.jump.jump_when = when;
1756 isn->isn_arg.jump.jump_where = where;
1757
1758 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1759 --stack->ga_len;
1760
1761 return OK;
1762}
1763
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001764/*
1765 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1766 */
1767 static int
1768generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1769{
1770 isn_T *isn;
1771
1772 RETURN_OK_IF_SKIP(cctx);
1773 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1776 // jump_where is set later
1777 return OK;
1778}
1779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 static int
1781generate_FOR(cctx_T *cctx, int loop_idx)
1782{
1783 isn_T *isn;
1784 garray_T *stack = &cctx->ctx_type_stack;
1785
Bram Moolenaar080457c2020-03-03 21:53:32 +01001786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1788 return FAIL;
1789 isn->isn_arg.forloop.for_idx = loop_idx;
1790
Bram Moolenaar35578162021-08-02 19:10:38 +02001791 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 return FAIL;
1793 // type doesn't matter, will be stored next
1794 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1795 ++stack->ga_len;
1796
1797 return OK;
1798}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001799/*
1800 * Generate an ISN_TRYCONT instruction.
1801 */
1802 static int
1803generate_TRYCONT(cctx_T *cctx, int levels, int where)
1804{
1805 isn_T *isn;
1806
1807 RETURN_OK_IF_SKIP(cctx);
1808 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1809 return FAIL;
1810 isn->isn_arg.trycont.tct_levels = levels;
1811 isn->isn_arg.trycont.tct_where = where;
1812
1813 return OK;
1814}
1815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
1817/*
1818 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001819 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 * Return FAIL if the number of arguments is wrong.
1821 */
1822 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001823generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824{
1825 isn_T *isn;
1826 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001827 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001828 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001829 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001830 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
Bram Moolenaar080457c2020-03-03 21:53:32 +01001832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001833 argoff = check_internal_func(func_idx, argcount);
1834 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 return FAIL;
1836
Bram Moolenaar389df252020-07-09 21:20:47 +02001837 if (method_call && argoff > 1)
1838 {
1839 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.shuffle.shfl_item = argcount;
1842 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1843 }
1844
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001845 if (argcount > 0)
1846 {
1847 // Check the types of the arguments.
1848 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001849 if (method_call && argoff > 1)
1850 {
1851 int i;
1852
1853 for (i = 0; i < argcount; ++i)
1854 shuffled_argtypes[i] = (i < argoff - 1)
1855 ? argtypes[i + 1]
1856 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1857 argtypes = shuffled_argtypes;
1858 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001859 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1860 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001861 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001862 if (internal_func_is_map(func_idx))
1863 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001864 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1867 return FAIL;
1868 isn->isn_arg.bfunc.cbf_idx = func_idx;
1869 isn->isn_arg.bfunc.cbf_argcount = argcount;
1870
Bram Moolenaar94738d82020-10-21 14:25:07 +02001871 // Drop the argument types and push the return type.
1872 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001873 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 return FAIL;
1875 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001876 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001877 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001879 if (maptype != NULL && maptype->tt_member != NULL
1880 && maptype->tt_member != &t_any)
1881 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001882 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 return OK;
1885}
1886
1887/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001888 * Generate an ISN_LISTAPPEND instruction. Works like add().
1889 * Argument count is already checked.
1890 */
1891 static int
1892generate_LISTAPPEND(cctx_T *cctx)
1893{
1894 garray_T *stack = &cctx->ctx_type_stack;
1895 type_T *list_type;
1896 type_T *item_type;
1897 type_T *expected;
1898
1899 // Caller already checked that list_type is a list.
1900 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1901 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1902 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001903 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001904 return FAIL;
1905
1906 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1907 return FAIL;
1908
1909 --stack->ga_len; // drop the argument
1910 return OK;
1911}
1912
1913/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001914 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1915 * Argument count is already checked.
1916 */
1917 static int
1918generate_BLOBAPPEND(cctx_T *cctx)
1919{
1920 garray_T *stack = &cctx->ctx_type_stack;
1921 type_T *item_type;
1922
1923 // Caller already checked that blob_type is a blob.
1924 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001925 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001926 return FAIL;
1927
1928 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1929 return FAIL;
1930
1931 --stack->ga_len; // drop the argument
1932 return OK;
1933}
1934
1935/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001936 * Return TRUE if "ufunc" should be compiled, taking into account whether
1937 * "profile" indicates profiling is to be done.
1938 */
1939 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001940func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001941{
1942 switch (ufunc->uf_def_status)
1943 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001944 case UF_TO_BE_COMPILED:
1945 return TRUE;
1946
Bram Moolenaarb2049902021-01-24 12:53:53 +01001947 case UF_COMPILED:
1948 {
1949 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1950 + ufunc->uf_dfunc_idx;
1951
Bram Moolenaare99d4222021-06-13 14:01:26 +02001952 switch (compile_type)
1953 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001954 case CT_PROFILE:
1955#ifdef FEAT_PROFILE
1956 return dfunc->df_instr_prof == NULL;
1957#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001958 case CT_NONE:
1959 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001960 case CT_DEBUG:
1961 return dfunc->df_instr_debug == NULL;
1962 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001963 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001964
1965 case UF_NOT_COMPILED:
1966 case UF_COMPILE_ERROR:
1967 case UF_COMPILING:
1968 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001969 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001970 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001971}
1972
1973/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 * Generate an ISN_DCALL or ISN_UCALL instruction.
1975 * Return FAIL if the number of arguments is wrong.
1976 */
1977 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001978generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979{
1980 isn_T *isn;
1981 garray_T *stack = &cctx->ctx_type_stack;
1982 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001983 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984
Bram Moolenaar080457c2020-03-03 21:53:32 +01001985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 if (argcount > regular_args && !has_varargs(ufunc))
1987 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001988 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 return FAIL;
1990 }
1991 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1992 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001993 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 return FAIL;
1995 }
1996
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001997 if (ufunc->uf_def_status != UF_NOT_COMPILED
1998 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001999 {
2000 int i;
2001
2002 for (i = 0; i < argcount; ++i)
2003 {
2004 type_T *expected;
2005 type_T *actual;
2006
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002007 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2008 if (actual == &t_special
2009 && i >= regular_args - ufunc->uf_def_args.ga_len)
2010 {
2011 // assume v:none used for default argument value
2012 continue;
2013 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002014 if (i < regular_args)
2015 {
2016 if (ufunc->uf_arg_types == NULL)
2017 continue;
2018 expected = ufunc->uf_arg_types[i];
2019 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002020 else if (ufunc->uf_va_type == NULL
2021 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002022 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002023 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002024 else
2025 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002026 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002027 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002028 {
2029 arg_type_mismatch(expected, actual, i + 1);
2030 return FAIL;
2031 }
2032 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002033 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002034 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002035 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002036 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002037 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002038 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2039 {
2040 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2041 ufunc->uf_name);
2042 return FAIL;
2043 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002046 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002047 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002049 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 {
2051 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2052 isn->isn_arg.dfunc.cdf_argcount = argcount;
2053 }
2054 else
2055 {
2056 // A user function may be deleted and redefined later, can't use the
2057 // ufunc pointer, need to look it up again at runtime.
2058 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2059 isn->isn_arg.ufunc.cuf_argcount = argcount;
2060 }
2061
2062 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002063 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 return FAIL;
2065 // add return value
2066 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2067 ++stack->ga_len;
2068
2069 return OK;
2070}
2071
2072/*
2073 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2074 */
2075 static int
2076generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2077{
2078 isn_T *isn;
2079 garray_T *stack = &cctx->ctx_type_stack;
2080
Bram Moolenaar080457c2020-03-03 21:53:32 +01002081 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2083 return FAIL;
2084 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2085 isn->isn_arg.ufunc.cuf_argcount = argcount;
2086
2087 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002088 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002089 return FAIL;
2090 // add return value
2091 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2092 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093
2094 return OK;
2095}
2096
2097/*
2098 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002099 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 */
2101 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002102generate_PCALL(
2103 cctx_T *cctx,
2104 int argcount,
2105 char_u *name,
2106 type_T *type,
2107 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108{
2109 isn_T *isn;
2110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002111 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112
Bram Moolenaar080457c2020-03-03 21:53:32 +01002113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002114
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002115 if (type->tt_type == VAR_ANY)
2116 ret_type = &t_any;
2117 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002118 {
2119 if (type->tt_argcount != -1)
2120 {
2121 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2122
2123 if (argcount < type->tt_min_argcount - varargs)
2124 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002125 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002126 return FAIL;
2127 }
2128 if (!varargs && argcount > type->tt_argcount)
2129 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002130 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002131 return FAIL;
2132 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002133 if (type->tt_args != NULL)
2134 {
2135 int i;
2136
2137 for (i = 0; i < argcount; ++i)
2138 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002139 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002140 type_T *actual = ((type_T **)stack->ga_data)[
2141 stack->ga_len + offset];
2142 type_T *expected;
2143
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002144 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002145 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002146 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002147 else if (i >= type->tt_min_argcount
2148 && actual == &t_special)
2149 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002150 else
2151 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002152 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002153 cctx, TRUE, FALSE) == FAIL)
2154 {
2155 arg_type_mismatch(expected, actual, i + 1);
2156 return FAIL;
2157 }
2158 }
2159 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002160 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002161 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002162 if (ret_type == &t_unknown)
2163 // return type not known yet, use a runtime check
2164 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002165 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002166 else
2167 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002168 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002169 return FAIL;
2170 }
2171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2173 return FAIL;
2174 isn->isn_arg.pfunc.cpf_top = at_top;
2175 isn->isn_arg.pfunc.cpf_argcount = argcount;
2176
2177 stack->ga_len -= argcount; // drop the arguments
2178
2179 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002180 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002182 // If partial is above the arguments it must be cleared and replaced with
2183 // the return value.
2184 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2185 return FAIL;
2186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 return OK;
2188}
2189
2190/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002191 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 */
2193 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195{
2196 isn_T *isn;
2197 garray_T *stack = &cctx->ctx_type_stack;
2198 type_T *type;
2199
Bram Moolenaar080457c2020-03-03 21:53:32 +01002200 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002201 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002203 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002205 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002207 if (type->tt_type != VAR_DICT && type != &t_any)
2208 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002209 char *tofree;
2210
2211 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2212 name, type_name(type, &tofree));
2213 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002214 return FAIL;
2215 }
2216 // change dict type to dict member type
2217 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002218 {
2219 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2220 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222
2223 return OK;
2224}
2225
2226/*
2227 * Generate an ISN_ECHO instruction.
2228 */
2229 static int
2230generate_ECHO(cctx_T *cctx, int with_white, int count)
2231{
2232 isn_T *isn;
2233
Bram Moolenaar080457c2020-03-03 21:53:32 +01002234 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2236 return FAIL;
2237 isn->isn_arg.echo.echo_with_white = with_white;
2238 isn->isn_arg.echo.echo_count = count;
2239
2240 return OK;
2241}
2242
Bram Moolenaarad39c092020-02-26 18:23:43 +01002243/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002244 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002245 */
2246 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002247generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002248{
2249 isn_T *isn;
2250
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002251 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002252 return FAIL;
2253 isn->isn_arg.number = count;
2254
2255 return OK;
2256}
2257
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002258/*
2259 * Generate an ISN_PUT instruction.
2260 */
2261 static int
2262generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2263{
2264 isn_T *isn;
2265
2266 RETURN_OK_IF_SKIP(cctx);
2267 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2268 return FAIL;
2269 isn->isn_arg.put.put_regname = regname;
2270 isn->isn_arg.put.put_lnum = lnum;
2271 return OK;
2272}
2273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 static int
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002275generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276{
2277 isn_T *isn;
2278
Bram Moolenaar080457c2020-03-03 21:53:32 +01002279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002280 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 return FAIL;
2282 isn->isn_arg.string = vim_strsave(line);
2283 return OK;
2284}
2285
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002286 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002287generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2288{
2289 isn_T *isn;
2290 garray_T *stack = &cctx->ctx_type_stack;
2291
2292 RETURN_OK_IF_SKIP(cctx);
2293 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2294 return FAIL;
2295 isn->isn_arg.string = vim_strsave(line);
2296
Bram Moolenaar35578162021-08-02 19:10:38 +02002297 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002298 return FAIL;
2299 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2300 ++stack->ga_len;
2301
2302 return OK;
2303}
2304
2305 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002306generate_EXECCONCAT(cctx_T *cctx, int count)
2307{
2308 isn_T *isn;
2309
2310 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2311 return FAIL;
2312 isn->isn_arg.number = count;
2313 return OK;
2314}
2315
Bram Moolenaar08597872020-12-10 19:43:40 +01002316/*
2317 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2318 */
2319 static int
2320generate_RANGE(cctx_T *cctx, char_u *range)
2321{
2322 isn_T *isn;
2323 garray_T *stack = &cctx->ctx_type_stack;
2324
2325 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2326 return FAIL;
2327 isn->isn_arg.string = range;
2328
Bram Moolenaar35578162021-08-02 19:10:38 +02002329 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002330 return FAIL;
2331 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2332 ++stack->ga_len;
2333 return OK;
2334}
2335
Bram Moolenaar792f7862020-11-23 08:31:18 +01002336 static int
2337generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2338{
2339 isn_T *isn;
2340
2341 RETURN_OK_IF_SKIP(cctx);
2342 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2343 return FAIL;
2344 isn->isn_arg.unpack.unp_count = var_count;
2345 isn->isn_arg.unpack.unp_semicolon = semicolon;
2346 return OK;
2347}
2348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002350 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002351 */
2352 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002353generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002354{
2355 isn_T *isn;
2356
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002357 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002358 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002359 cctx->ctx_has_cmdmod = TRUE;
2360
2361 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002362 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002363 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2364 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2365 return FAIL;
2366 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002367 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002368 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002369 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002370
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002371 return OK;
2372}
2373
2374 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002375generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002376{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002377 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2378 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002379 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002380 return OK;
2381}
2382
Bram Moolenaarfa984412021-03-25 22:15:28 +01002383 static int
2384misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002385{
2386 garray_T *instr = &cctx->ctx_instr;
2387
Bram Moolenaara91a7132021-03-25 21:12:15 +01002388 if (cctx->ctx_has_cmdmod
2389 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2390 == ISN_CMDMOD)
2391 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002392 emsg(_(e_misplaced_command_modifier));
2393 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002394 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002395 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002396}
2397
2398/*
2399 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002400 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 */
2402 static int
2403current_instr_idx(cctx_T *cctx)
2404{
2405 garray_T *instr = &cctx->ctx_instr;
2406 int idx = instr->ga_len;
2407
Bram Moolenaare99d4222021-06-13 14:01:26 +02002408 while (idx > 0)
2409 {
2410 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002411 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002412 {
2413 --idx;
2414 continue;
2415 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002416#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002417 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2418 {
2419 --idx;
2420 continue;
2421 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002422#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002423 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2424 {
2425 --idx;
2426 continue;
2427 }
2428 break;
2429 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002430 return idx;
2431}
2432
Bram Moolenaarf002a412021-01-24 13:34:18 +01002433#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002434 static void
2435may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2436{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002437 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002438 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002439}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002440#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002441
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002442/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002444 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002446 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002447reserve_local(
2448 cctx_T *cctx,
2449 char_u *name,
2450 size_t len,
2451 int isConst,
2452 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002455 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002457 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002459 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002460 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 }
2462
Bram Moolenaar35578162021-08-02 19:10:38 +02002463 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464 return NULL;
2465 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002466 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002468 // Every local variable uses the next entry on the stack. We could re-use
2469 // the last ones when leaving a scope, but then variables used in a closure
2470 // might get overwritten. To keep things simple do not re-use stack
2471 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002472 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2473 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002474
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002475 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 lvar->lv_const = isConst;
2477 lvar->lv_type = type;
2478
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002479 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002480 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002481 return NULL;
2482 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2483 vim_strsave(lvar->lv_name);
2484 ++dfunc->df_var_names.ga_len;
2485
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002486 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487}
2488
2489/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002490 * Remove local variables above "new_top".
2491 */
2492 static void
2493unwind_locals(cctx_T *cctx, int new_top)
2494{
2495 if (cctx->ctx_locals.ga_len > new_top)
2496 {
2497 int idx;
2498 lvar_T *lvar;
2499
2500 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2501 {
2502 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2503 vim_free(lvar->lv_name);
2504 }
2505 }
2506 cctx->ctx_locals.ga_len = new_top;
2507}
2508
2509/*
2510 * Free all local variables.
2511 */
2512 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002513free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514{
2515 unwind_locals(cctx, 0);
2516 ga_clear(&cctx->ctx_locals);
2517}
2518
2519/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002520 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2521 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2522 * error if the variable was defined with :const.
2523 */
2524 static int
2525check_item_writable(svar_T *sv, int check_writable, char_u *name)
2526{
2527 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2528 || (check_writable == ASSIGN_FINAL
2529 && sv->sv_const == ASSIGN_CONST))
2530 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002531 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002532 return FAIL;
2533 }
2534 return OK;
2535}
2536
2537/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002539 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 * Returns the index in "sn_var_vals" if found.
2541 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002542 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 */
2544 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002545get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546{
2547 hashtab_T *ht;
2548 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002549 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002550 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 int idx;
2552
Bram Moolenaare3d46852020-08-29 13:39:17 +02002553 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002555 if (sid == current_sctx.sc_sid)
2556 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002557 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002558
2559 if (sav == NULL)
2560 return -2;
2561 idx = sav->sav_var_vals_idx;
2562 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002563 if (check_item_writable(sv, check_writable, name) == FAIL)
2564 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002565 return idx;
2566 }
2567
2568 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 ht = &SCRIPT_VARS(sid);
2570 di = find_var_in_ht(ht, 0, name, TRUE);
2571 if (di == NULL)
2572 return -2;
2573
2574 // Now find the svar_T index in sn_var_vals.
2575 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2576 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002577 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (sv->sv_tv == &di->di_tv)
2579 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002580 if (check_item_writable(sv, check_writable, name) == FAIL)
2581 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 return idx;
2583 }
2584 }
2585 return -1;
2586}
2587
2588/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002589 * Find "name" in imported items of the current script or in "cctx" if not
2590 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 */
2592 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002593find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 int idx;
2596
Bram Moolenaare3d46852020-08-29 13:39:17 +02002597 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002598 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 if (cctx != NULL)
2600 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2601 {
2602 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2603 + idx;
2604
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002605 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2606 : STRLEN(import->imp_name) == len
2607 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 return import;
2609 }
2610
Bram Moolenaarefa94442020-08-08 22:16:00 +02002611 return find_imported_in_script(name, len, current_sctx.sc_sid);
2612}
2613
2614 imported_T *
2615find_imported_in_script(char_u *name, size_t len, int sid)
2616{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002617 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002618 int idx;
2619
Bram Moolenaare3d46852020-08-29 13:39:17 +02002620 if (!SCRIPT_ID_VALID(sid))
2621 return NULL;
2622 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2624 {
2625 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2626
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002627 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2628 : STRLEN(import->imp_name) == len
2629 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 return import;
2631 }
2632 return NULL;
2633}
2634
2635/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002636 * Free all imported variables.
2637 */
2638 static void
2639free_imported(cctx_T *cctx)
2640{
2641 int idx;
2642
2643 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2644 {
2645 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2646
2647 vim_free(import->imp_name);
2648 }
2649 ga_clear(&cctx->ctx_imports);
2650}
2651
2652/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002653 * Return a pointer to the next line that isn't empty or only contains a
2654 * comment. Skips over white space.
2655 * Returns NULL if there is none.
2656 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002657 char_u *
2658peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659{
2660 int lnum = cctx->ctx_lnum;
2661
2662 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2663 {
2664 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002665 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002666
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002667 // ignore NULLs inserted for continuation lines
2668 if (line != NULL)
2669 {
2670 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002671 if (vim9_bad_comment(p))
2672 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002673 if (*p != NUL && !vim9_comment_start(p))
2674 return p;
2675 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002676 }
2677 return NULL;
2678}
2679
2680/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002681 * Called when checking for a following operator at "arg". When the rest of
2682 * the line is empty or only a comment, peek the next line. If there is a next
2683 * line return a pointer to it and set "nextp".
2684 * Otherwise skip over white space.
2685 */
2686 static char_u *
2687may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2688{
2689 char_u *p = skipwhite(arg);
2690
2691 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002692 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002693 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002694 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002695 if (*nextp != NULL)
2696 return *nextp;
2697 }
2698 return p;
2699}
2700
2701/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002702 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002703 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002704 * Returns NULL when at the end.
2705 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002706 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002707next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002708{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002709 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002710
2711 do
2712 {
2713 ++cctx->ctx_lnum;
2714 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002715 {
2716 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002717 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002718 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002719 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002720 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002721 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002722 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002723 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002724 return line;
2725}
2726
2727/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002728 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002729 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002730 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002731 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2732 */
2733 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002734may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002735{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002736 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002737 if (vim9_bad_comment(*arg))
2738 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002739 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002740 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002741 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002742
2743 if (next == NULL)
2744 return FAIL;
2745 *arg = skipwhite(next);
2746 }
2747 return OK;
2748}
2749
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002750/*
2751 * Idem, and give an error when failed.
2752 */
2753 static int
2754may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2755{
2756 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2757 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002758 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002759 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002760 return FAIL;
2761 }
2762 return OK;
2763}
2764
2765
Bram Moolenaara5565e42020-05-09 15:44:01 +02002766// Structure passed between the compile_expr* functions to keep track of
2767// constants that have been parsed but for which no code was produced yet. If
2768// possible expressions on these constants are applied at compile time. If
2769// that is not possible, the code to push the constants needs to be generated
2770// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002771// Using 50 should be more than enough of 5 levels of ().
2772#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002773typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002774 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002775 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002776 int pp_is_const; // all generated code was constants, used for a
2777 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778} ppconst_T;
2779
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002780static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002781static int compile_expr0(char_u **arg, cctx_T *cctx);
2782static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2783
Bram Moolenaara5565e42020-05-09 15:44:01 +02002784/*
2785 * Generate a PUSH instruction for "tv".
2786 * "tv" will be consumed or cleared.
2787 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2788 */
2789 static int
2790generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2791{
2792 if (tv != NULL)
2793 {
2794 switch (tv->v_type)
2795 {
2796 case VAR_UNKNOWN:
2797 break;
2798 case VAR_BOOL:
2799 generate_PUSHBOOL(cctx, tv->vval.v_number);
2800 break;
2801 case VAR_SPECIAL:
2802 generate_PUSHSPEC(cctx, tv->vval.v_number);
2803 break;
2804 case VAR_NUMBER:
2805 generate_PUSHNR(cctx, tv->vval.v_number);
2806 break;
2807#ifdef FEAT_FLOAT
2808 case VAR_FLOAT:
2809 generate_PUSHF(cctx, tv->vval.v_float);
2810 break;
2811#endif
2812 case VAR_BLOB:
2813 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2814 tv->vval.v_blob = NULL;
2815 break;
2816 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002817 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 tv->vval.v_string = NULL;
2819 break;
2820 default:
2821 iemsg("constant type not supported");
2822 clear_tv(tv);
2823 return FAIL;
2824 }
2825 tv->v_type = VAR_UNKNOWN;
2826 }
2827 return OK;
2828}
2829
2830/*
2831 * Generate code for any ppconst entries.
2832 */
2833 static int
2834generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2835{
2836 int i;
2837 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002838 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002839
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002840 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002841 for (i = 0; i < ppconst->pp_used; ++i)
2842 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2843 ret = FAIL;
2844 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002845 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002846 return ret;
2847}
2848
2849/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002850 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002851 */
2852 static int
2853check_ppconst_bool(ppconst_T *ppconst)
2854{
2855 if (ppconst->pp_used > 0)
2856 {
2857 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002858 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002859
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002860 return check_typval_type(&t_bool, tv, where);
2861 }
2862 return OK;
2863}
2864
2865/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866 * Clear ppconst constants. Used when failing.
2867 */
2868 static void
2869clear_ppconst(ppconst_T *ppconst)
2870{
2871 int i;
2872
2873 for (i = 0; i < ppconst->pp_used; ++i)
2874 clear_tv(&ppconst->pp_tv[i]);
2875 ppconst->pp_used = 0;
2876}
2877
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002878/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002881 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002882 */
2883 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002884compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002885{
2886 type_T **typep;
2887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002888 vartype_T vartype;
2889 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002890
Bram Moolenaar261417b2021-05-06 21:04:55 +02002891 // We can index a list, dict and blob. If we don't know the type
2892 // we can use the index value type. If we still don't know use an "ANY"
2893 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002894 typep = ((type_T **)stack->ga_data) + stack->ga_len
2895 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002896 vartype = (*typep)->tt_type;
2897 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002898 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002899 if (*typep == &t_any && idxtype == &t_string)
2900 vartype = VAR_DICT;
2901 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002902 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002903 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 return FAIL;
2905 if (is_slice)
2906 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002907 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2908 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002909 FALSE, FALSE) == FAIL)
2910 return FAIL;
2911 }
2912 }
2913
Bram Moolenaar261417b2021-05-06 21:04:55 +02002914 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002915 {
2916 if (is_slice)
2917 {
2918 emsg(_(e_cannot_slice_dictionary));
2919 return FAIL;
2920 }
2921 if ((*typep)->tt_type == VAR_DICT)
2922 {
2923 *typep = (*typep)->tt_member;
2924 if (*typep == &t_unknown)
2925 // empty dict was used
2926 *typep = &t_any;
2927 }
2928 else
2929 {
2930 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2931 FALSE, FALSE) == FAIL)
2932 return FAIL;
2933 *typep = &t_any;
2934 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002935 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002936 return FAIL;
2937 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2938 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002939 if (keeping_dict != NULL)
2940 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002941 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002942 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002943 {
2944 *typep = &t_string;
2945 if ((is_slice
2946 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2947 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2948 return FAIL;
2949 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002950 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002951 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002952 if (is_slice)
2953 {
2954 *typep = &t_blob;
2955 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2956 return FAIL;
2957 }
2958 else
2959 {
2960 *typep = &t_number;
2961 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2962 return FAIL;
2963 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002964 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002965 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002966 {
2967 if (is_slice)
2968 {
2969 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002970 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002971 2) == FAIL)
2972 return FAIL;
2973 }
2974 else
2975 {
2976 if ((*typep)->tt_type == VAR_LIST)
2977 {
2978 *typep = (*typep)->tt_member;
2979 if (*typep == &t_unknown)
2980 // empty list was used
2981 *typep = &t_any;
2982 }
2983 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002984 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2985 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002986 return FAIL;
2987 }
2988 }
2989 else
2990 {
2991 emsg(_(e_string_list_dict_or_blob_required));
2992 return FAIL;
2993 }
2994 return OK;
2995}
2996
2997/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002998 * Generate an instruction to load script-local variable "name", without the
2999 * leading "s:".
3000 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 */
3002 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003003compile_load_scriptvar(
3004 cctx_T *cctx,
3005 char_u *name, // variable NUL terminated
3006 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003007 char_u **end, // end of variable
3008 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003010 scriptitem_T *si;
3011 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 imported_T *import;
3013
Bram Moolenaare3d46852020-08-29 13:39:17 +02003014 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3015 return FAIL;
3016 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003017 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003018 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003020 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003021 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3022 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 }
3024 if (idx >= 0)
3025 {
3026 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3027
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003028 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 current_sctx.sc_sid, idx, sv->sv_type);
3030 return OK;
3031 }
3032
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003033 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (import != NULL)
3035 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003036 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003037 {
3038 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003039 char_u *exp_name;
3040 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003041 ufunc_T *ufunc;
3042 type_T *type;
3043
3044 // Used "import * as Name", need to lookup the member.
3045 if (*p != '.')
3046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003047 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003048 return FAIL;
3049 }
3050 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003051 if (VIM_ISWHITE(*p))
3052 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003053 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003054 return FAIL;
3055 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003056
Bram Moolenaar1c991142020-07-04 13:15:31 +02003057 // isolate one name
3058 exp_name = p;
3059 while (eval_isnamec(*p))
3060 ++p;
3061 cc = *p;
3062 *p = NUL;
3063
Bram Moolenaaredba7072021-03-13 21:14:18 +01003064 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3065 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003066 *p = cc;
3067 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003068 *end = p;
3069
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003070 if (idx < 0)
3071 {
3072 if (*p == '(' && ufunc != NULL)
3073 {
3074 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3075 return OK;
3076 }
3077 return FAIL;
3078 }
3079
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003080 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3081 import->imp_sid,
3082 idx,
3083 type);
3084 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003085 else if (import->imp_funcname != NULL)
3086 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003087 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003088 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3089 import->imp_sid,
3090 import->imp_var_vals_idx,
3091 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 return OK;
3093 }
3094
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003095 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003096 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 return FAIL;
3098}
3099
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003100 static int
3101generate_funcref(cctx_T *cctx, char_u *name)
3102{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003103 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003104
3105 if (ufunc == NULL)
3106 return FAIL;
3107
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003108 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003109 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3110 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003111 == FAIL)
3112 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003113 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003114}
3115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116/*
3117 * Compile a variable name into a load instruction.
3118 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003119 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 * When "error" is FALSE do not give an error when not found.
3121 */
3122 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003123compile_load(
3124 char_u **arg,
3125 char_u *end_arg,
3126 cctx_T *cctx,
3127 int is_expr,
3128 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129{
3130 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003131 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003132 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003134 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135
3136 if (*(*arg + 1) == ':')
3137 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003138 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003139 {
3140 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
Bram Moolenaarfa596382021-04-07 21:58:16 +02003142 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003143 switch (**arg)
3144 {
3145 case 'g': isn_type = ISN_LOADGDICT; break;
3146 case 'w': isn_type = ISN_LOADWDICT; break;
3147 case 't': isn_type = ISN_LOADTDICT; break;
3148 case 'b': isn_type = ISN_LOADBDICT; break;
3149 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003151 goto theend;
3152 }
3153 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3154 goto theend;
3155 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 else
3158 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003159 isntype_T isn_type = ISN_DROP;
3160
Bram Moolenaarfa596382021-04-07 21:58:16 +02003161 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3163 if (name == NULL)
3164 return FAIL;
3165
3166 switch (**arg)
3167 {
3168 case 'v': res = generate_LOADV(cctx, name, error);
3169 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003170 case 's': if (is_expr && ASCII_ISUPPER(*name)
3171 && find_func(name, FALSE, cctx) != NULL)
3172 res = generate_funcref(cctx, name);
3173 else
3174 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003175 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003176 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003177 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003178 {
3179 if (is_expr && ASCII_ISUPPER(*name)
3180 && find_func(name, FALSE, cctx) != NULL)
3181 res = generate_funcref(cctx, name);
3182 else
3183 isn_type = ISN_LOADG;
3184 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003185 else
3186 {
3187 isn_type = ISN_LOADAUTO;
3188 vim_free(name);
3189 name = vim_strnsave(*arg, end - *arg);
3190 if (name == NULL)
3191 return FAIL;
3192 }
3193 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003194 case 'w': isn_type = ISN_LOADW; break;
3195 case 't': isn_type = ISN_LOADT; break;
3196 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003197 default: // cannot happen, just in case
3198 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003199 goto theend;
3200 }
3201 if (isn_type != ISN_DROP)
3202 {
3203 // Global, Buffer-local, Window-local and Tabpage-local
3204 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003205 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003206 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 }
3210 else
3211 {
3212 size_t len = end - *arg;
3213 int idx;
3214 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003215 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 name = vim_strnsave(*arg, end - *arg);
3218 if (name == NULL)
3219 return FAIL;
3220
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003221 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3222 {
3223 script_autoload(name, FALSE);
3224 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3225 }
3226 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3227 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003229 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003230 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 else
3233 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003234 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003235
Bram Moolenaar709664c2020-12-12 14:33:41 +01003236 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003238 type = lvar.lv_type;
3239 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003240 if (lvar.lv_from_outer != 0)
3241 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003242 else
3243 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
3245 else
3246 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003247 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003248 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003249 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003250 || find_imported(name, 0, cctx) != NULL)
3251 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003252
Bram Moolenaar0f769812020-09-12 18:32:34 +02003253 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003254 // uppercase letter it can be a user defined function.
3255 // generate_funcref() will fail if the function can't be found.
3256 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003257 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 }
3259 }
3260 if (gen_load)
3261 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003262 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003263 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003264 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003265 cctx->ctx_outer_used = TRUE;
3266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 }
3268
3269 *arg = end;
3270
3271theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003272 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003273 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 vim_free(name);
3275 return res;
3276}
3277
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003278 static void
3279clear_instr_ga(garray_T *gap)
3280{
3281 int idx;
3282
3283 for (idx = 0; idx < gap->ga_len; ++idx)
3284 delete_instr(((isn_T *)gap->ga_data) + idx);
3285 ga_clear(gap);
3286}
3287
3288/*
3289 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3290 * Returns FAIL if compilation fails.
3291 */
3292 static int
3293compile_string(isn_T *isn, cctx_T *cctx)
3294{
3295 char_u *s = isn->isn_arg.string;
3296 garray_T save_ga = cctx->ctx_instr;
3297 int expr_res;
3298 int trailing_error;
3299 int instr_count;
3300 isn_T *instr = NULL;
3301
Bram Moolenaarcd268012021-07-22 19:11:08 +02003302 // Remove the string type from the stack.
3303 --cctx->ctx_type_stack.ga_len;
3304
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003305 // Temporarily reset the list of instructions so that the jump labels are
3306 // correct.
3307 cctx->ctx_instr.ga_len = 0;
3308 cctx->ctx_instr.ga_maxlen = 0;
3309 cctx->ctx_instr.ga_data = NULL;
3310 expr_res = compile_expr0(&s, cctx);
3311 s = skipwhite(s);
3312 trailing_error = *s != NUL;
3313
Bram Moolenaarff652882021-05-16 15:24:49 +02003314 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003315 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 {
3317 if (trailing_error)
3318 semsg(_(e_trailing_arg), s);
3319 clear_instr_ga(&cctx->ctx_instr);
3320 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003321 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003322 return FAIL;
3323 }
3324
3325 // Move the generated instructions into the ISN_INSTR instruction, then
3326 // restore the list of instructions.
3327 instr_count = cctx->ctx_instr.ga_len;
3328 instr = cctx->ctx_instr.ga_data;
3329 instr[instr_count].isn_type = ISN_FINISH;
3330
3331 cctx->ctx_instr = save_ga;
3332 vim_free(isn->isn_arg.string);
3333 isn->isn_type = ISN_INSTR;
3334 isn->isn_arg.instr = instr;
3335 return OK;
3336}
3337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338/*
3339 * Compile the argument expressions.
3340 * "arg" points to just after the "(" and is advanced to after the ")"
3341 */
3342 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003343compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003345 char_u *p = *arg;
3346 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003347 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003348 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349
Bram Moolenaare6085c52020-04-12 20:19:16 +02003350 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003352 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3353 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003354 if (*p == ')')
3355 {
3356 *arg = p + 1;
3357 return OK;
3358 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003359 if (must_end)
3360 {
3361 semsg(_(e_missing_comma_before_argument_str), p);
3362 return FAIL;
3363 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003364
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003365 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003366 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 return FAIL;
3368 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003369
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003370 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003371 && cctx->ctx_instr.ga_len == instr_count + 1)
3372 {
3373 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3374
3375 // {skip} argument of searchpair() can be compiled if not empty
3376 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3377 compile_string(isn, cctx);
3378 }
3379
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003380 if (*p != ',' && *skipwhite(p) == ',')
3381 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003382 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003383 p = skipwhite(p);
3384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003386 {
3387 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003388 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003389 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003390 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003391 else
3392 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003393 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003394 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003396failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003397 emsg(_(e_missing_close));
3398 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399}
3400
3401/*
3402 * Compile a function call: name(arg1, arg2)
3403 * "arg" points to "name", "arg + varlen" to the "(".
3404 * "argcount_init" is 1 for "value->method()"
3405 * Instructions:
3406 * EVAL arg1
3407 * EVAL arg2
3408 * BCALL / DCALL / UCALL
3409 */
3410 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003411compile_call(
3412 char_u **arg,
3413 size_t varlen,
3414 cctx_T *cctx,
3415 ppconst_T *ppconst,
3416 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417{
3418 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003419 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 int argcount = argcount_init;
3421 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003422 char_u fname_buf[FLEN_FIXED + 1];
3423 char_u *tofree = NULL;
3424 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003425 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003426 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003427 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003428 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003430 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003431 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003432 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003433 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003434 {
3435 char_u *s = skipwhite(*arg + varlen + 1);
3436 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003437 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003438
3439 argvars[0].v_type = VAR_UNKNOWN;
3440 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003441 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003442 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003443 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003444 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003445 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003446 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003447 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003448 {
3449 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3450
3451 *arg = s + 1;
3452 argvars[1].v_type = VAR_UNKNOWN;
3453 tv->v_type = VAR_NUMBER;
3454 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003455 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003456 f_has(argvars, tv);
3457 else
3458 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003459 clear_tv(&argvars[0]);
3460 ++ppconst->pp_used;
3461 return OK;
3462 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003463 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003464 if (!is_has)
3465 {
3466 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3467 return FAIL;
3468 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003469 }
3470
3471 if (generate_ppconst(cctx, ppconst) == FAIL)
3472 return FAIL;
3473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 if (varlen >= sizeof(namebuf))
3475 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003476 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 return FAIL;
3478 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003479 vim_strncpy(namebuf, *arg, varlen);
3480 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003482 // We handle the "skip" argument of searchpair() and searchpairpos()
3483 // differently.
3484 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3485 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3486 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3487 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003490 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003491 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492
Bram Moolenaar03290b82020-12-19 16:30:44 +01003493 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003494 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 {
3496 int idx;
3497
3498 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003499 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003501 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003502 if (STRCMP(name, "flatten") == 0)
3503 {
3504 emsg(_(e_cannot_use_flatten_in_vim9_script));
3505 goto theend;
3506 }
3507
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003508 if (STRCMP(name, "add") == 0 && argcount == 2)
3509 {
3510 garray_T *stack = &cctx->ctx_type_stack;
3511 type_T *type = ((type_T **)stack->ga_data)[
3512 stack->ga_len - 2];
3513
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003514 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003515 if (type->tt_type == VAR_LIST)
3516 {
3517 // inline "add(list, item)" so that the type can be checked
3518 res = generate_LISTAPPEND(cctx);
3519 idx = -1;
3520 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003521 else if (type->tt_type == VAR_BLOB)
3522 {
3523 // inline "add(blob, nr)" so that the type can be checked
3524 res = generate_BLOBAPPEND(cctx);
3525 idx = -1;
3526 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003527 }
3528
3529 if (idx >= 0)
3530 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3531 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003532 else
3533 semsg(_(e_unknownfunc), namebuf);
3534 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003537 // An argument or local variable can be a function reference, this
3538 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003539 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003540 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003541 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003542 // If we can find the function by name generate the right call.
3543 // Skip global functions here, a local funcref takes precedence.
3544 ufunc = find_func(name, FALSE, cctx);
3545 if (ufunc != NULL && !func_is_global(ufunc))
3546 {
3547 res = generate_CALL(cctx, ufunc, argcount);
3548 goto theend;
3549 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551
3552 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003553 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003554 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003556 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003557 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003558 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003559 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003560 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003561
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003562 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003563 goto theend;
3564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565
Bram Moolenaar0f769812020-09-12 18:32:34 +02003566 // If we can find a global function by name generate the right call.
3567 if (ufunc != NULL)
3568 {
3569 res = generate_CALL(cctx, ufunc, argcount);
3570 goto theend;
3571 }
3572
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003573 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003574 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003575 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003576 res = generate_UCALL(cctx, name, argcount);
3577 else
3578 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003579
3580theend:
3581 vim_free(tofree);
3582 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583}
3584
3585// like NAMESPACE_CHAR but with 'a' and 'l'.
3586#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3587
3588/*
3589 * Find the end of a variable or function name. Unlike find_name_end() this
3590 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003591 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 * Return a pointer to just after the name. Equal to "arg" if there is no
3593 * valid name.
3594 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003595 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003596to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597{
3598 char_u *p;
3599
3600 // Quick check for valid starting character.
3601 if (!eval_isnamec1(*arg))
3602 return arg;
3603
3604 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3605 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3606 // and can be used in slice "[n:]".
3607 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003608 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3610 break;
3611 return p;
3612}
3613
3614/*
3615 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003616 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003617 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 */
3619 char_u *
3620to_name_const_end(char_u *arg)
3621{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003622 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 typval_T rettv;
3624
Bram Moolenaar678b2072021-07-26 21:10:11 +02003625 if (STRNCMP(p, "<SNR>", 5) == 0)
3626 p = skipdigits(p + 5);
3627 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 if (p == arg && *arg == '[')
3629 {
3630
3631 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 p = arg;
3634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 return p;
3636}
3637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638/*
3639 * parse a list: [expr, expr]
3640 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003641 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 */
3643 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003644compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645{
3646 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003647 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003649 int is_const;
3650 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003652 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003654 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003655 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003656 semsg(_(e_list_end), *arg);
3657 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003658 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003659 if (*p == ',')
3660 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003661 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003662 return FAIL;
3663 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003664 if (*p == ']')
3665 {
3666 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003667 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003668 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003669 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003670 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003671 if (!is_const)
3672 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 ++count;
3674 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003675 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003677 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3678 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003679 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003680 return FAIL;
3681 }
3682 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003683 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 p = skipwhite(p);
3685 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003686 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003688 ppconst->pp_is_const = is_all_const;
3689 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690}
3691
3692/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003693 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003694 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003695 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 */
3697 static int
3698compile_lambda(char_u **arg, cctx_T *cctx)
3699{
Bram Moolenaare462f522020-12-27 14:43:30 +01003700 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 typval_T rettv;
3702 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003703 evalarg_T evalarg;
3704
3705 CLEAR_FIELD(evalarg);
3706 evalarg.eval_flags = EVAL_EVALUATE;
3707 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
3709 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003710 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3711 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003712 {
3713 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003714 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003715 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003716
Bram Moolenaar65c44152020-12-24 15:14:01 +01003717 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003719 ++ufunc->uf_refcount;
3720 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721
Bram Moolenaara9931532021-06-12 15:58:16 +02003722 // Compile it here to get the return type. The return type is optional,
3723 // when it's missing use t_unknown. This is recognized in
3724 // compile_return().
3725 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3726 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003727 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728
Bram Moolenaar648594e2021-07-11 17:55:01 +02003729#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003730 // When the outer function is compiled for profiling, the lambda may be
3731 // called without profiling. Compile it here in the right context.
3732 if (cctx->ctx_compile_type == CT_PROFILE)
3733 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003734#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003735
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003736 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3737 // points into it. Point to the original line to avoid a dangling pointer.
3738 if (evalarg.eval_tofree_cmdline != NULL)
3739 {
3740 size_t off = *arg - evalarg.eval_tofree_cmdline;
3741
3742 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3743 + off;
3744 }
3745
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003746 clear_evalarg(&evalarg, NULL);
3747
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003748 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003749 {
3750 // The return type will now be known.
3751 set_function_type(ufunc);
3752
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003753 // The function reference count will be 1. When the ISN_FUNCREF
3754 // instruction is deleted the reference count is decremented and the
3755 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003756 return generate_FUNCREF(cctx, ufunc);
3757 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003758
3759 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761}
3762
3763/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003764 * Get a lambda and compile it. Uses Vim9 syntax.
3765 */
3766 int
3767get_lambda_tv_and_compile(
3768 char_u **arg,
3769 typval_T *rettv,
3770 int types_optional,
3771 evalarg_T *evalarg)
3772{
3773 int r;
3774 ufunc_T *ufunc;
3775 int save_sc_version = current_sctx.sc_version;
3776
3777 // Get the funcref in "rettv".
3778 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3779 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3780 current_sctx.sc_version = save_sc_version;
3781 if (r != OK)
3782 return r;
3783
3784 // "rettv" will now be a partial referencing the function.
3785 ufunc = rettv->vval.v_partial->pt_func;
3786
3787 // Compile it here to get the return type. The return type is optional,
3788 // when it's missing use t_unknown. This is recognized in
3789 // compile_return().
3790 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3791 ufunc->uf_ret_type = &t_unknown;
3792 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3793
3794 if (ufunc->uf_def_status == UF_COMPILED)
3795 {
3796 // The return type will now be known.
3797 set_function_type(ufunc);
3798 return OK;
3799 }
3800 clear_tv(rettv);
3801 return FAIL;
3802}
3803
3804/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003805 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003807 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 */
3809 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003810compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811{
3812 garray_T *instr = &cctx->ctx_instr;
3813 int count = 0;
3814 dict_T *d = dict_alloc();
3815 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003816 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003817 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003818 int is_const;
3819 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820
3821 if (d == NULL)
3822 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003823 if (generate_ppconst(cctx, ppconst) == FAIL)
3824 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003825 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003827 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
Bram Moolenaar23c55272020-06-21 16:58:13 +02003829 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003830 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003831 *arg = NULL;
3832 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003833 }
3834
3835 if (**arg == '}')
3836 break;
3837
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003838 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003840 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003842 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003843 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003847 if (isn->isn_type == ISN_PUSHNR)
3848 {
3849 char buf[NUMBUFLEN];
3850
3851 // Convert to string at compile time.
3852 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3853 isn->isn_type = ISN_PUSHS;
3854 isn->isn_arg.string = vim_strsave((char_u *)buf);
3855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 if (isn->isn_type == ISN_PUSHS)
3857 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003858 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003859 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003860 *arg = skipwhite(*arg);
3861 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003862 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003863 emsg(_(e_missing_matching_bracket_after_dict_key));
3864 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003865 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003866 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003868 else
3869 {
3870 // {"name": value},
3871 // {'name': value},
3872 // {name: value} use "name" as a literal key
3873 key = get_literal_key(arg);
3874 if (key == NULL)
3875 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003876 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003877 return FAIL;
3878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
3880 // Check for duplicate keys, if using string keys.
3881 if (key != NULL)
3882 {
3883 item = dict_find(d, key, -1);
3884 if (item != NULL)
3885 {
3886 semsg(_(e_duplicate_key), key);
3887 goto failret;
3888 }
3889 item = dictitem_alloc(key);
3890 if (item != NULL)
3891 {
3892 item->di_tv.v_type = VAR_UNKNOWN;
3893 item->di_tv.v_lock = 0;
3894 if (dict_add(d, item) == FAIL)
3895 dictitem_free(item);
3896 }
3897 }
3898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 if (**arg != ':')
3900 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003901 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003902 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003903 else
3904 semsg(_(e_missing_dict_colon), *arg);
3905 return FAIL;
3906 }
3907 whitep = *arg + 1;
3908 if (!IS_WHITE_OR_NUL(*whitep))
3909 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003910 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 return FAIL;
3912 }
3913
Bram Moolenaar23c55272020-06-21 16:58:13 +02003914 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003915 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003916 *arg = NULL;
3917 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003918 }
3919
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003920 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003922 if (!is_const)
3923 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 ++count;
3925
Bram Moolenaar2c330432020-04-13 14:41:35 +02003926 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003927 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003928 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003929 *arg = NULL;
3930 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 if (**arg == '}')
3933 break;
3934 if (**arg != ',')
3935 {
3936 semsg(_(e_missing_dict_comma), *arg);
3937 goto failret;
3938 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003939 if (IS_WHITE_OR_NUL(*whitep))
3940 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003941 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003942 return FAIL;
3943 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003944 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003945 if (!IS_WHITE_OR_NUL(*whitep))
3946 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003947 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003948 return FAIL;
3949 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003950 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 }
3952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 *arg = *arg + 1;
3954
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003955 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003956 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003957 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003958 *arg += STRLEN(*arg);
3959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003961 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 return generate_NEWDICT(cctx, count);
3963
3964failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003965 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003966 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003967 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003968 *arg = (char_u *)"";
3969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 dict_unref(d);
3971 return FAIL;
3972}
3973
3974/*
3975 * Compile "&option".
3976 */
3977 static int
3978compile_get_option(char_u **arg, cctx_T *cctx)
3979{
3980 typval_T rettv;
3981 char_u *start = *arg;
3982 int ret;
3983
3984 // parse the option and get the current value to get the type.
3985 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003986 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 if (ret == OK)
3988 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003989 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003990 char_u *name = vim_strnsave(start, *arg - start);
3991 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3992 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
3994 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3995 vim_free(name);
3996 }
3997 clear_tv(&rettv);
3998
3999 return ret;
4000}
4001
4002/*
4003 * Compile "$VAR".
4004 */
4005 static int
4006compile_get_env(char_u **arg, cctx_T *cctx)
4007{
4008 char_u *start = *arg;
4009 int len;
4010 int ret;
4011 char_u *name;
4012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 ++*arg;
4014 len = get_env_len(arg);
4015 if (len == 0)
4016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004017 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return FAIL;
4019 }
4020
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004021 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 name = vim_strnsave(start, len + 1);
4023 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4024 vim_free(name);
4025 return ret;
4026}
4027
4028/*
4029 * Compile "@r".
4030 */
4031 static int
4032compile_get_register(char_u **arg, cctx_T *cctx)
4033{
4034 int ret;
4035
4036 ++*arg;
4037 if (**arg == NUL)
4038 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004039 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 return FAIL;
4041 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004042 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 emsg_invreg(**arg);
4045 return FAIL;
4046 }
4047 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4048 ++*arg;
4049 return ret;
4050}
4051
4052/*
4053 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004054 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 */
4056 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004057apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004059 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060
4061 // this works from end to start
4062 while (p > start)
4063 {
4064 --p;
4065 if (*p == '-' || *p == '+')
4066 {
4067 // only '-' has an effect, for '+' we only check the type
4068#ifdef FEAT_FLOAT
4069 if (rettv->v_type == VAR_FLOAT)
4070 {
4071 if (*p == '-')
4072 rettv->vval.v_float = -rettv->vval.v_float;
4073 }
4074 else
4075#endif
4076 {
4077 varnumber_T val;
4078 int error = FALSE;
4079
4080 // tv_get_number_chk() accepts a string, but we don't want that
4081 // here
4082 if (check_not_string(rettv) == FAIL)
4083 return FAIL;
4084 val = tv_get_number_chk(rettv, &error);
4085 clear_tv(rettv);
4086 if (error)
4087 return FAIL;
4088 if (*p == '-')
4089 val = -val;
4090 rettv->v_type = VAR_NUMBER;
4091 rettv->vval.v_number = val;
4092 }
4093 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004094 else if (numeric_only)
4095 {
4096 ++p;
4097 break;
4098 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004099 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 {
4101 int v = tv2bool(rettv);
4102
4103 // '!' is permissive in the type.
4104 clear_tv(rettv);
4105 rettv->v_type = VAR_BOOL;
4106 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4107 }
4108 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004109 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 return OK;
4111}
4112
4113/*
4114 * Recognize v: variables that are constants and set "rettv".
4115 */
4116 static void
4117get_vim_constant(char_u **arg, typval_T *rettv)
4118{
4119 if (STRNCMP(*arg, "v:true", 6) == 0)
4120 {
4121 rettv->v_type = VAR_BOOL;
4122 rettv->vval.v_number = VVAL_TRUE;
4123 *arg += 6;
4124 }
4125 else if (STRNCMP(*arg, "v:false", 7) == 0)
4126 {
4127 rettv->v_type = VAR_BOOL;
4128 rettv->vval.v_number = VVAL_FALSE;
4129 *arg += 7;
4130 }
4131 else if (STRNCMP(*arg, "v:null", 6) == 0)
4132 {
4133 rettv->v_type = VAR_SPECIAL;
4134 rettv->vval.v_number = VVAL_NULL;
4135 *arg += 6;
4136 }
4137 else if (STRNCMP(*arg, "v:none", 6) == 0)
4138 {
4139 rettv->v_type = VAR_SPECIAL;
4140 rettv->vval.v_number = VVAL_NONE;
4141 *arg += 6;
4142 }
4143}
4144
Bram Moolenaar657137c2021-01-09 15:45:23 +01004145 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004146get_compare_type(char_u *p, int *len, int *type_is)
4147{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004148 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004149 int i;
4150
4151 switch (p[0])
4152 {
4153 case '=': if (p[1] == '=')
4154 type = EXPR_EQUAL;
4155 else if (p[1] == '~')
4156 type = EXPR_MATCH;
4157 break;
4158 case '!': if (p[1] == '=')
4159 type = EXPR_NEQUAL;
4160 else if (p[1] == '~')
4161 type = EXPR_NOMATCH;
4162 break;
4163 case '>': if (p[1] != '=')
4164 {
4165 type = EXPR_GREATER;
4166 *len = 1;
4167 }
4168 else
4169 type = EXPR_GEQUAL;
4170 break;
4171 case '<': if (p[1] != '=')
4172 {
4173 type = EXPR_SMALLER;
4174 *len = 1;
4175 }
4176 else
4177 type = EXPR_SEQUAL;
4178 break;
4179 case 'i': if (p[1] == 's')
4180 {
4181 // "is" and "isnot"; but not a prefix of a name
4182 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4183 *len = 5;
4184 i = p[*len];
4185 if (!isalnum(i) && i != '_')
4186 {
4187 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4188 *type_is = TRUE;
4189 }
4190 }
4191 break;
4192 }
4193 return type;
4194}
4195
4196/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004197 * Skip over an expression, ignoring most errors.
4198 */
4199 static void
4200skip_expr_cctx(char_u **arg, cctx_T *cctx)
4201{
4202 evalarg_T evalarg;
4203
4204 CLEAR_FIELD(evalarg);
4205 evalarg.eval_cctx = cctx;
4206 skip_expr(arg, &evalarg);
4207}
4208
4209/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004211 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 */
4213 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004214compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004216 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
4218 // this works from end to start
4219 while (p > start)
4220 {
4221 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004222 while (VIM_ISWHITE(*p))
4223 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (*p == '-' || *p == '+')
4225 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004226 int negate = *p == '-';
4227 isn_T *isn;
4228 garray_T *stack = &cctx->ctx_type_stack;
4229 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004231 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4232 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4233 return FAIL;
4234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4236 {
4237 --p;
4238 if (*p == '-')
4239 negate = !negate;
4240 }
4241 // only '-' has an effect, for '+' we only check the type
4242 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004243 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004245 if (isn == NULL)
4246 return FAIL;
4247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004249 else if (numeric_only)
4250 {
4251 ++p;
4252 break;
4253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 else
4255 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004256 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004258 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004260 if (p[-1] == '!')
4261 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004264 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 return FAIL;
4266 }
4267 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004268 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 return OK;
4270}
4271
4272/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004273 * Compile "(expression)": recursive!
4274 * Return FAIL/OK.
4275 */
4276 static int
4277compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4278{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004279 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004280 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004281
Bram Moolenaar24156692021-01-14 20:35:49 +01004282 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4283 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004284 if (ppconst->pp_used <= PPSIZE - 10)
4285 {
4286 ret = compile_expr1(arg, cctx, ppconst);
4287 }
4288 else
4289 {
4290 // Not enough space in ppconst, flush constants.
4291 if (generate_ppconst(cctx, ppconst) == FAIL)
4292 return FAIL;
4293 ret = compile_expr0(arg, cctx);
4294 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004295 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4296 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004297 if (**arg == ')')
4298 ++*arg;
4299 else if (ret == OK)
4300 {
4301 emsg(_(e_missing_close));
4302 ret = FAIL;
4303 }
4304 return ret;
4305}
4306
4307/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004309 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 */
4311 static int
4312compile_subscript(
4313 char_u **arg,
4314 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004315 char_u *start_leader,
4316 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004317 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004319 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004320 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 for (;;)
4323 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004324 char_u *p = skipwhite(*arg);
4325
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004326 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004327 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004328 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004329
4330 // If a following line starts with "->{" or "->X" advance to that
4331 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004332 // Also if a following line starts with ".x".
4333 if (next != NULL &&
4334 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004335 && (next[2] == '{'
4336 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004337 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004338 {
4339 next = next_line_from_context(cctx, TRUE);
4340 if (next == NULL)
4341 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004342 *arg = next;
4343 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004344 }
4345 }
4346
Bram Moolenaarcd268012021-07-22 19:11:08 +02004347 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4348 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004349 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004351 garray_T *stack = &cctx->ctx_type_stack;
4352 type_T *type;
4353 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004355 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004357 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004360 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4361
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004362 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004363 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004365 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004367 if (keeping_dict)
4368 {
4369 keeping_dict = FALSE;
4370 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4371 return FAIL;
4372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004374 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004376 char_u *pstart = p;
4377
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004378 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004379 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004380 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 // something->method()
4383 // Apply the '!', '-' and '+' first:
4384 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004385 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004388 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004389 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004390 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004391 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004392 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004393 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004394 garray_T *stack = &cctx->ctx_type_stack;
4395 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004396 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004397 int expr_isn_start = cctx->ctx_instr.ga_len;
4398 int expr_isn_end;
4399 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004400
4401 // Funcref call: list->(Refs[2])(arg)
4402 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004403 //
4404 // Fist compile the function expression.
4405 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004406 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004407
4408 // Remember the next instruction index, where the instructions
4409 // for arguments are being written.
4410 expr_isn_end = cctx->ctx_instr.ga_len;
4411
4412 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004413 if (**arg != '(')
4414 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004415 if (*skipwhite(*arg) == '(')
4416 emsg(_(e_nowhitespace));
4417 else
4418 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004419 return FAIL;
4420 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004421 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004422 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004423 return FAIL;
4424
Bram Moolenaar2927c072021-04-05 19:41:21 +02004425 // Move the instructions for the arguments to before the
4426 // instructions of the expression and move the type of the
4427 // expression after the argument types. This is what ISN_PCALL
4428 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004429 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004430 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4431 if (arg_isn_count > 0)
4432 {
4433 int expr_isn_count = expr_isn_end - expr_isn_start;
4434 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4435
4436 if (isn == NULL)
4437 return FAIL;
4438 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4439 + expr_isn_start,
4440 sizeof(isn_T) * expr_isn_count);
4441 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4442 + expr_isn_start,
4443 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4444 sizeof(isn_T) * arg_isn_count);
4445 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4446 + expr_isn_start + arg_isn_count,
4447 isn, sizeof(isn_T) * expr_isn_count);
4448 vim_free(isn);
4449
4450 type = ((type_T **)stack->ga_data)[type_idx_start];
4451 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4452 ((type_T **)stack->ga_data) + type_idx_start + 1,
4453 sizeof(type_T *)
4454 * (stack->ga_len - type_idx_start - 1));
4455 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4456 }
4457
Bram Moolenaar7e368202020-12-25 21:56:57 +01004458 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004459 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 return FAIL;
4461 }
4462 else
4463 {
4464 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004465 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004466 if (!eval_isnamec1(*p))
4467 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004468 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004469 return FAIL;
4470 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004471 if (ASCII_ISALPHA(*p) && p[1] == ':')
4472 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004473 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 ;
4475 if (*p != '(')
4476 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004477 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 return FAIL;
4479 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004480 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return FAIL;
4482 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004483 if (keeping_dict)
4484 {
4485 keeping_dict = FALSE;
4486 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4487 return FAIL;
4488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004490 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004492 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004493
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004494 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004495 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004496 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004497 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004498 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004499 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004500 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004501
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004502 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004503 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004504 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004505 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004506 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004507 // missing first index is equal to zero
4508 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004509 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004510 else
4511 {
4512 if (compile_expr0(arg, cctx) == FAIL)
4513 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004514 if (**arg == ':')
4515 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004516 semsg(_(e_white_space_required_before_and_after_str_at_str),
4517 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004518 return FAIL;
4519 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004520 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004521 return FAIL;
4522 *arg = skipwhite(*arg);
4523 }
4524 if (**arg == ':')
4525 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004526 is_slice = TRUE;
4527 ++*arg;
4528 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4529 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004530 semsg(_(e_white_space_required_before_and_after_str_at_str),
4531 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004532 return FAIL;
4533 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004534 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004535 return FAIL;
4536 if (**arg == ']')
4537 // missing second index is equal to end of string
4538 generate_PUSHNR(cctx, -1);
4539 else
4540 {
4541 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004542 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004543 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004544 return FAIL;
4545 *arg = skipwhite(*arg);
4546 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
4549 if (**arg != ']')
4550 {
4551 emsg(_(e_missbrac));
4552 return FAIL;
4553 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004554 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004556 if (keeping_dict)
4557 {
4558 keeping_dict = FALSE;
4559 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4560 return FAIL;
4561 }
4562 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004563 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004565 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004567 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004568 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004569 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004570 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004571
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004572 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004573 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004574 {
4575 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004576 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004577 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004578 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004579 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 while (eval_isnamec(*p))
4581 MB_PTR_ADV(p);
4582 if (p == *arg)
4583 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004584 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 return FAIL;
4586 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004587 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4588 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004589 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004591 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 *arg = p;
4593 }
4594 else
4595 break;
4596 }
4597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004599 // This needs to be done at runtime to be able to check the type.
4600 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4601 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602
4603 return OK;
4604}
4605
4606/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004607 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4608 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004610 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004611 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004612 *
4613 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 */
4615
4616/*
4617 * number number constant
4618 * 0zFFFFFFFF Blob constant
4619 * "string" string constant
4620 * 'string' literal string constant
4621 * &option-name option value
4622 * @r register contents
4623 * identifier variable value
4624 * function() function call
4625 * $VAR environment variable
4626 * (expression) nested expression
4627 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004628 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 *
4630 * Also handle:
4631 * ! in front logical NOT
4632 * - in front unary minus
4633 * + in front unary plus (ignored)
4634 * trailing (arg) funcref/partial call
4635 * trailing [] subscript in String or List
4636 * trailing .name entry in Dictionary
4637 * trailing ->name() method call
4638 */
4639 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004640compile_expr7(
4641 char_u **arg,
4642 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004643 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 char_u *start_leader, *end_leader;
4646 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004647 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004648 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004650 ppconst->pp_is_const = FALSE;
4651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 /*
4653 * Skip '!', '-' and '+' characters. They are handled later.
4654 */
4655 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004656 if (eval_leader(arg, TRUE) == FAIL)
4657 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 end_leader = *arg;
4659
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004660 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 switch (**arg)
4662 {
4663 /*
4664 * Number constant.
4665 */
4666 case '0': // also for blob starting with 0z
4667 case '1':
4668 case '2':
4669 case '3':
4670 case '4':
4671 case '5':
4672 case '6':
4673 case '7':
4674 case '8':
4675 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004676 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004678 // Apply "-" and "+" just before the number now, right to
4679 // left. Matters especially when "->" follows. Stops at
4680 // '!'.
4681 if (apply_leader(rettv, TRUE,
4682 start_leader, &end_leader) == FAIL)
4683 {
4684 clear_tv(rettv);
4685 return FAIL;
4686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 break;
4688
4689 /*
4690 * String constant: "string".
4691 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004692 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 return FAIL;
4694 break;
4695
4696 /*
4697 * Literal string constant: 'str''ing'.
4698 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004699 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 return FAIL;
4701 break;
4702
4703 /*
4704 * Constant Vim variable.
4705 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004706 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 ret = NOTDONE;
4708 break;
4709
4710 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 * "true" constant
4712 */
4713 case 't': if (STRNCMP(*arg, "true", 4) == 0
4714 && !eval_isnamec((*arg)[4]))
4715 {
4716 *arg += 4;
4717 rettv->v_type = VAR_BOOL;
4718 rettv->vval.v_number = VVAL_TRUE;
4719 }
4720 else
4721 ret = NOTDONE;
4722 break;
4723
4724 /*
4725 * "false" constant
4726 */
4727 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4728 && !eval_isnamec((*arg)[5]))
4729 {
4730 *arg += 5;
4731 rettv->v_type = VAR_BOOL;
4732 rettv->vval.v_number = VVAL_FALSE;
4733 }
4734 else
4735 ret = NOTDONE;
4736 break;
4737
4738 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004739 * "null" constant
4740 */
4741 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004742 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004743 {
4744 *arg += 4;
4745 rettv->v_type = VAR_SPECIAL;
4746 rettv->vval.v_number = VVAL_NULL;
4747 }
4748 else
4749 ret = NOTDONE;
4750 break;
4751
4752 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 * List: [expr, expr]
4754 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004755 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4756 return FAIL;
4757 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 break;
4759
4760 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 * Dictionary: {'key': val, 'key': val}
4762 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004763 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4764 return FAIL;
4765 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 break;
4767
4768 /*
4769 * Option value: &name
4770 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004771 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4772 return FAIL;
4773 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 break;
4775
4776 /*
4777 * Environment variable: $VAR.
4778 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004779 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4780 return FAIL;
4781 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 break;
4783
4784 /*
4785 * Register contents: @r.
4786 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004787 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4788 return FAIL;
4789 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 break;
4791 /*
4792 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004793 * lambda: (arg, arg) => expr
4794 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004796 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4797 ret = compile_lambda(arg, cctx);
4798 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004799 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 break;
4801
4802 default: ret = NOTDONE;
4803 break;
4804 }
4805 if (ret == FAIL)
4806 return FAIL;
4807
Bram Moolenaar1c747212020-05-09 18:28:34 +02004808 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004810 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004811 clear_tv(rettv);
4812 else
4813 // A constant expression can possibly be handled compile time,
4814 // return the value instead of generating code.
4815 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 }
4817 else if (ret == NOTDONE)
4818 {
4819 char_u *p;
4820 int r;
4821
4822 if (!eval_isnamec1(**arg))
4823 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004824 if (!vim9_bad_comment(*arg))
4825 {
4826 if (ends_excmd(*skipwhite(*arg)))
4827 semsg(_(e_empty_expression_str), *arg);
4828 else
4829 semsg(_(e_name_expected_str), *arg);
4830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 return FAIL;
4832 }
4833
4834 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004835 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004836 if (p - *arg == (size_t)1 && **arg == '_')
4837 {
4838 emsg(_(e_cannot_use_underscore_here));
4839 return FAIL;
4840 }
4841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004843 {
4844 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004847 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004848 if (cctx->ctx_skip != SKIP_YES
4849 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004851 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 if (r == FAIL)
4854 return FAIL;
4855 }
4856
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004857 // Handle following "[]", ".member", etc.
4858 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004859 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004860 ppconst) == FAIL)
4861 return FAIL;
4862 if (ppconst->pp_used > 0)
4863 {
4864 // apply the '!', '-' and '+' before the constant
4865 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004866 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004867 return FAIL;
4868 return OK;
4869 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004870 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004872 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873}
4874
4875/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004876 * Give the "white on both sides" error, taking the operator from "p[len]".
4877 */
4878 void
4879error_white_both(char_u *op, int len)
4880{
4881 char_u buf[10];
4882
4883 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004884 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004885}
4886
4887/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004888 * <type>expr7: runtime type check / conversion
4889 */
4890 static int
4891compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4892{
4893 type_T *want_type = NULL;
4894
4895 // Recognize <type>
4896 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4897 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004898 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004899 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4900 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004901 return FAIL;
4902
4903 if (**arg != '>')
4904 {
4905 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004906 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004907 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004908 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004909 return FAIL;
4910 }
4911 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004912 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004913 return FAIL;
4914 }
4915
4916 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4917 return FAIL;
4918
4919 if (want_type != NULL)
4920 {
4921 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004922 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004923 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004924
Bram Moolenaard1103582020-08-14 22:44:25 +02004925 generate_ppconst(cctx, ppconst);
4926 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004927 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004928 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004929 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004930 return FAIL;
4931 }
4932 }
4933
4934 return OK;
4935}
4936
4937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 * * number multiplication
4939 * / number division
4940 * % number modulo
4941 */
4942 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004943compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944{
4945 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004946 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004947 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004949 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004950 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return FAIL;
4952
4953 /*
4954 * Repeat computing, until no "*", "/" or "%" is following.
4955 */
4956 for (;;)
4957 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004958 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 if (*op != '*' && *op != '/' && *op != '%')
4960 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004961 if (next != NULL)
4962 {
4963 *arg = next_line_from_context(cctx, TRUE);
4964 op = skipwhite(*arg);
4965 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004966
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004967 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004969 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004972 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004973 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004975 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004976 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004978
4979 if (ppconst->pp_used == ppconst_used + 2
4980 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4981 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004982 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004983 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4984 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4985 varnumber_T res = 0;
4986 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004988 // both are numbers: compute the result
4989 switch (*op)
4990 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004991 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004992 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004993 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004994 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004995 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004996 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004997 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004998 break;
4999 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005000 if (failed)
5001 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005002 tv1->vval.v_number = res;
5003 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005004 }
5005 else
5006 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005007 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005008 generate_two_op(cctx, op);
5009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 }
5011
5012 return OK;
5013}
5014
5015/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005016 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 * - number subtraction
5018 * .. string concatenation
5019 */
5020 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005021compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022{
5023 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005024 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005026 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027
5028 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005029 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 return FAIL;
5031
5032 /*
5033 * Repeat computing, until no "+", "-" or ".." is following.
5034 */
5035 for (;;)
5036 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005037 op = may_peek_next_line(cctx, *arg, &next);
5038 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005040 if (op[0] == op[1] && *op != '.' && next)
5041 // Finding "++" or "--" on the next line is a separate command.
5042 // But ".." is concatenation.
5043 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005045 if (next != NULL)
5046 {
5047 *arg = next_line_from_context(cctx, TRUE);
5048 op = skipwhite(*arg);
5049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005051 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005053 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005054 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 }
5056
Bram Moolenaare0de1712020-12-02 17:36:54 +01005057 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005058 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005060 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005061 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 return FAIL;
5063
Bram Moolenaara5565e42020-05-09 15:44:01 +02005064 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005065 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005066 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5067 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5068 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5069 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5072 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005073
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005074 // concat/subtract/add constant numbers
5075 if (*op == '+')
5076 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5077 else if (*op == '-')
5078 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5079 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005080 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005081 // concatenate constant strings
5082 char_u *s1 = tv1->vval.v_string;
5083 char_u *s2 = tv2->vval.v_string;
5084 size_t len1 = STRLEN(s1);
5085
5086 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5087 if (tv1->vval.v_string == NULL)
5088 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005089 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005090 return FAIL;
5091 }
5092 mch_memmove(tv1->vval.v_string, s1, len1);
5093 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005094 vim_free(s1);
5095 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005096 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 }
5099 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005100 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005101 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005102 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005103 if (*op == '.')
5104 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005105 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5106 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005107 return FAIL;
5108 generate_instr_drop(cctx, ISN_CONCAT, 1);
5109 }
5110 else
5111 generate_two_op(cctx, op);
5112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 }
5114
5115 return OK;
5116}
5117
5118/*
5119 * expr5a == expr5b
5120 * expr5a =~ expr5b
5121 * expr5a != expr5b
5122 * expr5a !~ expr5b
5123 * expr5a > expr5b
5124 * expr5a >= expr5b
5125 * expr5a < expr5b
5126 * expr5a <= expr5b
5127 * expr5a is expr5b
5128 * expr5a isnot expr5b
5129 *
5130 * Produces instructions:
5131 * EVAL expr5a Push result of "expr5a"
5132 * EVAL expr5b Push result of "expr5b"
5133 * COMPARE one of the compare instructions
5134 */
5135 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005138 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005140 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144
5145 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005146 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 return FAIL;
5148
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005149 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005150 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151
5152 /*
5153 * If there is a comparative operator, use it.
5154 */
5155 if (type != EXPR_UNKNOWN)
5156 {
5157 int ic = FALSE; // Default: do not ignore case
5158
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005159 if (next != NULL)
5160 {
5161 *arg = next_line_from_context(cctx, TRUE);
5162 p = skipwhite(*arg);
5163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 if (type_is && (p[len] == '?' || p[len] == '#'))
5165 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005166 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 return FAIL;
5168 }
5169 // extra question mark appended: ignore case
5170 if (p[len] == '?')
5171 {
5172 ic = TRUE;
5173 ++len;
5174 }
5175 // extra '#' appended: match case (ignored)
5176 else if (p[len] == '#')
5177 ++len;
5178 // nothing appended: match case
5179
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005180 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005182 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005183 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 }
5185
5186 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005187 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005188 return FAIL;
5189
Bram Moolenaara5565e42020-05-09 15:44:01 +02005190 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 return FAIL;
5192
Bram Moolenaara5565e42020-05-09 15:44:01 +02005193 if (ppconst->pp_used == ppconst_used + 2)
5194 {
5195 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5196 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5197 int ret;
5198
5199 // Both sides are a constant, compute the result now.
5200 // First check for a valid combination of types, this is more
5201 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005202 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005203 ret = FAIL;
5204 else
5205 {
5206 ret = typval_compare(tv1, tv2, type, ic);
5207 tv1->v_type = VAR_BOOL;
5208 tv1->vval.v_number = tv1->vval.v_number
5209 ? VVAL_TRUE : VVAL_FALSE;
5210 clear_tv(tv2);
5211 --ppconst->pp_used;
5212 }
5213 return ret;
5214 }
5215
5216 generate_ppconst(cctx, ppconst);
5217 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219
5220 return OK;
5221}
5222
Bram Moolenaar7f141552020-05-09 17:35:53 +02005223static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225/*
5226 * Compile || or &&.
5227 */
5228 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005229compile_and_or(
5230 char_u **arg,
5231 cctx_T *cctx,
5232 char *op,
5233 ppconst_T *ppconst,
5234 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005236 char_u *next;
5237 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 int opchar = *op;
5239
5240 if (p[0] == opchar && p[1] == opchar)
5241 {
5242 garray_T *instr = &cctx->ctx_instr;
5243 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005244 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245
5246 /*
5247 * Repeat until there is no following "||" or "&&"
5248 */
5249 ga_init2(&end_ga, sizeof(int), 10);
5250 while (p[0] == opchar && p[1] == opchar)
5251 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005252 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005253 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005254 int start_ctx_lnum = cctx->ctx_lnum;
5255 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005256 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005257 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005258 jumpwhen_T jump_when = opchar == '|'
5259 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005260
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005261 if (next != NULL)
5262 {
5263 *arg = next_line_from_context(cctx, TRUE);
5264 p = skipwhite(*arg);
5265 }
5266
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005267 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5268 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005269 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005270 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005271 return FAIL;
5272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005274 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005275 SOURCING_LNUM = start_lnum;
5276 save_lnum = cctx->ctx_lnum;
5277 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005278
5279 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005280 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005281 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005282 // Use the last ppconst if possible.
5283 if (ppconst->pp_used > 0)
5284 {
5285 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5286 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005287
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005288 if ((is_true && opchar == '|')
5289 || (!is_true && opchar == '&'))
5290 {
5291 // For "false && expr" and "true || expr" the "expr"
5292 // does not need to be evaluated.
5293 cctx->ctx_skip = SKIP_YES;
5294 clear_tv(tv);
5295 tv->v_type = VAR_BOOL;
5296 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5297 }
5298 else
5299 {
5300 // For "true && expr" and "false || expr" only "expr"
5301 // needs to be evaluated.
5302 --ppconst->pp_used;
5303 jump_when = JUMP_NEVER;
5304 }
5305 }
5306 else
5307 {
5308 // Every part must evaluate to a bool.
5309 status = bool_on_stack(cctx);
5310 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005311 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005312 if (status != FAIL)
5313 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005314 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005315 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 {
5317 ga_clear(&end_ga);
5318 return FAIL;
5319 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005320
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005321 if (jump_when != JUMP_NEVER)
5322 {
5323 if (cctx->ctx_skip != SKIP_YES)
5324 {
5325 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5326 ++end_ga.ga_len;
5327 }
5328 generate_JUMP(cctx, jump_when, 0);
5329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330
5331 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005332 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005333 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005334 {
5335 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005336 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005337 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005338
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005339 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005340 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5341 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 {
5343 ga_clear(&end_ga);
5344 return FAIL;
5345 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005346
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005347 // "0 || 1" results in true, "1 && 0" results in false.
5348 if (ppconst->pp_used == const_used + 1)
5349 {
5350 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5351
5352 if (tv->v_type == VAR_NUMBER
5353 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5354 {
5355 tv->vval.v_number = tv->vval.v_number == 1
5356 ? VVAL_TRUE : VVAL_FALSE;
5357 tv->v_type = VAR_BOOL;
5358 }
5359 }
5360
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005361 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005363
5364 if (check_ppconst_bool(ppconst) == FAIL)
5365 {
5366 ga_clear(&end_ga);
5367 return FAIL;
5368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005370 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5371 // Every part must evaluate to a bool.
5372 if (bool_on_stack(cctx) == FAIL)
5373 {
5374 ga_clear(&end_ga);
5375 return FAIL;
5376 }
5377
5378 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005379 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005380 // Fill in the end label in all jumps.
5381 generate_ppconst(cctx, ppconst);
5382 while (end_ga.ga_len > 0)
5383 {
5384 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005385
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005386 --end_ga.ga_len;
5387 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005389 isn->isn_arg.jump.jump_where = instr->ga_len;
5390 }
5391 ga_clear(&end_ga);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005393
5394 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 }
5396
5397 return OK;
5398}
5399
5400/*
5401 * expr4a && expr4a && expr4a logical AND
5402 *
5403 * Produces instructions:
5404 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005405 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005406 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005408 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 * EVAL expr4c Push result of "expr4c"
5410 * end:
5411 */
5412 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005413compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005415 int ppconst_used = ppconst->pp_used;
5416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005418 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419 return FAIL;
5420
5421 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005422 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423}
5424
5425/*
5426 * expr3a || expr3b || expr3c logical OR
5427 *
5428 * Produces instructions:
5429 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005430 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005431 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005433 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 * EVAL expr3c Push result of "expr3c"
5435 * end:
5436 */
5437 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005438compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005440 int ppconst_used = ppconst->pp_used;
5441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005443 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 return FAIL;
5445
5446 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005447 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448}
5449
5450/*
5451 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005453 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 * JUMP_IF_FALSE alt jump if false
5455 * EVAL expr1a
5456 * JUMP_ALWAYS end
5457 * alt: EVAL expr1b
5458 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005459 *
5460 * Toplevel expression: expr2 ?? expr1
5461 * Produces instructions:
5462 * EVAL expr2 Push result of "expr2"
5463 * JUMP_AND_KEEP_IF_TRUE end jump if true
5464 * EVAL expr1
5465 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 */
5467 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005468compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469{
5470 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005471 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005472 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473
Bram Moolenaar3988f642020-08-27 22:43:03 +02005474 // Ignore all kinds of errors when not producing code.
5475 if (cctx->ctx_skip == SKIP_YES)
5476 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005477 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005478 return OK;
5479 }
5480
Bram Moolenaar61a89812020-05-07 16:58:17 +02005481 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 return FAIL;
5484
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005485 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 if (*p == '?')
5487 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005488 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 garray_T *instr = &cctx->ctx_instr;
5490 garray_T *stack = &cctx->ctx_type_stack;
5491 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005492 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005494 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005495 int has_const_expr = FALSE;
5496 int const_value = FALSE;
5497 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005499 if (next != NULL)
5500 {
5501 *arg = next_line_from_context(cctx, TRUE);
5502 p = skipwhite(*arg);
5503 }
5504
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005505 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005506 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005507 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005508 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005509 return FAIL;
5510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511
Bram Moolenaara5565e42020-05-09 15:44:01 +02005512 if (ppconst->pp_used == ppconst_used + 1)
5513 {
5514 // the condition is a constant, we know whether the ? or the :
5515 // expression is to be evaluated.
5516 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005517 if (op_falsy)
5518 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5519 else
5520 {
5521 int error = FALSE;
5522
5523 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5524 &error);
5525 if (error)
5526 return FAIL;
5527 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005528 cctx->ctx_skip = save_skip == SKIP_YES ||
5529 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5530
5531 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5532 // "left ?? right" and "left" is truthy: produce "left"
5533 generate_ppconst(cctx, ppconst);
5534 else
5535 {
5536 clear_tv(&ppconst->pp_tv[ppconst_used]);
5537 --ppconst->pp_used;
5538 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005539 }
5540 else
5541 {
5542 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005543 if (op_falsy)
5544 end_idx = instr->ga_len;
5545 generate_JUMP(cctx, op_falsy
5546 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5547 if (op_falsy)
5548 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550
5551 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005552 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005553 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005554 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005555 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556
Bram Moolenaara5565e42020-05-09 15:44:01 +02005557 if (!has_const_expr)
5558 {
5559 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005561 if (!op_falsy)
5562 {
5563 // remember the type and drop it
5564 --stack->ga_len;
5565 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005567 end_idx = instr->ga_len;
5568 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005569
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005570 // jump here from JUMP_IF_FALSE
5571 isn = ((isn_T *)instr->ga_data) + alt_idx;
5572 isn->isn_arg.jump.jump_where = instr->ga_len;
5573 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005575
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005576 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005578 // Check for the ":".
5579 p = may_peek_next_line(cctx, *arg, &next);
5580 if (*p != ':')
5581 {
5582 emsg(_(e_missing_colon));
5583 return FAIL;
5584 }
5585 if (next != NULL)
5586 {
5587 *arg = next_line_from_context(cctx, TRUE);
5588 p = skipwhite(*arg);
5589 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005590
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005591 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5592 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005593 semsg(_(e_white_space_required_before_and_after_str_at_str),
5594 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005595 return FAIL;
5596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005598 // evaluate the third expression
5599 if (has_const_expr)
5600 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005601 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005602 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005603 return FAIL;
5604 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5605 return FAIL;
5606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607
Bram Moolenaara5565e42020-05-09 15:44:01 +02005608 if (!has_const_expr)
5609 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005610 type_T **typep;
5611
Bram Moolenaara5565e42020-05-09 15:44:01 +02005612 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613
Bram Moolenaara5565e42020-05-09 15:44:01 +02005614 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005615 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5616 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005617
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005618 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005619 isn = ((isn_T *)instr->ga_data) + end_idx;
5620 isn->isn_arg.jump.jump_where = instr->ga_len;
5621 }
5622
5623 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624 }
5625 return OK;
5626}
5627
5628/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005629 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005630 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5631 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005632 */
5633 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005634compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635{
5636 ppconst_T ppconst;
5637
5638 CLEAR_FIELD(ppconst);
5639 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5640 {
5641 clear_ppconst(&ppconst);
5642 return FAIL;
5643 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005644 if (is_const != NULL)
5645 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005646 if (generate_ppconst(cctx, &ppconst) == FAIL)
5647 return FAIL;
5648 return OK;
5649}
5650
5651/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005652 * Toplevel expression.
5653 */
5654 static int
5655compile_expr0(char_u **arg, cctx_T *cctx)
5656{
5657 return compile_expr0_ext(arg, cctx, NULL);
5658}
5659
5660/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005661 * Compile "return [expr]".
5662 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 */
5664 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005665compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666{
5667 char_u *p = arg;
5668 garray_T *stack = &cctx->ctx_type_stack;
5669 type_T *stack_type;
5670
5671 if (*p != NUL && *p != '|' && *p != '\n')
5672 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005673 if (legacy)
5674 {
5675 int save_flags = cmdmod.cmod_flags;
5676
5677 generate_LEGACY_EVAL(cctx, p);
5678 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5679 0, cctx, FALSE, FALSE) == FAIL)
5680 return NULL;
5681 cmdmod.cmod_flags |= CMOD_LEGACY;
5682 (void)skip_expr(&p, NULL);
5683 cmdmod.cmod_flags = save_flags;
5684 }
5685 else
5686 {
5687 // compile return argument into instructions
5688 if (compile_expr0(&p, cctx) == FAIL)
5689 return NULL;
5690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005692 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005693 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005694 // "check_return_type" with uf_ret_type set to &t_unknown is used
5695 // for an inline function without a specified return type. Set the
5696 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005697 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005698 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005699 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5700 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005701 || (!check_return_type
5702 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005703 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005704 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005705 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005706 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005707 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005708 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5709 && stack_type->tt_type != VAR_VOID
5710 && stack_type->tt_type != VAR_UNKNOWN)
5711 {
5712 emsg(_(e_returning_value_in_function_without_return_type));
5713 return NULL;
5714 }
5715 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005716 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005717 return NULL;
5718 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720 }
5721 else
5722 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005723 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005724 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005725 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5726 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005728 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729 return NULL;
5730 }
5731
5732 // No argument, return zero.
5733 generate_PUSHNR(cctx, 0);
5734 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005735
5736 // Undo any command modifiers.
5737 generate_undo_cmdmods(cctx);
5738
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005739 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 return NULL;
5741
5742 // "return val | endif" is possible
5743 return skipwhite(p);
5744}
5745
5746/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005747 * Get a line from the compilation context, compatible with exarg_T getline().
5748 * Return a pointer to the line in allocated memory.
5749 * Return NULL for end-of-file or some error.
5750 */
5751 static char_u *
5752exarg_getline(
5753 int c UNUSED,
5754 void *cookie,
5755 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005756 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005757{
5758 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005759 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005760
Bram Moolenaar66250c92020-08-20 15:02:42 +02005761 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005762 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005763 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005764 return NULL;
5765 ++cctx->ctx_lnum;
5766 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5767 // Comment lines result in NULL pointers, skip them.
5768 if (p != NULL)
5769 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005770 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005771}
5772
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005773 void
5774fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5775{
5776 eap->getline = exarg_getline;
5777 eap->cookie = cctx;
5778}
5779
Bram Moolenaar04b12692020-05-04 23:24:44 +02005780/*
5781 * Compile a nested :def command.
5782 */
5783 static char_u *
5784compile_nested_function(exarg_T *eap, cctx_T *cctx)
5785{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005786 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005787 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005788 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005789 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005790 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005791 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005792 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005793
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005794 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005795 {
5796 emsg(_(e_cannot_use_bang_with_nested_def));
5797 return NULL;
5798 }
5799
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005800 if (*name_start == '/')
5801 {
5802 name_end = skip_regexp(name_start + 1, '/', TRUE);
5803 if (*name_end == '/')
5804 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005805 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005806 }
5807 if (name_end == name_start || *skipwhite(name_end) != '(')
5808 {
5809 if (!ends_excmd2(name_start, name_end))
5810 {
5811 semsg(_(e_invalid_command_str), eap->cmd);
5812 return NULL;
5813 }
5814
5815 // "def" or "def Name": list functions
5816 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5817 return NULL;
5818 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5819 }
5820
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005821 // Only g:Func() can use a namespace.
5822 if (name_start[1] == ':' && !is_global)
5823 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005824 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005825 return NULL;
5826 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005827 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005828 return NULL;
5829
Bram Moolenaar04b12692020-05-04 23:24:44 +02005830 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005831 fill_exarg_from_cctx(eap, cctx);
5832
Bram Moolenaar04b12692020-05-04 23:24:44 +02005833 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005834 lambda_name = vim_strsave(get_lambda_name());
5835 if (lambda_name == NULL)
5836 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005837 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005838
Bram Moolenaar822ba242020-05-24 23:00:18 +02005839 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005840 {
5841 r = eap->skip ? OK : FAIL;
5842 goto theend;
5843 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005844
5845 // copy over the block scope IDs before compiling
5846 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5847 {
5848 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5849
5850 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5851 if (ufunc->uf_block_ids != NULL)
5852 {
5853 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5854 sizeof(int) * block_depth);
5855 ufunc->uf_block_depth = block_depth;
5856 }
5857 }
5858
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005859 compile_type = COMPILE_TYPE(ufunc);
5860#ifdef FEAT_PROFILE
5861 // If the outer function is profiled, also compile the nested function for
5862 // profiling.
5863 if (cctx->ctx_compile_type == CT_PROFILE)
5864 compile_type = CT_PROFILE;
5865#endif
5866 if (func_needs_compiling(ufunc, compile_type)
5867 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005868 {
5869 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005870 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005871 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005872
Bram Moolenaar648594e2021-07-11 17:55:01 +02005873#ifdef FEAT_PROFILE
5874 // When the outer function is compiled for profiling, the nested function
5875 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005876 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005877 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5878#endif
5879
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005880 if (is_global)
5881 {
5882 char_u *func_name = vim_strnsave(name_start + 2,
5883 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005884
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005885 if (func_name == NULL)
5886 r = FAIL;
5887 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005888 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005889 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005890 lambda_name = NULL;
5891 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005892 }
5893 else
5894 {
5895 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005896 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005897 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005898
Bram Moolenaareef21022020-08-01 22:16:43 +02005899 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005900 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005901 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005902 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005903 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5904 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005905
5906theend:
5907 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005908 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005909}
5910
5911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 * Return the length of an assignment operator, or zero if there isn't one.
5913 */
5914 int
5915assignment_len(char_u *p, int *heredoc)
5916{
5917 if (*p == '=')
5918 {
5919 if (p[1] == '<' && p[2] == '<')
5920 {
5921 *heredoc = TRUE;
5922 return 3;
5923 }
5924 return 1;
5925 }
5926 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5927 return 2;
5928 if (STRNCMP(p, "..=", 3) == 0)
5929 return 3;
5930 return 0;
5931}
5932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005934 * Generate the load instruction for "name".
5935 */
5936 static void
5937generate_loadvar(
5938 cctx_T *cctx,
5939 assign_dest_T dest,
5940 char_u *name,
5941 lvar_T *lvar,
5942 type_T *type)
5943{
5944 switch (dest)
5945 {
5946 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005947 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5948 break;
5949 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005950 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5951 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5952 else
5953 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005954 break;
5955 case dest_buffer:
5956 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5957 break;
5958 case dest_window:
5959 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5960 break;
5961 case dest_tab:
5962 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5963 break;
5964 case dest_script:
5965 compile_load_scriptvar(cctx,
5966 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5967 break;
5968 case dest_env:
5969 // Include $ in the name here
5970 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5971 break;
5972 case dest_reg:
5973 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5974 break;
5975 case dest_vimvar:
5976 generate_LOADV(cctx, name + 2, TRUE);
5977 break;
5978 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005979 if (lvar->lv_from_outer > 0)
5980 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5981 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005982 else
5983 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5984 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005985 case dest_expr:
5986 // list or dict value should already be on the stack.
5987 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005988 }
5989}
5990
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005991/*
5992 * Skip over "[expr]" or ".member".
5993 * Does not check for any errors.
5994 */
5995 static char_u *
5996skip_index(char_u *start)
5997{
5998 char_u *p = start;
5999
6000 if (*p == '[')
6001 {
6002 p = skipwhite(p + 1);
6003 (void)skip_expr(&p, NULL);
6004 p = skipwhite(p);
6005 if (*p == ']')
6006 return p + 1;
6007 return p;
6008 }
6009 // if (*p == '.')
6010 return to_name_end(p + 1, TRUE);
6011}
6012
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006013 void
6014vim9_declare_error(char_u *name)
6015{
6016 char *scope = "";
6017
6018 switch (*name)
6019 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006020 case 'g': scope = _("global"); break;
6021 case 'b': scope = _("buffer"); break;
6022 case 'w': scope = _("window"); break;
6023 case 't': scope = _("tab"); break;
6024 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006025 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006026 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006027 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006028 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006029 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006030 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006031 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006032 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006033 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006034}
6035
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006036/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006037 * For one assignment figure out the type of destination. Return it in "dest".
6038 * When not recognized "dest" is not set.
6039 * For an option "opt_flags" is set.
6040 * For a v:var "vimvaridx" is set.
6041 * "type" is set to the destination type if known, unchanted otherwise.
6042 * Return FAIL if an error message was given.
6043 */
6044 static int
6045get_var_dest(
6046 char_u *name,
6047 assign_dest_T *dest,
6048 int cmdidx,
6049 int *opt_flags,
6050 int *vimvaridx,
6051 type_T **type,
6052 cctx_T *cctx)
6053{
6054 char_u *p;
6055
6056 if (*name == '&')
6057 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006058 int cc;
6059 long numval;
6060 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006061
6062 *dest = dest_option;
6063 if (cmdidx == CMD_final || cmdidx == CMD_const)
6064 {
6065 emsg(_(e_const_option));
6066 return FAIL;
6067 }
6068 p = name;
6069 p = find_option_end(&p, opt_flags);
6070 if (p == NULL)
6071 {
6072 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006073 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006074 return FAIL;
6075 }
6076 cc = *p;
6077 *p = NUL;
6078 opt_type = get_option_value(skip_option_env_lead(name),
6079 &numval, NULL, *opt_flags);
6080 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006081 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006082 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006083 case gov_unknown:
6084 semsg(_(e_unknown_option), name);
6085 return FAIL;
6086 case gov_string:
6087 case gov_hidden_string:
6088 *type = &t_string;
6089 break;
6090 case gov_bool:
6091 case gov_hidden_bool:
6092 *type = &t_bool;
6093 break;
6094 case gov_number:
6095 case gov_hidden_number:
6096 *type = &t_number;
6097 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006098 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006099 }
6100 else if (*name == '$')
6101 {
6102 *dest = dest_env;
6103 *type = &t_string;
6104 }
6105 else if (*name == '@')
6106 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006107 if (name[1] != '@'
6108 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006109 {
6110 emsg_invreg(name[1]);
6111 return FAIL;
6112 }
6113 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006114 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006115 }
6116 else if (STRNCMP(name, "g:", 2) == 0)
6117 {
6118 *dest = dest_global;
6119 }
6120 else if (STRNCMP(name, "b:", 2) == 0)
6121 {
6122 *dest = dest_buffer;
6123 }
6124 else if (STRNCMP(name, "w:", 2) == 0)
6125 {
6126 *dest = dest_window;
6127 }
6128 else if (STRNCMP(name, "t:", 2) == 0)
6129 {
6130 *dest = dest_tab;
6131 }
6132 else if (STRNCMP(name, "v:", 2) == 0)
6133 {
6134 typval_T *vtv;
6135 int di_flags;
6136
6137 *vimvaridx = find_vim_var(name + 2, &di_flags);
6138 if (*vimvaridx < 0)
6139 {
6140 semsg(_(e_variable_not_found_str), name);
6141 return FAIL;
6142 }
6143 // We use the current value of "sandbox" here, is that OK?
6144 if (var_check_ro(di_flags, name, FALSE))
6145 return FAIL;
6146 *dest = dest_vimvar;
6147 vtv = get_vim_var_tv(*vimvaridx);
6148 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6149 }
6150 return OK;
6151}
6152
6153/*
6154 * Generate a STORE instruction for "dest", not being "dest_local".
6155 * Return FAIL when out of memory.
6156 */
6157 static int
6158generate_store_var(
6159 cctx_T *cctx,
6160 assign_dest_T dest,
6161 int opt_flags,
6162 int vimvaridx,
6163 int scriptvar_idx,
6164 int scriptvar_sid,
6165 type_T *type,
6166 char_u *name)
6167{
6168 switch (dest)
6169 {
6170 case dest_option:
6171 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6172 opt_flags);
6173 case dest_global:
6174 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006175 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6176 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006177 case dest_buffer:
6178 // include b: with the name, easier to execute that way
6179 return generate_STORE(cctx, ISN_STOREB, 0, name);
6180 case dest_window:
6181 // include w: with the name, easier to execute that way
6182 return generate_STORE(cctx, ISN_STOREW, 0, name);
6183 case dest_tab:
6184 // include t: with the name, easier to execute that way
6185 return generate_STORE(cctx, ISN_STORET, 0, name);
6186 case dest_env:
6187 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6188 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006189 return generate_STORE(cctx, ISN_STOREREG,
6190 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006191 case dest_vimvar:
6192 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6193 case dest_script:
6194 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006195 // "s:" may be included in the name.
6196 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006197 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006198 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6199 scriptvar_sid, scriptvar_idx, type);
6200 case dest_local:
6201 case dest_expr:
6202 // cannot happen
6203 break;
6204 }
6205 return FAIL;
6206}
6207
Bram Moolenaar752fc692021-01-04 21:57:11 +01006208 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006209generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6210{
6211 if (lhs->lhs_dest != dest_local)
6212 return generate_store_var(cctx, lhs->lhs_dest,
6213 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6214 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6215 lhs->lhs_type, lhs->lhs_name);
6216
6217 if (lhs->lhs_lvar != NULL)
6218 {
6219 garray_T *instr = &cctx->ctx_instr;
6220 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6221
6222 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6223 // ISN_STORENR
6224 if (lhs->lhs_lvar->lv_from_outer == 0
6225 && instr->ga_len == instr_count + 1
6226 && isn->isn_type == ISN_PUSHNR)
6227 {
6228 varnumber_T val = isn->isn_arg.number;
6229 garray_T *stack = &cctx->ctx_type_stack;
6230
6231 isn->isn_type = ISN_STORENR;
6232 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6233 isn->isn_arg.storenr.stnr_val = val;
6234 if (stack->ga_len > 0)
6235 --stack->ga_len;
6236 }
6237 else if (lhs->lhs_lvar->lv_from_outer > 0)
6238 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6239 lhs->lhs_lvar->lv_from_outer);
6240 else
6241 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6242 }
6243 return OK;
6244}
6245
6246 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247is_decl_command(int cmdidx)
6248{
6249 return cmdidx == CMD_let || cmdidx == CMD_var
6250 || cmdidx == CMD_final || cmdidx == CMD_const;
6251}
6252
6253/*
6254 * Figure out the LHS type and other properties for an assignment or one item
6255 * of ":unlet" with an index.
6256 * Returns OK or FAIL.
6257 */
6258 static int
6259compile_lhs(
6260 char_u *var_start,
6261 lhs_T *lhs,
6262 int cmdidx,
6263 int heredoc,
6264 int oplen,
6265 cctx_T *cctx)
6266{
6267 char_u *var_end;
6268 int is_decl = is_decl_command(cmdidx);
6269
6270 CLEAR_POINTER(lhs);
6271 lhs->lhs_dest = dest_local;
6272 lhs->lhs_vimvaridx = -1;
6273 lhs->lhs_scriptvar_idx = -1;
6274
6275 // "dest_end" is the end of the destination, including "[expr]" or
6276 // ".name".
6277 // "var_end" is the end of the variable/option/etc. name.
6278 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6279 if (*var_start == '@')
6280 var_end = var_start + 2;
6281 else
6282 {
6283 // skip over the leading "&", "&l:", "&g:" and "$"
6284 var_end = skip_option_env_lead(var_start);
6285 var_end = to_name_end(var_end, TRUE);
6286 }
6287
6288 // "a: type" is declaring variable "a" with a type, not dict "a:".
6289 if (is_decl && lhs->lhs_dest_end == var_start + 2
6290 && lhs->lhs_dest_end[-1] == ':')
6291 --lhs->lhs_dest_end;
6292 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6293 --var_end;
6294
6295 // compute the length of the destination without "[expr]" or ".name"
6296 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006297 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006298 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6299 if (lhs->lhs_name == NULL)
6300 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006301
6302 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6303 // Something follows after the variable: "var[idx]" or "var.key".
6304 lhs->lhs_has_index = TRUE;
6305
Bram Moolenaar752fc692021-01-04 21:57:11 +01006306 if (heredoc)
6307 lhs->lhs_type = &t_list_string;
6308 else
6309 lhs->lhs_type = &t_any;
6310
6311 if (cctx->ctx_skip != SKIP_YES)
6312 {
6313 int declare_error = FALSE;
6314
6315 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6316 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6317 &lhs->lhs_type, cctx) == FAIL)
6318 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006319 if (lhs->lhs_dest != dest_local
6320 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 {
6322 // Specific kind of variable recognized.
6323 declare_error = is_decl;
6324 }
6325 else
6326 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006327 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006328 if (check_reserved_name(lhs->lhs_name) == FAIL)
6329 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330
6331 if (lookup_local(var_start, lhs->lhs_varlen,
6332 &lhs->lhs_local_lvar, cctx) == OK)
6333 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6334 else
6335 {
6336 CLEAR_FIELD(lhs->lhs_arg_lvar);
6337 if (arg_exists(var_start, lhs->lhs_varlen,
6338 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6339 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6340 {
6341 if (is_decl)
6342 {
6343 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6344 return FAIL;
6345 }
6346 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6347 }
6348 }
6349 if (lhs->lhs_lvar != NULL)
6350 {
6351 if (is_decl)
6352 {
6353 semsg(_(e_variable_already_declared), lhs->lhs_name);
6354 return FAIL;
6355 }
6356 }
6357 else
6358 {
6359 int script_namespace = lhs->lhs_varlen > 1
6360 && STRNCMP(var_start, "s:", 2) == 0;
6361 int script_var = (script_namespace
6362 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006363 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006365 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 imported_T *import =
6367 find_imported(var_start, lhs->lhs_varlen, cctx);
6368
6369 if (script_namespace || script_var || import != NULL)
6370 {
6371 char_u *rawname = lhs->lhs_name
6372 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6373
6374 if (is_decl)
6375 {
6376 if (script_namespace)
6377 semsg(_(e_cannot_declare_script_variable_in_function),
6378 lhs->lhs_name);
6379 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006380 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 lhs->lhs_name);
6382 return FAIL;
6383 }
6384 else if (cctx->ctx_ufunc->uf_script_ctx_version
6385 == SCRIPT_VERSION_VIM9
6386 && script_namespace
6387 && !script_var && import == NULL)
6388 {
6389 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6390 return FAIL;
6391 }
6392
6393 lhs->lhs_dest = dest_script;
6394
6395 // existing script-local variables should have a type
6396 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6397 if (import != NULL)
6398 lhs->lhs_scriptvar_sid = import->imp_sid;
6399 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6400 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006401 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006403 lhs->lhs_scriptvar_sid, rawname,
6404 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6405 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006406 if (lhs->lhs_scriptvar_idx >= 0)
6407 {
6408 scriptitem_T *si = SCRIPT_ITEM(
6409 lhs->lhs_scriptvar_sid);
6410 svar_T *sv =
6411 ((svar_T *)si->sn_var_vals.ga_data)
6412 + lhs->lhs_scriptvar_idx;
6413 lhs->lhs_type = sv->sv_type;
6414 }
6415 }
6416 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006417 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 == FAIL)
6419 return FAIL;
6420 }
6421 }
6422
6423 if (declare_error)
6424 {
6425 vim9_declare_error(lhs->lhs_name);
6426 return FAIL;
6427 }
6428 }
6429
6430 // handle "a:name" as a name, not index "name" on "a"
6431 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6432 var_end = lhs->lhs_dest_end;
6433
6434 if (lhs->lhs_dest != dest_option)
6435 {
6436 if (is_decl && *var_end == ':')
6437 {
6438 char_u *p;
6439
6440 // parse optional type: "let var: type = expr"
6441 if (!VIM_ISWHITE(var_end[1]))
6442 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006443 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006444 return FAIL;
6445 }
6446 p = skipwhite(var_end + 1);
6447 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6448 if (lhs->lhs_type == NULL)
6449 return FAIL;
6450 lhs->lhs_has_type = TRUE;
6451 }
6452 else if (lhs->lhs_lvar != NULL)
6453 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6454 }
6455
Bram Moolenaare42939a2021-04-05 17:11:17 +02006456 if (oplen == 3 && !heredoc
6457 && lhs->lhs_dest != dest_global
6458 && !lhs->lhs_has_index
6459 && lhs->lhs_type->tt_type != VAR_STRING
6460 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 {
6462 emsg(_(e_can_only_concatenate_to_string));
6463 return FAIL;
6464 }
6465
6466 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6467 && cctx->ctx_skip != SKIP_YES)
6468 {
6469 if (oplen > 1 && !heredoc)
6470 {
6471 // +=, /=, etc. require an existing variable
6472 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6473 return FAIL;
6474 }
6475 if (!is_decl)
6476 {
6477 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6478 return FAIL;
6479 }
6480
Bram Moolenaar3f327882021-03-17 20:56:38 +01006481 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 if ((lhs->lhs_type->tt_type == VAR_FUNC
6483 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006484 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006485 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006486
6487 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6489 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6490 if (lhs->lhs_lvar == NULL)
6491 return FAIL;
6492 lhs->lhs_new_local = TRUE;
6493 }
6494
6495 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006496 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497 {
6498 // Something follows after the variable: "var[idx]" or "var.key".
6499 // TODO: should we also handle "->func()" here?
6500 if (is_decl)
6501 {
6502 emsg(_(e_cannot_use_index_when_declaring_variable));
6503 return FAIL;
6504 }
6505
6506 if (var_start[lhs->lhs_varlen] == '['
6507 || var_start[lhs->lhs_varlen] == '.')
6508 {
6509 char_u *after = var_start + lhs->lhs_varlen;
6510 char_u *p;
6511
6512 // Only the last index is used below, if there are others
6513 // before it generate code for the expression. Thus for
6514 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6515 for (;;)
6516 {
6517 p = skip_index(after);
6518 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006519 {
6520 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006522 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006523 after = p;
6524 }
6525 if (after > var_start + lhs->lhs_varlen)
6526 {
6527 lhs->lhs_varlen = after - var_start;
6528 lhs->lhs_dest = dest_expr;
6529 // We don't know the type before evaluating the expression,
6530 // use "any" until then.
6531 lhs->lhs_type = &t_any;
6532 }
6533
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 if (lhs->lhs_type->tt_member == NULL)
6535 lhs->lhs_member_type = &t_any;
6536 else
6537 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6538 }
6539 else
6540 {
6541 semsg("Not supported yet: %s", var_start);
6542 return FAIL;
6543 }
6544 }
6545 return OK;
6546}
6547
6548/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006549 * Figure out the LHS and check a few errors.
6550 */
6551 static int
6552compile_assign_lhs(
6553 char_u *var_start,
6554 lhs_T *lhs,
6555 int cmdidx,
6556 int is_decl,
6557 int heredoc,
6558 int oplen,
6559 cctx_T *cctx)
6560{
6561 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6562 return FAIL;
6563
6564 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6565 {
6566 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6567 return FAIL;
6568 }
6569 if (!is_decl && lhs->lhs_lvar != NULL
6570 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6571 {
6572 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6573 return FAIL;
6574 }
6575 return OK;
6576}
6577
6578/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006579 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6580 */
6581 static int
6582has_list_index(char_u *idx_start, cctx_T *cctx)
6583{
6584 char_u *p = idx_start;
6585 int save_skip;
6586
6587 if (*p != '[')
6588 return FALSE;
6589
6590 p = skipwhite(p + 1);
6591 if (*p == ':')
6592 return TRUE;
6593
6594 save_skip = cctx->ctx_skip;
6595 cctx->ctx_skip = SKIP_YES;
6596 (void)compile_expr0(&p, cctx);
6597 cctx->ctx_skip = save_skip;
6598 return *skipwhite(p) == ':';
6599}
6600
6601/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006602 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6603 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006604 */
6605 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006606compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006607 char_u *var_start,
6608 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006609 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006610 cctx_T *cctx)
6611{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006613 char_u *p;
6614 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006615 int need_white_before = TRUE;
6616 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006617
Bram Moolenaar752fc692021-01-04 21:57:11 +01006618 p = var_start + varlen;
6619 if (*p == '[')
6620 {
6621 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006622 if (*p == ':')
6623 {
6624 // empty first index, push zero
6625 r = generate_PUSHNR(cctx, 0);
6626 need_white_before = FALSE;
6627 }
6628 else
6629 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006630
6631 if (r == OK && *skipwhite(p) == ':')
6632 {
6633 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006634 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006635 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006636 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006637 empty_second = *skipwhite(p + 1) == ']';
6638 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6639 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006640 {
6641 semsg(_(e_white_space_required_before_and_after_str_at_str),
6642 ":", p);
6643 return FAIL;
6644 }
6645 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006646 if (*p == ']')
6647 // empty second index, push "none"
6648 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6649 else
6650 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006651 }
6652
Bram Moolenaar752fc692021-01-04 21:57:11 +01006653 if (r == OK && *skipwhite(p) != ']')
6654 {
6655 // this should not happen
6656 emsg(_(e_missbrac));
6657 r = FAIL;
6658 }
6659 }
6660 else // if (*p == '.')
6661 {
6662 char_u *key_end = to_name_end(p + 1, TRUE);
6663 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6664
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006665 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006667 return r;
6668}
6669
6670/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006671 * For a LHS with an index, load the variable to be indexed.
6672 */
6673 static int
6674compile_load_lhs(
6675 lhs_T *lhs,
6676 char_u *var_start,
6677 type_T *rhs_type,
6678 cctx_T *cctx)
6679{
6680 if (lhs->lhs_dest == dest_expr)
6681 {
6682 size_t varlen = lhs->lhs_varlen;
6683 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006684 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006685 char_u *p = var_start;
6686 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006687 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006688
Bram Moolenaare97976b2021-08-01 13:17:17 +02006689 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6690 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006691 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006692 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6693 res = compile_expr0(&p, cctx);
6694 var_start[varlen] = c;
6695 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6696 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006697 {
6698 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006699 if (res != FAIL)
6700 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006701 return FAIL;
6702 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006703
6704 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6705 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6706 // now we can properly check the type
6707 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6708 && rhs_type != &t_void
6709 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6710 FALSE, FALSE) == FAIL)
6711 return FAIL;
6712 }
6713 else
6714 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6715 lhs->lhs_lvar, lhs->lhs_type);
6716 return OK;
6717}
6718
6719/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006720 * Produce code for loading "lhs" and also take care of an index.
6721 * Return OK/FAIL.
6722 */
6723 static int
6724compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6725{
6726 compile_load_lhs(lhs, var_start, NULL, cctx);
6727
6728 if (lhs->lhs_has_index)
6729 {
6730 int range = FALSE;
6731
6732 // Get member from list or dict. First compile the
6733 // index value.
6734 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6735 return FAIL;
6736 if (range)
6737 {
6738 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6739 var_start);
6740 return FAIL;
6741 }
6742
6743 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006744 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006745 return FAIL;
6746 }
6747 return OK;
6748}
6749
6750/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006751 * Assignment to a list or dict member, or ":unlet" for the item, using the
6752 * information in "lhs".
6753 * Returns OK or FAIL.
6754 */
6755 static int
6756compile_assign_unlet(
6757 char_u *var_start,
6758 lhs_T *lhs,
6759 int is_assign,
6760 type_T *rhs_type,
6761 cctx_T *cctx)
6762{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006763 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006764 garray_T *stack = &cctx->ctx_type_stack;
6765 int range = FALSE;
6766
Bram Moolenaar68452172021-04-12 21:21:02 +02006767 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006768 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006769 if (is_assign && range
6770 && lhs->lhs_type->tt_type != VAR_LIST
6771 && lhs->lhs_type != &t_blob
6772 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006773 {
6774 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6775 return FAIL;
6776 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006777
6778 if (lhs->lhs_type == &t_any)
6779 {
6780 // Index on variable of unknown type: check at runtime.
6781 dest_type = VAR_ANY;
6782 }
6783 else
6784 {
6785 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006786 if (dest_type == VAR_DICT && range)
6787 {
6788 emsg(e_cannot_use_range_with_dictionary);
6789 return FAIL;
6790 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006791 if (dest_type == VAR_DICT
6792 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006793 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006794 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006795 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006796 type_T *type;
6797
6798 if (range)
6799 {
6800 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6801 if (need_type(type, &t_number,
6802 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006803 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006804 }
6805 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006806 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006807 && need_type(type, &t_number,
6808 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006809 return FAIL;
6810 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006811 }
6812
6813 // Load the dict or list. On the stack we then have:
6814 // - value (for assignment, not for :unlet)
6815 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006816 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006817 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006818 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6819 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006820
Bram Moolenaar68452172021-04-12 21:21:02 +02006821 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6822 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006823 {
6824 if (is_assign)
6825 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006826 if (range)
6827 {
6828 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6829 return FAIL;
6830 }
6831 else
6832 {
6833 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834
Bram Moolenaar68452172021-04-12 21:21:02 +02006835 if (isn == NULL)
6836 return FAIL;
6837 isn->isn_arg.vartype = dest_type;
6838 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006839 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006840 else if (range)
6841 {
6842 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6843 return FAIL;
6844 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006845 else
6846 {
6847 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6848 return FAIL;
6849 }
6850 }
6851 else
6852 {
6853 emsg(_(e_indexable_type_required));
6854 return FAIL;
6855 }
6856
6857 return OK;
6858}
6859
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006860/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006861 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006862 * "let name"
6863 * "var name = expr"
6864 * "final name = expr"
6865 * "const name = expr"
6866 * "name = expr"
6867 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006868 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006869 * Return NULL for an error.
6870 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 */
6872 static char_u *
6873compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6874{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006875 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006877 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 char_u *ret = NULL;
6879 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006882 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 int oplen = 0;
6887 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006888 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006889 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006891 int is_decl = is_decl_command(cmdidx);
6892 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006893 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006895 // Skip over the "var" or "[var, var]" to get to any "=".
6896 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6897 if (p == NULL)
6898 return *arg == '[' ? arg : NULL;
6899
6900 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006902 // TODO: should we allow this, and figure out type inference from list
6903 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006904 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 return NULL;
6906 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006907 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 sp = p;
6910 p = skipwhite(p);
6911 op = p;
6912 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006913
6914 if (var_count > 0 && oplen == 0)
6915 // can be something like "[1, 2]->func()"
6916 return arg;
6917
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006918 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006920 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006921 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006922 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006923 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6924 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006925 if (VIM_ISWHITE(eap->cmd[2]))
6926 {
6927 semsg(_(e_no_white_space_allowed_after_str_str),
6928 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6929 return NULL;
6930 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006931 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6932 oplen = 2;
6933 incdec = TRUE;
6934 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 if (heredoc)
6937 {
6938 list_T *l;
6939 listitem_T *li;
6940
6941 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006942 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006944 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006945 if (l == NULL)
6946 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947
Bram Moolenaar078269b2020-09-21 20:35:55 +02006948 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006950 // Push each line and the create the list.
6951 FOR_ALL_LIST_ITEMS(l, li)
6952 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006953 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006954 li->li_tv.vval.v_string = NULL;
6955 }
6956 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 list_free(l);
6959 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006960 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006964 char_u *wp;
6965
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006966 // for "[var, var] = expr" evaluate the expression here, loop over the
6967 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006968 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006969
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006970 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006971 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006972 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006973 if (compile_expr0(&p, cctx) == FAIL)
6974 return NULL;
6975 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006977 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 type_T *stacktype;
6980
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006981 stacktype = stack->ga_len == 0 ? &t_void
6982 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006983 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006985 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006986 goto theend;
6987 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006988 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006989 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006990 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006991 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006992 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6993 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006994 if (stacktype->tt_member != NULL)
6995 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006996 }
6997 }
6998
6999 /*
7000 * Loop over variables in "[var, var] = expr".
7001 * For "var = expr" and "let var: type" this is done only once.
7002 */
7003 if (var_count > 0)
7004 var_start = skipwhite(arg + 1); // skip over the "["
7005 else
7006 var_start = arg;
7007 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7008 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007009 int instr_count = -1;
7010 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007011
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007012 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7013 {
7014 // Ignore underscore in "[a, _, b] = list".
7015 if (var_count > 0)
7016 {
7017 var_start = skipwhite(var_start + 2);
7018 continue;
7019 }
7020 emsg(_(e_cannot_use_underscore_here));
7021 goto theend;
7022 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007023 vim_free(lhs.lhs_name);
7024
7025 /*
7026 * Figure out the LHS type and other properties.
7027 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007028 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7029 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007030 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007031 if (heredoc)
7032 {
7033 SOURCING_LNUM = start_lnum;
7034 if (lhs.lhs_has_type
7035 && need_type(&t_list_string, lhs.lhs_type,
7036 -1, 0, cctx, FALSE, FALSE) == FAIL)
7037 goto theend;
7038 }
7039 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007040 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007041 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007042 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007043 if (oplen > 0 && var_count == 0)
7044 {
7045 // skip over the "=" and the expression
7046 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007047 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007048 }
7049 }
7050 else if (oplen > 0)
7051 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007052 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007053 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007054
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007055 // for "+=", "*=", "..=" etc. first load the current value
7056 if (*op != '='
7057 && compile_load_lhs_with_index(&lhs, var_start,
7058 cctx) == FAIL)
7059 goto theend;
7060
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007061 // For "var = expr" evaluate the expression.
7062 if (var_count == 0)
7063 {
7064 int r;
7065
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007066 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007067 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007068 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007069 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007070 r = generate_PUSHNR(cctx, 1);
7071 }
7072 else
7073 {
7074 // Temporarily hide the new local variable here, it is
7075 // not available to this expression.
7076 if (lhs.lhs_new_local)
7077 --cctx->ctx_locals.ga_len;
7078 wp = op + oplen;
7079 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7080 {
7081 if (lhs.lhs_new_local)
7082 ++cctx->ctx_locals.ga_len;
7083 goto theend;
7084 }
7085 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007086 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007087 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007088 if (r == FAIL)
7089 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007090 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007091 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007092 else if (semicolon && var_idx == var_count - 1)
7093 {
7094 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007095 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007096 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7097 goto theend;
7098 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007099 else
7100 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007101 // For "[var, var] = expr" get the "var_idx" item from the
7102 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007103 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007104 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007105 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007106
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007107 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007108 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007109 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007110 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007111 if ((rhs_type->tt_type == VAR_FUNC
7112 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007113 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007114 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007115 goto theend;
7116
Bram Moolenaar752fc692021-01-04 21:57:11 +01007117 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007118 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007119 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007120 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007121 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007122 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007123 }
7124 else
7125 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007126 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007127 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007128 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007129 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007130 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007131 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007132 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007133 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007134 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007135 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007136 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007137 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007138 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007139 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007140 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007141 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007142
Bram Moolenaar77709b12021-04-03 21:01:01 +02007143 // Without operator check type here, otherwise below.
7144 // Use the line number of the assignment.
7145 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007146 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7147 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007148 // If assigning to a list or dict member, use the
7149 // member type. Not for "list[:] =".
7150 if (lhs.lhs_has_index
7151 && !has_list_index(var_start + lhs.lhs_varlen,
7152 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007153 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007154 if (need_type_where(rhs_type, use_type, -1, where,
7155 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007156 goto theend;
7157 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007158 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007159 else
7160 {
7161 type_T *lhs_type = lhs.lhs_member_type;
7162
7163 // Special case: assigning to @# can use a number or a
7164 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007165 // Also: can assign a number to a float.
7166 if ((lhs_type == &t_number_or_string
7167 || lhs_type == &t_float)
7168 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007169 lhs_type = &t_number;
7170 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007171 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007172 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007175 else if (cmdidx == CMD_final)
7176 {
7177 emsg(_(e_final_requires_a_value));
7178 goto theend;
7179 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007180 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007182 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007183 goto theend;
7184 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007185 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007186 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007187 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007188 goto theend;
7189 }
7190 else
7191 {
7192 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007193 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007194 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007195 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007196 {
7197 case VAR_BOOL:
7198 generate_PUSHBOOL(cctx, VVAL_FALSE);
7199 break;
7200 case VAR_FLOAT:
7201#ifdef FEAT_FLOAT
7202 generate_PUSHF(cctx, 0.0);
7203#endif
7204 break;
7205 case VAR_STRING:
7206 generate_PUSHS(cctx, NULL);
7207 break;
7208 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007209 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007210 break;
7211 case VAR_FUNC:
7212 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7213 break;
7214 case VAR_LIST:
7215 generate_NEWLIST(cctx, 0);
7216 break;
7217 case VAR_DICT:
7218 generate_NEWDICT(cctx, 0);
7219 break;
7220 case VAR_JOB:
7221 generate_PUSHJOB(cctx, NULL);
7222 break;
7223 case VAR_CHANNEL:
7224 generate_PUSHCHANNEL(cctx, NULL);
7225 break;
7226 case VAR_NUMBER:
7227 case VAR_UNKNOWN:
7228 case VAR_ANY:
7229 case VAR_PARTIAL:
7230 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007231 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007232 case VAR_SPECIAL: // cannot happen
7233 generate_PUSHNR(cctx, 0);
7234 break;
7235 }
7236 }
7237 if (var_count == 0)
7238 end = p;
7239 }
7240
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007241 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007242 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007243 break;
7244
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007245 if (oplen > 0 && *op != '=')
7246 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007247 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007248 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007249
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007250 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007251 {
7252 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7253 goto theend;
7254 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007255 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007256 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007257 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007258 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7259 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007260#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007261 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007262 !(expected == &t_float && (stacktype == &t_number
7263 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007264#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007265 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007266 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007267 goto theend;
7268 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007269
7270 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007271 {
7272 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7273 goto theend;
7274 }
7275 else if (*op == '+')
7276 {
7277 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007278 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007279 lhs.lhs_member_type, stacktype,
7280 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007281 goto theend;
7282 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007283 else if (generate_two_op(cctx, op) == FAIL)
7284 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007287 // Use the line number of the assignment for store instruction.
7288 save_lnum = cctx->ctx_lnum;
7289 cctx->ctx_lnum = start_lnum - 1;
7290
Bram Moolenaar752fc692021-01-04 21:57:11 +01007291 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007292 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007293 // Use the info in "lhs" to store the value at the index in the
7294 // list or dict.
7295 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7296 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007297 {
7298 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007299 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007300 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007301 }
7302 else
7303 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007304 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007305 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007306 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007307 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007308 generate_LOCKCONST(cctx);
7309
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007310 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007311 && (lhs.lhs_type->tt_type == VAR_DICT
7312 || lhs.lhs_type->tt_type == VAR_LIST)
7313 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007314 && !(lhs.lhs_type->tt_member == &t_any
7315 && oplen > 0
7316 && rhs_type != NULL
7317 && rhs_type->tt_type == lhs.lhs_type->tt_type
7318 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007319 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007320 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007321 // also in legacy script. Not for "list<any> = val", then the
7322 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007323 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007324
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007325 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007326 {
7327 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007328 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007329 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007330 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007331 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007332
7333 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007334 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007336
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007337 // For "[var, var] = expr" drop the "expr" value.
7338 // Also for "[var, var; _] = expr".
7339 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007340 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007341 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007342 goto theend;
7343 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007344
Bram Moolenaarb2097502020-07-19 17:17:02 +02007345 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346
7347theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007348 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 return ret;
7350}
7351
7352/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007353 * Check for an assignment at "eap->cmd", compile it if found.
7354 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7355 */
7356 static int
7357may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7358{
7359 char_u *pskip;
7360 char_u *p;
7361
7362 // Assuming the command starts with a variable or function name,
7363 // find what follows.
7364 // Skip over "var.member", "var[idx]" and the like.
7365 // Also "&opt = val", "$ENV = val" and "@r = val".
7366 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7367 ? eap->cmd + 1 : eap->cmd;
7368 p = to_name_end(pskip, TRUE);
7369 if (p > eap->cmd && *p != NUL)
7370 {
7371 char_u *var_end;
7372 int oplen;
7373 int heredoc;
7374
7375 if (eap->cmd[0] == '@')
7376 var_end = eap->cmd + 2;
7377 else
7378 var_end = find_name_end(pskip, NULL, NULL,
7379 FNE_CHECK_START | FNE_INCL_BR);
7380 oplen = assignment_len(skipwhite(var_end), &heredoc);
7381 if (oplen > 0)
7382 {
7383 size_t len = p - eap->cmd;
7384
7385 // Recognize an assignment if we recognize the variable
7386 // name:
7387 // "g:var = expr"
7388 // "local = expr" where "local" is a local var.
7389 // "script = expr" where "script" is a script-local var.
7390 // "import = expr" where "import" is an imported var
7391 // "&opt = expr"
7392 // "$ENV = expr"
7393 // "@r = expr"
7394 if (*eap->cmd == '&'
7395 || *eap->cmd == '$'
7396 || *eap->cmd == '@'
7397 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007398 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007399 {
7400 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7401 if (*line == NULL || *line == eap->cmd)
7402 return FAIL;
7403 return OK;
7404 }
7405 }
7406 }
7407
7408 if (*eap->cmd == '[')
7409 {
7410 // [var, var] = expr
7411 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7412 if (*line == NULL)
7413 return FAIL;
7414 if (*line != eap->cmd)
7415 return OK;
7416 }
7417 return NOTDONE;
7418}
7419
7420/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007421 * Check if "name" can be "unlet".
7422 */
7423 int
7424check_vim9_unlet(char_u *name)
7425{
7426 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7427 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007428 // "unlet s:var" is allowed in legacy script.
7429 if (*name == 's' && !script_is_vim9())
7430 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007431 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007432 return FAIL;
7433 }
7434 return OK;
7435}
7436
7437/*
7438 * Callback passed to ex_unletlock().
7439 */
7440 static int
7441compile_unlet(
7442 lval_T *lvp,
7443 char_u *name_end,
7444 exarg_T *eap,
7445 int deep UNUSED,
7446 void *coookie)
7447{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007448 cctx_T *cctx = coookie;
7449 char_u *p = lvp->ll_name;
7450 int cc = *name_end;
7451 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007452
Bram Moolenaar752fc692021-01-04 21:57:11 +01007453 if (cctx->ctx_skip == SKIP_YES)
7454 return OK;
7455
7456 *name_end = NUL;
7457 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007458 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007459 // :unlet $ENV_VAR
7460 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7461 }
7462 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7463 {
7464 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007465
Bram Moolenaar752fc692021-01-04 21:57:11 +01007466 // This is similar to assigning: lookup the list/dict, compile the
7467 // idx/key. Then instead of storing the value unlet the item.
7468 // unlet {list}[idx]
7469 // unlet {dict}[key] dict.key
7470 //
7471 // Figure out the LHS type and other properties.
7472 //
7473 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7474
7475 // : unlet an indexed item
7476 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007477 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007478 iemsg("called compile_lhs() without an index");
7479 ret = FAIL;
7480 }
7481 else
7482 {
7483 // Use the info in "lhs" to unlet the item at the index in the
7484 // list or dict.
7485 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007486 }
7487
Bram Moolenaar752fc692021-01-04 21:57:11 +01007488 vim_free(lhs.lhs_name);
7489 }
7490 else if (check_vim9_unlet(p) == FAIL)
7491 {
7492 ret = FAIL;
7493 }
7494 else
7495 {
7496 // Normal name. Only supports g:, w:, t: and b: namespaces.
7497 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007498 }
7499
Bram Moolenaar752fc692021-01-04 21:57:11 +01007500 *name_end = cc;
7501 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007502}
7503
7504/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007505 * Callback passed to ex_unletlock().
7506 */
7507 static int
7508compile_lock_unlock(
7509 lval_T *lvp,
7510 char_u *name_end,
7511 exarg_T *eap,
7512 int deep UNUSED,
7513 void *coookie)
7514{
7515 cctx_T *cctx = coookie;
7516 int cc = *name_end;
7517 char_u *p = lvp->ll_name;
7518 int ret = OK;
7519 size_t len;
7520 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007521 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007522
7523 if (cctx->ctx_skip == SKIP_YES)
7524 return OK;
7525
7526 // Cannot use :lockvar and :unlockvar on local variables.
7527 if (p[1] != ':')
7528 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007529 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007530
7531 if (lookup_local(p, end - p, NULL, cctx) == OK)
7532 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007533 char_u *s = p;
7534
7535 if (*end != '.' && *end != '[')
7536 {
7537 emsg(_(e_cannot_lock_unlock_local_variable));
7538 return FAIL;
7539 }
7540
7541 // For "d.member" put the local variable on the stack, it will be
7542 // passed to ex_lockvar() indirectly.
7543 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7544 return FAIL;
7545 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007546 }
7547 }
7548
7549 // Checking is done at runtime.
7550 *name_end = NUL;
7551 len = name_end - p + 20;
7552 buf = alloc(len);
7553 if (buf == NULL)
7554 ret = FAIL;
7555 else
7556 {
7557 vim_snprintf((char *)buf, len, "%s %s",
7558 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7559 p);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007560 ret = generate_EXEC(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007561
7562 vim_free(buf);
7563 *name_end = cc;
7564 }
7565 return ret;
7566}
7567
7568/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007569 * compile "unlet var", "lock var" and "unlock var"
7570 * "arg" points to "var".
7571 */
7572 static char_u *
7573compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7574{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007575 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7576 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7577 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007578 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7579}
7580
7581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7583 */
7584 static int
7585compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7586{
7587 garray_T *instr = &cctx->ctx_instr;
7588 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7589
7590 if (endlabel == NULL)
7591 return FAIL;
7592 endlabel->el_next = *el;
7593 *el = endlabel;
7594 endlabel->el_end_label = instr->ga_len;
7595
7596 generate_JUMP(cctx, when, 0);
7597 return OK;
7598}
7599
7600 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007601compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602{
7603 garray_T *instr = &cctx->ctx_instr;
7604
7605 while (*el != NULL)
7606 {
7607 endlabel_T *cur = (*el);
7608 isn_T *isn;
7609
7610 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007611 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 *el = cur->el_next;
7613 vim_free(cur);
7614 }
7615}
7616
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007617 static void
7618compile_free_jump_to_end(endlabel_T **el)
7619{
7620 while (*el != NULL)
7621 {
7622 endlabel_T *cur = (*el);
7623
7624 *el = cur->el_next;
7625 vim_free(cur);
7626 }
7627}
7628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629/*
7630 * Create a new scope and set up the generic items.
7631 */
7632 static scope_T *
7633new_scope(cctx_T *cctx, scopetype_T type)
7634{
7635 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7636
7637 if (scope == NULL)
7638 return NULL;
7639 scope->se_outer = cctx->ctx_scope;
7640 cctx->ctx_scope = scope;
7641 scope->se_type = type;
7642 scope->se_local_count = cctx->ctx_locals.ga_len;
7643 return scope;
7644}
7645
7646/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007647 * Free the current scope and go back to the outer scope.
7648 */
7649 static void
7650drop_scope(cctx_T *cctx)
7651{
7652 scope_T *scope = cctx->ctx_scope;
7653
7654 if (scope == NULL)
7655 {
7656 iemsg("calling drop_scope() without a scope");
7657 return;
7658 }
7659 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007660 switch (scope->se_type)
7661 {
7662 case IF_SCOPE:
7663 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7664 case FOR_SCOPE:
7665 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7666 case WHILE_SCOPE:
7667 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7668 case TRY_SCOPE:
7669 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7670 case NO_SCOPE:
7671 case BLOCK_SCOPE:
7672 break;
7673 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007674 vim_free(scope);
7675}
7676
7677/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 * compile "if expr"
7679 *
7680 * "if expr" Produces instructions:
7681 * EVAL expr Push result of "expr"
7682 * JUMP_IF_FALSE end
7683 * ... body ...
7684 * end:
7685 *
7686 * "if expr | else" Produces instructions:
7687 * EVAL expr Push result of "expr"
7688 * JUMP_IF_FALSE else
7689 * ... body ...
7690 * JUMP_ALWAYS end
7691 * else:
7692 * ... body ...
7693 * end:
7694 *
7695 * "if expr1 | elseif expr2 | else" Produces instructions:
7696 * EVAL expr Push result of "expr"
7697 * JUMP_IF_FALSE elseif
7698 * ... body ...
7699 * JUMP_ALWAYS end
7700 * elseif:
7701 * EVAL expr Push result of "expr"
7702 * JUMP_IF_FALSE else
7703 * ... body ...
7704 * JUMP_ALWAYS end
7705 * else:
7706 * ... body ...
7707 * end:
7708 */
7709 static char_u *
7710compile_if(char_u *arg, cctx_T *cctx)
7711{
7712 char_u *p = arg;
7713 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007714 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007716 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007717 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718
Bram Moolenaara5565e42020-05-09 15:44:01 +02007719 CLEAR_FIELD(ppconst);
7720 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007721 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007722 clear_ppconst(&ppconst);
7723 return NULL;
7724 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007725 if (!ends_excmd2(arg, skipwhite(p)))
7726 {
7727 semsg(_(e_trailing_arg), p);
7728 return NULL;
7729 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007730 if (cctx->ctx_skip == SKIP_YES)
7731 clear_ppconst(&ppconst);
7732 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007733 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007734 int error = FALSE;
7735 int v;
7736
Bram Moolenaara5565e42020-05-09 15:44:01 +02007737 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007738 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007739 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007740 if (error)
7741 return NULL;
7742 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007743 }
7744 else
7745 {
7746 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007747 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007748 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007749 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007750 if (bool_on_stack(cctx) == FAIL)
7751 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753
Bram Moolenaara91a7132021-03-25 21:12:15 +01007754 // CMDMOD_REV must come before the jump
7755 generate_undo_cmdmods(cctx);
7756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 scope = new_scope(cctx, IF_SCOPE);
7758 if (scope == NULL)
7759 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007760 scope->se_skip_save = skip_save;
7761 // "is_had_return" will be reset if any block does not end in :return
7762 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007764 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007765 {
7766 // "where" is set when ":elseif", "else" or ":endif" is found
7767 scope->se_u.se_if.is_if_label = instr->ga_len;
7768 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7769 }
7770 else
7771 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772
Bram Moolenaarced68a02021-01-24 17:53:47 +01007773#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007774 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007775 && skip_save != SKIP_YES)
7776 {
7777 // generated a profile start, need to generate a profile end, since it
7778 // won't be done after returning
7779 cctx->ctx_skip = SKIP_NOT;
7780 generate_instr(cctx, ISN_PROF_END);
7781 cctx->ctx_skip = SKIP_YES;
7782 }
7783#endif
7784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 return p;
7786}
7787
7788 static char_u *
7789compile_elseif(char_u *arg, cctx_T *cctx)
7790{
7791 char_u *p = arg;
7792 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007793 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 isn_T *isn;
7795 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007796 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007797 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798
7799 if (scope == NULL || scope->se_type != IF_SCOPE)
7800 {
7801 emsg(_(e_elseif_without_if));
7802 return NULL;
7803 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007804 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007805 if (!cctx->ctx_had_return)
7806 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807
Bram Moolenaarced68a02021-01-24 17:53:47 +01007808 if (cctx->ctx_skip == SKIP_NOT)
7809 {
7810 // previous block was executed, this one and following will not
7811 cctx->ctx_skip = SKIP_YES;
7812 scope->se_u.se_if.is_seen_skip_not = TRUE;
7813 }
7814 if (scope->se_u.se_if.is_seen_skip_not)
7815 {
7816 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007817 // Do not count the "elseif" for profiling and cmdmod
7818 instr->ga_len = current_instr_idx(cctx);
7819
Bram Moolenaarced68a02021-01-24 17:53:47 +01007820 skip_expr_cctx(&p, cctx);
7821 return p;
7822 }
7823
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007824 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007825 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007826 int moved_cmdmod = FALSE;
7827 int saved_debug = FALSE;
7828 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007829
7830 // Move any CMDMOD instruction to after the jump
7831 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7832 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007833 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007834 return NULL;
7835 ((isn_T *)instr->ga_data)[instr->ga_len] =
7836 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7837 --instr->ga_len;
7838 moved_cmdmod = TRUE;
7839 }
7840
Bram Moolenaar093165c2021-08-22 13:35:31 +02007841 // Remove the already generated ISN_DEBUG, it is written below the
7842 // ISN_FOR instruction.
7843 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7844 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7845 .isn_type == ISN_DEBUG)
7846 {
7847 --instr->ga_len;
7848 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7849 saved_debug = TRUE;
7850 }
7851
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007852 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007854 return NULL;
7855 // previous "if" or "elseif" jumps here
7856 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7857 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007858
Bram Moolenaara91a7132021-03-25 21:12:15 +01007859 if (moved_cmdmod)
7860 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007861
7862 if (saved_debug)
7863 {
7864 // move the debug instruction here
7865 if (GA_GROW_FAILS(instr, 1))
7866 return NULL;
7867 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7868 ++instr->ga_len;
7869 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007872 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007873 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007874 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007875 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007876 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007877#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007878 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007879 {
7880 // the previous block was skipped, need to profile this line
7881 generate_instr(cctx, ISN_PROF_START);
7882 instr_count = instr->ga_len;
7883 }
7884#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007885 if (cctx->ctx_compile_type == CT_DEBUG)
7886 {
7887 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007888 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007889 instr_count = instr->ga_len;
7890 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007891 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007892 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007893 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007894 clear_ppconst(&ppconst);
7895 return NULL;
7896 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007897 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007898 if (!ends_excmd2(arg, skipwhite(p)))
7899 {
7900 semsg(_(e_trailing_arg), p);
7901 return NULL;
7902 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007903 if (scope->se_skip_save == SKIP_YES)
7904 clear_ppconst(&ppconst);
7905 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007906 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007907 int error = FALSE;
7908 int v;
7909
Bram Moolenaar7f141552020-05-09 17:35:53 +02007910 // The expression results in a constant.
7911 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007912 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7913 if (error)
7914 return NULL;
7915 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007916 clear_ppconst(&ppconst);
7917 scope->se_u.se_if.is_if_label = -1;
7918 }
7919 else
7920 {
7921 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007922 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007923 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007924 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007925 if (bool_on_stack(cctx) == FAIL)
7926 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927
Bram Moolenaara91a7132021-03-25 21:12:15 +01007928 // CMDMOD_REV must come before the jump
7929 generate_undo_cmdmods(cctx);
7930
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007931 // "where" is set when ":elseif", "else" or ":endif" is found
7932 scope->se_u.se_if.is_if_label = instr->ga_len;
7933 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935
7936 return p;
7937}
7938
7939 static char_u *
7940compile_else(char_u *arg, cctx_T *cctx)
7941{
7942 char_u *p = arg;
7943 garray_T *instr = &cctx->ctx_instr;
7944 isn_T *isn;
7945 scope_T *scope = cctx->ctx_scope;
7946
7947 if (scope == NULL || scope->se_type != IF_SCOPE)
7948 {
7949 emsg(_(e_else_without_if));
7950 return NULL;
7951 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007952 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007953 if (!cctx->ctx_had_return)
7954 scope->se_u.se_if.is_had_return = FALSE;
7955 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaarced68a02021-01-24 17:53:47 +01007957#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007958 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007959 {
7960 if (cctx->ctx_skip == SKIP_NOT
7961 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7962 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007963 // the previous block was executed, do not count "else" for
7964 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007965 --instr->ga_len;
7966 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7967 {
7968 // the previous block was not executed, this one will, do count the
7969 // "else" for profiling
7970 cctx->ctx_skip = SKIP_NOT;
7971 generate_instr(cctx, ISN_PROF_END);
7972 generate_instr(cctx, ISN_PROF_START);
7973 cctx->ctx_skip = SKIP_YES;
7974 }
7975 }
7976#endif
7977
7978 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007979 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007980 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007981 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007982 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007983 if (!cctx->ctx_had_return
7984 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7985 JUMP_ALWAYS, cctx) == FAIL)
7986 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007987 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007988
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007989 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007990 {
7991 if (scope->se_u.se_if.is_if_label >= 0)
7992 {
7993 // previous "if" or "elseif" jumps here
7994 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7995 isn->isn_arg.jump.jump_where = instr->ga_len;
7996 scope->se_u.se_if.is_if_label = -1;
7997 }
7998 }
7999
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008000 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008001 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008003
8004 return p;
8005}
8006
8007 static char_u *
8008compile_endif(char_u *arg, cctx_T *cctx)
8009{
8010 scope_T *scope = cctx->ctx_scope;
8011 ifscope_T *ifscope;
8012 garray_T *instr = &cctx->ctx_instr;
8013 isn_T *isn;
8014
Bram Moolenaarfa984412021-03-25 22:15:28 +01008015 if (misplaced_cmdmod(cctx))
8016 return NULL;
8017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 if (scope == NULL || scope->se_type != IF_SCOPE)
8019 {
8020 emsg(_(e_endif_without_if));
8021 return NULL;
8022 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008023 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008024 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008025 if (!cctx->ctx_had_return)
8026 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008028 if (scope->se_u.se_if.is_if_label >= 0)
8029 {
8030 // previous "if" or "elseif" jumps here
8031 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8032 isn->isn_arg.jump.jump_where = instr->ga_len;
8033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008035 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008036
8037#ifdef FEAT_PROFILE
8038 // even when skipping we count the endif as executed, unless the block it's
8039 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008040 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008041 && scope->se_skip_save != SKIP_YES)
8042 {
8043 cctx->ctx_skip = SKIP_NOT;
8044 generate_instr(cctx, ISN_PROF_START);
8045 }
8046#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008047 cctx->ctx_skip = scope->se_skip_save;
8048
8049 // If all the blocks end in :return and there is an :else then the
8050 // had_return flag is set.
8051 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008053 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 return arg;
8055}
8056
8057/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008058 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 *
8060 * Produces instructions:
8061 * PUSHNR -1
8062 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008063 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8065 * - if beyond end, jump to "end"
8066 * - otherwise get item from list and push it
8067 * STORE var Store item in "var"
8068 * ... body ...
8069 * JUMP top Jump back to repeat
8070 * end: DROP Drop the result of "expr"
8071 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008072 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8073 * UNPACK 2 Split item in 2
8074 * STORE var1 Store item in "var1"
8075 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 */
8077 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008078compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008080 char_u *arg;
8081 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008082 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008084 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008085 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008086 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008087 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008090 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008092 lvar_T *loop_lvar; // loop iteration variable
8093 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008095 type_T *item_type = &t_any;
8096 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008097 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098
Bram Moolenaar792f7862020-11-23 08:31:18 +01008099 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008100 if (p == NULL)
8101 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008102 if (var_count == 0)
8103 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008104 else
8105 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106
8107 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008108 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008109 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8110 return NULL;
8111 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008113 if (*p == ':' && wp != p)
8114 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8115 else
8116 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 return NULL;
8118 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008119 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008120 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8121 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008123 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8124 // instruction.
8125 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8126 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8127 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008128 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008129 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008130 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8131 .isn_arg.debug.dbg_break_lnum;
8132 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 scope = new_scope(cctx, FOR_SCOPE);
8135 if (scope == NULL)
8136 return NULL;
8137
Bram Moolenaar792f7862020-11-23 08:31:18 +01008138 // Reserve a variable to store the loop iteration counter and initialize it
8139 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008140 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8141 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008142 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008143 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008144 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008146 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008147 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148
8149 // compile "expr", it remains on the stack until "endfor"
8150 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008151 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008152 {
8153 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008155 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008156 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157
rbtnnbebf0692021-08-21 17:26:50 +02008158 if (cctx->ctx_skip != SKIP_YES)
8159 {
8160 // If we know the type of "var" and it is a not a supported type we can
8161 // give an error now.
8162 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8163 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008164 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008165 {
rbtnnbebf0692021-08-21 17:26:50 +02008166 semsg(_(e_for_loop_on_str_not_supported),
8167 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008168 drop_scope(cctx);
8169 return NULL;
8170 }
rbtnnbebf0692021-08-21 17:26:50 +02008171
8172 if (vartype->tt_type == VAR_STRING)
8173 item_type = &t_string;
8174 else if (vartype->tt_type == VAR_BLOB)
8175 item_type = &t_number;
8176 else if (vartype->tt_type == VAR_LIST
8177 && vartype->tt_member->tt_type != VAR_ANY)
8178 {
8179 if (!var_list)
8180 item_type = vartype->tt_member;
8181 else if (vartype->tt_member->tt_type == VAR_LIST
8182 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
8183 // TODO: should get the type for each lhs
8184 item_type = vartype->tt_member->tt_member;
8185 }
8186
8187 // CMDMOD_REV must come before the FOR instruction.
8188 generate_undo_cmdmods(cctx);
8189
8190 // "for_end" is set when ":endfor" is found
8191 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8192
8193 generate_FOR(cctx, loop_lvar->lv_idx);
8194
8195 arg = arg_start;
8196 if (var_list)
8197 {
8198 generate_UNPACK(cctx, var_count, semicolon);
8199 arg = skipwhite(arg + 1); // skip white after '['
8200
8201 // the list item is replaced by a number of items
8202 if (GA_GROW_FAILS(stack, var_count - 1))
8203 {
8204 drop_scope(cctx);
8205 return NULL;
8206 }
8207 --stack->ga_len;
8208 for (idx = 0; idx < var_count; ++idx)
8209 {
8210 ((type_T **)stack->ga_data)[stack->ga_len] =
8211 (semicolon && idx == 0) ? vartype : item_type;
8212 ++stack->ga_len;
8213 }
8214 }
8215
Bram Moolenaar792f7862020-11-23 08:31:18 +01008216 for (idx = 0; idx < var_count; ++idx)
8217 {
rbtnnbebf0692021-08-21 17:26:50 +02008218 assign_dest_T dest = dest_local;
8219 int opt_flags = 0;
8220 int vimvaridx = -1;
8221 type_T *type = &t_any;
8222 type_T *lhs_type = &t_any;
8223 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008224
rbtnnbebf0692021-08-21 17:26:50 +02008225 p = skip_var_one(arg, FALSE);
8226 varlen = p - arg;
8227 name = vim_strnsave(arg, varlen);
8228 if (name == NULL)
8229 goto failed;
8230 if (*p == ':')
8231 {
8232 p = skipwhite(p + 1);
8233 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8234 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008235
rbtnnbebf0692021-08-21 17:26:50 +02008236 // TODO: script var not supported?
8237 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008238 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008239 goto failed;
8240 if (dest != dest_local)
8241 {
8242 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008243 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008244 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008245 }
rbtnnbebf0692021-08-21 17:26:50 +02008246 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008247 {
rbtnnbebf0692021-08-21 17:26:50 +02008248 // Assigning to "_": drop the value.
8249 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8250 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008251 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008252 else
rbtnnbebf0692021-08-21 17:26:50 +02008253 {
8254 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8255 {
8256 semsg(_(e_variable_already_declared), arg);
8257 goto failed;
8258 }
8259
8260 if (STRNCMP(name, "s:", 2) == 0)
8261 {
8262 semsg(_(e_cannot_declare_script_variable_in_function), name);
8263 goto failed;
8264 }
8265
8266 // Reserve a variable to store "var".
8267 where.wt_index = var_list ? idx + 1 : 0;
8268 where.wt_variable = TRUE;
8269 if (lhs_type == &t_any)
8270 lhs_type = item_type;
8271 else if (item_type != &t_unknown
8272 && (item_type == &t_any
8273 ? need_type(item_type, lhs_type,
8274 -1, 0, cctx, FALSE, FALSE)
8275 : check_type(lhs_type, item_type, TRUE, where))
8276 == FAIL)
8277 goto failed;
8278 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8279 if (var_lvar == NULL)
8280 // out of memory or used as an argument
8281 goto failed;
8282
8283 if (semicolon && idx == var_count - 1)
8284 var_lvar->lv_type = vartype;
8285 else
8286 var_lvar->lv_type = item_type;
8287 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8288 }
8289
8290 if (*p == ',' || *p == ';')
8291 ++p;
8292 arg = skipwhite(p);
8293 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008294 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008295
rbtnnbebf0692021-08-21 17:26:50 +02008296 if (cctx->ctx_compile_type == CT_DEBUG)
8297 {
8298 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008299
rbtnnbebf0692021-08-21 17:26:50 +02008300 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8301 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8302 cctx->ctx_prev_lnum = prev_lnum;
8303 generate_instr_debug(cctx);
8304 cctx->ctx_prev_lnum = save_prev_lnum;
8305 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008306 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008307
Bram Moolenaar792f7862020-11-23 08:31:18 +01008308 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008309
8310failed:
8311 vim_free(name);
8312 drop_scope(cctx);
8313 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314}
8315
8316/*
8317 * compile "endfor"
8318 */
8319 static char_u *
8320compile_endfor(char_u *arg, cctx_T *cctx)
8321{
8322 garray_T *instr = &cctx->ctx_instr;
8323 scope_T *scope = cctx->ctx_scope;
8324 forscope_T *forscope;
8325 isn_T *isn;
8326
Bram Moolenaarfa984412021-03-25 22:15:28 +01008327 if (misplaced_cmdmod(cctx))
8328 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008330 if (scope == NULL || scope->se_type != FOR_SCOPE)
8331 {
8332 emsg(_(e_for));
8333 return NULL;
8334 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008335 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008337 if (cctx->ctx_skip != SKIP_YES)
8338 {
8339 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340
rbtnnbebf0692021-08-21 17:26:50 +02008341 // At end of ":for" scope jump back to the FOR instruction.
8342 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008343
rbtnnbebf0692021-08-21 17:26:50 +02008344 // Fill in the "end" label in the FOR statement so it can jump here.
8345 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8346 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347
rbtnnbebf0692021-08-21 17:26:50 +02008348 // Fill in the "end" label any BREAK statements
8349 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008350
rbtnnbebf0692021-08-21 17:26:50 +02008351 // Below the ":for" scope drop the "expr" list from the stack.
8352 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8353 return NULL;
8354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355
8356 vim_free(scope);
8357
8358 return arg;
8359}
8360
8361/*
8362 * compile "while expr"
8363 *
8364 * Produces instructions:
8365 * top: EVAL expr Push result of "expr"
8366 * JUMP_IF_FALSE end jump if false
8367 * ... body ...
8368 * JUMP top Jump back to repeat
8369 * end:
8370 *
8371 */
8372 static char_u *
8373compile_while(char_u *arg, cctx_T *cctx)
8374{
8375 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008376 scope_T *scope;
8377
8378 scope = new_scope(cctx, WHILE_SCOPE);
8379 if (scope == NULL)
8380 return NULL;
8381
Bram Moolenaara91a7132021-03-25 21:12:15 +01008382 // "endwhile" jumps back here, one before when profiling or using cmdmods
8383 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384
8385 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008386 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008388
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008389 if (!ends_excmd2(arg, skipwhite(p)))
8390 {
8391 semsg(_(e_trailing_arg), p);
8392 return NULL;
8393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394
rbtnnd895b1d2021-08-20 20:54:25 +02008395 if (cctx->ctx_skip != SKIP_YES)
8396 {
8397 if (bool_on_stack(cctx) == FAIL)
8398 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008399
rbtnnd895b1d2021-08-20 20:54:25 +02008400 // CMDMOD_REV must come before the jump
8401 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008402
rbtnnd895b1d2021-08-20 20:54:25 +02008403 // "while_end" is set when ":endwhile" is found
8404 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008405 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008406 return FAIL;
8407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408
8409 return p;
8410}
8411
8412/*
8413 * compile "endwhile"
8414 */
8415 static char_u *
8416compile_endwhile(char_u *arg, cctx_T *cctx)
8417{
8418 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008419 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420
Bram Moolenaarfa984412021-03-25 22:15:28 +01008421 if (misplaced_cmdmod(cctx))
8422 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8424 {
8425 emsg(_(e_while));
8426 return NULL;
8427 }
8428 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008429 if (cctx->ctx_skip != SKIP_YES)
8430 {
8431 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432
Bram Moolenaarf002a412021-01-24 13:34:18 +01008433#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008434 // count the endwhile before jumping
8435 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008436#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008437
rbtnnd895b1d2021-08-20 20:54:25 +02008438 // At end of ":for" scope jump back to the FOR instruction.
8439 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440
rbtnnd895b1d2021-08-20 20:54:25 +02008441 // Fill in the "end" label in the WHILE statement so it can jump here.
8442 // And in any jumps for ":break"
8443 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008444 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446
8447 vim_free(scope);
8448
8449 return arg;
8450}
8451
8452/*
8453 * compile "continue"
8454 */
8455 static char_u *
8456compile_continue(char_u *arg, cctx_T *cctx)
8457{
8458 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008459 int try_scopes = 0;
8460 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008461
8462 for (;;)
8463 {
8464 if (scope == NULL)
8465 {
8466 emsg(_(e_continue));
8467 return NULL;
8468 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008469 if (scope->se_type == FOR_SCOPE)
8470 {
8471 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008473 }
8474 if (scope->se_type == WHILE_SCOPE)
8475 {
8476 loop_label = scope->se_u.se_while.ws_top_label;
8477 break;
8478 }
8479 if (scope->se_type == TRY_SCOPE)
8480 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481 scope = scope->se_outer;
8482 }
8483
Bram Moolenaarc150c092021-02-13 15:02:46 +01008484 if (try_scopes > 0)
8485 // Inside one or more try/catch blocks we first need to jump to the
8486 // "finally" or "endtry" to cleanup.
8487 generate_TRYCONT(cctx, try_scopes, loop_label);
8488 else
8489 // Jump back to the FOR or WHILE instruction.
8490 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008492 return arg;
8493}
8494
8495/*
8496 * compile "break"
8497 */
8498 static char_u *
8499compile_break(char_u *arg, cctx_T *cctx)
8500{
8501 scope_T *scope = cctx->ctx_scope;
8502 endlabel_T **el;
8503
8504 for (;;)
8505 {
8506 if (scope == NULL)
8507 {
8508 emsg(_(e_break));
8509 return NULL;
8510 }
8511 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8512 break;
8513 scope = scope->se_outer;
8514 }
8515
8516 // Jump to the end of the FOR or WHILE loop.
8517 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008518 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008519 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008520 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008521 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8522 return FAIL;
8523
8524 return arg;
8525}
8526
8527/*
8528 * compile "{" start of block
8529 */
8530 static char_u *
8531compile_block(char_u *arg, cctx_T *cctx)
8532{
8533 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8534 return NULL;
8535 return skipwhite(arg + 1);
8536}
8537
8538/*
8539 * compile end of block: drop one scope
8540 */
8541 static void
8542compile_endblock(cctx_T *cctx)
8543{
8544 scope_T *scope = cctx->ctx_scope;
8545
8546 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008547 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 vim_free(scope);
8549}
8550
8551/*
8552 * compile "try"
8553 * Creates a new scope for the try-endtry, pointing to the first catch and
8554 * finally.
8555 * Creates another scope for the "try" block itself.
8556 * TRY instruction sets up exception handling at runtime.
8557 *
8558 * "try"
8559 * TRY -> catch1, -> finally push trystack entry
8560 * ... try block
8561 * "throw {exception}"
8562 * EVAL {exception}
8563 * THROW create exception
8564 * ... try block
8565 * " catch {expr}"
8566 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008567 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 * EVAL {expr}
8569 * MATCH
8570 * JUMP nomatch -> catch2
8571 * CATCH remove exception
8572 * ... catch block
8573 * " catch"
8574 * JUMP -> finally
8575 * catch2: CATCH remove exception
8576 * ... catch block
8577 * " finally"
8578 * finally:
8579 * ... finally block
8580 * " endtry"
8581 * ENDTRY pop trystack entry, may rethrow
8582 */
8583 static char_u *
8584compile_try(char_u *arg, cctx_T *cctx)
8585{
8586 garray_T *instr = &cctx->ctx_instr;
8587 scope_T *try_scope;
8588 scope_T *scope;
8589
Bram Moolenaarfa984412021-03-25 22:15:28 +01008590 if (misplaced_cmdmod(cctx))
8591 return NULL;
8592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 // scope that holds the jumps that go to catch/finally/endtry
8594 try_scope = new_scope(cctx, TRY_SCOPE);
8595 if (try_scope == NULL)
8596 return NULL;
8597
Bram Moolenaar69f70502021-01-01 16:10:46 +01008598 if (cctx->ctx_skip != SKIP_YES)
8599 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008600 isn_T *isn;
8601
8602 // "try_catch" is set when the first ":catch" is found or when no catch
8603 // is found and ":finally" is found.
8604 // "try_finally" is set when ":finally" is found
8605 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008606 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008607 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8608 return NULL;
8609 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8610 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008611 return NULL;
8612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613
8614 // scope for the try block itself
8615 scope = new_scope(cctx, BLOCK_SCOPE);
8616 if (scope == NULL)
8617 return NULL;
8618
8619 return arg;
8620}
8621
8622/*
8623 * compile "catch {expr}"
8624 */
8625 static char_u *
8626compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8627{
8628 scope_T *scope = cctx->ctx_scope;
8629 garray_T *instr = &cctx->ctx_instr;
8630 char_u *p;
8631 isn_T *isn;
8632
Bram Moolenaarfa984412021-03-25 22:15:28 +01008633 if (misplaced_cmdmod(cctx))
8634 return NULL;
8635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008636 // end block scope from :try or :catch
8637 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8638 compile_endblock(cctx);
8639 scope = cctx->ctx_scope;
8640
8641 // Error if not in a :try scope
8642 if (scope == NULL || scope->se_type != TRY_SCOPE)
8643 {
8644 emsg(_(e_catch));
8645 return NULL;
8646 }
8647
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008648 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008650 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 return NULL;
8652 }
8653
Bram Moolenaar69f70502021-01-01 16:10:46 +01008654 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008656#ifdef FEAT_PROFILE
8657 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008658 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008659 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008660 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008661 .isn_type == ISN_PROF_START)
8662 --instr->ga_len;
8663#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008664 // Jump from end of previous block to :finally or :endtry
8665 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8666 JUMP_ALWAYS, cctx) == FAIL)
8667 return NULL;
8668
8669 // End :try or :catch scope: set value in ISN_TRY instruction
8670 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008671 if (isn->isn_arg.try.try_ref->try_catch == 0)
8672 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008673 if (scope->se_u.se_try.ts_catch_label != 0)
8674 {
8675 // Previous catch without match jumps here
8676 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8677 isn->isn_arg.jump.jump_where = instr->ga_len;
8678 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008679#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008680 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008681 {
8682 // a "throw" that jumps here needs to be counted
8683 generate_instr(cctx, ISN_PROF_END);
8684 // the "catch" is also counted
8685 generate_instr(cctx, ISN_PROF_START);
8686 }
8687#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008688 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008689 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 }
8691
8692 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008693 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008695 scope->se_u.se_try.ts_caught_all = TRUE;
8696 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008697 }
8698 else
8699 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008700 char_u *end;
8701 char_u *pat;
8702 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008703 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008704 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008706 // Push v:exception, push {expr} and MATCH
8707 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8708
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008709 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008710 if (*end != *p)
8711 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008712 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008713 vim_free(tofree);
8714 return FAIL;
8715 }
8716 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008717 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008718 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008719 len = (int)(end - tofree);
8720 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008721 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008722 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008723 if (pat == NULL)
8724 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008725 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008726 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8729 return NULL;
8730
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008731 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008732 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8733 return NULL;
8734 }
8735
Bram Moolenaar69f70502021-01-01 16:10:46 +01008736 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 return NULL;
8738
8739 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8740 return NULL;
8741 return p;
8742}
8743
8744 static char_u *
8745compile_finally(char_u *arg, cctx_T *cctx)
8746{
8747 scope_T *scope = cctx->ctx_scope;
8748 garray_T *instr = &cctx->ctx_instr;
8749 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008750 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008751
Bram Moolenaarfa984412021-03-25 22:15:28 +01008752 if (misplaced_cmdmod(cctx))
8753 return NULL;
8754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008755 // end block scope from :try or :catch
8756 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8757 compile_endblock(cctx);
8758 scope = cctx->ctx_scope;
8759
8760 // Error if not in a :try scope
8761 if (scope == NULL || scope->se_type != TRY_SCOPE)
8762 {
8763 emsg(_(e_finally));
8764 return NULL;
8765 }
8766
rbtnn84934992021-08-07 13:26:53 +02008767 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008768 {
rbtnn84934992021-08-07 13:26:53 +02008769 // End :catch or :finally scope: set value in ISN_TRY instruction
8770 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8771 if (isn->isn_arg.try.try_ref->try_finally != 0)
8772 {
8773 emsg(_(e_finally_dup));
8774 return NULL;
8775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008776
rbtnn84934992021-08-07 13:26:53 +02008777 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008778#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008779 if (cctx->ctx_compile_type == CT_PROFILE
8780 && ((isn_T *)instr->ga_data)[this_instr - 1]
8781 .isn_type == ISN_PROF_START)
8782 {
8783 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008784 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008785
8786 // jump to the profile end above it
8787 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8788 .isn_type == ISN_PROF_END)
8789 --this_instr;
8790 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008791#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792
rbtnn84934992021-08-07 13:26:53 +02008793 // Fill in the "end" label in jumps at the end of the blocks.
8794 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8795 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008796
rbtnn84934992021-08-07 13:26:53 +02008797 // If there is no :catch then an exception jumps to :finally.
8798 if (isn->isn_arg.try.try_ref->try_catch == 0)
8799 isn->isn_arg.try.try_ref->try_catch = this_instr;
8800 isn->isn_arg.try.try_ref->try_finally = this_instr;
8801 if (scope->se_u.se_try.ts_catch_label != 0)
8802 {
8803 // Previous catch without match jumps here
8804 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8805 isn->isn_arg.jump.jump_where = this_instr;
8806 scope->se_u.se_try.ts_catch_label = 0;
8807 }
8808 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8809 return NULL;
8810
8811 // TODO: set index in ts_finally_label jumps
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813
8814 return arg;
8815}
8816
8817 static char_u *
8818compile_endtry(char_u *arg, cctx_T *cctx)
8819{
8820 scope_T *scope = cctx->ctx_scope;
8821 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008822 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823
Bram Moolenaarfa984412021-03-25 22:15:28 +01008824 if (misplaced_cmdmod(cctx))
8825 return NULL;
8826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008827 // end block scope from :catch or :finally
8828 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8829 compile_endblock(cctx);
8830 scope = cctx->ctx_scope;
8831
8832 // Error if not in a :try scope
8833 if (scope == NULL || scope->se_type != TRY_SCOPE)
8834 {
8835 if (scope == NULL)
8836 emsg(_(e_no_endtry));
8837 else if (scope->se_type == WHILE_SCOPE)
8838 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008839 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008840 emsg(_(e_endfor));
8841 else
8842 emsg(_(e_endif));
8843 return NULL;
8844 }
8845
Bram Moolenaarc150c092021-02-13 15:02:46 +01008846 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008847 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008849 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8850 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008851 {
8852 emsg(_(e_missing_catch_or_finally));
8853 return NULL;
8854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008855
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008856#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008857 if (cctx->ctx_compile_type == CT_PROFILE
8858 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008859 .isn_type == ISN_PROF_START)
8860 // move the profile start after "endtry" so that it's not counted when
8861 // the exception is rethrown.
8862 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008863#endif
8864
Bram Moolenaar69f70502021-01-01 16:10:46 +01008865 // Fill in the "end" label in jumps at the end of the blocks, if not
8866 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008867 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8868 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008869
Bram Moolenaar69f70502021-01-01 16:10:46 +01008870 if (scope->se_u.se_try.ts_catch_label != 0)
8871 {
8872 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008873 isn_T *isn = ((isn_T *)instr->ga_data)
8874 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008875 isn->isn_arg.jump.jump_where = instr->ga_len;
8876 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008877 }
8878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 compile_endblock(cctx);
8880
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008881 if (cctx->ctx_skip != SKIP_YES)
8882 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008883 // End :catch or :finally scope: set instruction index in ISN_TRY
8884 // instruction
8885 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008886 if (cctx->ctx_skip != SKIP_YES
8887 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8888 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008889#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008890 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008891 generate_instr(cctx, ISN_PROF_START);
8892#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894 return arg;
8895}
8896
8897/*
8898 * compile "throw {expr}"
8899 */
8900 static char_u *
8901compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8902{
8903 char_u *p = skipwhite(arg);
8904
Bram Moolenaara5565e42020-05-09 15:44:01 +02008905 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008906 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008907 if (cctx->ctx_skip == SKIP_YES)
8908 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008909 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910 return NULL;
8911 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8912 return NULL;
8913
8914 return p;
8915}
8916
Bram Moolenaarc3235272021-07-10 19:42:03 +02008917 static char_u *
8918compile_eval(char_u *arg, cctx_T *cctx)
8919{
8920 char_u *p = arg;
8921 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008922 long lnum = SOURCING_LNUM;
8923
8924 // find_ex_command() will consider a variable name an expression, assuming
8925 // that something follows on the next line. Check that something actually
8926 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008927 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008928
Bram Moolenaarc3235272021-07-10 19:42:03 +02008929 if (compile_expr0(&p, cctx) == FAIL)
8930 return NULL;
8931
8932 if (name_only && lnum == SOURCING_LNUM)
8933 {
8934 semsg(_(e_expression_without_effect_str), arg);
8935 return NULL;
8936 }
8937
8938 // drop the result
8939 generate_instr_drop(cctx, ISN_DROP, 1);
8940
8941 return skipwhite(p);
8942}
8943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944/*
8945 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008946 * compile "echomsg expr"
8947 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008948 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008949 * compile "execute expr"
8950 */
8951 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008952compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008953{
8954 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008955 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008956 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008957 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008958 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008959 garray_T *stack = &cctx->ctx_type_stack;
8960 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008961
8962 for (;;)
8963 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008964 if (ends_excmd2(prev, p))
8965 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008966 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008967 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008968 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008969
8970 if (cctx->ctx_skip != SKIP_YES)
8971 {
8972 // check for non-void type
8973 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8974 if (type->tt_type == VAR_VOID)
8975 {
8976 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8977 return NULL;
8978 }
8979 }
8980
Bram Moolenaarad39c092020-02-26 18:23:43 +01008981 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008982 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008983 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008984 }
8985
Bram Moolenaare4984292020-12-13 14:19:25 +01008986 if (count > 0)
8987 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008988 long save_lnum = cctx->ctx_lnum;
8989
8990 // Use the line number where the command started.
8991 cctx->ctx_lnum = start_ctx_lnum;
8992
Bram Moolenaare4984292020-12-13 14:19:25 +01008993 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8994 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8995 else if (cmdidx == CMD_execute)
8996 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8997 else if (cmdidx == CMD_echomsg)
8998 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02008999 else if (cmdidx == CMD_echoconsole)
9000 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009001 else
9002 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009003
9004 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 return p;
9007}
9008
9009/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009010 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009011 * instruction to compute it and return OK.
9012 * Otherwise return FAIL, the caller must deal with any range.
9013 */
9014 static int
9015compile_variable_range(exarg_T *eap, cctx_T *cctx)
9016{
9017 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9018 char_u *p = skipdigits(eap->cmd);
9019
9020 if (p == range_end)
9021 return FAIL;
9022 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9023}
9024
9025/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009026 * :put r
9027 * :put ={expr}
9028 */
9029 static char_u *
9030compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9031{
9032 char_u *line = arg;
9033 linenr_T lnum;
9034 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009035 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009036
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009037 eap->regname = *line;
9038
9039 if (eap->regname == '=')
9040 {
9041 char_u *p = line + 1;
9042
9043 if (compile_expr0(&p, cctx) == FAIL)
9044 return NULL;
9045 line = p;
9046 }
9047 else if (eap->regname != NUL)
9048 ++line;
9049
Bram Moolenaar08597872020-12-10 19:43:40 +01009050 if (compile_variable_range(eap, cctx) == OK)
9051 {
9052 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9053 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009054 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009055 {
9056 // Either no range or a number.
9057 // "errormsg" will not be set because the range is ADDR_LINES.
9058 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009059 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009060 return NULL;
9061 if (eap->addr_count == 0)
9062 lnum = -1;
9063 else
9064 lnum = eap->line2;
9065 if (above)
9066 --lnum;
9067 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009068
9069 generate_PUT(cctx, eap->regname, lnum);
9070 return line;
9071}
9072
9073/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009074 * A command that is not compiled, execute with legacy code.
9075 */
9076 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009077compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009078{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009079 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009080 char_u *p;
9081 int has_expr = FALSE;
9082 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009083 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009084
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009085 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009086 goto theend;
9087
Bram Moolenaare729ce22021-06-06 21:38:09 +02009088 // If there was a prececing command modifier, drop it and include it in the
9089 // EXEC command.
9090 if (cctx->ctx_has_cmdmod)
9091 {
9092 garray_T *instr = &cctx->ctx_instr;
9093 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9094
9095 if (isn->isn_type == ISN_CMDMOD)
9096 {
9097 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9098 ->cmod_filter_regmatch.regprog);
9099 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9100 --instr->ga_len;
9101 cctx->ctx_has_cmdmod = FALSE;
9102 }
9103 }
9104
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009105 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009106 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009107 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009108 int usefilter = FALSE;
9109
9110 has_expr = argt & (EX_XFILE | EX_EXPAND);
9111
9112 // If the command can be followed by a bar, find the bar and truncate
9113 // it, so that the following command can be compiled.
9114 // The '|' is overwritten with a NUL, it is put back below.
9115 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9116 && *eap->arg == '!')
9117 // :w !filter or :r !filter or :r! filter
9118 usefilter = TRUE;
9119 if ((argt & EX_TRLBAR) && !usefilter)
9120 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009121 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009122 separate_nextcmd(eap);
9123 if (eap->nextcmd != NULL)
9124 nextcmd = eap->nextcmd;
9125 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009126 else if (eap->cmdidx == CMD_wincmd)
9127 {
9128 p = eap->arg;
9129 if (*p != NUL)
9130 ++p;
9131 if (*p == 'g' || *p == Ctrl_G)
9132 ++p;
9133 p = skipwhite(p);
9134 if (*p == '|')
9135 {
9136 *p = NUL;
9137 nextcmd = p + 1;
9138 }
9139 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009140 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9141 {
9142 // If there is a trailing '{' read lines until the '}'
9143 p = eap->arg + STRLEN(eap->arg) - 1;
9144 while (p > eap->arg && VIM_ISWHITE(*p))
9145 --p;
9146 if (*p == '{')
9147 {
9148 exarg_T ea;
9149 int flags; // unused
9150 int start_lnum = SOURCING_LNUM;
9151
9152 CLEAR_FIELD(ea);
9153 ea.arg = eap->arg;
9154 fill_exarg_from_cctx(&ea, cctx);
9155 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9156 if (tofree != NULL)
9157 {
9158 *p = NUL;
9159 line = concat_str(line, tofree);
9160 if (line == NULL)
9161 goto theend;
9162 vim_free(tofree);
9163 tofree = line;
9164 SOURCING_LNUM = start_lnum;
9165 }
9166 }
9167 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009168 }
9169
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009170 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9171 {
9172 // expand filename in "syntax include [@group] filename"
9173 has_expr = TRUE;
9174 eap->arg = skipwhite(eap->arg + 7);
9175 if (*eap->arg == '@')
9176 eap->arg = skiptowhite(eap->arg);
9177 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009178
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009179 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9180 && STRLEN(eap->arg) > 4)
9181 {
9182 int delim = *eap->arg;
9183
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009184 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009185 if (*p == delim)
9186 {
9187 eap->arg = p + 1;
9188 has_expr = TRUE;
9189 }
9190 }
9191
Bram Moolenaarecac5912021-01-05 19:23:28 +01009192 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
9193 {
9194 // TODO: should only expand when appropriate for the command
9195 eap->arg = skiptowhite(eap->arg);
9196 has_expr = TRUE;
9197 }
9198
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009199 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009200 {
9201 int count = 0;
9202 char_u *start = skipwhite(line);
9203
9204 // :cmd xxx`=expr1`yyy`=expr2`zzz
9205 // PUSHS ":cmd xxx"
9206 // eval expr1
9207 // PUSHS "yyy"
9208 // eval expr2
9209 // PUSHS "zzz"
9210 // EXECCONCAT 5
9211 for (;;)
9212 {
9213 if (p > start)
9214 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009215 char_u *val = vim_strnsave(start, p - start);
9216
9217 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009218 ++count;
9219 }
9220 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009221 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009222 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009223 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009224 ++count;
9225 p = skipwhite(p);
9226 if (*p != '`')
9227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009228 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009229 return NULL;
9230 }
9231 start = p + 1;
9232
9233 p = (char_u *)strstr((char *)start, "`=");
9234 if (p == NULL)
9235 {
9236 if (*skipwhite(start) != NUL)
9237 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009238 char_u *val = vim_strsave(start);
9239
9240 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009241 ++count;
9242 }
9243 break;
9244 }
9245 }
9246 generate_EXECCONCAT(cctx, count);
9247 }
9248 else
Bram Moolenaaraacc9662021-08-13 19:40:51 +02009249 generate_EXEC(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009250
9251theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009252 if (*nextcmd != NUL)
9253 {
9254 // the parser expects a pointer to the bar, put it back
9255 --nextcmd;
9256 *nextcmd = '|';
9257 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009258 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009259
9260 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009261}
9262
Bram Moolenaar20677332021-06-06 17:02:53 +02009263/*
9264 * A script command with heredoc, e.g.
9265 * ruby << EOF
9266 * command
9267 * EOF
9268 * Has been turned into one long line with NL characters by
9269 * get_function_body():
9270 * ruby << EOF<NL> command<NL>EOF
9271 */
9272 static char_u *
9273compile_script(char_u *line, cctx_T *cctx)
9274{
9275 if (cctx->ctx_skip != SKIP_YES)
9276 {
9277 isn_T *isn;
9278
9279 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9280 return NULL;
9281 isn->isn_arg.string = vim_strsave(line);
9282 }
9283 return (char_u *)"";
9284}
9285
Bram Moolenaar8238f082021-04-20 21:10:48 +02009286
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009287/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009288 * :s/pat/repl/
9289 */
9290 static char_u *
9291compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9292{
9293 char_u *cmd = eap->arg;
9294 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9295
9296 if (expr != NULL)
9297 {
9298 int delimiter = *cmd++;
9299
9300 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009301 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009302 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9303 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009304 garray_T save_ga = cctx->ctx_instr;
9305 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009306 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009307 int trailing_error;
9308 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009309 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009310 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009311
9312 cmd += 3;
9313 end = skip_substitute(cmd, delimiter);
9314
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009315 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009316 // labels are correct.
9317 cctx->ctx_instr.ga_len = 0;
9318 cctx->ctx_instr.ga_maxlen = 0;
9319 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009320 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009321 if (end[-1] == NUL)
9322 end[-1] = delimiter;
9323 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009324 trailing_error = *cmd != delimiter && *cmd != NUL;
9325
Bram Moolenaar169502f2021-04-21 12:19:35 +02009326 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009327 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009328 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009329 if (trailing_error)
9330 semsg(_(e_trailing_arg), cmd);
9331 clear_instr_ga(&cctx->ctx_instr);
9332 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009333 return NULL;
9334 }
9335
Bram Moolenaar8238f082021-04-20 21:10:48 +02009336 // Move the generated instructions into the ISN_SUBSTITUTE
9337 // instructions, then restore the list of instructions before
9338 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009339 instr_count = cctx->ctx_instr.ga_len;
9340 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009341 instr[instr_count].isn_type = ISN_FINISH;
9342
9343 cctx->ctx_instr = save_ga;
9344 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9345 {
9346 int idx;
9347
9348 for (idx = 0; idx < instr_count; ++idx)
9349 delete_instr(instr + idx);
9350 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009351 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009352 }
9353 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9354 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009355
9356 // skip over flags
9357 if (*end == '&')
9358 ++end;
9359 while (ASCII_ISALPHA(*end) || *end == '#')
9360 ++end;
9361 return end;
9362 }
9363 }
9364
9365 return compile_exec(arg, eap, cctx);
9366}
9367
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009368 static char_u *
9369compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9370{
9371 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009372 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009373
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009374 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009375 {
9376 if (STRNCMP(arg, "END", 3) == 0)
9377 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009378 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009379 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009380 // First load the current variable value.
9381 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9382 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009383 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009384 }
9385
9386 // Gets the redirected text and put it on the stack, then store it
9387 // in the variable.
9388 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9389
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009390 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009391 generate_instr_drop(cctx, ISN_CONCAT, 1);
9392
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009393 if (lhs->lhs_has_index)
9394 {
9395 // Use the info in "lhs" to store the value at the index in the
9396 // list or dict.
9397 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9398 &t_string, cctx) == FAIL)
9399 return NULL;
9400 }
9401 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009402 return NULL;
9403
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009404 VIM_CLEAR(lhs->lhs_name);
9405 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009406 return arg + 3;
9407 }
9408 emsg(_(e_cannot_nest_redir));
9409 return NULL;
9410 }
9411
9412 if (arg[0] == '=' && arg[1] == '>')
9413 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009414 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009415
9416 // redirect to a variable is compiled
9417 arg += 2;
9418 if (*arg == '>')
9419 {
9420 ++arg;
9421 append = TRUE;
9422 }
9423 arg = skipwhite(arg);
9424
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009425 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009426 FALSE, FALSE, 1, cctx) == FAIL)
9427 return NULL;
9428 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009429 lhs->lhs_append = append;
9430 if (lhs->lhs_has_index)
9431 {
9432 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9433 if (lhs->lhs_whole == NULL)
9434 return NULL;
9435 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009436
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009437 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009438 }
9439
9440 // other redirects are handled like at script level
9441 return compile_exec(line, eap, cctx);
9442}
9443
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009444#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009445 static char_u *
9446compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9447{
9448 isn_T *isn;
9449 char_u *p;
9450
9451 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9452 if (isn == NULL)
9453 return NULL;
9454 isn->isn_arg.number = eap->cmdidx;
9455
9456 p = eap->arg;
9457 if (compile_expr0(&p, cctx) == FAIL)
9458 return NULL;
9459
9460 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9461 if (isn == NULL)
9462 return NULL;
9463 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9464 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9465 return NULL;
9466 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9467 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9468 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9469
9470 return p;
9471}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009472#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009473
Bram Moolenaar4c137212021-04-19 16:48:48 +02009474/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009475 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009476 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009477 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009478 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009479add_def_function(ufunc_T *ufunc)
9480{
9481 dfunc_T *dfunc;
9482
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009483 if (def_functions.ga_len == 0)
9484 {
9485 // The first position is not used, so that a zero uf_dfunc_idx means it
9486 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009487 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009488 return FAIL;
9489 ++def_functions.ga_len;
9490 }
9491
Bram Moolenaar09689a02020-05-09 22:50:08 +02009492 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009493 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009494 return FAIL;
9495 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9496 CLEAR_POINTER(dfunc);
9497 dfunc->df_idx = def_functions.ga_len;
9498 ufunc->uf_dfunc_idx = dfunc->df_idx;
9499 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009500 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009501 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009502 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009503 ++def_functions.ga_len;
9504 return OK;
9505}
9506
9507/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009508 * After ex_function() has collected all the function lines: parse and compile
9509 * the lines into instructions.
9510 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009511 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9512 * the return statement (used for lambda). When uf_ret_type is already set
9513 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009514 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009515 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009516 * This can be used recursively through compile_lambda(), which may reallocate
9517 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009518 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009519 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009520 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009521compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009522 ufunc_T *ufunc,
9523 int check_return_type,
9524 compiletype_T compile_type,
9525 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009526{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009527 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009528 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009529 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009530 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009531 cctx_T cctx;
9532 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009533 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009534 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 int ret = FAIL;
9536 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009537 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009538 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009539 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009540 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009541#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009542 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009543#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009544 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009545
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009546 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009547 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009548 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009549 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009550 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9551 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009552 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009553
9554 switch (compile_type)
9555 {
9556 case CT_PROFILE:
9557#ifdef FEAT_PROFILE
9558 instr_dest = dfunc->df_instr_prof; break;
9559#endif
9560 case CT_NONE: instr_dest = dfunc->df_instr; break;
9561 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9562 }
9563 if (instr_dest != NULL)
9564 // Was compiled in this mode before: Free old instructions.
9565 delete_def_function_contents(dfunc, FALSE);
9566 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009567 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009568 else
9569 {
9570 if (add_def_function(ufunc) == FAIL)
9571 return FAIL;
9572 new_def_function = TRUE;
9573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009574
Bram Moolenaar985116a2020-07-12 17:31:09 +02009575 ufunc->uf_def_status = UF_COMPILING;
9576
Bram Moolenaara80faa82020-04-12 19:37:17 +02009577 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009578
Bram Moolenaare99d4222021-06-13 14:01:26 +02009579 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009580 cctx.ctx_ufunc = ufunc;
9581 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009582 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009583 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9584 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9585 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9586 cctx.ctx_type_list = &ufunc->uf_type_list;
9587 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9588 instr = &cctx.ctx_instr;
9589
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009590 // Set the context to the function, it may be compiled when called from
9591 // another script. Set the script version to the most modern one.
9592 // The line number will be set in next_line_from_context().
9593 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009594 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9595
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009596 // Don't use the flag from ":legacy" here.
9597 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9598
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009599 // Make sure error messages are OK.
9600 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9601 if (do_estack_push)
9602 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009603 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009604
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009605 if (ufunc->uf_def_args.ga_len > 0)
9606 {
9607 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009608 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009609 int i;
9610 char_u *arg;
9611 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009612 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009613
9614 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009615 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009616 for (i = 0; i < count; ++i)
9617 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009618 garray_T *stack = &cctx.ctx_type_stack;
9619 type_T *val_type;
9620 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009621 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009622 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009623 int jump_instr_idx = instr->ga_len;
9624 isn_T *isn;
9625
9626 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9627 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9628 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009629
9630 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009631 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009632
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009633 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009634 r = compile_expr0(&arg, &cctx);
9635
Bram Moolenaar12bce952021-03-11 20:04:04 +01009636 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009637 goto erret;
9638
9639 // If no type specified use the type of the default value.
9640 // Otherwise check that the default value type matches the
9641 // specified type.
9642 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009643 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009644 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009645 {
9646 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009647 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009648 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009649 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009650 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009651 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009652
9653 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009654 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009655
9656 // set instruction index in JUMP_IF_ARG_SET to here
9657 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9658 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009659 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009660
9661 if (did_set_arg_type)
9662 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009663 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009664 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009665
9666 /*
9667 * Loop over all the lines of the function and generate instructions.
9668 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009669 for (;;)
9670 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009671 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009672 int starts_with_colon = FALSE;
9673 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009674 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009675
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009676 // Bail out on the first error to avoid a flood of errors and report
9677 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009678 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009679 goto erret;
9680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009681 if (line != NULL && *line == '|')
9682 // the line continues after a '|'
9683 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009684 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009685 && !(*line == '#' && (line == cctx.ctx_line_start
9686 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009687 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009688 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009689 goto erret;
9690 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009691 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9692 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009693 else
9694 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009695 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009696 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009697 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009698 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009699#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009700 if (cctx.ctx_skip != SKIP_YES)
9701 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009702#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009704 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009705 // Make a copy, splitting off nextcmd and removing trailing spaces
9706 // may change it.
9707 if (line != NULL)
9708 {
9709 line = vim_strsave(line);
9710 vim_free(line_to_free);
9711 line_to_free = line;
9712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009713 }
9714
Bram Moolenaara80faa82020-04-12 19:37:17 +02009715 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009716 ea.cmdlinep = &line;
9717 ea.cmd = skipwhite(line);
9718
Bram Moolenaarb2049902021-01-24 12:53:53 +01009719 if (*ea.cmd == '#')
9720 {
9721 // "#" starts a comment
9722 line = (char_u *)"";
9723 continue;
9724 }
9725
Bram Moolenaarf002a412021-01-24 13:34:18 +01009726#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009727 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9728 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009729 {
9730 may_generate_prof_end(&cctx, prof_lnum);
9731
9732 prof_lnum = cctx.ctx_lnum;
9733 generate_instr(&cctx, ISN_PROF_START);
9734 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009735#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009736 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9737 && cctx.ctx_skip != SKIP_YES)
9738 {
9739 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009740 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009741 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009742 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009743
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009744 // Some things can be recognized by the first character.
9745 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009746 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009747 case '}':
9748 {
9749 // "}" ends a block scope
9750 scopetype_T stype = cctx.ctx_scope == NULL
9751 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009752
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009753 if (stype == BLOCK_SCOPE)
9754 {
9755 compile_endblock(&cctx);
9756 line = ea.cmd;
9757 }
9758 else
9759 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009760 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009761 goto erret;
9762 }
9763 if (line != NULL)
9764 line = skipwhite(ea.cmd + 1);
9765 continue;
9766 }
9767
9768 case '{':
9769 // "{" starts a block scope
9770 // "{'a': 1}->func() is something else
9771 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9772 {
9773 line = compile_block(ea.cmd, &cctx);
9774 continue;
9775 }
9776 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009777 }
9778
9779 /*
9780 * COMMAND MODIFIERS
9781 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009782 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009783 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9784 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009785 {
9786 if (errormsg != NULL)
9787 goto erret;
9788 // empty line or comment
9789 line = (char_u *)"";
9790 continue;
9791 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009792 generate_cmdmods(&cctx, &local_cmdmod);
9793 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009795 // Check if there was a colon after the last command modifier or before
9796 // the current position.
9797 for (p = ea.cmd; p >= line; --p)
9798 {
9799 if (*p == ':')
9800 starts_with_colon = TRUE;
9801 if (p < ea.cmd && !VIM_ISWHITE(*p))
9802 break;
9803 }
9804
Bram Moolenaarce024c32021-06-26 13:00:49 +02009805 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009806 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009807 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009808 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009809 if (checkforcmd(&ea.cmd, "call", 3))
9810 {
9811 if (*ea.cmd == '(')
9812 // not for "call()"
9813 ea.cmd = p;
9814 else
9815 ea.cmd = skipwhite(ea.cmd);
9816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009817
Bram Moolenaarce024c32021-06-26 13:00:49 +02009818 if (!starts_with_colon)
9819 {
9820 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009821
Bram Moolenaarce024c32021-06-26 13:00:49 +02009822 // Check for assignment after command modifiers.
9823 assign = may_compile_assignment(&ea, &line, &cctx);
9824 if (assign == OK)
9825 goto nextline;
9826 if (assign == FAIL)
9827 goto erret;
9828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009829 }
9830
9831 /*
9832 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009833 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009834 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009835 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009836 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009837 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009838 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009839 && (*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009840 && (starts_with_colon || !(*cmd == '\''
9841 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009842 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009843 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009844 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009845 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009846 if (!starts_with_colon)
9847 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009848 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009849 goto erret;
9850 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009851 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009852 if (ends_excmd2(line, ea.cmd))
9853 {
9854 // A range without a command: jump to the line.
9855 // TODO: compile to a more efficient command, possibly
9856 // calling parse_cmd_address().
9857 ea.cmdidx = CMD_SIZE;
9858 line = compile_exec(line, &ea, &cctx);
9859 goto nextline;
9860 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009861 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009862 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009863 p = find_ex_command(&ea, NULL,
9864 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009865 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009866
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009867 if (p == NULL)
9868 {
9869 if (cctx.ctx_skip != SKIP_YES)
9870 emsg(_(e_ambiguous_use_of_user_defined_command));
9871 goto erret;
9872 }
9873
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009874 // When using ":legacy cmd" always use compile_exec().
9875 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009876 {
9877 char_u *start = ea.cmd;
9878
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009879 switch (ea.cmdidx)
9880 {
9881 case CMD_if:
9882 case CMD_elseif:
9883 case CMD_else:
9884 case CMD_endif:
9885 case CMD_for:
9886 case CMD_endfor:
9887 case CMD_continue:
9888 case CMD_break:
9889 case CMD_while:
9890 case CMD_endwhile:
9891 case CMD_try:
9892 case CMD_catch:
9893 case CMD_finally:
9894 case CMD_endtry:
9895 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9896 goto erret;
9897 default: break;
9898 }
9899
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009900 // ":legacy return expr" needs to be handled differently.
9901 if (checkforcmd(&start, "return", 4))
9902 ea.cmdidx = CMD_return;
9903 else
9904 ea.cmdidx = CMD_legacy;
9905 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009907 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9908 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009909 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009910 {
9911 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009912 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009913 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009914 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009915 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009916 // CMD_var cannot happen, compile_assignment() above would be
9917 // used. Most likely an assignment to a non-existing variable.
9918 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009919 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009921 }
9922
Bram Moolenaar3988f642020-08-27 22:43:03 +02009923 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009924 && ea.cmdidx != CMD_elseif
9925 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009926 && ea.cmdidx != CMD_endif
9927 && ea.cmdidx != CMD_endfor
9928 && ea.cmdidx != CMD_endwhile
9929 && ea.cmdidx != CMD_catch
9930 && ea.cmdidx != CMD_finally
9931 && ea.cmdidx != CMD_endtry)
9932 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009933 emsg(_(e_unreachable_code_after_return));
9934 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009935 }
9936
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009937 p = skipwhite(p);
9938 if (ea.cmdidx != CMD_SIZE
9939 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9940 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009941 if (ea.cmdidx >= 0)
9942 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009943 if ((ea.argt & EX_BANG) && *p == '!')
9944 {
9945 ea.forceit = TRUE;
9946 p = skipwhite(p + 1);
9947 }
9948 }
9949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 switch (ea.cmdidx)
9951 {
9952 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009953 ea.arg = p;
9954 line = compile_nested_function(&ea, &cctx);
9955 break;
9956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009957 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009958 // TODO: should we allow this, e.g. to declare a global
9959 // function?
9960 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 goto erret;
9962
9963 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009964 line = compile_return(p, check_return_type,
9965 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009966 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967 break;
9968
9969 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009970 emsg(_(e_cannot_use_let_in_vim9_script));
9971 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009972 case CMD_var:
9973 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009975 case CMD_increment:
9976 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009977 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009978 if (line == p)
9979 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009980 break;
9981
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009982 case CMD_unlet:
9983 case CMD_unlockvar:
9984 case CMD_lockvar:
9985 line = compile_unletlock(p, &ea, &cctx);
9986 break;
9987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009988 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009989 emsg(_(e_import_can_only_be_used_in_script));
9990 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009991 break;
9992
9993 case CMD_if:
9994 line = compile_if(p, &cctx);
9995 break;
9996 case CMD_elseif:
9997 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009998 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009999 break;
10000 case CMD_else:
10001 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010002 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010003 break;
10004 case CMD_endif:
10005 line = compile_endif(p, &cctx);
10006 break;
10007
10008 case CMD_while:
10009 line = compile_while(p, &cctx);
10010 break;
10011 case CMD_endwhile:
10012 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010013 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010014 break;
10015
10016 case CMD_for:
10017 line = compile_for(p, &cctx);
10018 break;
10019 case CMD_endfor:
10020 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010021 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010022 break;
10023 case CMD_continue:
10024 line = compile_continue(p, &cctx);
10025 break;
10026 case CMD_break:
10027 line = compile_break(p, &cctx);
10028 break;
10029
10030 case CMD_try:
10031 line = compile_try(p, &cctx);
10032 break;
10033 case CMD_catch:
10034 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010035 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010036 break;
10037 case CMD_finally:
10038 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010039 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010040 break;
10041 case CMD_endtry:
10042 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010043 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010044 break;
10045 case CMD_throw:
10046 line = compile_throw(p, &cctx);
10047 break;
10048
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010049 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010050 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010051 break;
10052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010054 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010055 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010056 case CMD_echomsg:
10057 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010058 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010059 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010060 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010061
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010062 case CMD_put:
10063 ea.cmd = cmd;
10064 line = compile_put(p, &ea, &cctx);
10065 break;
10066
Bram Moolenaar4c137212021-04-19 16:48:48 +020010067 case CMD_substitute:
10068 if (cctx.ctx_skip == SKIP_YES)
10069 line = (char_u *)"";
10070 else
10071 {
10072 ea.arg = p;
10073 line = compile_substitute(line, &ea, &cctx);
10074 }
10075 break;
10076
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010077 case CMD_redir:
10078 ea.arg = p;
10079 line = compile_redir(line, &ea, &cctx);
10080 break;
10081
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010082 case CMD_cexpr:
10083 case CMD_lexpr:
10084 case CMD_caddexpr:
10085 case CMD_laddexpr:
10086 case CMD_cgetexpr:
10087 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010088#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010089 ea.arg = p;
10090 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010091#else
10092 ex_ni(&ea);
10093 line = NULL;
10094#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010095 break;
10096
Bram Moolenaarae616492020-07-28 20:07:27 +020010097 case CMD_append:
10098 case CMD_change:
10099 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010100 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010101 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010102 case CMD_xit:
10103 not_in_vim9(&ea);
10104 goto erret;
10105
Bram Moolenaar002262f2020-07-08 17:47:57 +020010106 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010107 if (cctx.ctx_skip != SKIP_YES)
10108 {
10109 semsg(_(e_invalid_command_str), ea.cmd);
10110 goto erret;
10111 }
10112 // We don't check for a next command here.
10113 line = (char_u *)"";
10114 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010115
Bram Moolenaar20677332021-06-06 17:02:53 +020010116 case CMD_lua:
10117 case CMD_mzscheme:
10118 case CMD_perl:
10119 case CMD_py3:
10120 case CMD_python3:
10121 case CMD_python:
10122 case CMD_pythonx:
10123 case CMD_ruby:
10124 case CMD_tcl:
10125 ea.arg = p;
10126 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010127 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010128 else
10129 // heredoc lines have been concatenated with NL
10130 // characters in get_function_body()
10131 line = compile_script(line, &cctx);
10132 break;
10133
10134 default:
10135 // Not recognized, execute with do_cmdline_cmd().
10136 ea.arg = p;
10137 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010138 break;
10139 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010140nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010141 if (line == NULL)
10142 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010143 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010145 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010146 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010148 if (cctx.ctx_type_stack.ga_len < 0)
10149 {
10150 iemsg("Type stack underflow");
10151 goto erret;
10152 }
10153 }
10154
10155 if (cctx.ctx_scope != NULL)
10156 {
10157 if (cctx.ctx_scope->se_type == IF_SCOPE)
10158 emsg(_(e_endif));
10159 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10160 emsg(_(e_endwhile));
10161 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10162 emsg(_(e_endfor));
10163 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010164 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010165 goto erret;
10166 }
10167
Bram Moolenaarefd88552020-06-18 20:50:10 +020010168 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010169 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010170 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10171 ufunc->uf_ret_type = &t_void;
10172 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010173 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010174 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010175 goto erret;
10176 }
10177
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010178 // Return void if there is no return at the end.
10179 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010180 }
10181
Bram Moolenaar599410c2021-04-10 14:03:43 +020010182 // When compiled with ":silent!" and there was an error don't consider the
10183 // function compiled.
10184 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010185 {
10186 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10187 + ufunc->uf_dfunc_idx;
10188 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010189 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010190#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010191 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010192 {
10193 dfunc->df_instr_prof = instr->ga_data;
10194 dfunc->df_instr_prof_count = instr->ga_len;
10195 }
10196 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010197#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010198 if (cctx.ctx_compile_type == CT_DEBUG)
10199 {
10200 dfunc->df_instr_debug = instr->ga_data;
10201 dfunc->df_instr_debug_count = instr->ga_len;
10202 }
10203 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010204 {
10205 dfunc->df_instr = instr->ga_data;
10206 dfunc->df_instr_count = instr->ga_len;
10207 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010208 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010209 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010210 if (cctx.ctx_outer_used)
10211 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010212 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010214
10215 ret = OK;
10216
10217erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010218 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010219 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10221 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010222
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010223 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010224 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010225 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010226 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010227
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010228 // If using the last entry in the table and it was added above, we
10229 // might as well remove it.
10230 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010231 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010232 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010233 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010234 ufunc->uf_dfunc_idx = 0;
10235 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010236 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010237
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010238 while (cctx.ctx_scope != NULL)
10239 drop_scope(&cctx);
10240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 if (errormsg != NULL)
10242 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010243 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010244 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010245 }
10246
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010247 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10248 {
10249 if (ret == OK)
10250 {
10251 emsg(_(e_missing_redir_end));
10252 ret = FAIL;
10253 }
10254 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010255 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010256 }
10257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010259 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010260 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010261 if (do_estack_push)
10262 estack_pop();
10263
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010264 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010265 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010266 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010267 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010268 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010269}
10270
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010271 void
10272set_function_type(ufunc_T *ufunc)
10273{
10274 int varargs = ufunc->uf_va_name != NULL;
10275 int argcount = ufunc->uf_args.ga_len;
10276
10277 // Create a type for the function, with the return type and any
10278 // argument types.
10279 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10280 // The type is included in "tt_args".
10281 if (argcount > 0 || varargs)
10282 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010283 if (ufunc->uf_type_list.ga_itemsize == 0)
10284 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010285 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10286 argcount, &ufunc->uf_type_list);
10287 // Add argument types to the function type.
10288 if (func_type_add_arg_types(ufunc->uf_func_type,
10289 argcount + varargs,
10290 &ufunc->uf_type_list) == FAIL)
10291 return;
10292 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10293 ufunc->uf_func_type->tt_min_argcount =
10294 argcount - ufunc->uf_def_args.ga_len;
10295 if (ufunc->uf_arg_types == NULL)
10296 {
10297 int i;
10298
10299 // lambda does not have argument types.
10300 for (i = 0; i < argcount; ++i)
10301 ufunc->uf_func_type->tt_args[i] = &t_any;
10302 }
10303 else
10304 mch_memmove(ufunc->uf_func_type->tt_args,
10305 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10306 if (varargs)
10307 {
10308 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010309 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010310 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10311 }
10312 }
10313 else
10314 // No arguments, can use a predefined type.
10315 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10316 argcount, &ufunc->uf_type_list);
10317}
10318
10319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010320/*
10321 * Delete an instruction, free what it contains.
10322 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010323 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010324delete_instr(isn_T *isn)
10325{
10326 switch (isn->isn_type)
10327 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010328 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010329 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010330 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010331 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010332 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010333 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010334 case ISN_LOADENV:
10335 case ISN_LOADG:
10336 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010337 case ISN_LOADT:
10338 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010339 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010340 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010341 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010342 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010343 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010344 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010345 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010346 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010347 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010348 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010349 case ISN_STOREW:
10350 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 vim_free(isn->isn_arg.string);
10352 break;
10353
Bram Moolenaar4c137212021-04-19 16:48:48 +020010354 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010355 {
10356 int idx;
10357 isn_T *list = isn->isn_arg.subs.subs_instr;
10358
10359 vim_free(isn->isn_arg.subs.subs_cmd);
10360 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10361 delete_instr(list + idx);
10362 vim_free(list);
10363 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010364 break;
10365
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010366 case ISN_INSTR:
10367 {
10368 int idx;
10369 isn_T *list = isn->isn_arg.instr;
10370
10371 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10372 delete_instr(list + idx);
10373 vim_free(list);
10374 }
10375 break;
10376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010377 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010378 case ISN_STORES:
10379 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010380 break;
10381
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010382 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010383 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010384 vim_free(isn->isn_arg.unlet.ul_name);
10385 break;
10386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010387 case ISN_STOREOPT:
10388 vim_free(isn->isn_arg.storeopt.so_name);
10389 break;
10390
10391 case ISN_PUSHBLOB: // push blob isn_arg.blob
10392 blob_unref(isn->isn_arg.blob);
10393 break;
10394
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010395 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010396#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010397 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010398#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010399 break;
10400
10401 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010402#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010403 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010404#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010405 break;
10406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010407 case ISN_UCALL:
10408 vim_free(isn->isn_arg.ufunc.cuf_name);
10409 break;
10410
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010411 case ISN_FUNCREF:
10412 {
10413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10414 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010415 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010416
Bram Moolenaar077a4232020-12-22 18:33:27 +010010417 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10418 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010419 }
10420 break;
10421
10422 case ISN_DCALL:
10423 {
10424 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10425 + isn->isn_arg.dfunc.cdf_idx;
10426
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010427 if (dfunc->df_ufunc != NULL
10428 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010429 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010430 }
10431 break;
10432
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010433 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010434 {
10435 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10436 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10437
10438 if (ufunc != NULL)
10439 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010440 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010441 func_ptr_unref(ufunc);
10442 }
10443
10444 vim_free(lambda);
10445 vim_free(isn->isn_arg.newfunc.nf_global);
10446 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010447 break;
10448
Bram Moolenaar5e654232020-09-16 15:22:00 +020010449 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010450 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010451 free_type(isn->isn_arg.type.ct_type);
10452 break;
10453
Bram Moolenaar02194d22020-10-24 23:08:38 +020010454 case ISN_CMDMOD:
10455 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10456 ->cmod_filter_regmatch.regprog);
10457 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10458 break;
10459
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010460 case ISN_LOADSCRIPT:
10461 case ISN_STORESCRIPT:
10462 vim_free(isn->isn_arg.script.scriptref);
10463 break;
10464
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010465 case ISN_TRY:
10466 vim_free(isn->isn_arg.try.try_ref);
10467 break;
10468
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010469 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010470 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010471 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10472 break;
10473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010474 case ISN_2BOOL:
10475 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010476 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010477 case ISN_ADDBLOB:
10478 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010479 case ISN_ANYINDEX:
10480 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010481 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010482 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010483 case ISN_BLOBINDEX:
10484 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010485 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010486 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010487 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010488 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010489 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010490 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010491 case ISN_COMPAREANY:
10492 case ISN_COMPAREBLOB:
10493 case ISN_COMPAREBOOL:
10494 case ISN_COMPAREDICT:
10495 case ISN_COMPAREFLOAT:
10496 case ISN_COMPAREFUNC:
10497 case ISN_COMPARELIST:
10498 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010499 case ISN_COMPARESPECIAL:
10500 case ISN_COMPARESTRING:
10501 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010502 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010503 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010504 case ISN_DROP:
10505 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010506 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010507 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010508 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010509 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010510 case ISN_EXECCONCAT:
10511 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010512 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010513 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010514 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010515 case ISN_GETITEM:
10516 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010517 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010518 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010519 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010520 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010521 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010522 case ISN_LOADBDICT:
10523 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010524 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010525 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010526 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010527 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010528 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010529 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010530 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010531 case ISN_NEGATENR:
10532 case ISN_NEWDICT:
10533 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010534 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010535 case ISN_OPFLOAT:
10536 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010537 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010538 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010539 case ISN_PROF_END:
10540 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010541 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010542 case ISN_PUSHF:
10543 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010544 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010545 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010546 case ISN_REDIREND:
10547 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010548 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010549 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010550 case ISN_SHUFFLE:
10551 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010552 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010553 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010554 case ISN_STORENR:
10555 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010556 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010557 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010558 case ISN_STOREV:
10559 case ISN_STRINDEX:
10560 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010561 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010562 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010563 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010564 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010565 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010566 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010567 // nothing allocated
10568 break;
10569 }
10570}
10571
10572/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010573 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010574 */
10575 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010576delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010577{
10578 int idx;
10579
10580 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010581 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010582
10583 if (dfunc->df_instr != NULL)
10584 {
10585 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10586 delete_instr(dfunc->df_instr + idx);
10587 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010588 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010589 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010590 if (dfunc->df_instr_debug != NULL)
10591 {
10592 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10593 delete_instr(dfunc->df_instr_debug + idx);
10594 VIM_CLEAR(dfunc->df_instr_debug);
10595 dfunc->df_instr_debug = NULL;
10596 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010597#ifdef FEAT_PROFILE
10598 if (dfunc->df_instr_prof != NULL)
10599 {
10600 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10601 delete_instr(dfunc->df_instr_prof + idx);
10602 VIM_CLEAR(dfunc->df_instr_prof);
10603 dfunc->df_instr_prof = NULL;
10604 }
10605#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010606
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010607 if (mark_deleted)
10608 dfunc->df_deleted = TRUE;
10609 if (dfunc->df_ufunc != NULL)
10610 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010611}
10612
10613/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010614 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010615 * function, unless another user function still uses it.
10616 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010617 */
10618 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010619unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010620{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010621 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 {
10623 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10624 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010625
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010626 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010627 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010628 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010629 ufunc->uf_dfunc_idx = 0;
10630 if (dfunc->df_ufunc == ufunc)
10631 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 }
10633}
10634
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010635/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010636 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010637 */
10638 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010639link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010640{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010641 if (ufunc->uf_dfunc_idx > 0)
10642 {
10643 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10644 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010645
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010646 ++dfunc->df_refcount;
10647 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010648}
10649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010650#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010651/*
10652 * Free all functions defined with ":def".
10653 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010654 void
10655free_def_functions(void)
10656{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010657 int idx;
10658
10659 for (idx = 0; idx < def_functions.ga_len; ++idx)
10660 {
10661 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10662
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010663 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010664 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010665 }
10666
10667 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010668}
10669#endif
10670
10671
10672#endif // FEAT_EVAL