blob: d96724b12c639ba0f616189b09c2f637e10f79e0 [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 Moolenaardd29f1b2020-08-07 20:46:20 +0200693 static int
694generate_add_instr(
695 cctx_T *cctx,
696 vartype_T vartype,
697 type_T *type1,
698 type_T *type2)
699{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100700 garray_T *stack = &cctx->ctx_type_stack;
701 isn_T *isn = generate_instr_drop(cctx,
702 vartype == VAR_NUMBER ? ISN_OPNR
703 : vartype == VAR_LIST ? ISN_ADDLIST
704 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200707#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100708 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709
710 if (vartype != VAR_LIST && vartype != VAR_BLOB
711 && type1->tt_type != VAR_ANY
712 && type2->tt_type != VAR_ANY
713 && check_number_or_float(
714 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
715 return FAIL;
716
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100719
720 // When concatenating two lists with different member types the member type
721 // becomes "any".
722 if (vartype == VAR_LIST
723 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
724 && type1->tt_member != type2->tt_member)
725 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
726
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200727 return isn == NULL ? FAIL : OK;
728}
729
730/*
731 * Get the type to use for an instruction for an operation on "type1" and
732 * "type2". If they are matching use a type-specific instruction. Otherwise
733 * fall back to runtime type checking.
734 */
735 static vartype_T
736operator_type(type_T *type1, type_T *type2)
737{
738 if (type1->tt_type == type2->tt_type
739 && (type1->tt_type == VAR_NUMBER
740 || type1->tt_type == VAR_LIST
741#ifdef FEAT_FLOAT
742 || type1->tt_type == VAR_FLOAT
743#endif
744 || type1->tt_type == VAR_BLOB))
745 return type1->tt_type;
746 return VAR_ANY;
747}
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749/*
750 * Generate an instruction with two arguments. The instruction depends on the
751 * type of the arguments.
752 */
753 static int
754generate_two_op(cctx_T *cctx, char_u *op)
755{
756 garray_T *stack = &cctx->ctx_type_stack;
757 type_T *type1;
758 type_T *type2;
759 vartype_T vartype;
760 isn_T *isn;
761
Bram Moolenaar080457c2020-03-03 21:53:32 +0100762 RETURN_OK_IF_SKIP(cctx);
763
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200764 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
766 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200767 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
769 switch (*op)
770 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200771 case '+':
772 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 break;
775
776 case '-':
777 case '*':
778 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
779 op) == FAIL)
780 return FAIL;
781 if (vartype == VAR_NUMBER)
782 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
783#ifdef FEAT_FLOAT
784 else if (vartype == VAR_FLOAT)
785 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
786#endif
787 else
788 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
789 if (isn != NULL)
790 isn->isn_arg.op.op_type = *op == '*'
791 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
792 break;
793
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200796 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 && type2->tt_type != VAR_NUMBER))
798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200799 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 return FAIL;
801 }
802 isn = generate_instr_drop(cctx,
803 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
804 if (isn != NULL)
805 isn->isn_arg.op.op_type = EXPR_REM;
806 break;
807 }
808
809 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 {
812 type_T *type = &t_any;
813
814#ifdef FEAT_FLOAT
815 // float+number and number+float results in float
816 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
817 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
818 type = &t_float;
819#endif
820 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
821 }
822
823 return OK;
824}
825
826/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200827 * Get the instruction to use for comparing "type1" with "type2"
828 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200830 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100831get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832{
833 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar4c683752020-04-05 21:38:23 +0200835 if (type1 == VAR_UNKNOWN)
836 type1 = VAR_ANY;
837 if (type2 == VAR_UNKNOWN)
838 type2 = VAR_ANY;
839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if (type1 == type2)
841 {
842 switch (type1)
843 {
844 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
845 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
846 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
847 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
848 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
849 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
850 case VAR_LIST: isntype = ISN_COMPARELIST; break;
851 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
852 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 default: isntype = ISN_COMPAREANY; break;
854 }
855 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200856 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100858 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 isntype = ISN_COMPAREANY;
860
Bram Moolenaar657137c2021-01-09 15:45:23 +0100861 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 && (isntype == ISN_COMPAREBOOL
863 || isntype == ISN_COMPARESPECIAL
864 || isntype == ISN_COMPARENR
865 || isntype == ISN_COMPAREFLOAT))
866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200867 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 }
871 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
874 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100875 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
876 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 && (type1 == VAR_BLOB || type2 == VAR_BLOB
878 || type1 == VAR_LIST || type2 == VAR_LIST))))
879 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200880 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884 return isntype;
885}
886
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200887 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100888check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200889{
890 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
891 return FAIL;
892 return OK;
893}
894
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895/*
896 * Generate an ISN_COMPARE* instruction with a boolean result.
897 */
898 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200900{
901 isntype_T isntype;
902 isn_T *isn;
903 garray_T *stack = &cctx->ctx_type_stack;
904 vartype_T type1;
905 vartype_T type2;
906
907 RETURN_OK_IF_SKIP(cctx);
908
909 // Get the known type of the two items on the stack. If they are matching
910 // use a type-specific instruction. Otherwise fall back to runtime type
911 // checking.
912 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
913 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100914 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200915 if (isntype == ISN_DROP)
916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 if ((isn = generate_instr(cctx, isntype)) == NULL)
919 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100920 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 isn->isn_arg.op.op_ic = ic;
922
923 // takes two arguments, puts one bool back
924 if (stack->ga_len >= 2)
925 {
926 --stack->ga_len;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
928 }
929
930 return OK;
931}
932
933/*
934 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200935 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 */
937 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200938generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939{
940 isn_T *isn;
941 garray_T *stack = &cctx->ctx_type_stack;
942
Bram Moolenaar080457c2020-03-03 21:53:32 +0100943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
945 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 isn->isn_arg.tobool.invert = invert;
947 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
949 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 return OK;
953}
954
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200955/*
956 * Generate an ISN_COND2BOOL instruction.
957 */
958 static int
959generate_COND2BOOL(cctx_T *cctx)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
966 return FAIL;
967
968 // type becomes bool
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970
971 return OK;
972}
973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975generate_TYPECHECK(
976 cctx_T *cctx,
977 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100978 int offset,
979 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
986 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100988 isn->isn_arg.type.ct_off = (int8_T)offset;
989 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
Bram Moolenaar5e654232020-09-16 15:22:00 +0200991 // type becomes expected
992 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 return OK;
995}
996
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100997 static int
998generate_SETTYPE(
999 cctx_T *cctx,
1000 type_T *expected)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.type.ct_type = alloc_type(expected);
1008 return OK;
1009}
1010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001012 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1013 * used. Return FALSE if the types will never match.
1014 */
Bram Moolenaar193f6202020-11-16 20:08:35 +01001015 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001016use_typecheck(type_T *actual, type_T *expected)
1017{
1018 if (actual->tt_type == VAR_ANY
1019 || actual->tt_type == VAR_UNKNOWN
1020 || (actual->tt_type == VAR_FUNC
1021 && (expected->tt_type == VAR_FUNC
1022 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001023 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1024 && ((actual->tt_member == &t_void)
1025 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026 return TRUE;
1027 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1028 && actual->tt_type == expected->tt_type)
1029 // This takes care of a nested list or dict.
1030 return use_typecheck(actual->tt_member, expected->tt_member);
1031 return FALSE;
1032}
1033
1034/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001036 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 * - "actual" is a type that can be "expected" type: add a runtime check; or
1038 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001039 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1040 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001041 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001042 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001043need_type(
1044 type_T *actual,
1045 type_T *expected,
1046 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001047 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001048 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001049 int silent,
1050 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001052 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001053
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001054 if (expected == &t_bool && actual != &t_bool
1055 && (actual->tt_flags & TTFLAG_BOOL_OK))
1056 {
1057 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1058 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001059 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001060 return OK;
1061 }
1062
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001063 where.wt_index = arg_idx;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001064 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001065 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001066
1067 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001068 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001069 if ((!actual_is_const || actual == &t_any)
1070 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001071 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001072 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001073 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001074 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001075
1076 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001077 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001078 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001079}
1080
1081/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001082 * Check that the top of the type stack has a type that can be used as a
1083 * condition. Give an error and return FAIL if not.
1084 */
1085 static int
1086bool_on_stack(cctx_T *cctx)
1087{
1088 garray_T *stack = &cctx->ctx_type_stack;
1089 type_T *type;
1090
1091 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 if (type == &t_bool)
1093 return OK;
1094
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001095 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001096 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1097 // This requires a runtime type check.
1098 return generate_COND2BOOL(cctx);
1099
Bram Moolenaar351ead02021-01-16 16:07:01 +01001100 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001101}
1102
1103/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 * Generate an ISN_PUSHNR instruction.
1105 */
1106 static int
1107generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1108{
1109 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111
Bram Moolenaar080457c2020-03-03 21:53:32 +01001112 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1114 return FAIL;
1115 isn->isn_arg.number = number;
1116
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001117 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001118 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001119 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHBOOL instruction.
1125 */
1126 static int
1127generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139/*
1140 * Generate an ISN_PUSHSPEC instruction.
1141 */
1142 static int
1143generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.number = number;
1151
1152 return OK;
1153}
1154
1155#ifdef FEAT_FLOAT
1156/*
1157 * Generate an ISN_PUSHF instruction.
1158 */
1159 static int
1160generate_PUSHF(cctx_T *cctx, float_T fnumber)
1161{
1162 isn_T *isn;
1163
Bram Moolenaar080457c2020-03-03 21:53:32 +01001164 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1166 return FAIL;
1167 isn->isn_arg.fnumber = fnumber;
1168
1169 return OK;
1170}
1171#endif
1172
1173/*
1174 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001175 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 */
1177 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001178generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179{
1180 isn_T *isn;
1181
Bram Moolenaar508b5612021-01-02 18:17:26 +01001182 if (cctx->ctx_skip == SKIP_YES)
1183 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001184 if (str != NULL)
1185 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001186 return OK;
1187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001189 {
1190 if (str != NULL)
1191 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001193 }
1194 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
1196 return OK;
1197}
1198
1199/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001200 * Generate an ISN_PUSHCHANNEL instruction.
1201 * Consumes "channel".
1202 */
1203 static int
1204generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1205{
1206 isn_T *isn;
1207
Bram Moolenaar080457c2020-03-03 21:53:32 +01001208 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001209 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1210 return FAIL;
1211 isn->isn_arg.channel = channel;
1212
1213 return OK;
1214}
1215
1216/*
1217 * Generate an ISN_PUSHJOB instruction.
1218 * Consumes "job".
1219 */
1220 static int
1221generate_PUSHJOB(cctx_T *cctx, job_T *job)
1222{
1223 isn_T *isn;
1224
Bram Moolenaar080457c2020-03-03 21:53:32 +01001225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001226 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001227 return FAIL;
1228 isn->isn_arg.job = job;
1229
1230 return OK;
1231}
1232
1233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 * Generate an ISN_PUSHBLOB instruction.
1235 * Consumes "blob".
1236 */
1237 static int
1238generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.blob = blob;
1246
1247 return OK;
1248}
1249
1250/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001251 * Generate an ISN_PUSHFUNC instruction with name "name".
1252 * Consumes "name".
1253 */
1254 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001255generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001256{
1257 isn_T *isn;
1258
Bram Moolenaar080457c2020-03-03 21:53:32 +01001259 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001260 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001261 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001262 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001263
1264 return OK;
1265}
1266
1267/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001268 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001269 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1270 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001271 */
1272 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001273generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001274{
1275 isn_T *isn;
1276 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001277 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1278 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001279 type_T *item_type = &t_any;
1280
1281 RETURN_OK_IF_SKIP(cctx);
1282
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001283 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001284 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001285 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001286 emsg(_(e_listreq));
1287 return FAIL;
1288 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001289 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001290 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1291 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001292 isn->isn_arg.getitem.gi_index = index;
1293 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001294
1295 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001296 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001297 return FAIL;
1298 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1299 ++stack->ga_len;
1300 return OK;
1301}
1302
1303/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001304 * Generate an ISN_SLICE instruction with "count".
1305 */
1306 static int
1307generate_SLICE(cctx_T *cctx, int count)
1308{
1309 isn_T *isn;
1310
1311 RETURN_OK_IF_SKIP(cctx);
1312 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1313 return FAIL;
1314 isn->isn_arg.number = count;
1315 return OK;
1316}
1317
1318/*
1319 * Generate an ISN_CHECKLEN instruction with "min_len".
1320 */
1321 static int
1322generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1323{
1324 isn_T *isn;
1325
1326 RETURN_OK_IF_SKIP(cctx);
1327
1328 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.checklen.cl_min_len = min_len;
1331 isn->isn_arg.checklen.cl_more_OK = more_OK;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 * Generate an ISN_STORE instruction.
1338 */
1339 static int
1340generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1341{
1342 isn_T *isn;
1343
Bram Moolenaar080457c2020-03-03 21:53:32 +01001344 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1346 return FAIL;
1347 if (name != NULL)
1348 isn->isn_arg.string = vim_strsave(name);
1349 else
1350 isn->isn_arg.number = idx;
1351
1352 return OK;
1353}
1354
1355/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001356 * Generate an ISN_STOREOUTER instruction.
1357 */
1358 static int
1359generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1360{
1361 isn_T *isn;
1362
1363 RETURN_OK_IF_SKIP(cctx);
1364 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1365 return FAIL;
1366 isn->isn_arg.outer.outer_idx = idx;
1367 isn->isn_arg.outer.outer_depth = level;
1368
1369 return OK;
1370}
1371
1372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1374 */
1375 static int
1376generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1377{
1378 isn_T *isn;
1379
Bram Moolenaar080457c2020-03-03 21:53:32 +01001380 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1382 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001383 isn->isn_arg.storenr.stnr_idx = idx;
1384 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 return OK;
1387}
1388
1389/*
1390 * Generate an ISN_STOREOPT instruction
1391 */
1392 static int
1393generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1394{
1395 isn_T *isn;
1396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001398 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 return FAIL;
1400 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1401 isn->isn_arg.storeopt.so_flags = opt_flags;
1402
1403 return OK;
1404}
1405
1406/*
1407 * Generate an ISN_LOAD or similar instruction.
1408 */
1409 static int
1410generate_LOAD(
1411 cctx_T *cctx,
1412 isntype_T isn_type,
1413 int idx,
1414 char_u *name,
1415 type_T *type)
1416{
1417 isn_T *isn;
1418
Bram Moolenaar080457c2020-03-03 21:53:32 +01001419 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1421 return FAIL;
1422 if (name != NULL)
1423 isn->isn_arg.string = vim_strsave(name);
1424 else
1425 isn->isn_arg.number = idx;
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001431 * Generate an ISN_LOADOUTER instruction
1432 */
1433 static int
1434generate_LOADOUTER(
1435 cctx_T *cctx,
1436 int idx,
1437 int nesting,
1438 type_T *type)
1439{
1440 isn_T *isn;
1441
1442 RETURN_OK_IF_SKIP(cctx);
1443 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1444 return FAIL;
1445 isn->isn_arg.outer.outer_idx = idx;
1446 isn->isn_arg.outer.outer_depth = nesting;
1447
1448 return OK;
1449}
1450
1451/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001452 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453 */
1454 static int
1455generate_LOADV(
1456 cctx_T *cctx,
1457 char_u *name,
1458 int error)
1459{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001460 int di_flags;
1461 int vidx = find_vim_var(name, &di_flags);
1462 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001463
Bram Moolenaar080457c2020-03-03 21:53:32 +01001464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001465 if (vidx < 0)
1466 {
1467 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001468 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469 return FAIL;
1470 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001471 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001472
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001473 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001474}
1475
1476/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001477 * Generate an ISN_UNLET instruction.
1478 */
1479 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001480generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001481{
1482 isn_T *isn;
1483
1484 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001485 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001486 return FAIL;
1487 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1488 isn->isn_arg.unlet.ul_forceit = forceit;
1489
1490 return OK;
1491}
1492
1493/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001494 * Generate an ISN_LOCKCONST instruction.
1495 */
1496 static int
1497generate_LOCKCONST(cctx_T *cctx)
1498{
1499 isn_T *isn;
1500
1501 RETURN_OK_IF_SKIP(cctx);
1502 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1503 return FAIL;
1504 return OK;
1505}
1506
1507/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 * Generate an ISN_LOADS instruction.
1509 */
1510 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001511generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001513 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001515 int sid,
1516 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517{
1518 isn_T *isn;
1519
Bram Moolenaar080457c2020-03-03 21:53:32 +01001520 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001521 if (isn_type == ISN_LOADS)
1522 isn = generate_instr_type(cctx, isn_type, type);
1523 else
1524 isn = generate_instr_drop(cctx, isn_type, 1);
1525 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001527 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1528 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529
1530 return OK;
1531}
1532
1533/*
1534 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1535 */
1536 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001537generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 cctx_T *cctx,
1539 isntype_T isn_type,
1540 int sid,
1541 int idx,
1542 type_T *type)
1543{
1544 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001545 scriptref_T *sref;
1546 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547
Bram Moolenaar080457c2020-03-03 21:53:32 +01001548 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 if (isn_type == ISN_LOADSCRIPT)
1550 isn = generate_instr_type(cctx, isn_type, type);
1551 else
1552 isn = generate_instr_drop(cctx, isn_type, 1);
1553 if (isn == NULL)
1554 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001555
1556 // This requires three arguments, which doesn't fit in an instruction, thus
1557 // we need to allocate a struct for this.
1558 sref = ALLOC_ONE(scriptref_T);
1559 if (sref == NULL)
1560 return FAIL;
1561 isn->isn_arg.script.scriptref = sref;
1562 sref->sref_sid = sid;
1563 sref->sref_idx = idx;
1564 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001565 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 return OK;
1567}
1568
1569/*
1570 * Generate an ISN_NEWLIST instruction.
1571 */
1572 static int
1573generate_NEWLIST(cctx_T *cctx, int count)
1574{
1575 isn_T *isn;
1576 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 type_T *type;
1578 type_T *member;
1579
Bram Moolenaar080457c2020-03-03 21:53:32 +01001580 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1582 return FAIL;
1583 isn->isn_arg.number = count;
1584
Bram Moolenaar127542b2020-08-09 17:22:04 +02001585 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001586 if (count == 0)
1587 member = &t_void;
1588 else
1589 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001590 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1591 cctx->ctx_type_list);
1592 type = get_list_type(member, cctx->ctx_type_list);
1593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 // drop the value types
1595 stack->ga_len -= count;
1596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001598 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 return FAIL;
1600 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1601 ++stack->ga_len;
1602
1603 return OK;
1604}
1605
1606/*
1607 * Generate an ISN_NEWDICT instruction.
1608 */
1609 static int
1610generate_NEWDICT(cctx_T *cctx, int count)
1611{
1612 isn_T *isn;
1613 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 type_T *type;
1615 type_T *member;
1616
Bram Moolenaar080457c2020-03-03 21:53:32 +01001617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.number = count;
1621
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001622 if (count == 0)
1623 member = &t_void;
1624 else
1625 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001626 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1627 cctx->ctx_type_list);
1628 type = get_dict_type(member, cctx->ctx_type_list);
1629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 // drop the key and value types
1631 stack->ga_len -= 2 * count;
1632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001634 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 return FAIL;
1636 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1637 ++stack->ga_len;
1638
1639 return OK;
1640}
1641
1642/*
1643 * Generate an ISN_FUNCREF instruction.
1644 */
1645 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001646generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647{
1648 isn_T *isn;
1649 garray_T *stack = &cctx->ctx_type_stack;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1653 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001654 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001655 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001657 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001658 // the nested context, including this one.
1659 if (ufunc->uf_flags & FC_CLOSURE)
1660 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1661
Bram Moolenaar35578162021-08-02 19:10:38 +02001662 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001664 ((type_T **)stack->ga_data)[stack->ga_len] =
1665 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 ++stack->ga_len;
1667
1668 return OK;
1669}
1670
1671/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001672 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001673 * "lambda_name" and "func_name" must be in allocated memory and will be
1674 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001675 */
1676 static int
1677generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1678{
1679 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001680
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001681 if (cctx->ctx_skip == SKIP_YES)
1682 {
1683 vim_free(lambda_name);
1684 vim_free(func_name);
1685 return OK;
1686 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001687 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001688 {
1689 vim_free(lambda_name);
1690 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001691 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001692 }
1693 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001694 isn->isn_arg.newfunc.nf_global = func_name;
1695
1696 return OK;
1697}
1698
1699/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001700 * Generate an ISN_DEF instruction: list functions
1701 */
1702 static int
1703generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1704{
1705 isn_T *isn;
1706
1707 RETURN_OK_IF_SKIP(cctx);
1708 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1709 return FAIL;
1710 if (len > 0)
1711 {
1712 isn->isn_arg.string = vim_strnsave(name, len);
1713 if (isn->isn_arg.string == NULL)
1714 return FAIL;
1715 }
1716 return OK;
1717}
1718
1719/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 * Generate an ISN_JUMP instruction.
1721 */
1722 static int
1723generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1724{
1725 isn_T *isn;
1726 garray_T *stack = &cctx->ctx_type_stack;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1730 return FAIL;
1731 isn->isn_arg.jump.jump_when = when;
1732 isn->isn_arg.jump.jump_where = where;
1733
1734 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1735 --stack->ga_len;
1736
1737 return OK;
1738}
1739
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001740/*
1741 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1742 */
1743 static int
1744generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1745{
1746 isn_T *isn;
1747
1748 RETURN_OK_IF_SKIP(cctx);
1749 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1750 return FAIL;
1751 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1752 // jump_where is set later
1753 return OK;
1754}
1755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 static int
1757generate_FOR(cctx_T *cctx, int loop_idx)
1758{
1759 isn_T *isn;
1760 garray_T *stack = &cctx->ctx_type_stack;
1761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.forloop.for_idx = loop_idx;
1766
Bram Moolenaar35578162021-08-02 19:10:38 +02001767 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 return FAIL;
1769 // type doesn't matter, will be stored next
1770 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1771 ++stack->ga_len;
1772
1773 return OK;
1774}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001775/*
1776 * Generate an ISN_TRYCONT instruction.
1777 */
1778 static int
1779generate_TRYCONT(cctx_T *cctx, int levels, int where)
1780{
1781 isn_T *isn;
1782
1783 RETURN_OK_IF_SKIP(cctx);
1784 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1785 return FAIL;
1786 isn->isn_arg.trycont.tct_levels = levels;
1787 isn->isn_arg.trycont.tct_where = where;
1788
1789 return OK;
1790}
1791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792
1793/*
1794 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001795 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 * Return FAIL if the number of arguments is wrong.
1797 */
1798 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001799generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800{
1801 isn_T *isn;
1802 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001803 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001804 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001805 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001806 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807
Bram Moolenaar080457c2020-03-03 21:53:32 +01001808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001809 argoff = check_internal_func(func_idx, argcount);
1810 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 return FAIL;
1812
Bram Moolenaar389df252020-07-09 21:20:47 +02001813 if (method_call && argoff > 1)
1814 {
1815 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1816 return FAIL;
1817 isn->isn_arg.shuffle.shfl_item = argcount;
1818 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1819 }
1820
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001821 if (argcount > 0)
1822 {
1823 // Check the types of the arguments.
1824 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001825 if (method_call && argoff > 1)
1826 {
1827 int i;
1828
1829 for (i = 0; i < argcount; ++i)
1830 shuffled_argtypes[i] = (i < argoff - 1)
1831 ? argtypes[i + 1]
1832 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1833 argtypes = shuffled_argtypes;
1834 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001835 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1836 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001837 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001838 if (internal_func_is_map(func_idx))
1839 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001840 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1843 return FAIL;
1844 isn->isn_arg.bfunc.cbf_idx = func_idx;
1845 isn->isn_arg.bfunc.cbf_argcount = argcount;
1846
Bram Moolenaar94738d82020-10-21 14:25:07 +02001847 // Drop the argument types and push the return type.
1848 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001849 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 return FAIL;
1851 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001852 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001853 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001855 if (maptype != NULL && maptype->tt_member != NULL
1856 && maptype->tt_member != &t_any)
1857 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001858 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 return OK;
1861}
1862
1863/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001864 * Generate an ISN_LISTAPPEND instruction. Works like add().
1865 * Argument count is already checked.
1866 */
1867 static int
1868generate_LISTAPPEND(cctx_T *cctx)
1869{
1870 garray_T *stack = &cctx->ctx_type_stack;
1871 type_T *list_type;
1872 type_T *item_type;
1873 type_T *expected;
1874
1875 // Caller already checked that list_type is a list.
1876 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1877 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1878 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001879 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001880 return FAIL;
1881
1882 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1883 return FAIL;
1884
1885 --stack->ga_len; // drop the argument
1886 return OK;
1887}
1888
1889/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001890 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1891 * Argument count is already checked.
1892 */
1893 static int
1894generate_BLOBAPPEND(cctx_T *cctx)
1895{
1896 garray_T *stack = &cctx->ctx_type_stack;
1897 type_T *item_type;
1898
1899 // Caller already checked that blob_type is a blob.
1900 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001901 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001902 return FAIL;
1903
1904 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1905 return FAIL;
1906
1907 --stack->ga_len; // drop the argument
1908 return OK;
1909}
1910
1911/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001912 * Return TRUE if "ufunc" should be compiled, taking into account whether
1913 * "profile" indicates profiling is to be done.
1914 */
1915 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001916func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001917{
1918 switch (ufunc->uf_def_status)
1919 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001920 case UF_TO_BE_COMPILED:
1921 return TRUE;
1922
Bram Moolenaarb2049902021-01-24 12:53:53 +01001923 case UF_COMPILED:
1924 {
1925 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1926 + ufunc->uf_dfunc_idx;
1927
Bram Moolenaare99d4222021-06-13 14:01:26 +02001928 switch (compile_type)
1929 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001930 case CT_PROFILE:
1931#ifdef FEAT_PROFILE
1932 return dfunc->df_instr_prof == NULL;
1933#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001934 case CT_NONE:
1935 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001936 case CT_DEBUG:
1937 return dfunc->df_instr_debug == NULL;
1938 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001939 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001940
1941 case UF_NOT_COMPILED:
1942 case UF_COMPILE_ERROR:
1943 case UF_COMPILING:
1944 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001945 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001946 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001947}
1948
1949/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 * Generate an ISN_DCALL or ISN_UCALL instruction.
1951 * Return FAIL if the number of arguments is wrong.
1952 */
1953 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001954generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955{
1956 isn_T *isn;
1957 garray_T *stack = &cctx->ctx_type_stack;
1958 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001959 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960
Bram Moolenaar080457c2020-03-03 21:53:32 +01001961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 if (argcount > regular_args && !has_varargs(ufunc))
1963 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001964 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 return FAIL;
1966 }
1967 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1968 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001969 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 return FAIL;
1971 }
1972
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001973 if (ufunc->uf_def_status != UF_NOT_COMPILED
1974 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001975 {
1976 int i;
1977
1978 for (i = 0; i < argcount; ++i)
1979 {
1980 type_T *expected;
1981 type_T *actual;
1982
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001983 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1984 if (actual == &t_special
1985 && i >= regular_args - ufunc->uf_def_args.ga_len)
1986 {
1987 // assume v:none used for default argument value
1988 continue;
1989 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001990 if (i < regular_args)
1991 {
1992 if (ufunc->uf_arg_types == NULL)
1993 continue;
1994 expected = ufunc->uf_arg_types[i];
1995 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001996 else if (ufunc->uf_va_type == NULL
1997 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001998 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001999 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002000 else
2001 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002002 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002003 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002004 {
2005 arg_type_mismatch(expected, actual, i + 1);
2006 return FAIL;
2007 }
2008 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002009 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002010 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002011 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002012 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002013 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002014 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2015 {
2016 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2017 ufunc->uf_name);
2018 return FAIL;
2019 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002022 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002023 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002025 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 {
2027 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2028 isn->isn_arg.dfunc.cdf_argcount = argcount;
2029 }
2030 else
2031 {
2032 // A user function may be deleted and redefined later, can't use the
2033 // ufunc pointer, need to look it up again at runtime.
2034 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2035 isn->isn_arg.ufunc.cuf_argcount = argcount;
2036 }
2037
2038 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002039 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 return FAIL;
2041 // add return value
2042 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2043 ++stack->ga_len;
2044
2045 return OK;
2046}
2047
2048/*
2049 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2050 */
2051 static int
2052generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2053{
2054 isn_T *isn;
2055 garray_T *stack = &cctx->ctx_type_stack;
2056
Bram Moolenaar080457c2020-03-03 21:53:32 +01002057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2059 return FAIL;
2060 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2061 isn->isn_arg.ufunc.cuf_argcount = argcount;
2062
2063 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002064 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002065 return FAIL;
2066 // add return value
2067 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2068 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069
2070 return OK;
2071}
2072
2073/*
2074 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002075 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 */
2077 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002078generate_PCALL(
2079 cctx_T *cctx,
2080 int argcount,
2081 char_u *name,
2082 type_T *type,
2083 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084{
2085 isn_T *isn;
2086 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002087 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088
Bram Moolenaar080457c2020-03-03 21:53:32 +01002089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002090
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002091 if (type->tt_type == VAR_ANY)
2092 ret_type = &t_any;
2093 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002094 {
2095 if (type->tt_argcount != -1)
2096 {
2097 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2098
2099 if (argcount < type->tt_min_argcount - varargs)
2100 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002101 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002102 return FAIL;
2103 }
2104 if (!varargs && argcount > type->tt_argcount)
2105 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002106 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002107 return FAIL;
2108 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002109 if (type->tt_args != NULL)
2110 {
2111 int i;
2112
2113 for (i = 0; i < argcount; ++i)
2114 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002115 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002116 type_T *actual = ((type_T **)stack->ga_data)[
2117 stack->ga_len + offset];
2118 type_T *expected;
2119
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002120 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002121 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002122 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002123 else if (i >= type->tt_min_argcount
2124 && actual == &t_special)
2125 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002126 else
2127 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002128 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002129 cctx, TRUE, FALSE) == FAIL)
2130 {
2131 arg_type_mismatch(expected, actual, i + 1);
2132 return FAIL;
2133 }
2134 }
2135 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002136 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002137 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002138 if (ret_type == &t_unknown)
2139 // return type not known yet, use a runtime check
2140 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002141 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002142 else
2143 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002144 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002145 return FAIL;
2146 }
2147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2149 return FAIL;
2150 isn->isn_arg.pfunc.cpf_top = at_top;
2151 isn->isn_arg.pfunc.cpf_argcount = argcount;
2152
2153 stack->ga_len -= argcount; // drop the arguments
2154
2155 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002156 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002158 // If partial is above the arguments it must be cleared and replaced with
2159 // the return value.
2160 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2161 return FAIL;
2162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 return OK;
2164}
2165
2166/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002167 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 */
2169 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002170generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171{
2172 isn_T *isn;
2173 garray_T *stack = &cctx->ctx_type_stack;
2174 type_T *type;
2175
Bram Moolenaar080457c2020-03-03 21:53:32 +01002176 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002177 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002179 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002181 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002183 if (type->tt_type != VAR_DICT && type != &t_any)
2184 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002185 char *tofree;
2186
2187 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2188 name, type_name(type, &tofree));
2189 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002190 return FAIL;
2191 }
2192 // change dict type to dict member type
2193 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002194 {
2195 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2196 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198
2199 return OK;
2200}
2201
2202/*
2203 * Generate an ISN_ECHO instruction.
2204 */
2205 static int
2206generate_ECHO(cctx_T *cctx, int with_white, int count)
2207{
2208 isn_T *isn;
2209
Bram Moolenaar080457c2020-03-03 21:53:32 +01002210 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2212 return FAIL;
2213 isn->isn_arg.echo.echo_with_white = with_white;
2214 isn->isn_arg.echo.echo_count = count;
2215
2216 return OK;
2217}
2218
Bram Moolenaarad39c092020-02-26 18:23:43 +01002219/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002220 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002221 */
2222 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002223generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002224{
2225 isn_T *isn;
2226
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002227 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002228 return FAIL;
2229 isn->isn_arg.number = count;
2230
2231 return OK;
2232}
2233
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002234/*
2235 * Generate an ISN_PUT instruction.
2236 */
2237 static int
2238generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2239{
2240 isn_T *isn;
2241
2242 RETURN_OK_IF_SKIP(cctx);
2243 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2244 return FAIL;
2245 isn->isn_arg.put.put_regname = regname;
2246 isn->isn_arg.put.put_lnum = lnum;
2247 return OK;
2248}
2249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 static int
2251generate_EXEC(cctx_T *cctx, char_u *line)
2252{
2253 isn_T *isn;
2254
Bram Moolenaar080457c2020-03-03 21:53:32 +01002255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2257 return FAIL;
2258 isn->isn_arg.string = vim_strsave(line);
2259 return OK;
2260}
2261
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002262 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002263generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2264{
2265 isn_T *isn;
2266 garray_T *stack = &cctx->ctx_type_stack;
2267
2268 RETURN_OK_IF_SKIP(cctx);
2269 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2270 return FAIL;
2271 isn->isn_arg.string = vim_strsave(line);
2272
Bram Moolenaar35578162021-08-02 19:10:38 +02002273 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002274 return FAIL;
2275 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2276 ++stack->ga_len;
2277
2278 return OK;
2279}
2280
2281 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002282generate_EXECCONCAT(cctx_T *cctx, int count)
2283{
2284 isn_T *isn;
2285
2286 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2287 return FAIL;
2288 isn->isn_arg.number = count;
2289 return OK;
2290}
2291
Bram Moolenaar08597872020-12-10 19:43:40 +01002292/*
2293 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2294 */
2295 static int
2296generate_RANGE(cctx_T *cctx, char_u *range)
2297{
2298 isn_T *isn;
2299 garray_T *stack = &cctx->ctx_type_stack;
2300
2301 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2302 return FAIL;
2303 isn->isn_arg.string = range;
2304
Bram Moolenaar35578162021-08-02 19:10:38 +02002305 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002306 return FAIL;
2307 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2308 ++stack->ga_len;
2309 return OK;
2310}
2311
Bram Moolenaar792f7862020-11-23 08:31:18 +01002312 static int
2313generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2314{
2315 isn_T *isn;
2316
2317 RETURN_OK_IF_SKIP(cctx);
2318 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2319 return FAIL;
2320 isn->isn_arg.unpack.unp_count = var_count;
2321 isn->isn_arg.unpack.unp_semicolon = semicolon;
2322 return OK;
2323}
2324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002326 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002327 */
2328 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002329generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002330{
2331 isn_T *isn;
2332
Bram Moolenaarfa984412021-03-25 22:15:28 +01002333 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002334 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002335 cctx->ctx_has_cmdmod = TRUE;
2336
2337 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002338 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002339 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2340 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2341 return FAIL;
2342 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002343 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002344 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002345 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002346
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002347 return OK;
2348}
2349
2350 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002351generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002352{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002353 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2354 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002355 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002356 return OK;
2357}
2358
Bram Moolenaarfa984412021-03-25 22:15:28 +01002359 static int
2360misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002361{
2362 garray_T *instr = &cctx->ctx_instr;
2363
Bram Moolenaara91a7132021-03-25 21:12:15 +01002364 if (cctx->ctx_has_cmdmod
2365 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2366 == ISN_CMDMOD)
2367 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002368 emsg(_(e_misplaced_command_modifier));
2369 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002370 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002371 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002372}
2373
2374/*
2375 * Get the index of the current instruction.
2376 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2377 */
2378 static int
2379current_instr_idx(cctx_T *cctx)
2380{
2381 garray_T *instr = &cctx->ctx_instr;
2382 int idx = instr->ga_len;
2383
Bram Moolenaare99d4222021-06-13 14:01:26 +02002384 while (idx > 0)
2385 {
2386 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002387 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002388 {
2389 --idx;
2390 continue;
2391 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002392#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002393 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2394 {
2395 --idx;
2396 continue;
2397 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002398#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002399 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2400 {
2401 --idx;
2402 continue;
2403 }
2404 break;
2405 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002406 return idx;
2407}
2408
Bram Moolenaarf002a412021-01-24 13:34:18 +01002409#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002410 static void
2411may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2412{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002413 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002414 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002415}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002416#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002417
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002418/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002420 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002422 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002423reserve_local(
2424 cctx_T *cctx,
2425 char_u *name,
2426 size_t len,
2427 int isConst,
2428 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002431 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002433 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002435 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002436 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 }
2438
Bram Moolenaar35578162021-08-02 19:10:38 +02002439 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002440 return NULL;
2441 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002442 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002444 // Every local variable uses the next entry on the stack. We could re-use
2445 // the last ones when leaving a scope, but then variables used in a closure
2446 // might get overwritten. To keep things simple do not re-use stack
2447 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002448 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2449 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002451 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 lvar->lv_const = isConst;
2453 lvar->lv_type = type;
2454
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002455 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002456 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002457 return NULL;
2458 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2459 vim_strsave(lvar->lv_name);
2460 ++dfunc->df_var_names.ga_len;
2461
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002462 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463}
2464
2465/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002466 * Remove local variables above "new_top".
2467 */
2468 static void
2469unwind_locals(cctx_T *cctx, int new_top)
2470{
2471 if (cctx->ctx_locals.ga_len > new_top)
2472 {
2473 int idx;
2474 lvar_T *lvar;
2475
2476 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2477 {
2478 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2479 vim_free(lvar->lv_name);
2480 }
2481 }
2482 cctx->ctx_locals.ga_len = new_top;
2483}
2484
2485/*
2486 * Free all local variables.
2487 */
2488 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002489free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002490{
2491 unwind_locals(cctx, 0);
2492 ga_clear(&cctx->ctx_locals);
2493}
2494
2495/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002496 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2497 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2498 * error if the variable was defined with :const.
2499 */
2500 static int
2501check_item_writable(svar_T *sv, int check_writable, char_u *name)
2502{
2503 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2504 || (check_writable == ASSIGN_FINAL
2505 && sv->sv_const == ASSIGN_CONST))
2506 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002507 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002508 return FAIL;
2509 }
2510 return OK;
2511}
2512
2513/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002515 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 * Returns the index in "sn_var_vals" if found.
2517 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002518 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 */
2520 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002521get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522{
2523 hashtab_T *ht;
2524 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002525 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002526 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 int idx;
2528
Bram Moolenaare3d46852020-08-29 13:39:17 +02002529 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002531 if (sid == current_sctx.sc_sid)
2532 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002533 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002534
2535 if (sav == NULL)
2536 return -2;
2537 idx = sav->sav_var_vals_idx;
2538 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002539 if (check_item_writable(sv, check_writable, name) == FAIL)
2540 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002541 return idx;
2542 }
2543
2544 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 ht = &SCRIPT_VARS(sid);
2546 di = find_var_in_ht(ht, 0, name, TRUE);
2547 if (di == NULL)
2548 return -2;
2549
2550 // Now find the svar_T index in sn_var_vals.
2551 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2552 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002553 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (sv->sv_tv == &di->di_tv)
2555 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002556 if (check_item_writable(sv, check_writable, name) == FAIL)
2557 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 return idx;
2559 }
2560 }
2561 return -1;
2562}
2563
2564/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002565 * Find "name" in imported items of the current script or in "cctx" if not
2566 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 */
2568 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002569find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 int idx;
2572
Bram Moolenaare3d46852020-08-29 13:39:17 +02002573 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002574 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 if (cctx != NULL)
2576 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2577 {
2578 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2579 + idx;
2580
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002581 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2582 : STRLEN(import->imp_name) == len
2583 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 return import;
2585 }
2586
Bram Moolenaarefa94442020-08-08 22:16:00 +02002587 return find_imported_in_script(name, len, current_sctx.sc_sid);
2588}
2589
2590 imported_T *
2591find_imported_in_script(char_u *name, size_t len, int sid)
2592{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002593 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002594 int idx;
2595
Bram Moolenaare3d46852020-08-29 13:39:17 +02002596 if (!SCRIPT_ID_VALID(sid))
2597 return NULL;
2598 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2600 {
2601 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2602
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002603 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2604 : STRLEN(import->imp_name) == len
2605 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 return import;
2607 }
2608 return NULL;
2609}
2610
2611/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002612 * Free all imported variables.
2613 */
2614 static void
2615free_imported(cctx_T *cctx)
2616{
2617 int idx;
2618
2619 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2620 {
2621 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2622
2623 vim_free(import->imp_name);
2624 }
2625 ga_clear(&cctx->ctx_imports);
2626}
2627
2628/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002629 * Return a pointer to the next line that isn't empty or only contains a
2630 * comment. Skips over white space.
2631 * Returns NULL if there is none.
2632 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002633 char_u *
2634peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002635{
2636 int lnum = cctx->ctx_lnum;
2637
2638 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2639 {
2640 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002641 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002642
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002643 // ignore NULLs inserted for continuation lines
2644 if (line != NULL)
2645 {
2646 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002647 if (vim9_bad_comment(p))
2648 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002649 if (*p != NUL && !vim9_comment_start(p))
2650 return p;
2651 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002652 }
2653 return NULL;
2654}
2655
2656/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002657 * Called when checking for a following operator at "arg". When the rest of
2658 * the line is empty or only a comment, peek the next line. If there is a next
2659 * line return a pointer to it and set "nextp".
2660 * Otherwise skip over white space.
2661 */
2662 static char_u *
2663may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2664{
2665 char_u *p = skipwhite(arg);
2666
2667 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002668 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002669 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002670 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002671 if (*nextp != NULL)
2672 return *nextp;
2673 }
2674 return p;
2675}
2676
2677/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002678 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002679 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002680 * Returns NULL when at the end.
2681 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002682 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002683next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002684{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002685 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002686
2687 do
2688 {
2689 ++cctx->ctx_lnum;
2690 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002691 {
2692 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002693 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002694 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002695 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002696 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002697 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002698 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002699 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002700 return line;
2701}
2702
2703/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002704 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002705 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002706 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002707 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2708 */
2709 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002710may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002711{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002712 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002713 if (vim9_bad_comment(*arg))
2714 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002715 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002716 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002717 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002718
2719 if (next == NULL)
2720 return FAIL;
2721 *arg = skipwhite(next);
2722 }
2723 return OK;
2724}
2725
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002726/*
2727 * Idem, and give an error when failed.
2728 */
2729 static int
2730may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2731{
2732 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2733 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002734 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002735 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002736 return FAIL;
2737 }
2738 return OK;
2739}
2740
2741
Bram Moolenaara5565e42020-05-09 15:44:01 +02002742// Structure passed between the compile_expr* functions to keep track of
2743// constants that have been parsed but for which no code was produced yet. If
2744// possible expressions on these constants are applied at compile time. If
2745// that is not possible, the code to push the constants needs to be generated
2746// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002747// Using 50 should be more than enough of 5 levels of ().
2748#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002749typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002750 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002751 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002752 int pp_is_const; // all generated code was constants, used for a
2753 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002754} ppconst_T;
2755
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002756static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002757static int compile_expr0(char_u **arg, cctx_T *cctx);
2758static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2759
Bram Moolenaara5565e42020-05-09 15:44:01 +02002760/*
2761 * Generate a PUSH instruction for "tv".
2762 * "tv" will be consumed or cleared.
2763 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2764 */
2765 static int
2766generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2767{
2768 if (tv != NULL)
2769 {
2770 switch (tv->v_type)
2771 {
2772 case VAR_UNKNOWN:
2773 break;
2774 case VAR_BOOL:
2775 generate_PUSHBOOL(cctx, tv->vval.v_number);
2776 break;
2777 case VAR_SPECIAL:
2778 generate_PUSHSPEC(cctx, tv->vval.v_number);
2779 break;
2780 case VAR_NUMBER:
2781 generate_PUSHNR(cctx, tv->vval.v_number);
2782 break;
2783#ifdef FEAT_FLOAT
2784 case VAR_FLOAT:
2785 generate_PUSHF(cctx, tv->vval.v_float);
2786 break;
2787#endif
2788 case VAR_BLOB:
2789 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2790 tv->vval.v_blob = NULL;
2791 break;
2792 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002793 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002794 tv->vval.v_string = NULL;
2795 break;
2796 default:
2797 iemsg("constant type not supported");
2798 clear_tv(tv);
2799 return FAIL;
2800 }
2801 tv->v_type = VAR_UNKNOWN;
2802 }
2803 return OK;
2804}
2805
2806/*
2807 * Generate code for any ppconst entries.
2808 */
2809 static int
2810generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2811{
2812 int i;
2813 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002814 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002815
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002816 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002817 for (i = 0; i < ppconst->pp_used; ++i)
2818 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2819 ret = FAIL;
2820 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002821 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002822 return ret;
2823}
2824
2825/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002826 * Check that the last item of "ppconst" is a bool.
2827 */
2828 static int
2829check_ppconst_bool(ppconst_T *ppconst)
2830{
2831 if (ppconst->pp_used > 0)
2832 {
2833 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002834 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002835
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002836 return check_typval_type(&t_bool, tv, where);
2837 }
2838 return OK;
2839}
2840
2841/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002842 * Clear ppconst constants. Used when failing.
2843 */
2844 static void
2845clear_ppconst(ppconst_T *ppconst)
2846{
2847 int i;
2848
2849 for (i = 0; i < ppconst->pp_used; ++i)
2850 clear_tv(&ppconst->pp_tv[i]);
2851 ppconst->pp_used = 0;
2852}
2853
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002854/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002855 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002856 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002857 */
2858 static int
2859compile_member(int is_slice, cctx_T *cctx)
2860{
2861 type_T **typep;
2862 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002863 vartype_T vartype;
2864 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002865
Bram Moolenaar261417b2021-05-06 21:04:55 +02002866 // We can index a list, dict and blob. If we don't know the type
2867 // we can use the index value type. If we still don't know use an "ANY"
2868 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 typep = ((type_T **)stack->ga_data) + stack->ga_len
2870 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002871 vartype = (*typep)->tt_type;
2872 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002873 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002874 if (*typep == &t_any && idxtype == &t_string)
2875 vartype = VAR_DICT;
2876 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002877 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002878 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 return FAIL;
2880 if (is_slice)
2881 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002882 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2883 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002884 FALSE, FALSE) == FAIL)
2885 return FAIL;
2886 }
2887 }
2888
Bram Moolenaar261417b2021-05-06 21:04:55 +02002889 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002890 {
2891 if (is_slice)
2892 {
2893 emsg(_(e_cannot_slice_dictionary));
2894 return FAIL;
2895 }
2896 if ((*typep)->tt_type == VAR_DICT)
2897 {
2898 *typep = (*typep)->tt_member;
2899 if (*typep == &t_unknown)
2900 // empty dict was used
2901 *typep = &t_any;
2902 }
2903 else
2904 {
2905 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2906 FALSE, FALSE) == FAIL)
2907 return FAIL;
2908 *typep = &t_any;
2909 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002910 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002911 return FAIL;
2912 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2913 return FAIL;
2914 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002915 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002916 {
2917 *typep = &t_string;
2918 if ((is_slice
2919 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2920 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2921 return FAIL;
2922 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002923 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002924 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002925 if (is_slice)
2926 {
2927 *typep = &t_blob;
2928 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2929 return FAIL;
2930 }
2931 else
2932 {
2933 *typep = &t_number;
2934 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2935 return FAIL;
2936 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002937 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002939 {
2940 if (is_slice)
2941 {
2942 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002943 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002944 2) == FAIL)
2945 return FAIL;
2946 }
2947 else
2948 {
2949 if ((*typep)->tt_type == VAR_LIST)
2950 {
2951 *typep = (*typep)->tt_member;
2952 if (*typep == &t_unknown)
2953 // empty list was used
2954 *typep = &t_any;
2955 }
2956 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002957 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2958 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002959 return FAIL;
2960 }
2961 }
2962 else
2963 {
2964 emsg(_(e_string_list_dict_or_blob_required));
2965 return FAIL;
2966 }
2967 return OK;
2968}
2969
2970/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002971 * Generate an instruction to load script-local variable "name", without the
2972 * leading "s:".
2973 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 */
2975 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002976compile_load_scriptvar(
2977 cctx_T *cctx,
2978 char_u *name, // variable NUL terminated
2979 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002980 char_u **end, // end of variable
2981 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002983 scriptitem_T *si;
2984 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 imported_T *import;
2986
Bram Moolenaare3d46852020-08-29 13:39:17 +02002987 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2988 return FAIL;
2989 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002990 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002991 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002993 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002994 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2995 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 }
2997 if (idx >= 0)
2998 {
2999 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3000
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003001 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 current_sctx.sc_sid, idx, sv->sv_type);
3003 return OK;
3004 }
3005
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003006 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 if (import != NULL)
3008 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003009 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003010 {
3011 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003012 char_u *exp_name;
3013 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003014 ufunc_T *ufunc;
3015 type_T *type;
3016
3017 // Used "import * as Name", need to lookup the member.
3018 if (*p != '.')
3019 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003020 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003021 return FAIL;
3022 }
3023 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003024 if (VIM_ISWHITE(*p))
3025 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003026 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003027 return FAIL;
3028 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003029
Bram Moolenaar1c991142020-07-04 13:15:31 +02003030 // isolate one name
3031 exp_name = p;
3032 while (eval_isnamec(*p))
3033 ++p;
3034 cc = *p;
3035 *p = NUL;
3036
Bram Moolenaaredba7072021-03-13 21:14:18 +01003037 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3038 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003039 *p = cc;
3040 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003041 *end = p;
3042
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003043 if (idx < 0)
3044 {
3045 if (*p == '(' && ufunc != NULL)
3046 {
3047 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3048 return OK;
3049 }
3050 return FAIL;
3051 }
3052
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003053 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3054 import->imp_sid,
3055 idx,
3056 type);
3057 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003058 else if (import->imp_funcname != NULL)
3059 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003060 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003061 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3062 import->imp_sid,
3063 import->imp_var_vals_idx,
3064 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 return OK;
3066 }
3067
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003068 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003069 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 return FAIL;
3071}
3072
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003073 static int
3074generate_funcref(cctx_T *cctx, char_u *name)
3075{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003076 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003077
3078 if (ufunc == NULL)
3079 return FAIL;
3080
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003081 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003082 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3083 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003084 == FAIL)
3085 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003086 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003087}
3088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089/*
3090 * Compile a variable name into a load instruction.
3091 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003092 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 * When "error" is FALSE do not give an error when not found.
3094 */
3095 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003096compile_load(
3097 char_u **arg,
3098 char_u *end_arg,
3099 cctx_T *cctx,
3100 int is_expr,
3101 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102{
3103 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003104 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003105 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003107 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108
3109 if (*(*arg + 1) == ':')
3110 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003111 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 {
3113 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114
Bram Moolenaarfa596382021-04-07 21:58:16 +02003115 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003116 switch (**arg)
3117 {
3118 case 'g': isn_type = ISN_LOADGDICT; break;
3119 case 'w': isn_type = ISN_LOADWDICT; break;
3120 case 't': isn_type = ISN_LOADTDICT; break;
3121 case 'b': isn_type = ISN_LOADBDICT; break;
3122 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003123 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003124 goto theend;
3125 }
3126 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3127 goto theend;
3128 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 else
3131 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003132 isntype_T isn_type = ISN_DROP;
3133
Bram Moolenaarfa596382021-04-07 21:58:16 +02003134 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003135 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3136 if (name == NULL)
3137 return FAIL;
3138
3139 switch (**arg)
3140 {
3141 case 'v': res = generate_LOADV(cctx, name, error);
3142 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003143 case 's': if (is_expr && ASCII_ISUPPER(*name)
3144 && find_func(name, FALSE, cctx) != NULL)
3145 res = generate_funcref(cctx, name);
3146 else
3147 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003148 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003149 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003150 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003151 {
3152 if (is_expr && ASCII_ISUPPER(*name)
3153 && find_func(name, FALSE, cctx) != NULL)
3154 res = generate_funcref(cctx, name);
3155 else
3156 isn_type = ISN_LOADG;
3157 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003158 else
3159 {
3160 isn_type = ISN_LOADAUTO;
3161 vim_free(name);
3162 name = vim_strnsave(*arg, end - *arg);
3163 if (name == NULL)
3164 return FAIL;
3165 }
3166 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003167 case 'w': isn_type = ISN_LOADW; break;
3168 case 't': isn_type = ISN_LOADT; break;
3169 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003170 default: // cannot happen, just in case
3171 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003172 goto theend;
3173 }
3174 if (isn_type != ISN_DROP)
3175 {
3176 // Global, Buffer-local, Window-local and Tabpage-local
3177 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003178 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003179 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 }
3182 }
3183 else
3184 {
3185 size_t len = end - *arg;
3186 int idx;
3187 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003188 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189
3190 name = vim_strnsave(*arg, end - *arg);
3191 if (name == NULL)
3192 return FAIL;
3193
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003194 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3195 {
3196 script_autoload(name, FALSE);
3197 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3198 }
3199 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3200 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003202 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003203 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
3205 else
3206 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003207 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003208
Bram Moolenaar709664c2020-12-12 14:33:41 +01003209 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003211 type = lvar.lv_type;
3212 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003213 if (lvar.lv_from_outer != 0)
3214 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003215 else
3216 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 }
3218 else
3219 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003220 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003221 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003222 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003223 || find_imported(name, 0, cctx) != NULL)
3224 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003225
Bram Moolenaar0f769812020-09-12 18:32:34 +02003226 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003227 // uppercase letter it can be a user defined function.
3228 // generate_funcref() will fail if the function can't be found.
3229 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003230 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 }
3233 if (gen_load)
3234 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003235 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003236 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003237 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003238 cctx->ctx_outer_used = TRUE;
3239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 }
3241
3242 *arg = end;
3243
3244theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003245 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003246 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 vim_free(name);
3248 return res;
3249}
3250
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003251 static void
3252clear_instr_ga(garray_T *gap)
3253{
3254 int idx;
3255
3256 for (idx = 0; idx < gap->ga_len; ++idx)
3257 delete_instr(((isn_T *)gap->ga_data) + idx);
3258 ga_clear(gap);
3259}
3260
3261/*
3262 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3263 * Returns FAIL if compilation fails.
3264 */
3265 static int
3266compile_string(isn_T *isn, cctx_T *cctx)
3267{
3268 char_u *s = isn->isn_arg.string;
3269 garray_T save_ga = cctx->ctx_instr;
3270 int expr_res;
3271 int trailing_error;
3272 int instr_count;
3273 isn_T *instr = NULL;
3274
Bram Moolenaarcd268012021-07-22 19:11:08 +02003275 // Remove the string type from the stack.
3276 --cctx->ctx_type_stack.ga_len;
3277
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003278 // Temporarily reset the list of instructions so that the jump labels are
3279 // correct.
3280 cctx->ctx_instr.ga_len = 0;
3281 cctx->ctx_instr.ga_maxlen = 0;
3282 cctx->ctx_instr.ga_data = NULL;
3283 expr_res = compile_expr0(&s, cctx);
3284 s = skipwhite(s);
3285 trailing_error = *s != NUL;
3286
Bram Moolenaarff652882021-05-16 15:24:49 +02003287 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003288 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003289 {
3290 if (trailing_error)
3291 semsg(_(e_trailing_arg), s);
3292 clear_instr_ga(&cctx->ctx_instr);
3293 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003294 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003295 return FAIL;
3296 }
3297
3298 // Move the generated instructions into the ISN_INSTR instruction, then
3299 // restore the list of instructions.
3300 instr_count = cctx->ctx_instr.ga_len;
3301 instr = cctx->ctx_instr.ga_data;
3302 instr[instr_count].isn_type = ISN_FINISH;
3303
3304 cctx->ctx_instr = save_ga;
3305 vim_free(isn->isn_arg.string);
3306 isn->isn_type = ISN_INSTR;
3307 isn->isn_arg.instr = instr;
3308 return OK;
3309}
3310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311/*
3312 * Compile the argument expressions.
3313 * "arg" points to just after the "(" and is advanced to after the ")"
3314 */
3315 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003318 char_u *p = *arg;
3319 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003320 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003321 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322
Bram Moolenaare6085c52020-04-12 20:19:16 +02003323 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003325 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3326 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003327 if (*p == ')')
3328 {
3329 *arg = p + 1;
3330 return OK;
3331 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003332 if (must_end)
3333 {
3334 semsg(_(e_missing_comma_before_argument_str), p);
3335 return FAIL;
3336 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003337
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003338 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003339 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 return FAIL;
3341 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003342
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003343 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003344 && cctx->ctx_instr.ga_len == instr_count + 1)
3345 {
3346 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3347
3348 // {skip} argument of searchpair() can be compiled if not empty
3349 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3350 compile_string(isn, cctx);
3351 }
3352
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003353 if (*p != ',' && *skipwhite(p) == ',')
3354 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003355 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003356 p = skipwhite(p);
3357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003359 {
3360 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003361 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003362 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003363 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003364 else
3365 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003366 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003367 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003369failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003370 emsg(_(e_missing_close));
3371 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372}
3373
3374/*
3375 * Compile a function call: name(arg1, arg2)
3376 * "arg" points to "name", "arg + varlen" to the "(".
3377 * "argcount_init" is 1 for "value->method()"
3378 * Instructions:
3379 * EVAL arg1
3380 * EVAL arg2
3381 * BCALL / DCALL / UCALL
3382 */
3383 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003384compile_call(
3385 char_u **arg,
3386 size_t varlen,
3387 cctx_T *cctx,
3388 ppconst_T *ppconst,
3389 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390{
3391 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003392 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 int argcount = argcount_init;
3394 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003395 char_u fname_buf[FLEN_FIXED + 1];
3396 char_u *tofree = NULL;
3397 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003398 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003399 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003400 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003401 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003403 // We can evaluate "has('name')" at compile time.
3404 // We can evaluate some "exists()" values at compile time.
3405 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3406 || (varlen == 6 && STRNCMP(*arg, "exists", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 {
3408 char_u *s = skipwhite(*arg + varlen + 1);
3409 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003410 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003411
3412 argvars[0].v_type = VAR_UNKNOWN;
3413 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003414 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003415 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003416 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003417 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003418 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003419 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
3420 || (!is_has && (*argvars[0].vval.v_string == '+'
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003421 || *argvars[0].vval.v_string == '&'))))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003422 {
3423 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3424
3425 *arg = s + 1;
3426 argvars[1].v_type = VAR_UNKNOWN;
3427 tv->v_type = VAR_NUMBER;
3428 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003429 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003430 f_has(argvars, tv);
3431 else
3432 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003433 clear_tv(&argvars[0]);
3434 ++ppconst->pp_used;
3435 return OK;
3436 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003437 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003438 }
3439
3440 if (generate_ppconst(cctx, ppconst) == FAIL)
3441 return FAIL;
3442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 if (varlen >= sizeof(namebuf))
3444 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003445 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 return FAIL;
3447 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003448 vim_strncpy(namebuf, *arg, varlen);
3449 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003451 // We handle the "skip" argument of searchpair() and searchpairpos()
3452 // differently.
3453 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3454 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3455 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3456 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003459 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003460 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461
Bram Moolenaar03290b82020-12-19 16:30:44 +01003462 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003463 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 {
3465 int idx;
3466
3467 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003468 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003470 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003471 if (STRCMP(name, "flatten") == 0)
3472 {
3473 emsg(_(e_cannot_use_flatten_in_vim9_script));
3474 goto theend;
3475 }
3476
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003477 if (STRCMP(name, "add") == 0 && argcount == 2)
3478 {
3479 garray_T *stack = &cctx->ctx_type_stack;
3480 type_T *type = ((type_T **)stack->ga_data)[
3481 stack->ga_len - 2];
3482
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003483 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003484 if (type->tt_type == VAR_LIST)
3485 {
3486 // inline "add(list, item)" so that the type can be checked
3487 res = generate_LISTAPPEND(cctx);
3488 idx = -1;
3489 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003490 else if (type->tt_type == VAR_BLOB)
3491 {
3492 // inline "add(blob, nr)" so that the type can be checked
3493 res = generate_BLOBAPPEND(cctx);
3494 idx = -1;
3495 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003496 }
3497
3498 if (idx >= 0)
3499 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3500 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003501 else
3502 semsg(_(e_unknownfunc), namebuf);
3503 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003506 // An argument or local variable can be a function reference, this
3507 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003508 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003509 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003510 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003511 // If we can find the function by name generate the right call.
3512 // Skip global functions here, a local funcref takes precedence.
3513 ufunc = find_func(name, FALSE, cctx);
3514 if (ufunc != NULL && !func_is_global(ufunc))
3515 {
3516 res = generate_CALL(cctx, ufunc, argcount);
3517 goto theend;
3518 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003519 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520
3521 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003522 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003523 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003525 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003526 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003527 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003528 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003529 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003530
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003531 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003532 goto theend;
3533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534
Bram Moolenaar0f769812020-09-12 18:32:34 +02003535 // If we can find a global function by name generate the right call.
3536 if (ufunc != NULL)
3537 {
3538 res = generate_CALL(cctx, ufunc, argcount);
3539 goto theend;
3540 }
3541
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003542 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003543 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003544 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003545 res = generate_UCALL(cctx, name, argcount);
3546 else
3547 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003548
3549theend:
3550 vim_free(tofree);
3551 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552}
3553
3554// like NAMESPACE_CHAR but with 'a' and 'l'.
3555#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3556
3557/*
3558 * Find the end of a variable or function name. Unlike find_name_end() this
3559 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003560 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 * Return a pointer to just after the name. Equal to "arg" if there is no
3562 * valid name.
3563 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003564 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003565to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566{
3567 char_u *p;
3568
3569 // Quick check for valid starting character.
3570 if (!eval_isnamec1(*arg))
3571 return arg;
3572
3573 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3574 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3575 // and can be used in slice "[n:]".
3576 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003577 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3579 break;
3580 return p;
3581}
3582
3583/*
3584 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003585 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003586 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 */
3588 char_u *
3589to_name_const_end(char_u *arg)
3590{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003591 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 typval_T rettv;
3593
Bram Moolenaar678b2072021-07-26 21:10:11 +02003594 if (STRNCMP(p, "<SNR>", 5) == 0)
3595 p = skipdigits(p + 5);
3596 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 if (p == arg && *arg == '[')
3598 {
3599
3600 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003601 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 p = arg;
3603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 return p;
3605}
3606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607/*
3608 * parse a list: [expr, expr]
3609 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003610 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 */
3612 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003613compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614{
3615 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003616 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003618 int is_const;
3619 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003621 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003623 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003624 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003625 semsg(_(e_list_end), *arg);
3626 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003627 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003628 if (*p == ',')
3629 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003630 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003631 return FAIL;
3632 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003633 if (*p == ']')
3634 {
3635 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003636 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003637 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003638 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003639 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003640 if (!is_const)
3641 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 ++count;
3643 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003644 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003646 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3647 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003648 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003649 return FAIL;
3650 }
3651 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003652 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 p = skipwhite(p);
3654 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003655 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003657 ppconst->pp_is_const = is_all_const;
3658 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659}
3660
3661/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003662 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003663 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003664 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 */
3666 static int
3667compile_lambda(char_u **arg, cctx_T *cctx)
3668{
Bram Moolenaare462f522020-12-27 14:43:30 +01003669 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 typval_T rettv;
3671 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003672 evalarg_T evalarg;
3673
3674 CLEAR_FIELD(evalarg);
3675 evalarg.eval_flags = EVAL_EVALUATE;
3676 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
3678 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003679 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3680 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003681 {
3682 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003683 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003684 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003685
Bram Moolenaar65c44152020-12-24 15:14:01 +01003686 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003688 ++ufunc->uf_refcount;
3689 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690
Bram Moolenaara9931532021-06-12 15:58:16 +02003691 // Compile it here to get the return type. The return type is optional,
3692 // when it's missing use t_unknown. This is recognized in
3693 // compile_return().
3694 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3695 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003696 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697
Bram Moolenaar648594e2021-07-11 17:55:01 +02003698#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003699 // When the outer function is compiled for profiling, the lambda may be
3700 // called without profiling. Compile it here in the right context.
3701 if (cctx->ctx_compile_type == CT_PROFILE)
3702 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003703#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003704
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003705 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3706 // points into it. Point to the original line to avoid a dangling pointer.
3707 if (evalarg.eval_tofree_cmdline != NULL)
3708 {
3709 size_t off = *arg - evalarg.eval_tofree_cmdline;
3710
3711 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3712 + off;
3713 }
3714
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003715 clear_evalarg(&evalarg, NULL);
3716
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003717 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003718 {
3719 // The return type will now be known.
3720 set_function_type(ufunc);
3721
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003722 // The function reference count will be 1. When the ISN_FUNCREF
3723 // instruction is deleted the reference count is decremented and the
3724 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003725 return generate_FUNCREF(cctx, ufunc);
3726 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003727
3728 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 return FAIL;
3730}
3731
3732/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003733 * Get a lambda and compile it. Uses Vim9 syntax.
3734 */
3735 int
3736get_lambda_tv_and_compile(
3737 char_u **arg,
3738 typval_T *rettv,
3739 int types_optional,
3740 evalarg_T *evalarg)
3741{
3742 int r;
3743 ufunc_T *ufunc;
3744 int save_sc_version = current_sctx.sc_version;
3745
3746 // Get the funcref in "rettv".
3747 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3748 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3749 current_sctx.sc_version = save_sc_version;
3750 if (r != OK)
3751 return r;
3752
3753 // "rettv" will now be a partial referencing the function.
3754 ufunc = rettv->vval.v_partial->pt_func;
3755
3756 // Compile it here to get the return type. The return type is optional,
3757 // when it's missing use t_unknown. This is recognized in
3758 // compile_return().
3759 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3760 ufunc->uf_ret_type = &t_unknown;
3761 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3762
3763 if (ufunc->uf_def_status == UF_COMPILED)
3764 {
3765 // The return type will now be known.
3766 set_function_type(ufunc);
3767 return OK;
3768 }
3769 clear_tv(rettv);
3770 return FAIL;
3771}
3772
3773/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003774 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003776 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 */
3778 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003779compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780{
3781 garray_T *instr = &cctx->ctx_instr;
3782 int count = 0;
3783 dict_T *d = dict_alloc();
3784 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003785 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003786 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003787 int is_const;
3788 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 if (d == NULL)
3791 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003792 if (generate_ppconst(cctx, ppconst) == FAIL)
3793 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003794 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003796 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
Bram Moolenaar23c55272020-06-21 16:58:13 +02003798 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003799 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003800 *arg = NULL;
3801 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003802 }
3803
3804 if (**arg == '}')
3805 break;
3806
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003807 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003809 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003811 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003812 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003813 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003816 if (isn->isn_type == ISN_PUSHNR)
3817 {
3818 char buf[NUMBUFLEN];
3819
3820 // Convert to string at compile time.
3821 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3822 isn->isn_type = ISN_PUSHS;
3823 isn->isn_arg.string = vim_strsave((char_u *)buf);
3824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 if (isn->isn_type == ISN_PUSHS)
3826 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003827 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003828 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003829 *arg = skipwhite(*arg);
3830 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003831 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003832 emsg(_(e_missing_matching_bracket_after_dict_key));
3833 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003834 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003835 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003837 else
3838 {
3839 // {"name": value},
3840 // {'name': value},
3841 // {name: value} use "name" as a literal key
3842 key = get_literal_key(arg);
3843 if (key == NULL)
3844 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003845 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003846 return FAIL;
3847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
3849 // Check for duplicate keys, if using string keys.
3850 if (key != NULL)
3851 {
3852 item = dict_find(d, key, -1);
3853 if (item != NULL)
3854 {
3855 semsg(_(e_duplicate_key), key);
3856 goto failret;
3857 }
3858 item = dictitem_alloc(key);
3859 if (item != NULL)
3860 {
3861 item->di_tv.v_type = VAR_UNKNOWN;
3862 item->di_tv.v_lock = 0;
3863 if (dict_add(d, item) == FAIL)
3864 dictitem_free(item);
3865 }
3866 }
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 if (**arg != ':')
3869 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003870 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003871 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003872 else
3873 semsg(_(e_missing_dict_colon), *arg);
3874 return FAIL;
3875 }
3876 whitep = *arg + 1;
3877 if (!IS_WHITE_OR_NUL(*whitep))
3878 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003879 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 return FAIL;
3881 }
3882
Bram Moolenaar23c55272020-06-21 16:58:13 +02003883 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003884 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003885 *arg = NULL;
3886 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003887 }
3888
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003889 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003891 if (!is_const)
3892 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 ++count;
3894
Bram Moolenaar2c330432020-04-13 14:41:35 +02003895 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003896 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003897 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003898 *arg = NULL;
3899 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 if (**arg == '}')
3902 break;
3903 if (**arg != ',')
3904 {
3905 semsg(_(e_missing_dict_comma), *arg);
3906 goto failret;
3907 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003908 if (IS_WHITE_OR_NUL(*whitep))
3909 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003910 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003911 return FAIL;
3912 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003913 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003914 if (!IS_WHITE_OR_NUL(*whitep))
3915 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003916 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003917 return FAIL;
3918 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003919 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 }
3921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 *arg = *arg + 1;
3923
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003924 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003925 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003926 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003927 *arg += STRLEN(*arg);
3928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003930 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 return generate_NEWDICT(cctx, count);
3932
3933failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003934 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003935 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003936 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003937 *arg = (char_u *)"";
3938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 dict_unref(d);
3940 return FAIL;
3941}
3942
3943/*
3944 * Compile "&option".
3945 */
3946 static int
3947compile_get_option(char_u **arg, cctx_T *cctx)
3948{
3949 typval_T rettv;
3950 char_u *start = *arg;
3951 int ret;
3952
3953 // parse the option and get the current value to get the type.
3954 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003955 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 if (ret == OK)
3957 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003958 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003959 char_u *name = vim_strnsave(start, *arg - start);
3960 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3961 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3964 vim_free(name);
3965 }
3966 clear_tv(&rettv);
3967
3968 return ret;
3969}
3970
3971/*
3972 * Compile "$VAR".
3973 */
3974 static int
3975compile_get_env(char_u **arg, cctx_T *cctx)
3976{
3977 char_u *start = *arg;
3978 int len;
3979 int ret;
3980 char_u *name;
3981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 ++*arg;
3983 len = get_env_len(arg);
3984 if (len == 0)
3985 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003986 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 return FAIL;
3988 }
3989
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003990 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 name = vim_strnsave(start, len + 1);
3992 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3993 vim_free(name);
3994 return ret;
3995}
3996
3997/*
3998 * Compile "@r".
3999 */
4000 static int
4001compile_get_register(char_u **arg, cctx_T *cctx)
4002{
4003 int ret;
4004
4005 ++*arg;
4006 if (**arg == NUL)
4007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004008 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 return FAIL;
4010 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004011 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 {
4013 emsg_invreg(**arg);
4014 return FAIL;
4015 }
4016 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4017 ++*arg;
4018 return ret;
4019}
4020
4021/*
4022 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004023 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 */
4025 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004026apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004028 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029
4030 // this works from end to start
4031 while (p > start)
4032 {
4033 --p;
4034 if (*p == '-' || *p == '+')
4035 {
4036 // only '-' has an effect, for '+' we only check the type
4037#ifdef FEAT_FLOAT
4038 if (rettv->v_type == VAR_FLOAT)
4039 {
4040 if (*p == '-')
4041 rettv->vval.v_float = -rettv->vval.v_float;
4042 }
4043 else
4044#endif
4045 {
4046 varnumber_T val;
4047 int error = FALSE;
4048
4049 // tv_get_number_chk() accepts a string, but we don't want that
4050 // here
4051 if (check_not_string(rettv) == FAIL)
4052 return FAIL;
4053 val = tv_get_number_chk(rettv, &error);
4054 clear_tv(rettv);
4055 if (error)
4056 return FAIL;
4057 if (*p == '-')
4058 val = -val;
4059 rettv->v_type = VAR_NUMBER;
4060 rettv->vval.v_number = val;
4061 }
4062 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004063 else if (numeric_only)
4064 {
4065 ++p;
4066 break;
4067 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004068 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 {
4070 int v = tv2bool(rettv);
4071
4072 // '!' is permissive in the type.
4073 clear_tv(rettv);
4074 rettv->v_type = VAR_BOOL;
4075 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4076 }
4077 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004078 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 return OK;
4080}
4081
4082/*
4083 * Recognize v: variables that are constants and set "rettv".
4084 */
4085 static void
4086get_vim_constant(char_u **arg, typval_T *rettv)
4087{
4088 if (STRNCMP(*arg, "v:true", 6) == 0)
4089 {
4090 rettv->v_type = VAR_BOOL;
4091 rettv->vval.v_number = VVAL_TRUE;
4092 *arg += 6;
4093 }
4094 else if (STRNCMP(*arg, "v:false", 7) == 0)
4095 {
4096 rettv->v_type = VAR_BOOL;
4097 rettv->vval.v_number = VVAL_FALSE;
4098 *arg += 7;
4099 }
4100 else if (STRNCMP(*arg, "v:null", 6) == 0)
4101 {
4102 rettv->v_type = VAR_SPECIAL;
4103 rettv->vval.v_number = VVAL_NULL;
4104 *arg += 6;
4105 }
4106 else if (STRNCMP(*arg, "v:none", 6) == 0)
4107 {
4108 rettv->v_type = VAR_SPECIAL;
4109 rettv->vval.v_number = VVAL_NONE;
4110 *arg += 6;
4111 }
4112}
4113
Bram Moolenaar657137c2021-01-09 15:45:23 +01004114 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004115get_compare_type(char_u *p, int *len, int *type_is)
4116{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004117 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004118 int i;
4119
4120 switch (p[0])
4121 {
4122 case '=': if (p[1] == '=')
4123 type = EXPR_EQUAL;
4124 else if (p[1] == '~')
4125 type = EXPR_MATCH;
4126 break;
4127 case '!': if (p[1] == '=')
4128 type = EXPR_NEQUAL;
4129 else if (p[1] == '~')
4130 type = EXPR_NOMATCH;
4131 break;
4132 case '>': if (p[1] != '=')
4133 {
4134 type = EXPR_GREATER;
4135 *len = 1;
4136 }
4137 else
4138 type = EXPR_GEQUAL;
4139 break;
4140 case '<': if (p[1] != '=')
4141 {
4142 type = EXPR_SMALLER;
4143 *len = 1;
4144 }
4145 else
4146 type = EXPR_SEQUAL;
4147 break;
4148 case 'i': if (p[1] == 's')
4149 {
4150 // "is" and "isnot"; but not a prefix of a name
4151 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4152 *len = 5;
4153 i = p[*len];
4154 if (!isalnum(i) && i != '_')
4155 {
4156 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4157 *type_is = TRUE;
4158 }
4159 }
4160 break;
4161 }
4162 return type;
4163}
4164
4165/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004166 * Skip over an expression, ignoring most errors.
4167 */
4168 static void
4169skip_expr_cctx(char_u **arg, cctx_T *cctx)
4170{
4171 evalarg_T evalarg;
4172
4173 CLEAR_FIELD(evalarg);
4174 evalarg.eval_cctx = cctx;
4175 skip_expr(arg, &evalarg);
4176}
4177
4178/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004180 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 */
4182 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004183compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004185 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186
4187 // this works from end to start
4188 while (p > start)
4189 {
4190 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004191 while (VIM_ISWHITE(*p))
4192 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 if (*p == '-' || *p == '+')
4194 {
4195 int negate = *p == '-';
4196 isn_T *isn;
4197
4198 // TODO: check type
4199 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4200 {
4201 --p;
4202 if (*p == '-')
4203 negate = !negate;
4204 }
4205 // only '-' has an effect, for '+' we only check the type
4206 if (negate)
4207 isn = generate_instr(cctx, ISN_NEGATENR);
4208 else
4209 isn = generate_instr(cctx, ISN_CHECKNR);
4210 if (isn == NULL)
4211 return FAIL;
4212 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004213 else if (numeric_only)
4214 {
4215 ++p;
4216 break;
4217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 else
4219 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004220 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004222 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004224 if (p[-1] == '!')
4225 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004228 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 return FAIL;
4230 }
4231 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004232 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return OK;
4234}
4235
4236/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004237 * Compile "(expression)": recursive!
4238 * Return FAIL/OK.
4239 */
4240 static int
4241compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4242{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004243 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004244 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004245
Bram Moolenaar24156692021-01-14 20:35:49 +01004246 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4247 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004248 if (ppconst->pp_used <= PPSIZE - 10)
4249 {
4250 ret = compile_expr1(arg, cctx, ppconst);
4251 }
4252 else
4253 {
4254 // Not enough space in ppconst, flush constants.
4255 if (generate_ppconst(cctx, ppconst) == FAIL)
4256 return FAIL;
4257 ret = compile_expr0(arg, cctx);
4258 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004259 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4260 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004261 if (**arg == ')')
4262 ++*arg;
4263 else if (ret == OK)
4264 {
4265 emsg(_(e_missing_close));
4266 ret = FAIL;
4267 }
4268 return ret;
4269}
4270
4271/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004273 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 */
4275 static int
4276compile_subscript(
4277 char_u **arg,
4278 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004279 char_u *start_leader,
4280 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004281 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004283 char_u *name_start = *end_leader;
4284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 for (;;)
4286 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004287 char_u *p = skipwhite(*arg);
4288
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004289 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004290 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004291 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004292
4293 // If a following line starts with "->{" or "->X" advance to that
4294 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004295 // Also if a following line starts with ".x".
4296 if (next != NULL &&
4297 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004298 && (next[2] == '{'
4299 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004300 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004301 {
4302 next = next_line_from_context(cctx, TRUE);
4303 if (next == NULL)
4304 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004305 *arg = next;
4306 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004307 }
4308 }
4309
Bram Moolenaarcd268012021-07-22 19:11:08 +02004310 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4311 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004312 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004314 garray_T *stack = &cctx->ctx_type_stack;
4315 type_T *type;
4316 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004318 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004319 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004320 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004323 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4324
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004325 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004326 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004328 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 return FAIL;
4330 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004331 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004333 char_u *pstart = p;
4334
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004335 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004336 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004337 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 // something->method()
4340 // Apply the '!', '-' and '+' first:
4341 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004342 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004345 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004346 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004347 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004348 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004349 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004350 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004351 garray_T *stack = &cctx->ctx_type_stack;
4352 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004353 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004354 int expr_isn_start = cctx->ctx_instr.ga_len;
4355 int expr_isn_end;
4356 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004357
4358 // Funcref call: list->(Refs[2])(arg)
4359 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004360 //
4361 // Fist compile the function expression.
4362 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004363 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004364
4365 // Remember the next instruction index, where the instructions
4366 // for arguments are being written.
4367 expr_isn_end = cctx->ctx_instr.ga_len;
4368
4369 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004370 if (**arg != '(')
4371 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004372 if (*skipwhite(*arg) == '(')
4373 emsg(_(e_nowhitespace));
4374 else
4375 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004376 return FAIL;
4377 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004378 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004379 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004380 return FAIL;
4381
Bram Moolenaar2927c072021-04-05 19:41:21 +02004382 // Move the instructions for the arguments to before the
4383 // instructions of the expression and move the type of the
4384 // expression after the argument types. This is what ISN_PCALL
4385 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004386 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004387 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4388 if (arg_isn_count > 0)
4389 {
4390 int expr_isn_count = expr_isn_end - expr_isn_start;
4391 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4392
4393 if (isn == NULL)
4394 return FAIL;
4395 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4396 + expr_isn_start,
4397 sizeof(isn_T) * expr_isn_count);
4398 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4399 + expr_isn_start,
4400 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4401 sizeof(isn_T) * arg_isn_count);
4402 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4403 + expr_isn_start + arg_isn_count,
4404 isn, sizeof(isn_T) * expr_isn_count);
4405 vim_free(isn);
4406
4407 type = ((type_T **)stack->ga_data)[type_idx_start];
4408 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4409 ((type_T **)stack->ga_data) + type_idx_start + 1,
4410 sizeof(type_T *)
4411 * (stack->ga_len - type_idx_start - 1));
4412 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4413 }
4414
Bram Moolenaar7e368202020-12-25 21:56:57 +01004415 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004416 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 return FAIL;
4418 }
4419 else
4420 {
4421 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004422 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004423 if (!eval_isnamec1(*p))
4424 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004425 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004426 return FAIL;
4427 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004428 if (ASCII_ISALPHA(*p) && p[1] == ':')
4429 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004430 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 ;
4432 if (*p != '(')
4433 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004434 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 return FAIL;
4436 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 return FAIL;
4439 }
4440 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004441 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004443 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004444
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004445 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004446 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004447 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004448 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004449 // TODO: more arguments
4450 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004451 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004452 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004453 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004454
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004455 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004456 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004457 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004458 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004459 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004460 // missing first index is equal to zero
4461 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004462 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004463 else
4464 {
4465 if (compile_expr0(arg, cctx) == FAIL)
4466 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004467 if (**arg == ':')
4468 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004469 semsg(_(e_white_space_required_before_and_after_str_at_str),
4470 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004471 return FAIL;
4472 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004473 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004474 return FAIL;
4475 *arg = skipwhite(*arg);
4476 }
4477 if (**arg == ':')
4478 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004479 is_slice = TRUE;
4480 ++*arg;
4481 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4482 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004483 semsg(_(e_white_space_required_before_and_after_str_at_str),
4484 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004485 return FAIL;
4486 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004487 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004488 return FAIL;
4489 if (**arg == ']')
4490 // missing second index is equal to end of string
4491 generate_PUSHNR(cctx, -1);
4492 else
4493 {
4494 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004495 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004496 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004497 return FAIL;
4498 *arg = skipwhite(*arg);
4499 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501
4502 if (**arg != ']')
4503 {
4504 emsg(_(e_missbrac));
4505 return FAIL;
4506 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004507 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaare42939a2021-04-05 17:11:17 +02004509 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004510 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004512 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004514 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004515 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004516 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004517 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004518
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004519 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004520 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004521 {
4522 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004523 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004524 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004525 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004526 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 while (eval_isnamec(*p))
4528 MB_PTR_ADV(p);
4529 if (p == *arg)
4530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004531 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 return FAIL;
4533 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004534 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 return FAIL;
4536 *arg = p;
4537 }
4538 else
4539 break;
4540 }
4541
4542 // TODO - see handle_subscript():
4543 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4544 // Don't do this when "Func" is already a partial that was bound
4545 // explicitly (pt_auto is FALSE).
4546
4547 return OK;
4548}
4549
4550/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004551 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4552 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004554 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004555 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004556 *
4557 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 */
4559
4560/*
4561 * number number constant
4562 * 0zFFFFFFFF Blob constant
4563 * "string" string constant
4564 * 'string' literal string constant
4565 * &option-name option value
4566 * @r register contents
4567 * identifier variable value
4568 * function() function call
4569 * $VAR environment variable
4570 * (expression) nested expression
4571 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004572 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 *
4574 * Also handle:
4575 * ! in front logical NOT
4576 * - in front unary minus
4577 * + in front unary plus (ignored)
4578 * trailing (arg) funcref/partial call
4579 * trailing [] subscript in String or List
4580 * trailing .name entry in Dictionary
4581 * trailing ->name() method call
4582 */
4583 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004584compile_expr7(
4585 char_u **arg,
4586 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004587 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 char_u *start_leader, *end_leader;
4590 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004591 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004592 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004594 ppconst->pp_is_const = FALSE;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 /*
4597 * Skip '!', '-' and '+' characters. They are handled later.
4598 */
4599 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004600 if (eval_leader(arg, TRUE) == FAIL)
4601 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 end_leader = *arg;
4603
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004604 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 switch (**arg)
4606 {
4607 /*
4608 * Number constant.
4609 */
4610 case '0': // also for blob starting with 0z
4611 case '1':
4612 case '2':
4613 case '3':
4614 case '4':
4615 case '5':
4616 case '6':
4617 case '7':
4618 case '8':
4619 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004620 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004622 // Apply "-" and "+" just before the number now, right to
4623 // left. Matters especially when "->" follows. Stops at
4624 // '!'.
4625 if (apply_leader(rettv, TRUE,
4626 start_leader, &end_leader) == FAIL)
4627 {
4628 clear_tv(rettv);
4629 return FAIL;
4630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 break;
4632
4633 /*
4634 * String constant: "string".
4635 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004636 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 return FAIL;
4638 break;
4639
4640 /*
4641 * Literal string constant: 'str''ing'.
4642 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004643 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 return FAIL;
4645 break;
4646
4647 /*
4648 * Constant Vim variable.
4649 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004650 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 ret = NOTDONE;
4652 break;
4653
4654 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 * "true" constant
4656 */
4657 case 't': if (STRNCMP(*arg, "true", 4) == 0
4658 && !eval_isnamec((*arg)[4]))
4659 {
4660 *arg += 4;
4661 rettv->v_type = VAR_BOOL;
4662 rettv->vval.v_number = VVAL_TRUE;
4663 }
4664 else
4665 ret = NOTDONE;
4666 break;
4667
4668 /*
4669 * "false" constant
4670 */
4671 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4672 && !eval_isnamec((*arg)[5]))
4673 {
4674 *arg += 5;
4675 rettv->v_type = VAR_BOOL;
4676 rettv->vval.v_number = VVAL_FALSE;
4677 }
4678 else
4679 ret = NOTDONE;
4680 break;
4681
4682 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004683 * "null" constant
4684 */
4685 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004686 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004687 {
4688 *arg += 4;
4689 rettv->v_type = VAR_SPECIAL;
4690 rettv->vval.v_number = VVAL_NULL;
4691 }
4692 else
4693 ret = NOTDONE;
4694 break;
4695
4696 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 * List: [expr, expr]
4698 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004699 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4700 return FAIL;
4701 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 break;
4703
4704 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 * Dictionary: {'key': val, 'key': val}
4706 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004707 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4708 return FAIL;
4709 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 break;
4711
4712 /*
4713 * Option value: &name
4714 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004715 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4716 return FAIL;
4717 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 break;
4719
4720 /*
4721 * Environment variable: $VAR.
4722 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004723 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4724 return FAIL;
4725 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 break;
4727
4728 /*
4729 * Register contents: @r.
4730 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004731 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4732 return FAIL;
4733 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 break;
4735 /*
4736 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004737 * lambda: (arg, arg) => expr
4738 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004740 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4741 ret = compile_lambda(arg, cctx);
4742 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004743 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 break;
4745
4746 default: ret = NOTDONE;
4747 break;
4748 }
4749 if (ret == FAIL)
4750 return FAIL;
4751
Bram Moolenaar1c747212020-05-09 18:28:34 +02004752 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004754 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 clear_tv(rettv);
4756 else
4757 // A constant expression can possibly be handled compile time,
4758 // return the value instead of generating code.
4759 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 }
4761 else if (ret == NOTDONE)
4762 {
4763 char_u *p;
4764 int r;
4765
4766 if (!eval_isnamec1(**arg))
4767 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004768 if (!vim9_bad_comment(*arg))
4769 {
4770 if (ends_excmd(*skipwhite(*arg)))
4771 semsg(_(e_empty_expression_str), *arg);
4772 else
4773 semsg(_(e_name_expected_str), *arg);
4774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 return FAIL;
4776 }
4777
4778 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004779 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004780 if (p - *arg == (size_t)1 && **arg == '_')
4781 {
4782 emsg(_(e_cannot_use_underscore_here));
4783 return FAIL;
4784 }
4785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787 {
4788 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 {
4792 if (generate_ppconst(cctx, ppconst) == FAIL)
4793 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004794 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 if (r == FAIL)
4797 return FAIL;
4798 }
4799
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004800 // Handle following "[]", ".member", etc.
4801 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004802 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803 ppconst) == FAIL)
4804 return FAIL;
4805 if (ppconst->pp_used > 0)
4806 {
4807 // apply the '!', '-' and '+' before the constant
4808 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004809 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004810 return FAIL;
4811 return OK;
4812 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004813 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004815 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816}
4817
4818/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004819 * Give the "white on both sides" error, taking the operator from "p[len]".
4820 */
4821 void
4822error_white_both(char_u *op, int len)
4823{
4824 char_u buf[10];
4825
4826 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004827 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004828}
4829
4830/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004831 * <type>expr7: runtime type check / conversion
4832 */
4833 static int
4834compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4835{
4836 type_T *want_type = NULL;
4837
4838 // Recognize <type>
4839 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4840 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004841 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004842 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4843 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004844 return FAIL;
4845
4846 if (**arg != '>')
4847 {
4848 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004849 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004850 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004851 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004852 return FAIL;
4853 }
4854 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004855 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004856 return FAIL;
4857 }
4858
4859 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4860 return FAIL;
4861
4862 if (want_type != NULL)
4863 {
4864 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004865 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004866 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004867
Bram Moolenaard1103582020-08-14 22:44:25 +02004868 generate_ppconst(cctx, ppconst);
4869 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004870 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004871 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004872 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004873 return FAIL;
4874 }
4875 }
4876
4877 return OK;
4878}
4879
4880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 * * number multiplication
4882 * / number division
4883 * % number modulo
4884 */
4885 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004886compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887{
4888 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004889 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004890 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004892 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004893 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 return FAIL;
4895
4896 /*
4897 * Repeat computing, until no "*", "/" or "%" is following.
4898 */
4899 for (;;)
4900 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004901 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 if (*op != '*' && *op != '/' && *op != '%')
4903 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004904 if (next != NULL)
4905 {
4906 *arg = next_line_from_context(cctx, TRUE);
4907 op = skipwhite(*arg);
4908 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004909
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004910 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004912 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004913 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004915 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004918 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004919 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004921
4922 if (ppconst->pp_used == ppconst_used + 2
4923 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4924 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004925 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004926 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4927 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4928 varnumber_T res = 0;
4929 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004931 // both are numbers: compute the result
4932 switch (*op)
4933 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004934 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004935 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004936 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004937 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004938 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004939 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004940 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004941 break;
4942 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004943 if (failed)
4944 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004945 tv1->vval.v_number = res;
4946 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004947 }
4948 else
4949 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004950 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004951 generate_two_op(cctx, op);
4952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 }
4954
4955 return OK;
4956}
4957
4958/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004959 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 * - number subtraction
4961 * .. string concatenation
4962 */
4963 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004964compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965{
4966 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004967 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970
4971 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004972 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 return FAIL;
4974
4975 /*
4976 * Repeat computing, until no "+", "-" or ".." is following.
4977 */
4978 for (;;)
4979 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004980 op = may_peek_next_line(cctx, *arg, &next);
4981 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004983 if (op[0] == op[1] && *op != '.' && next)
4984 // Finding "++" or "--" on the next line is a separate command.
4985 // But ".." is concatenation.
4986 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004988 if (next != NULL)
4989 {
4990 *arg = next_line_from_context(cctx, TRUE);
4991 op = skipwhite(*arg);
4992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004994 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004996 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004997 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 }
4999
Bram Moolenaare0de1712020-12-02 17:36:54 +01005000 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005001 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005003 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 return FAIL;
5006
Bram Moolenaara5565e42020-05-09 15:44:01 +02005007 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005008 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005009 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5010 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5011 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5012 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005014 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5015 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005016
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005017 // concat/subtract/add constant numbers
5018 if (*op == '+')
5019 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5020 else if (*op == '-')
5021 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5022 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005023 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005024 // concatenate constant strings
5025 char_u *s1 = tv1->vval.v_string;
5026 char_u *s2 = tv2->vval.v_string;
5027 size_t len1 = STRLEN(s1);
5028
5029 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5030 if (tv1->vval.v_string == NULL)
5031 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005033 return FAIL;
5034 }
5035 mch_memmove(tv1->vval.v_string, s1, len1);
5036 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005037 vim_free(s1);
5038 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005039 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005040 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 }
5042 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005043 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005044 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005045 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005046 if (*op == '.')
5047 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005048 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5049 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005050 return FAIL;
5051 generate_instr_drop(cctx, ISN_CONCAT, 1);
5052 }
5053 else
5054 generate_two_op(cctx, op);
5055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
5057
5058 return OK;
5059}
5060
5061/*
5062 * expr5a == expr5b
5063 * expr5a =~ expr5b
5064 * expr5a != expr5b
5065 * expr5a !~ expr5b
5066 * expr5a > expr5b
5067 * expr5a >= expr5b
5068 * expr5a < expr5b
5069 * expr5a <= expr5b
5070 * expr5a is expr5b
5071 * expr5a isnot expr5b
5072 *
5073 * Produces instructions:
5074 * EVAL expr5a Push result of "expr5a"
5075 * EVAL expr5b Push result of "expr5b"
5076 * COMPARE one of the compare instructions
5077 */
5078 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005079compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005081 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005083 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005086 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087
5088 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005089 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 return FAIL;
5091
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005092 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005093 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
5095 /*
5096 * If there is a comparative operator, use it.
5097 */
5098 if (type != EXPR_UNKNOWN)
5099 {
5100 int ic = FALSE; // Default: do not ignore case
5101
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005102 if (next != NULL)
5103 {
5104 *arg = next_line_from_context(cctx, TRUE);
5105 p = skipwhite(*arg);
5106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 if (type_is && (p[len] == '?' || p[len] == '#'))
5108 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005109 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 return FAIL;
5111 }
5112 // extra question mark appended: ignore case
5113 if (p[len] == '?')
5114 {
5115 ic = TRUE;
5116 ++len;
5117 }
5118 // extra '#' appended: match case (ignored)
5119 else if (p[len] == '#')
5120 ++len;
5121 // nothing appended: match case
5122
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005123 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005125 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005126 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 }
5128
5129 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005130 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005131 return FAIL;
5132
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 return FAIL;
5135
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136 if (ppconst->pp_used == ppconst_used + 2)
5137 {
5138 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5139 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5140 int ret;
5141
5142 // Both sides are a constant, compute the result now.
5143 // First check for a valid combination of types, this is more
5144 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005145 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005146 ret = FAIL;
5147 else
5148 {
5149 ret = typval_compare(tv1, tv2, type, ic);
5150 tv1->v_type = VAR_BOOL;
5151 tv1->vval.v_number = tv1->vval.v_number
5152 ? VVAL_TRUE : VVAL_FALSE;
5153 clear_tv(tv2);
5154 --ppconst->pp_used;
5155 }
5156 return ret;
5157 }
5158
5159 generate_ppconst(cctx, ppconst);
5160 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 }
5162
5163 return OK;
5164}
5165
Bram Moolenaar7f141552020-05-09 17:35:53 +02005166static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168/*
5169 * Compile || or &&.
5170 */
5171 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005172compile_and_or(
5173 char_u **arg,
5174 cctx_T *cctx,
5175 char *op,
5176 ppconst_T *ppconst,
5177 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005179 char_u *next;
5180 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 int opchar = *op;
5182
5183 if (p[0] == opchar && p[1] == opchar)
5184 {
5185 garray_T *instr = &cctx->ctx_instr;
5186 garray_T end_ga;
5187
5188 /*
5189 * Repeat until there is no following "||" or "&&"
5190 */
5191 ga_init2(&end_ga, sizeof(int), 10);
5192 while (p[0] == opchar && p[1] == opchar)
5193 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005194 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005195 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005196 int start_ctx_lnum = cctx->ctx_lnum;
5197 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005198 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005199
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005200 if (next != NULL)
5201 {
5202 *arg = next_line_from_context(cctx, TRUE);
5203 p = skipwhite(*arg);
5204 }
5205
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005206 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5207 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005208 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005209 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005210 return FAIL;
5211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005213 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005214 SOURCING_LNUM = start_lnum;
5215 save_lnum = cctx->ctx_lnum;
5216 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005217
5218 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005219 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005220 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005221 // TODO: use ppconst if the value is a constant
5222 generate_ppconst(cctx, ppconst);
5223
5224 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005225 status = bool_on_stack(cctx);
5226 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005227 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005228 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005229 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005230 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 {
5232 ga_clear(&end_ga);
5233 return FAIL;
5234 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5237 ++end_ga.ga_len;
5238 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005239 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240
5241 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005242 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005243 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005244 {
5245 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005246 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005247 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005248
Bram Moolenaara5565e42020-05-09 15:44:01 +02005249 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5250 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 {
5252 ga_clear(&end_ga);
5253 return FAIL;
5254 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005255
5256 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005258
5259 if (check_ppconst_bool(ppconst) == FAIL)
5260 {
5261 ga_clear(&end_ga);
5262 return FAIL;
5263 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005264 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005266 // Every part must evaluate to a bool.
5267 if (bool_on_stack(cctx) == FAIL)
5268 {
5269 ga_clear(&end_ga);
5270 return FAIL;
5271 }
5272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273 // Fill in the end label in all jumps.
5274 while (end_ga.ga_len > 0)
5275 {
5276 isn_T *isn;
5277
5278 --end_ga.ga_len;
5279 isn = ((isn_T *)instr->ga_data)
5280 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5281 isn->isn_arg.jump.jump_where = instr->ga_len;
5282 }
5283 ga_clear(&end_ga);
5284 }
5285
5286 return OK;
5287}
5288
5289/*
5290 * expr4a && expr4a && expr4a logical AND
5291 *
5292 * Produces instructions:
5293 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005294 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005295 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005297 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 * EVAL expr4c Push result of "expr4c"
5299 * end:
5300 */
5301 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005302compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005304 int ppconst_used = ppconst->pp_used;
5305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005307 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 return FAIL;
5309
5310 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312}
5313
5314/*
5315 * expr3a || expr3b || expr3c logical OR
5316 *
5317 * Produces instructions:
5318 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005319 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005320 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005322 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 * EVAL expr3c Push result of "expr3c"
5324 * end:
5325 */
5326 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005327compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005329 int ppconst_used = ppconst->pp_used;
5330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005332 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 return FAIL;
5334
5335 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337}
5338
5339/*
5340 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005342 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 * JUMP_IF_FALSE alt jump if false
5344 * EVAL expr1a
5345 * JUMP_ALWAYS end
5346 * alt: EVAL expr1b
5347 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005348 *
5349 * Toplevel expression: expr2 ?? expr1
5350 * Produces instructions:
5351 * EVAL expr2 Push result of "expr2"
5352 * JUMP_AND_KEEP_IF_TRUE end jump if true
5353 * EVAL expr1
5354 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 */
5356 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005357compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358{
5359 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005360 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005361 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362
Bram Moolenaar3988f642020-08-27 22:43:03 +02005363 // Ignore all kinds of errors when not producing code.
5364 if (cctx->ctx_skip == SKIP_YES)
5365 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005366 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005367 return OK;
5368 }
5369
Bram Moolenaar61a89812020-05-07 16:58:17 +02005370 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005371 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 return FAIL;
5373
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005374 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 if (*p == '?')
5376 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005377 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 garray_T *instr = &cctx->ctx_instr;
5379 garray_T *stack = &cctx->ctx_type_stack;
5380 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005381 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005383 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005384 int has_const_expr = FALSE;
5385 int const_value = FALSE;
5386 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005388 if (next != NULL)
5389 {
5390 *arg = next_line_from_context(cctx, TRUE);
5391 p = skipwhite(*arg);
5392 }
5393
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005394 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005395 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005396 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005397 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005398 return FAIL;
5399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
Bram Moolenaara5565e42020-05-09 15:44:01 +02005401 if (ppconst->pp_used == ppconst_used + 1)
5402 {
5403 // the condition is a constant, we know whether the ? or the :
5404 // expression is to be evaluated.
5405 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005406 if (op_falsy)
5407 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5408 else
5409 {
5410 int error = FALSE;
5411
5412 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5413 &error);
5414 if (error)
5415 return FAIL;
5416 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005417 cctx->ctx_skip = save_skip == SKIP_YES ||
5418 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5419
5420 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5421 // "left ?? right" and "left" is truthy: produce "left"
5422 generate_ppconst(cctx, ppconst);
5423 else
5424 {
5425 clear_tv(&ppconst->pp_tv[ppconst_used]);
5426 --ppconst->pp_used;
5427 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005428 }
5429 else
5430 {
5431 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005432 if (op_falsy)
5433 end_idx = instr->ga_len;
5434 generate_JUMP(cctx, op_falsy
5435 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5436 if (op_falsy)
5437 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439
5440 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005441 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005442 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005443 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005444 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445
Bram Moolenaara5565e42020-05-09 15:44:01 +02005446 if (!has_const_expr)
5447 {
5448 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005450 if (!op_falsy)
5451 {
5452 // remember the type and drop it
5453 --stack->ga_len;
5454 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005456 end_idx = instr->ga_len;
5457 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005458
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005459 // jump here from JUMP_IF_FALSE
5460 isn = ((isn_T *)instr->ga_data) + alt_idx;
5461 isn->isn_arg.jump.jump_where = instr->ga_len;
5462 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005465 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 // Check for the ":".
5468 p = may_peek_next_line(cctx, *arg, &next);
5469 if (*p != ':')
5470 {
5471 emsg(_(e_missing_colon));
5472 return FAIL;
5473 }
5474 if (next != NULL)
5475 {
5476 *arg = next_line_from_context(cctx, TRUE);
5477 p = skipwhite(*arg);
5478 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005479
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005480 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5481 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005482 semsg(_(e_white_space_required_before_and_after_str_at_str),
5483 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005484 return FAIL;
5485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005487 // evaluate the third expression
5488 if (has_const_expr)
5489 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005490 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005491 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005492 return FAIL;
5493 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5494 return FAIL;
5495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496
Bram Moolenaara5565e42020-05-09 15:44:01 +02005497 if (!has_const_expr)
5498 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005499 type_T **typep;
5500
Bram Moolenaara5565e42020-05-09 15:44:01 +02005501 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502
Bram Moolenaara5565e42020-05-09 15:44:01 +02005503 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005504 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5505 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005506
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005507 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005508 isn = ((isn_T *)instr->ga_data) + end_idx;
5509 isn->isn_arg.jump.jump_where = instr->ga_len;
5510 }
5511
5512 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 }
5514 return OK;
5515}
5516
5517/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005518 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005519 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5520 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005521 */
5522 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005523compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005524{
5525 ppconst_T ppconst;
5526
5527 CLEAR_FIELD(ppconst);
5528 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5529 {
5530 clear_ppconst(&ppconst);
5531 return FAIL;
5532 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005533 if (is_const != NULL)
5534 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005535 if (generate_ppconst(cctx, &ppconst) == FAIL)
5536 return FAIL;
5537 return OK;
5538}
5539
5540/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005541 * Toplevel expression.
5542 */
5543 static int
5544compile_expr0(char_u **arg, cctx_T *cctx)
5545{
5546 return compile_expr0_ext(arg, cctx, NULL);
5547}
5548
5549/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005550 * Compile "return [expr]".
5551 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552 */
5553 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005554compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555{
5556 char_u *p = arg;
5557 garray_T *stack = &cctx->ctx_type_stack;
5558 type_T *stack_type;
5559
5560 if (*p != NUL && *p != '|' && *p != '\n')
5561 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005562 if (legacy)
5563 {
5564 int save_flags = cmdmod.cmod_flags;
5565
5566 generate_LEGACY_EVAL(cctx, p);
5567 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5568 0, cctx, FALSE, FALSE) == FAIL)
5569 return NULL;
5570 cmdmod.cmod_flags |= CMOD_LEGACY;
5571 (void)skip_expr(&p, NULL);
5572 cmdmod.cmod_flags = save_flags;
5573 }
5574 else
5575 {
5576 // compile return argument into instructions
5577 if (compile_expr0(&p, cctx) == FAIL)
5578 return NULL;
5579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005581 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005582 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005583 // "check_return_type" with uf_ret_type set to &t_unknown is used
5584 // for an inline function without a specified return type. Set the
5585 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005586 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005587 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005588 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5589 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005590 || (!check_return_type
5591 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005592 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005593 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005594 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005595 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005596 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005597 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5598 && stack_type->tt_type != VAR_VOID
5599 && stack_type->tt_type != VAR_UNKNOWN)
5600 {
5601 emsg(_(e_returning_value_in_function_without_return_type));
5602 return NULL;
5603 }
5604 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005605 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005606 return NULL;
5607 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 }
5610 else
5611 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005612 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005613 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005614 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5615 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005617 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 return NULL;
5619 }
5620
5621 // No argument, return zero.
5622 generate_PUSHNR(cctx, 0);
5623 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005624
5625 // Undo any command modifiers.
5626 generate_undo_cmdmods(cctx);
5627
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005628 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 return NULL;
5630
5631 // "return val | endif" is possible
5632 return skipwhite(p);
5633}
5634
5635/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005636 * Get a line from the compilation context, compatible with exarg_T getline().
5637 * Return a pointer to the line in allocated memory.
5638 * Return NULL for end-of-file or some error.
5639 */
5640 static char_u *
5641exarg_getline(
5642 int c UNUSED,
5643 void *cookie,
5644 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005645 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005646{
5647 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005648 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005649
Bram Moolenaar66250c92020-08-20 15:02:42 +02005650 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005651 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005652 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005653 return NULL;
5654 ++cctx->ctx_lnum;
5655 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5656 // Comment lines result in NULL pointers, skip them.
5657 if (p != NULL)
5658 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005659 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005660}
5661
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005662 void
5663fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5664{
5665 eap->getline = exarg_getline;
5666 eap->cookie = cctx;
5667}
5668
Bram Moolenaar04b12692020-05-04 23:24:44 +02005669/*
5670 * Compile a nested :def command.
5671 */
5672 static char_u *
5673compile_nested_function(exarg_T *eap, cctx_T *cctx)
5674{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005675 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005676 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005677 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005678 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005679 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005680 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005681 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005683 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005684 {
5685 emsg(_(e_cannot_use_bang_with_nested_def));
5686 return NULL;
5687 }
5688
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005689 if (*name_start == '/')
5690 {
5691 name_end = skip_regexp(name_start + 1, '/', TRUE);
5692 if (*name_end == '/')
5693 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005694 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005695 }
5696 if (name_end == name_start || *skipwhite(name_end) != '(')
5697 {
5698 if (!ends_excmd2(name_start, name_end))
5699 {
5700 semsg(_(e_invalid_command_str), eap->cmd);
5701 return NULL;
5702 }
5703
5704 // "def" or "def Name": list functions
5705 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5706 return NULL;
5707 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5708 }
5709
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005710 // Only g:Func() can use a namespace.
5711 if (name_start[1] == ':' && !is_global)
5712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005713 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005714 return NULL;
5715 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005716 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005717 return NULL;
5718
Bram Moolenaar04b12692020-05-04 23:24:44 +02005719 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005720 fill_exarg_from_cctx(eap, cctx);
5721
Bram Moolenaar04b12692020-05-04 23:24:44 +02005722 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005723 lambda_name = vim_strsave(get_lambda_name());
5724 if (lambda_name == NULL)
5725 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005726 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005727
Bram Moolenaar822ba242020-05-24 23:00:18 +02005728 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005729 {
5730 r = eap->skip ? OK : FAIL;
5731 goto theend;
5732 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005733
5734 // copy over the block scope IDs before compiling
5735 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5736 {
5737 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5738
5739 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5740 if (ufunc->uf_block_ids != NULL)
5741 {
5742 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5743 sizeof(int) * block_depth);
5744 ufunc->uf_block_depth = block_depth;
5745 }
5746 }
5747
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005748 compile_type = COMPILE_TYPE(ufunc);
5749#ifdef FEAT_PROFILE
5750 // If the outer function is profiled, also compile the nested function for
5751 // profiling.
5752 if (cctx->ctx_compile_type == CT_PROFILE)
5753 compile_type = CT_PROFILE;
5754#endif
5755 if (func_needs_compiling(ufunc, compile_type)
5756 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005757 {
5758 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005759 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005760 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005761
Bram Moolenaar648594e2021-07-11 17:55:01 +02005762#ifdef FEAT_PROFILE
5763 // When the outer function is compiled for profiling, the nested function
5764 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005765 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005766 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5767#endif
5768
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005769 if (is_global)
5770 {
5771 char_u *func_name = vim_strnsave(name_start + 2,
5772 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005773
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005774 if (func_name == NULL)
5775 r = FAIL;
5776 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005777 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005778 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005779 lambda_name = NULL;
5780 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005781 }
5782 else
5783 {
5784 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005785 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005786 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005787
Bram Moolenaareef21022020-08-01 22:16:43 +02005788 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005789 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005790 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005791 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005792 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5793 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005794 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005795
5796theend:
5797 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005798 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005799}
5800
5801/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 * Return the length of an assignment operator, or zero if there isn't one.
5803 */
5804 int
5805assignment_len(char_u *p, int *heredoc)
5806{
5807 if (*p == '=')
5808 {
5809 if (p[1] == '<' && p[2] == '<')
5810 {
5811 *heredoc = TRUE;
5812 return 3;
5813 }
5814 return 1;
5815 }
5816 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5817 return 2;
5818 if (STRNCMP(p, "..=", 3) == 0)
5819 return 3;
5820 return 0;
5821}
5822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005824 * Generate the load instruction for "name".
5825 */
5826 static void
5827generate_loadvar(
5828 cctx_T *cctx,
5829 assign_dest_T dest,
5830 char_u *name,
5831 lvar_T *lvar,
5832 type_T *type)
5833{
5834 switch (dest)
5835 {
5836 case dest_option:
5837 // TODO: check the option exists
5838 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5839 break;
5840 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005841 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5842 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5843 else
5844 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005845 break;
5846 case dest_buffer:
5847 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5848 break;
5849 case dest_window:
5850 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5851 break;
5852 case dest_tab:
5853 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5854 break;
5855 case dest_script:
5856 compile_load_scriptvar(cctx,
5857 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5858 break;
5859 case dest_env:
5860 // Include $ in the name here
5861 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5862 break;
5863 case dest_reg:
5864 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5865 break;
5866 case dest_vimvar:
5867 generate_LOADV(cctx, name + 2, TRUE);
5868 break;
5869 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005870 if (lvar->lv_from_outer > 0)
5871 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5872 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005873 else
5874 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5875 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005876 case dest_expr:
5877 // list or dict value should already be on the stack.
5878 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005879 }
5880}
5881
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005882/*
5883 * Skip over "[expr]" or ".member".
5884 * Does not check for any errors.
5885 */
5886 static char_u *
5887skip_index(char_u *start)
5888{
5889 char_u *p = start;
5890
5891 if (*p == '[')
5892 {
5893 p = skipwhite(p + 1);
5894 (void)skip_expr(&p, NULL);
5895 p = skipwhite(p);
5896 if (*p == ']')
5897 return p + 1;
5898 return p;
5899 }
5900 // if (*p == '.')
5901 return to_name_end(p + 1, TRUE);
5902}
5903
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005904 void
5905vim9_declare_error(char_u *name)
5906{
5907 char *scope = "";
5908
5909 switch (*name)
5910 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005911 case 'g': scope = _("global"); break;
5912 case 'b': scope = _("buffer"); break;
5913 case 'w': scope = _("window"); break;
5914 case 't': scope = _("tab"); break;
5915 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005916 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005917 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005918 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005919 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005920 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005921 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005922 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005923 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005924 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005925}
5926
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005927/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005928 * For one assignment figure out the type of destination. Return it in "dest".
5929 * When not recognized "dest" is not set.
5930 * For an option "opt_flags" is set.
5931 * For a v:var "vimvaridx" is set.
5932 * "type" is set to the destination type if known, unchanted otherwise.
5933 * Return FAIL if an error message was given.
5934 */
5935 static int
5936get_var_dest(
5937 char_u *name,
5938 assign_dest_T *dest,
5939 int cmdidx,
5940 int *opt_flags,
5941 int *vimvaridx,
5942 type_T **type,
5943 cctx_T *cctx)
5944{
5945 char_u *p;
5946
5947 if (*name == '&')
5948 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005949 int cc;
5950 long numval;
5951 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005952
5953 *dest = dest_option;
5954 if (cmdidx == CMD_final || cmdidx == CMD_const)
5955 {
5956 emsg(_(e_const_option));
5957 return FAIL;
5958 }
5959 p = name;
5960 p = find_option_end(&p, opt_flags);
5961 if (p == NULL)
5962 {
5963 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005964 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005965 return FAIL;
5966 }
5967 cc = *p;
5968 *p = NUL;
5969 opt_type = get_option_value(skip_option_env_lead(name),
5970 &numval, NULL, *opt_flags);
5971 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005972 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005973 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005974 case gov_unknown:
5975 semsg(_(e_unknown_option), name);
5976 return FAIL;
5977 case gov_string:
5978 case gov_hidden_string:
5979 *type = &t_string;
5980 break;
5981 case gov_bool:
5982 case gov_hidden_bool:
5983 *type = &t_bool;
5984 break;
5985 case gov_number:
5986 case gov_hidden_number:
5987 *type = &t_number;
5988 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005989 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005990 }
5991 else if (*name == '$')
5992 {
5993 *dest = dest_env;
5994 *type = &t_string;
5995 }
5996 else if (*name == '@')
5997 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005998 if (name[1] != '@'
5999 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006000 {
6001 emsg_invreg(name[1]);
6002 return FAIL;
6003 }
6004 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006005 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006006 }
6007 else if (STRNCMP(name, "g:", 2) == 0)
6008 {
6009 *dest = dest_global;
6010 }
6011 else if (STRNCMP(name, "b:", 2) == 0)
6012 {
6013 *dest = dest_buffer;
6014 }
6015 else if (STRNCMP(name, "w:", 2) == 0)
6016 {
6017 *dest = dest_window;
6018 }
6019 else if (STRNCMP(name, "t:", 2) == 0)
6020 {
6021 *dest = dest_tab;
6022 }
6023 else if (STRNCMP(name, "v:", 2) == 0)
6024 {
6025 typval_T *vtv;
6026 int di_flags;
6027
6028 *vimvaridx = find_vim_var(name + 2, &di_flags);
6029 if (*vimvaridx < 0)
6030 {
6031 semsg(_(e_variable_not_found_str), name);
6032 return FAIL;
6033 }
6034 // We use the current value of "sandbox" here, is that OK?
6035 if (var_check_ro(di_flags, name, FALSE))
6036 return FAIL;
6037 *dest = dest_vimvar;
6038 vtv = get_vim_var_tv(*vimvaridx);
6039 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6040 }
6041 return OK;
6042}
6043
6044/*
6045 * Generate a STORE instruction for "dest", not being "dest_local".
6046 * Return FAIL when out of memory.
6047 */
6048 static int
6049generate_store_var(
6050 cctx_T *cctx,
6051 assign_dest_T dest,
6052 int opt_flags,
6053 int vimvaridx,
6054 int scriptvar_idx,
6055 int scriptvar_sid,
6056 type_T *type,
6057 char_u *name)
6058{
6059 switch (dest)
6060 {
6061 case dest_option:
6062 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6063 opt_flags);
6064 case dest_global:
6065 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006066 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6067 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006068 case dest_buffer:
6069 // include b: with the name, easier to execute that way
6070 return generate_STORE(cctx, ISN_STOREB, 0, name);
6071 case dest_window:
6072 // include w: with the name, easier to execute that way
6073 return generate_STORE(cctx, ISN_STOREW, 0, name);
6074 case dest_tab:
6075 // include t: with the name, easier to execute that way
6076 return generate_STORE(cctx, ISN_STORET, 0, name);
6077 case dest_env:
6078 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6079 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006080 return generate_STORE(cctx, ISN_STOREREG,
6081 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006082 case dest_vimvar:
6083 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6084 case dest_script:
6085 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006086 // "s:" may be included in the name.
6087 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006088 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006089 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6090 scriptvar_sid, scriptvar_idx, type);
6091 case dest_local:
6092 case dest_expr:
6093 // cannot happen
6094 break;
6095 }
6096 return FAIL;
6097}
6098
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006100generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6101{
6102 if (lhs->lhs_dest != dest_local)
6103 return generate_store_var(cctx, lhs->lhs_dest,
6104 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6105 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6106 lhs->lhs_type, lhs->lhs_name);
6107
6108 if (lhs->lhs_lvar != NULL)
6109 {
6110 garray_T *instr = &cctx->ctx_instr;
6111 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6112
6113 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6114 // ISN_STORENR
6115 if (lhs->lhs_lvar->lv_from_outer == 0
6116 && instr->ga_len == instr_count + 1
6117 && isn->isn_type == ISN_PUSHNR)
6118 {
6119 varnumber_T val = isn->isn_arg.number;
6120 garray_T *stack = &cctx->ctx_type_stack;
6121
6122 isn->isn_type = ISN_STORENR;
6123 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6124 isn->isn_arg.storenr.stnr_val = val;
6125 if (stack->ga_len > 0)
6126 --stack->ga_len;
6127 }
6128 else if (lhs->lhs_lvar->lv_from_outer > 0)
6129 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6130 lhs->lhs_lvar->lv_from_outer);
6131 else
6132 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6133 }
6134 return OK;
6135}
6136
6137 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006138is_decl_command(int cmdidx)
6139{
6140 return cmdidx == CMD_let || cmdidx == CMD_var
6141 || cmdidx == CMD_final || cmdidx == CMD_const;
6142}
6143
6144/*
6145 * Figure out the LHS type and other properties for an assignment or one item
6146 * of ":unlet" with an index.
6147 * Returns OK or FAIL.
6148 */
6149 static int
6150compile_lhs(
6151 char_u *var_start,
6152 lhs_T *lhs,
6153 int cmdidx,
6154 int heredoc,
6155 int oplen,
6156 cctx_T *cctx)
6157{
6158 char_u *var_end;
6159 int is_decl = is_decl_command(cmdidx);
6160
6161 CLEAR_POINTER(lhs);
6162 lhs->lhs_dest = dest_local;
6163 lhs->lhs_vimvaridx = -1;
6164 lhs->lhs_scriptvar_idx = -1;
6165
6166 // "dest_end" is the end of the destination, including "[expr]" or
6167 // ".name".
6168 // "var_end" is the end of the variable/option/etc. name.
6169 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6170 if (*var_start == '@')
6171 var_end = var_start + 2;
6172 else
6173 {
6174 // skip over the leading "&", "&l:", "&g:" and "$"
6175 var_end = skip_option_env_lead(var_start);
6176 var_end = to_name_end(var_end, TRUE);
6177 }
6178
6179 // "a: type" is declaring variable "a" with a type, not dict "a:".
6180 if (is_decl && lhs->lhs_dest_end == var_start + 2
6181 && lhs->lhs_dest_end[-1] == ':')
6182 --lhs->lhs_dest_end;
6183 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6184 --var_end;
6185
6186 // compute the length of the destination without "[expr]" or ".name"
6187 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006188 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6190 if (lhs->lhs_name == NULL)
6191 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006192
6193 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6194 // Something follows after the variable: "var[idx]" or "var.key".
6195 lhs->lhs_has_index = TRUE;
6196
Bram Moolenaar752fc692021-01-04 21:57:11 +01006197 if (heredoc)
6198 lhs->lhs_type = &t_list_string;
6199 else
6200 lhs->lhs_type = &t_any;
6201
6202 if (cctx->ctx_skip != SKIP_YES)
6203 {
6204 int declare_error = FALSE;
6205
6206 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6207 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6208 &lhs->lhs_type, cctx) == FAIL)
6209 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006210 if (lhs->lhs_dest != dest_local
6211 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006212 {
6213 // Specific kind of variable recognized.
6214 declare_error = is_decl;
6215 }
6216 else
6217 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006219 if (check_reserved_name(lhs->lhs_name) == FAIL)
6220 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221
6222 if (lookup_local(var_start, lhs->lhs_varlen,
6223 &lhs->lhs_local_lvar, cctx) == OK)
6224 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6225 else
6226 {
6227 CLEAR_FIELD(lhs->lhs_arg_lvar);
6228 if (arg_exists(var_start, lhs->lhs_varlen,
6229 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6230 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6231 {
6232 if (is_decl)
6233 {
6234 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6235 return FAIL;
6236 }
6237 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6238 }
6239 }
6240 if (lhs->lhs_lvar != NULL)
6241 {
6242 if (is_decl)
6243 {
6244 semsg(_(e_variable_already_declared), lhs->lhs_name);
6245 return FAIL;
6246 }
6247 }
6248 else
6249 {
6250 int script_namespace = lhs->lhs_varlen > 1
6251 && STRNCMP(var_start, "s:", 2) == 0;
6252 int script_var = (script_namespace
6253 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006254 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006255 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006256 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257 imported_T *import =
6258 find_imported(var_start, lhs->lhs_varlen, cctx);
6259
6260 if (script_namespace || script_var || import != NULL)
6261 {
6262 char_u *rawname = lhs->lhs_name
6263 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6264
6265 if (is_decl)
6266 {
6267 if (script_namespace)
6268 semsg(_(e_cannot_declare_script_variable_in_function),
6269 lhs->lhs_name);
6270 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006271 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 lhs->lhs_name);
6273 return FAIL;
6274 }
6275 else if (cctx->ctx_ufunc->uf_script_ctx_version
6276 == SCRIPT_VERSION_VIM9
6277 && script_namespace
6278 && !script_var && import == NULL)
6279 {
6280 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6281 return FAIL;
6282 }
6283
6284 lhs->lhs_dest = dest_script;
6285
6286 // existing script-local variables should have a type
6287 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6288 if (import != NULL)
6289 lhs->lhs_scriptvar_sid = import->imp_sid;
6290 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6291 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006292 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006293 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006294 lhs->lhs_scriptvar_sid, rawname,
6295 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6296 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006297 if (lhs->lhs_scriptvar_idx >= 0)
6298 {
6299 scriptitem_T *si = SCRIPT_ITEM(
6300 lhs->lhs_scriptvar_sid);
6301 svar_T *sv =
6302 ((svar_T *)si->sn_var_vals.ga_data)
6303 + lhs->lhs_scriptvar_idx;
6304 lhs->lhs_type = sv->sv_type;
6305 }
6306 }
6307 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006308 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006309 == FAIL)
6310 return FAIL;
6311 }
6312 }
6313
6314 if (declare_error)
6315 {
6316 vim9_declare_error(lhs->lhs_name);
6317 return FAIL;
6318 }
6319 }
6320
6321 // handle "a:name" as a name, not index "name" on "a"
6322 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6323 var_end = lhs->lhs_dest_end;
6324
6325 if (lhs->lhs_dest != dest_option)
6326 {
6327 if (is_decl && *var_end == ':')
6328 {
6329 char_u *p;
6330
6331 // parse optional type: "let var: type = expr"
6332 if (!VIM_ISWHITE(var_end[1]))
6333 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006334 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 return FAIL;
6336 }
6337 p = skipwhite(var_end + 1);
6338 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6339 if (lhs->lhs_type == NULL)
6340 return FAIL;
6341 lhs->lhs_has_type = TRUE;
6342 }
6343 else if (lhs->lhs_lvar != NULL)
6344 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6345 }
6346
Bram Moolenaare42939a2021-04-05 17:11:17 +02006347 if (oplen == 3 && !heredoc
6348 && lhs->lhs_dest != dest_global
6349 && !lhs->lhs_has_index
6350 && lhs->lhs_type->tt_type != VAR_STRING
6351 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006352 {
6353 emsg(_(e_can_only_concatenate_to_string));
6354 return FAIL;
6355 }
6356
6357 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6358 && cctx->ctx_skip != SKIP_YES)
6359 {
6360 if (oplen > 1 && !heredoc)
6361 {
6362 // +=, /=, etc. require an existing variable
6363 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6364 return FAIL;
6365 }
6366 if (!is_decl)
6367 {
6368 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6369 return FAIL;
6370 }
6371
Bram Moolenaar3f327882021-03-17 20:56:38 +01006372 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 if ((lhs->lhs_type->tt_type == VAR_FUNC
6374 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006375 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006376 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006377
6378 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6380 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6381 if (lhs->lhs_lvar == NULL)
6382 return FAIL;
6383 lhs->lhs_new_local = TRUE;
6384 }
6385
6386 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006387 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006388 {
6389 // Something follows after the variable: "var[idx]" or "var.key".
6390 // TODO: should we also handle "->func()" here?
6391 if (is_decl)
6392 {
6393 emsg(_(e_cannot_use_index_when_declaring_variable));
6394 return FAIL;
6395 }
6396
6397 if (var_start[lhs->lhs_varlen] == '['
6398 || var_start[lhs->lhs_varlen] == '.')
6399 {
6400 char_u *after = var_start + lhs->lhs_varlen;
6401 char_u *p;
6402
6403 // Only the last index is used below, if there are others
6404 // before it generate code for the expression. Thus for
6405 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6406 for (;;)
6407 {
6408 p = skip_index(after);
6409 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006410 {
6411 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006412 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006413 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006414 after = p;
6415 }
6416 if (after > var_start + lhs->lhs_varlen)
6417 {
6418 lhs->lhs_varlen = after - var_start;
6419 lhs->lhs_dest = dest_expr;
6420 // We don't know the type before evaluating the expression,
6421 // use "any" until then.
6422 lhs->lhs_type = &t_any;
6423 }
6424
Bram Moolenaar752fc692021-01-04 21:57:11 +01006425 if (lhs->lhs_type->tt_member == NULL)
6426 lhs->lhs_member_type = &t_any;
6427 else
6428 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6429 }
6430 else
6431 {
6432 semsg("Not supported yet: %s", var_start);
6433 return FAIL;
6434 }
6435 }
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006440 * Figure out the LHS and check a few errors.
6441 */
6442 static int
6443compile_assign_lhs(
6444 char_u *var_start,
6445 lhs_T *lhs,
6446 int cmdidx,
6447 int is_decl,
6448 int heredoc,
6449 int oplen,
6450 cctx_T *cctx)
6451{
6452 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6453 return FAIL;
6454
6455 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6456 {
6457 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6458 return FAIL;
6459 }
6460 if (!is_decl && lhs->lhs_lvar != NULL
6461 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6462 {
6463 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6464 return FAIL;
6465 }
6466 return OK;
6467}
6468
6469/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006470 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6471 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 */
6473 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006474compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 char_u *var_start,
6476 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006477 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 cctx_T *cctx)
6479{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006481 char_u *p;
6482 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006483 int need_white_before = TRUE;
6484 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006485
Bram Moolenaar752fc692021-01-04 21:57:11 +01006486 p = var_start + varlen;
6487 if (*p == '[')
6488 {
6489 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006490 if (*p == ':')
6491 {
6492 // empty first index, push zero
6493 r = generate_PUSHNR(cctx, 0);
6494 need_white_before = FALSE;
6495 }
6496 else
6497 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006498
6499 if (r == OK && *skipwhite(p) == ':')
6500 {
6501 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006502 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006503 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006504 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006505 empty_second = *skipwhite(p + 1) == ']';
6506 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6507 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006508 {
6509 semsg(_(e_white_space_required_before_and_after_str_at_str),
6510 ":", p);
6511 return FAIL;
6512 }
6513 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006514 if (*p == ']')
6515 // empty second index, push "none"
6516 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6517 else
6518 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006519 }
6520
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 if (r == OK && *skipwhite(p) != ']')
6522 {
6523 // this should not happen
6524 emsg(_(e_missbrac));
6525 r = FAIL;
6526 }
6527 }
6528 else // if (*p == '.')
6529 {
6530 char_u *key_end = to_name_end(p + 1, TRUE);
6531 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6532
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006533 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006535 return r;
6536}
6537
6538/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006539 * For a LHS with an index, load the variable to be indexed.
6540 */
6541 static int
6542compile_load_lhs(
6543 lhs_T *lhs,
6544 char_u *var_start,
6545 type_T *rhs_type,
6546 cctx_T *cctx)
6547{
6548 if (lhs->lhs_dest == dest_expr)
6549 {
6550 size_t varlen = lhs->lhs_varlen;
6551 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006552 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006553 char_u *p = var_start;
6554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006555 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006556
Bram Moolenaare97976b2021-08-01 13:17:17 +02006557 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6558 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006559 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006560 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6561 res = compile_expr0(&p, cctx);
6562 var_start[varlen] = c;
6563 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6564 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006565 {
6566 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006567 if (res != FAIL)
6568 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006569 return FAIL;
6570 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006571
6572 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6573 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6574 // now we can properly check the type
6575 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6576 && rhs_type != &t_void
6577 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6578 FALSE, FALSE) == FAIL)
6579 return FAIL;
6580 }
6581 else
6582 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6583 lhs->lhs_lvar, lhs->lhs_type);
6584 return OK;
6585}
6586
6587/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006588 * Produce code for loading "lhs" and also take care of an index.
6589 * Return OK/FAIL.
6590 */
6591 static int
6592compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6593{
6594 compile_load_lhs(lhs, var_start, NULL, cctx);
6595
6596 if (lhs->lhs_has_index)
6597 {
6598 int range = FALSE;
6599
6600 // Get member from list or dict. First compile the
6601 // index value.
6602 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6603 return FAIL;
6604 if (range)
6605 {
6606 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6607 var_start);
6608 return FAIL;
6609 }
6610
6611 // Get the member.
6612 if (compile_member(FALSE, cctx) == FAIL)
6613 return FAIL;
6614 }
6615 return OK;
6616}
6617
6618/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006619 * Assignment to a list or dict member, or ":unlet" for the item, using the
6620 * information in "lhs".
6621 * Returns OK or FAIL.
6622 */
6623 static int
6624compile_assign_unlet(
6625 char_u *var_start,
6626 lhs_T *lhs,
6627 int is_assign,
6628 type_T *rhs_type,
6629 cctx_T *cctx)
6630{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006631 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006632 garray_T *stack = &cctx->ctx_type_stack;
6633 int range = FALSE;
6634
Bram Moolenaar68452172021-04-12 21:21:02 +02006635 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006637 if (is_assign && range && lhs->lhs_type != &t_blob
6638 && lhs->lhs_type != &t_any)
6639 {
6640 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6641 return FAIL;
6642 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006643
6644 if (lhs->lhs_type == &t_any)
6645 {
6646 // Index on variable of unknown type: check at runtime.
6647 dest_type = VAR_ANY;
6648 }
6649 else
6650 {
6651 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006652 if (dest_type == VAR_DICT && range)
6653 {
6654 emsg(e_cannot_use_range_with_dictionary);
6655 return FAIL;
6656 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006657 if (dest_type == VAR_DICT
6658 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006660 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006661 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006662 type_T *type;
6663
6664 if (range)
6665 {
6666 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6667 if (need_type(type, &t_number,
6668 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006669 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006670 }
6671 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006672 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006673 && need_type(type, &t_number,
6674 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006675 return FAIL;
6676 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 }
6678
6679 // Load the dict or list. On the stack we then have:
6680 // - value (for assignment, not for :unlet)
6681 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006682 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006683 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006684 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6685 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006686
Bram Moolenaar68452172021-04-12 21:21:02 +02006687 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6688 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006689 {
6690 if (is_assign)
6691 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006692 if (range)
6693 {
6694 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6695 return FAIL;
6696 }
6697 else
6698 {
6699 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006700
Bram Moolenaar68452172021-04-12 21:21:02 +02006701 if (isn == NULL)
6702 return FAIL;
6703 isn->isn_arg.vartype = dest_type;
6704 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006706 else if (range)
6707 {
6708 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6709 return FAIL;
6710 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006711 else
6712 {
6713 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6714 return FAIL;
6715 }
6716 }
6717 else
6718 {
6719 emsg(_(e_indexable_type_required));
6720 return FAIL;
6721 }
6722
6723 return OK;
6724}
6725
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006726/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006727 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006728 * "let name"
6729 * "var name = expr"
6730 * "final name = expr"
6731 * "const name = expr"
6732 * "name = expr"
6733 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006734 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006735 * Return NULL for an error.
6736 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 */
6738 static char_u *
6739compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6740{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006741 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006743 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 char_u *ret = NULL;
6745 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006746 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006748 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006750 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 int oplen = 0;
6753 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006754 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006755 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006757 int is_decl = is_decl_command(cmdidx);
6758 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006759 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006761 // Skip over the "var" or "[var, var]" to get to any "=".
6762 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6763 if (p == NULL)
6764 return *arg == '[' ? arg : NULL;
6765
6766 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006768 // TODO: should we allow this, and figure out type inference from list
6769 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006770 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 return NULL;
6772 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006773 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 sp = p;
6776 p = skipwhite(p);
6777 op = p;
6778 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006779
6780 if (var_count > 0 && oplen == 0)
6781 // can be something like "[1, 2]->func()"
6782 return arg;
6783
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006784 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006785 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006786 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006788 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006789 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6790 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006791 if (VIM_ISWHITE(eap->cmd[2]))
6792 {
6793 semsg(_(e_no_white_space_allowed_after_str_str),
6794 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6795 return NULL;
6796 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006797 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6798 oplen = 2;
6799 incdec = TRUE;
6800 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 if (heredoc)
6803 {
6804 list_T *l;
6805 listitem_T *li;
6806
6807 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006808 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006810 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006811 if (l == NULL)
6812 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813
Bram Moolenaar078269b2020-09-21 20:35:55 +02006814 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006816 // Push each line and the create the list.
6817 FOR_ALL_LIST_ITEMS(l, li)
6818 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006819 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006820 li->li_tv.vval.v_string = NULL;
6821 }
6822 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 list_free(l);
6825 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006826 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006828 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006830 char_u *wp;
6831
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832 // for "[var, var] = expr" evaluate the expression here, loop over the
6833 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006834 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006835
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006836 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006837 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006838 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006839 if (compile_expr0(&p, cctx) == FAIL)
6840 return NULL;
6841 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006843 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006845 type_T *stacktype;
6846
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006847 stacktype = stack->ga_len == 0 ? &t_void
6848 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006849 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006851 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006852 goto theend;
6853 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006854 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006855 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006856 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006857 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006858 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6859 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006860 if (stacktype->tt_member != NULL)
6861 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006862 }
6863 }
6864
6865 /*
6866 * Loop over variables in "[var, var] = expr".
6867 * For "var = expr" and "let var: type" this is done only once.
6868 */
6869 if (var_count > 0)
6870 var_start = skipwhite(arg + 1); // skip over the "["
6871 else
6872 var_start = arg;
6873 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6874 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006875 int instr_count = -1;
6876 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006878 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6879 {
6880 // Ignore underscore in "[a, _, b] = list".
6881 if (var_count > 0)
6882 {
6883 var_start = skipwhite(var_start + 2);
6884 continue;
6885 }
6886 emsg(_(e_cannot_use_underscore_here));
6887 goto theend;
6888 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006889 vim_free(lhs.lhs_name);
6890
6891 /*
6892 * Figure out the LHS type and other properties.
6893 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006894 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6895 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006896 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006897 if (heredoc)
6898 {
6899 SOURCING_LNUM = start_lnum;
6900 if (lhs.lhs_has_type
6901 && need_type(&t_list_string, lhs.lhs_type,
6902 -1, 0, cctx, FALSE, FALSE) == FAIL)
6903 goto theend;
6904 }
6905 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006906 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006907 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006908 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006909 if (oplen > 0 && var_count == 0)
6910 {
6911 // skip over the "=" and the expression
6912 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006913 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006914 }
6915 }
6916 else if (oplen > 0)
6917 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006918 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006919 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006920
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006921 // for "+=", "*=", "..=" etc. first load the current value
6922 if (*op != '='
6923 && compile_load_lhs_with_index(&lhs, var_start,
6924 cctx) == FAIL)
6925 goto theend;
6926
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006927 // For "var = expr" evaluate the expression.
6928 if (var_count == 0)
6929 {
6930 int r;
6931
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006932 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006933 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006934 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006935 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006936 r = generate_PUSHNR(cctx, 1);
6937 }
6938 else
6939 {
6940 // Temporarily hide the new local variable here, it is
6941 // not available to this expression.
6942 if (lhs.lhs_new_local)
6943 --cctx->ctx_locals.ga_len;
6944 wp = op + oplen;
6945 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6946 {
6947 if (lhs.lhs_new_local)
6948 ++cctx->ctx_locals.ga_len;
6949 goto theend;
6950 }
6951 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006952 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006953 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006954 if (r == FAIL)
6955 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006956 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006958 else if (semicolon && var_idx == var_count - 1)
6959 {
6960 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006961 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006962 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6963 goto theend;
6964 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 else
6966 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006967 // For "[var, var] = expr" get the "var_idx" item from the
6968 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006969 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006970 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006972
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006973 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006974 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006976 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006977 if ((rhs_type->tt_type == VAR_FUNC
6978 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006979 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006981 goto theend;
6982
Bram Moolenaar752fc692021-01-04 21:57:11 +01006983 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006984 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006985 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006986 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006987 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006988 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 }
6990 else
6991 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006992 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006993 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006994 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006995 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006996 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006997 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006998 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006999 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007000 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007001 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007002 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007003 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007004 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007005 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007006 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007007
Bram Moolenaar77709b12021-04-03 21:01:01 +02007008 // Without operator check type here, otherwise below.
7009 // Use the line number of the assignment.
7010 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007011 if (lhs.lhs_has_index)
7012 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01007013 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007014 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007015 goto theend;
7016 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007017 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007018 else
7019 {
7020 type_T *lhs_type = lhs.lhs_member_type;
7021
7022 // Special case: assigning to @# can use a number or a
7023 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007024 // Also: can assign a number to a float.
7025 if ((lhs_type == &t_number_or_string
7026 || lhs_type == &t_float)
7027 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007028 lhs_type = &t_number;
7029 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007030 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007031 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007034 else if (cmdidx == CMD_final)
7035 {
7036 emsg(_(e_final_requires_a_value));
7037 goto theend;
7038 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007039 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007041 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007042 goto theend;
7043 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007044 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007045 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007046 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007047 goto theend;
7048 }
7049 else
7050 {
7051 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007052 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007053 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007054 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007055 {
7056 case VAR_BOOL:
7057 generate_PUSHBOOL(cctx, VVAL_FALSE);
7058 break;
7059 case VAR_FLOAT:
7060#ifdef FEAT_FLOAT
7061 generate_PUSHF(cctx, 0.0);
7062#endif
7063 break;
7064 case VAR_STRING:
7065 generate_PUSHS(cctx, NULL);
7066 break;
7067 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007068 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007069 break;
7070 case VAR_FUNC:
7071 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7072 break;
7073 case VAR_LIST:
7074 generate_NEWLIST(cctx, 0);
7075 break;
7076 case VAR_DICT:
7077 generate_NEWDICT(cctx, 0);
7078 break;
7079 case VAR_JOB:
7080 generate_PUSHJOB(cctx, NULL);
7081 break;
7082 case VAR_CHANNEL:
7083 generate_PUSHCHANNEL(cctx, NULL);
7084 break;
7085 case VAR_NUMBER:
7086 case VAR_UNKNOWN:
7087 case VAR_ANY:
7088 case VAR_PARTIAL:
7089 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007090 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007091 case VAR_SPECIAL: // cannot happen
7092 generate_PUSHNR(cctx, 0);
7093 break;
7094 }
7095 }
7096 if (var_count == 0)
7097 end = p;
7098 }
7099
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007100 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007101 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007102 break;
7103
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007104 if (oplen > 0 && *op != '=')
7105 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007106 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007107 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007108
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007109 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007110 {
7111 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7112 goto theend;
7113 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007114 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007115 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007116 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007117 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7118 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007119#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007120 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007121 !(expected == &t_float && (stacktype == &t_number
7122 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007123#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007124 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007125 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007126 goto theend;
7127 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007128
7129 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007130 {
7131 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7132 goto theend;
7133 }
7134 else if (*op == '+')
7135 {
7136 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007137 operator_type(lhs.lhs_member_type, stacktype),
7138 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007139 goto theend;
7140 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007141 else if (generate_two_op(cctx, op) == FAIL)
7142 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007145 // Use the line number of the assignment for store instruction.
7146 save_lnum = cctx->ctx_lnum;
7147 cctx->ctx_lnum = start_lnum - 1;
7148
Bram Moolenaar752fc692021-01-04 21:57:11 +01007149 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007150 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007151 // Use the info in "lhs" to store the value at the index in the
7152 // list or dict.
7153 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7154 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007155 {
7156 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007157 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007158 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007159 }
7160 else
7161 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007162 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007163 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007164 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007165 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007166 generate_LOCKCONST(cctx);
7167
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007168 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007169 && (lhs.lhs_type->tt_type == VAR_DICT
7170 || lhs.lhs_type->tt_type == VAR_LIST)
7171 && lhs.lhs_type->tt_member != NULL
7172 && lhs.lhs_type->tt_member != &t_any
7173 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007174 // Set the type in the list or dict, so that it can be checked,
7175 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007176 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007177
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007178 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007179 {
7180 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007181 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007182 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007183 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007184 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007185
7186 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007187 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007189
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007190 // For "[var, var] = expr" drop the "expr" value.
7191 // Also for "[var, var; _] = expr".
7192 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007193 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007194 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007195 goto theend;
7196 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007197
Bram Moolenaarb2097502020-07-19 17:17:02 +02007198 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199
7200theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007201 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 return ret;
7203}
7204
7205/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007206 * Check for an assignment at "eap->cmd", compile it if found.
7207 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7208 */
7209 static int
7210may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7211{
7212 char_u *pskip;
7213 char_u *p;
7214
7215 // Assuming the command starts with a variable or function name,
7216 // find what follows.
7217 // Skip over "var.member", "var[idx]" and the like.
7218 // Also "&opt = val", "$ENV = val" and "@r = val".
7219 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7220 ? eap->cmd + 1 : eap->cmd;
7221 p = to_name_end(pskip, TRUE);
7222 if (p > eap->cmd && *p != NUL)
7223 {
7224 char_u *var_end;
7225 int oplen;
7226 int heredoc;
7227
7228 if (eap->cmd[0] == '@')
7229 var_end = eap->cmd + 2;
7230 else
7231 var_end = find_name_end(pskip, NULL, NULL,
7232 FNE_CHECK_START | FNE_INCL_BR);
7233 oplen = assignment_len(skipwhite(var_end), &heredoc);
7234 if (oplen > 0)
7235 {
7236 size_t len = p - eap->cmd;
7237
7238 // Recognize an assignment if we recognize the variable
7239 // name:
7240 // "g:var = expr"
7241 // "local = expr" where "local" is a local var.
7242 // "script = expr" where "script" is a script-local var.
7243 // "import = expr" where "import" is an imported var
7244 // "&opt = expr"
7245 // "$ENV = expr"
7246 // "@r = expr"
7247 if (*eap->cmd == '&'
7248 || *eap->cmd == '$'
7249 || *eap->cmd == '@'
7250 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007251 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007252 {
7253 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7254 if (*line == NULL || *line == eap->cmd)
7255 return FAIL;
7256 return OK;
7257 }
7258 }
7259 }
7260
7261 if (*eap->cmd == '[')
7262 {
7263 // [var, var] = expr
7264 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7265 if (*line == NULL)
7266 return FAIL;
7267 if (*line != eap->cmd)
7268 return OK;
7269 }
7270 return NOTDONE;
7271}
7272
7273/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007274 * Check if "name" can be "unlet".
7275 */
7276 int
7277check_vim9_unlet(char_u *name)
7278{
7279 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7280 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007281 // "unlet s:var" is allowed in legacy script.
7282 if (*name == 's' && !script_is_vim9())
7283 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007284 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007285 return FAIL;
7286 }
7287 return OK;
7288}
7289
7290/*
7291 * Callback passed to ex_unletlock().
7292 */
7293 static int
7294compile_unlet(
7295 lval_T *lvp,
7296 char_u *name_end,
7297 exarg_T *eap,
7298 int deep UNUSED,
7299 void *coookie)
7300{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007301 cctx_T *cctx = coookie;
7302 char_u *p = lvp->ll_name;
7303 int cc = *name_end;
7304 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007305
Bram Moolenaar752fc692021-01-04 21:57:11 +01007306 if (cctx->ctx_skip == SKIP_YES)
7307 return OK;
7308
7309 *name_end = NUL;
7310 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007311 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007312 // :unlet $ENV_VAR
7313 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7314 }
7315 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7316 {
7317 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007318
Bram Moolenaar752fc692021-01-04 21:57:11 +01007319 // This is similar to assigning: lookup the list/dict, compile the
7320 // idx/key. Then instead of storing the value unlet the item.
7321 // unlet {list}[idx]
7322 // unlet {dict}[key] dict.key
7323 //
7324 // Figure out the LHS type and other properties.
7325 //
7326 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7327
7328 // : unlet an indexed item
7329 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007330 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007331 iemsg("called compile_lhs() without an index");
7332 ret = FAIL;
7333 }
7334 else
7335 {
7336 // Use the info in "lhs" to unlet the item at the index in the
7337 // list or dict.
7338 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007339 }
7340
Bram Moolenaar752fc692021-01-04 21:57:11 +01007341 vim_free(lhs.lhs_name);
7342 }
7343 else if (check_vim9_unlet(p) == FAIL)
7344 {
7345 ret = FAIL;
7346 }
7347 else
7348 {
7349 // Normal name. Only supports g:, w:, t: and b: namespaces.
7350 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007351 }
7352
Bram Moolenaar752fc692021-01-04 21:57:11 +01007353 *name_end = cc;
7354 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007355}
7356
7357/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007358 * Callback passed to ex_unletlock().
7359 */
7360 static int
7361compile_lock_unlock(
7362 lval_T *lvp,
7363 char_u *name_end,
7364 exarg_T *eap,
7365 int deep UNUSED,
7366 void *coookie)
7367{
7368 cctx_T *cctx = coookie;
7369 int cc = *name_end;
7370 char_u *p = lvp->ll_name;
7371 int ret = OK;
7372 size_t len;
7373 char_u *buf;
7374
7375 if (cctx->ctx_skip == SKIP_YES)
7376 return OK;
7377
7378 // Cannot use :lockvar and :unlockvar on local variables.
7379 if (p[1] != ':')
7380 {
7381 char_u *end = skip_var_one(p, FALSE);
7382
7383 if (lookup_local(p, end - p, NULL, cctx) == OK)
7384 {
7385 emsg(_(e_cannot_lock_unlock_local_variable));
7386 return FAIL;
7387 }
7388 }
7389
7390 // Checking is done at runtime.
7391 *name_end = NUL;
7392 len = name_end - p + 20;
7393 buf = alloc(len);
7394 if (buf == NULL)
7395 ret = FAIL;
7396 else
7397 {
7398 vim_snprintf((char *)buf, len, "%s %s",
7399 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7400 p);
7401 ret = generate_EXEC(cctx, buf);
7402
7403 vim_free(buf);
7404 *name_end = cc;
7405 }
7406 return ret;
7407}
7408
7409/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007410 * compile "unlet var", "lock var" and "unlock var"
7411 * "arg" points to "var".
7412 */
7413 static char_u *
7414compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7415{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007416 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7417 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7418 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007419 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7420}
7421
7422/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7424 */
7425 static int
7426compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7427{
7428 garray_T *instr = &cctx->ctx_instr;
7429 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7430
7431 if (endlabel == NULL)
7432 return FAIL;
7433 endlabel->el_next = *el;
7434 *el = endlabel;
7435 endlabel->el_end_label = instr->ga_len;
7436
7437 generate_JUMP(cctx, when, 0);
7438 return OK;
7439}
7440
7441 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007442compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443{
7444 garray_T *instr = &cctx->ctx_instr;
7445
7446 while (*el != NULL)
7447 {
7448 endlabel_T *cur = (*el);
7449 isn_T *isn;
7450
7451 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007452 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 *el = cur->el_next;
7454 vim_free(cur);
7455 }
7456}
7457
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007458 static void
7459compile_free_jump_to_end(endlabel_T **el)
7460{
7461 while (*el != NULL)
7462 {
7463 endlabel_T *cur = (*el);
7464
7465 *el = cur->el_next;
7466 vim_free(cur);
7467 }
7468}
7469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470/*
7471 * Create a new scope and set up the generic items.
7472 */
7473 static scope_T *
7474new_scope(cctx_T *cctx, scopetype_T type)
7475{
7476 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7477
7478 if (scope == NULL)
7479 return NULL;
7480 scope->se_outer = cctx->ctx_scope;
7481 cctx->ctx_scope = scope;
7482 scope->se_type = type;
7483 scope->se_local_count = cctx->ctx_locals.ga_len;
7484 return scope;
7485}
7486
7487/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007488 * Free the current scope and go back to the outer scope.
7489 */
7490 static void
7491drop_scope(cctx_T *cctx)
7492{
7493 scope_T *scope = cctx->ctx_scope;
7494
7495 if (scope == NULL)
7496 {
7497 iemsg("calling drop_scope() without a scope");
7498 return;
7499 }
7500 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007501 switch (scope->se_type)
7502 {
7503 case IF_SCOPE:
7504 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7505 case FOR_SCOPE:
7506 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7507 case WHILE_SCOPE:
7508 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7509 case TRY_SCOPE:
7510 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7511 case NO_SCOPE:
7512 case BLOCK_SCOPE:
7513 break;
7514 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007515 vim_free(scope);
7516}
7517
7518/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 * compile "if expr"
7520 *
7521 * "if expr" Produces instructions:
7522 * EVAL expr Push result of "expr"
7523 * JUMP_IF_FALSE end
7524 * ... body ...
7525 * end:
7526 *
7527 * "if expr | else" Produces instructions:
7528 * EVAL expr Push result of "expr"
7529 * JUMP_IF_FALSE else
7530 * ... body ...
7531 * JUMP_ALWAYS end
7532 * else:
7533 * ... body ...
7534 * end:
7535 *
7536 * "if expr1 | elseif expr2 | else" Produces instructions:
7537 * EVAL expr Push result of "expr"
7538 * JUMP_IF_FALSE elseif
7539 * ... body ...
7540 * JUMP_ALWAYS end
7541 * elseif:
7542 * EVAL expr Push result of "expr"
7543 * JUMP_IF_FALSE else
7544 * ... body ...
7545 * JUMP_ALWAYS end
7546 * else:
7547 * ... body ...
7548 * end:
7549 */
7550 static char_u *
7551compile_if(char_u *arg, cctx_T *cctx)
7552{
7553 char_u *p = arg;
7554 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007555 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007557 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007558 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559
Bram Moolenaara5565e42020-05-09 15:44:01 +02007560 CLEAR_FIELD(ppconst);
7561 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007562 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007563 clear_ppconst(&ppconst);
7564 return NULL;
7565 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007566 if (!ends_excmd2(arg, skipwhite(p)))
7567 {
7568 semsg(_(e_trailing_arg), p);
7569 return NULL;
7570 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007571 if (cctx->ctx_skip == SKIP_YES)
7572 clear_ppconst(&ppconst);
7573 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007574 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007575 int error = FALSE;
7576 int v;
7577
Bram Moolenaara5565e42020-05-09 15:44:01 +02007578 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007579 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007580 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007581 if (error)
7582 return NULL;
7583 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007584 }
7585 else
7586 {
7587 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007588 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007589 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007590 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007591 if (bool_on_stack(cctx) == FAIL)
7592 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594
Bram Moolenaara91a7132021-03-25 21:12:15 +01007595 // CMDMOD_REV must come before the jump
7596 generate_undo_cmdmods(cctx);
7597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 scope = new_scope(cctx, IF_SCOPE);
7599 if (scope == NULL)
7600 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007601 scope->se_skip_save = skip_save;
7602 // "is_had_return" will be reset if any block does not end in :return
7603 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007605 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007606 {
7607 // "where" is set when ":elseif", "else" or ":endif" is found
7608 scope->se_u.se_if.is_if_label = instr->ga_len;
7609 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7610 }
7611 else
7612 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613
Bram Moolenaarced68a02021-01-24 17:53:47 +01007614#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007615 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007616 && skip_save != SKIP_YES)
7617 {
7618 // generated a profile start, need to generate a profile end, since it
7619 // won't be done after returning
7620 cctx->ctx_skip = SKIP_NOT;
7621 generate_instr(cctx, ISN_PROF_END);
7622 cctx->ctx_skip = SKIP_YES;
7623 }
7624#endif
7625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 return p;
7627}
7628
7629 static char_u *
7630compile_elseif(char_u *arg, cctx_T *cctx)
7631{
7632 char_u *p = arg;
7633 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007634 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 isn_T *isn;
7636 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007637 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007638 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639
7640 if (scope == NULL || scope->se_type != IF_SCOPE)
7641 {
7642 emsg(_(e_elseif_without_if));
7643 return NULL;
7644 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007645 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007646 if (!cctx->ctx_had_return)
7647 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648
Bram Moolenaarced68a02021-01-24 17:53:47 +01007649 if (cctx->ctx_skip == SKIP_NOT)
7650 {
7651 // previous block was executed, this one and following will not
7652 cctx->ctx_skip = SKIP_YES;
7653 scope->se_u.se_if.is_seen_skip_not = TRUE;
7654 }
7655 if (scope->se_u.se_if.is_seen_skip_not)
7656 {
7657 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007658 // Do not count the "elseif" for profiling and cmdmod
7659 instr->ga_len = current_instr_idx(cctx);
7660
Bram Moolenaarced68a02021-01-24 17:53:47 +01007661 skip_expr_cctx(&p, cctx);
7662 return p;
7663 }
7664
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007665 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007666 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007667 int moved_cmdmod = FALSE;
7668
7669 // Move any CMDMOD instruction to after the jump
7670 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7671 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007672 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007673 return NULL;
7674 ((isn_T *)instr->ga_data)[instr->ga_len] =
7675 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7676 --instr->ga_len;
7677 moved_cmdmod = TRUE;
7678 }
7679
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007680 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007682 return NULL;
7683 // previous "if" or "elseif" jumps here
7684 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7685 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007686 if (moved_cmdmod)
7687 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007688 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007690 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007691 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007692 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007693 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007694 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007695#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007696 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007697 {
7698 // the previous block was skipped, need to profile this line
7699 generate_instr(cctx, ISN_PROF_START);
7700 instr_count = instr->ga_len;
7701 }
7702#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007703 if (cctx->ctx_compile_type == CT_DEBUG)
7704 {
7705 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007706 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007707 instr_count = instr->ga_len;
7708 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007709 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007710 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007711 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007712 clear_ppconst(&ppconst);
7713 return NULL;
7714 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007715 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007716 if (!ends_excmd2(arg, skipwhite(p)))
7717 {
7718 semsg(_(e_trailing_arg), p);
7719 return NULL;
7720 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007721 if (scope->se_skip_save == SKIP_YES)
7722 clear_ppconst(&ppconst);
7723 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007724 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007725 int error = FALSE;
7726 int v;
7727
Bram Moolenaar7f141552020-05-09 17:35:53 +02007728 // The expression results in a constant.
7729 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007730 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7731 if (error)
7732 return NULL;
7733 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007734 clear_ppconst(&ppconst);
7735 scope->se_u.se_if.is_if_label = -1;
7736 }
7737 else
7738 {
7739 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007740 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007741 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007742 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007743 if (bool_on_stack(cctx) == FAIL)
7744 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745
Bram Moolenaara91a7132021-03-25 21:12:15 +01007746 // CMDMOD_REV must come before the jump
7747 generate_undo_cmdmods(cctx);
7748
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007749 // "where" is set when ":elseif", "else" or ":endif" is found
7750 scope->se_u.se_if.is_if_label = instr->ga_len;
7751 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753
7754 return p;
7755}
7756
7757 static char_u *
7758compile_else(char_u *arg, cctx_T *cctx)
7759{
7760 char_u *p = arg;
7761 garray_T *instr = &cctx->ctx_instr;
7762 isn_T *isn;
7763 scope_T *scope = cctx->ctx_scope;
7764
7765 if (scope == NULL || scope->se_type != IF_SCOPE)
7766 {
7767 emsg(_(e_else_without_if));
7768 return NULL;
7769 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007770 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007771 if (!cctx->ctx_had_return)
7772 scope->se_u.se_if.is_had_return = FALSE;
7773 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774
Bram Moolenaarced68a02021-01-24 17:53:47 +01007775#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007776 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007777 {
7778 if (cctx->ctx_skip == SKIP_NOT
7779 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7780 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007781 // the previous block was executed, do not count "else" for
7782 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007783 --instr->ga_len;
7784 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7785 {
7786 // the previous block was not executed, this one will, do count the
7787 // "else" for profiling
7788 cctx->ctx_skip = SKIP_NOT;
7789 generate_instr(cctx, ISN_PROF_END);
7790 generate_instr(cctx, ISN_PROF_START);
7791 cctx->ctx_skip = SKIP_YES;
7792 }
7793 }
7794#endif
7795
7796 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007797 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007798 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007799 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007800 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007801 if (!cctx->ctx_had_return
7802 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7803 JUMP_ALWAYS, cctx) == FAIL)
7804 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007805 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007806
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007807 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007808 {
7809 if (scope->se_u.se_if.is_if_label >= 0)
7810 {
7811 // previous "if" or "elseif" jumps here
7812 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7813 isn->isn_arg.jump.jump_where = instr->ga_len;
7814 scope->se_u.se_if.is_if_label = -1;
7815 }
7816 }
7817
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007818 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007819 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821
7822 return p;
7823}
7824
7825 static char_u *
7826compile_endif(char_u *arg, cctx_T *cctx)
7827{
7828 scope_T *scope = cctx->ctx_scope;
7829 ifscope_T *ifscope;
7830 garray_T *instr = &cctx->ctx_instr;
7831 isn_T *isn;
7832
Bram Moolenaarfa984412021-03-25 22:15:28 +01007833 if (misplaced_cmdmod(cctx))
7834 return NULL;
7835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 if (scope == NULL || scope->se_type != IF_SCOPE)
7837 {
7838 emsg(_(e_endif_without_if));
7839 return NULL;
7840 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007841 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007842 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007843 if (!cctx->ctx_had_return)
7844 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007846 if (scope->se_u.se_if.is_if_label >= 0)
7847 {
7848 // previous "if" or "elseif" jumps here
7849 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7850 isn->isn_arg.jump.jump_where = instr->ga_len;
7851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007853 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007854
7855#ifdef FEAT_PROFILE
7856 // even when skipping we count the endif as executed, unless the block it's
7857 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007858 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007859 && scope->se_skip_save != SKIP_YES)
7860 {
7861 cctx->ctx_skip = SKIP_NOT;
7862 generate_instr(cctx, ISN_PROF_START);
7863 }
7864#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007865 cctx->ctx_skip = scope->se_skip_save;
7866
7867 // If all the blocks end in :return and there is an :else then the
7868 // had_return flag is set.
7869 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007871 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 return arg;
7873}
7874
7875/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007876 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 *
7878 * Produces instructions:
7879 * PUSHNR -1
7880 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007881 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7883 * - if beyond end, jump to "end"
7884 * - otherwise get item from list and push it
7885 * STORE var Store item in "var"
7886 * ... body ...
7887 * JUMP top Jump back to repeat
7888 * end: DROP Drop the result of "expr"
7889 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007890 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7891 * UNPACK 2 Split item in 2
7892 * STORE var1 Store item in "var1"
7893 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 */
7895 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007896compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007897{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007898 char_u *arg;
7899 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007900 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007902 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007903 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007904 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007905 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007907 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007908 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007909 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007910 lvar_T *loop_lvar; // loop iteration variable
7911 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007913 type_T *item_type = &t_any;
7914 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007915 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916
Bram Moolenaar792f7862020-11-23 08:31:18 +01007917 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007918 if (p == NULL)
7919 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007920 if (var_count == 0)
7921 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007922 else
7923 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924
7925 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007926 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007927 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7928 return NULL;
7929 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007930 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007931 if (*p == ':' && wp != p)
7932 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7933 else
7934 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 return NULL;
7936 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007937 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007938 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7939 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007941 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7942 // instruction.
7943 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7944 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7945 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007946 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007947 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007948 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7949 .isn_arg.debug.dbg_break_lnum;
7950 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952 scope = new_scope(cctx, FOR_SCOPE);
7953 if (scope == NULL)
7954 return NULL;
7955
Bram Moolenaar792f7862020-11-23 08:31:18 +01007956 // Reserve a variable to store the loop iteration counter and initialize it
7957 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007958 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7959 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007960 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007961 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007962 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007964 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007965 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966
7967 // compile "expr", it remains on the stack until "endfor"
7968 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007969 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007970 {
7971 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007973 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007974 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007976 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007977 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007979 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007980 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007982 semsg(_(e_for_loop_on_str_not_supported),
7983 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007984 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 return NULL;
7986 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007987
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007988 if (vartype->tt_type == VAR_STRING)
7989 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007990 else if (vartype->tt_type == VAR_BLOB)
7991 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007992 else if (vartype->tt_type == VAR_LIST
7993 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007994 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007995 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007996 item_type = vartype->tt_member;
7997 else if (vartype->tt_member->tt_type == VAR_LIST
7998 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007999 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01008000 item_type = vartype->tt_member->tt_member;
8001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008003 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01008004 generate_undo_cmdmods(cctx);
8005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008006 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01008007 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008008
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008009 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010
Bram Moolenaar792f7862020-11-23 08:31:18 +01008011 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008012 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008013 {
8014 generate_UNPACK(cctx, var_count, semicolon);
8015 arg = skipwhite(arg + 1); // skip white after '['
8016
8017 // the list item is replaced by a number of items
Bram Moolenaar35578162021-08-02 19:10:38 +02008018 if (GA_GROW_FAILS(stack, var_count - 1))
Bram Moolenaar792f7862020-11-23 08:31:18 +01008019 {
8020 drop_scope(cctx);
8021 return NULL;
8022 }
8023 --stack->ga_len;
8024 for (idx = 0; idx < var_count; ++idx)
8025 {
8026 ((type_T **)stack->ga_data)[stack->ga_len] =
8027 (semicolon && idx == 0) ? vartype : item_type;
8028 ++stack->ga_len;
8029 }
8030 }
8031
8032 for (idx = 0; idx < var_count; ++idx)
8033 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008034 assign_dest_T dest = dest_local;
8035 int opt_flags = 0;
8036 int vimvaridx = -1;
8037 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008038 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008039 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008040
8041 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008042 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008043 name = vim_strnsave(arg, varlen);
8044 if (name == NULL)
8045 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008046 if (*p == ':')
8047 {
8048 p = skipwhite(p + 1);
8049 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8050 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008051
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008052 // TODO: script var not supported?
8053 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8054 &vimvaridx, &type, cctx) == FAIL)
8055 goto failed;
8056 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008057 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008058 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8059 0, 0, type, name) == FAIL)
8060 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008061 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008062 else if (varlen == 1 && *arg == '_')
8063 {
8064 // Assigning to "_": drop the value.
8065 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8066 goto failed;
8067 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008068 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008069 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008070 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008071 {
8072 semsg(_(e_variable_already_declared), arg);
8073 goto failed;
8074 }
8075
Bram Moolenaarea870692020-12-02 14:24:30 +01008076 if (STRNCMP(name, "s:", 2) == 0)
8077 {
8078 semsg(_(e_cannot_declare_script_variable_in_function), name);
8079 goto failed;
8080 }
8081
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008082 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008083 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008084 where.wt_variable = TRUE;
8085 if (lhs_type == &t_any)
8086 lhs_type = item_type;
8087 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008088 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008089 ? need_type(item_type, lhs_type,
8090 -1, 0, cctx, FALSE, FALSE)
8091 : check_type(lhs_type, item_type, TRUE, where))
8092 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008093 goto failed;
8094 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008095 if (var_lvar == NULL)
8096 // out of memory or used as an argument
8097 goto failed;
8098
8099 if (semicolon && idx == var_count - 1)
8100 var_lvar->lv_type = vartype;
8101 else
8102 var_lvar->lv_type = item_type;
8103 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8104 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008105
8106 if (*p == ',' || *p == ';')
8107 ++p;
8108 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008109 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008110 }
8111
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008112 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008113 {
8114 int save_prev_lnum = cctx->ctx_prev_lnum;
8115
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008116 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008117 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8118 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008119 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008120 cctx->ctx_prev_lnum = save_prev_lnum;
8121 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008122
Bram Moolenaar792f7862020-11-23 08:31:18 +01008123 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008124
8125failed:
8126 vim_free(name);
8127 drop_scope(cctx);
8128 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129}
8130
8131/*
8132 * compile "endfor"
8133 */
8134 static char_u *
8135compile_endfor(char_u *arg, cctx_T *cctx)
8136{
8137 garray_T *instr = &cctx->ctx_instr;
8138 scope_T *scope = cctx->ctx_scope;
8139 forscope_T *forscope;
8140 isn_T *isn;
8141
Bram Moolenaarfa984412021-03-25 22:15:28 +01008142 if (misplaced_cmdmod(cctx))
8143 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 if (scope == NULL || scope->se_type != FOR_SCOPE)
8146 {
8147 emsg(_(e_for));
8148 return NULL;
8149 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008150 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008152 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153
8154 // At end of ":for" scope jump back to the FOR instruction.
8155 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8156
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008157 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8159 isn->isn_arg.forloop.for_end = instr->ga_len;
8160
8161 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008162 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163
8164 // Below the ":for" scope drop the "expr" list from the stack.
8165 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8166 return NULL;
8167
8168 vim_free(scope);
8169
8170 return arg;
8171}
8172
8173/*
8174 * compile "while expr"
8175 *
8176 * Produces instructions:
8177 * top: EVAL expr Push result of "expr"
8178 * JUMP_IF_FALSE end jump if false
8179 * ... body ...
8180 * JUMP top Jump back to repeat
8181 * end:
8182 *
8183 */
8184 static char_u *
8185compile_while(char_u *arg, cctx_T *cctx)
8186{
8187 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 scope_T *scope;
8189
8190 scope = new_scope(cctx, WHILE_SCOPE);
8191 if (scope == NULL)
8192 return NULL;
8193
Bram Moolenaara91a7132021-03-25 21:12:15 +01008194 // "endwhile" jumps back here, one before when profiling or using cmdmods
8195 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196
8197 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008198 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008200 if (!ends_excmd2(arg, skipwhite(p)))
8201 {
8202 semsg(_(e_trailing_arg), p);
8203 return NULL;
8204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205
Bram Moolenaar13106602020-10-04 16:06:05 +02008206 if (bool_on_stack(cctx) == FAIL)
8207 return FAIL;
8208
Bram Moolenaara91a7132021-03-25 21:12:15 +01008209 // CMDMOD_REV must come before the jump
8210 generate_undo_cmdmods(cctx);
8211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008213 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 JUMP_IF_FALSE, cctx) == FAIL)
8215 return FAIL;
8216
8217 return p;
8218}
8219
8220/*
8221 * compile "endwhile"
8222 */
8223 static char_u *
8224compile_endwhile(char_u *arg, cctx_T *cctx)
8225{
8226 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008227 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228
Bram Moolenaarfa984412021-03-25 22:15:28 +01008229 if (misplaced_cmdmod(cctx))
8230 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8232 {
8233 emsg(_(e_while));
8234 return NULL;
8235 }
8236 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008237 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238
Bram Moolenaarf002a412021-01-24 13:34:18 +01008239#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008240 // count the endwhile before jumping
8241 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008242#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008245 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008246
8247 // Fill in the "end" label in the WHILE statement so it can jump here.
8248 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008249 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8250 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008251
8252 vim_free(scope);
8253
8254 return arg;
8255}
8256
8257/*
8258 * compile "continue"
8259 */
8260 static char_u *
8261compile_continue(char_u *arg, cctx_T *cctx)
8262{
8263 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008264 int try_scopes = 0;
8265 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266
8267 for (;;)
8268 {
8269 if (scope == NULL)
8270 {
8271 emsg(_(e_continue));
8272 return NULL;
8273 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008274 if (scope->se_type == FOR_SCOPE)
8275 {
8276 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008278 }
8279 if (scope->se_type == WHILE_SCOPE)
8280 {
8281 loop_label = scope->se_u.se_while.ws_top_label;
8282 break;
8283 }
8284 if (scope->se_type == TRY_SCOPE)
8285 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 scope = scope->se_outer;
8287 }
8288
Bram Moolenaarc150c092021-02-13 15:02:46 +01008289 if (try_scopes > 0)
8290 // Inside one or more try/catch blocks we first need to jump to the
8291 // "finally" or "endtry" to cleanup.
8292 generate_TRYCONT(cctx, try_scopes, loop_label);
8293 else
8294 // Jump back to the FOR or WHILE instruction.
8295 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 return arg;
8298}
8299
8300/*
8301 * compile "break"
8302 */
8303 static char_u *
8304compile_break(char_u *arg, cctx_T *cctx)
8305{
8306 scope_T *scope = cctx->ctx_scope;
8307 endlabel_T **el;
8308
8309 for (;;)
8310 {
8311 if (scope == NULL)
8312 {
8313 emsg(_(e_break));
8314 return NULL;
8315 }
8316 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8317 break;
8318 scope = scope->se_outer;
8319 }
8320
8321 // Jump to the end of the FOR or WHILE loop.
8322 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008323 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008324 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008325 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008326 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8327 return FAIL;
8328
8329 return arg;
8330}
8331
8332/*
8333 * compile "{" start of block
8334 */
8335 static char_u *
8336compile_block(char_u *arg, cctx_T *cctx)
8337{
8338 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8339 return NULL;
8340 return skipwhite(arg + 1);
8341}
8342
8343/*
8344 * compile end of block: drop one scope
8345 */
8346 static void
8347compile_endblock(cctx_T *cctx)
8348{
8349 scope_T *scope = cctx->ctx_scope;
8350
8351 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008352 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008353 vim_free(scope);
8354}
8355
8356/*
8357 * compile "try"
8358 * Creates a new scope for the try-endtry, pointing to the first catch and
8359 * finally.
8360 * Creates another scope for the "try" block itself.
8361 * TRY instruction sets up exception handling at runtime.
8362 *
8363 * "try"
8364 * TRY -> catch1, -> finally push trystack entry
8365 * ... try block
8366 * "throw {exception}"
8367 * EVAL {exception}
8368 * THROW create exception
8369 * ... try block
8370 * " catch {expr}"
8371 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008372 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 * EVAL {expr}
8374 * MATCH
8375 * JUMP nomatch -> catch2
8376 * CATCH remove exception
8377 * ... catch block
8378 * " catch"
8379 * JUMP -> finally
8380 * catch2: CATCH remove exception
8381 * ... catch block
8382 * " finally"
8383 * finally:
8384 * ... finally block
8385 * " endtry"
8386 * ENDTRY pop trystack entry, may rethrow
8387 */
8388 static char_u *
8389compile_try(char_u *arg, cctx_T *cctx)
8390{
8391 garray_T *instr = &cctx->ctx_instr;
8392 scope_T *try_scope;
8393 scope_T *scope;
8394
Bram Moolenaarfa984412021-03-25 22:15:28 +01008395 if (misplaced_cmdmod(cctx))
8396 return NULL;
8397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008398 // scope that holds the jumps that go to catch/finally/endtry
8399 try_scope = new_scope(cctx, TRY_SCOPE);
8400 if (try_scope == NULL)
8401 return NULL;
8402
Bram Moolenaar69f70502021-01-01 16:10:46 +01008403 if (cctx->ctx_skip != SKIP_YES)
8404 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008405 isn_T *isn;
8406
8407 // "try_catch" is set when the first ":catch" is found or when no catch
8408 // is found and ":finally" is found.
8409 // "try_finally" is set when ":finally" is found
8410 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008411 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008412 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8413 return NULL;
8414 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8415 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008416 return NULL;
8417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418
8419 // scope for the try block itself
8420 scope = new_scope(cctx, BLOCK_SCOPE);
8421 if (scope == NULL)
8422 return NULL;
8423
8424 return arg;
8425}
8426
8427/*
8428 * compile "catch {expr}"
8429 */
8430 static char_u *
8431compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8432{
8433 scope_T *scope = cctx->ctx_scope;
8434 garray_T *instr = &cctx->ctx_instr;
8435 char_u *p;
8436 isn_T *isn;
8437
Bram Moolenaarfa984412021-03-25 22:15:28 +01008438 if (misplaced_cmdmod(cctx))
8439 return NULL;
8440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 // end block scope from :try or :catch
8442 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8443 compile_endblock(cctx);
8444 scope = cctx->ctx_scope;
8445
8446 // Error if not in a :try scope
8447 if (scope == NULL || scope->se_type != TRY_SCOPE)
8448 {
8449 emsg(_(e_catch));
8450 return NULL;
8451 }
8452
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008453 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008454 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008455 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 return NULL;
8457 }
8458
Bram Moolenaar69f70502021-01-01 16:10:46 +01008459 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008461#ifdef FEAT_PROFILE
8462 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008463 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008464 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008465 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008466 .isn_type == ISN_PROF_START)
8467 --instr->ga_len;
8468#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008469 // Jump from end of previous block to :finally or :endtry
8470 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8471 JUMP_ALWAYS, cctx) == FAIL)
8472 return NULL;
8473
8474 // End :try or :catch scope: set value in ISN_TRY instruction
8475 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008476 if (isn->isn_arg.try.try_ref->try_catch == 0)
8477 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008478 if (scope->se_u.se_try.ts_catch_label != 0)
8479 {
8480 // Previous catch without match jumps here
8481 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8482 isn->isn_arg.jump.jump_where = instr->ga_len;
8483 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008484#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008485 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008486 {
8487 // a "throw" that jumps here needs to be counted
8488 generate_instr(cctx, ISN_PROF_END);
8489 // the "catch" is also counted
8490 generate_instr(cctx, ISN_PROF_START);
8491 }
8492#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008493 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008494 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495 }
8496
8497 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008498 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008500 scope->se_u.se_try.ts_caught_all = TRUE;
8501 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 }
8503 else
8504 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008505 char_u *end;
8506 char_u *pat;
8507 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008508 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008509 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 // Push v:exception, push {expr} and MATCH
8512 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8513
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008514 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008515 if (*end != *p)
8516 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008517 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008518 vim_free(tofree);
8519 return FAIL;
8520 }
8521 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008522 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008523 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008524 len = (int)(end - tofree);
8525 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008526 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008527 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008528 if (pat == NULL)
8529 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008530 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008531 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8534 return NULL;
8535
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008536 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008537 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8538 return NULL;
8539 }
8540
Bram Moolenaar69f70502021-01-01 16:10:46 +01008541 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 return NULL;
8543
8544 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8545 return NULL;
8546 return p;
8547}
8548
8549 static char_u *
8550compile_finally(char_u *arg, cctx_T *cctx)
8551{
8552 scope_T *scope = cctx->ctx_scope;
8553 garray_T *instr = &cctx->ctx_instr;
8554 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008555 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556
Bram Moolenaarfa984412021-03-25 22:15:28 +01008557 if (misplaced_cmdmod(cctx))
8558 return NULL;
8559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 // end block scope from :try or :catch
8561 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8562 compile_endblock(cctx);
8563 scope = cctx->ctx_scope;
8564
8565 // Error if not in a :try scope
8566 if (scope == NULL || scope->se_type != TRY_SCOPE)
8567 {
8568 emsg(_(e_finally));
8569 return NULL;
8570 }
8571
8572 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008573 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008574 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 {
8576 emsg(_(e_finally_dup));
8577 return NULL;
8578 }
8579
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008580 this_instr = instr->ga_len;
8581#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008582 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008583 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008584 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008585 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008586 // jump to the profile start of the "finally"
8587 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008588
8589 // jump to the profile end above it
8590 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8591 .isn_type == ISN_PROF_END)
8592 --this_instr;
8593 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008594#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008595
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008596 // Fill in the "end" label in jumps at the end of the blocks.
8597 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8598 this_instr, cctx);
8599
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008600 // If there is no :catch then an exception jumps to :finally.
8601 if (isn->isn_arg.try.try_ref->try_catch == 0)
8602 isn->isn_arg.try.try_ref->try_catch = this_instr;
8603 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008604 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605 {
8606 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008607 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008608 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008609 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008611 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8612 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614 // TODO: set index in ts_finally_label jumps
8615
8616 return arg;
8617}
8618
8619 static char_u *
8620compile_endtry(char_u *arg, cctx_T *cctx)
8621{
8622 scope_T *scope = cctx->ctx_scope;
8623 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008624 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008625
Bram Moolenaarfa984412021-03-25 22:15:28 +01008626 if (misplaced_cmdmod(cctx))
8627 return NULL;
8628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008629 // end block scope from :catch or :finally
8630 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8631 compile_endblock(cctx);
8632 scope = cctx->ctx_scope;
8633
8634 // Error if not in a :try scope
8635 if (scope == NULL || scope->se_type != TRY_SCOPE)
8636 {
8637 if (scope == NULL)
8638 emsg(_(e_no_endtry));
8639 else if (scope->se_type == WHILE_SCOPE)
8640 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008641 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008642 emsg(_(e_endfor));
8643 else
8644 emsg(_(e_endif));
8645 return NULL;
8646 }
8647
Bram Moolenaarc150c092021-02-13 15:02:46 +01008648 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008649 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008651 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8652 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008653 {
8654 emsg(_(e_missing_catch_or_finally));
8655 return NULL;
8656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008658#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008659 if (cctx->ctx_compile_type == CT_PROFILE
8660 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008661 .isn_type == ISN_PROF_START)
8662 // move the profile start after "endtry" so that it's not counted when
8663 // the exception is rethrown.
8664 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008665#endif
8666
Bram Moolenaar69f70502021-01-01 16:10:46 +01008667 // Fill in the "end" label in jumps at the end of the blocks, if not
8668 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008669 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8670 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671
Bram Moolenaar69f70502021-01-01 16:10:46 +01008672 if (scope->se_u.se_try.ts_catch_label != 0)
8673 {
8674 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008675 isn_T *isn = ((isn_T *)instr->ga_data)
8676 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008677 isn->isn_arg.jump.jump_where = instr->ga_len;
8678 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008679 }
8680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008681 compile_endblock(cctx);
8682
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008683 if (cctx->ctx_skip != SKIP_YES)
8684 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008685 // End :catch or :finally scope: set instruction index in ISN_TRY
8686 // instruction
8687 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008688 if (cctx->ctx_skip != SKIP_YES
8689 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8690 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008691#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008692 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008693 generate_instr(cctx, ISN_PROF_START);
8694#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696 return arg;
8697}
8698
8699/*
8700 * compile "throw {expr}"
8701 */
8702 static char_u *
8703compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8704{
8705 char_u *p = skipwhite(arg);
8706
Bram Moolenaara5565e42020-05-09 15:44:01 +02008707 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008708 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008709 if (cctx->ctx_skip == SKIP_YES)
8710 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008711 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008712 return NULL;
8713 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8714 return NULL;
8715
8716 return p;
8717}
8718
Bram Moolenaarc3235272021-07-10 19:42:03 +02008719 static char_u *
8720compile_eval(char_u *arg, cctx_T *cctx)
8721{
8722 char_u *p = arg;
8723 int name_only;
8724 char_u *alias;
8725 long lnum = SOURCING_LNUM;
8726
8727 // find_ex_command() will consider a variable name an expression, assuming
8728 // that something follows on the next line. Check that something actually
8729 // follows, otherwise it's probably a misplaced command.
8730 get_name_len(&p, &alias, FALSE, FALSE);
8731 name_only = ends_excmd2(arg, skipwhite(p));
8732 vim_free(alias);
8733
8734 p = arg;
8735 if (compile_expr0(&p, cctx) == FAIL)
8736 return NULL;
8737
8738 if (name_only && lnum == SOURCING_LNUM)
8739 {
8740 semsg(_(e_expression_without_effect_str), arg);
8741 return NULL;
8742 }
8743
8744 // drop the result
8745 generate_instr_drop(cctx, ISN_DROP, 1);
8746
8747 return skipwhite(p);
8748}
8749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750/*
8751 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008752 * compile "echomsg expr"
8753 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008754 * compile "execute expr"
8755 */
8756 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008757compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008758{
8759 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008760 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008761 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008762 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008763 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008764 garray_T *stack = &cctx->ctx_type_stack;
8765 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008766
8767 for (;;)
8768 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008769 if (ends_excmd2(prev, p))
8770 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008771 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008772 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008773 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008774
8775 if (cctx->ctx_skip != SKIP_YES)
8776 {
8777 // check for non-void type
8778 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8779 if (type->tt_type == VAR_VOID)
8780 {
8781 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8782 return NULL;
8783 }
8784 }
8785
Bram Moolenaarad39c092020-02-26 18:23:43 +01008786 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008787 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008788 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008789 }
8790
Bram Moolenaare4984292020-12-13 14:19:25 +01008791 if (count > 0)
8792 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008793 long save_lnum = cctx->ctx_lnum;
8794
8795 // Use the line number where the command started.
8796 cctx->ctx_lnum = start_ctx_lnum;
8797
Bram Moolenaare4984292020-12-13 14:19:25 +01008798 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8799 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8800 else if (cmdidx == CMD_execute)
8801 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8802 else if (cmdidx == CMD_echomsg)
8803 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8804 else
8805 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008806
8807 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 return p;
8810}
8811
8812/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008813 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008814 * instruction to compute it and return OK.
8815 * Otherwise return FAIL, the caller must deal with any range.
8816 */
8817 static int
8818compile_variable_range(exarg_T *eap, cctx_T *cctx)
8819{
8820 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8821 char_u *p = skipdigits(eap->cmd);
8822
8823 if (p == range_end)
8824 return FAIL;
8825 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8826}
8827
8828/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008829 * :put r
8830 * :put ={expr}
8831 */
8832 static char_u *
8833compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8834{
8835 char_u *line = arg;
8836 linenr_T lnum;
8837 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008838 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008839
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008840 eap->regname = *line;
8841
8842 if (eap->regname == '=')
8843 {
8844 char_u *p = line + 1;
8845
8846 if (compile_expr0(&p, cctx) == FAIL)
8847 return NULL;
8848 line = p;
8849 }
8850 else if (eap->regname != NUL)
8851 ++line;
8852
Bram Moolenaar08597872020-12-10 19:43:40 +01008853 if (compile_variable_range(eap, cctx) == OK)
8854 {
8855 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8856 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008857 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008858 {
8859 // Either no range or a number.
8860 // "errormsg" will not be set because the range is ADDR_LINES.
8861 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008862 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008863 return NULL;
8864 if (eap->addr_count == 0)
8865 lnum = -1;
8866 else
8867 lnum = eap->line2;
8868 if (above)
8869 --lnum;
8870 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008871
8872 generate_PUT(cctx, eap->regname, lnum);
8873 return line;
8874}
8875
8876/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008877 * A command that is not compiled, execute with legacy code.
8878 */
8879 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008880compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008881{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008882 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02008883 char_u *p;
8884 int has_expr = FALSE;
8885 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008886 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008887
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008888 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008889 goto theend;
8890
Bram Moolenaare729ce22021-06-06 21:38:09 +02008891 // If there was a prececing command modifier, drop it and include it in the
8892 // EXEC command.
8893 if (cctx->ctx_has_cmdmod)
8894 {
8895 garray_T *instr = &cctx->ctx_instr;
8896 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8897
8898 if (isn->isn_type == ISN_CMDMOD)
8899 {
8900 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8901 ->cmod_filter_regmatch.regprog);
8902 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8903 --instr->ga_len;
8904 cctx->ctx_has_cmdmod = FALSE;
8905 }
8906 }
8907
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008908 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008909 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008910 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008911 int usefilter = FALSE;
8912
8913 has_expr = argt & (EX_XFILE | EX_EXPAND);
8914
8915 // If the command can be followed by a bar, find the bar and truncate
8916 // it, so that the following command can be compiled.
8917 // The '|' is overwritten with a NUL, it is put back below.
8918 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8919 && *eap->arg == '!')
8920 // :w !filter or :r !filter or :r! filter
8921 usefilter = TRUE;
8922 if ((argt & EX_TRLBAR) && !usefilter)
8923 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008924 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008925 separate_nextcmd(eap);
8926 if (eap->nextcmd != NULL)
8927 nextcmd = eap->nextcmd;
8928 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008929 else if (eap->cmdidx == CMD_wincmd)
8930 {
8931 p = eap->arg;
8932 if (*p != NUL)
8933 ++p;
8934 if (*p == 'g' || *p == Ctrl_G)
8935 ++p;
8936 p = skipwhite(p);
8937 if (*p == '|')
8938 {
8939 *p = NUL;
8940 nextcmd = p + 1;
8941 }
8942 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008943 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
8944 {
8945 // If there is a trailing '{' read lines until the '}'
8946 p = eap->arg + STRLEN(eap->arg) - 1;
8947 while (p > eap->arg && VIM_ISWHITE(*p))
8948 --p;
8949 if (*p == '{')
8950 {
8951 exarg_T ea;
8952 int flags; // unused
8953 int start_lnum = SOURCING_LNUM;
8954
8955 CLEAR_FIELD(ea);
8956 ea.arg = eap->arg;
8957 fill_exarg_from_cctx(&ea, cctx);
8958 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
8959 if (tofree != NULL)
8960 {
8961 *p = NUL;
8962 line = concat_str(line, tofree);
8963 if (line == NULL)
8964 goto theend;
8965 vim_free(tofree);
8966 tofree = line;
8967 SOURCING_LNUM = start_lnum;
8968 }
8969 }
8970 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008971 }
8972
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008973 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8974 {
8975 // expand filename in "syntax include [@group] filename"
8976 has_expr = TRUE;
8977 eap->arg = skipwhite(eap->arg + 7);
8978 if (*eap->arg == '@')
8979 eap->arg = skiptowhite(eap->arg);
8980 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008981
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008982 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8983 && STRLEN(eap->arg) > 4)
8984 {
8985 int delim = *eap->arg;
8986
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008987 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008988 if (*p == delim)
8989 {
8990 eap->arg = p + 1;
8991 has_expr = TRUE;
8992 }
8993 }
8994
Bram Moolenaarecac5912021-01-05 19:23:28 +01008995 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8996 {
8997 // TODO: should only expand when appropriate for the command
8998 eap->arg = skiptowhite(eap->arg);
8999 has_expr = TRUE;
9000 }
9001
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009002 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009003 {
9004 int count = 0;
9005 char_u *start = skipwhite(line);
9006
9007 // :cmd xxx`=expr1`yyy`=expr2`zzz
9008 // PUSHS ":cmd xxx"
9009 // eval expr1
9010 // PUSHS "yyy"
9011 // eval expr2
9012 // PUSHS "zzz"
9013 // EXECCONCAT 5
9014 for (;;)
9015 {
9016 if (p > start)
9017 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009018 char_u *val = vim_strnsave(start, p - start);
9019
9020 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009021 ++count;
9022 }
9023 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009024 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009025 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009026 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009027 ++count;
9028 p = skipwhite(p);
9029 if (*p != '`')
9030 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009031 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009032 return NULL;
9033 }
9034 start = p + 1;
9035
9036 p = (char_u *)strstr((char *)start, "`=");
9037 if (p == NULL)
9038 {
9039 if (*skipwhite(start) != NUL)
9040 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009041 char_u *val = vim_strsave(start);
9042
9043 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009044 ++count;
9045 }
9046 break;
9047 }
9048 }
9049 generate_EXECCONCAT(cctx, count);
9050 }
9051 else
9052 generate_EXEC(cctx, line);
9053
9054theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009055 if (*nextcmd != NUL)
9056 {
9057 // the parser expects a pointer to the bar, put it back
9058 --nextcmd;
9059 *nextcmd = '|';
9060 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009061 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009062
9063 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009064}
9065
Bram Moolenaar20677332021-06-06 17:02:53 +02009066/*
9067 * A script command with heredoc, e.g.
9068 * ruby << EOF
9069 * command
9070 * EOF
9071 * Has been turned into one long line with NL characters by
9072 * get_function_body():
9073 * ruby << EOF<NL> command<NL>EOF
9074 */
9075 static char_u *
9076compile_script(char_u *line, cctx_T *cctx)
9077{
9078 if (cctx->ctx_skip != SKIP_YES)
9079 {
9080 isn_T *isn;
9081
9082 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9083 return NULL;
9084 isn->isn_arg.string = vim_strsave(line);
9085 }
9086 return (char_u *)"";
9087}
9088
Bram Moolenaar8238f082021-04-20 21:10:48 +02009089
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009090/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009091 * :s/pat/repl/
9092 */
9093 static char_u *
9094compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9095{
9096 char_u *cmd = eap->arg;
9097 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9098
9099 if (expr != NULL)
9100 {
9101 int delimiter = *cmd++;
9102
9103 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009104 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009105 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9106 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009107 garray_T save_ga = cctx->ctx_instr;
9108 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009109 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009110 int trailing_error;
9111 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009112 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009113 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009114
9115 cmd += 3;
9116 end = skip_substitute(cmd, delimiter);
9117
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009118 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009119 // labels are correct.
9120 cctx->ctx_instr.ga_len = 0;
9121 cctx->ctx_instr.ga_maxlen = 0;
9122 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009123 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009124 if (end[-1] == NUL)
9125 end[-1] = delimiter;
9126 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009127 trailing_error = *cmd != delimiter && *cmd != NUL;
9128
Bram Moolenaar169502f2021-04-21 12:19:35 +02009129 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009130 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009131 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009132 if (trailing_error)
9133 semsg(_(e_trailing_arg), cmd);
9134 clear_instr_ga(&cctx->ctx_instr);
9135 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009136 return NULL;
9137 }
9138
Bram Moolenaar8238f082021-04-20 21:10:48 +02009139 // Move the generated instructions into the ISN_SUBSTITUTE
9140 // instructions, then restore the list of instructions before
9141 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009142 instr_count = cctx->ctx_instr.ga_len;
9143 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009144 instr[instr_count].isn_type = ISN_FINISH;
9145
9146 cctx->ctx_instr = save_ga;
9147 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9148 {
9149 int idx;
9150
9151 for (idx = 0; idx < instr_count; ++idx)
9152 delete_instr(instr + idx);
9153 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009154 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009155 }
9156 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9157 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009158
9159 // skip over flags
9160 if (*end == '&')
9161 ++end;
9162 while (ASCII_ISALPHA(*end) || *end == '#')
9163 ++end;
9164 return end;
9165 }
9166 }
9167
9168 return compile_exec(arg, eap, cctx);
9169}
9170
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009171 static char_u *
9172compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9173{
9174 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009175 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009176
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009177 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009178 {
9179 if (STRNCMP(arg, "END", 3) == 0)
9180 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009181 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009182 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009183 // First load the current variable value.
9184 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9185 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009186 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009187 }
9188
9189 // Gets the redirected text and put it on the stack, then store it
9190 // in the variable.
9191 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9192
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009193 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009194 generate_instr_drop(cctx, ISN_CONCAT, 1);
9195
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009196 if (lhs->lhs_has_index)
9197 {
9198 // Use the info in "lhs" to store the value at the index in the
9199 // list or dict.
9200 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9201 &t_string, cctx) == FAIL)
9202 return NULL;
9203 }
9204 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009205 return NULL;
9206
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009207 VIM_CLEAR(lhs->lhs_name);
9208 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009209 return arg + 3;
9210 }
9211 emsg(_(e_cannot_nest_redir));
9212 return NULL;
9213 }
9214
9215 if (arg[0] == '=' && arg[1] == '>')
9216 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009217 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009218
9219 // redirect to a variable is compiled
9220 arg += 2;
9221 if (*arg == '>')
9222 {
9223 ++arg;
9224 append = TRUE;
9225 }
9226 arg = skipwhite(arg);
9227
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009228 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009229 FALSE, FALSE, 1, cctx) == FAIL)
9230 return NULL;
9231 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009232 lhs->lhs_append = append;
9233 if (lhs->lhs_has_index)
9234 {
9235 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9236 if (lhs->lhs_whole == NULL)
9237 return NULL;
9238 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009239
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009240 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009241 }
9242
9243 // other redirects are handled like at script level
9244 return compile_exec(line, eap, cctx);
9245}
9246
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009247#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009248 static char_u *
9249compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9250{
9251 isn_T *isn;
9252 char_u *p;
9253
9254 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9255 if (isn == NULL)
9256 return NULL;
9257 isn->isn_arg.number = eap->cmdidx;
9258
9259 p = eap->arg;
9260 if (compile_expr0(&p, cctx) == FAIL)
9261 return NULL;
9262
9263 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9264 if (isn == NULL)
9265 return NULL;
9266 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9267 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9268 return NULL;
9269 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9270 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9271 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9272
9273 return p;
9274}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009275#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009276
Bram Moolenaar4c137212021-04-19 16:48:48 +02009277/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009278 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009279 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009280 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009281 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009282add_def_function(ufunc_T *ufunc)
9283{
9284 dfunc_T *dfunc;
9285
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009286 if (def_functions.ga_len == 0)
9287 {
9288 // The first position is not used, so that a zero uf_dfunc_idx means it
9289 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009290 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009291 return FAIL;
9292 ++def_functions.ga_len;
9293 }
9294
Bram Moolenaar09689a02020-05-09 22:50:08 +02009295 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009296 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009297 return FAIL;
9298 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9299 CLEAR_POINTER(dfunc);
9300 dfunc->df_idx = def_functions.ga_len;
9301 ufunc->uf_dfunc_idx = dfunc->df_idx;
9302 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009303 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009304 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009305 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009306 ++def_functions.ga_len;
9307 return OK;
9308}
9309
9310/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009311 * After ex_function() has collected all the function lines: parse and compile
9312 * the lines into instructions.
9313 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009314 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9315 * the return statement (used for lambda). When uf_ret_type is already set
9316 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009317 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009318 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009319 * This can be used recursively through compile_lambda(), which may reallocate
9320 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009321 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009322 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009323 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009324compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009325 ufunc_T *ufunc,
9326 int check_return_type,
9327 compiletype_T compile_type,
9328 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009330 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009331 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009332 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009333 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 cctx_T cctx;
9335 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009336 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009337 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009338 int ret = FAIL;
9339 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009340 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009341 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009342 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009343 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009344#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009345 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009346#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009347 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009348
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009349 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009350 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009351 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009352 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9354 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009355 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009356
9357 switch (compile_type)
9358 {
9359 case CT_PROFILE:
9360#ifdef FEAT_PROFILE
9361 instr_dest = dfunc->df_instr_prof; break;
9362#endif
9363 case CT_NONE: instr_dest = dfunc->df_instr; break;
9364 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9365 }
9366 if (instr_dest != NULL)
9367 // Was compiled in this mode before: Free old instructions.
9368 delete_def_function_contents(dfunc, FALSE);
9369 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009370 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009371 else
9372 {
9373 if (add_def_function(ufunc) == FAIL)
9374 return FAIL;
9375 new_def_function = TRUE;
9376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009377
Bram Moolenaar985116a2020-07-12 17:31:09 +02009378 ufunc->uf_def_status = UF_COMPILING;
9379
Bram Moolenaara80faa82020-04-12 19:37:17 +02009380 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009381
Bram Moolenaare99d4222021-06-13 14:01:26 +02009382 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009383 cctx.ctx_ufunc = ufunc;
9384 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009385 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9387 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9388 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9389 cctx.ctx_type_list = &ufunc->uf_type_list;
9390 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9391 instr = &cctx.ctx_instr;
9392
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009393 // Set the context to the function, it may be compiled when called from
9394 // another script. Set the script version to the most modern one.
9395 // The line number will be set in next_line_from_context().
9396 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009397 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9398
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009399 // Don't use the flag from ":legacy" here.
9400 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9401
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009402 // Make sure error messages are OK.
9403 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9404 if (do_estack_push)
9405 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009406 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009407
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009408 if (ufunc->uf_def_args.ga_len > 0)
9409 {
9410 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009411 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009412 int i;
9413 char_u *arg;
9414 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009415 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009416
9417 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009418 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009419 for (i = 0; i < count; ++i)
9420 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009421 garray_T *stack = &cctx.ctx_type_stack;
9422 type_T *val_type;
9423 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009424 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009425 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009426 int jump_instr_idx = instr->ga_len;
9427 isn_T *isn;
9428
9429 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9430 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9431 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009432
9433 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009434 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009435
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009436 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009437 r = compile_expr0(&arg, &cctx);
9438
Bram Moolenaar12bce952021-03-11 20:04:04 +01009439 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009440 goto erret;
9441
9442 // If no type specified use the type of the default value.
9443 // Otherwise check that the default value type matches the
9444 // specified type.
9445 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009446 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009447 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009448 {
9449 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009450 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009451 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009452 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009453 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009454 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009455
9456 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009457 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009458
9459 // set instruction index in JUMP_IF_ARG_SET to here
9460 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9461 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009462 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009463
9464 if (did_set_arg_type)
9465 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009466 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009467 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009468
9469 /*
9470 * Loop over all the lines of the function and generate instructions.
9471 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 for (;;)
9473 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009474 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009475 int starts_with_colon = FALSE;
9476 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009477 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009478
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009479 // Bail out on the first error to avoid a flood of errors and report
9480 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009481 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009482 goto erret;
9483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009484 if (line != NULL && *line == '|')
9485 // the line continues after a '|'
9486 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009487 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009488 && !(*line == '#' && (line == cctx.ctx_line_start
9489 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009490 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009491 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009492 goto erret;
9493 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009494 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9495 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009496 else
9497 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009498 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009499 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009500 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009501 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009502#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009503 if (cctx.ctx_skip != SKIP_YES)
9504 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009505#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009506 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009507 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009508 // Make a copy, splitting off nextcmd and removing trailing spaces
9509 // may change it.
9510 if (line != NULL)
9511 {
9512 line = vim_strsave(line);
9513 vim_free(line_to_free);
9514 line_to_free = line;
9515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009516 }
9517
Bram Moolenaara80faa82020-04-12 19:37:17 +02009518 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009519 ea.cmdlinep = &line;
9520 ea.cmd = skipwhite(line);
9521
Bram Moolenaarb2049902021-01-24 12:53:53 +01009522 if (*ea.cmd == '#')
9523 {
9524 // "#" starts a comment
9525 line = (char_u *)"";
9526 continue;
9527 }
9528
Bram Moolenaarf002a412021-01-24 13:34:18 +01009529#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009530 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9531 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009532 {
9533 may_generate_prof_end(&cctx, prof_lnum);
9534
9535 prof_lnum = cctx.ctx_lnum;
9536 generate_instr(&cctx, ISN_PROF_START);
9537 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009538#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009539 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9540 && cctx.ctx_skip != SKIP_YES)
9541 {
9542 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009543 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009544 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009545 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009546
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009547 // Some things can be recognized by the first character.
9548 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009549 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009550 case '}':
9551 {
9552 // "}" ends a block scope
9553 scopetype_T stype = cctx.ctx_scope == NULL
9554 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009555
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009556 if (stype == BLOCK_SCOPE)
9557 {
9558 compile_endblock(&cctx);
9559 line = ea.cmd;
9560 }
9561 else
9562 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009563 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009564 goto erret;
9565 }
9566 if (line != NULL)
9567 line = skipwhite(ea.cmd + 1);
9568 continue;
9569 }
9570
9571 case '{':
9572 // "{" starts a block scope
9573 // "{'a': 1}->func() is something else
9574 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9575 {
9576 line = compile_block(ea.cmd, &cctx);
9577 continue;
9578 }
9579 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009580 }
9581
9582 /*
9583 * COMMAND MODIFIERS
9584 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009585 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009586 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9587 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009588 {
9589 if (errormsg != NULL)
9590 goto erret;
9591 // empty line or comment
9592 line = (char_u *)"";
9593 continue;
9594 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009595 generate_cmdmods(&cctx, &local_cmdmod);
9596 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009598 // Check if there was a colon after the last command modifier or before
9599 // the current position.
9600 for (p = ea.cmd; p >= line; --p)
9601 {
9602 if (*p == ':')
9603 starts_with_colon = TRUE;
9604 if (p < ea.cmd && !VIM_ISWHITE(*p))
9605 break;
9606 }
9607
Bram Moolenaarce024c32021-06-26 13:00:49 +02009608 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009609 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009610 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009611 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009612 if (checkforcmd(&ea.cmd, "call", 3))
9613 {
9614 if (*ea.cmd == '(')
9615 // not for "call()"
9616 ea.cmd = p;
9617 else
9618 ea.cmd = skipwhite(ea.cmd);
9619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009620
Bram Moolenaarce024c32021-06-26 13:00:49 +02009621 if (!starts_with_colon)
9622 {
9623 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009624
Bram Moolenaarce024c32021-06-26 13:00:49 +02009625 // Check for assignment after command modifiers.
9626 assign = may_compile_assignment(&ea, &line, &cctx);
9627 if (assign == OK)
9628 goto nextline;
9629 if (assign == FAIL)
9630 goto erret;
9631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 }
9633
9634 /*
9635 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009636 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009637 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009639 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009640 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9641 && (starts_with_colon || !(*cmd == '\''
9642 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009643 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009644 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009645 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009646 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009647 if (!starts_with_colon)
9648 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009649 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009650 goto erret;
9651 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009652 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009653 if (ends_excmd2(line, ea.cmd))
9654 {
9655 // A range without a command: jump to the line.
9656 // TODO: compile to a more efficient command, possibly
9657 // calling parse_cmd_address().
9658 ea.cmdidx = CMD_SIZE;
9659 line = compile_exec(line, &ea, &cctx);
9660 goto nextline;
9661 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009662 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009663 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009664 p = find_ex_command(&ea, NULL,
9665 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009666 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009667
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009668 if (p == NULL)
9669 {
9670 if (cctx.ctx_skip != SKIP_YES)
9671 emsg(_(e_ambiguous_use_of_user_defined_command));
9672 goto erret;
9673 }
9674
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009675 // When using ":legacy cmd" always use compile_exec().
9676 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009677 {
9678 char_u *start = ea.cmd;
9679
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009680 switch (ea.cmdidx)
9681 {
9682 case CMD_if:
9683 case CMD_elseif:
9684 case CMD_else:
9685 case CMD_endif:
9686 case CMD_for:
9687 case CMD_endfor:
9688 case CMD_continue:
9689 case CMD_break:
9690 case CMD_while:
9691 case CMD_endwhile:
9692 case CMD_try:
9693 case CMD_catch:
9694 case CMD_finally:
9695 case CMD_endtry:
9696 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9697 goto erret;
9698 default: break;
9699 }
9700
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009701 // ":legacy return expr" needs to be handled differently.
9702 if (checkforcmd(&start, "return", 4))
9703 ea.cmdidx = CMD_return;
9704 else
9705 ea.cmdidx = CMD_legacy;
9706 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9709 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009710 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009711 {
9712 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009713 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009714 }
9715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009716 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009717 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009719 // CMD_var cannot happen, compile_assignment() above would be
9720 // used. Most likely an assignment to a non-existing variable.
9721 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009722 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009724 }
9725
Bram Moolenaar3988f642020-08-27 22:43:03 +02009726 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009727 && ea.cmdidx != CMD_elseif
9728 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009729 && ea.cmdidx != CMD_endif
9730 && ea.cmdidx != CMD_endfor
9731 && ea.cmdidx != CMD_endwhile
9732 && ea.cmdidx != CMD_catch
9733 && ea.cmdidx != CMD_finally
9734 && ea.cmdidx != CMD_endtry)
9735 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009736 emsg(_(e_unreachable_code_after_return));
9737 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009738 }
9739
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009740 p = skipwhite(p);
9741 if (ea.cmdidx != CMD_SIZE
9742 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9743 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009744 if (ea.cmdidx >= 0)
9745 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009746 if ((ea.argt & EX_BANG) && *p == '!')
9747 {
9748 ea.forceit = TRUE;
9749 p = skipwhite(p + 1);
9750 }
9751 }
9752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009753 switch (ea.cmdidx)
9754 {
9755 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009756 ea.arg = p;
9757 line = compile_nested_function(&ea, &cctx);
9758 break;
9759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009761 // TODO: should we allow this, e.g. to declare a global
9762 // function?
9763 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009764 goto erret;
9765
9766 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009767 line = compile_return(p, check_return_type,
9768 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009769 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009770 break;
9771
9772 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009773 emsg(_(e_cannot_use_let_in_vim9_script));
9774 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009775 case CMD_var:
9776 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009777 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009778 case CMD_increment:
9779 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009780 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009781 if (line == p)
9782 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783 break;
9784
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009785 case CMD_unlet:
9786 case CMD_unlockvar:
9787 case CMD_lockvar:
9788 line = compile_unletlock(p, &ea, &cctx);
9789 break;
9790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009791 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009792 emsg(_(e_import_can_only_be_used_in_script));
9793 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 break;
9795
9796 case CMD_if:
9797 line = compile_if(p, &cctx);
9798 break;
9799 case CMD_elseif:
9800 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009801 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009802 break;
9803 case CMD_else:
9804 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009805 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009806 break;
9807 case CMD_endif:
9808 line = compile_endif(p, &cctx);
9809 break;
9810
9811 case CMD_while:
9812 line = compile_while(p, &cctx);
9813 break;
9814 case CMD_endwhile:
9815 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009816 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009817 break;
9818
9819 case CMD_for:
9820 line = compile_for(p, &cctx);
9821 break;
9822 case CMD_endfor:
9823 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009824 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009825 break;
9826 case CMD_continue:
9827 line = compile_continue(p, &cctx);
9828 break;
9829 case CMD_break:
9830 line = compile_break(p, &cctx);
9831 break;
9832
9833 case CMD_try:
9834 line = compile_try(p, &cctx);
9835 break;
9836 case CMD_catch:
9837 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009838 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009839 break;
9840 case CMD_finally:
9841 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009842 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009843 break;
9844 case CMD_endtry:
9845 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009846 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009847 break;
9848 case CMD_throw:
9849 line = compile_throw(p, &cctx);
9850 break;
9851
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009852 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009853 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009854 break;
9855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009857 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009858 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009859 case CMD_echomsg:
9860 case CMD_echoerr:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009861 // TODO: "echoconsole"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009862 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009863 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009864
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009865 case CMD_put:
9866 ea.cmd = cmd;
9867 line = compile_put(p, &ea, &cctx);
9868 break;
9869
Bram Moolenaar4c137212021-04-19 16:48:48 +02009870 case CMD_substitute:
9871 if (cctx.ctx_skip == SKIP_YES)
9872 line = (char_u *)"";
9873 else
9874 {
9875 ea.arg = p;
9876 line = compile_substitute(line, &ea, &cctx);
9877 }
9878 break;
9879
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009880 case CMD_redir:
9881 ea.arg = p;
9882 line = compile_redir(line, &ea, &cctx);
9883 break;
9884
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009885 case CMD_cexpr:
9886 case CMD_lexpr:
9887 case CMD_caddexpr:
9888 case CMD_laddexpr:
9889 case CMD_cgetexpr:
9890 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009891#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009892 ea.arg = p;
9893 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009894#else
9895 ex_ni(&ea);
9896 line = NULL;
9897#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009898 break;
9899
Bram Moolenaarae616492020-07-28 20:07:27 +02009900 case CMD_append:
9901 case CMD_change:
9902 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009903 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009904 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009905 case CMD_xit:
9906 not_in_vim9(&ea);
9907 goto erret;
9908
Bram Moolenaar002262f2020-07-08 17:47:57 +02009909 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009910 if (cctx.ctx_skip != SKIP_YES)
9911 {
9912 semsg(_(e_invalid_command_str), ea.cmd);
9913 goto erret;
9914 }
9915 // We don't check for a next command here.
9916 line = (char_u *)"";
9917 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009918
Bram Moolenaar20677332021-06-06 17:02:53 +02009919 case CMD_lua:
9920 case CMD_mzscheme:
9921 case CMD_perl:
9922 case CMD_py3:
9923 case CMD_python3:
9924 case CMD_python:
9925 case CMD_pythonx:
9926 case CMD_ruby:
9927 case CMD_tcl:
9928 ea.arg = p;
9929 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009930 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009931 else
9932 // heredoc lines have been concatenated with NL
9933 // characters in get_function_body()
9934 line = compile_script(line, &cctx);
9935 break;
9936
9937 default:
9938 // Not recognized, execute with do_cmdline_cmd().
9939 ea.arg = p;
9940 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 break;
9942 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009943nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009944 if (line == NULL)
9945 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009946 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009947
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009948 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009949 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009951 if (cctx.ctx_type_stack.ga_len < 0)
9952 {
9953 iemsg("Type stack underflow");
9954 goto erret;
9955 }
9956 }
9957
9958 if (cctx.ctx_scope != NULL)
9959 {
9960 if (cctx.ctx_scope->se_type == IF_SCOPE)
9961 emsg(_(e_endif));
9962 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9963 emsg(_(e_endwhile));
9964 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9965 emsg(_(e_endfor));
9966 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009967 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009968 goto erret;
9969 }
9970
Bram Moolenaarefd88552020-06-18 20:50:10 +02009971 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009973 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9974 ufunc->uf_ret_type = &t_void;
9975 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009977 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009978 goto erret;
9979 }
9980
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009981 // Return void if there is no return at the end.
9982 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009983 }
9984
Bram Moolenaar599410c2021-04-10 14:03:43 +02009985 // When compiled with ":silent!" and there was an error don't consider the
9986 // function compiled.
9987 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009988 {
9989 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9990 + ufunc->uf_dfunc_idx;
9991 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009992 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009993#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009994 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009995 {
9996 dfunc->df_instr_prof = instr->ga_data;
9997 dfunc->df_instr_prof_count = instr->ga_len;
9998 }
9999 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010000#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010001 if (cctx.ctx_compile_type == CT_DEBUG)
10002 {
10003 dfunc->df_instr_debug = instr->ga_data;
10004 dfunc->df_instr_debug_count = instr->ga_len;
10005 }
10006 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010007 {
10008 dfunc->df_instr = instr->ga_data;
10009 dfunc->df_instr_count = instr->ga_len;
10010 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010011 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010012 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010013 if (cctx.ctx_outer_used)
10014 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010015 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010017
10018 ret = OK;
10019
10020erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010021 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010022 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010023 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10024 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010025
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010026 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010027 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010028 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010029 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010030
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010031 // If using the last entry in the table and it was added above, we
10032 // might as well remove it.
10033 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010034 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010035 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010036 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010037 ufunc->uf_dfunc_idx = 0;
10038 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010039 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010040
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010041 while (cctx.ctx_scope != NULL)
10042 drop_scope(&cctx);
10043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010044 if (errormsg != NULL)
10045 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010046 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010047 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048 }
10049
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010050 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10051 {
10052 if (ret == OK)
10053 {
10054 emsg(_(e_missing_redir_end));
10055 ret = FAIL;
10056 }
10057 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010058 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010059 }
10060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010061 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010062 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010063 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010064 if (do_estack_push)
10065 estack_pop();
10066
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010067 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010068 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010069 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010071 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010072}
10073
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010074 void
10075set_function_type(ufunc_T *ufunc)
10076{
10077 int varargs = ufunc->uf_va_name != NULL;
10078 int argcount = ufunc->uf_args.ga_len;
10079
10080 // Create a type for the function, with the return type and any
10081 // argument types.
10082 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10083 // The type is included in "tt_args".
10084 if (argcount > 0 || varargs)
10085 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010086 if (ufunc->uf_type_list.ga_itemsize == 0)
10087 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010088 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10089 argcount, &ufunc->uf_type_list);
10090 // Add argument types to the function type.
10091 if (func_type_add_arg_types(ufunc->uf_func_type,
10092 argcount + varargs,
10093 &ufunc->uf_type_list) == FAIL)
10094 return;
10095 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10096 ufunc->uf_func_type->tt_min_argcount =
10097 argcount - ufunc->uf_def_args.ga_len;
10098 if (ufunc->uf_arg_types == NULL)
10099 {
10100 int i;
10101
10102 // lambda does not have argument types.
10103 for (i = 0; i < argcount; ++i)
10104 ufunc->uf_func_type->tt_args[i] = &t_any;
10105 }
10106 else
10107 mch_memmove(ufunc->uf_func_type->tt_args,
10108 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10109 if (varargs)
10110 {
10111 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010112 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010113 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10114 }
10115 }
10116 else
10117 // No arguments, can use a predefined type.
10118 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10119 argcount, &ufunc->uf_type_list);
10120}
10121
10122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010123/*
10124 * Delete an instruction, free what it contains.
10125 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010126 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010127delete_instr(isn_T *isn)
10128{
10129 switch (isn->isn_type)
10130 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010131 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010132 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010133 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010134 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010135 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010136 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010137 case ISN_LOADENV:
10138 case ISN_LOADG:
10139 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010140 case ISN_LOADT:
10141 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010142 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010143 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010145 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010146 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010147 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010148 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010149 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010150 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010151 case ISN_STOREW:
10152 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010153 vim_free(isn->isn_arg.string);
10154 break;
10155
Bram Moolenaar4c137212021-04-19 16:48:48 +020010156 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010157 {
10158 int idx;
10159 isn_T *list = isn->isn_arg.subs.subs_instr;
10160
10161 vim_free(isn->isn_arg.subs.subs_cmd);
10162 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10163 delete_instr(list + idx);
10164 vim_free(list);
10165 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010166 break;
10167
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010168 case ISN_INSTR:
10169 {
10170 int idx;
10171 isn_T *list = isn->isn_arg.instr;
10172
10173 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10174 delete_instr(list + idx);
10175 vim_free(list);
10176 }
10177 break;
10178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010179 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010180 case ISN_STORES:
10181 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010182 break;
10183
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010184 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010185 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010186 vim_free(isn->isn_arg.unlet.ul_name);
10187 break;
10188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010189 case ISN_STOREOPT:
10190 vim_free(isn->isn_arg.storeopt.so_name);
10191 break;
10192
10193 case ISN_PUSHBLOB: // push blob isn_arg.blob
10194 blob_unref(isn->isn_arg.blob);
10195 break;
10196
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010197 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010199 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010200#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010201 break;
10202
10203 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010204#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010205 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010206#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010207 break;
10208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 case ISN_UCALL:
10210 vim_free(isn->isn_arg.ufunc.cuf_name);
10211 break;
10212
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010213 case ISN_FUNCREF:
10214 {
10215 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10216 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010217 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010218
Bram Moolenaar077a4232020-12-22 18:33:27 +010010219 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10220 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010221 }
10222 break;
10223
10224 case ISN_DCALL:
10225 {
10226 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10227 + isn->isn_arg.dfunc.cdf_idx;
10228
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010229 if (dfunc->df_ufunc != NULL
10230 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010231 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010232 }
10233 break;
10234
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010235 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010236 {
10237 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10238 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10239
10240 if (ufunc != NULL)
10241 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010242 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010243 func_ptr_unref(ufunc);
10244 }
10245
10246 vim_free(lambda);
10247 vim_free(isn->isn_arg.newfunc.nf_global);
10248 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010249 break;
10250
Bram Moolenaar5e654232020-09-16 15:22:00 +020010251 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010252 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010253 free_type(isn->isn_arg.type.ct_type);
10254 break;
10255
Bram Moolenaar02194d22020-10-24 23:08:38 +020010256 case ISN_CMDMOD:
10257 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10258 ->cmod_filter_regmatch.regprog);
10259 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10260 break;
10261
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010262 case ISN_LOADSCRIPT:
10263 case ISN_STORESCRIPT:
10264 vim_free(isn->isn_arg.script.scriptref);
10265 break;
10266
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010267 case ISN_TRY:
10268 vim_free(isn->isn_arg.try.try_ref);
10269 break;
10270
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010271 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010272 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010273 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10274 break;
10275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276 case ISN_2BOOL:
10277 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010278 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279 case ISN_ADDBLOB:
10280 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010281 case ISN_ANYINDEX:
10282 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010283 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010284 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010285 case ISN_BLOBINDEX:
10286 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010287 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010288 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010289 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010290 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010291 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010292 case ISN_COMPAREANY:
10293 case ISN_COMPAREBLOB:
10294 case ISN_COMPAREBOOL:
10295 case ISN_COMPAREDICT:
10296 case ISN_COMPAREFLOAT:
10297 case ISN_COMPAREFUNC:
10298 case ISN_COMPARELIST:
10299 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010300 case ISN_COMPARESPECIAL:
10301 case ISN_COMPARESTRING:
10302 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010303 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010304 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010305 case ISN_DROP:
10306 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010307 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010308 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010309 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010310 case ISN_EXECCONCAT:
10311 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010312 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010313 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010314 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010315 case ISN_GETITEM:
10316 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010317 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010318 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010319 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010320 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010321 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010322 case ISN_LOADBDICT:
10323 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010324 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010325 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010326 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010327 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010328 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010329 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010330 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010331 case ISN_NEGATENR:
10332 case ISN_NEWDICT:
10333 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010334 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010335 case ISN_OPFLOAT:
10336 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010337 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010338 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010339 case ISN_PROF_END:
10340 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010341 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010342 case ISN_PUSHF:
10343 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010344 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010345 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010346 case ISN_REDIREND:
10347 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010348 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010349 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010350 case ISN_SHUFFLE:
10351 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010352 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010353 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010354 case ISN_STORENR:
10355 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010356 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010357 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010358 case ISN_STOREV:
10359 case ISN_STRINDEX:
10360 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010361 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010362 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010363 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010364 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010365 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010366 // nothing allocated
10367 break;
10368 }
10369}
10370
10371/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010372 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010373 */
10374 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010375delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010376{
10377 int idx;
10378
10379 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010380 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010381
10382 if (dfunc->df_instr != NULL)
10383 {
10384 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10385 delete_instr(dfunc->df_instr + idx);
10386 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010387 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010388 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010389 if (dfunc->df_instr_debug != NULL)
10390 {
10391 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10392 delete_instr(dfunc->df_instr_debug + idx);
10393 VIM_CLEAR(dfunc->df_instr_debug);
10394 dfunc->df_instr_debug = NULL;
10395 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010396#ifdef FEAT_PROFILE
10397 if (dfunc->df_instr_prof != NULL)
10398 {
10399 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10400 delete_instr(dfunc->df_instr_prof + idx);
10401 VIM_CLEAR(dfunc->df_instr_prof);
10402 dfunc->df_instr_prof = NULL;
10403 }
10404#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010405
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010406 if (mark_deleted)
10407 dfunc->df_deleted = TRUE;
10408 if (dfunc->df_ufunc != NULL)
10409 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010410}
10411
10412/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010413 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010414 * function, unless another user function still uses it.
10415 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010416 */
10417 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010418unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010419{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010420 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010421 {
10422 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10423 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010424
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010425 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010426 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010427 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010428 ufunc->uf_dfunc_idx = 0;
10429 if (dfunc->df_ufunc == ufunc)
10430 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010431 }
10432}
10433
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010434/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010435 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010436 */
10437 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010438link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010439{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010440 if (ufunc->uf_dfunc_idx > 0)
10441 {
10442 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10443 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010444
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010445 ++dfunc->df_refcount;
10446 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010447}
10448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010449#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010450/*
10451 * Free all functions defined with ":def".
10452 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010453 void
10454free_def_functions(void)
10455{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010456 int idx;
10457
10458 for (idx = 0; idx < def_functions.ga_len; ++idx)
10459 {
10460 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10461
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010462 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010463 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010464 }
10465
10466 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010467}
10468#endif
10469
10470
10471#endif // FEAT_EVAL