blob: e5e806cb60dcb2f9cf7c03b4f9834a8223e130be [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 Moolenaar38453522021-11-28 22:00:12 +00001678 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1679 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1680 else
1681 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001682 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001684 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001685 // the nested context, including this one.
1686 if (ufunc->uf_flags & FC_CLOSURE)
1687 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1688
Bram Moolenaar35578162021-08-02 19:10:38 +02001689 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001691 ((type_T **)stack->ga_data)[stack->ga_len] =
1692 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 ++stack->ga_len;
1694
1695 return OK;
1696}
1697
1698/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001699 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001700 * "lambda_name" and "func_name" must be in allocated memory and will be
1701 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001702 */
1703 static int
1704generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1705{
1706 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001707
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001708 if (cctx->ctx_skip == SKIP_YES)
1709 {
1710 vim_free(lambda_name);
1711 vim_free(func_name);
1712 return OK;
1713 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001714 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001715 {
1716 vim_free(lambda_name);
1717 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001718 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001719 }
1720 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001721 isn->isn_arg.newfunc.nf_global = func_name;
1722
1723 return OK;
1724}
1725
1726/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001727 * Generate an ISN_DEF instruction: list functions
1728 */
1729 static int
1730generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1731{
1732 isn_T *isn;
1733
1734 RETURN_OK_IF_SKIP(cctx);
1735 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1736 return FAIL;
1737 if (len > 0)
1738 {
1739 isn->isn_arg.string = vim_strnsave(name, len);
1740 if (isn->isn_arg.string == NULL)
1741 return FAIL;
1742 }
1743 return OK;
1744}
1745
1746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 * Generate an ISN_JUMP instruction.
1748 */
1749 static int
1750generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1751{
1752 isn_T *isn;
1753 garray_T *stack = &cctx->ctx_type_stack;
1754
Bram Moolenaar080457c2020-03-03 21:53:32 +01001755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.jump.jump_when = when;
1759 isn->isn_arg.jump.jump_where = where;
1760
1761 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1762 --stack->ga_len;
1763
1764 return OK;
1765}
1766
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001767/*
1768 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1769 */
1770 static int
1771generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1772{
1773 isn_T *isn;
1774
1775 RETURN_OK_IF_SKIP(cctx);
1776 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1777 return FAIL;
1778 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1779 // jump_where is set later
1780 return OK;
1781}
1782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 static int
1784generate_FOR(cctx_T *cctx, int loop_idx)
1785{
1786 isn_T *isn;
1787 garray_T *stack = &cctx->ctx_type_stack;
1788
Bram Moolenaar080457c2020-03-03 21:53:32 +01001789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1791 return FAIL;
1792 isn->isn_arg.forloop.for_idx = loop_idx;
1793
Bram Moolenaar35578162021-08-02 19:10:38 +02001794 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 return FAIL;
1796 // type doesn't matter, will be stored next
1797 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1798 ++stack->ga_len;
1799
1800 return OK;
1801}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001802/*
1803 * Generate an ISN_TRYCONT instruction.
1804 */
1805 static int
1806generate_TRYCONT(cctx_T *cctx, int levels, int where)
1807{
1808 isn_T *isn;
1809
1810 RETURN_OK_IF_SKIP(cctx);
1811 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1812 return FAIL;
1813 isn->isn_arg.trycont.tct_levels = levels;
1814 isn->isn_arg.trycont.tct_where = where;
1815
1816 return OK;
1817}
1818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819
1820/*
1821 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001822 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 * Return FAIL if the number of arguments is wrong.
1824 */
1825 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001826generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827{
1828 isn_T *isn;
1829 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001830 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001831 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001832 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001833 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834
Bram Moolenaar080457c2020-03-03 21:53:32 +01001835 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001836 argoff = check_internal_func(func_idx, argcount);
1837 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 return FAIL;
1839
Bram Moolenaar389df252020-07-09 21:20:47 +02001840 if (method_call && argoff > 1)
1841 {
1842 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1843 return FAIL;
1844 isn->isn_arg.shuffle.shfl_item = argcount;
1845 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1846 }
1847
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001848 if (argcount > 0)
1849 {
1850 // Check the types of the arguments.
1851 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001852 if (method_call && argoff > 1)
1853 {
1854 int i;
1855
1856 for (i = 0; i < argcount; ++i)
1857 shuffled_argtypes[i] = (i < argoff - 1)
1858 ? argtypes[i + 1]
1859 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1860 argtypes = shuffled_argtypes;
1861 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001862 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1863 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001864 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001865 if (internal_func_is_map(func_idx))
1866 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001867 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1870 return FAIL;
1871 isn->isn_arg.bfunc.cbf_idx = func_idx;
1872 isn->isn_arg.bfunc.cbf_argcount = argcount;
1873
Bram Moolenaar94738d82020-10-21 14:25:07 +02001874 // Drop the argument types and push the return type.
1875 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001876 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 return FAIL;
1878 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001879 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001880 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001882 if (maptype != NULL && maptype->tt_member != NULL
1883 && maptype->tt_member != &t_any)
1884 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001885 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 return OK;
1888}
1889
1890/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001891 * Generate an ISN_LISTAPPEND instruction. Works like add().
1892 * Argument count is already checked.
1893 */
1894 static int
1895generate_LISTAPPEND(cctx_T *cctx)
1896{
1897 garray_T *stack = &cctx->ctx_type_stack;
1898 type_T *list_type;
1899 type_T *item_type;
1900 type_T *expected;
1901
1902 // Caller already checked that list_type is a list.
1903 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1904 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1905 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001906 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001907 return FAIL;
1908
1909 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1910 return FAIL;
1911
1912 --stack->ga_len; // drop the argument
1913 return OK;
1914}
1915
1916/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001917 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1918 * Argument count is already checked.
1919 */
1920 static int
1921generate_BLOBAPPEND(cctx_T *cctx)
1922{
1923 garray_T *stack = &cctx->ctx_type_stack;
1924 type_T *item_type;
1925
1926 // Caller already checked that blob_type is a blob.
1927 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001928 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001929 return FAIL;
1930
1931 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1932 return FAIL;
1933
1934 --stack->ga_len; // drop the argument
1935 return OK;
1936}
1937
1938/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001939 * Return TRUE if "ufunc" should be compiled, taking into account whether
1940 * "profile" indicates profiling is to be done.
1941 */
1942 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001943func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001944{
1945 switch (ufunc->uf_def_status)
1946 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001947 case UF_TO_BE_COMPILED:
1948 return TRUE;
1949
Bram Moolenaarb2049902021-01-24 12:53:53 +01001950 case UF_COMPILED:
1951 {
1952 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1953 + ufunc->uf_dfunc_idx;
1954
Bram Moolenaare99d4222021-06-13 14:01:26 +02001955 switch (compile_type)
1956 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001957 case CT_PROFILE:
1958#ifdef FEAT_PROFILE
1959 return dfunc->df_instr_prof == NULL;
1960#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001961 case CT_NONE:
1962 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001963 case CT_DEBUG:
1964 return dfunc->df_instr_debug == NULL;
1965 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001966 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001967
1968 case UF_NOT_COMPILED:
1969 case UF_COMPILE_ERROR:
1970 case UF_COMPILING:
1971 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001972 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001973 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001974}
1975
1976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 * Generate an ISN_DCALL or ISN_UCALL instruction.
1978 * Return FAIL if the number of arguments is wrong.
1979 */
1980 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001981generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982{
1983 isn_T *isn;
1984 garray_T *stack = &cctx->ctx_type_stack;
1985 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001986 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987
Bram Moolenaar080457c2020-03-03 21:53:32 +01001988 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 if (argcount > regular_args && !has_varargs(ufunc))
1990 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001991 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 return FAIL;
1993 }
1994 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1995 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001996 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 return FAIL;
1998 }
1999
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002000 if (ufunc->uf_def_status != UF_NOT_COMPILED
2001 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002002 {
2003 int i;
2004
2005 for (i = 0; i < argcount; ++i)
2006 {
2007 type_T *expected;
2008 type_T *actual;
2009
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002010 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2011 if (actual == &t_special
2012 && i >= regular_args - ufunc->uf_def_args.ga_len)
2013 {
2014 // assume v:none used for default argument value
2015 continue;
2016 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002017 if (i < regular_args)
2018 {
2019 if (ufunc->uf_arg_types == NULL)
2020 continue;
2021 expected = ufunc->uf_arg_types[i];
2022 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002023 else if (ufunc->uf_va_type == NULL
2024 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002025 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002026 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002027 else
2028 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002029 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002030 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002031 {
2032 arg_type_mismatch(expected, actual, i + 1);
2033 return FAIL;
2034 }
2035 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002036 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002037 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002038 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002039 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002040 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002041 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2042 {
2043 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2044 ufunc->uf_name);
2045 return FAIL;
2046 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002049 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002050 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002052 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 {
2054 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2055 isn->isn_arg.dfunc.cdf_argcount = argcount;
2056 }
2057 else
2058 {
2059 // A user function may be deleted and redefined later, can't use the
2060 // ufunc pointer, need to look it up again at runtime.
2061 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2062 isn->isn_arg.ufunc.cuf_argcount = argcount;
2063 }
2064
2065 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002066 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 return FAIL;
2068 // add return value
2069 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2070 ++stack->ga_len;
2071
2072 return OK;
2073}
2074
2075/*
2076 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2077 */
2078 static int
2079generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2080{
2081 isn_T *isn;
2082 garray_T *stack = &cctx->ctx_type_stack;
2083
Bram Moolenaar080457c2020-03-03 21:53:32 +01002084 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2086 return FAIL;
2087 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2088 isn->isn_arg.ufunc.cuf_argcount = argcount;
2089
2090 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002091 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002092 return FAIL;
2093 // add return value
2094 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2095 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
2097 return OK;
2098}
2099
2100/*
2101 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002102 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 */
2104 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105generate_PCALL(
2106 cctx_T *cctx,
2107 int argcount,
2108 char_u *name,
2109 type_T *type,
2110 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111{
2112 isn_T *isn;
2113 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002114 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115
Bram Moolenaar080457c2020-03-03 21:53:32 +01002116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002117
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002118 if (type->tt_type == VAR_ANY)
2119 ret_type = &t_any;
2120 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002121 {
2122 if (type->tt_argcount != -1)
2123 {
2124 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2125
2126 if (argcount < type->tt_min_argcount - varargs)
2127 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002128 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002129 return FAIL;
2130 }
2131 if (!varargs && argcount > type->tt_argcount)
2132 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002133 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002134 return FAIL;
2135 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002136 if (type->tt_args != NULL)
2137 {
2138 int i;
2139
2140 for (i = 0; i < argcount; ++i)
2141 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002142 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002143 type_T *actual = ((type_T **)stack->ga_data)[
2144 stack->ga_len + offset];
2145 type_T *expected;
2146
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002147 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002148 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002149 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002150 else if (i >= type->tt_min_argcount
2151 && actual == &t_special)
2152 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002153 else
2154 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002155 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002156 cctx, TRUE, FALSE) == FAIL)
2157 {
2158 arg_type_mismatch(expected, actual, i + 1);
2159 return FAIL;
2160 }
2161 }
2162 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002163 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002164 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002165 if (ret_type == &t_unknown)
2166 // return type not known yet, use a runtime check
2167 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002168 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002169 else
2170 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002171 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002172 return FAIL;
2173 }
2174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2176 return FAIL;
2177 isn->isn_arg.pfunc.cpf_top = at_top;
2178 isn->isn_arg.pfunc.cpf_argcount = argcount;
2179
2180 stack->ga_len -= argcount; // drop the arguments
2181
2182 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002183 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002185 // If partial is above the arguments it must be cleared and replaced with
2186 // the return value.
2187 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2188 return FAIL;
2189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 return OK;
2191}
2192
2193/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 */
2196 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002197generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198{
2199 isn_T *isn;
2200 garray_T *stack = &cctx->ctx_type_stack;
2201 type_T *type;
2202
Bram Moolenaar080457c2020-03-03 21:53:32 +01002203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002204 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002206 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002208 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002210 if (type->tt_type != VAR_DICT && type != &t_any)
2211 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002212 char *tofree;
2213
2214 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2215 name, type_name(type, &tofree));
2216 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002217 return FAIL;
2218 }
2219 // change dict type to dict member type
2220 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002221 {
2222 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2223 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225
2226 return OK;
2227}
2228
2229/*
2230 * Generate an ISN_ECHO instruction.
2231 */
2232 static int
2233generate_ECHO(cctx_T *cctx, int with_white, int count)
2234{
2235 isn_T *isn;
2236
Bram Moolenaar080457c2020-03-03 21:53:32 +01002237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.echo.echo_with_white = with_white;
2241 isn->isn_arg.echo.echo_count = count;
2242
2243 return OK;
2244}
2245
Bram Moolenaarad39c092020-02-26 18:23:43 +01002246/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002247 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002248 */
2249 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002250generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002251{
2252 isn_T *isn;
2253
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002254 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002255 return FAIL;
2256 isn->isn_arg.number = count;
2257
2258 return OK;
2259}
2260
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002261/*
2262 * Generate an ISN_PUT instruction.
2263 */
2264 static int
2265generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2266{
2267 isn_T *isn;
2268
2269 RETURN_OK_IF_SKIP(cctx);
2270 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2271 return FAIL;
2272 isn->isn_arg.put.put_regname = regname;
2273 isn->isn_arg.put.put_lnum = lnum;
2274 return OK;
2275}
2276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 static int
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002278generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279{
2280 isn_T *isn;
2281
Bram Moolenaar080457c2020-03-03 21:53:32 +01002282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002283 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 return FAIL;
2285 isn->isn_arg.string = vim_strsave(line);
2286 return OK;
2287}
2288
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002289 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002290generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2291{
2292 isn_T *isn;
2293 garray_T *stack = &cctx->ctx_type_stack;
2294
2295 RETURN_OK_IF_SKIP(cctx);
2296 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2297 return FAIL;
2298 isn->isn_arg.string = vim_strsave(line);
2299
Bram Moolenaar35578162021-08-02 19:10:38 +02002300 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002301 return FAIL;
2302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2303 ++stack->ga_len;
2304
2305 return OK;
2306}
2307
2308 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002309generate_EXECCONCAT(cctx_T *cctx, int count)
2310{
2311 isn_T *isn;
2312
2313 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2314 return FAIL;
2315 isn->isn_arg.number = count;
2316 return OK;
2317}
2318
Bram Moolenaar08597872020-12-10 19:43:40 +01002319/*
2320 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2321 */
2322 static int
2323generate_RANGE(cctx_T *cctx, char_u *range)
2324{
2325 isn_T *isn;
2326 garray_T *stack = &cctx->ctx_type_stack;
2327
2328 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2329 return FAIL;
2330 isn->isn_arg.string = range;
2331
Bram Moolenaar35578162021-08-02 19:10:38 +02002332 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002333 return FAIL;
2334 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2335 ++stack->ga_len;
2336 return OK;
2337}
2338
Bram Moolenaar792f7862020-11-23 08:31:18 +01002339 static int
2340generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2341{
2342 isn_T *isn;
2343
2344 RETURN_OK_IF_SKIP(cctx);
2345 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2346 return FAIL;
2347 isn->isn_arg.unpack.unp_count = var_count;
2348 isn->isn_arg.unpack.unp_semicolon = semicolon;
2349 return OK;
2350}
2351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002353 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002354 */
2355 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002356generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002357{
2358 isn_T *isn;
2359
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002360 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002361 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002362 cctx->ctx_has_cmdmod = TRUE;
2363
2364 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002365 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002366 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2367 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2368 return FAIL;
2369 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002370 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002371 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002372 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002373
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002374 return OK;
2375}
2376
2377 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002378generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002379{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002380 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2381 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002382 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002383 return OK;
2384}
2385
Bram Moolenaarfa984412021-03-25 22:15:28 +01002386 static int
2387misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002388{
2389 garray_T *instr = &cctx->ctx_instr;
2390
Bram Moolenaara91a7132021-03-25 21:12:15 +01002391 if (cctx->ctx_has_cmdmod
2392 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2393 == ISN_CMDMOD)
2394 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002395 emsg(_(e_misplaced_command_modifier));
2396 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002397 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002398 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002399}
2400
2401/*
2402 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002403 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002404 */
2405 static int
2406current_instr_idx(cctx_T *cctx)
2407{
2408 garray_T *instr = &cctx->ctx_instr;
2409 int idx = instr->ga_len;
2410
Bram Moolenaare99d4222021-06-13 14:01:26 +02002411 while (idx > 0)
2412 {
2413 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002414 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002415 {
2416 --idx;
2417 continue;
2418 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002419#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002420 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2421 {
2422 --idx;
2423 continue;
2424 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002425#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002426 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2427 {
2428 --idx;
2429 continue;
2430 }
2431 break;
2432 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002433 return idx;
2434}
2435
Bram Moolenaarf002a412021-01-24 13:34:18 +01002436#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002437 static void
2438may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2439{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002440 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002441 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002442}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002443#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002444
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002445/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002447 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002449 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002450reserve_local(
2451 cctx_T *cctx,
2452 char_u *name,
2453 size_t len,
2454 int isConst,
2455 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002458 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002460 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002462 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002463 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 }
2465
Bram Moolenaar35578162021-08-02 19:10:38 +02002466 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002467 return NULL;
2468 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002469 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002471 // Every local variable uses the next entry on the stack. We could re-use
2472 // the last ones when leaving a scope, but then variables used in a closure
2473 // might get overwritten. To keep things simple do not re-use stack
2474 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002475 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2476 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002477
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002478 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 lvar->lv_const = isConst;
2480 lvar->lv_type = type;
2481
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002482 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002483 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002484 return NULL;
2485 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2486 vim_strsave(lvar->lv_name);
2487 ++dfunc->df_var_names.ga_len;
2488
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002489 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490}
2491
2492/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002493 * Remove local variables above "new_top".
2494 */
2495 static void
2496unwind_locals(cctx_T *cctx, int new_top)
2497{
2498 if (cctx->ctx_locals.ga_len > new_top)
2499 {
2500 int idx;
2501 lvar_T *lvar;
2502
2503 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2504 {
2505 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2506 vim_free(lvar->lv_name);
2507 }
2508 }
2509 cctx->ctx_locals.ga_len = new_top;
2510}
2511
2512/*
2513 * Free all local variables.
2514 */
2515 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002517{
2518 unwind_locals(cctx, 0);
2519 ga_clear(&cctx->ctx_locals);
2520}
2521
2522/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002523 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2524 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2525 * error if the variable was defined with :const.
2526 */
2527 static int
2528check_item_writable(svar_T *sv, int check_writable, char_u *name)
2529{
2530 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2531 || (check_writable == ASSIGN_FINAL
2532 && sv->sv_const == ASSIGN_CONST))
2533 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002534 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002535 return FAIL;
2536 }
2537 return OK;
2538}
2539
2540/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002542 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 * Returns the index in "sn_var_vals" if found.
2544 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002545 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 */
2547 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002548get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549{
2550 hashtab_T *ht;
2551 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002552 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002553 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 int idx;
2555
Bram Moolenaare3d46852020-08-29 13:39:17 +02002556 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002558 if (sid == current_sctx.sc_sid)
2559 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002560 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002561
2562 if (sav == NULL)
2563 return -2;
2564 idx = sav->sav_var_vals_idx;
2565 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002566 if (check_item_writable(sv, check_writable, name) == FAIL)
2567 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002568 return idx;
2569 }
2570
2571 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 ht = &SCRIPT_VARS(sid);
2573 di = find_var_in_ht(ht, 0, name, TRUE);
2574 if (di == NULL)
2575 return -2;
2576
2577 // Now find the svar_T index in sn_var_vals.
2578 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2579 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002580 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 if (sv->sv_tv == &di->di_tv)
2582 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002583 if (check_item_writable(sv, check_writable, name) == FAIL)
2584 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 return idx;
2586 }
2587 }
2588 return -1;
2589}
2590
2591/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002592 * Find "name" in imported items of the current script or in "cctx" if not
2593 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 */
2595 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002596find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 int idx;
2599
Bram Moolenaare3d46852020-08-29 13:39:17 +02002600 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002601 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 if (cctx != NULL)
2603 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2604 {
2605 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2606 + idx;
2607
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002608 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2609 : STRLEN(import->imp_name) == len
2610 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return import;
2612 }
2613
Bram Moolenaarefa94442020-08-08 22:16:00 +02002614 return find_imported_in_script(name, len, current_sctx.sc_sid);
2615}
2616
2617 imported_T *
2618find_imported_in_script(char_u *name, size_t len, int sid)
2619{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002620 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002621 int idx;
2622
Bram Moolenaare3d46852020-08-29 13:39:17 +02002623 if (!SCRIPT_ID_VALID(sid))
2624 return NULL;
2625 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2627 {
2628 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2629
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002630 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2631 : STRLEN(import->imp_name) == len
2632 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 return import;
2634 }
2635 return NULL;
2636}
2637
2638/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002639 * Free all imported variables.
2640 */
2641 static void
2642free_imported(cctx_T *cctx)
2643{
2644 int idx;
2645
2646 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2647 {
2648 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2649
2650 vim_free(import->imp_name);
2651 }
2652 ga_clear(&cctx->ctx_imports);
2653}
2654
2655/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002656 * Return a pointer to the next line that isn't empty or only contains a
2657 * comment. Skips over white space.
2658 * Returns NULL if there is none.
2659 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002660 char_u *
2661peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002662{
2663 int lnum = cctx->ctx_lnum;
2664
2665 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2666 {
2667 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002668 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002669
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002670 // ignore NULLs inserted for continuation lines
2671 if (line != NULL)
2672 {
2673 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002674 if (vim9_bad_comment(p))
2675 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002676 if (*p != NUL && !vim9_comment_start(p))
2677 return p;
2678 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002679 }
2680 return NULL;
2681}
2682
2683/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002684 * Called when checking for a following operator at "arg". When the rest of
2685 * the line is empty or only a comment, peek the next line. If there is a next
2686 * line return a pointer to it and set "nextp".
2687 * Otherwise skip over white space.
2688 */
2689 static char_u *
2690may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2691{
2692 char_u *p = skipwhite(arg);
2693
2694 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002695 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002696 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002697 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002698 if (*nextp != NULL)
2699 return *nextp;
2700 }
2701 return p;
2702}
2703
2704/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002705 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002706 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002707 * Returns NULL when at the end.
2708 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002709 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002710next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002711{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002712 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002713
2714 do
2715 {
2716 ++cctx->ctx_lnum;
2717 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002718 {
2719 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002720 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002721 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002722 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002723 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002724 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002725 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002726 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002727 return line;
2728}
2729
2730/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002731 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002732 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002733 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002734 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2735 */
2736 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002737may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002738{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002739 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002740 if (vim9_bad_comment(*arg))
2741 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002742 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002743 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002744 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002745
2746 if (next == NULL)
2747 return FAIL;
2748 *arg = skipwhite(next);
2749 }
2750 return OK;
2751}
2752
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002753/*
2754 * Idem, and give an error when failed.
2755 */
2756 static int
2757may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2758{
2759 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2760 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002761 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002762 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002763 return FAIL;
2764 }
2765 return OK;
2766}
2767
2768
Bram Moolenaara5565e42020-05-09 15:44:01 +02002769// Structure passed between the compile_expr* functions to keep track of
2770// constants that have been parsed but for which no code was produced yet. If
2771// possible expressions on these constants are applied at compile time. If
2772// that is not possible, the code to push the constants needs to be generated
2773// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002774// Using 50 should be more than enough of 5 levels of ().
2775#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002776typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002777 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002779 int pp_is_const; // all generated code was constants, used for a
2780 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002781} ppconst_T;
2782
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002783static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002784static int compile_expr0(char_u **arg, cctx_T *cctx);
2785static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2786
Bram Moolenaara5565e42020-05-09 15:44:01 +02002787/*
2788 * Generate a PUSH instruction for "tv".
2789 * "tv" will be consumed or cleared.
2790 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2791 */
2792 static int
2793generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2794{
2795 if (tv != NULL)
2796 {
2797 switch (tv->v_type)
2798 {
2799 case VAR_UNKNOWN:
2800 break;
2801 case VAR_BOOL:
2802 generate_PUSHBOOL(cctx, tv->vval.v_number);
2803 break;
2804 case VAR_SPECIAL:
2805 generate_PUSHSPEC(cctx, tv->vval.v_number);
2806 break;
2807 case VAR_NUMBER:
2808 generate_PUSHNR(cctx, tv->vval.v_number);
2809 break;
2810#ifdef FEAT_FLOAT
2811 case VAR_FLOAT:
2812 generate_PUSHF(cctx, tv->vval.v_float);
2813 break;
2814#endif
2815 case VAR_BLOB:
2816 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2817 tv->vval.v_blob = NULL;
2818 break;
2819 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002820 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002821 tv->vval.v_string = NULL;
2822 break;
2823 default:
2824 iemsg("constant type not supported");
2825 clear_tv(tv);
2826 return FAIL;
2827 }
2828 tv->v_type = VAR_UNKNOWN;
2829 }
2830 return OK;
2831}
2832
2833/*
2834 * Generate code for any ppconst entries.
2835 */
2836 static int
2837generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2838{
2839 int i;
2840 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002841 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002842
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002843 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002844 for (i = 0; i < ppconst->pp_used; ++i)
2845 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2846 ret = FAIL;
2847 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002848 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002849 return ret;
2850}
2851
2852/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002853 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002854 */
2855 static int
2856check_ppconst_bool(ppconst_T *ppconst)
2857{
2858 if (ppconst->pp_used > 0)
2859 {
2860 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002861 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002862
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002863 return check_typval_type(&t_bool, tv, where);
2864 }
2865 return OK;
2866}
2867
2868/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002869 * Clear ppconst constants. Used when failing.
2870 */
2871 static void
2872clear_ppconst(ppconst_T *ppconst)
2873{
2874 int i;
2875
2876 for (i = 0; i < ppconst->pp_used; ++i)
2877 clear_tv(&ppconst->pp_tv[i]);
2878 ppconst->pp_used = 0;
2879}
2880
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002881/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002882 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002883 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002884 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002885 */
2886 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002887compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002888{
2889 type_T **typep;
2890 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002891 vartype_T vartype;
2892 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002893
Bram Moolenaar261417b2021-05-06 21:04:55 +02002894 // We can index a list, dict and blob. If we don't know the type
2895 // we can use the index value type. If we still don't know use an "ANY"
2896 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002897 typep = ((type_T **)stack->ga_data) + stack->ga_len
2898 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002899 vartype = (*typep)->tt_type;
2900 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002901 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002902 if (*typep == &t_any && idxtype == &t_string)
2903 vartype = VAR_DICT;
2904 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002905 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002906 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002907 return FAIL;
2908 if (is_slice)
2909 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002910 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2911 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002912 FALSE, FALSE) == FAIL)
2913 return FAIL;
2914 }
2915 }
2916
Bram Moolenaar261417b2021-05-06 21:04:55 +02002917 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002918 {
2919 if (is_slice)
2920 {
2921 emsg(_(e_cannot_slice_dictionary));
2922 return FAIL;
2923 }
2924 if ((*typep)->tt_type == VAR_DICT)
2925 {
2926 *typep = (*typep)->tt_member;
2927 if (*typep == &t_unknown)
2928 // empty dict was used
2929 *typep = &t_any;
2930 }
2931 else
2932 {
2933 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2934 FALSE, FALSE) == FAIL)
2935 return FAIL;
2936 *typep = &t_any;
2937 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002938 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002939 return FAIL;
2940 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2941 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002942 if (keeping_dict != NULL)
2943 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002944 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002945 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002946 {
2947 *typep = &t_string;
2948 if ((is_slice
2949 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2950 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2951 return FAIL;
2952 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002953 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002954 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002955 if (is_slice)
2956 {
2957 *typep = &t_blob;
2958 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2959 return FAIL;
2960 }
2961 else
2962 {
2963 *typep = &t_number;
2964 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2965 return FAIL;
2966 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002967 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002968 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002969 {
2970 if (is_slice)
2971 {
2972 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002973 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002974 2) == FAIL)
2975 return FAIL;
2976 }
2977 else
2978 {
2979 if ((*typep)->tt_type == VAR_LIST)
2980 {
2981 *typep = (*typep)->tt_member;
2982 if (*typep == &t_unknown)
2983 // empty list was used
2984 *typep = &t_any;
2985 }
2986 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002987 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2988 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002989 return FAIL;
2990 }
2991 }
2992 else
2993 {
2994 emsg(_(e_string_list_dict_or_blob_required));
2995 return FAIL;
2996 }
2997 return OK;
2998}
2999
3000/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003001 * Generate an instruction to load script-local variable "name", without the
3002 * leading "s:".
3003 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 */
3005 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003006compile_load_scriptvar(
3007 cctx_T *cctx,
3008 char_u *name, // variable NUL terminated
3009 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003010 char_u **end, // end of variable
3011 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003013 scriptitem_T *si;
3014 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 imported_T *import;
3016
Bram Moolenaare3d46852020-08-29 13:39:17 +02003017 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3018 return FAIL;
3019 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003020 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003021 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003023 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003024 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3025 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027 if (idx >= 0)
3028 {
3029 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3030
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003031 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 current_sctx.sc_sid, idx, sv->sv_type);
3033 return OK;
3034 }
3035
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003036 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if (import != NULL)
3038 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003039 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003040 {
3041 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003042 char_u *exp_name;
3043 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003044 ufunc_T *ufunc;
3045 type_T *type;
3046
3047 // Used "import * as Name", need to lookup the member.
3048 if (*p != '.')
3049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003050 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003051 return FAIL;
3052 }
3053 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003054 if (VIM_ISWHITE(*p))
3055 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003056 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003057 return FAIL;
3058 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003059
Bram Moolenaar1c991142020-07-04 13:15:31 +02003060 // isolate one name
3061 exp_name = p;
3062 while (eval_isnamec(*p))
3063 ++p;
3064 cc = *p;
3065 *p = NUL;
3066
Bram Moolenaaredba7072021-03-13 21:14:18 +01003067 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3068 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003069 *p = cc;
3070 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003071 *end = p;
3072
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003073 if (idx < 0)
3074 {
3075 if (*p == '(' && ufunc != NULL)
3076 {
3077 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3078 return OK;
3079 }
3080 return FAIL;
3081 }
3082
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003083 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3084 import->imp_sid,
3085 idx,
3086 type);
3087 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003088 else if (import->imp_funcname != NULL)
3089 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003090 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003091 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3092 import->imp_sid,
3093 import->imp_var_vals_idx,
3094 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 return OK;
3096 }
3097
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003098 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003099 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 return FAIL;
3101}
3102
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003103 static int
3104generate_funcref(cctx_T *cctx, char_u *name)
3105{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003106 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003107
3108 if (ufunc == NULL)
3109 return FAIL;
3110
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003111 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003112 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3113 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003114 == FAIL)
3115 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003116 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003117}
3118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119/*
3120 * Compile a variable name into a load instruction.
3121 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003122 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 * When "error" is FALSE do not give an error when not found.
3124 */
3125 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003126compile_load(
3127 char_u **arg,
3128 char_u *end_arg,
3129 cctx_T *cctx,
3130 int is_expr,
3131 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132{
3133 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003134 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003135 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003137 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
3139 if (*(*arg + 1) == ':')
3140 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003141 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003142 {
3143 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144
Bram Moolenaarfa596382021-04-07 21:58:16 +02003145 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003146 switch (**arg)
3147 {
3148 case 'g': isn_type = ISN_LOADGDICT; break;
3149 case 'w': isn_type = ISN_LOADWDICT; break;
3150 case 't': isn_type = ISN_LOADTDICT; break;
3151 case 'b': isn_type = ISN_LOADBDICT; break;
3152 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003153 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003154 goto theend;
3155 }
3156 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3157 goto theend;
3158 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 else
3161 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 isntype_T isn_type = ISN_DROP;
3163
Bram Moolenaarfa596382021-04-07 21:58:16 +02003164 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003165 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3166 if (name == NULL)
3167 return FAIL;
3168
3169 switch (**arg)
3170 {
3171 case 'v': res = generate_LOADV(cctx, name, error);
3172 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003173 case 's': if (is_expr && ASCII_ISUPPER(*name)
3174 && find_func(name, FALSE, cctx) != NULL)
3175 res = generate_funcref(cctx, name);
3176 else
3177 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003178 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003179 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003180 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003181 {
3182 if (is_expr && ASCII_ISUPPER(*name)
3183 && find_func(name, FALSE, cctx) != NULL)
3184 res = generate_funcref(cctx, name);
3185 else
3186 isn_type = ISN_LOADG;
3187 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003188 else
3189 {
3190 isn_type = ISN_LOADAUTO;
3191 vim_free(name);
3192 name = vim_strnsave(*arg, end - *arg);
3193 if (name == NULL)
3194 return FAIL;
3195 }
3196 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003197 case 'w': isn_type = ISN_LOADW; break;
3198 case 't': isn_type = ISN_LOADT; break;
3199 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003200 default: // cannot happen, just in case
3201 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003202 goto theend;
3203 }
3204 if (isn_type != ISN_DROP)
3205 {
3206 // Global, Buffer-local, Window-local and Tabpage-local
3207 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003208 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003209 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 }
3213 else
3214 {
3215 size_t len = end - *arg;
3216 int idx;
3217 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003218 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219
3220 name = vim_strnsave(*arg, end - *arg);
3221 if (name == NULL)
3222 return FAIL;
3223
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003224 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3225 {
3226 script_autoload(name, FALSE);
3227 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3228 }
3229 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3230 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003232 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003233 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 }
3235 else
3236 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003237 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003238
Bram Moolenaar709664c2020-12-12 14:33:41 +01003239 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003241 type = lvar.lv_type;
3242 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003243 if (lvar.lv_from_outer != 0)
3244 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003245 else
3246 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 }
3248 else
3249 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003250 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003251 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003252 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003253 || find_imported(name, 0, cctx) != NULL)
3254 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003255
Bram Moolenaar0f769812020-09-12 18:32:34 +02003256 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003257 // uppercase letter it can be a user defined function.
3258 // generate_funcref() will fail if the function can't be found.
3259 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003260 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 }
3262 }
3263 if (gen_load)
3264 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003265 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003266 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003267 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003268 cctx->ctx_outer_used = TRUE;
3269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
3271
3272 *arg = end;
3273
3274theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003275 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003276 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 vim_free(name);
3278 return res;
3279}
3280
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003281 static void
3282clear_instr_ga(garray_T *gap)
3283{
3284 int idx;
3285
3286 for (idx = 0; idx < gap->ga_len; ++idx)
3287 delete_instr(((isn_T *)gap->ga_data) + idx);
3288 ga_clear(gap);
3289}
3290
3291/*
3292 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3293 * Returns FAIL if compilation fails.
3294 */
3295 static int
3296compile_string(isn_T *isn, cctx_T *cctx)
3297{
3298 char_u *s = isn->isn_arg.string;
3299 garray_T save_ga = cctx->ctx_instr;
3300 int expr_res;
3301 int trailing_error;
3302 int instr_count;
3303 isn_T *instr = NULL;
3304
Bram Moolenaarcd268012021-07-22 19:11:08 +02003305 // Remove the string type from the stack.
3306 --cctx->ctx_type_stack.ga_len;
3307
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003308 // Temporarily reset the list of instructions so that the jump labels are
3309 // correct.
3310 cctx->ctx_instr.ga_len = 0;
3311 cctx->ctx_instr.ga_maxlen = 0;
3312 cctx->ctx_instr.ga_data = NULL;
3313 expr_res = compile_expr0(&s, cctx);
3314 s = skipwhite(s);
3315 trailing_error = *s != NUL;
3316
Bram Moolenaarff652882021-05-16 15:24:49 +02003317 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003318 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003319 {
3320 if (trailing_error)
3321 semsg(_(e_trailing_arg), s);
3322 clear_instr_ga(&cctx->ctx_instr);
3323 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003324 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003325 return FAIL;
3326 }
3327
3328 // Move the generated instructions into the ISN_INSTR instruction, then
3329 // restore the list of instructions.
3330 instr_count = cctx->ctx_instr.ga_len;
3331 instr = cctx->ctx_instr.ga_data;
3332 instr[instr_count].isn_type = ISN_FINISH;
3333
3334 cctx->ctx_instr = save_ga;
3335 vim_free(isn->isn_arg.string);
3336 isn->isn_type = ISN_INSTR;
3337 isn->isn_arg.instr = instr;
3338 return OK;
3339}
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341/*
3342 * Compile the argument expressions.
3343 * "arg" points to just after the "(" and is advanced to after the ")"
3344 */
3345 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003346compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003348 char_u *p = *arg;
3349 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003350 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003351 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352
Bram Moolenaare6085c52020-04-12 20:19:16 +02003353 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003355 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3356 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003357 if (*p == ')')
3358 {
3359 *arg = p + 1;
3360 return OK;
3361 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003362 if (must_end)
3363 {
3364 semsg(_(e_missing_comma_before_argument_str), p);
3365 return FAIL;
3366 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003367
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003368 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003369 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 return FAIL;
3371 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003372
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003373 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003374 && cctx->ctx_instr.ga_len == instr_count + 1)
3375 {
3376 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3377
3378 // {skip} argument of searchpair() can be compiled if not empty
3379 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3380 compile_string(isn, cctx);
3381 }
3382
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003383 if (*p != ',' && *skipwhite(p) == ',')
3384 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003385 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003386 p = skipwhite(p);
3387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003389 {
3390 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003391 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003392 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003393 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003394 else
3395 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003396 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003397 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003399failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003400 emsg(_(e_missing_close));
3401 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402}
3403
3404/*
3405 * Compile a function call: name(arg1, arg2)
3406 * "arg" points to "name", "arg + varlen" to the "(".
3407 * "argcount_init" is 1 for "value->method()"
3408 * Instructions:
3409 * EVAL arg1
3410 * EVAL arg2
3411 * BCALL / DCALL / UCALL
3412 */
3413 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003414compile_call(
3415 char_u **arg,
3416 size_t varlen,
3417 cctx_T *cctx,
3418 ppconst_T *ppconst,
3419 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420{
3421 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003422 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 int argcount = argcount_init;
3424 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003425 char_u fname_buf[FLEN_FIXED + 1];
3426 char_u *tofree = NULL;
3427 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003428 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003429 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003430 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003431 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003433 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003434 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003435 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003436 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003437 {
3438 char_u *s = skipwhite(*arg + varlen + 1);
3439 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003440 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003441
3442 argvars[0].v_type = VAR_UNKNOWN;
3443 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003444 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003445 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003446 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003447 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003448 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003449 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003450 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003451 {
3452 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3453
3454 *arg = s + 1;
3455 argvars[1].v_type = VAR_UNKNOWN;
3456 tv->v_type = VAR_NUMBER;
3457 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003458 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003459 f_has(argvars, tv);
3460 else
3461 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003462 clear_tv(&argvars[0]);
3463 ++ppconst->pp_used;
3464 return OK;
3465 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003466 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003467 if (!is_has)
3468 {
3469 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3470 return FAIL;
3471 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003472 }
3473
3474 if (generate_ppconst(cctx, ppconst) == FAIL)
3475 return FAIL;
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 if (varlen >= sizeof(namebuf))
3478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003479 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 return FAIL;
3481 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003482 vim_strncpy(namebuf, *arg, varlen);
3483 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003485 // We handle the "skip" argument of searchpair() and searchpairpos()
3486 // differently.
3487 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3488 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3489 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3490 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003493 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003494 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495
Bram Moolenaar03290b82020-12-19 16:30:44 +01003496 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003497 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 {
3499 int idx;
3500
3501 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003502 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003504 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003505 if (STRCMP(name, "flatten") == 0)
3506 {
3507 emsg(_(e_cannot_use_flatten_in_vim9_script));
3508 goto theend;
3509 }
3510
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003511 if (STRCMP(name, "add") == 0 && argcount == 2)
3512 {
3513 garray_T *stack = &cctx->ctx_type_stack;
3514 type_T *type = ((type_T **)stack->ga_data)[
3515 stack->ga_len - 2];
3516
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003517 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003518 if (type->tt_type == VAR_LIST)
3519 {
3520 // inline "add(list, item)" so that the type can be checked
3521 res = generate_LISTAPPEND(cctx);
3522 idx = -1;
3523 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003524 else if (type->tt_type == VAR_BLOB)
3525 {
3526 // inline "add(blob, nr)" so that the type can be checked
3527 res = generate_BLOBAPPEND(cctx);
3528 idx = -1;
3529 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003530 }
3531
3532 if (idx >= 0)
3533 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3534 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003535 else
3536 semsg(_(e_unknownfunc), namebuf);
3537 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 }
3539
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003540 // An argument or local variable can be a function reference, this
3541 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003542 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003543 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003544 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003545 // If we can find the function by name generate the right call.
3546 // Skip global functions here, a local funcref takes precedence.
3547 ufunc = find_func(name, FALSE, cctx);
3548 if (ufunc != NULL && !func_is_global(ufunc))
3549 {
3550 res = generate_CALL(cctx, ufunc, argcount);
3551 goto theend;
3552 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
3555 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003556 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003557 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003559 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003560 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003561 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003562 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003563 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003564
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003565 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003566 goto theend;
3567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568
Bram Moolenaar0f769812020-09-12 18:32:34 +02003569 // If we can find a global function by name generate the right call.
3570 if (ufunc != NULL)
3571 {
3572 res = generate_CALL(cctx, ufunc, argcount);
3573 goto theend;
3574 }
3575
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003576 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003577 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003578 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003579 res = generate_UCALL(cctx, name, argcount);
3580 else
3581 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003582
3583theend:
3584 vim_free(tofree);
3585 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586}
3587
3588// like NAMESPACE_CHAR but with 'a' and 'l'.
3589#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3590
3591/*
3592 * Find the end of a variable or function name. Unlike find_name_end() this
3593 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003594 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 * Return a pointer to just after the name. Equal to "arg" if there is no
3596 * valid name.
3597 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003598 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003599to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600{
3601 char_u *p;
3602
3603 // Quick check for valid starting character.
3604 if (!eval_isnamec1(*arg))
3605 return arg;
3606
3607 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3608 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3609 // and can be used in slice "[n:]".
3610 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003611 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3613 break;
3614 return p;
3615}
3616
3617/*
3618 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003619 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003620 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 */
3622 char_u *
3623to_name_const_end(char_u *arg)
3624{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003625 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 typval_T rettv;
3627
Bram Moolenaar678b2072021-07-26 21:10:11 +02003628 if (STRNCMP(p, "<SNR>", 5) == 0)
3629 p = skipdigits(p + 5);
3630 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 if (p == arg && *arg == '[')
3632 {
3633
3634 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003635 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 p = arg;
3637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 return p;
3639}
3640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641/*
3642 * parse a list: [expr, expr]
3643 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003644 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 */
3646 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003647compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648{
3649 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003650 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003652 int is_const;
3653 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003655 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003657 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003658 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003659 semsg(_(e_list_end), *arg);
3660 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003661 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003662 if (*p == ',')
3663 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003664 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003665 return FAIL;
3666 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003667 if (*p == ']')
3668 {
3669 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003670 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003671 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003672 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003673 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003674 if (!is_const)
3675 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 ++count;
3677 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003678 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003680 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3681 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003682 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003683 return FAIL;
3684 }
3685 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003686 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 p = skipwhite(p);
3688 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003689 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003691 ppconst->pp_is_const = is_all_const;
3692 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693}
3694
3695/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003696 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003697 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003698 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 */
3700 static int
3701compile_lambda(char_u **arg, cctx_T *cctx)
3702{
Bram Moolenaare462f522020-12-27 14:43:30 +01003703 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 typval_T rettv;
3705 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003706 evalarg_T evalarg;
3707
Bram Moolenaar844fb642021-10-23 13:32:30 +01003708 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003709 evalarg.eval_flags = EVAL_EVALUATE;
3710 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711
3712 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003713 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3714 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003715 {
3716 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003717 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003718 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003719
Bram Moolenaar65c44152020-12-24 15:14:01 +01003720 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003722 ++ufunc->uf_refcount;
3723 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
Bram Moolenaara9931532021-06-12 15:58:16 +02003725 // Compile it here to get the return type. The return type is optional,
3726 // when it's missing use t_unknown. This is recognized in
3727 // compile_return().
3728 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3729 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003730 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaar648594e2021-07-11 17:55:01 +02003732#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003733 // When the outer function is compiled for profiling, the lambda may be
3734 // called without profiling. Compile it here in the right context.
3735 if (cctx->ctx_compile_type == CT_PROFILE)
3736 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003737#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003738
Bram Moolenaar844fb642021-10-23 13:32:30 +01003739 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3740 // "*arg" may point into it. Point into the original line to avoid a
3741 // dangling pointer.
3742 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003743 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003744 garray_T *gap = &evalarg.eval_tofree_ga;
3745 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003746
3747 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3748 + off;
3749 }
3750
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003751 clear_evalarg(&evalarg, NULL);
3752
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003753 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003754 {
3755 // The return type will now be known.
3756 set_function_type(ufunc);
3757
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003758 // The function reference count will be 1. When the ISN_FUNCREF
3759 // instruction is deleted the reference count is decremented and the
3760 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003761 return generate_FUNCREF(cctx, ufunc);
3762 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003763
3764 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 return FAIL;
3766}
3767
3768/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003769 * Get a lambda and compile it. Uses Vim9 syntax.
3770 */
3771 int
3772get_lambda_tv_and_compile(
3773 char_u **arg,
3774 typval_T *rettv,
3775 int types_optional,
3776 evalarg_T *evalarg)
3777{
3778 int r;
3779 ufunc_T *ufunc;
3780 int save_sc_version = current_sctx.sc_version;
3781
3782 // Get the funcref in "rettv".
3783 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3784 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3785 current_sctx.sc_version = save_sc_version;
3786 if (r != OK)
3787 return r;
3788
3789 // "rettv" will now be a partial referencing the function.
3790 ufunc = rettv->vval.v_partial->pt_func;
3791
3792 // Compile it here to get the return type. The return type is optional,
3793 // when it's missing use t_unknown. This is recognized in
3794 // compile_return().
3795 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3796 ufunc->uf_ret_type = &t_unknown;
3797 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3798
3799 if (ufunc->uf_def_status == UF_COMPILED)
3800 {
3801 // The return type will now be known.
3802 set_function_type(ufunc);
3803 return OK;
3804 }
3805 clear_tv(rettv);
3806 return FAIL;
3807}
3808
3809/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003810 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003812 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 */
3814 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003815compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816{
3817 garray_T *instr = &cctx->ctx_instr;
3818 int count = 0;
3819 dict_T *d = dict_alloc();
3820 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003821 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003822 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003823 int is_const;
3824 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825
3826 if (d == NULL)
3827 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003828 if (generate_ppconst(cctx, ppconst) == FAIL)
3829 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003830 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003832 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833
Bram Moolenaar23c55272020-06-21 16:58:13 +02003834 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003835 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003836 *arg = NULL;
3837 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003838 }
3839
3840 if (**arg == '}')
3841 break;
3842
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003843 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003845 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003847 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003848 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003849 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003852 if (isn->isn_type == ISN_PUSHNR)
3853 {
3854 char buf[NUMBUFLEN];
3855
3856 // Convert to string at compile time.
3857 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3858 isn->isn_type = ISN_PUSHS;
3859 isn->isn_arg.string = vim_strsave((char_u *)buf);
3860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 if (isn->isn_type == ISN_PUSHS)
3862 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003863 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003864 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003865 *arg = skipwhite(*arg);
3866 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003867 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003868 emsg(_(e_missing_matching_bracket_after_dict_key));
3869 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003870 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003871 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003873 else
3874 {
3875 // {"name": value},
3876 // {'name': value},
3877 // {name: value} use "name" as a literal key
3878 key = get_literal_key(arg);
3879 if (key == NULL)
3880 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003881 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003882 return FAIL;
3883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884
3885 // Check for duplicate keys, if using string keys.
3886 if (key != NULL)
3887 {
3888 item = dict_find(d, key, -1);
3889 if (item != NULL)
3890 {
3891 semsg(_(e_duplicate_key), key);
3892 goto failret;
3893 }
3894 item = dictitem_alloc(key);
3895 if (item != NULL)
3896 {
3897 item->di_tv.v_type = VAR_UNKNOWN;
3898 item->di_tv.v_lock = 0;
3899 if (dict_add(d, item) == FAIL)
3900 dictitem_free(item);
3901 }
3902 }
3903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 if (**arg != ':')
3905 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003906 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003907 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003908 else
3909 semsg(_(e_missing_dict_colon), *arg);
3910 return FAIL;
3911 }
3912 whitep = *arg + 1;
3913 if (!IS_WHITE_OR_NUL(*whitep))
3914 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003915 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 return FAIL;
3917 }
3918
Bram Moolenaar23c55272020-06-21 16:58:13 +02003919 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003920 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003921 *arg = NULL;
3922 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003923 }
3924
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003925 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003927 if (!is_const)
3928 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 ++count;
3930
Bram Moolenaar2c330432020-04-13 14:41:35 +02003931 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003932 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003933 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003934 *arg = NULL;
3935 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 if (**arg == '}')
3938 break;
3939 if (**arg != ',')
3940 {
3941 semsg(_(e_missing_dict_comma), *arg);
3942 goto failret;
3943 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003944 if (IS_WHITE_OR_NUL(*whitep))
3945 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003946 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003947 return FAIL;
3948 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003949 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003950 if (!IS_WHITE_OR_NUL(*whitep))
3951 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003952 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003953 return FAIL;
3954 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003955 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 *arg = *arg + 1;
3959
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003960 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003961 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003962 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003963 *arg += STRLEN(*arg);
3964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003966 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 return generate_NEWDICT(cctx, count);
3968
3969failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003970 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003971 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003972 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003973 *arg = (char_u *)"";
3974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 dict_unref(d);
3976 return FAIL;
3977}
3978
3979/*
3980 * Compile "&option".
3981 */
3982 static int
3983compile_get_option(char_u **arg, cctx_T *cctx)
3984{
3985 typval_T rettv;
3986 char_u *start = *arg;
3987 int ret;
3988
3989 // parse the option and get the current value to get the type.
3990 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003991 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 if (ret == OK)
3993 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003994 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003995 char_u *name = vim_strnsave(start, *arg - start);
3996 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3997 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998
3999 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4000 vim_free(name);
4001 }
4002 clear_tv(&rettv);
4003
4004 return ret;
4005}
4006
4007/*
4008 * Compile "$VAR".
4009 */
4010 static int
4011compile_get_env(char_u **arg, cctx_T *cctx)
4012{
4013 char_u *start = *arg;
4014 int len;
4015 int ret;
4016 char_u *name;
4017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 ++*arg;
4019 len = get_env_len(arg);
4020 if (len == 0)
4021 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004022 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 return FAIL;
4024 }
4025
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004026 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 name = vim_strnsave(start, len + 1);
4028 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4029 vim_free(name);
4030 return ret;
4031}
4032
4033/*
4034 * Compile "@r".
4035 */
4036 static int
4037compile_get_register(char_u **arg, cctx_T *cctx)
4038{
4039 int ret;
4040
4041 ++*arg;
4042 if (**arg == NUL)
4043 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004044 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
4046 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004047 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 {
4049 emsg_invreg(**arg);
4050 return FAIL;
4051 }
4052 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4053 ++*arg;
4054 return ret;
4055}
4056
4057/*
4058 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004059 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 */
4061 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004062apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004064 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065
4066 // this works from end to start
4067 while (p > start)
4068 {
4069 --p;
4070 if (*p == '-' || *p == '+')
4071 {
4072 // only '-' has an effect, for '+' we only check the type
4073#ifdef FEAT_FLOAT
4074 if (rettv->v_type == VAR_FLOAT)
4075 {
4076 if (*p == '-')
4077 rettv->vval.v_float = -rettv->vval.v_float;
4078 }
4079 else
4080#endif
4081 {
4082 varnumber_T val;
4083 int error = FALSE;
4084
4085 // tv_get_number_chk() accepts a string, but we don't want that
4086 // here
4087 if (check_not_string(rettv) == FAIL)
4088 return FAIL;
4089 val = tv_get_number_chk(rettv, &error);
4090 clear_tv(rettv);
4091 if (error)
4092 return FAIL;
4093 if (*p == '-')
4094 val = -val;
4095 rettv->v_type = VAR_NUMBER;
4096 rettv->vval.v_number = val;
4097 }
4098 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004099 else if (numeric_only)
4100 {
4101 ++p;
4102 break;
4103 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004104 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 {
4106 int v = tv2bool(rettv);
4107
4108 // '!' is permissive in the type.
4109 clear_tv(rettv);
4110 rettv->v_type = VAR_BOOL;
4111 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4112 }
4113 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004114 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 return OK;
4116}
4117
4118/*
4119 * Recognize v: variables that are constants and set "rettv".
4120 */
4121 static void
4122get_vim_constant(char_u **arg, typval_T *rettv)
4123{
4124 if (STRNCMP(*arg, "v:true", 6) == 0)
4125 {
4126 rettv->v_type = VAR_BOOL;
4127 rettv->vval.v_number = VVAL_TRUE;
4128 *arg += 6;
4129 }
4130 else if (STRNCMP(*arg, "v:false", 7) == 0)
4131 {
4132 rettv->v_type = VAR_BOOL;
4133 rettv->vval.v_number = VVAL_FALSE;
4134 *arg += 7;
4135 }
4136 else if (STRNCMP(*arg, "v:null", 6) == 0)
4137 {
4138 rettv->v_type = VAR_SPECIAL;
4139 rettv->vval.v_number = VVAL_NULL;
4140 *arg += 6;
4141 }
4142 else if (STRNCMP(*arg, "v:none", 6) == 0)
4143 {
4144 rettv->v_type = VAR_SPECIAL;
4145 rettv->vval.v_number = VVAL_NONE;
4146 *arg += 6;
4147 }
4148}
4149
Bram Moolenaar657137c2021-01-09 15:45:23 +01004150 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004151get_compare_type(char_u *p, int *len, int *type_is)
4152{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004153 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004154 int i;
4155
4156 switch (p[0])
4157 {
4158 case '=': if (p[1] == '=')
4159 type = EXPR_EQUAL;
4160 else if (p[1] == '~')
4161 type = EXPR_MATCH;
4162 break;
4163 case '!': if (p[1] == '=')
4164 type = EXPR_NEQUAL;
4165 else if (p[1] == '~')
4166 type = EXPR_NOMATCH;
4167 break;
4168 case '>': if (p[1] != '=')
4169 {
4170 type = EXPR_GREATER;
4171 *len = 1;
4172 }
4173 else
4174 type = EXPR_GEQUAL;
4175 break;
4176 case '<': if (p[1] != '=')
4177 {
4178 type = EXPR_SMALLER;
4179 *len = 1;
4180 }
4181 else
4182 type = EXPR_SEQUAL;
4183 break;
4184 case 'i': if (p[1] == 's')
4185 {
4186 // "is" and "isnot"; but not a prefix of a name
4187 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4188 *len = 5;
4189 i = p[*len];
4190 if (!isalnum(i) && i != '_')
4191 {
4192 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4193 *type_is = TRUE;
4194 }
4195 }
4196 break;
4197 }
4198 return type;
4199}
4200
4201/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004202 * Skip over an expression, ignoring most errors.
4203 */
4204 static void
4205skip_expr_cctx(char_u **arg, cctx_T *cctx)
4206{
4207 evalarg_T evalarg;
4208
Bram Moolenaar844fb642021-10-23 13:32:30 +01004209 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004210 evalarg.eval_cctx = cctx;
4211 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004212 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004213}
4214
4215/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004217 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 */
4219 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004220compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004222 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223
4224 // this works from end to start
4225 while (p > start)
4226 {
4227 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004228 while (VIM_ISWHITE(*p))
4229 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 if (*p == '-' || *p == '+')
4231 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004232 int negate = *p == '-';
4233 isn_T *isn;
4234 garray_T *stack = &cctx->ctx_type_stack;
4235 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004237 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4238 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4239 return FAIL;
4240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4242 {
4243 --p;
4244 if (*p == '-')
4245 negate = !negate;
4246 }
4247 // only '-' has an effect, for '+' we only check the type
4248 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004249 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004251 if (isn == NULL)
4252 return FAIL;
4253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004255 else if (numeric_only)
4256 {
4257 ++p;
4258 break;
4259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 else
4261 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004262 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004264 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004266 if (p[-1] == '!')
4267 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004270 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 return FAIL;
4272 }
4273 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004274 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 return OK;
4276}
4277
4278/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004279 * Compile "(expression)": recursive!
4280 * Return FAIL/OK.
4281 */
4282 static int
4283compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4284{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004285 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004286 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004287
Bram Moolenaar24156692021-01-14 20:35:49 +01004288 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4289 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004290 if (ppconst->pp_used <= PPSIZE - 10)
4291 {
4292 ret = compile_expr1(arg, cctx, ppconst);
4293 }
4294 else
4295 {
4296 // Not enough space in ppconst, flush constants.
4297 if (generate_ppconst(cctx, ppconst) == FAIL)
4298 return FAIL;
4299 ret = compile_expr0(arg, cctx);
4300 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004301 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4302 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004303 if (**arg == ')')
4304 ++*arg;
4305 else if (ret == OK)
4306 {
4307 emsg(_(e_missing_close));
4308 ret = FAIL;
4309 }
4310 return ret;
4311}
4312
4313/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004315 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 */
4317 static int
4318compile_subscript(
4319 char_u **arg,
4320 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004321 char_u *start_leader,
4322 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004323 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004325 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004326 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 for (;;)
4329 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004330 char_u *p = skipwhite(*arg);
4331
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004332 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004333 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004334 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004335
4336 // If a following line starts with "->{" or "->X" advance to that
4337 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004338 // Also if a following line starts with ".x".
4339 if (next != NULL &&
4340 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004341 && (next[2] == '{'
4342 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004343 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004344 {
4345 next = next_line_from_context(cctx, TRUE);
4346 if (next == NULL)
4347 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004348 *arg = next;
4349 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004350 }
4351 }
4352
Bram Moolenaarcd268012021-07-22 19:11:08 +02004353 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4354 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004355 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004357 garray_T *stack = &cctx->ctx_type_stack;
4358 type_T *type;
4359 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004361 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004362 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004363 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004366 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4367
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004368 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004369 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004371 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004373 if (keeping_dict)
4374 {
4375 keeping_dict = FALSE;
4376 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4377 return FAIL;
4378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004380 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004382 char_u *pstart = p;
4383
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004384 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004385 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004386 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 // something->method()
4389 // Apply the '!', '-' and '+' first:
4390 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004391 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004394 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004395 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004396 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004397 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004398 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004399 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004400 garray_T *stack = &cctx->ctx_type_stack;
4401 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004402 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004403 int expr_isn_start = cctx->ctx_instr.ga_len;
4404 int expr_isn_end;
4405 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004406
4407 // Funcref call: list->(Refs[2])(arg)
4408 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004409 //
4410 // Fist compile the function expression.
4411 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004412 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004413
4414 // Remember the next instruction index, where the instructions
4415 // for arguments are being written.
4416 expr_isn_end = cctx->ctx_instr.ga_len;
4417
4418 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004419 if (**arg != '(')
4420 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004421 if (*skipwhite(*arg) == '(')
4422 emsg(_(e_nowhitespace));
4423 else
4424 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004425 return FAIL;
4426 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004427 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004428 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004429 return FAIL;
4430
Bram Moolenaar2927c072021-04-05 19:41:21 +02004431 // Move the instructions for the arguments to before the
4432 // instructions of the expression and move the type of the
4433 // expression after the argument types. This is what ISN_PCALL
4434 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004435 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004436 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4437 if (arg_isn_count > 0)
4438 {
4439 int expr_isn_count = expr_isn_end - expr_isn_start;
4440 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4441
4442 if (isn == NULL)
4443 return FAIL;
4444 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4445 + expr_isn_start,
4446 sizeof(isn_T) * expr_isn_count);
4447 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4448 + expr_isn_start,
4449 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4450 sizeof(isn_T) * arg_isn_count);
4451 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4452 + expr_isn_start + arg_isn_count,
4453 isn, sizeof(isn_T) * expr_isn_count);
4454 vim_free(isn);
4455
4456 type = ((type_T **)stack->ga_data)[type_idx_start];
4457 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4458 ((type_T **)stack->ga_data) + type_idx_start + 1,
4459 sizeof(type_T *)
4460 * (stack->ga_len - type_idx_start - 1));
4461 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4462 }
4463
Bram Moolenaar7e368202020-12-25 21:56:57 +01004464 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004465 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 return FAIL;
4467 }
4468 else
4469 {
4470 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004471 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004472 if (!eval_isnamec1(*p))
4473 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004474 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004475 return FAIL;
4476 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004477 if (ASCII_ISALPHA(*p) && p[1] == ':')
4478 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004479 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 ;
4481 if (*p != '(')
4482 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004483 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 return FAIL;
4485 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 return FAIL;
4488 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004489 if (keeping_dict)
4490 {
4491 keeping_dict = FALSE;
4492 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4493 return FAIL;
4494 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004496 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004498 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004499
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004500 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004501 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004502 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004503 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004504 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004505 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004506 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004507
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004508 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004509 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004510 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004511 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004512 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004513 // missing first index is equal to zero
4514 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004515 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004516 else
4517 {
4518 if (compile_expr0(arg, cctx) == FAIL)
4519 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004520 if (**arg == ':')
4521 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004522 semsg(_(e_white_space_required_before_and_after_str_at_str),
4523 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004524 return FAIL;
4525 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004526 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004527 return FAIL;
4528 *arg = skipwhite(*arg);
4529 }
4530 if (**arg == ':')
4531 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004532 is_slice = TRUE;
4533 ++*arg;
4534 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4535 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004536 semsg(_(e_white_space_required_before_and_after_str_at_str),
4537 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004538 return FAIL;
4539 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004540 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004541 return FAIL;
4542 if (**arg == ']')
4543 // missing second index is equal to end of string
4544 generate_PUSHNR(cctx, -1);
4545 else
4546 {
4547 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004548 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004549 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004550 return FAIL;
4551 *arg = skipwhite(*arg);
4552 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554
4555 if (**arg != ']')
4556 {
4557 emsg(_(e_missbrac));
4558 return FAIL;
4559 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004560 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004562 if (keeping_dict)
4563 {
4564 keeping_dict = FALSE;
4565 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4566 return FAIL;
4567 }
4568 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004569 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004571 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004573 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004575 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004576 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004577
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004578 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004579 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004580 {
4581 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004582 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004583 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004584 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004585 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 while (eval_isnamec(*p))
4587 MB_PTR_ADV(p);
4588 if (p == *arg)
4589 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004590 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 return FAIL;
4592 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004593 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4594 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004595 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004597 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 *arg = p;
4599 }
4600 else
4601 break;
4602 }
4603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004605 // This needs to be done at runtime to be able to check the type.
4606 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4607 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
4609 return OK;
4610}
4611
4612/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004613 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4614 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004616 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004617 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004618 *
4619 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 */
4621
4622/*
4623 * number number constant
4624 * 0zFFFFFFFF Blob constant
4625 * "string" string constant
4626 * 'string' literal string constant
4627 * &option-name option value
4628 * @r register contents
4629 * identifier variable value
4630 * function() function call
4631 * $VAR environment variable
4632 * (expression) nested expression
4633 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004634 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 *
4636 * Also handle:
4637 * ! in front logical NOT
4638 * - in front unary minus
4639 * + in front unary plus (ignored)
4640 * trailing (arg) funcref/partial call
4641 * trailing [] subscript in String or List
4642 * trailing .name entry in Dictionary
4643 * trailing ->name() method call
4644 */
4645 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646compile_expr7(
4647 char_u **arg,
4648 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004649 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 char_u *start_leader, *end_leader;
4652 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004653 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004654 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004656 ppconst->pp_is_const = FALSE;
4657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 /*
4659 * Skip '!', '-' and '+' characters. They are handled later.
4660 */
4661 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004662 if (eval_leader(arg, TRUE) == FAIL)
4663 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 end_leader = *arg;
4665
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004666 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 switch (**arg)
4668 {
4669 /*
4670 * Number constant.
4671 */
4672 case '0': // also for blob starting with 0z
4673 case '1':
4674 case '2':
4675 case '3':
4676 case '4':
4677 case '5':
4678 case '6':
4679 case '7':
4680 case '8':
4681 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004682 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004684 // Apply "-" and "+" just before the number now, right to
4685 // left. Matters especially when "->" follows. Stops at
4686 // '!'.
4687 if (apply_leader(rettv, TRUE,
4688 start_leader, &end_leader) == FAIL)
4689 {
4690 clear_tv(rettv);
4691 return FAIL;
4692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 break;
4694
4695 /*
4696 * String constant: "string".
4697 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004698 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 return FAIL;
4700 break;
4701
4702 /*
4703 * Literal string constant: 'str''ing'.
4704 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004705 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 return FAIL;
4707 break;
4708
4709 /*
4710 * Constant Vim variable.
4711 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004712 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 ret = NOTDONE;
4714 break;
4715
4716 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 * "true" constant
4718 */
4719 case 't': if (STRNCMP(*arg, "true", 4) == 0
4720 && !eval_isnamec((*arg)[4]))
4721 {
4722 *arg += 4;
4723 rettv->v_type = VAR_BOOL;
4724 rettv->vval.v_number = VVAL_TRUE;
4725 }
4726 else
4727 ret = NOTDONE;
4728 break;
4729
4730 /*
4731 * "false" constant
4732 */
4733 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4734 && !eval_isnamec((*arg)[5]))
4735 {
4736 *arg += 5;
4737 rettv->v_type = VAR_BOOL;
4738 rettv->vval.v_number = VVAL_FALSE;
4739 }
4740 else
4741 ret = NOTDONE;
4742 break;
4743
4744 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004745 * "null" constant
4746 */
4747 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004748 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004749 {
4750 *arg += 4;
4751 rettv->v_type = VAR_SPECIAL;
4752 rettv->vval.v_number = VVAL_NULL;
4753 }
4754 else
4755 ret = NOTDONE;
4756 break;
4757
4758 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 * List: [expr, expr]
4760 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004761 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4762 return FAIL;
4763 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 break;
4765
4766 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 * Dictionary: {'key': val, 'key': val}
4768 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004769 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4770 return FAIL;
4771 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 break;
4773
4774 /*
4775 * Option value: &name
4776 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004777 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4778 return FAIL;
4779 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 break;
4781
4782 /*
4783 * Environment variable: $VAR.
4784 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004785 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4786 return FAIL;
4787 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 break;
4789
4790 /*
4791 * Register contents: @r.
4792 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004793 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4794 return FAIL;
4795 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 break;
4797 /*
4798 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004799 * lambda: (arg, arg) => expr
4800 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004802 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4803 ret = compile_lambda(arg, cctx);
4804 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004805 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 break;
4807
4808 default: ret = NOTDONE;
4809 break;
4810 }
4811 if (ret == FAIL)
4812 return FAIL;
4813
Bram Moolenaar1c747212020-05-09 18:28:34 +02004814 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004816 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004817 clear_tv(rettv);
4818 else
4819 // A constant expression can possibly be handled compile time,
4820 // return the value instead of generating code.
4821 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 }
4823 else if (ret == NOTDONE)
4824 {
4825 char_u *p;
4826 int r;
4827
4828 if (!eval_isnamec1(**arg))
4829 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004830 if (!vim9_bad_comment(*arg))
4831 {
4832 if (ends_excmd(*skipwhite(*arg)))
4833 semsg(_(e_empty_expression_str), *arg);
4834 else
4835 semsg(_(e_name_expected_str), *arg);
4836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 return FAIL;
4838 }
4839
4840 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004841 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004842 if (p - *arg == (size_t)1 && **arg == '_')
4843 {
4844 emsg(_(e_cannot_use_underscore_here));
4845 return FAIL;
4846 }
4847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849 {
4850 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004854 if (cctx->ctx_skip != SKIP_YES
4855 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004857 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 if (r == FAIL)
4860 return FAIL;
4861 }
4862
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004863 // Handle following "[]", ".member", etc.
4864 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004865 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 ppconst) == FAIL)
4867 return FAIL;
4868 if (ppconst->pp_used > 0)
4869 {
4870 // apply the '!', '-' and '+' before the constant
4871 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004872 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 return FAIL;
4874 return OK;
4875 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004876 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004878 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879}
4880
4881/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004882 * Give the "white on both sides" error, taking the operator from "p[len]".
4883 */
4884 void
4885error_white_both(char_u *op, int len)
4886{
4887 char_u buf[10];
4888
4889 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004890 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004891}
4892
4893/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004894 * <type>expr7: runtime type check / conversion
4895 */
4896 static int
4897compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4898{
4899 type_T *want_type = NULL;
4900
4901 // Recognize <type>
4902 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4903 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004904 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004905 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4906 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004907 return FAIL;
4908
4909 if (**arg != '>')
4910 {
4911 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004912 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004913 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004914 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004915 return FAIL;
4916 }
4917 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004918 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004919 return FAIL;
4920 }
4921
4922 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4923 return FAIL;
4924
4925 if (want_type != NULL)
4926 {
4927 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004928 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004929 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004930
Bram Moolenaard1103582020-08-14 22:44:25 +02004931 generate_ppconst(cctx, ppconst);
4932 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004933 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004934 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004935 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004936 return FAIL;
4937 }
4938 }
4939
4940 return OK;
4941}
4942
4943/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 * * number multiplication
4945 * / number division
4946 * % number modulo
4947 */
4948 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004949compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950{
4951 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004952 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004953 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004955 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004956 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 return FAIL;
4958
4959 /*
4960 * Repeat computing, until no "*", "/" or "%" is following.
4961 */
4962 for (;;)
4963 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004964 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 if (*op != '*' && *op != '/' && *op != '%')
4966 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004967 if (next != NULL)
4968 {
4969 *arg = next_line_from_context(cctx, TRUE);
4970 op = skipwhite(*arg);
4971 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004972
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004973 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004975 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004978 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004979 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004981 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004982 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004984
4985 if (ppconst->pp_used == ppconst_used + 2
4986 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4987 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004988 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004989 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4990 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4991 varnumber_T res = 0;
4992 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004994 // both are numbers: compute the result
4995 switch (*op)
4996 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004997 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004998 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004999 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005000 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005001 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005002 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005003 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005004 break;
5005 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005006 if (failed)
5007 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005008 tv1->vval.v_number = res;
5009 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005010 }
5011 else
5012 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005013 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005014 generate_two_op(cctx, op);
5015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 }
5017
5018 return OK;
5019}
5020
5021/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005022 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 * - number subtraction
5024 * .. string concatenation
5025 */
5026 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028{
5029 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005030 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033
5034 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005035 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 return FAIL;
5037
5038 /*
5039 * Repeat computing, until no "+", "-" or ".." is following.
5040 */
5041 for (;;)
5042 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005043 op = may_peek_next_line(cctx, *arg, &next);
5044 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005046 if (op[0] == op[1] && *op != '.' && next)
5047 // Finding "++" or "--" on the next line is a separate command.
5048 // But ".." is concatenation.
5049 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005051 if (next != NULL)
5052 {
5053 *arg = next_line_from_context(cctx, TRUE);
5054 op = skipwhite(*arg);
5055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005057 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005059 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005060 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 }
5062
Bram Moolenaare0de1712020-12-02 17:36:54 +01005063 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005064 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005066 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005067 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 return FAIL;
5069
Bram Moolenaara5565e42020-05-09 15:44:01 +02005070 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005071 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005072 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5073 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5074 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5075 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5078 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005079
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005080 // concat/subtract/add constant numbers
5081 if (*op == '+')
5082 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5083 else if (*op == '-')
5084 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5085 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005086 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005087 // concatenate constant strings
5088 char_u *s1 = tv1->vval.v_string;
5089 char_u *s2 = tv2->vval.v_string;
5090 size_t len1 = STRLEN(s1);
5091
5092 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5093 if (tv1->vval.v_string == NULL)
5094 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005095 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005096 return FAIL;
5097 }
5098 mch_memmove(tv1->vval.v_string, s1, len1);
5099 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005100 vim_free(s1);
5101 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005102 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 }
5105 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005106 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005107 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005108 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005109 if (*op == '.')
5110 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005111 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5112 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005113 return FAIL;
5114 generate_instr_drop(cctx, ISN_CONCAT, 1);
5115 }
5116 else
5117 generate_two_op(cctx, op);
5118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 }
5120
5121 return OK;
5122}
5123
5124/*
5125 * expr5a == expr5b
5126 * expr5a =~ expr5b
5127 * expr5a != expr5b
5128 * expr5a !~ expr5b
5129 * expr5a > expr5b
5130 * expr5a >= expr5b
5131 * expr5a < expr5b
5132 * expr5a <= expr5b
5133 * expr5a is expr5b
5134 * expr5a isnot expr5b
5135 *
5136 * Produces instructions:
5137 * EVAL expr5a Push result of "expr5a"
5138 * EVAL expr5b Push result of "expr5b"
5139 * COMPARE one of the compare instructions
5140 */
5141 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005142compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005144 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005146 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005149 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150
5151 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005152 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 return FAIL;
5154
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005155 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005156 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157
5158 /*
5159 * If there is a comparative operator, use it.
5160 */
5161 if (type != EXPR_UNKNOWN)
5162 {
5163 int ic = FALSE; // Default: do not ignore case
5164
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005165 if (next != NULL)
5166 {
5167 *arg = next_line_from_context(cctx, TRUE);
5168 p = skipwhite(*arg);
5169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 if (type_is && (p[len] == '?' || p[len] == '#'))
5171 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005172 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 return FAIL;
5174 }
5175 // extra question mark appended: ignore case
5176 if (p[len] == '?')
5177 {
5178 ic = TRUE;
5179 ++len;
5180 }
5181 // extra '#' appended: match case (ignored)
5182 else if (p[len] == '#')
5183 ++len;
5184 // nothing appended: match case
5185
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005186 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005188 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005189 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 }
5191
5192 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005193 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005194 return FAIL;
5195
Bram Moolenaara5565e42020-05-09 15:44:01 +02005196 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 return FAIL;
5198
Bram Moolenaara5565e42020-05-09 15:44:01 +02005199 if (ppconst->pp_used == ppconst_used + 2)
5200 {
5201 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5202 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5203 int ret;
5204
5205 // Both sides are a constant, compute the result now.
5206 // First check for a valid combination of types, this is more
5207 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005208 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005209 ret = FAIL;
5210 else
5211 {
5212 ret = typval_compare(tv1, tv2, type, ic);
5213 tv1->v_type = VAR_BOOL;
5214 tv1->vval.v_number = tv1->vval.v_number
5215 ? VVAL_TRUE : VVAL_FALSE;
5216 clear_tv(tv2);
5217 --ppconst->pp_used;
5218 }
5219 return ret;
5220 }
5221
5222 generate_ppconst(cctx, ppconst);
5223 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 }
5225
5226 return OK;
5227}
5228
Bram Moolenaar7f141552020-05-09 17:35:53 +02005229static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231/*
5232 * Compile || or &&.
5233 */
5234 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005235compile_and_or(
5236 char_u **arg,
5237 cctx_T *cctx,
5238 char *op,
5239 ppconst_T *ppconst,
5240 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005242 char_u *next;
5243 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 int opchar = *op;
5245
5246 if (p[0] == opchar && p[1] == opchar)
5247 {
5248 garray_T *instr = &cctx->ctx_instr;
5249 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005250 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251
5252 /*
5253 * Repeat until there is no following "||" or "&&"
5254 */
5255 ga_init2(&end_ga, sizeof(int), 10);
5256 while (p[0] == opchar && p[1] == opchar)
5257 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005258 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005259 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005260 int start_ctx_lnum = cctx->ctx_lnum;
5261 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005262 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005263 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005264 jumpwhen_T jump_when = opchar == '|'
5265 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005266
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005267 if (next != NULL)
5268 {
5269 *arg = next_line_from_context(cctx, TRUE);
5270 p = skipwhite(*arg);
5271 }
5272
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005273 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5274 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005275 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005276 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005277 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005278 return FAIL;
5279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005281 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005282 SOURCING_LNUM = start_lnum;
5283 save_lnum = cctx->ctx_lnum;
5284 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005285
5286 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005287 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005288 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005289 // Use the last ppconst if possible.
5290 if (ppconst->pp_used > 0)
5291 {
5292 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5293 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005294
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005295 if ((is_true && opchar == '|')
5296 || (!is_true && opchar == '&'))
5297 {
5298 // For "false && expr" and "true || expr" the "expr"
5299 // does not need to be evaluated.
5300 cctx->ctx_skip = SKIP_YES;
5301 clear_tv(tv);
5302 tv->v_type = VAR_BOOL;
5303 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5304 }
5305 else
5306 {
5307 // For "true && expr" and "false || expr" only "expr"
5308 // needs to be evaluated.
5309 --ppconst->pp_used;
5310 jump_when = JUMP_NEVER;
5311 }
5312 }
5313 else
5314 {
5315 // Every part must evaluate to a bool.
5316 status = bool_on_stack(cctx);
5317 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005318 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005319 if (status != FAIL)
5320 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005321 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005322 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 {
5324 ga_clear(&end_ga);
5325 return FAIL;
5326 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005327
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005328 if (jump_when != JUMP_NEVER)
5329 {
5330 if (cctx->ctx_skip != SKIP_YES)
5331 {
5332 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5333 ++end_ga.ga_len;
5334 }
5335 generate_JUMP(cctx, jump_when, 0);
5336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337
5338 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005339 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005340 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005341 {
5342 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005343 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005344 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005345
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005346 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5348 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 {
5350 ga_clear(&end_ga);
5351 return FAIL;
5352 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005353
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005354 // "0 || 1" results in true, "1 && 0" results in false.
5355 if (ppconst->pp_used == const_used + 1)
5356 {
5357 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5358
5359 if (tv->v_type == VAR_NUMBER
5360 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5361 {
5362 tv->vval.v_number = tv->vval.v_number == 1
5363 ? VVAL_TRUE : VVAL_FALSE;
5364 tv->v_type = VAR_BOOL;
5365 }
5366 }
5367
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005368 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005370
5371 if (check_ppconst_bool(ppconst) == FAIL)
5372 {
5373 ga_clear(&end_ga);
5374 return FAIL;
5375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005377 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5378 // Every part must evaluate to a bool.
5379 if (bool_on_stack(cctx) == FAIL)
5380 {
5381 ga_clear(&end_ga);
5382 return FAIL;
5383 }
5384
5385 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005386 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005387 // Fill in the end label in all jumps.
5388 generate_ppconst(cctx, ppconst);
5389 while (end_ga.ga_len > 0)
5390 {
5391 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005392
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005393 --end_ga.ga_len;
5394 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005396 isn->isn_arg.jump.jump_where = instr->ga_len;
5397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005399 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005400
5401 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 }
5403
5404 return OK;
5405}
5406
5407/*
5408 * expr4a && expr4a && expr4a logical AND
5409 *
5410 * Produces instructions:
5411 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005412 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005413 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005415 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 * EVAL expr4c Push result of "expr4c"
5417 * end:
5418 */
5419 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005420compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005422 int ppconst_used = ppconst->pp_used;
5423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005425 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 return FAIL;
5427
5428 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005429 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430}
5431
5432/*
5433 * expr3a || expr3b || expr3c logical OR
5434 *
5435 * Produces instructions:
5436 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005437 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005438 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005440 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 * EVAL expr3c Push result of "expr3c"
5442 * end:
5443 */
5444 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005445compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005447 int ppconst_used = ppconst->pp_used;
5448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005450 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451 return FAIL;
5452
5453 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005454 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455}
5456
5457/*
5458 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005460 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 * JUMP_IF_FALSE alt jump if false
5462 * EVAL expr1a
5463 * JUMP_ALWAYS end
5464 * alt: EVAL expr1b
5465 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005466 *
5467 * Toplevel expression: expr2 ?? expr1
5468 * Produces instructions:
5469 * EVAL expr2 Push result of "expr2"
5470 * JUMP_AND_KEEP_IF_TRUE end jump if true
5471 * EVAL expr1
5472 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 */
5474 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005475compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476{
5477 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005478 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005479 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480
Bram Moolenaar3988f642020-08-27 22:43:03 +02005481 // Ignore all kinds of errors when not producing code.
5482 if (cctx->ctx_skip == SKIP_YES)
5483 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005484 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005485 return OK;
5486 }
5487
Bram Moolenaar61a89812020-05-07 16:58:17 +02005488 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005489 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 return FAIL;
5491
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005492 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 if (*p == '?')
5494 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005495 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 garray_T *instr = &cctx->ctx_instr;
5497 garray_T *stack = &cctx->ctx_type_stack;
5498 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005499 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005501 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005502 int has_const_expr = FALSE;
5503 int const_value = FALSE;
5504 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005506 if (next != NULL)
5507 {
5508 *arg = next_line_from_context(cctx, TRUE);
5509 p = skipwhite(*arg);
5510 }
5511
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005512 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005513 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005514 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005515 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005516 return FAIL;
5517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518
Bram Moolenaara5565e42020-05-09 15:44:01 +02005519 if (ppconst->pp_used == ppconst_used + 1)
5520 {
5521 // the condition is a constant, we know whether the ? or the :
5522 // expression is to be evaluated.
5523 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005524 if (op_falsy)
5525 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5526 else
5527 {
5528 int error = FALSE;
5529
5530 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5531 &error);
5532 if (error)
5533 return FAIL;
5534 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005535 cctx->ctx_skip = save_skip == SKIP_YES ||
5536 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5537
5538 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5539 // "left ?? right" and "left" is truthy: produce "left"
5540 generate_ppconst(cctx, ppconst);
5541 else
5542 {
5543 clear_tv(&ppconst->pp_tv[ppconst_used]);
5544 --ppconst->pp_used;
5545 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005546 }
5547 else
5548 {
5549 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005550 if (op_falsy)
5551 end_idx = instr->ga_len;
5552 generate_JUMP(cctx, op_falsy
5553 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5554 if (op_falsy)
5555 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
5558 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005559 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005560 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005561 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005562 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563
Bram Moolenaara5565e42020-05-09 15:44:01 +02005564 if (!has_const_expr)
5565 {
5566 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005568 if (!op_falsy)
5569 {
5570 // remember the type and drop it
5571 --stack->ga_len;
5572 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005574 end_idx = instr->ga_len;
5575 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005576
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005577 // jump here from JUMP_IF_FALSE
5578 isn = ((isn_T *)instr->ga_data) + alt_idx;
5579 isn->isn_arg.jump.jump_where = instr->ga_len;
5580 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005582
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005583 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005585 // Check for the ":".
5586 p = may_peek_next_line(cctx, *arg, &next);
5587 if (*p != ':')
5588 {
5589 emsg(_(e_missing_colon));
5590 return FAIL;
5591 }
5592 if (next != NULL)
5593 {
5594 *arg = next_line_from_context(cctx, TRUE);
5595 p = skipwhite(*arg);
5596 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005597
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005598 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5599 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005600 semsg(_(e_white_space_required_before_and_after_str_at_str),
5601 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005602 return FAIL;
5603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005605 // evaluate the third expression
5606 if (has_const_expr)
5607 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005608 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005609 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005610 return FAIL;
5611 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5612 return FAIL;
5613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614
Bram Moolenaara5565e42020-05-09 15:44:01 +02005615 if (!has_const_expr)
5616 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005617 type_T **typep;
5618
Bram Moolenaara5565e42020-05-09 15:44:01 +02005619 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620
Bram Moolenaara5565e42020-05-09 15:44:01 +02005621 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005622 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5623 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005624
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005625 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005626 isn = ((isn_T *)instr->ga_data) + end_idx;
5627 isn->isn_arg.jump.jump_where = instr->ga_len;
5628 }
5629
5630 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631 }
5632 return OK;
5633}
5634
5635/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005636 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005637 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5638 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005639 */
5640 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005641compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005642{
5643 ppconst_T ppconst;
5644
5645 CLEAR_FIELD(ppconst);
5646 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5647 {
5648 clear_ppconst(&ppconst);
5649 return FAIL;
5650 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005651 if (is_const != NULL)
5652 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005653 if (generate_ppconst(cctx, &ppconst) == FAIL)
5654 return FAIL;
5655 return OK;
5656}
5657
5658/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005659 * Toplevel expression.
5660 */
5661 static int
5662compile_expr0(char_u **arg, cctx_T *cctx)
5663{
5664 return compile_expr0_ext(arg, cctx, NULL);
5665}
5666
5667/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005668 * Compile "return [expr]".
5669 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 */
5671 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005672compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673{
5674 char_u *p = arg;
5675 garray_T *stack = &cctx->ctx_type_stack;
5676 type_T *stack_type;
5677
5678 if (*p != NUL && *p != '|' && *p != '\n')
5679 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005680 if (legacy)
5681 {
5682 int save_flags = cmdmod.cmod_flags;
5683
5684 generate_LEGACY_EVAL(cctx, p);
5685 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5686 0, cctx, FALSE, FALSE) == FAIL)
5687 return NULL;
5688 cmdmod.cmod_flags |= CMOD_LEGACY;
5689 (void)skip_expr(&p, NULL);
5690 cmdmod.cmod_flags = save_flags;
5691 }
5692 else
5693 {
5694 // compile return argument into instructions
5695 if (compile_expr0(&p, cctx) == FAIL)
5696 return NULL;
5697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005699 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005700 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005701 // "check_return_type" with uf_ret_type set to &t_unknown is used
5702 // for an inline function without a specified return type. Set the
5703 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005704 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005705 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005706 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5707 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005708 || (!check_return_type
5709 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005710 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005711 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005712 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005713 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005714 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005715 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5716 && stack_type->tt_type != VAR_VOID
5717 && stack_type->tt_type != VAR_UNKNOWN)
5718 {
5719 emsg(_(e_returning_value_in_function_without_return_type));
5720 return NULL;
5721 }
5722 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005723 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005724 return NULL;
5725 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727 }
5728 else
5729 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005730 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005731 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005732 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5733 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005735 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736 return NULL;
5737 }
5738
5739 // No argument, return zero.
5740 generate_PUSHNR(cctx, 0);
5741 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005742
5743 // Undo any command modifiers.
5744 generate_undo_cmdmods(cctx);
5745
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005746 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 return NULL;
5748
5749 // "return val | endif" is possible
5750 return skipwhite(p);
5751}
5752
5753/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005754 * Get a line from the compilation context, compatible with exarg_T getline().
5755 * Return a pointer to the line in allocated memory.
5756 * Return NULL for end-of-file or some error.
5757 */
5758 static char_u *
5759exarg_getline(
5760 int c UNUSED,
5761 void *cookie,
5762 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005763 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005764{
5765 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005766 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005767
Bram Moolenaar66250c92020-08-20 15:02:42 +02005768 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005769 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005770 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005771 return NULL;
5772 ++cctx->ctx_lnum;
5773 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5774 // Comment lines result in NULL pointers, skip them.
5775 if (p != NULL)
5776 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005777 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005778}
5779
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005780 void
5781fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5782{
5783 eap->getline = exarg_getline;
5784 eap->cookie = cctx;
5785}
5786
Bram Moolenaar04b12692020-05-04 23:24:44 +02005787/*
5788 * Compile a nested :def command.
5789 */
5790 static char_u *
5791compile_nested_function(exarg_T *eap, cctx_T *cctx)
5792{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005793 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005794 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005795 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005796 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005797 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005798 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005799 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005800
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005801 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005802 {
5803 emsg(_(e_cannot_use_bang_with_nested_def));
5804 return NULL;
5805 }
5806
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005807 if (*name_start == '/')
5808 {
5809 name_end = skip_regexp(name_start + 1, '/', TRUE);
5810 if (*name_end == '/')
5811 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005812 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005813 }
5814 if (name_end == name_start || *skipwhite(name_end) != '(')
5815 {
5816 if (!ends_excmd2(name_start, name_end))
5817 {
5818 semsg(_(e_invalid_command_str), eap->cmd);
5819 return NULL;
5820 }
5821
5822 // "def" or "def Name": list functions
5823 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5824 return NULL;
5825 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5826 }
5827
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005828 // Only g:Func() can use a namespace.
5829 if (name_start[1] == ':' && !is_global)
5830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005831 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005832 return NULL;
5833 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005834 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005835 return NULL;
5836
Bram Moolenaar04b12692020-05-04 23:24:44 +02005837 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005838 fill_exarg_from_cctx(eap, cctx);
5839
Bram Moolenaar04b12692020-05-04 23:24:44 +02005840 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005841 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005842 lambda_name = vim_strsave(get_lambda_name());
5843 if (lambda_name == NULL)
5844 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005845 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005846
Bram Moolenaar822ba242020-05-24 23:00:18 +02005847 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005848 {
5849 r = eap->skip ? OK : FAIL;
5850 goto theend;
5851 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005852
5853 // copy over the block scope IDs before compiling
5854 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5855 {
5856 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5857
5858 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5859 if (ufunc->uf_block_ids != NULL)
5860 {
5861 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5862 sizeof(int) * block_depth);
5863 ufunc->uf_block_depth = block_depth;
5864 }
5865 }
5866
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005867 compile_type = COMPILE_TYPE(ufunc);
5868#ifdef FEAT_PROFILE
5869 // If the outer function is profiled, also compile the nested function for
5870 // profiling.
5871 if (cctx->ctx_compile_type == CT_PROFILE)
5872 compile_type = CT_PROFILE;
5873#endif
5874 if (func_needs_compiling(ufunc, compile_type)
5875 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005876 {
5877 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005878 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005879 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005880
Bram Moolenaar648594e2021-07-11 17:55:01 +02005881#ifdef FEAT_PROFILE
5882 // When the outer function is compiled for profiling, the nested function
5883 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005884 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005885 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5886#endif
5887
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005888 if (is_global)
5889 {
5890 char_u *func_name = vim_strnsave(name_start + 2,
5891 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005892
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005893 if (func_name == NULL)
5894 r = FAIL;
5895 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005896 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005897 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005898 lambda_name = NULL;
5899 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005900 }
5901 else
5902 {
5903 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005904 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005905 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005906
Bram Moolenaareef21022020-08-01 22:16:43 +02005907 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005908 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005909 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005910 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005911 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5912 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005913
5914theend:
5915 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005916 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005917}
5918
5919/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 * Return the length of an assignment operator, or zero if there isn't one.
5921 */
5922 int
5923assignment_len(char_u *p, int *heredoc)
5924{
5925 if (*p == '=')
5926 {
5927 if (p[1] == '<' && p[2] == '<')
5928 {
5929 *heredoc = TRUE;
5930 return 3;
5931 }
5932 return 1;
5933 }
5934 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5935 return 2;
5936 if (STRNCMP(p, "..=", 3) == 0)
5937 return 3;
5938 return 0;
5939}
5940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005942 * Generate the load instruction for "name".
5943 */
5944 static void
5945generate_loadvar(
5946 cctx_T *cctx,
5947 assign_dest_T dest,
5948 char_u *name,
5949 lvar_T *lvar,
5950 type_T *type)
5951{
5952 switch (dest)
5953 {
5954 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005955 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5956 break;
5957 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005958 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5959 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5960 else
5961 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005962 break;
5963 case dest_buffer:
5964 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5965 break;
5966 case dest_window:
5967 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5968 break;
5969 case dest_tab:
5970 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5971 break;
5972 case dest_script:
5973 compile_load_scriptvar(cctx,
5974 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5975 break;
5976 case dest_env:
5977 // Include $ in the name here
5978 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5979 break;
5980 case dest_reg:
5981 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5982 break;
5983 case dest_vimvar:
5984 generate_LOADV(cctx, name + 2, TRUE);
5985 break;
5986 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005987 if (lvar->lv_from_outer > 0)
5988 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5989 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005990 else
5991 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5992 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005993 case dest_expr:
5994 // list or dict value should already be on the stack.
5995 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005996 }
5997}
5998
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005999/*
6000 * Skip over "[expr]" or ".member".
6001 * Does not check for any errors.
6002 */
6003 static char_u *
6004skip_index(char_u *start)
6005{
6006 char_u *p = start;
6007
6008 if (*p == '[')
6009 {
6010 p = skipwhite(p + 1);
6011 (void)skip_expr(&p, NULL);
6012 p = skipwhite(p);
6013 if (*p == ']')
6014 return p + 1;
6015 return p;
6016 }
6017 // if (*p == '.')
6018 return to_name_end(p + 1, TRUE);
6019}
6020
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006021 void
6022vim9_declare_error(char_u *name)
6023{
6024 char *scope = "";
6025
6026 switch (*name)
6027 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006028 case 'g': scope = _("global"); break;
6029 case 'b': scope = _("buffer"); break;
6030 case 'w': scope = _("window"); break;
6031 case 't': scope = _("tab"); break;
6032 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006033 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006034 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006035 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006036 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006037 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006038 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006039 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006040 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006041 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006042}
6043
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006044/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006045 * For one assignment figure out the type of destination. Return it in "dest".
6046 * When not recognized "dest" is not set.
6047 * For an option "opt_flags" is set.
6048 * For a v:var "vimvaridx" is set.
6049 * "type" is set to the destination type if known, unchanted otherwise.
6050 * Return FAIL if an error message was given.
6051 */
6052 static int
6053get_var_dest(
6054 char_u *name,
6055 assign_dest_T *dest,
6056 int cmdidx,
6057 int *opt_flags,
6058 int *vimvaridx,
6059 type_T **type,
6060 cctx_T *cctx)
6061{
6062 char_u *p;
6063
6064 if (*name == '&')
6065 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006066 int cc;
6067 long numval;
6068 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006069
6070 *dest = dest_option;
6071 if (cmdidx == CMD_final || cmdidx == CMD_const)
6072 {
6073 emsg(_(e_const_option));
6074 return FAIL;
6075 }
6076 p = name;
6077 p = find_option_end(&p, opt_flags);
6078 if (p == NULL)
6079 {
6080 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006081 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006082 return FAIL;
6083 }
6084 cc = *p;
6085 *p = NUL;
6086 opt_type = get_option_value(skip_option_env_lead(name),
6087 &numval, NULL, *opt_flags);
6088 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006089 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006090 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006091 case gov_unknown:
6092 semsg(_(e_unknown_option), name);
6093 return FAIL;
6094 case gov_string:
6095 case gov_hidden_string:
6096 *type = &t_string;
6097 break;
6098 case gov_bool:
6099 case gov_hidden_bool:
6100 *type = &t_bool;
6101 break;
6102 case gov_number:
6103 case gov_hidden_number:
6104 *type = &t_number;
6105 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006106 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006107 }
6108 else if (*name == '$')
6109 {
6110 *dest = dest_env;
6111 *type = &t_string;
6112 }
6113 else if (*name == '@')
6114 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006115 if (name[1] != '@'
6116 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006117 {
6118 emsg_invreg(name[1]);
6119 return FAIL;
6120 }
6121 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006122 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006123 }
6124 else if (STRNCMP(name, "g:", 2) == 0)
6125 {
6126 *dest = dest_global;
6127 }
6128 else if (STRNCMP(name, "b:", 2) == 0)
6129 {
6130 *dest = dest_buffer;
6131 }
6132 else if (STRNCMP(name, "w:", 2) == 0)
6133 {
6134 *dest = dest_window;
6135 }
6136 else if (STRNCMP(name, "t:", 2) == 0)
6137 {
6138 *dest = dest_tab;
6139 }
6140 else if (STRNCMP(name, "v:", 2) == 0)
6141 {
6142 typval_T *vtv;
6143 int di_flags;
6144
6145 *vimvaridx = find_vim_var(name + 2, &di_flags);
6146 if (*vimvaridx < 0)
6147 {
6148 semsg(_(e_variable_not_found_str), name);
6149 return FAIL;
6150 }
6151 // We use the current value of "sandbox" here, is that OK?
6152 if (var_check_ro(di_flags, name, FALSE))
6153 return FAIL;
6154 *dest = dest_vimvar;
6155 vtv = get_vim_var_tv(*vimvaridx);
6156 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6157 }
6158 return OK;
6159}
6160
6161/*
6162 * Generate a STORE instruction for "dest", not being "dest_local".
6163 * Return FAIL when out of memory.
6164 */
6165 static int
6166generate_store_var(
6167 cctx_T *cctx,
6168 assign_dest_T dest,
6169 int opt_flags,
6170 int vimvaridx,
6171 int scriptvar_idx,
6172 int scriptvar_sid,
6173 type_T *type,
6174 char_u *name)
6175{
6176 switch (dest)
6177 {
6178 case dest_option:
6179 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6180 opt_flags);
6181 case dest_global:
6182 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006183 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6184 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006185 case dest_buffer:
6186 // include b: with the name, easier to execute that way
6187 return generate_STORE(cctx, ISN_STOREB, 0, name);
6188 case dest_window:
6189 // include w: with the name, easier to execute that way
6190 return generate_STORE(cctx, ISN_STOREW, 0, name);
6191 case dest_tab:
6192 // include t: with the name, easier to execute that way
6193 return generate_STORE(cctx, ISN_STORET, 0, name);
6194 case dest_env:
6195 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6196 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006197 return generate_STORE(cctx, ISN_STOREREG,
6198 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006199 case dest_vimvar:
6200 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6201 case dest_script:
6202 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006203 // "s:" may be included in the name.
6204 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006205 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006206 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6207 scriptvar_sid, scriptvar_idx, type);
6208 case dest_local:
6209 case dest_expr:
6210 // cannot happen
6211 break;
6212 }
6213 return FAIL;
6214}
6215
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006217generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6218{
6219 if (lhs->lhs_dest != dest_local)
6220 return generate_store_var(cctx, lhs->lhs_dest,
6221 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6222 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6223 lhs->lhs_type, lhs->lhs_name);
6224
6225 if (lhs->lhs_lvar != NULL)
6226 {
6227 garray_T *instr = &cctx->ctx_instr;
6228 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6229
6230 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6231 // ISN_STORENR
6232 if (lhs->lhs_lvar->lv_from_outer == 0
6233 && instr->ga_len == instr_count + 1
6234 && isn->isn_type == ISN_PUSHNR)
6235 {
6236 varnumber_T val = isn->isn_arg.number;
6237 garray_T *stack = &cctx->ctx_type_stack;
6238
6239 isn->isn_type = ISN_STORENR;
6240 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6241 isn->isn_arg.storenr.stnr_val = val;
6242 if (stack->ga_len > 0)
6243 --stack->ga_len;
6244 }
6245 else if (lhs->lhs_lvar->lv_from_outer > 0)
6246 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6247 lhs->lhs_lvar->lv_from_outer);
6248 else
6249 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6250 }
6251 return OK;
6252}
6253
6254 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006255is_decl_command(int cmdidx)
6256{
6257 return cmdidx == CMD_let || cmdidx == CMD_var
6258 || cmdidx == CMD_final || cmdidx == CMD_const;
6259}
6260
6261/*
6262 * Figure out the LHS type and other properties for an assignment or one item
6263 * of ":unlet" with an index.
6264 * Returns OK or FAIL.
6265 */
6266 static int
6267compile_lhs(
6268 char_u *var_start,
6269 lhs_T *lhs,
6270 int cmdidx,
6271 int heredoc,
6272 int oplen,
6273 cctx_T *cctx)
6274{
6275 char_u *var_end;
6276 int is_decl = is_decl_command(cmdidx);
6277
6278 CLEAR_POINTER(lhs);
6279 lhs->lhs_dest = dest_local;
6280 lhs->lhs_vimvaridx = -1;
6281 lhs->lhs_scriptvar_idx = -1;
6282
6283 // "dest_end" is the end of the destination, including "[expr]" or
6284 // ".name".
6285 // "var_end" is the end of the variable/option/etc. name.
6286 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6287 if (*var_start == '@')
6288 var_end = var_start + 2;
6289 else
6290 {
6291 // skip over the leading "&", "&l:", "&g:" and "$"
6292 var_end = skip_option_env_lead(var_start);
6293 var_end = to_name_end(var_end, TRUE);
6294 }
6295
6296 // "a: type" is declaring variable "a" with a type, not dict "a:".
6297 if (is_decl && lhs->lhs_dest_end == var_start + 2
6298 && lhs->lhs_dest_end[-1] == ':')
6299 --lhs->lhs_dest_end;
6300 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6301 --var_end;
6302
6303 // compute the length of the destination without "[expr]" or ".name"
6304 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006305 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006306 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6307 if (lhs->lhs_name == NULL)
6308 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006309
6310 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6311 // Something follows after the variable: "var[idx]" or "var.key".
6312 lhs->lhs_has_index = TRUE;
6313
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 if (heredoc)
6315 lhs->lhs_type = &t_list_string;
6316 else
6317 lhs->lhs_type = &t_any;
6318
6319 if (cctx->ctx_skip != SKIP_YES)
6320 {
6321 int declare_error = FALSE;
6322
6323 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6324 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6325 &lhs->lhs_type, cctx) == FAIL)
6326 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006327 if (lhs->lhs_dest != dest_local
6328 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006329 {
6330 // Specific kind of variable recognized.
6331 declare_error = is_decl;
6332 }
6333 else
6334 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006336 if (check_reserved_name(lhs->lhs_name) == FAIL)
6337 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338
6339 if (lookup_local(var_start, lhs->lhs_varlen,
6340 &lhs->lhs_local_lvar, cctx) == OK)
6341 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6342 else
6343 {
6344 CLEAR_FIELD(lhs->lhs_arg_lvar);
6345 if (arg_exists(var_start, lhs->lhs_varlen,
6346 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6347 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6348 {
6349 if (is_decl)
6350 {
6351 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6352 return FAIL;
6353 }
6354 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6355 }
6356 }
6357 if (lhs->lhs_lvar != NULL)
6358 {
6359 if (is_decl)
6360 {
6361 semsg(_(e_variable_already_declared), lhs->lhs_name);
6362 return FAIL;
6363 }
6364 }
6365 else
6366 {
6367 int script_namespace = lhs->lhs_varlen > 1
6368 && STRNCMP(var_start, "s:", 2) == 0;
6369 int script_var = (script_namespace
6370 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006371 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006373 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006374 imported_T *import =
6375 find_imported(var_start, lhs->lhs_varlen, cctx);
6376
6377 if (script_namespace || script_var || import != NULL)
6378 {
6379 char_u *rawname = lhs->lhs_name
6380 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6381
6382 if (is_decl)
6383 {
6384 if (script_namespace)
6385 semsg(_(e_cannot_declare_script_variable_in_function),
6386 lhs->lhs_name);
6387 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006388 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006389 lhs->lhs_name);
6390 return FAIL;
6391 }
6392 else if (cctx->ctx_ufunc->uf_script_ctx_version
6393 == SCRIPT_VERSION_VIM9
6394 && script_namespace
6395 && !script_var && import == NULL)
6396 {
6397 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6398 return FAIL;
6399 }
6400
6401 lhs->lhs_dest = dest_script;
6402
6403 // existing script-local variables should have a type
6404 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6405 if (import != NULL)
6406 lhs->lhs_scriptvar_sid = import->imp_sid;
6407 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6408 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006409 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006410 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006411 lhs->lhs_scriptvar_sid, rawname,
6412 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6413 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006414 if (lhs->lhs_scriptvar_idx >= 0)
6415 {
6416 scriptitem_T *si = SCRIPT_ITEM(
6417 lhs->lhs_scriptvar_sid);
6418 svar_T *sv =
6419 ((svar_T *)si->sn_var_vals.ga_data)
6420 + lhs->lhs_scriptvar_idx;
6421 lhs->lhs_type = sv->sv_type;
6422 }
6423 }
6424 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006425 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426 == FAIL)
6427 return FAIL;
6428 }
6429 }
6430
6431 if (declare_error)
6432 {
6433 vim9_declare_error(lhs->lhs_name);
6434 return FAIL;
6435 }
6436 }
6437
6438 // handle "a:name" as a name, not index "name" on "a"
6439 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6440 var_end = lhs->lhs_dest_end;
6441
6442 if (lhs->lhs_dest != dest_option)
6443 {
6444 if (is_decl && *var_end == ':')
6445 {
6446 char_u *p;
6447
6448 // parse optional type: "let var: type = expr"
6449 if (!VIM_ISWHITE(var_end[1]))
6450 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006451 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 return FAIL;
6453 }
6454 p = skipwhite(var_end + 1);
6455 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6456 if (lhs->lhs_type == NULL)
6457 return FAIL;
6458 lhs->lhs_has_type = TRUE;
6459 }
6460 else if (lhs->lhs_lvar != NULL)
6461 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6462 }
6463
Bram Moolenaare42939a2021-04-05 17:11:17 +02006464 if (oplen == 3 && !heredoc
6465 && lhs->lhs_dest != dest_global
6466 && !lhs->lhs_has_index
6467 && lhs->lhs_type->tt_type != VAR_STRING
6468 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 {
6470 emsg(_(e_can_only_concatenate_to_string));
6471 return FAIL;
6472 }
6473
6474 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6475 && cctx->ctx_skip != SKIP_YES)
6476 {
6477 if (oplen > 1 && !heredoc)
6478 {
6479 // +=, /=, etc. require an existing variable
6480 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6481 return FAIL;
6482 }
6483 if (!is_decl)
6484 {
6485 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6486 return FAIL;
6487 }
6488
Bram Moolenaar3f327882021-03-17 20:56:38 +01006489 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 if ((lhs->lhs_type->tt_type == VAR_FUNC
6491 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006492 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006494
6495 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6497 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6498 if (lhs->lhs_lvar == NULL)
6499 return FAIL;
6500 lhs->lhs_new_local = TRUE;
6501 }
6502
6503 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006504 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006506 char_u *after = var_start + lhs->lhs_varlen;
6507 char_u *p;
6508
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006510 if (is_decl)
6511 {
6512 emsg(_(e_cannot_use_index_when_declaring_variable));
6513 return FAIL;
6514 }
6515
Bram Moolenaarc750d912021-11-29 22:02:12 +00006516 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6517 // Only the last index is used below, if there are others
6518 // before it generate code for the expression. Thus for
6519 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6520 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006522 p = skip_index(after);
6523 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006525 lhs->lhs_varlen_total = p - var_start;
6526 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006528 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006529 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006530 if (after > var_start + lhs->lhs_varlen)
6531 {
6532 lhs->lhs_varlen = after - var_start;
6533 lhs->lhs_dest = dest_expr;
6534 // We don't know the type before evaluating the expression,
6535 // use "any" until then.
6536 lhs->lhs_type = &t_any;
6537 }
6538
6539 if (lhs->lhs_type->tt_member == NULL)
6540 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006542 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006543 }
6544 return OK;
6545}
6546
6547/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006548 * Figure out the LHS and check a few errors.
6549 */
6550 static int
6551compile_assign_lhs(
6552 char_u *var_start,
6553 lhs_T *lhs,
6554 int cmdidx,
6555 int is_decl,
6556 int heredoc,
6557 int oplen,
6558 cctx_T *cctx)
6559{
6560 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6561 return FAIL;
6562
6563 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6564 {
6565 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6566 return FAIL;
6567 }
6568 if (!is_decl && lhs->lhs_lvar != NULL
6569 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6570 {
6571 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6572 return FAIL;
6573 }
6574 return OK;
6575}
6576
6577/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006578 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6579 */
6580 static int
6581has_list_index(char_u *idx_start, cctx_T *cctx)
6582{
6583 char_u *p = idx_start;
6584 int save_skip;
6585
6586 if (*p != '[')
6587 return FALSE;
6588
6589 p = skipwhite(p + 1);
6590 if (*p == ':')
6591 return TRUE;
6592
6593 save_skip = cctx->ctx_skip;
6594 cctx->ctx_skip = SKIP_YES;
6595 (void)compile_expr0(&p, cctx);
6596 cctx->ctx_skip = save_skip;
6597 return *skipwhite(p) == ':';
6598}
6599
6600/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006601 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6602 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006603 */
6604 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006605compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006606 char_u *var_start,
6607 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006608 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 cctx_T *cctx)
6610{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006612 char_u *p;
6613 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006614 int need_white_before = TRUE;
6615 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616
Bram Moolenaar752fc692021-01-04 21:57:11 +01006617 p = var_start + varlen;
6618 if (*p == '[')
6619 {
6620 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006621 if (*p == ':')
6622 {
6623 // empty first index, push zero
6624 r = generate_PUSHNR(cctx, 0);
6625 need_white_before = FALSE;
6626 }
6627 else
6628 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006629
6630 if (r == OK && *skipwhite(p) == ':')
6631 {
6632 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006633 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006634 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006635 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006636 empty_second = *skipwhite(p + 1) == ']';
6637 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6638 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006639 {
6640 semsg(_(e_white_space_required_before_and_after_str_at_str),
6641 ":", p);
6642 return FAIL;
6643 }
6644 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006645 if (*p == ']')
6646 // empty second index, push "none"
6647 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6648 else
6649 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006650 }
6651
Bram Moolenaar752fc692021-01-04 21:57:11 +01006652 if (r == OK && *skipwhite(p) != ']')
6653 {
6654 // this should not happen
6655 emsg(_(e_missbrac));
6656 r = FAIL;
6657 }
6658 }
6659 else // if (*p == '.')
6660 {
6661 char_u *key_end = to_name_end(p + 1, TRUE);
6662 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6663
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006664 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006666 return r;
6667}
6668
6669/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006670 * For a LHS with an index, load the variable to be indexed.
6671 */
6672 static int
6673compile_load_lhs(
6674 lhs_T *lhs,
6675 char_u *var_start,
6676 type_T *rhs_type,
6677 cctx_T *cctx)
6678{
6679 if (lhs->lhs_dest == dest_expr)
6680 {
6681 size_t varlen = lhs->lhs_varlen;
6682 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006683 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006684 char_u *p = var_start;
6685 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006686 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006687
Bram Moolenaare97976b2021-08-01 13:17:17 +02006688 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6689 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006690 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006691 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6692 res = compile_expr0(&p, cctx);
6693 var_start[varlen] = c;
6694 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6695 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006696 {
6697 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006698 if (res != FAIL)
6699 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006700 return FAIL;
6701 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006702
6703 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6704 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6705 // now we can properly check the type
6706 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6707 && rhs_type != &t_void
6708 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6709 FALSE, FALSE) == FAIL)
6710 return FAIL;
6711 }
6712 else
6713 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6714 lhs->lhs_lvar, lhs->lhs_type);
6715 return OK;
6716}
6717
6718/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006719 * Produce code for loading "lhs" and also take care of an index.
6720 * Return OK/FAIL.
6721 */
6722 static int
6723compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6724{
6725 compile_load_lhs(lhs, var_start, NULL, cctx);
6726
6727 if (lhs->lhs_has_index)
6728 {
6729 int range = FALSE;
6730
6731 // Get member from list or dict. First compile the
6732 // index value.
6733 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6734 return FAIL;
6735 if (range)
6736 {
6737 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6738 var_start);
6739 return FAIL;
6740 }
6741
6742 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006743 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006744 return FAIL;
6745 }
6746 return OK;
6747}
6748
6749/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006750 * Assignment to a list or dict member, or ":unlet" for the item, using the
6751 * information in "lhs".
6752 * Returns OK or FAIL.
6753 */
6754 static int
6755compile_assign_unlet(
6756 char_u *var_start,
6757 lhs_T *lhs,
6758 int is_assign,
6759 type_T *rhs_type,
6760 cctx_T *cctx)
6761{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006762 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006763 garray_T *stack = &cctx->ctx_type_stack;
6764 int range = FALSE;
6765
Bram Moolenaar68452172021-04-12 21:21:02 +02006766 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006767 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006768 if (is_assign && range
6769 && lhs->lhs_type->tt_type != VAR_LIST
6770 && lhs->lhs_type != &t_blob
6771 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006772 {
6773 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6774 return FAIL;
6775 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006776
6777 if (lhs->lhs_type == &t_any)
6778 {
6779 // Index on variable of unknown type: check at runtime.
6780 dest_type = VAR_ANY;
6781 }
6782 else
6783 {
6784 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006785 if (dest_type == VAR_DICT && range)
6786 {
6787 emsg(e_cannot_use_range_with_dictionary);
6788 return FAIL;
6789 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006790 if (dest_type == VAR_DICT
6791 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006792 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006793 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006794 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006795 type_T *type;
6796
6797 if (range)
6798 {
6799 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6800 if (need_type(type, &t_number,
6801 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006802 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006803 }
6804 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006805 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006806 && need_type(type, &t_number,
6807 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006808 return FAIL;
6809 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 }
6811
6812 // Load the dict or list. On the stack we then have:
6813 // - value (for assignment, not for :unlet)
6814 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006815 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006816 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006817 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6818 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006819
Bram Moolenaar68452172021-04-12 21:21:02 +02006820 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6821 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006822 {
6823 if (is_assign)
6824 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006825 if (range)
6826 {
6827 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6828 return FAIL;
6829 }
6830 else
6831 {
6832 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006833
Bram Moolenaar68452172021-04-12 21:21:02 +02006834 if (isn == NULL)
6835 return FAIL;
6836 isn->isn_arg.vartype = dest_type;
6837 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006838 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006839 else if (range)
6840 {
6841 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6842 return FAIL;
6843 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844 else
6845 {
6846 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6847 return FAIL;
6848 }
6849 }
6850 else
6851 {
6852 emsg(_(e_indexable_type_required));
6853 return FAIL;
6854 }
6855
6856 return OK;
6857}
6858
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006859/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006860 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006861 * "let name"
6862 * "var name = expr"
6863 * "final name = expr"
6864 * "const name = expr"
6865 * "name = expr"
6866 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006867 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006868 * Return NULL for an error.
6869 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 */
6871 static char_u *
6872compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6873{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006876 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 char_u *ret = NULL;
6878 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006879 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006881 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006883 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 int oplen = 0;
6886 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006887 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006888 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006890 int is_decl = is_decl_command(cmdidx);
6891 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006892 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006894 // Skip over the "var" or "[var, var]" to get to any "=".
6895 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6896 if (p == NULL)
6897 return *arg == '[' ? arg : NULL;
6898
6899 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006901 // TODO: should we allow this, and figure out type inference from list
6902 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006903 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 return NULL;
6905 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006906 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 sp = p;
6909 p = skipwhite(p);
6910 op = p;
6911 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006912
6913 if (var_count > 0 && oplen == 0)
6914 // can be something like "[1, 2]->func()"
6915 return arg;
6916
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006917 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006919 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006920 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006921 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006922 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6923 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006924 if (VIM_ISWHITE(eap->cmd[2]))
6925 {
6926 semsg(_(e_no_white_space_allowed_after_str_str),
6927 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6928 return NULL;
6929 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006930 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6931 oplen = 2;
6932 incdec = TRUE;
6933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 if (heredoc)
6936 {
6937 list_T *l;
6938 listitem_T *li;
6939
6940 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006941 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006943 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006944 if (l == NULL)
6945 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946
Bram Moolenaar078269b2020-09-21 20:35:55 +02006947 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006949 // Push each line and the create the list.
6950 FOR_ALL_LIST_ITEMS(l, li)
6951 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006952 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006953 li->li_tv.vval.v_string = NULL;
6954 }
6955 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 list_free(l);
6958 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006959 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006961 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006963 char_u *wp;
6964
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006965 // for "[var, var] = expr" evaluate the expression here, loop over the
6966 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006967 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006968
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006969 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006970 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006971 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006972 if (compile_expr0(&p, cctx) == FAIL)
6973 return NULL;
6974 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006976 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006978 type_T *stacktype;
6979
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006980 stacktype = stack->ga_len == 0 ? &t_void
6981 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006982 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006984 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006985 goto theend;
6986 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006987 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006988 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006990 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006991 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6992 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006993 if (stacktype->tt_member != NULL)
6994 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995 }
6996 }
6997
6998 /*
6999 * Loop over variables in "[var, var] = expr".
7000 * For "var = expr" and "let var: type" this is done only once.
7001 */
7002 if (var_count > 0)
7003 var_start = skipwhite(arg + 1); // skip over the "["
7004 else
7005 var_start = arg;
7006 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7007 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007008 int instr_count = -1;
7009 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007010
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007011 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7012 {
7013 // Ignore underscore in "[a, _, b] = list".
7014 if (var_count > 0)
7015 {
7016 var_start = skipwhite(var_start + 2);
7017 continue;
7018 }
7019 emsg(_(e_cannot_use_underscore_here));
7020 goto theend;
7021 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007022 vim_free(lhs.lhs_name);
7023
7024 /*
7025 * Figure out the LHS type and other properties.
7026 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007027 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7028 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007029 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007030 if (heredoc)
7031 {
7032 SOURCING_LNUM = start_lnum;
7033 if (lhs.lhs_has_type
7034 && need_type(&t_list_string, lhs.lhs_type,
7035 -1, 0, cctx, FALSE, FALSE) == FAIL)
7036 goto theend;
7037 }
7038 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007039 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007040 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007041 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007042 if (oplen > 0 && var_count == 0)
7043 {
7044 // skip over the "=" and the expression
7045 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007046 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007047 }
7048 }
7049 else if (oplen > 0)
7050 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007051 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007052 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007053
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007054 // for "+=", "*=", "..=" etc. first load the current value
7055 if (*op != '='
7056 && compile_load_lhs_with_index(&lhs, var_start,
7057 cctx) == FAIL)
7058 goto theend;
7059
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007060 // For "var = expr" evaluate the expression.
7061 if (var_count == 0)
7062 {
7063 int r;
7064
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007065 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007066 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007067 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007068 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007069 r = generate_PUSHNR(cctx, 1);
7070 }
7071 else
7072 {
7073 // Temporarily hide the new local variable here, it is
7074 // not available to this expression.
7075 if (lhs.lhs_new_local)
7076 --cctx->ctx_locals.ga_len;
7077 wp = op + oplen;
7078 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7079 {
7080 if (lhs.lhs_new_local)
7081 ++cctx->ctx_locals.ga_len;
7082 goto theend;
7083 }
7084 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007085 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007086 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007087 if (r == FAIL)
7088 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007089 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007090 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007091 else if (semicolon && var_idx == var_count - 1)
7092 {
7093 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007094 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007095 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7096 goto theend;
7097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007098 else
7099 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007100 // For "[var, var] = expr" get the "var_idx" item from the
7101 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007102 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007103 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007104 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007105
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007106 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007107 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007108 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007109 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007110 if ((rhs_type->tt_type == VAR_FUNC
7111 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007112 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007113 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007114 goto theend;
7115
Bram Moolenaar752fc692021-01-04 21:57:11 +01007116 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007117 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007118 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007120 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007121 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007122 }
7123 else
7124 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007125 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007126 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007127 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007128 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007129 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007131 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007132 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007133 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007134 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007135 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007136 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007137 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007138 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007139 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007140 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007141
Bram Moolenaar77709b12021-04-03 21:01:01 +02007142 // Without operator check type here, otherwise below.
7143 // Use the line number of the assignment.
7144 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007145 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7146 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007147 // If assigning to a list or dict member, use the
7148 // member type. Not for "list[:] =".
7149 if (lhs.lhs_has_index
7150 && !has_list_index(var_start + lhs.lhs_varlen,
7151 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007152 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007153 if (need_type_where(rhs_type, use_type, -1, where,
7154 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007155 goto theend;
7156 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007157 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007158 else
7159 {
7160 type_T *lhs_type = lhs.lhs_member_type;
7161
7162 // Special case: assigning to @# can use a number or a
7163 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007164 // Also: can assign a number to a float.
7165 if ((lhs_type == &t_number_or_string
7166 || lhs_type == &t_float)
7167 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007168 lhs_type = &t_number;
7169 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007170 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007171 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007174 else if (cmdidx == CMD_final)
7175 {
7176 emsg(_(e_final_requires_a_value));
7177 goto theend;
7178 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007179 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007181 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007182 goto theend;
7183 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007184 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007185 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007186 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007187 goto theend;
7188 }
7189 else
7190 {
7191 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007192 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007193 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007194 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007195 {
7196 case VAR_BOOL:
7197 generate_PUSHBOOL(cctx, VVAL_FALSE);
7198 break;
7199 case VAR_FLOAT:
7200#ifdef FEAT_FLOAT
7201 generate_PUSHF(cctx, 0.0);
7202#endif
7203 break;
7204 case VAR_STRING:
7205 generate_PUSHS(cctx, NULL);
7206 break;
7207 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007208 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007209 break;
7210 case VAR_FUNC:
7211 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7212 break;
7213 case VAR_LIST:
7214 generate_NEWLIST(cctx, 0);
7215 break;
7216 case VAR_DICT:
7217 generate_NEWDICT(cctx, 0);
7218 break;
7219 case VAR_JOB:
7220 generate_PUSHJOB(cctx, NULL);
7221 break;
7222 case VAR_CHANNEL:
7223 generate_PUSHCHANNEL(cctx, NULL);
7224 break;
7225 case VAR_NUMBER:
7226 case VAR_UNKNOWN:
7227 case VAR_ANY:
7228 case VAR_PARTIAL:
7229 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007230 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007231 case VAR_SPECIAL: // cannot happen
7232 generate_PUSHNR(cctx, 0);
7233 break;
7234 }
7235 }
7236 if (var_count == 0)
7237 end = p;
7238 }
7239
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007240 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007241 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007242 break;
7243
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007244 if (oplen > 0 && *op != '=')
7245 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007246 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007247 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007248
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007249 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007250 {
7251 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7252 goto theend;
7253 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007254 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007255 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007256 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007257 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7258 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007259#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007260 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007261 !(expected == &t_float && (stacktype == &t_number
7262 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007263#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007264 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007265 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007266 goto theend;
7267 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007268
7269 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007270 {
7271 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7272 goto theend;
7273 }
7274 else if (*op == '+')
7275 {
7276 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007277 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007278 lhs.lhs_member_type, stacktype,
7279 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007280 goto theend;
7281 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007282 else if (generate_two_op(cctx, op) == FAIL)
7283 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007286 // Use the line number of the assignment for store instruction.
7287 save_lnum = cctx->ctx_lnum;
7288 cctx->ctx_lnum = start_lnum - 1;
7289
Bram Moolenaar752fc692021-01-04 21:57:11 +01007290 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007291 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007292 // Use the info in "lhs" to store the value at the index in the
7293 // list or dict.
7294 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7295 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007296 {
7297 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007298 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007299 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007300 }
7301 else
7302 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007303 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007304 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007305 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007306 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007307 generate_LOCKCONST(cctx);
7308
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007309 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007310 && (lhs.lhs_type->tt_type == VAR_DICT
7311 || lhs.lhs_type->tt_type == VAR_LIST)
7312 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007313 && !(lhs.lhs_type->tt_member == &t_any
7314 && oplen > 0
7315 && rhs_type != NULL
7316 && rhs_type->tt_type == lhs.lhs_type->tt_type
7317 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007318 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007319 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007320 // also in legacy script. Not for "list<any> = val", then the
7321 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007322 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007323
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007324 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007325 {
7326 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007327 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007328 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007329 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007330 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007331
7332 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007333 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007335
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007336 // For "[var, var] = expr" drop the "expr" value.
7337 // Also for "[var, var; _] = expr".
7338 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007339 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007340 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007341 goto theend;
7342 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007343
Bram Moolenaarb2097502020-07-19 17:17:02 +02007344 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345
7346theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007347 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 return ret;
7349}
7350
7351/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007352 * Check for an assignment at "eap->cmd", compile it if found.
7353 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7354 */
7355 static int
7356may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7357{
7358 char_u *pskip;
7359 char_u *p;
7360
7361 // Assuming the command starts with a variable or function name,
7362 // find what follows.
7363 // Skip over "var.member", "var[idx]" and the like.
7364 // Also "&opt = val", "$ENV = val" and "@r = val".
7365 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7366 ? eap->cmd + 1 : eap->cmd;
7367 p = to_name_end(pskip, TRUE);
7368 if (p > eap->cmd && *p != NUL)
7369 {
7370 char_u *var_end;
7371 int oplen;
7372 int heredoc;
7373
7374 if (eap->cmd[0] == '@')
7375 var_end = eap->cmd + 2;
7376 else
7377 var_end = find_name_end(pskip, NULL, NULL,
7378 FNE_CHECK_START | FNE_INCL_BR);
7379 oplen = assignment_len(skipwhite(var_end), &heredoc);
7380 if (oplen > 0)
7381 {
7382 size_t len = p - eap->cmd;
7383
7384 // Recognize an assignment if we recognize the variable
7385 // name:
7386 // "g:var = expr"
7387 // "local = expr" where "local" is a local var.
7388 // "script = expr" where "script" is a script-local var.
7389 // "import = expr" where "import" is an imported var
7390 // "&opt = expr"
7391 // "$ENV = expr"
7392 // "@r = expr"
7393 if (*eap->cmd == '&'
7394 || *eap->cmd == '$'
7395 || *eap->cmd == '@'
7396 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007397 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007398 {
7399 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7400 if (*line == NULL || *line == eap->cmd)
7401 return FAIL;
7402 return OK;
7403 }
7404 }
7405 }
7406
7407 if (*eap->cmd == '[')
7408 {
7409 // [var, var] = expr
7410 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7411 if (*line == NULL)
7412 return FAIL;
7413 if (*line != eap->cmd)
7414 return OK;
7415 }
7416 return NOTDONE;
7417}
7418
7419/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007420 * Check if "name" can be "unlet".
7421 */
7422 int
7423check_vim9_unlet(char_u *name)
7424{
7425 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7426 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007427 // "unlet s:var" is allowed in legacy script.
7428 if (*name == 's' && !script_is_vim9())
7429 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007430 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007431 return FAIL;
7432 }
7433 return OK;
7434}
7435
7436/*
7437 * Callback passed to ex_unletlock().
7438 */
7439 static int
7440compile_unlet(
7441 lval_T *lvp,
7442 char_u *name_end,
7443 exarg_T *eap,
7444 int deep UNUSED,
7445 void *coookie)
7446{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007447 cctx_T *cctx = coookie;
7448 char_u *p = lvp->ll_name;
7449 int cc = *name_end;
7450 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007451
Bram Moolenaar752fc692021-01-04 21:57:11 +01007452 if (cctx->ctx_skip == SKIP_YES)
7453 return OK;
7454
7455 *name_end = NUL;
7456 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007457 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007458 // :unlet $ENV_VAR
7459 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7460 }
7461 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7462 {
7463 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007464
Bram Moolenaar752fc692021-01-04 21:57:11 +01007465 // This is similar to assigning: lookup the list/dict, compile the
7466 // idx/key. Then instead of storing the value unlet the item.
7467 // unlet {list}[idx]
7468 // unlet {dict}[key] dict.key
7469 //
7470 // Figure out the LHS type and other properties.
7471 //
7472 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7473
7474 // : unlet an indexed item
7475 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007476 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007477 iemsg("called compile_lhs() without an index");
7478 ret = FAIL;
7479 }
7480 else
7481 {
7482 // Use the info in "lhs" to unlet the item at the index in the
7483 // list or dict.
7484 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007485 }
7486
Bram Moolenaar752fc692021-01-04 21:57:11 +01007487 vim_free(lhs.lhs_name);
7488 }
7489 else if (check_vim9_unlet(p) == FAIL)
7490 {
7491 ret = FAIL;
7492 }
7493 else
7494 {
7495 // Normal name. Only supports g:, w:, t: and b: namespaces.
7496 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007497 }
7498
Bram Moolenaar752fc692021-01-04 21:57:11 +01007499 *name_end = cc;
7500 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007501}
7502
7503/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007504 * Callback passed to ex_unletlock().
7505 */
7506 static int
7507compile_lock_unlock(
7508 lval_T *lvp,
7509 char_u *name_end,
7510 exarg_T *eap,
7511 int deep UNUSED,
7512 void *coookie)
7513{
7514 cctx_T *cctx = coookie;
7515 int cc = *name_end;
7516 char_u *p = lvp->ll_name;
7517 int ret = OK;
7518 size_t len;
7519 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007520 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007521
7522 if (cctx->ctx_skip == SKIP_YES)
7523 return OK;
7524
7525 // Cannot use :lockvar and :unlockvar on local variables.
7526 if (p[1] != ':')
7527 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007528 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007529
7530 if (lookup_local(p, end - p, NULL, cctx) == OK)
7531 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007532 char_u *s = p;
7533
7534 if (*end != '.' && *end != '[')
7535 {
7536 emsg(_(e_cannot_lock_unlock_local_variable));
7537 return FAIL;
7538 }
7539
7540 // For "d.member" put the local variable on the stack, it will be
7541 // passed to ex_lockvar() indirectly.
7542 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7543 return FAIL;
7544 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007545 }
7546 }
7547
7548 // Checking is done at runtime.
7549 *name_end = NUL;
7550 len = name_end - p + 20;
7551 buf = alloc(len);
7552 if (buf == NULL)
7553 ret = FAIL;
7554 else
7555 {
7556 vim_snprintf((char *)buf, len, "%s %s",
7557 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7558 p);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007559 ret = generate_EXEC(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007560
7561 vim_free(buf);
7562 *name_end = cc;
7563 }
7564 return ret;
7565}
7566
7567/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007568 * compile "unlet var", "lock var" and "unlock var"
7569 * "arg" points to "var".
7570 */
7571 static char_u *
7572compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7573{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007574 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7575 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7576 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007577 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7578}
7579
7580/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7582 */
7583 static int
7584compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7585{
7586 garray_T *instr = &cctx->ctx_instr;
7587 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7588
7589 if (endlabel == NULL)
7590 return FAIL;
7591 endlabel->el_next = *el;
7592 *el = endlabel;
7593 endlabel->el_end_label = instr->ga_len;
7594
7595 generate_JUMP(cctx, when, 0);
7596 return OK;
7597}
7598
7599 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007600compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601{
7602 garray_T *instr = &cctx->ctx_instr;
7603
7604 while (*el != NULL)
7605 {
7606 endlabel_T *cur = (*el);
7607 isn_T *isn;
7608
7609 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007610 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 *el = cur->el_next;
7612 vim_free(cur);
7613 }
7614}
7615
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007616 static void
7617compile_free_jump_to_end(endlabel_T **el)
7618{
7619 while (*el != NULL)
7620 {
7621 endlabel_T *cur = (*el);
7622
7623 *el = cur->el_next;
7624 vim_free(cur);
7625 }
7626}
7627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628/*
7629 * Create a new scope and set up the generic items.
7630 */
7631 static scope_T *
7632new_scope(cctx_T *cctx, scopetype_T type)
7633{
7634 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7635
7636 if (scope == NULL)
7637 return NULL;
7638 scope->se_outer = cctx->ctx_scope;
7639 cctx->ctx_scope = scope;
7640 scope->se_type = type;
7641 scope->se_local_count = cctx->ctx_locals.ga_len;
7642 return scope;
7643}
7644
7645/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007646 * Free the current scope and go back to the outer scope.
7647 */
7648 static void
7649drop_scope(cctx_T *cctx)
7650{
7651 scope_T *scope = cctx->ctx_scope;
7652
7653 if (scope == NULL)
7654 {
7655 iemsg("calling drop_scope() without a scope");
7656 return;
7657 }
7658 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007659 switch (scope->se_type)
7660 {
7661 case IF_SCOPE:
7662 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7663 case FOR_SCOPE:
7664 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7665 case WHILE_SCOPE:
7666 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7667 case TRY_SCOPE:
7668 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7669 case NO_SCOPE:
7670 case BLOCK_SCOPE:
7671 break;
7672 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007673 vim_free(scope);
7674}
7675
7676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 * compile "if expr"
7678 *
7679 * "if expr" Produces instructions:
7680 * EVAL expr Push result of "expr"
7681 * JUMP_IF_FALSE end
7682 * ... body ...
7683 * end:
7684 *
7685 * "if expr | else" Produces instructions:
7686 * EVAL expr Push result of "expr"
7687 * JUMP_IF_FALSE else
7688 * ... body ...
7689 * JUMP_ALWAYS end
7690 * else:
7691 * ... body ...
7692 * end:
7693 *
7694 * "if expr1 | elseif expr2 | else" Produces instructions:
7695 * EVAL expr Push result of "expr"
7696 * JUMP_IF_FALSE elseif
7697 * ... body ...
7698 * JUMP_ALWAYS end
7699 * elseif:
7700 * EVAL expr Push result of "expr"
7701 * JUMP_IF_FALSE else
7702 * ... body ...
7703 * JUMP_ALWAYS end
7704 * else:
7705 * ... body ...
7706 * end:
7707 */
7708 static char_u *
7709compile_if(char_u *arg, cctx_T *cctx)
7710{
7711 char_u *p = arg;
7712 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007713 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007715 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007716 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717
Bram Moolenaara5565e42020-05-09 15:44:01 +02007718 CLEAR_FIELD(ppconst);
7719 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007720 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007721 clear_ppconst(&ppconst);
7722 return NULL;
7723 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007724 if (!ends_excmd2(arg, skipwhite(p)))
7725 {
7726 semsg(_(e_trailing_arg), p);
7727 return NULL;
7728 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007729 if (cctx->ctx_skip == SKIP_YES)
7730 clear_ppconst(&ppconst);
7731 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007732 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007733 int error = FALSE;
7734 int v;
7735
Bram Moolenaara5565e42020-05-09 15:44:01 +02007736 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007737 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007738 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007739 if (error)
7740 return NULL;
7741 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007742 }
7743 else
7744 {
7745 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007746 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007747 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007748 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007749 if (bool_on_stack(cctx) == FAIL)
7750 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752
Bram Moolenaara91a7132021-03-25 21:12:15 +01007753 // CMDMOD_REV must come before the jump
7754 generate_undo_cmdmods(cctx);
7755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 scope = new_scope(cctx, IF_SCOPE);
7757 if (scope == NULL)
7758 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007759 scope->se_skip_save = skip_save;
7760 // "is_had_return" will be reset if any block does not end in :return
7761 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007763 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007764 {
7765 // "where" is set when ":elseif", "else" or ":endif" is found
7766 scope->se_u.se_if.is_if_label = instr->ga_len;
7767 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7768 }
7769 else
7770 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771
Bram Moolenaarced68a02021-01-24 17:53:47 +01007772#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007773 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007774 && skip_save != SKIP_YES)
7775 {
7776 // generated a profile start, need to generate a profile end, since it
7777 // won't be done after returning
7778 cctx->ctx_skip = SKIP_NOT;
7779 generate_instr(cctx, ISN_PROF_END);
7780 cctx->ctx_skip = SKIP_YES;
7781 }
7782#endif
7783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 return p;
7785}
7786
7787 static char_u *
7788compile_elseif(char_u *arg, cctx_T *cctx)
7789{
7790 char_u *p = arg;
7791 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007792 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 isn_T *isn;
7794 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007795 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007796 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797
7798 if (scope == NULL || scope->se_type != IF_SCOPE)
7799 {
7800 emsg(_(e_elseif_without_if));
7801 return NULL;
7802 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007803 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007804 if (!cctx->ctx_had_return)
7805 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806
Bram Moolenaarced68a02021-01-24 17:53:47 +01007807 if (cctx->ctx_skip == SKIP_NOT)
7808 {
7809 // previous block was executed, this one and following will not
7810 cctx->ctx_skip = SKIP_YES;
7811 scope->se_u.se_if.is_seen_skip_not = TRUE;
7812 }
7813 if (scope->se_u.se_if.is_seen_skip_not)
7814 {
7815 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007816 // Do not count the "elseif" for profiling and cmdmod
7817 instr->ga_len = current_instr_idx(cctx);
7818
Bram Moolenaarced68a02021-01-24 17:53:47 +01007819 skip_expr_cctx(&p, cctx);
7820 return p;
7821 }
7822
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007823 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007824 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007825 int moved_cmdmod = FALSE;
7826 int saved_debug = FALSE;
7827 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007828
7829 // Move any CMDMOD instruction to after the jump
7830 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7831 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007832 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007833 return NULL;
7834 ((isn_T *)instr->ga_data)[instr->ga_len] =
7835 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7836 --instr->ga_len;
7837 moved_cmdmod = TRUE;
7838 }
7839
Bram Moolenaar093165c2021-08-22 13:35:31 +02007840 // Remove the already generated ISN_DEBUG, it is written below the
7841 // ISN_FOR instruction.
7842 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7843 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7844 .isn_type == ISN_DEBUG)
7845 {
7846 --instr->ga_len;
7847 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7848 saved_debug = TRUE;
7849 }
7850
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007851 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007853 return NULL;
7854 // previous "if" or "elseif" jumps here
7855 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7856 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007857
Bram Moolenaara91a7132021-03-25 21:12:15 +01007858 if (moved_cmdmod)
7859 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007860
7861 if (saved_debug)
7862 {
7863 // move the debug instruction here
7864 if (GA_GROW_FAILS(instr, 1))
7865 return NULL;
7866 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7867 ++instr->ga_len;
7868 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007871 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007872 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007873 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007874 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007875 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007876#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007877 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007878 {
7879 // the previous block was skipped, need to profile this line
7880 generate_instr(cctx, ISN_PROF_START);
7881 instr_count = instr->ga_len;
7882 }
7883#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007884 if (cctx->ctx_compile_type == CT_DEBUG)
7885 {
7886 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007887 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007888 instr_count = instr->ga_len;
7889 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007890 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007891 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007892 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007893 clear_ppconst(&ppconst);
7894 return NULL;
7895 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007896 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007897 if (!ends_excmd2(arg, skipwhite(p)))
7898 {
7899 semsg(_(e_trailing_arg), p);
7900 return NULL;
7901 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007902 if (scope->se_skip_save == SKIP_YES)
7903 clear_ppconst(&ppconst);
7904 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007905 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007906 int error = FALSE;
7907 int v;
7908
Bram Moolenaar7f141552020-05-09 17:35:53 +02007909 // The expression results in a constant.
7910 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007911 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7912 if (error)
7913 return NULL;
7914 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007915 clear_ppconst(&ppconst);
7916 scope->se_u.se_if.is_if_label = -1;
7917 }
7918 else
7919 {
7920 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007921 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007922 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007923 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007924 if (bool_on_stack(cctx) == FAIL)
7925 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
Bram Moolenaara91a7132021-03-25 21:12:15 +01007927 // CMDMOD_REV must come before the jump
7928 generate_undo_cmdmods(cctx);
7929
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007930 // "where" is set when ":elseif", "else" or ":endif" is found
7931 scope->se_u.se_if.is_if_label = instr->ga_len;
7932 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934
7935 return p;
7936}
7937
7938 static char_u *
7939compile_else(char_u *arg, cctx_T *cctx)
7940{
7941 char_u *p = arg;
7942 garray_T *instr = &cctx->ctx_instr;
7943 isn_T *isn;
7944 scope_T *scope = cctx->ctx_scope;
7945
7946 if (scope == NULL || scope->se_type != IF_SCOPE)
7947 {
7948 emsg(_(e_else_without_if));
7949 return NULL;
7950 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007951 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007952 if (!cctx->ctx_had_return)
7953 scope->se_u.se_if.is_had_return = FALSE;
7954 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955
Bram Moolenaarced68a02021-01-24 17:53:47 +01007956#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007957 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007958 {
7959 if (cctx->ctx_skip == SKIP_NOT
7960 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7961 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007962 // the previous block was executed, do not count "else" for
7963 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007964 --instr->ga_len;
7965 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7966 {
7967 // the previous block was not executed, this one will, do count the
7968 // "else" for profiling
7969 cctx->ctx_skip = SKIP_NOT;
7970 generate_instr(cctx, ISN_PROF_END);
7971 generate_instr(cctx, ISN_PROF_START);
7972 cctx->ctx_skip = SKIP_YES;
7973 }
7974 }
7975#endif
7976
7977 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007978 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007979 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007980 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007981 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007982 if (!cctx->ctx_had_return
7983 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7984 JUMP_ALWAYS, cctx) == FAIL)
7985 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007986 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007987
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007988 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007989 {
7990 if (scope->se_u.se_if.is_if_label >= 0)
7991 {
7992 // previous "if" or "elseif" jumps here
7993 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7994 isn->isn_arg.jump.jump_where = instr->ga_len;
7995 scope->se_u.se_if.is_if_label = -1;
7996 }
7997 }
7998
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007999 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008000 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002
8003 return p;
8004}
8005
8006 static char_u *
8007compile_endif(char_u *arg, cctx_T *cctx)
8008{
8009 scope_T *scope = cctx->ctx_scope;
8010 ifscope_T *ifscope;
8011 garray_T *instr = &cctx->ctx_instr;
8012 isn_T *isn;
8013
Bram Moolenaarfa984412021-03-25 22:15:28 +01008014 if (misplaced_cmdmod(cctx))
8015 return NULL;
8016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017 if (scope == NULL || scope->se_type != IF_SCOPE)
8018 {
8019 emsg(_(e_endif_without_if));
8020 return NULL;
8021 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008022 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008023 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008024 if (!cctx->ctx_had_return)
8025 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008027 if (scope->se_u.se_if.is_if_label >= 0)
8028 {
8029 // previous "if" or "elseif" jumps here
8030 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8031 isn->isn_arg.jump.jump_where = instr->ga_len;
8032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008034 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008035
8036#ifdef FEAT_PROFILE
8037 // even when skipping we count the endif as executed, unless the block it's
8038 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008039 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008040 && scope->se_skip_save != SKIP_YES)
8041 {
8042 cctx->ctx_skip = SKIP_NOT;
8043 generate_instr(cctx, ISN_PROF_START);
8044 }
8045#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008046 cctx->ctx_skip = scope->se_skip_save;
8047
8048 // If all the blocks end in :return and there is an :else then the
8049 // had_return flag is set.
8050 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008052 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008053 return arg;
8054}
8055
8056/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008057 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 *
8059 * Produces instructions:
8060 * PUSHNR -1
8061 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008062 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8064 * - if beyond end, jump to "end"
8065 * - otherwise get item from list and push it
8066 * STORE var Store item in "var"
8067 * ... body ...
8068 * JUMP top Jump back to repeat
8069 * end: DROP Drop the result of "expr"
8070 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008071 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8072 * UNPACK 2 Split item in 2
8073 * STORE var1 Store item in "var1"
8074 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 */
8076 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008077compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008078{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008079 char_u *arg;
8080 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008081 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008083 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008084 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008085 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008086 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008089 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008091 lvar_T *loop_lvar; // loop iteration variable
8092 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008094 type_T *item_type = &t_any;
8095 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008096 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097
Bram Moolenaar792f7862020-11-23 08:31:18 +01008098 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008099 if (p == NULL)
8100 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008101 if (var_count == 0)
8102 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008103 else
8104 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008105
8106 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008107 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008108 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8109 return NULL;
8110 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008112 if (*p == ':' && wp != p)
8113 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8114 else
8115 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 return NULL;
8117 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008118 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008119 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8120 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008122 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8123 // instruction.
8124 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8125 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8126 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008127 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008128 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008129 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8130 .isn_arg.debug.dbg_break_lnum;
8131 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 scope = new_scope(cctx, FOR_SCOPE);
8134 if (scope == NULL)
8135 return NULL;
8136
Bram Moolenaar792f7862020-11-23 08:31:18 +01008137 // Reserve a variable to store the loop iteration counter and initialize it
8138 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008139 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8140 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008141 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008142 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008143 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008145 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008146 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147
8148 // compile "expr", it remains on the stack until "endfor"
8149 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008150 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008151 {
8152 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008154 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008155 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156
rbtnnbebf0692021-08-21 17:26:50 +02008157 if (cctx->ctx_skip != SKIP_YES)
8158 {
8159 // If we know the type of "var" and it is a not a supported type we can
8160 // give an error now.
8161 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8162 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008163 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008164 {
rbtnnbebf0692021-08-21 17:26:50 +02008165 semsg(_(e_for_loop_on_str_not_supported),
8166 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008167 drop_scope(cctx);
8168 return NULL;
8169 }
rbtnnbebf0692021-08-21 17:26:50 +02008170
8171 if (vartype->tt_type == VAR_STRING)
8172 item_type = &t_string;
8173 else if (vartype->tt_type == VAR_BLOB)
8174 item_type = &t_number;
8175 else if (vartype->tt_type == VAR_LIST
8176 && vartype->tt_member->tt_type != VAR_ANY)
8177 {
8178 if (!var_list)
8179 item_type = vartype->tt_member;
8180 else if (vartype->tt_member->tt_type == VAR_LIST
8181 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
8182 // TODO: should get the type for each lhs
8183 item_type = vartype->tt_member->tt_member;
8184 }
8185
8186 // CMDMOD_REV must come before the FOR instruction.
8187 generate_undo_cmdmods(cctx);
8188
8189 // "for_end" is set when ":endfor" is found
8190 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8191
8192 generate_FOR(cctx, loop_lvar->lv_idx);
8193
8194 arg = arg_start;
8195 if (var_list)
8196 {
8197 generate_UNPACK(cctx, var_count, semicolon);
8198 arg = skipwhite(arg + 1); // skip white after '['
8199
8200 // the list item is replaced by a number of items
8201 if (GA_GROW_FAILS(stack, var_count - 1))
8202 {
8203 drop_scope(cctx);
8204 return NULL;
8205 }
8206 --stack->ga_len;
8207 for (idx = 0; idx < var_count; ++idx)
8208 {
8209 ((type_T **)stack->ga_data)[stack->ga_len] =
8210 (semicolon && idx == 0) ? vartype : item_type;
8211 ++stack->ga_len;
8212 }
8213 }
8214
Bram Moolenaar792f7862020-11-23 08:31:18 +01008215 for (idx = 0; idx < var_count; ++idx)
8216 {
rbtnnbebf0692021-08-21 17:26:50 +02008217 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008218 int opt_flags = 0;
8219 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008220 type_T *type = &t_any;
8221 type_T *lhs_type = &t_any;
8222 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008223
rbtnnbebf0692021-08-21 17:26:50 +02008224 p = skip_var_one(arg, FALSE);
8225 varlen = p - arg;
8226 name = vim_strnsave(arg, varlen);
8227 if (name == NULL)
8228 goto failed;
8229 if (*p == ':')
8230 {
8231 p = skipwhite(p + 1);
8232 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8233 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008234
rbtnnbebf0692021-08-21 17:26:50 +02008235 // TODO: script var not supported?
8236 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008237 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008238 goto failed;
8239 if (dest != dest_local)
8240 {
8241 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008242 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008243 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008244 }
rbtnnbebf0692021-08-21 17:26:50 +02008245 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008246 {
rbtnnbebf0692021-08-21 17:26:50 +02008247 // Assigning to "_": drop the value.
8248 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8249 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008250 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008251 else
rbtnnbebf0692021-08-21 17:26:50 +02008252 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008253 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008254 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008255 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8256 {
8257 semsg(_(e_variable_already_declared), arg);
8258 goto failed;
8259 }
8260
8261 if (STRNCMP(name, "s:", 2) == 0)
8262 {
8263 semsg(_(e_cannot_declare_script_variable_in_function), name);
8264 goto failed;
8265 }
8266
8267 // Reserve a variable to store "var".
8268 where.wt_index = var_list ? idx + 1 : 0;
8269 where.wt_variable = TRUE;
8270 if (lhs_type == &t_any)
8271 lhs_type = item_type;
8272 else if (item_type != &t_unknown
8273 && (item_type == &t_any
8274 ? need_type(item_type, lhs_type,
8275 -1, 0, cctx, FALSE, FALSE)
8276 : check_type(lhs_type, item_type, TRUE, where))
8277 == FAIL)
8278 goto failed;
8279 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8280 if (var_lvar == NULL)
8281 // out of memory or used as an argument
8282 goto failed;
8283
8284 if (semicolon && idx == var_count - 1)
8285 var_lvar->lv_type = vartype;
8286 else
8287 var_lvar->lv_type = item_type;
8288 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8289 }
8290
8291 if (*p == ',' || *p == ';')
8292 ++p;
8293 arg = skipwhite(p);
8294 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008295 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008296
rbtnnbebf0692021-08-21 17:26:50 +02008297 if (cctx->ctx_compile_type == CT_DEBUG)
8298 {
8299 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008300
rbtnnbebf0692021-08-21 17:26:50 +02008301 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8302 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8303 cctx->ctx_prev_lnum = prev_lnum;
8304 generate_instr_debug(cctx);
8305 cctx->ctx_prev_lnum = save_prev_lnum;
8306 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008307 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008308
Bram Moolenaar792f7862020-11-23 08:31:18 +01008309 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008310
8311failed:
8312 vim_free(name);
8313 drop_scope(cctx);
8314 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008315}
8316
8317/*
8318 * compile "endfor"
8319 */
8320 static char_u *
8321compile_endfor(char_u *arg, cctx_T *cctx)
8322{
8323 garray_T *instr = &cctx->ctx_instr;
8324 scope_T *scope = cctx->ctx_scope;
8325 forscope_T *forscope;
8326 isn_T *isn;
8327
Bram Moolenaarfa984412021-03-25 22:15:28 +01008328 if (misplaced_cmdmod(cctx))
8329 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 if (scope == NULL || scope->se_type != FOR_SCOPE)
8332 {
8333 emsg(_(e_for));
8334 return NULL;
8335 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008336 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008338 if (cctx->ctx_skip != SKIP_YES)
8339 {
8340 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008341
rbtnnbebf0692021-08-21 17:26:50 +02008342 // At end of ":for" scope jump back to the FOR instruction.
8343 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344
rbtnnbebf0692021-08-21 17:26:50 +02008345 // Fill in the "end" label in the FOR statement so it can jump here.
8346 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8347 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008348
rbtnnbebf0692021-08-21 17:26:50 +02008349 // Fill in the "end" label any BREAK statements
8350 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351
rbtnnbebf0692021-08-21 17:26:50 +02008352 // Below the ":for" scope drop the "expr" list from the stack.
8353 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8354 return NULL;
8355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356
8357 vim_free(scope);
8358
8359 return arg;
8360}
8361
8362/*
8363 * compile "while expr"
8364 *
8365 * Produces instructions:
8366 * top: EVAL expr Push result of "expr"
8367 * JUMP_IF_FALSE end jump if false
8368 * ... body ...
8369 * JUMP top Jump back to repeat
8370 * end:
8371 *
8372 */
8373 static char_u *
8374compile_while(char_u *arg, cctx_T *cctx)
8375{
8376 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008377 scope_T *scope;
8378
8379 scope = new_scope(cctx, WHILE_SCOPE);
8380 if (scope == NULL)
8381 return NULL;
8382
Bram Moolenaara91a7132021-03-25 21:12:15 +01008383 // "endwhile" jumps back here, one before when profiling or using cmdmods
8384 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385
8386 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008387 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008388 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008389
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008390 if (!ends_excmd2(arg, skipwhite(p)))
8391 {
8392 semsg(_(e_trailing_arg), p);
8393 return NULL;
8394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395
rbtnnd895b1d2021-08-20 20:54:25 +02008396 if (cctx->ctx_skip != SKIP_YES)
8397 {
8398 if (bool_on_stack(cctx) == FAIL)
8399 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008400
rbtnnd895b1d2021-08-20 20:54:25 +02008401 // CMDMOD_REV must come before the jump
8402 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008403
rbtnnd895b1d2021-08-20 20:54:25 +02008404 // "while_end" is set when ":endwhile" is found
8405 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008407 return FAIL;
8408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409
8410 return p;
8411}
8412
8413/*
8414 * compile "endwhile"
8415 */
8416 static char_u *
8417compile_endwhile(char_u *arg, cctx_T *cctx)
8418{
8419 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008420 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421
Bram Moolenaarfa984412021-03-25 22:15:28 +01008422 if (misplaced_cmdmod(cctx))
8423 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8425 {
8426 emsg(_(e_while));
8427 return NULL;
8428 }
8429 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008430 if (cctx->ctx_skip != SKIP_YES)
8431 {
8432 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433
Bram Moolenaarf002a412021-01-24 13:34:18 +01008434#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008435 // count the endwhile before jumping
8436 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008437#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008438
rbtnnd895b1d2021-08-20 20:54:25 +02008439 // At end of ":for" scope jump back to the FOR instruction.
8440 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441
rbtnnd895b1d2021-08-20 20:54:25 +02008442 // Fill in the "end" label in the WHILE statement so it can jump here.
8443 // And in any jumps for ":break"
8444 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008445 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447
8448 vim_free(scope);
8449
8450 return arg;
8451}
8452
8453/*
8454 * compile "continue"
8455 */
8456 static char_u *
8457compile_continue(char_u *arg, cctx_T *cctx)
8458{
8459 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008460 int try_scopes = 0;
8461 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462
8463 for (;;)
8464 {
8465 if (scope == NULL)
8466 {
8467 emsg(_(e_continue));
8468 return NULL;
8469 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008470 if (scope->se_type == FOR_SCOPE)
8471 {
8472 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008474 }
8475 if (scope->se_type == WHILE_SCOPE)
8476 {
8477 loop_label = scope->se_u.se_while.ws_top_label;
8478 break;
8479 }
8480 if (scope->se_type == TRY_SCOPE)
8481 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 scope = scope->se_outer;
8483 }
8484
Bram Moolenaarc150c092021-02-13 15:02:46 +01008485 if (try_scopes > 0)
8486 // Inside one or more try/catch blocks we first need to jump to the
8487 // "finally" or "endtry" to cleanup.
8488 generate_TRYCONT(cctx, try_scopes, loop_label);
8489 else
8490 // Jump back to the FOR or WHILE instruction.
8491 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493 return arg;
8494}
8495
8496/*
8497 * compile "break"
8498 */
8499 static char_u *
8500compile_break(char_u *arg, cctx_T *cctx)
8501{
8502 scope_T *scope = cctx->ctx_scope;
8503 endlabel_T **el;
8504
8505 for (;;)
8506 {
8507 if (scope == NULL)
8508 {
8509 emsg(_(e_break));
8510 return NULL;
8511 }
8512 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8513 break;
8514 scope = scope->se_outer;
8515 }
8516
8517 // Jump to the end of the FOR or WHILE loop.
8518 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008519 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008521 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8523 return FAIL;
8524
8525 return arg;
8526}
8527
8528/*
8529 * compile "{" start of block
8530 */
8531 static char_u *
8532compile_block(char_u *arg, cctx_T *cctx)
8533{
8534 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8535 return NULL;
8536 return skipwhite(arg + 1);
8537}
8538
8539/*
8540 * compile end of block: drop one scope
8541 */
8542 static void
8543compile_endblock(cctx_T *cctx)
8544{
8545 scope_T *scope = cctx->ctx_scope;
8546
8547 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008548 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008549 vim_free(scope);
8550}
8551
8552/*
8553 * compile "try"
8554 * Creates a new scope for the try-endtry, pointing to the first catch and
8555 * finally.
8556 * Creates another scope for the "try" block itself.
8557 * TRY instruction sets up exception handling at runtime.
8558 *
8559 * "try"
8560 * TRY -> catch1, -> finally push trystack entry
8561 * ... try block
8562 * "throw {exception}"
8563 * EVAL {exception}
8564 * THROW create exception
8565 * ... try block
8566 * " catch {expr}"
8567 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008568 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 * EVAL {expr}
8570 * MATCH
8571 * JUMP nomatch -> catch2
8572 * CATCH remove exception
8573 * ... catch block
8574 * " catch"
8575 * JUMP -> finally
8576 * catch2: CATCH remove exception
8577 * ... catch block
8578 * " finally"
8579 * finally:
8580 * ... finally block
8581 * " endtry"
8582 * ENDTRY pop trystack entry, may rethrow
8583 */
8584 static char_u *
8585compile_try(char_u *arg, cctx_T *cctx)
8586{
8587 garray_T *instr = &cctx->ctx_instr;
8588 scope_T *try_scope;
8589 scope_T *scope;
8590
Bram Moolenaarfa984412021-03-25 22:15:28 +01008591 if (misplaced_cmdmod(cctx))
8592 return NULL;
8593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 // scope that holds the jumps that go to catch/finally/endtry
8595 try_scope = new_scope(cctx, TRY_SCOPE);
8596 if (try_scope == NULL)
8597 return NULL;
8598
Bram Moolenaar69f70502021-01-01 16:10:46 +01008599 if (cctx->ctx_skip != SKIP_YES)
8600 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008601 isn_T *isn;
8602
8603 // "try_catch" is set when the first ":catch" is found or when no catch
8604 // is found and ":finally" is found.
8605 // "try_finally" is set when ":finally" is found
8606 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008607 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008608 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8609 return NULL;
8610 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8611 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008612 return NULL;
8613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614
8615 // scope for the try block itself
8616 scope = new_scope(cctx, BLOCK_SCOPE);
8617 if (scope == NULL)
8618 return NULL;
8619
8620 return arg;
8621}
8622
8623/*
8624 * compile "catch {expr}"
8625 */
8626 static char_u *
8627compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8628{
8629 scope_T *scope = cctx->ctx_scope;
8630 garray_T *instr = &cctx->ctx_instr;
8631 char_u *p;
8632 isn_T *isn;
8633
Bram Moolenaarfa984412021-03-25 22:15:28 +01008634 if (misplaced_cmdmod(cctx))
8635 return NULL;
8636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637 // end block scope from :try or :catch
8638 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8639 compile_endblock(cctx);
8640 scope = cctx->ctx_scope;
8641
8642 // Error if not in a :try scope
8643 if (scope == NULL || scope->se_type != TRY_SCOPE)
8644 {
8645 emsg(_(e_catch));
8646 return NULL;
8647 }
8648
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008649 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008651 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008652 return NULL;
8653 }
8654
Bram Moolenaar69f70502021-01-01 16:10:46 +01008655 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008657#ifdef FEAT_PROFILE
8658 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008659 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008660 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008661 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008662 .isn_type == ISN_PROF_START)
8663 --instr->ga_len;
8664#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008665 // Jump from end of previous block to :finally or :endtry
8666 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8667 JUMP_ALWAYS, cctx) == FAIL)
8668 return NULL;
8669
8670 // End :try or :catch scope: set value in ISN_TRY instruction
8671 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008672 if (isn->isn_arg.try.try_ref->try_catch == 0)
8673 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008674 if (scope->se_u.se_try.ts_catch_label != 0)
8675 {
8676 // Previous catch without match jumps here
8677 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8678 isn->isn_arg.jump.jump_where = instr->ga_len;
8679 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008680#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008681 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008682 {
8683 // a "throw" that jumps here needs to be counted
8684 generate_instr(cctx, ISN_PROF_END);
8685 // the "catch" is also counted
8686 generate_instr(cctx, ISN_PROF_START);
8687 }
8688#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008689 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008690 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691 }
8692
8693 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008694 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008695 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008696 scope->se_u.se_try.ts_caught_all = TRUE;
8697 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008698 }
8699 else
8700 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008701 char_u *end;
8702 char_u *pat;
8703 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008704 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008705 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008707 // Push v:exception, push {expr} and MATCH
8708 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8709
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008710 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008711 if (*end != *p)
8712 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008713 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008714 vim_free(tofree);
8715 return FAIL;
8716 }
8717 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008718 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008719 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008720 len = (int)(end - tofree);
8721 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008722 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008723 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008724 if (pat == NULL)
8725 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008726 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008727 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8730 return NULL;
8731
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008732 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008733 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8734 return NULL;
8735 }
8736
Bram Moolenaar69f70502021-01-01 16:10:46 +01008737 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 return NULL;
8739
8740 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8741 return NULL;
8742 return p;
8743}
8744
8745 static char_u *
8746compile_finally(char_u *arg, cctx_T *cctx)
8747{
8748 scope_T *scope = cctx->ctx_scope;
8749 garray_T *instr = &cctx->ctx_instr;
8750 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008751 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008752
Bram Moolenaarfa984412021-03-25 22:15:28 +01008753 if (misplaced_cmdmod(cctx))
8754 return NULL;
8755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008756 // end block scope from :try or :catch
8757 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8758 compile_endblock(cctx);
8759 scope = cctx->ctx_scope;
8760
8761 // Error if not in a :try scope
8762 if (scope == NULL || scope->se_type != TRY_SCOPE)
8763 {
8764 emsg(_(e_finally));
8765 return NULL;
8766 }
8767
rbtnn84934992021-08-07 13:26:53 +02008768 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 {
rbtnn84934992021-08-07 13:26:53 +02008770 // End :catch or :finally scope: set value in ISN_TRY instruction
8771 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8772 if (isn->isn_arg.try.try_ref->try_finally != 0)
8773 {
8774 emsg(_(e_finally_dup));
8775 return NULL;
8776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777
rbtnn84934992021-08-07 13:26:53 +02008778 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008779#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008780 if (cctx->ctx_compile_type == CT_PROFILE
8781 && ((isn_T *)instr->ga_data)[this_instr - 1]
8782 .isn_type == ISN_PROF_START)
8783 {
8784 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008785 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008786
8787 // jump to the profile end above it
8788 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8789 .isn_type == ISN_PROF_END)
8790 --this_instr;
8791 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008792#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793
rbtnn84934992021-08-07 13:26:53 +02008794 // Fill in the "end" label in jumps at the end of the blocks.
8795 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8796 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008797
rbtnn84934992021-08-07 13:26:53 +02008798 // If there is no :catch then an exception jumps to :finally.
8799 if (isn->isn_arg.try.try_ref->try_catch == 0)
8800 isn->isn_arg.try.try_ref->try_catch = this_instr;
8801 isn->isn_arg.try.try_ref->try_finally = this_instr;
8802 if (scope->se_u.se_try.ts_catch_label != 0)
8803 {
8804 // Previous catch without match jumps here
8805 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8806 isn->isn_arg.jump.jump_where = this_instr;
8807 scope->se_u.se_try.ts_catch_label = 0;
8808 }
8809 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8810 return NULL;
8811
8812 // TODO: set index in ts_finally_label jumps
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008814
8815 return arg;
8816}
8817
8818 static char_u *
8819compile_endtry(char_u *arg, cctx_T *cctx)
8820{
8821 scope_T *scope = cctx->ctx_scope;
8822 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008823 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824
Bram Moolenaarfa984412021-03-25 22:15:28 +01008825 if (misplaced_cmdmod(cctx))
8826 return NULL;
8827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828 // end block scope from :catch or :finally
8829 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8830 compile_endblock(cctx);
8831 scope = cctx->ctx_scope;
8832
8833 // Error if not in a :try scope
8834 if (scope == NULL || scope->se_type != TRY_SCOPE)
8835 {
8836 if (scope == NULL)
8837 emsg(_(e_no_endtry));
8838 else if (scope->se_type == WHILE_SCOPE)
8839 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008840 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 emsg(_(e_endfor));
8842 else
8843 emsg(_(e_endif));
8844 return NULL;
8845 }
8846
Bram Moolenaarc150c092021-02-13 15:02:46 +01008847 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008848 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008849 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008850 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8851 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008852 {
8853 emsg(_(e_missing_catch_or_finally));
8854 return NULL;
8855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008857#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008858 if (cctx->ctx_compile_type == CT_PROFILE
8859 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008860 .isn_type == ISN_PROF_START)
8861 // move the profile start after "endtry" so that it's not counted when
8862 // the exception is rethrown.
8863 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008864#endif
8865
Bram Moolenaar69f70502021-01-01 16:10:46 +01008866 // Fill in the "end" label in jumps at the end of the blocks, if not
8867 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008868 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8869 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870
Bram Moolenaar69f70502021-01-01 16:10:46 +01008871 if (scope->se_u.se_try.ts_catch_label != 0)
8872 {
8873 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008874 isn_T *isn = ((isn_T *)instr->ga_data)
8875 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008876 isn->isn_arg.jump.jump_where = instr->ga_len;
8877 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008878 }
8879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008880 compile_endblock(cctx);
8881
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008882 if (cctx->ctx_skip != SKIP_YES)
8883 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008884 // End :catch or :finally scope: set instruction index in ISN_TRY
8885 // instruction
8886 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008887 if (cctx->ctx_skip != SKIP_YES
8888 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8889 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008890#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008891 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008892 generate_instr(cctx, ISN_PROF_START);
8893#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008895 return arg;
8896}
8897
8898/*
8899 * compile "throw {expr}"
8900 */
8901 static char_u *
8902compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8903{
8904 char_u *p = skipwhite(arg);
8905
Bram Moolenaara5565e42020-05-09 15:44:01 +02008906 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008907 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008908 if (cctx->ctx_skip == SKIP_YES)
8909 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008910 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911 return NULL;
8912 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8913 return NULL;
8914
8915 return p;
8916}
8917
Bram Moolenaarc3235272021-07-10 19:42:03 +02008918 static char_u *
8919compile_eval(char_u *arg, cctx_T *cctx)
8920{
8921 char_u *p = arg;
8922 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008923 long lnum = SOURCING_LNUM;
8924
8925 // find_ex_command() will consider a variable name an expression, assuming
8926 // that something follows on the next line. Check that something actually
8927 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008928 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008929
Bram Moolenaarc3235272021-07-10 19:42:03 +02008930 if (compile_expr0(&p, cctx) == FAIL)
8931 return NULL;
8932
8933 if (name_only && lnum == SOURCING_LNUM)
8934 {
8935 semsg(_(e_expression_without_effect_str), arg);
8936 return NULL;
8937 }
8938
8939 // drop the result
8940 generate_instr_drop(cctx, ISN_DROP, 1);
8941
8942 return skipwhite(p);
8943}
8944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945/*
8946 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008947 * compile "echomsg expr"
8948 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008949 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008950 * compile "execute expr"
8951 */
8952 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008953compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008954{
8955 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008956 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008957 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008958 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008959 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008960 garray_T *stack = &cctx->ctx_type_stack;
8961 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008962
8963 for (;;)
8964 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008965 if (ends_excmd2(prev, p))
8966 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008967 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008968 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008969 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008970
8971 if (cctx->ctx_skip != SKIP_YES)
8972 {
8973 // check for non-void type
8974 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8975 if (type->tt_type == VAR_VOID)
8976 {
8977 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8978 return NULL;
8979 }
8980 }
8981
Bram Moolenaarad39c092020-02-26 18:23:43 +01008982 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008983 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008984 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008985 }
8986
Bram Moolenaare4984292020-12-13 14:19:25 +01008987 if (count > 0)
8988 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008989 long save_lnum = cctx->ctx_lnum;
8990
8991 // Use the line number where the command started.
8992 cctx->ctx_lnum = start_ctx_lnum;
8993
Bram Moolenaare4984292020-12-13 14:19:25 +01008994 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8995 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8996 else if (cmdidx == CMD_execute)
8997 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8998 else if (cmdidx == CMD_echomsg)
8999 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009000 else if (cmdidx == CMD_echoconsole)
9001 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009002 else
9003 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009004
9005 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009007 return p;
9008}
9009
9010/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009011 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009012 * instruction to compute it and return OK.
9013 * Otherwise return FAIL, the caller must deal with any range.
9014 */
9015 static int
9016compile_variable_range(exarg_T *eap, cctx_T *cctx)
9017{
9018 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9019 char_u *p = skipdigits(eap->cmd);
9020
9021 if (p == range_end)
9022 return FAIL;
9023 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9024}
9025
9026/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009027 * :put r
9028 * :put ={expr}
9029 */
9030 static char_u *
9031compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9032{
9033 char_u *line = arg;
9034 linenr_T lnum;
9035 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009036 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009037
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009038 eap->regname = *line;
9039
9040 if (eap->regname == '=')
9041 {
9042 char_u *p = line + 1;
9043
9044 if (compile_expr0(&p, cctx) == FAIL)
9045 return NULL;
9046 line = p;
9047 }
9048 else if (eap->regname != NUL)
9049 ++line;
9050
Bram Moolenaar08597872020-12-10 19:43:40 +01009051 if (compile_variable_range(eap, cctx) == OK)
9052 {
9053 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9054 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009055 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009056 {
9057 // Either no range or a number.
9058 // "errormsg" will not be set because the range is ADDR_LINES.
9059 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009060 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009061 return NULL;
9062 if (eap->addr_count == 0)
9063 lnum = -1;
9064 else
9065 lnum = eap->line2;
9066 if (above)
9067 --lnum;
9068 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009069
9070 generate_PUT(cctx, eap->regname, lnum);
9071 return line;
9072}
9073
9074/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009075 * A command that is not compiled, execute with legacy code.
9076 */
9077 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009078compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009079{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009080 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009081 char_u *p;
9082 int has_expr = FALSE;
9083 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009084 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009085
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009086 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009087 goto theend;
9088
Bram Moolenaare729ce22021-06-06 21:38:09 +02009089 // If there was a prececing command modifier, drop it and include it in the
9090 // EXEC command.
9091 if (cctx->ctx_has_cmdmod)
9092 {
9093 garray_T *instr = &cctx->ctx_instr;
9094 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9095
9096 if (isn->isn_type == ISN_CMDMOD)
9097 {
9098 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9099 ->cmod_filter_regmatch.regprog);
9100 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9101 --instr->ga_len;
9102 cctx->ctx_has_cmdmod = FALSE;
9103 }
9104 }
9105
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009106 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009107 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009108 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009109 int usefilter = FALSE;
9110
9111 has_expr = argt & (EX_XFILE | EX_EXPAND);
9112
9113 // If the command can be followed by a bar, find the bar and truncate
9114 // it, so that the following command can be compiled.
9115 // The '|' is overwritten with a NUL, it is put back below.
9116 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9117 && *eap->arg == '!')
9118 // :w !filter or :r !filter or :r! filter
9119 usefilter = TRUE;
9120 if ((argt & EX_TRLBAR) && !usefilter)
9121 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009122 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009123 separate_nextcmd(eap);
9124 if (eap->nextcmd != NULL)
9125 nextcmd = eap->nextcmd;
9126 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009127 else if (eap->cmdidx == CMD_wincmd)
9128 {
9129 p = eap->arg;
9130 if (*p != NUL)
9131 ++p;
9132 if (*p == 'g' || *p == Ctrl_G)
9133 ++p;
9134 p = skipwhite(p);
9135 if (*p == '|')
9136 {
9137 *p = NUL;
9138 nextcmd = p + 1;
9139 }
9140 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009141 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9142 {
9143 // If there is a trailing '{' read lines until the '}'
9144 p = eap->arg + STRLEN(eap->arg) - 1;
9145 while (p > eap->arg && VIM_ISWHITE(*p))
9146 --p;
9147 if (*p == '{')
9148 {
9149 exarg_T ea;
9150 int flags; // unused
9151 int start_lnum = SOURCING_LNUM;
9152
9153 CLEAR_FIELD(ea);
9154 ea.arg = eap->arg;
9155 fill_exarg_from_cctx(&ea, cctx);
9156 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9157 if (tofree != NULL)
9158 {
9159 *p = NUL;
9160 line = concat_str(line, tofree);
9161 if (line == NULL)
9162 goto theend;
9163 vim_free(tofree);
9164 tofree = line;
9165 SOURCING_LNUM = start_lnum;
9166 }
9167 }
9168 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009169 }
9170
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009171 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9172 {
9173 // expand filename in "syntax include [@group] filename"
9174 has_expr = TRUE;
9175 eap->arg = skipwhite(eap->arg + 7);
9176 if (*eap->arg == '@')
9177 eap->arg = skiptowhite(eap->arg);
9178 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009179
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009180 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9181 && STRLEN(eap->arg) > 4)
9182 {
9183 int delim = *eap->arg;
9184
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009185 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009186 if (*p == delim)
9187 {
9188 eap->arg = p + 1;
9189 has_expr = TRUE;
9190 }
9191 }
9192
Bram Moolenaarecac5912021-01-05 19:23:28 +01009193 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
9194 {
9195 // TODO: should only expand when appropriate for the command
9196 eap->arg = skiptowhite(eap->arg);
9197 has_expr = TRUE;
9198 }
9199
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009200 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009201 {
9202 int count = 0;
9203 char_u *start = skipwhite(line);
9204
9205 // :cmd xxx`=expr1`yyy`=expr2`zzz
9206 // PUSHS ":cmd xxx"
9207 // eval expr1
9208 // PUSHS "yyy"
9209 // eval expr2
9210 // PUSHS "zzz"
9211 // EXECCONCAT 5
9212 for (;;)
9213 {
9214 if (p > start)
9215 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009216 char_u *val = vim_strnsave(start, p - start);
9217
9218 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009219 ++count;
9220 }
9221 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009222 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009223 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009224 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009225 ++count;
9226 p = skipwhite(p);
9227 if (*p != '`')
9228 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009229 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009230 return NULL;
9231 }
9232 start = p + 1;
9233
9234 p = (char_u *)strstr((char *)start, "`=");
9235 if (p == NULL)
9236 {
9237 if (*skipwhite(start) != NUL)
9238 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009239 char_u *val = vim_strsave(start);
9240
9241 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009242 ++count;
9243 }
9244 break;
9245 }
9246 }
9247 generate_EXECCONCAT(cctx, count);
9248 }
9249 else
Bram Moolenaaraacc9662021-08-13 19:40:51 +02009250 generate_EXEC(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009251
9252theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009253 if (*nextcmd != NUL)
9254 {
9255 // the parser expects a pointer to the bar, put it back
9256 --nextcmd;
9257 *nextcmd = '|';
9258 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009259 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009260
9261 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009262}
9263
Bram Moolenaar20677332021-06-06 17:02:53 +02009264/*
9265 * A script command with heredoc, e.g.
9266 * ruby << EOF
9267 * command
9268 * EOF
9269 * Has been turned into one long line with NL characters by
9270 * get_function_body():
9271 * ruby << EOF<NL> command<NL>EOF
9272 */
9273 static char_u *
9274compile_script(char_u *line, cctx_T *cctx)
9275{
9276 if (cctx->ctx_skip != SKIP_YES)
9277 {
9278 isn_T *isn;
9279
9280 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9281 return NULL;
9282 isn->isn_arg.string = vim_strsave(line);
9283 }
9284 return (char_u *)"";
9285}
9286
Bram Moolenaar8238f082021-04-20 21:10:48 +02009287
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009288/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009289 * :s/pat/repl/
9290 */
9291 static char_u *
9292compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9293{
9294 char_u *cmd = eap->arg;
9295 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9296
9297 if (expr != NULL)
9298 {
9299 int delimiter = *cmd++;
9300
9301 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009302 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009303 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9304 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009305 garray_T save_ga = cctx->ctx_instr;
9306 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009307 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009308 int trailing_error;
9309 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009310 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009311 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009312
9313 cmd += 3;
9314 end = skip_substitute(cmd, delimiter);
9315
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009316 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009317 // labels are correct.
9318 cctx->ctx_instr.ga_len = 0;
9319 cctx->ctx_instr.ga_maxlen = 0;
9320 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009321 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009322 if (end[-1] == NUL)
9323 end[-1] = delimiter;
9324 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009325 trailing_error = *cmd != delimiter && *cmd != NUL;
9326
Bram Moolenaar169502f2021-04-21 12:19:35 +02009327 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009328 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009329 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009330 if (trailing_error)
9331 semsg(_(e_trailing_arg), cmd);
9332 clear_instr_ga(&cctx->ctx_instr);
9333 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009334 return NULL;
9335 }
9336
Bram Moolenaar8238f082021-04-20 21:10:48 +02009337 // Move the generated instructions into the ISN_SUBSTITUTE
9338 // instructions, then restore the list of instructions before
9339 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009340 instr_count = cctx->ctx_instr.ga_len;
9341 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009342 instr[instr_count].isn_type = ISN_FINISH;
9343
9344 cctx->ctx_instr = save_ga;
9345 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9346 {
9347 int idx;
9348
9349 for (idx = 0; idx < instr_count; ++idx)
9350 delete_instr(instr + idx);
9351 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009352 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009353 }
9354 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9355 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009356
9357 // skip over flags
9358 if (*end == '&')
9359 ++end;
9360 while (ASCII_ISALPHA(*end) || *end == '#')
9361 ++end;
9362 return end;
9363 }
9364 }
9365
9366 return compile_exec(arg, eap, cctx);
9367}
9368
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009369 static char_u *
9370compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9371{
9372 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009373 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009374
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009375 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009376 {
9377 if (STRNCMP(arg, "END", 3) == 0)
9378 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009379 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009380 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009381 // First load the current variable value.
9382 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9383 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009384 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009385 }
9386
9387 // Gets the redirected text and put it on the stack, then store it
9388 // in the variable.
9389 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9390
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009391 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009392 generate_instr_drop(cctx, ISN_CONCAT, 1);
9393
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009394 if (lhs->lhs_has_index)
9395 {
9396 // Use the info in "lhs" to store the value at the index in the
9397 // list or dict.
9398 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9399 &t_string, cctx) == FAIL)
9400 return NULL;
9401 }
9402 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009403 return NULL;
9404
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009405 VIM_CLEAR(lhs->lhs_name);
9406 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009407 return arg + 3;
9408 }
9409 emsg(_(e_cannot_nest_redir));
9410 return NULL;
9411 }
9412
9413 if (arg[0] == '=' && arg[1] == '>')
9414 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009415 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009416
9417 // redirect to a variable is compiled
9418 arg += 2;
9419 if (*arg == '>')
9420 {
9421 ++arg;
9422 append = TRUE;
9423 }
9424 arg = skipwhite(arg);
9425
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009426 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009427 FALSE, FALSE, 1, cctx) == FAIL)
9428 return NULL;
9429 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009430 lhs->lhs_append = append;
9431 if (lhs->lhs_has_index)
9432 {
9433 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9434 if (lhs->lhs_whole == NULL)
9435 return NULL;
9436 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009437
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009438 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009439 }
9440
9441 // other redirects are handled like at script level
9442 return compile_exec(line, eap, cctx);
9443}
9444
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009445#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009446 static char_u *
9447compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9448{
9449 isn_T *isn;
9450 char_u *p;
9451
9452 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9453 if (isn == NULL)
9454 return NULL;
9455 isn->isn_arg.number = eap->cmdidx;
9456
9457 p = eap->arg;
9458 if (compile_expr0(&p, cctx) == FAIL)
9459 return NULL;
9460
9461 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9462 if (isn == NULL)
9463 return NULL;
9464 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9465 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9466 return NULL;
9467 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9468 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9469 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9470
9471 return p;
9472}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009473#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009474
Bram Moolenaar4c137212021-04-19 16:48:48 +02009475/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009476 * Check if the separator for a :global or :substitute command is OK.
9477 */
9478 int
9479check_global_and_subst(char_u *cmd, char_u *arg)
9480{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009481 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009482 {
9483 semsg(_(e_separator_not_supported_str), arg);
9484 return FAIL;
9485 }
9486 if (VIM_ISWHITE(cmd[1]))
9487 {
9488 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9489 return FAIL;
9490 }
9491 return OK;
9492}
9493
9494
9495/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009496 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009497 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009498 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009499 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009500add_def_function(ufunc_T *ufunc)
9501{
9502 dfunc_T *dfunc;
9503
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009504 if (def_functions.ga_len == 0)
9505 {
9506 // The first position is not used, so that a zero uf_dfunc_idx means it
9507 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009508 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009509 return FAIL;
9510 ++def_functions.ga_len;
9511 }
9512
Bram Moolenaar09689a02020-05-09 22:50:08 +02009513 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009514 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009515 return FAIL;
9516 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9517 CLEAR_POINTER(dfunc);
9518 dfunc->df_idx = def_functions.ga_len;
9519 ufunc->uf_dfunc_idx = dfunc->df_idx;
9520 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009521 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009522 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009523 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009524 ++def_functions.ga_len;
9525 return OK;
9526}
9527
9528/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009529 * After ex_function() has collected all the function lines: parse and compile
9530 * the lines into instructions.
9531 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009532 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9533 * the return statement (used for lambda). When uf_ret_type is already set
9534 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009535 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009536 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009537 * This can be used recursively through compile_lambda(), which may reallocate
9538 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009539 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009540 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009541 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009542compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009543 ufunc_T *ufunc,
9544 int check_return_type,
9545 compiletype_T compile_type,
9546 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009547{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009548 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009549 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009550 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009552 cctx_T cctx;
9553 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009554 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009555 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009556 int ret = FAIL;
9557 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009558 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009559 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009560 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009561 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009562#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009563 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009564#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009565 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009566
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009567 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009568 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009569 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009570 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009571 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9572 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009573 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009574
9575 switch (compile_type)
9576 {
9577 case CT_PROFILE:
9578#ifdef FEAT_PROFILE
9579 instr_dest = dfunc->df_instr_prof; break;
9580#endif
9581 case CT_NONE: instr_dest = dfunc->df_instr; break;
9582 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9583 }
9584 if (instr_dest != NULL)
9585 // Was compiled in this mode before: Free old instructions.
9586 delete_def_function_contents(dfunc, FALSE);
9587 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009588 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009589 else
9590 {
9591 if (add_def_function(ufunc) == FAIL)
9592 return FAIL;
9593 new_def_function = TRUE;
9594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595
Bram Moolenaar985116a2020-07-12 17:31:09 +02009596 ufunc->uf_def_status = UF_COMPILING;
9597
Bram Moolenaara80faa82020-04-12 19:37:17 +02009598 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009599
Bram Moolenaare99d4222021-06-13 14:01:26 +02009600 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601 cctx.ctx_ufunc = ufunc;
9602 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009603 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009604 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9605 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9606 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9607 cctx.ctx_type_list = &ufunc->uf_type_list;
9608 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9609 instr = &cctx.ctx_instr;
9610
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009611 // Set the context to the function, it may be compiled when called from
9612 // another script. Set the script version to the most modern one.
9613 // The line number will be set in next_line_from_context().
9614 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009615 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9616
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009617 // Don't use the flag from ":legacy" here.
9618 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9619
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009620 // Make sure error messages are OK.
9621 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9622 if (do_estack_push)
9623 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009624 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009625
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009626 if (ufunc->uf_def_args.ga_len > 0)
9627 {
9628 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009629 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009630 int i;
9631 char_u *arg;
9632 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009633 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009634
9635 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009636 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009637 for (i = 0; i < count; ++i)
9638 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009639 garray_T *stack = &cctx.ctx_type_stack;
9640 type_T *val_type;
9641 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009642 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009643 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009644 int jump_instr_idx = instr->ga_len;
9645 isn_T *isn;
9646
9647 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9648 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9649 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009650
9651 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009652 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009653
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009654 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009655 r = compile_expr0(&arg, &cctx);
9656
Bram Moolenaar12bce952021-03-11 20:04:04 +01009657 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009658 goto erret;
9659
9660 // If no type specified use the type of the default value.
9661 // Otherwise check that the default value type matches the
9662 // specified type.
9663 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009664 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009665 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009666 {
9667 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009668 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009669 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009670 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009671 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009672 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009673
9674 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009675 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009676
9677 // set instruction index in JUMP_IF_ARG_SET to here
9678 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9679 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009680 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009681
9682 if (did_set_arg_type)
9683 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009684 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009685 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009686
9687 /*
9688 * Loop over all the lines of the function and generate instructions.
9689 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 for (;;)
9691 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009692 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009693 int starts_with_colon = FALSE;
9694 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009695 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009696
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009697 // Bail out on the first error to avoid a flood of errors and report
9698 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009699 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009700 goto erret;
9701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009702 if (line != NULL && *line == '|')
9703 // the line continues after a '|'
9704 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009705 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009706 && !(*line == '#' && (line == cctx.ctx_line_start
9707 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009709 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009710 goto erret;
9711 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009712 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9713 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714 else
9715 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009716 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009717 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009718 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009719 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009720#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009721 if (cctx.ctx_skip != SKIP_YES)
9722 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009723#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009724 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009725 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009726 // Make a copy, splitting off nextcmd and removing trailing spaces
9727 // may change it.
9728 if (line != NULL)
9729 {
9730 line = vim_strsave(line);
9731 vim_free(line_to_free);
9732 line_to_free = line;
9733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009734 }
9735
Bram Moolenaara80faa82020-04-12 19:37:17 +02009736 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009737 ea.cmdlinep = &line;
9738 ea.cmd = skipwhite(line);
9739
Bram Moolenaarb2049902021-01-24 12:53:53 +01009740 if (*ea.cmd == '#')
9741 {
9742 // "#" starts a comment
9743 line = (char_u *)"";
9744 continue;
9745 }
9746
Bram Moolenaarf002a412021-01-24 13:34:18 +01009747#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009748 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9749 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009750 {
9751 may_generate_prof_end(&cctx, prof_lnum);
9752
9753 prof_lnum = cctx.ctx_lnum;
9754 generate_instr(&cctx, ISN_PROF_START);
9755 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009756#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009757 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9758 && cctx.ctx_skip != SKIP_YES)
9759 {
9760 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009761 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009762 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009763 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009764
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009765 // Some things can be recognized by the first character.
9766 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009767 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009768 case '}':
9769 {
9770 // "}" ends a block scope
9771 scopetype_T stype = cctx.ctx_scope == NULL
9772 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009773
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009774 if (stype == BLOCK_SCOPE)
9775 {
9776 compile_endblock(&cctx);
9777 line = ea.cmd;
9778 }
9779 else
9780 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009781 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009782 goto erret;
9783 }
9784 if (line != NULL)
9785 line = skipwhite(ea.cmd + 1);
9786 continue;
9787 }
9788
9789 case '{':
9790 // "{" starts a block scope
9791 // "{'a': 1}->func() is something else
9792 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9793 {
9794 line = compile_block(ea.cmd, &cctx);
9795 continue;
9796 }
9797 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009798 }
9799
9800 /*
9801 * COMMAND MODIFIERS
9802 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009803 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009804 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9805 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009806 {
9807 if (errormsg != NULL)
9808 goto erret;
9809 // empty line or comment
9810 line = (char_u *)"";
9811 continue;
9812 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009813 generate_cmdmods(&cctx, &local_cmdmod);
9814 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009815
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009816 // Check if there was a colon after the last command modifier or before
9817 // the current position.
9818 for (p = ea.cmd; p >= line; --p)
9819 {
9820 if (*p == ':')
9821 starts_with_colon = TRUE;
9822 if (p < ea.cmd && !VIM_ISWHITE(*p))
9823 break;
9824 }
9825
Bram Moolenaarce024c32021-06-26 13:00:49 +02009826 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009827 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009828 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009829 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009830 if (checkforcmd(&ea.cmd, "call", 3))
9831 {
9832 if (*ea.cmd == '(')
9833 // not for "call()"
9834 ea.cmd = p;
9835 else
9836 ea.cmd = skipwhite(ea.cmd);
9837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009838
Bram Moolenaarce024c32021-06-26 13:00:49 +02009839 if (!starts_with_colon)
9840 {
9841 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009842
Bram Moolenaarce024c32021-06-26 13:00:49 +02009843 // Check for assignment after command modifiers.
9844 assign = may_compile_assignment(&ea, &line, &cctx);
9845 if (assign == OK)
9846 goto nextline;
9847 if (assign == FAIL)
9848 goto erret;
9849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009850 }
9851
9852 /*
9853 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009854 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009855 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009856 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009857 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009858 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009859 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009860 && (*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009861 && (starts_with_colon || !(*cmd == '\''
9862 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009863 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009864 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009865 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009866 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009867 if (!starts_with_colon)
9868 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009869 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009870 goto erret;
9871 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009872 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009873 if (ends_excmd2(line, ea.cmd))
9874 {
9875 // A range without a command: jump to the line.
9876 // TODO: compile to a more efficient command, possibly
9877 // calling parse_cmd_address().
9878 ea.cmdidx = CMD_SIZE;
9879 line = compile_exec(line, &ea, &cctx);
9880 goto nextline;
9881 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009882 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009883 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009884 p = find_ex_command(&ea, NULL,
9885 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009886 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009888 if (p == NULL)
9889 {
9890 if (cctx.ctx_skip != SKIP_YES)
9891 emsg(_(e_ambiguous_use_of_user_defined_command));
9892 goto erret;
9893 }
9894
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009895 // When using ":legacy cmd" always use compile_exec().
9896 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009897 {
9898 char_u *start = ea.cmd;
9899
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009900 switch (ea.cmdidx)
9901 {
9902 case CMD_if:
9903 case CMD_elseif:
9904 case CMD_else:
9905 case CMD_endif:
9906 case CMD_for:
9907 case CMD_endfor:
9908 case CMD_continue:
9909 case CMD_break:
9910 case CMD_while:
9911 case CMD_endwhile:
9912 case CMD_try:
9913 case CMD_catch:
9914 case CMD_finally:
9915 case CMD_endtry:
9916 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9917 goto erret;
9918 default: break;
9919 }
9920
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009921 // ":legacy return expr" needs to be handled differently.
9922 if (checkforcmd(&start, "return", 4))
9923 ea.cmdidx = CMD_return;
9924 else
9925 ea.cmdidx = CMD_legacy;
9926 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009928 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9929 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009930 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009931 {
9932 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009933 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009934 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009935 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009936 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009937 // CMD_var cannot happen, compile_assignment() above would be
9938 // used. Most likely an assignment to a non-existing variable.
9939 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009940 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 }
9943
Bram Moolenaar3988f642020-08-27 22:43:03 +02009944 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009945 && ea.cmdidx != CMD_elseif
9946 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009947 && ea.cmdidx != CMD_endif
9948 && ea.cmdidx != CMD_endfor
9949 && ea.cmdidx != CMD_endwhile
9950 && ea.cmdidx != CMD_catch
9951 && ea.cmdidx != CMD_finally
9952 && ea.cmdidx != CMD_endtry)
9953 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009954 emsg(_(e_unreachable_code_after_return));
9955 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009956 }
9957
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009958 p = skipwhite(p);
9959 if (ea.cmdidx != CMD_SIZE
9960 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9961 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009962 if (ea.cmdidx >= 0)
9963 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009964 if ((ea.argt & EX_BANG) && *p == '!')
9965 {
9966 ea.forceit = TRUE;
9967 p = skipwhite(p + 1);
9968 }
9969 }
9970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971 switch (ea.cmdidx)
9972 {
9973 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +00009974 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009975 ea.arg = p;
9976 line = compile_nested_function(&ea, &cctx);
9977 break;
9978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009979 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009980 line = compile_return(p, check_return_type,
9981 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009982 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009983 break;
9984
9985 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009986 emsg(_(e_cannot_use_let_in_vim9_script));
9987 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009988 case CMD_var:
9989 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009990 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009991 case CMD_increment:
9992 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009993 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009994 if (line == p)
9995 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009996 break;
9997
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009998 case CMD_unlet:
9999 case CMD_unlockvar:
10000 case CMD_lockvar:
10001 line = compile_unletlock(p, &ea, &cctx);
10002 break;
10003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010004 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010005 emsg(_(e_import_can_only_be_used_in_script));
10006 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010007 break;
10008
10009 case CMD_if:
10010 line = compile_if(p, &cctx);
10011 break;
10012 case CMD_elseif:
10013 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010014 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010015 break;
10016 case CMD_else:
10017 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010018 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010019 break;
10020 case CMD_endif:
10021 line = compile_endif(p, &cctx);
10022 break;
10023
10024 case CMD_while:
10025 line = compile_while(p, &cctx);
10026 break;
10027 case CMD_endwhile:
10028 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010029 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010030 break;
10031
10032 case CMD_for:
10033 line = compile_for(p, &cctx);
10034 break;
10035 case CMD_endfor:
10036 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010037 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010038 break;
10039 case CMD_continue:
10040 line = compile_continue(p, &cctx);
10041 break;
10042 case CMD_break:
10043 line = compile_break(p, &cctx);
10044 break;
10045
10046 case CMD_try:
10047 line = compile_try(p, &cctx);
10048 break;
10049 case CMD_catch:
10050 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010051 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 break;
10053 case CMD_finally:
10054 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010055 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010056 break;
10057 case CMD_endtry:
10058 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010059 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010060 break;
10061 case CMD_throw:
10062 line = compile_throw(p, &cctx);
10063 break;
10064
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010065 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010066 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010067 break;
10068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010071 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010072 case CMD_echomsg:
10073 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010074 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010075 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010076 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010077
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010078 case CMD_put:
10079 ea.cmd = cmd;
10080 line = compile_put(p, &ea, &cctx);
10081 break;
10082
Bram Moolenaar4c137212021-04-19 16:48:48 +020010083 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010084 if (check_global_and_subst(ea.cmd, p) == FAIL)
10085 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010086 if (cctx.ctx_skip == SKIP_YES)
10087 line = (char_u *)"";
10088 else
10089 {
10090 ea.arg = p;
10091 line = compile_substitute(line, &ea, &cctx);
10092 }
10093 break;
10094
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010095 case CMD_redir:
10096 ea.arg = p;
10097 line = compile_redir(line, &ea, &cctx);
10098 break;
10099
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010100 case CMD_cexpr:
10101 case CMD_lexpr:
10102 case CMD_caddexpr:
10103 case CMD_laddexpr:
10104 case CMD_cgetexpr:
10105 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010106#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010107 ea.arg = p;
10108 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010109#else
10110 ex_ni(&ea);
10111 line = NULL;
10112#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010113 break;
10114
Bram Moolenaarae616492020-07-28 20:07:27 +020010115 case CMD_append:
10116 case CMD_change:
10117 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010118 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010119 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010120 case CMD_xit:
10121 not_in_vim9(&ea);
10122 goto erret;
10123
Bram Moolenaar002262f2020-07-08 17:47:57 +020010124 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010125 if (cctx.ctx_skip != SKIP_YES)
10126 {
10127 semsg(_(e_invalid_command_str), ea.cmd);
10128 goto erret;
10129 }
10130 // We don't check for a next command here.
10131 line = (char_u *)"";
10132 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010133
Bram Moolenaar20677332021-06-06 17:02:53 +020010134 case CMD_lua:
10135 case CMD_mzscheme:
10136 case CMD_perl:
10137 case CMD_py3:
10138 case CMD_python3:
10139 case CMD_python:
10140 case CMD_pythonx:
10141 case CMD_ruby:
10142 case CMD_tcl:
10143 ea.arg = p;
10144 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010145 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010146 else
10147 // heredoc lines have been concatenated with NL
10148 // characters in get_function_body()
10149 line = compile_script(line, &cctx);
10150 break;
10151
Bram Moolenaar7b829262021-10-13 15:04:34 +010010152 case CMD_global:
10153 if (check_global_and_subst(ea.cmd, p) == FAIL)
10154 goto erret;
10155 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010156 default:
10157 // Not recognized, execute with do_cmdline_cmd().
10158 ea.arg = p;
10159 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010160 break;
10161 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010162nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010163 if (line == NULL)
10164 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010165 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010166
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010167 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010168 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010170 if (cctx.ctx_type_stack.ga_len < 0)
10171 {
10172 iemsg("Type stack underflow");
10173 goto erret;
10174 }
10175 }
10176
10177 if (cctx.ctx_scope != NULL)
10178 {
10179 if (cctx.ctx_scope->se_type == IF_SCOPE)
10180 emsg(_(e_endif));
10181 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10182 emsg(_(e_endwhile));
10183 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10184 emsg(_(e_endfor));
10185 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010186 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010187 goto erret;
10188 }
10189
Bram Moolenaarefd88552020-06-18 20:50:10 +020010190 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010191 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010192 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10193 ufunc->uf_ret_type = &t_void;
10194 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010195 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010196 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010197 goto erret;
10198 }
10199
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010200 // Return void if there is no return at the end.
10201 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010202 }
10203
Bram Moolenaar599410c2021-04-10 14:03:43 +020010204 // When compiled with ":silent!" and there was an error don't consider the
10205 // function compiled.
10206 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010207 {
10208 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10209 + ufunc->uf_dfunc_idx;
10210 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010211 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010212#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010213 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010214 {
10215 dfunc->df_instr_prof = instr->ga_data;
10216 dfunc->df_instr_prof_count = instr->ga_len;
10217 }
10218 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010219#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010220 if (cctx.ctx_compile_type == CT_DEBUG)
10221 {
10222 dfunc->df_instr_debug = instr->ga_data;
10223 dfunc->df_instr_debug_count = instr->ga_len;
10224 }
10225 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010226 {
10227 dfunc->df_instr = instr->ga_data;
10228 dfunc->df_instr_count = instr->ga_len;
10229 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010230 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010231 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010232 if (cctx.ctx_outer_used)
10233 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010234 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236
10237 ret = OK;
10238
10239erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010240 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010242 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10243 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010244
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010245 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010246 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010247 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010248 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010249
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010250 // If using the last entry in the table and it was added above, we
10251 // might as well remove it.
10252 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010253 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010254 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010255 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010256 ufunc->uf_dfunc_idx = 0;
10257 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010258 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010259
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010260 while (cctx.ctx_scope != NULL)
10261 drop_scope(&cctx);
10262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010263 if (errormsg != NULL)
10264 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010265 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010266 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010267 }
10268
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010269 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10270 {
10271 if (ret == OK)
10272 {
10273 emsg(_(e_missing_redir_end));
10274 ret = FAIL;
10275 }
10276 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010277 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010278 }
10279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010280 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010281 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010282 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010283 if (do_estack_push)
10284 estack_pop();
10285
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010286 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010287 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010288 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010289 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010290 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010291}
10292
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010293 void
10294set_function_type(ufunc_T *ufunc)
10295{
10296 int varargs = ufunc->uf_va_name != NULL;
10297 int argcount = ufunc->uf_args.ga_len;
10298
10299 // Create a type for the function, with the return type and any
10300 // argument types.
10301 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10302 // The type is included in "tt_args".
10303 if (argcount > 0 || varargs)
10304 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010305 if (ufunc->uf_type_list.ga_itemsize == 0)
10306 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010307 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10308 argcount, &ufunc->uf_type_list);
10309 // Add argument types to the function type.
10310 if (func_type_add_arg_types(ufunc->uf_func_type,
10311 argcount + varargs,
10312 &ufunc->uf_type_list) == FAIL)
10313 return;
10314 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10315 ufunc->uf_func_type->tt_min_argcount =
10316 argcount - ufunc->uf_def_args.ga_len;
10317 if (ufunc->uf_arg_types == NULL)
10318 {
10319 int i;
10320
10321 // lambda does not have argument types.
10322 for (i = 0; i < argcount; ++i)
10323 ufunc->uf_func_type->tt_args[i] = &t_any;
10324 }
10325 else
10326 mch_memmove(ufunc->uf_func_type->tt_args,
10327 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10328 if (varargs)
10329 {
10330 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010331 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010332 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10333 }
10334 }
10335 else
10336 // No arguments, can use a predefined type.
10337 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10338 argcount, &ufunc->uf_type_list);
10339}
10340
10341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010342/*
10343 * Delete an instruction, free what it contains.
10344 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010345 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346delete_instr(isn_T *isn)
10347{
10348 switch (isn->isn_type)
10349 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010350 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010352 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010353 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010354 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010355 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010356 case ISN_LOADENV:
10357 case ISN_LOADG:
10358 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010359 case ISN_LOADT:
10360 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010361 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010362 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010363 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010364 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010365 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010366 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010367 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010368 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010369 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010370 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010371 case ISN_STOREW:
10372 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373 vim_free(isn->isn_arg.string);
10374 break;
10375
Bram Moolenaar4c137212021-04-19 16:48:48 +020010376 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010377 {
10378 int idx;
10379 isn_T *list = isn->isn_arg.subs.subs_instr;
10380
10381 vim_free(isn->isn_arg.subs.subs_cmd);
10382 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10383 delete_instr(list + idx);
10384 vim_free(list);
10385 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010386 break;
10387
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010388 case ISN_INSTR:
10389 {
10390 int idx;
10391 isn_T *list = isn->isn_arg.instr;
10392
10393 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10394 delete_instr(list + idx);
10395 vim_free(list);
10396 }
10397 break;
10398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010399 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010400 case ISN_STORES:
10401 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010402 break;
10403
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010404 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010405 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010406 vim_free(isn->isn_arg.unlet.ul_name);
10407 break;
10408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010409 case ISN_STOREOPT:
10410 vim_free(isn->isn_arg.storeopt.so_name);
10411 break;
10412
10413 case ISN_PUSHBLOB: // push blob isn_arg.blob
10414 blob_unref(isn->isn_arg.blob);
10415 break;
10416
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010417 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010418#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010419 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010420#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010421 break;
10422
10423 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010425 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010426#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010427 break;
10428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010429 case ISN_UCALL:
10430 vim_free(isn->isn_arg.ufunc.cuf_name);
10431 break;
10432
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010433 case ISN_FUNCREF:
10434 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010435 if (isn->isn_arg.funcref.fr_func_name == NULL)
10436 {
10437 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10438 + isn->isn_arg.funcref.fr_dfunc_idx;
10439 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010440
Bram Moolenaar38453522021-11-28 22:00:12 +000010441 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10442 func_ptr_unref(ufunc);
10443 }
10444 else
10445 {
10446 char_u *name = isn->isn_arg.funcref.fr_func_name;
10447
10448 if (name != NULL)
10449 func_unref(name);
10450 vim_free(isn->isn_arg.funcref.fr_func_name);
10451 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010452 }
10453 break;
10454
10455 case ISN_DCALL:
10456 {
10457 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10458 + isn->isn_arg.dfunc.cdf_idx;
10459
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010460 if (dfunc->df_ufunc != NULL
10461 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010462 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010463 }
10464 break;
10465
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010466 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010467 {
10468 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10469 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10470
10471 if (ufunc != NULL)
10472 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010473 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010474 func_ptr_unref(ufunc);
10475 }
10476
10477 vim_free(lambda);
10478 vim_free(isn->isn_arg.newfunc.nf_global);
10479 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010480 break;
10481
Bram Moolenaar5e654232020-09-16 15:22:00 +020010482 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010483 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010484 free_type(isn->isn_arg.type.ct_type);
10485 break;
10486
Bram Moolenaar02194d22020-10-24 23:08:38 +020010487 case ISN_CMDMOD:
10488 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10489 ->cmod_filter_regmatch.regprog);
10490 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10491 break;
10492
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010493 case ISN_LOADSCRIPT:
10494 case ISN_STORESCRIPT:
10495 vim_free(isn->isn_arg.script.scriptref);
10496 break;
10497
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010498 case ISN_TRY:
10499 vim_free(isn->isn_arg.try.try_ref);
10500 break;
10501
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010502 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010503 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010504 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10505 break;
10506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010507 case ISN_2BOOL:
10508 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010509 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010510 case ISN_ADDBLOB:
10511 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010512 case ISN_ANYINDEX:
10513 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010514 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010515 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010516 case ISN_BLOBINDEX:
10517 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010518 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010519 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010520 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010521 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010522 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010523 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010524 case ISN_COMPAREANY:
10525 case ISN_COMPAREBLOB:
10526 case ISN_COMPAREBOOL:
10527 case ISN_COMPAREDICT:
10528 case ISN_COMPAREFLOAT:
10529 case ISN_COMPAREFUNC:
10530 case ISN_COMPARELIST:
10531 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010532 case ISN_COMPARESPECIAL:
10533 case ISN_COMPARESTRING:
10534 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010535 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010536 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010537 case ISN_DROP:
10538 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010539 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010540 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010541 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010542 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010543 case ISN_EXECCONCAT:
10544 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010545 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010546 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010547 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010548 case ISN_GETITEM:
10549 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010550 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010551 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010552 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010553 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010554 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010555 case ISN_LOADBDICT:
10556 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010557 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010558 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010559 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010560 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010561 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010562 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010563 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010564 case ISN_NEGATENR:
10565 case ISN_NEWDICT:
10566 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010567 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010568 case ISN_OPFLOAT:
10569 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010570 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010571 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010572 case ISN_PROF_END:
10573 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010574 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010575 case ISN_PUSHF:
10576 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010577 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010578 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010579 case ISN_REDIREND:
10580 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010581 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010582 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010583 case ISN_SHUFFLE:
10584 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010585 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010586 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010587 case ISN_STORENR:
10588 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010589 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010590 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010591 case ISN_STOREV:
10592 case ISN_STRINDEX:
10593 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010594 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010595 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010596 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010597 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010598 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010599 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010600 // nothing allocated
10601 break;
10602 }
10603}
10604
10605/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010606 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010607 */
10608 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010609delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010610{
10611 int idx;
10612
10613 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010614 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010615
10616 if (dfunc->df_instr != NULL)
10617 {
10618 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10619 delete_instr(dfunc->df_instr + idx);
10620 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010621 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010622 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010623 if (dfunc->df_instr_debug != NULL)
10624 {
10625 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10626 delete_instr(dfunc->df_instr_debug + idx);
10627 VIM_CLEAR(dfunc->df_instr_debug);
10628 dfunc->df_instr_debug = NULL;
10629 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010630#ifdef FEAT_PROFILE
10631 if (dfunc->df_instr_prof != NULL)
10632 {
10633 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10634 delete_instr(dfunc->df_instr_prof + idx);
10635 VIM_CLEAR(dfunc->df_instr_prof);
10636 dfunc->df_instr_prof = NULL;
10637 }
10638#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010639
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010640 if (mark_deleted)
10641 dfunc->df_deleted = TRUE;
10642 if (dfunc->df_ufunc != NULL)
10643 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010644}
10645
10646/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010647 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010648 * function, unless another user function still uses it.
10649 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010650 */
10651 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010652unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010653{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010654 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010655 {
10656 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10657 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010658
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010659 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010660 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010661 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010662 ufunc->uf_dfunc_idx = 0;
10663 if (dfunc->df_ufunc == ufunc)
10664 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010665 }
10666}
10667
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010668/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010669 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010670 */
10671 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010672link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010673{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010674 if (ufunc->uf_dfunc_idx > 0)
10675 {
10676 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10677 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010678
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010679 ++dfunc->df_refcount;
10680 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010681}
10682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010683#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010684/*
10685 * Free all functions defined with ":def".
10686 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010687 void
10688free_def_functions(void)
10689{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010690 int idx;
10691
10692 for (idx = 0; idx < def_functions.ga_len; ++idx)
10693 {
10694 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10695
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010696 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010697 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010698 }
10699
10700 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010701}
10702#endif
10703
10704
10705#endif // FEAT_EVAL