blob: e7c7c07a6926944f480af3d82bd77e3b0593c768 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200358 if (sav->sav_block_id == 0)
359 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 return sav;
361
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200362 if (cctx == NULL)
363 {
364 // Not in a function scope, find variable with block id equal to or
365 // smaller than the current block id.
366 while (sav != NULL)
367 {
368 if (sav->sav_block_id <= si->sn_current_block_id)
369 break;
370 sav = sav->sav_next;
371 }
372 return sav;
373 }
374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 // Go over the variables with this name and find one that was visible
376 // from the function.
377 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 while (sav != NULL)
380 {
381 int idx;
382
383 // Go over the blocks that this function was defined in. If the
384 // variable block ID matches it was visible to the function.
385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
387 return sav;
388 sav = sav->sav_next;
389 }
390
Bram Moolenaar88421d62021-07-24 14:14:52 +0200391 // Not found, assume variable at script level was visible.
392 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393}
394
395/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100396 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200397 */
398 static int
399script_is_vim9()
400{
401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
402}
403
404/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200405 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 * Returns OK or FAIL.
408 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200409 static int
410script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200412 if (current_sctx.sc_sid <= 0)
413 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200414 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200415 {
416 // Check script variables that were visible where the function was
417 // defined.
418 if (find_script_var(name, len, cctx) != NULL)
419 return OK;
420 }
421 else
422 {
423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
424 dictitem_T *di;
425 int cc;
426
427 // Check script variables that are currently visible
428 cc = name[len];
429 name[len] = NUL;
430 di = find_var_in_ht(ht, 0, name, TRUE);
431 name[len] = cc;
432 if (di != NULL)
433 return OK;
434 }
435
436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437}
438
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100439/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100440 * Return TRUE if "name" is a local variable, argument, script variable or
441 * imported.
442 */
443 static int
444variable_exists(char_u *name, size_t len, cctx_T *cctx)
445{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100446 return (cctx != NULL
447 && (lookup_local(name, len, NULL, cctx) == OK
448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200449 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100450 || find_imported(name, len, cctx) != NULL;
451}
452
453/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100454 * Return TRUE if "name" is a local variable, argument, script variable,
455 * imported or function.
456 */
457 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100459{
460 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100461 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462
463 if (variable_exists(name, len, cctx))
464 return TRUE;
465
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100466 // This is similar to what is in lookup_scriptitem():
467 // Find a function, so that a following "->" works.
468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
469 // a function call.
470 p = skipwhite(name + len);
471
472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
473 {
474 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200475 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100476 // Skip "g:" before a function name.
477 is_global = (name[0] == 'g' && name[1] == ':');
478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
479 }
480 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100481}
482
483/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100484 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200485 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200486 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100487 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100488 * Return FAIL and give an error if it defined.
489 */
490 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100492{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200493 int c = p[len];
494 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495
Bram Moolenaarda479c72021-04-10 21:01:38 +0200496 // underscore argument is OK
497 if (len == 1 && *p == '_')
498 return OK;
499
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200500 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 {
502 if (is_arg)
503 semsg(_(e_argument_already_declared_in_script_str), p);
504 else
505 semsg(_(e_variable_already_declared_in_script_str), p);
506 return FAIL;
507 }
508
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100510 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100511 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
518 && (!func_is_global(ufunc)
519 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200520 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100521 if (is_arg)
522 semsg(_(e_argument_name_shadows_existing_variable_str), p);
523 else
524 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200525 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 return FAIL;
527 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100528 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200529 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 return OK;
531}
532
Bram Moolenaar65b95452020-07-19 14:03:09 +0200533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534/////////////////////////////////////////////////////////////////////
535// Following generate_ functions expect the caller to call ga_grow().
536
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200537#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
538#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540/*
541 * Generate an instruction without arguments.
542 * Returns a pointer to the new instruction, NULL if failed.
543 */
544 static isn_T *
545generate_instr(cctx_T *cctx, isntype_T isn_type)
546{
547 garray_T *instr = &cctx->ctx_instr;
548 isn_T *isn;
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200551 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return NULL;
553 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
554 isn->isn_type = isn_type;
555 isn->isn_lnum = cctx->ctx_lnum + 1;
556 ++instr->ga_len;
557
558 return isn;
559}
560
561/*
562 * Generate an instruction without arguments.
563 * "drop" will be removed from the stack.
564 * Returns a pointer to the new instruction, NULL if failed.
565 */
566 static isn_T *
567generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
568{
569 garray_T *stack = &cctx->ctx_type_stack;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 stack->ga_len -= drop;
573 return generate_instr(cctx, isn_type);
574}
575
576/*
577 * Generate instruction "isn_type" and put "type" on the type stack.
578 */
579 static isn_T *
580generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 if ((isn = generate_instr(cctx, isn_type)) == NULL)
586 return NULL;
587
Bram Moolenaar35578162021-08-02 19:10:38 +0200588 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 ++stack->ga_len;
592
593 return isn;
594}
595
596/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 * Generate an ISN_DEBUG instruction.
598 */
599 static isn_T *
600generate_instr_debug(cctx_T *cctx)
601{
602 isn_T *isn;
603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
604 + cctx->ctx_ufunc->uf_dfunc_idx;
605
606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
607 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200610 return isn;
611}
612
613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200616 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 */
618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200619may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620{
621 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200622 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100624 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 RETURN_OK_IF_SKIP(cctx);
627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 switch ((*type)->tt_type)
629 {
630 // nothing to be done
631 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200633 // conversion possible
634 case VAR_SPECIAL:
635 case VAR_BOOL:
636 case VAR_NUMBER:
637 case VAR_FLOAT:
638 break;
639
640 // conversion possible (with runtime check)
641 case VAR_ANY:
642 case VAR_UNKNOWN:
643 isntype = ISN_2STRING_ANY;
644 break;
645
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200646 // conversion possible when tolerant
647 case VAR_LIST:
648 if (tolerant)
649 {
650 isntype = ISN_2STRING_ANY;
651 break;
652 }
653 // FALLTHROUGH
654
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200655 // conversion not possible
656 case VAR_VOID:
657 case VAR_BLOB:
658 case VAR_FUNC:
659 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200660 case VAR_DICT:
661 case VAR_JOB:
662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200663 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200664 to_string_error((*type)->tt_type);
665 return FAIL;
666 }
667
668 *type = &t_string;
669 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200671 isn->isn_arg.tostring.offset = offset;
672 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 return OK;
675}
676
677 static int
678check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
679{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 {
684 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200685 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 return FAIL;
689 }
690 return OK;
691}
692
Bram Moolenaar07802042021-09-09 23:01:14 +0200693/*
694 * Generate instruction for "+". For a list this creates a new list.
695 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200696 static int
697generate_add_instr(
698 cctx_T *cctx,
699 vartype_T vartype,
700 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200701 type_T *type2,
702 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200703{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100704 garray_T *stack = &cctx->ctx_type_stack;
705 isn_T *isn = generate_instr_drop(cctx,
706 vartype == VAR_NUMBER ? ISN_OPNR
707 : vartype == VAR_LIST ? ISN_ADDLIST
708 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100710 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100712 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200713
714 if (vartype != VAR_LIST && vartype != VAR_BLOB
715 && type1->tt_type != VAR_ANY
716 && type2->tt_type != VAR_ANY
717 && check_number_or_float(
718 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
719 return FAIL;
720
721 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200722 {
723 if (isn->isn_type == ISN_ADDLIST)
724 isn->isn_arg.op.op_type = expr_type;
725 else
726 isn->isn_arg.op.op_type = EXPR_ADD;
727 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100728
729 // When concatenating two lists with different member types the member type
730 // becomes "any".
731 if (vartype == VAR_LIST
732 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
733 && type1->tt_member != type2->tt_member)
734 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
735
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200736 return isn == NULL ? FAIL : OK;
737}
738
739/*
740 * Get the type to use for an instruction for an operation on "type1" and
741 * "type2". If they are matching use a type-specific instruction. Otherwise
742 * fall back to runtime type checking.
743 */
744 static vartype_T
745operator_type(type_T *type1, type_T *type2)
746{
747 if (type1->tt_type == type2->tt_type
748 && (type1->tt_type == VAR_NUMBER
749 || type1->tt_type == VAR_LIST
750#ifdef FEAT_FLOAT
751 || type1->tt_type == VAR_FLOAT
752#endif
753 || type1->tt_type == VAR_BLOB))
754 return type1->tt_type;
755 return VAR_ANY;
756}
757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758/*
759 * Generate an instruction with two arguments. The instruction depends on the
760 * type of the arguments.
761 */
762 static int
763generate_two_op(cctx_T *cctx, char_u *op)
764{
765 garray_T *stack = &cctx->ctx_type_stack;
766 type_T *type1;
767 type_T *type2;
768 vartype_T vartype;
769 isn_T *isn;
770
Bram Moolenaar080457c2020-03-03 21:53:32 +0100771 RETURN_OK_IF_SKIP(cctx);
772
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200773 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
775 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200776 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
778 switch (*op)
779 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200780 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200781 if (generate_add_instr(cctx, vartype, type1, type2,
782 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 break;
785
786 case '-':
787 case '*':
788 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
789 op) == FAIL)
790 return FAIL;
791 if (vartype == VAR_NUMBER)
792 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
793#ifdef FEAT_FLOAT
794 else if (vartype == VAR_FLOAT)
795 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
796#endif
797 else
798 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
799 if (isn != NULL)
800 isn->isn_arg.op.op_type = *op == '*'
801 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
802 break;
803
Bram Moolenaar4c683752020-04-05 21:38:23 +0200804 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200806 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 && type2->tt_type != VAR_NUMBER))
808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200809 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 return FAIL;
811 }
812 isn = generate_instr_drop(cctx,
813 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
814 if (isn != NULL)
815 isn->isn_arg.op.op_type = EXPR_REM;
816 break;
817 }
818
819 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200820 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 {
822 type_T *type = &t_any;
823
824#ifdef FEAT_FLOAT
825 // float+number and number+float results in float
826 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
827 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
828 type = &t_float;
829#endif
830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
831 }
832
833 return OK;
834}
835
836/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200837 * Get the instruction to use for comparing "type1" with "type2"
838 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200840 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100841get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842{
843 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844
Bram Moolenaar4c683752020-04-05 21:38:23 +0200845 if (type1 == VAR_UNKNOWN)
846 type1 = VAR_ANY;
847 if (type2 == VAR_UNKNOWN)
848 type2 = VAR_ANY;
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 if (type1 == type2)
851 {
852 switch (type1)
853 {
854 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
855 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
856 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
857 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
858 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
859 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
860 case VAR_LIST: isntype = ISN_COMPARELIST; break;
861 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
862 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 default: isntype = ISN_COMPAREANY; break;
864 }
865 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200866 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100868 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 isntype = ISN_COMPAREANY;
870
Bram Moolenaar657137c2021-01-09 15:45:23 +0100871 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 && (isntype == ISN_COMPAREBOOL
873 || isntype == ISN_COMPARESPECIAL
874 || isntype == ISN_COMPARENR
875 || isntype == ISN_COMPAREFLOAT))
876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200877 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100878 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 }
881 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100882 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
884 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100885 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
886 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 && (type1 == VAR_BLOB || type2 == VAR_BLOB
888 || type1 == VAR_LIST || type2 == VAR_LIST))))
889 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200890 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200892 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200894 return isntype;
895}
896
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200897 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100898check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200899{
900 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
901 return FAIL;
902 return OK;
903}
904
Bram Moolenaara5565e42020-05-09 15:44:01 +0200905/*
906 * Generate an ISN_COMPARE* instruction with a boolean result.
907 */
908 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100909generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200910{
911 isntype_T isntype;
912 isn_T *isn;
913 garray_T *stack = &cctx->ctx_type_stack;
914 vartype_T type1;
915 vartype_T type2;
916
917 RETURN_OK_IF_SKIP(cctx);
918
919 // Get the known type of the two items on the stack. If they are matching
920 // use a type-specific instruction. Otherwise fall back to runtime type
921 // checking.
922 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
923 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100924 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200925 if (isntype == ISN_DROP)
926 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if ((isn = generate_instr(cctx, isntype)) == NULL)
929 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100930 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 isn->isn_arg.op.op_ic = ic;
932
933 // takes two arguments, puts one bool back
934 if (stack->ga_len >= 2)
935 {
936 --stack->ga_len;
937 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
938 }
939
940 return OK;
941}
942
943/*
944 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200945 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 */
947 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200948generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949{
950 isn_T *isn;
951 garray_T *stack = &cctx->ctx_type_stack;
952
Bram Moolenaar080457c2020-03-03 21:53:32 +0100953 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
955 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200956 isn->isn_arg.tobool.invert = invert;
957 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958
959 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200960 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961
962 return OK;
963}
964
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200965/*
966 * Generate an ISN_COND2BOOL instruction.
967 */
968 static int
969generate_COND2BOOL(cctx_T *cctx)
970{
971 isn_T *isn;
972 garray_T *stack = &cctx->ctx_type_stack;
973
974 RETURN_OK_IF_SKIP(cctx);
975 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
976 return FAIL;
977
978 // type becomes bool
979 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
980
981 return OK;
982}
983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200985generate_TYPECHECK(
986 cctx_T *cctx,
987 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100988 int offset,
989 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990{
991 isn_T *isn;
992 garray_T *stack = &cctx->ctx_type_stack;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
996 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200997 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100998 isn->isn_arg.type.ct_off = (int8_T)offset;
999 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000
Bram Moolenaar5e654232020-09-16 15:22:00 +02001001 // type becomes expected
1002 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
1004 return OK;
1005}
1006
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001007 static int
1008generate_SETTYPE(
1009 cctx_T *cctx,
1010 type_T *expected)
1011{
1012 isn_T *isn;
1013
1014 RETURN_OK_IF_SKIP(cctx);
1015 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1016 return FAIL;
1017 isn->isn_arg.type.ct_type = alloc_type(expected);
1018 return OK;
1019}
1020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001022 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1023 * used. Return FALSE if the types will never match.
1024 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001025 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026use_typecheck(type_T *actual, type_T *expected)
1027{
1028 if (actual->tt_type == VAR_ANY
1029 || actual->tt_type == VAR_UNKNOWN
1030 || (actual->tt_type == VAR_FUNC
1031 && (expected->tt_type == VAR_FUNC
1032 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001033 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1034 && ((actual->tt_member == &t_void)
1035 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001036 return TRUE;
1037 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1038 && actual->tt_type == expected->tt_type)
1039 // This takes care of a nested list or dict.
1040 return use_typecheck(actual->tt_member, expected->tt_member);
1041 return FALSE;
1042}
1043
1044/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001045 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001046 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001047 * - "actual" is a type that can be "expected" type: add a runtime check; or
1048 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001049 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1050 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001052 static int
1053need_type_where(
1054 type_T *actual,
1055 type_T *expected,
1056 int offset,
1057 where_T where,
1058 cctx_T *cctx,
1059 int silent,
1060 int actual_is_const)
1061{
1062 if (expected == &t_bool && actual != &t_bool
1063 && (actual->tt_flags & TTFLAG_BOOL_OK))
1064 {
1065 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1066 // boolean is OK but requires a conversion.
1067 generate_2BOOL(cctx, FALSE, offset);
1068 return OK;
1069 }
1070
1071 if (check_type(expected, actual, FALSE, where) == OK)
1072 return OK;
1073
1074 // If the actual type can be the expected type add a runtime check.
1075 // If it's a constant a runtime check makes no sense.
1076 if ((!actual_is_const || actual == &t_any)
1077 && use_typecheck(actual, expected))
1078 {
1079 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1080 return OK;
1081 }
1082
1083 if (!silent)
1084 type_mismatch_where(expected, actual, where);
1085 return FAIL;
1086}
1087
Bram Moolenaar351ead02021-01-16 16:07:01 +01001088 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001089need_type(
1090 type_T *actual,
1091 type_T *expected,
1092 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001093 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001094 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001095 int silent,
1096 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001097{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001098 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001099
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001100 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001101 return need_type_where(actual, expected, offset, where,
1102 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001103}
1104
1105/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001106 * Check that the top of the type stack has a type that can be used as a
1107 * condition. Give an error and return FAIL if not.
1108 */
1109 static int
1110bool_on_stack(cctx_T *cctx)
1111{
1112 garray_T *stack = &cctx->ctx_type_stack;
1113 type_T *type;
1114
1115 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1116 if (type == &t_bool)
1117 return OK;
1118
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001119 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001120 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1121 // This requires a runtime type check.
1122 return generate_COND2BOOL(cctx);
1123
Bram Moolenaar351ead02021-01-16 16:07:01 +01001124 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001125}
1126
1127/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 * Generate an ISN_PUSHNR instruction.
1129 */
1130 static int
1131generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1132{
1133 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001134 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.number = number;
1140
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001141 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001142 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001143 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_PUSHBOOL instruction.
1149 */
1150 static int
1151generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.number = number;
1159
1160 return OK;
1161}
1162
1163/*
1164 * Generate an ISN_PUSHSPEC instruction.
1165 */
1166 static int
1167generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1168{
1169 isn_T *isn;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1173 return FAIL;
1174 isn->isn_arg.number = number;
1175
1176 return OK;
1177}
1178
1179#ifdef FEAT_FLOAT
1180/*
1181 * Generate an ISN_PUSHF instruction.
1182 */
1183 static int
1184generate_PUSHF(cctx_T *cctx, float_T fnumber)
1185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.fnumber = fnumber;
1192
1193 return OK;
1194}
1195#endif
1196
1197/*
1198 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001199 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 */
1201 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001202generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203{
1204 isn_T *isn;
1205
Bram Moolenaar508b5612021-01-02 18:17:26 +01001206 if (cctx->ctx_skip == SKIP_YES)
1207 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001208 if (str != NULL)
1209 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001210 return OK;
1211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001213 {
1214 if (str != NULL)
1215 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001217 }
1218 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
1220 return OK;
1221}
1222
1223/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001224 * Generate an ISN_PUSHCHANNEL instruction.
1225 * Consumes "channel".
1226 */
1227 static int
1228generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1229{
1230 isn_T *isn;
1231
Bram Moolenaar080457c2020-03-03 21:53:32 +01001232 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001233 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.channel = channel;
1236
1237 return OK;
1238}
1239
1240/*
1241 * Generate an ISN_PUSHJOB instruction.
1242 * Consumes "job".
1243 */
1244 static int
1245generate_PUSHJOB(cctx_T *cctx, job_T *job)
1246{
1247 isn_T *isn;
1248
Bram Moolenaar080457c2020-03-03 21:53:32 +01001249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001250 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001251 return FAIL;
1252 isn->isn_arg.job = job;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_PUSHBLOB instruction.
1259 * Consumes "blob".
1260 */
1261 static int
1262generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1263{
1264 isn_T *isn;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.blob = blob;
1270
1271 return OK;
1272}
1273
1274/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001275 * Generate an ISN_PUSHFUNC instruction with name "name".
1276 * Consumes "name".
1277 */
1278 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001279generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001284 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001285 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001286 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001287
1288 return OK;
1289}
1290
1291/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001292 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001293 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1294 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001295 */
1296 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001297generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001301 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1302 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001303 type_T *item_type = &t_any;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001307 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001309 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001310 emsg(_(e_listreq));
1311 return FAIL;
1312 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001313 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001314 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1315 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001316 isn->isn_arg.getitem.gi_index = index;
1317 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001318
1319 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001320 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001321 return FAIL;
1322 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1323 ++stack->ga_len;
1324 return OK;
1325}
1326
1327/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001328 * Generate an ISN_SLICE instruction with "count".
1329 */
1330 static int
1331generate_SLICE(cctx_T *cctx, int count)
1332{
1333 isn_T *isn;
1334
1335 RETURN_OK_IF_SKIP(cctx);
1336 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.number = count;
1339 return OK;
1340}
1341
1342/*
1343 * Generate an ISN_CHECKLEN instruction with "min_len".
1344 */
1345 static int
1346generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1347{
1348 isn_T *isn;
1349
1350 RETURN_OK_IF_SKIP(cctx);
1351
1352 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1353 return FAIL;
1354 isn->isn_arg.checklen.cl_min_len = min_len;
1355 isn->isn_arg.checklen.cl_more_OK = more_OK;
1356
1357 return OK;
1358}
1359
1360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 * Generate an ISN_STORE instruction.
1362 */
1363 static int
1364generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1365{
1366 isn_T *isn;
1367
Bram Moolenaar080457c2020-03-03 21:53:32 +01001368 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1370 return FAIL;
1371 if (name != NULL)
1372 isn->isn_arg.string = vim_strsave(name);
1373 else
1374 isn->isn_arg.number = idx;
1375
1376 return OK;
1377}
1378
1379/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001380 * Generate an ISN_STOREOUTER instruction.
1381 */
1382 static int
1383generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1384{
1385 isn_T *isn;
1386
1387 RETURN_OK_IF_SKIP(cctx);
1388 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1389 return FAIL;
1390 isn->isn_arg.outer.outer_idx = idx;
1391 isn->isn_arg.outer.outer_depth = level;
1392
1393 return OK;
1394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1398 */
1399 static int
1400generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1401{
1402 isn_T *isn;
1403
Bram Moolenaar080457c2020-03-03 21:53:32 +01001404 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1406 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001407 isn->isn_arg.storenr.stnr_idx = idx;
1408 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 return OK;
1411}
1412
1413/*
1414 * Generate an ISN_STOREOPT instruction
1415 */
1416 static int
1417generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1418{
1419 isn_T *isn;
1420
Bram Moolenaar080457c2020-03-03 21:53:32 +01001421 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001422 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 return FAIL;
1424 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1425 isn->isn_arg.storeopt.so_flags = opt_flags;
1426
1427 return OK;
1428}
1429
1430/*
1431 * Generate an ISN_LOAD or similar instruction.
1432 */
1433 static int
1434generate_LOAD(
1435 cctx_T *cctx,
1436 isntype_T isn_type,
1437 int idx,
1438 char_u *name,
1439 type_T *type)
1440{
1441 isn_T *isn;
1442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1445 return FAIL;
1446 if (name != NULL)
1447 isn->isn_arg.string = vim_strsave(name);
1448 else
1449 isn->isn_arg.number = idx;
1450
1451 return OK;
1452}
1453
1454/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001455 * Generate an ISN_LOADOUTER instruction
1456 */
1457 static int
1458generate_LOADOUTER(
1459 cctx_T *cctx,
1460 int idx,
1461 int nesting,
1462 type_T *type)
1463{
1464 isn_T *isn;
1465
1466 RETURN_OK_IF_SKIP(cctx);
1467 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1468 return FAIL;
1469 isn->isn_arg.outer.outer_idx = idx;
1470 isn->isn_arg.outer.outer_depth = nesting;
1471
1472 return OK;
1473}
1474
1475/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001476 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001477 */
1478 static int
1479generate_LOADV(
1480 cctx_T *cctx,
1481 char_u *name,
1482 int error)
1483{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001484 int di_flags;
1485 int vidx = find_vim_var(name, &di_flags);
1486 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001487
Bram Moolenaar080457c2020-03-03 21:53:32 +01001488 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001489 if (vidx < 0)
1490 {
1491 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001492 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 return FAIL;
1494 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001495 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001496
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001497 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001498}
1499
1500/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001501 * Generate an ISN_UNLET instruction.
1502 */
1503 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001504generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001505{
1506 isn_T *isn;
1507
1508 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001510 return FAIL;
1511 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1512 isn->isn_arg.unlet.ul_forceit = forceit;
1513
1514 return OK;
1515}
1516
1517/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001518 * Generate an ISN_LOCKCONST instruction.
1519 */
1520 static int
1521generate_LOCKCONST(cctx_T *cctx)
1522{
1523 isn_T *isn;
1524
1525 RETURN_OK_IF_SKIP(cctx);
1526 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1527 return FAIL;
1528 return OK;
1529}
1530
1531/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 * Generate an ISN_LOADS instruction.
1533 */
1534 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001535generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001537 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001539 int sid,
1540 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541{
1542 isn_T *isn;
1543
Bram Moolenaar080457c2020-03-03 21:53:32 +01001544 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001545 if (isn_type == ISN_LOADS)
1546 isn = generate_instr_type(cctx, isn_type, type);
1547 else
1548 isn = generate_instr_drop(cctx, isn_type, 1);
1549 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001551 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1552 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553
1554 return OK;
1555}
1556
1557/*
1558 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1559 */
1560 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001561generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 cctx_T *cctx,
1563 isntype_T isn_type,
1564 int sid,
1565 int idx,
1566 type_T *type)
1567{
1568 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001569 scriptref_T *sref;
1570 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if (isn_type == ISN_LOADSCRIPT)
1574 isn = generate_instr_type(cctx, isn_type, type);
1575 else
1576 isn = generate_instr_drop(cctx, isn_type, 1);
1577 if (isn == NULL)
1578 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001579
1580 // This requires three arguments, which doesn't fit in an instruction, thus
1581 // we need to allocate a struct for this.
1582 sref = ALLOC_ONE(scriptref_T);
1583 if (sref == NULL)
1584 return FAIL;
1585 isn->isn_arg.script.scriptref = sref;
1586 sref->sref_sid = sid;
1587 sref->sref_idx = idx;
1588 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001589 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return OK;
1591}
1592
1593/*
1594 * Generate an ISN_NEWLIST instruction.
1595 */
1596 static int
1597generate_NEWLIST(cctx_T *cctx, int count)
1598{
1599 isn_T *isn;
1600 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 type_T *type;
1602 type_T *member;
1603
Bram Moolenaar080457c2020-03-03 21:53:32 +01001604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1606 return FAIL;
1607 isn->isn_arg.number = count;
1608
Bram Moolenaar127542b2020-08-09 17:22:04 +02001609 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001610 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001611 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001612 else
1613 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001614 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1615 cctx->ctx_type_list);
1616 type = get_list_type(member, cctx->ctx_type_list);
1617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 // drop the value types
1619 stack->ga_len -= count;
1620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001622 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 return FAIL;
1624 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_NEWDICT instruction.
1632 */
1633 static int
1634generate_NEWDICT(cctx_T *cctx, int count)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 type_T *type;
1639 type_T *member;
1640
Bram Moolenaar080457c2020-03-03 21:53:32 +01001641 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1643 return FAIL;
1644 isn->isn_arg.number = count;
1645
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001646 if (count == 0)
1647 member = &t_void;
1648 else
1649 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001650 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1651 cctx->ctx_type_list);
1652 type = get_dict_type(member, cctx->ctx_type_list);
1653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 // drop the key and value types
1655 stack->ga_len -= 2 * count;
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001658 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 return FAIL;
1660 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1661 ++stack->ga_len;
1662
1663 return OK;
1664}
1665
1666/*
1667 * Generate an ISN_FUNCREF instruction.
1668 */
1669 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001670generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671{
1672 isn_T *isn;
1673 garray_T *stack = &cctx->ctx_type_stack;
1674
Bram Moolenaar080457c2020-03-03 21:53:32 +01001675 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1677 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001678 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001679 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001681 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001682 // the nested context, including this one.
1683 if (ufunc->uf_flags & FC_CLOSURE)
1684 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1685
Bram Moolenaar35578162021-08-02 19:10:38 +02001686 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001688 ((type_T **)stack->ga_data)[stack->ga_len] =
1689 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 ++stack->ga_len;
1691
1692 return OK;
1693}
1694
1695/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001696 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001697 * "lambda_name" and "func_name" must be in allocated memory and will be
1698 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001699 */
1700 static int
1701generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1702{
1703 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001704
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001705 if (cctx->ctx_skip == SKIP_YES)
1706 {
1707 vim_free(lambda_name);
1708 vim_free(func_name);
1709 return OK;
1710 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001711 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001712 {
1713 vim_free(lambda_name);
1714 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001715 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001716 }
1717 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001718 isn->isn_arg.newfunc.nf_global = func_name;
1719
1720 return OK;
1721}
1722
1723/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001724 * Generate an ISN_DEF instruction: list functions
1725 */
1726 static int
1727generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1728{
1729 isn_T *isn;
1730
1731 RETURN_OK_IF_SKIP(cctx);
1732 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1733 return FAIL;
1734 if (len > 0)
1735 {
1736 isn->isn_arg.string = vim_strnsave(name, len);
1737 if (isn->isn_arg.string == NULL)
1738 return FAIL;
1739 }
1740 return OK;
1741}
1742
1743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 * Generate an ISN_JUMP instruction.
1745 */
1746 static int
1747generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1748{
1749 isn_T *isn;
1750 garray_T *stack = &cctx->ctx_type_stack;
1751
Bram Moolenaar080457c2020-03-03 21:53:32 +01001752 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.jump.jump_when = when;
1756 isn->isn_arg.jump.jump_where = where;
1757
1758 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1759 --stack->ga_len;
1760
1761 return OK;
1762}
1763
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001764/*
1765 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1766 */
1767 static int
1768generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1769{
1770 isn_T *isn;
1771
1772 RETURN_OK_IF_SKIP(cctx);
1773 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1776 // jump_where is set later
1777 return OK;
1778}
1779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 static int
1781generate_FOR(cctx_T *cctx, int loop_idx)
1782{
1783 isn_T *isn;
1784 garray_T *stack = &cctx->ctx_type_stack;
1785
Bram Moolenaar080457c2020-03-03 21:53:32 +01001786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1788 return FAIL;
1789 isn->isn_arg.forloop.for_idx = loop_idx;
1790
Bram Moolenaar35578162021-08-02 19:10:38 +02001791 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 return FAIL;
1793 // type doesn't matter, will be stored next
1794 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1795 ++stack->ga_len;
1796
1797 return OK;
1798}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001799/*
1800 * Generate an ISN_TRYCONT instruction.
1801 */
1802 static int
1803generate_TRYCONT(cctx_T *cctx, int levels, int where)
1804{
1805 isn_T *isn;
1806
1807 RETURN_OK_IF_SKIP(cctx);
1808 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1809 return FAIL;
1810 isn->isn_arg.trycont.tct_levels = levels;
1811 isn->isn_arg.trycont.tct_where = where;
1812
1813 return OK;
1814}
1815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
1817/*
1818 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001819 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 * Return FAIL if the number of arguments is wrong.
1821 */
1822 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001823generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824{
1825 isn_T *isn;
1826 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001827 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001828 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001829 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001830 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
Bram Moolenaar080457c2020-03-03 21:53:32 +01001832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001833 argoff = check_internal_func(func_idx, argcount);
1834 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 return FAIL;
1836
Bram Moolenaar389df252020-07-09 21:20:47 +02001837 if (method_call && argoff > 1)
1838 {
1839 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.shuffle.shfl_item = argcount;
1842 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1843 }
1844
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001845 if (argcount > 0)
1846 {
1847 // Check the types of the arguments.
1848 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001849 if (method_call && argoff > 1)
1850 {
1851 int i;
1852
1853 for (i = 0; i < argcount; ++i)
1854 shuffled_argtypes[i] = (i < argoff - 1)
1855 ? argtypes[i + 1]
1856 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1857 argtypes = shuffled_argtypes;
1858 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001859 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1860 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001861 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001862 if (internal_func_is_map(func_idx))
1863 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001864 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1867 return FAIL;
1868 isn->isn_arg.bfunc.cbf_idx = func_idx;
1869 isn->isn_arg.bfunc.cbf_argcount = argcount;
1870
Bram Moolenaar94738d82020-10-21 14:25:07 +02001871 // Drop the argument types and push the return type.
1872 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001873 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 return FAIL;
1875 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001876 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001877 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001879 if (maptype != NULL && maptype->tt_member != NULL
1880 && maptype->tt_member != &t_any)
1881 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001882 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 return OK;
1885}
1886
1887/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001888 * Generate an ISN_LISTAPPEND instruction. Works like add().
1889 * Argument count is already checked.
1890 */
1891 static int
1892generate_LISTAPPEND(cctx_T *cctx)
1893{
1894 garray_T *stack = &cctx->ctx_type_stack;
1895 type_T *list_type;
1896 type_T *item_type;
1897 type_T *expected;
1898
1899 // Caller already checked that list_type is a list.
1900 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1901 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1902 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001903 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001904 return FAIL;
1905
1906 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1907 return FAIL;
1908
1909 --stack->ga_len; // drop the argument
1910 return OK;
1911}
1912
1913/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001914 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1915 * Argument count is already checked.
1916 */
1917 static int
1918generate_BLOBAPPEND(cctx_T *cctx)
1919{
1920 garray_T *stack = &cctx->ctx_type_stack;
1921 type_T *item_type;
1922
1923 // Caller already checked that blob_type is a blob.
1924 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001925 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001926 return FAIL;
1927
1928 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1929 return FAIL;
1930
1931 --stack->ga_len; // drop the argument
1932 return OK;
1933}
1934
1935/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001936 * Return TRUE if "ufunc" should be compiled, taking into account whether
1937 * "profile" indicates profiling is to be done.
1938 */
1939 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001940func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001941{
1942 switch (ufunc->uf_def_status)
1943 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001944 case UF_TO_BE_COMPILED:
1945 return TRUE;
1946
Bram Moolenaarb2049902021-01-24 12:53:53 +01001947 case UF_COMPILED:
1948 {
1949 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1950 + ufunc->uf_dfunc_idx;
1951
Bram Moolenaare99d4222021-06-13 14:01:26 +02001952 switch (compile_type)
1953 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001954 case CT_PROFILE:
1955#ifdef FEAT_PROFILE
1956 return dfunc->df_instr_prof == NULL;
1957#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001958 case CT_NONE:
1959 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001960 case CT_DEBUG:
1961 return dfunc->df_instr_debug == NULL;
1962 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001963 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001964
1965 case UF_NOT_COMPILED:
1966 case UF_COMPILE_ERROR:
1967 case UF_COMPILING:
1968 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001969 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001970 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001971}
1972
1973/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 * Generate an ISN_DCALL or ISN_UCALL instruction.
1975 * Return FAIL if the number of arguments is wrong.
1976 */
1977 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001978generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979{
1980 isn_T *isn;
1981 garray_T *stack = &cctx->ctx_type_stack;
1982 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001983 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984
Bram Moolenaar080457c2020-03-03 21:53:32 +01001985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 if (argcount > regular_args && !has_varargs(ufunc))
1987 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001988 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 return FAIL;
1990 }
1991 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1992 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001993 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 return FAIL;
1995 }
1996
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001997 if (ufunc->uf_def_status != UF_NOT_COMPILED
1998 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001999 {
2000 int i;
2001
2002 for (i = 0; i < argcount; ++i)
2003 {
2004 type_T *expected;
2005 type_T *actual;
2006
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002007 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2008 if (actual == &t_special
2009 && i >= regular_args - ufunc->uf_def_args.ga_len)
2010 {
2011 // assume v:none used for default argument value
2012 continue;
2013 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002014 if (i < regular_args)
2015 {
2016 if (ufunc->uf_arg_types == NULL)
2017 continue;
2018 expected = ufunc->uf_arg_types[i];
2019 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002020 else if (ufunc->uf_va_type == NULL
2021 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002022 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002023 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002024 else
2025 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002026 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002027 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002028 {
2029 arg_type_mismatch(expected, actual, i + 1);
2030 return FAIL;
2031 }
2032 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002033 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002034 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002035 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002036 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002037 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002038 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2039 {
2040 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2041 ufunc->uf_name);
2042 return FAIL;
2043 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002046 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002047 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002049 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 {
2051 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2052 isn->isn_arg.dfunc.cdf_argcount = argcount;
2053 }
2054 else
2055 {
2056 // A user function may be deleted and redefined later, can't use the
2057 // ufunc pointer, need to look it up again at runtime.
2058 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2059 isn->isn_arg.ufunc.cuf_argcount = argcount;
2060 }
2061
2062 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002063 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 return FAIL;
2065 // add return value
2066 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2067 ++stack->ga_len;
2068
2069 return OK;
2070}
2071
2072/*
2073 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2074 */
2075 static int
2076generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2077{
2078 isn_T *isn;
2079 garray_T *stack = &cctx->ctx_type_stack;
2080
Bram Moolenaar080457c2020-03-03 21:53:32 +01002081 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2083 return FAIL;
2084 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2085 isn->isn_arg.ufunc.cuf_argcount = argcount;
2086
2087 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002088 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002089 return FAIL;
2090 // add return value
2091 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2092 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093
2094 return OK;
2095}
2096
2097/*
2098 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002099 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 */
2101 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002102generate_PCALL(
2103 cctx_T *cctx,
2104 int argcount,
2105 char_u *name,
2106 type_T *type,
2107 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108{
2109 isn_T *isn;
2110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002111 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112
Bram Moolenaar080457c2020-03-03 21:53:32 +01002113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002114
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002115 if (type->tt_type == VAR_ANY)
2116 ret_type = &t_any;
2117 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002118 {
2119 if (type->tt_argcount != -1)
2120 {
2121 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2122
2123 if (argcount < type->tt_min_argcount - varargs)
2124 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002125 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002126 return FAIL;
2127 }
2128 if (!varargs && argcount > type->tt_argcount)
2129 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002130 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002131 return FAIL;
2132 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002133 if (type->tt_args != NULL)
2134 {
2135 int i;
2136
2137 for (i = 0; i < argcount; ++i)
2138 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002139 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002140 type_T *actual = ((type_T **)stack->ga_data)[
2141 stack->ga_len + offset];
2142 type_T *expected;
2143
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002144 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002145 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002146 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002147 else if (i >= type->tt_min_argcount
2148 && actual == &t_special)
2149 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002150 else
2151 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002152 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002153 cctx, TRUE, FALSE) == FAIL)
2154 {
2155 arg_type_mismatch(expected, actual, i + 1);
2156 return FAIL;
2157 }
2158 }
2159 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002160 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002161 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002162 if (ret_type == &t_unknown)
2163 // return type not known yet, use a runtime check
2164 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002165 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002166 else
2167 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002168 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002169 return FAIL;
2170 }
2171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2173 return FAIL;
2174 isn->isn_arg.pfunc.cpf_top = at_top;
2175 isn->isn_arg.pfunc.cpf_argcount = argcount;
2176
2177 stack->ga_len -= argcount; // drop the arguments
2178
2179 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002180 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002182 // If partial is above the arguments it must be cleared and replaced with
2183 // the return value.
2184 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2185 return FAIL;
2186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 return OK;
2188}
2189
2190/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002191 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 */
2193 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195{
2196 isn_T *isn;
2197 garray_T *stack = &cctx->ctx_type_stack;
2198 type_T *type;
2199
Bram Moolenaar080457c2020-03-03 21:53:32 +01002200 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002201 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002203 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002205 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002207 if (type->tt_type != VAR_DICT && type != &t_any)
2208 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002209 char *tofree;
2210
2211 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2212 name, type_name(type, &tofree));
2213 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002214 return FAIL;
2215 }
2216 // change dict type to dict member type
2217 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002218 {
2219 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2220 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222
2223 return OK;
2224}
2225
2226/*
2227 * Generate an ISN_ECHO instruction.
2228 */
2229 static int
2230generate_ECHO(cctx_T *cctx, int with_white, int count)
2231{
2232 isn_T *isn;
2233
Bram Moolenaar080457c2020-03-03 21:53:32 +01002234 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2236 return FAIL;
2237 isn->isn_arg.echo.echo_with_white = with_white;
2238 isn->isn_arg.echo.echo_count = count;
2239
2240 return OK;
2241}
2242
Bram Moolenaarad39c092020-02-26 18:23:43 +01002243/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002244 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002245 */
2246 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002247generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002248{
2249 isn_T *isn;
2250
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002251 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002252 return FAIL;
2253 isn->isn_arg.number = count;
2254
2255 return OK;
2256}
2257
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002258/*
2259 * Generate an ISN_PUT instruction.
2260 */
2261 static int
2262generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2263{
2264 isn_T *isn;
2265
2266 RETURN_OK_IF_SKIP(cctx);
2267 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2268 return FAIL;
2269 isn->isn_arg.put.put_regname = regname;
2270 isn->isn_arg.put.put_lnum = lnum;
2271 return OK;
2272}
2273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 static int
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002275generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276{
2277 isn_T *isn;
2278
Bram Moolenaar080457c2020-03-03 21:53:32 +01002279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002280 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 return FAIL;
2282 isn->isn_arg.string = vim_strsave(line);
2283 return OK;
2284}
2285
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002286 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002287generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2288{
2289 isn_T *isn;
2290 garray_T *stack = &cctx->ctx_type_stack;
2291
2292 RETURN_OK_IF_SKIP(cctx);
2293 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2294 return FAIL;
2295 isn->isn_arg.string = vim_strsave(line);
2296
Bram Moolenaar35578162021-08-02 19:10:38 +02002297 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002298 return FAIL;
2299 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2300 ++stack->ga_len;
2301
2302 return OK;
2303}
2304
2305 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002306generate_EXECCONCAT(cctx_T *cctx, int count)
2307{
2308 isn_T *isn;
2309
2310 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2311 return FAIL;
2312 isn->isn_arg.number = count;
2313 return OK;
2314}
2315
Bram Moolenaar08597872020-12-10 19:43:40 +01002316/*
2317 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2318 */
2319 static int
2320generate_RANGE(cctx_T *cctx, char_u *range)
2321{
2322 isn_T *isn;
2323 garray_T *stack = &cctx->ctx_type_stack;
2324
2325 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2326 return FAIL;
2327 isn->isn_arg.string = range;
2328
Bram Moolenaar35578162021-08-02 19:10:38 +02002329 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002330 return FAIL;
2331 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2332 ++stack->ga_len;
2333 return OK;
2334}
2335
Bram Moolenaar792f7862020-11-23 08:31:18 +01002336 static int
2337generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2338{
2339 isn_T *isn;
2340
2341 RETURN_OK_IF_SKIP(cctx);
2342 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2343 return FAIL;
2344 isn->isn_arg.unpack.unp_count = var_count;
2345 isn->isn_arg.unpack.unp_semicolon = semicolon;
2346 return OK;
2347}
2348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002350 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002351 */
2352 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002353generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002354{
2355 isn_T *isn;
2356
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002357 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002358 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002359 cctx->ctx_has_cmdmod = TRUE;
2360
2361 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002362 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002363 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2364 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2365 return FAIL;
2366 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002367 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002368 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002369 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002370
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002371 return OK;
2372}
2373
2374 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002375generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002376{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002377 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2378 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002379 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002380 return OK;
2381}
2382
Bram Moolenaarfa984412021-03-25 22:15:28 +01002383 static int
2384misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002385{
2386 garray_T *instr = &cctx->ctx_instr;
2387
Bram Moolenaara91a7132021-03-25 21:12:15 +01002388 if (cctx->ctx_has_cmdmod
2389 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2390 == ISN_CMDMOD)
2391 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002392 emsg(_(e_misplaced_command_modifier));
2393 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002394 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002395 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002396}
2397
2398/*
2399 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002400 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 */
2402 static int
2403current_instr_idx(cctx_T *cctx)
2404{
2405 garray_T *instr = &cctx->ctx_instr;
2406 int idx = instr->ga_len;
2407
Bram Moolenaare99d4222021-06-13 14:01:26 +02002408 while (idx > 0)
2409 {
2410 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002411 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002412 {
2413 --idx;
2414 continue;
2415 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002416#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002417 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2418 {
2419 --idx;
2420 continue;
2421 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002422#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002423 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2424 {
2425 --idx;
2426 continue;
2427 }
2428 break;
2429 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002430 return idx;
2431}
2432
Bram Moolenaarf002a412021-01-24 13:34:18 +01002433#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002434 static void
2435may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2436{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002437 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002438 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002439}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002440#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002441
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002442/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002444 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002446 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002447reserve_local(
2448 cctx_T *cctx,
2449 char_u *name,
2450 size_t len,
2451 int isConst,
2452 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002455 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002457 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002459 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002460 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 }
2462
Bram Moolenaar35578162021-08-02 19:10:38 +02002463 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464 return NULL;
2465 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002466 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002468 // Every local variable uses the next entry on the stack. We could re-use
2469 // the last ones when leaving a scope, but then variables used in a closure
2470 // might get overwritten. To keep things simple do not re-use stack
2471 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002472 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2473 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002474
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002475 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 lvar->lv_const = isConst;
2477 lvar->lv_type = type;
2478
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002479 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002480 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002481 return NULL;
2482 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2483 vim_strsave(lvar->lv_name);
2484 ++dfunc->df_var_names.ga_len;
2485
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002486 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487}
2488
2489/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002490 * Remove local variables above "new_top".
2491 */
2492 static void
2493unwind_locals(cctx_T *cctx, int new_top)
2494{
2495 if (cctx->ctx_locals.ga_len > new_top)
2496 {
2497 int idx;
2498 lvar_T *lvar;
2499
2500 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2501 {
2502 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2503 vim_free(lvar->lv_name);
2504 }
2505 }
2506 cctx->ctx_locals.ga_len = new_top;
2507}
2508
2509/*
2510 * Free all local variables.
2511 */
2512 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002513free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514{
2515 unwind_locals(cctx, 0);
2516 ga_clear(&cctx->ctx_locals);
2517}
2518
2519/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002520 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2521 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2522 * error if the variable was defined with :const.
2523 */
2524 static int
2525check_item_writable(svar_T *sv, int check_writable, char_u *name)
2526{
2527 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2528 || (check_writable == ASSIGN_FINAL
2529 && sv->sv_const == ASSIGN_CONST))
2530 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002531 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002532 return FAIL;
2533 }
2534 return OK;
2535}
2536
2537/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002539 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 * Returns the index in "sn_var_vals" if found.
2541 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002542 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 */
2544 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002545get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546{
2547 hashtab_T *ht;
2548 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002549 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002550 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 int idx;
2552
Bram Moolenaare3d46852020-08-29 13:39:17 +02002553 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002555 if (sid == current_sctx.sc_sid)
2556 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002557 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002558
2559 if (sav == NULL)
2560 return -2;
2561 idx = sav->sav_var_vals_idx;
2562 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002563 if (check_item_writable(sv, check_writable, name) == FAIL)
2564 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002565 return idx;
2566 }
2567
2568 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 ht = &SCRIPT_VARS(sid);
2570 di = find_var_in_ht(ht, 0, name, TRUE);
2571 if (di == NULL)
2572 return -2;
2573
2574 // Now find the svar_T index in sn_var_vals.
2575 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2576 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002577 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (sv->sv_tv == &di->di_tv)
2579 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002580 if (check_item_writable(sv, check_writable, name) == FAIL)
2581 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 return idx;
2583 }
2584 }
2585 return -1;
2586}
2587
2588/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002589 * Find "name" in imported items of the current script or in "cctx" if not
2590 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 */
2592 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002593find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 int idx;
2596
Bram Moolenaare3d46852020-08-29 13:39:17 +02002597 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002598 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 if (cctx != NULL)
2600 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2601 {
2602 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2603 + idx;
2604
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002605 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2606 : STRLEN(import->imp_name) == len
2607 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 return import;
2609 }
2610
Bram Moolenaarefa94442020-08-08 22:16:00 +02002611 return find_imported_in_script(name, len, current_sctx.sc_sid);
2612}
2613
2614 imported_T *
2615find_imported_in_script(char_u *name, size_t len, int sid)
2616{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002617 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002618 int idx;
2619
Bram Moolenaare3d46852020-08-29 13:39:17 +02002620 if (!SCRIPT_ID_VALID(sid))
2621 return NULL;
2622 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2624 {
2625 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2626
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002627 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2628 : STRLEN(import->imp_name) == len
2629 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 return import;
2631 }
2632 return NULL;
2633}
2634
2635/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002636 * Free all imported variables.
2637 */
2638 static void
2639free_imported(cctx_T *cctx)
2640{
2641 int idx;
2642
2643 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2644 {
2645 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2646
2647 vim_free(import->imp_name);
2648 }
2649 ga_clear(&cctx->ctx_imports);
2650}
2651
2652/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002653 * Return a pointer to the next line that isn't empty or only contains a
2654 * comment. Skips over white space.
2655 * Returns NULL if there is none.
2656 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002657 char_u *
2658peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659{
2660 int lnum = cctx->ctx_lnum;
2661
2662 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2663 {
2664 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002665 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002666
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002667 // ignore NULLs inserted for continuation lines
2668 if (line != NULL)
2669 {
2670 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002671 if (vim9_bad_comment(p))
2672 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002673 if (*p != NUL && !vim9_comment_start(p))
2674 return p;
2675 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002676 }
2677 return NULL;
2678}
2679
2680/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002681 * Called when checking for a following operator at "arg". When the rest of
2682 * the line is empty or only a comment, peek the next line. If there is a next
2683 * line return a pointer to it and set "nextp".
2684 * Otherwise skip over white space.
2685 */
2686 static char_u *
2687may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2688{
2689 char_u *p = skipwhite(arg);
2690
2691 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002692 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002693 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002694 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002695 if (*nextp != NULL)
2696 return *nextp;
2697 }
2698 return p;
2699}
2700
2701/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002702 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002703 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002704 * Returns NULL when at the end.
2705 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002706 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002707next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002708{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002709 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002710
2711 do
2712 {
2713 ++cctx->ctx_lnum;
2714 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002715 {
2716 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002717 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002718 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002719 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002720 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002721 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002722 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002723 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002724 return line;
2725}
2726
2727/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002728 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002729 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002730 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002731 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2732 */
2733 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002734may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002735{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002736 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002737 if (vim9_bad_comment(*arg))
2738 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002739 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002740 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002741 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002742
2743 if (next == NULL)
2744 return FAIL;
2745 *arg = skipwhite(next);
2746 }
2747 return OK;
2748}
2749
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002750/*
2751 * Idem, and give an error when failed.
2752 */
2753 static int
2754may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2755{
2756 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2757 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002758 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002759 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002760 return FAIL;
2761 }
2762 return OK;
2763}
2764
2765
Bram Moolenaara5565e42020-05-09 15:44:01 +02002766// Structure passed between the compile_expr* functions to keep track of
2767// constants that have been parsed but for which no code was produced yet. If
2768// possible expressions on these constants are applied at compile time. If
2769// that is not possible, the code to push the constants needs to be generated
2770// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002771// Using 50 should be more than enough of 5 levels of ().
2772#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002773typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002774 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002775 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002776 int pp_is_const; // all generated code was constants, used for a
2777 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778} ppconst_T;
2779
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002780static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002781static int compile_expr0(char_u **arg, cctx_T *cctx);
2782static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2783
Bram Moolenaara5565e42020-05-09 15:44:01 +02002784/*
2785 * Generate a PUSH instruction for "tv".
2786 * "tv" will be consumed or cleared.
2787 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2788 */
2789 static int
2790generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2791{
2792 if (tv != NULL)
2793 {
2794 switch (tv->v_type)
2795 {
2796 case VAR_UNKNOWN:
2797 break;
2798 case VAR_BOOL:
2799 generate_PUSHBOOL(cctx, tv->vval.v_number);
2800 break;
2801 case VAR_SPECIAL:
2802 generate_PUSHSPEC(cctx, tv->vval.v_number);
2803 break;
2804 case VAR_NUMBER:
2805 generate_PUSHNR(cctx, tv->vval.v_number);
2806 break;
2807#ifdef FEAT_FLOAT
2808 case VAR_FLOAT:
2809 generate_PUSHF(cctx, tv->vval.v_float);
2810 break;
2811#endif
2812 case VAR_BLOB:
2813 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2814 tv->vval.v_blob = NULL;
2815 break;
2816 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002817 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 tv->vval.v_string = NULL;
2819 break;
2820 default:
2821 iemsg("constant type not supported");
2822 clear_tv(tv);
2823 return FAIL;
2824 }
2825 tv->v_type = VAR_UNKNOWN;
2826 }
2827 return OK;
2828}
2829
2830/*
2831 * Generate code for any ppconst entries.
2832 */
2833 static int
2834generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2835{
2836 int i;
2837 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002838 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002839
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002840 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002841 for (i = 0; i < ppconst->pp_used; ++i)
2842 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2843 ret = FAIL;
2844 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002845 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002846 return ret;
2847}
2848
2849/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002850 * Check that the last item of "ppconst" is a bool.
2851 */
2852 static int
2853check_ppconst_bool(ppconst_T *ppconst)
2854{
2855 if (ppconst->pp_used > 0)
2856 {
2857 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002858 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002859
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002860 return check_typval_type(&t_bool, tv, where);
2861 }
2862 return OK;
2863}
2864
2865/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866 * Clear ppconst constants. Used when failing.
2867 */
2868 static void
2869clear_ppconst(ppconst_T *ppconst)
2870{
2871 int i;
2872
2873 for (i = 0; i < ppconst->pp_used; ++i)
2874 clear_tv(&ppconst->pp_tv[i]);
2875 ppconst->pp_used = 0;
2876}
2877
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002878/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002881 */
2882 static int
2883compile_member(int is_slice, cctx_T *cctx)
2884{
2885 type_T **typep;
2886 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002887 vartype_T vartype;
2888 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002889
Bram Moolenaar261417b2021-05-06 21:04:55 +02002890 // We can index a list, dict and blob. If we don't know the type
2891 // we can use the index value type. If we still don't know use an "ANY"
2892 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002893 typep = ((type_T **)stack->ga_data) + stack->ga_len
2894 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002895 vartype = (*typep)->tt_type;
2896 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002897 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002898 if (*typep == &t_any && idxtype == &t_string)
2899 vartype = VAR_DICT;
2900 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002901 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002902 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002903 return FAIL;
2904 if (is_slice)
2905 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002906 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2907 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002908 FALSE, FALSE) == FAIL)
2909 return FAIL;
2910 }
2911 }
2912
Bram Moolenaar261417b2021-05-06 21:04:55 +02002913 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002914 {
2915 if (is_slice)
2916 {
2917 emsg(_(e_cannot_slice_dictionary));
2918 return FAIL;
2919 }
2920 if ((*typep)->tt_type == VAR_DICT)
2921 {
2922 *typep = (*typep)->tt_member;
2923 if (*typep == &t_unknown)
2924 // empty dict was used
2925 *typep = &t_any;
2926 }
2927 else
2928 {
2929 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2930 FALSE, FALSE) == FAIL)
2931 return FAIL;
2932 *typep = &t_any;
2933 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002934 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002935 return FAIL;
2936 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2937 return FAIL;
2938 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002939 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940 {
2941 *typep = &t_string;
2942 if ((is_slice
2943 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2944 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2945 return FAIL;
2946 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002947 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002948 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002949 if (is_slice)
2950 {
2951 *typep = &t_blob;
2952 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2953 return FAIL;
2954 }
2955 else
2956 {
2957 *typep = &t_number;
2958 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2959 return FAIL;
2960 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002961 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002962 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002963 {
2964 if (is_slice)
2965 {
2966 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002967 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002968 2) == FAIL)
2969 return FAIL;
2970 }
2971 else
2972 {
2973 if ((*typep)->tt_type == VAR_LIST)
2974 {
2975 *typep = (*typep)->tt_member;
2976 if (*typep == &t_unknown)
2977 // empty list was used
2978 *typep = &t_any;
2979 }
2980 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002981 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2982 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002983 return FAIL;
2984 }
2985 }
2986 else
2987 {
2988 emsg(_(e_string_list_dict_or_blob_required));
2989 return FAIL;
2990 }
2991 return OK;
2992}
2993
2994/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002995 * Generate an instruction to load script-local variable "name", without the
2996 * leading "s:".
2997 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 */
2999 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003000compile_load_scriptvar(
3001 cctx_T *cctx,
3002 char_u *name, // variable NUL terminated
3003 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003004 char_u **end, // end of variable
3005 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003007 scriptitem_T *si;
3008 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 imported_T *import;
3010
Bram Moolenaare3d46852020-08-29 13:39:17 +02003011 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3012 return FAIL;
3013 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003014 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003015 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003017 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003018 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3019 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 }
3021 if (idx >= 0)
3022 {
3023 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3024
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003025 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 current_sctx.sc_sid, idx, sv->sv_type);
3027 return OK;
3028 }
3029
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003030 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (import != NULL)
3032 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003033 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003034 {
3035 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003036 char_u *exp_name;
3037 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003038 ufunc_T *ufunc;
3039 type_T *type;
3040
3041 // Used "import * as Name", need to lookup the member.
3042 if (*p != '.')
3043 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003044 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003045 return FAIL;
3046 }
3047 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003048 if (VIM_ISWHITE(*p))
3049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003050 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003051 return FAIL;
3052 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003053
Bram Moolenaar1c991142020-07-04 13:15:31 +02003054 // isolate one name
3055 exp_name = p;
3056 while (eval_isnamec(*p))
3057 ++p;
3058 cc = *p;
3059 *p = NUL;
3060
Bram Moolenaaredba7072021-03-13 21:14:18 +01003061 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3062 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003063 *p = cc;
3064 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003065 *end = p;
3066
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003067 if (idx < 0)
3068 {
3069 if (*p == '(' && ufunc != NULL)
3070 {
3071 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3072 return OK;
3073 }
3074 return FAIL;
3075 }
3076
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003077 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3078 import->imp_sid,
3079 idx,
3080 type);
3081 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003082 else if (import->imp_funcname != NULL)
3083 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003084 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003085 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3086 import->imp_sid,
3087 import->imp_var_vals_idx,
3088 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 return OK;
3090 }
3091
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003092 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003093 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 return FAIL;
3095}
3096
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003097 static int
3098generate_funcref(cctx_T *cctx, char_u *name)
3099{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003100 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003101
3102 if (ufunc == NULL)
3103 return FAIL;
3104
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003105 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003106 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3107 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003108 == FAIL)
3109 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003110 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003111}
3112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113/*
3114 * Compile a variable name into a load instruction.
3115 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003116 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 * When "error" is FALSE do not give an error when not found.
3118 */
3119 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003120compile_load(
3121 char_u **arg,
3122 char_u *end_arg,
3123 cctx_T *cctx,
3124 int is_expr,
3125 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126{
3127 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003128 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003129 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003131 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
3133 if (*(*arg + 1) == ':')
3134 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003135 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003136 {
3137 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
Bram Moolenaarfa596382021-04-07 21:58:16 +02003139 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003140 switch (**arg)
3141 {
3142 case 'g': isn_type = ISN_LOADGDICT; break;
3143 case 'w': isn_type = ISN_LOADWDICT; break;
3144 case 't': isn_type = ISN_LOADTDICT; break;
3145 case 'b': isn_type = ISN_LOADBDICT; break;
3146 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003147 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003148 goto theend;
3149 }
3150 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3151 goto theend;
3152 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 else
3155 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003156 isntype_T isn_type = ISN_DROP;
3157
Bram Moolenaarfa596382021-04-07 21:58:16 +02003158 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003159 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3160 if (name == NULL)
3161 return FAIL;
3162
3163 switch (**arg)
3164 {
3165 case 'v': res = generate_LOADV(cctx, name, error);
3166 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003167 case 's': if (is_expr && ASCII_ISUPPER(*name)
3168 && find_func(name, FALSE, cctx) != NULL)
3169 res = generate_funcref(cctx, name);
3170 else
3171 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003172 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003173 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003174 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003175 {
3176 if (is_expr && ASCII_ISUPPER(*name)
3177 && find_func(name, FALSE, cctx) != NULL)
3178 res = generate_funcref(cctx, name);
3179 else
3180 isn_type = ISN_LOADG;
3181 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003182 else
3183 {
3184 isn_type = ISN_LOADAUTO;
3185 vim_free(name);
3186 name = vim_strnsave(*arg, end - *arg);
3187 if (name == NULL)
3188 return FAIL;
3189 }
3190 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003191 case 'w': isn_type = ISN_LOADW; break;
3192 case 't': isn_type = ISN_LOADT; break;
3193 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003194 default: // cannot happen, just in case
3195 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003196 goto theend;
3197 }
3198 if (isn_type != ISN_DROP)
3199 {
3200 // Global, Buffer-local, Window-local and Tabpage-local
3201 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003202 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003203 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 }
3206 }
3207 else
3208 {
3209 size_t len = end - *arg;
3210 int idx;
3211 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003212 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213
3214 name = vim_strnsave(*arg, end - *arg);
3215 if (name == NULL)
3216 return FAIL;
3217
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003218 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3219 {
3220 script_autoload(name, FALSE);
3221 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3222 }
3223 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3224 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003226 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003227 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 }
3229 else
3230 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003231 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003232
Bram Moolenaar709664c2020-12-12 14:33:41 +01003233 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003235 type = lvar.lv_type;
3236 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003237 if (lvar.lv_from_outer != 0)
3238 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003239 else
3240 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 }
3242 else
3243 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003244 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003245 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003246 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003247 || find_imported(name, 0, cctx) != NULL)
3248 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003249
Bram Moolenaar0f769812020-09-12 18:32:34 +02003250 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003251 // uppercase letter it can be a user defined function.
3252 // generate_funcref() will fail if the function can't be found.
3253 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003254 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 }
3256 }
3257 if (gen_load)
3258 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003259 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003260 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003261 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003262 cctx->ctx_outer_used = TRUE;
3263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 }
3265
3266 *arg = end;
3267
3268theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003269 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003270 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 vim_free(name);
3272 return res;
3273}
3274
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003275 static void
3276clear_instr_ga(garray_T *gap)
3277{
3278 int idx;
3279
3280 for (idx = 0; idx < gap->ga_len; ++idx)
3281 delete_instr(((isn_T *)gap->ga_data) + idx);
3282 ga_clear(gap);
3283}
3284
3285/*
3286 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3287 * Returns FAIL if compilation fails.
3288 */
3289 static int
3290compile_string(isn_T *isn, cctx_T *cctx)
3291{
3292 char_u *s = isn->isn_arg.string;
3293 garray_T save_ga = cctx->ctx_instr;
3294 int expr_res;
3295 int trailing_error;
3296 int instr_count;
3297 isn_T *instr = NULL;
3298
Bram Moolenaarcd268012021-07-22 19:11:08 +02003299 // Remove the string type from the stack.
3300 --cctx->ctx_type_stack.ga_len;
3301
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003302 // Temporarily reset the list of instructions so that the jump labels are
3303 // correct.
3304 cctx->ctx_instr.ga_len = 0;
3305 cctx->ctx_instr.ga_maxlen = 0;
3306 cctx->ctx_instr.ga_data = NULL;
3307 expr_res = compile_expr0(&s, cctx);
3308 s = skipwhite(s);
3309 trailing_error = *s != NUL;
3310
Bram Moolenaarff652882021-05-16 15:24:49 +02003311 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003312 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003313 {
3314 if (trailing_error)
3315 semsg(_(e_trailing_arg), s);
3316 clear_instr_ga(&cctx->ctx_instr);
3317 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003318 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003319 return FAIL;
3320 }
3321
3322 // Move the generated instructions into the ISN_INSTR instruction, then
3323 // restore the list of instructions.
3324 instr_count = cctx->ctx_instr.ga_len;
3325 instr = cctx->ctx_instr.ga_data;
3326 instr[instr_count].isn_type = ISN_FINISH;
3327
3328 cctx->ctx_instr = save_ga;
3329 vim_free(isn->isn_arg.string);
3330 isn->isn_type = ISN_INSTR;
3331 isn->isn_arg.instr = instr;
3332 return OK;
3333}
3334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335/*
3336 * Compile the argument expressions.
3337 * "arg" points to just after the "(" and is advanced to after the ")"
3338 */
3339 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003340compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003342 char_u *p = *arg;
3343 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003344 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003345 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
Bram Moolenaare6085c52020-04-12 20:19:16 +02003347 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003349 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3350 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003351 if (*p == ')')
3352 {
3353 *arg = p + 1;
3354 return OK;
3355 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003356 if (must_end)
3357 {
3358 semsg(_(e_missing_comma_before_argument_str), p);
3359 return FAIL;
3360 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003361
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003362 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003363 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 return FAIL;
3365 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003366
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003367 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003368 && cctx->ctx_instr.ga_len == instr_count + 1)
3369 {
3370 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3371
3372 // {skip} argument of searchpair() can be compiled if not empty
3373 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3374 compile_string(isn, cctx);
3375 }
3376
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003377 if (*p != ',' && *skipwhite(p) == ',')
3378 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003379 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003380 p = skipwhite(p);
3381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003383 {
3384 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003385 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003386 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003387 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003388 else
3389 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003390 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003391 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003393failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003394 emsg(_(e_missing_close));
3395 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396}
3397
3398/*
3399 * Compile a function call: name(arg1, arg2)
3400 * "arg" points to "name", "arg + varlen" to the "(".
3401 * "argcount_init" is 1 for "value->method()"
3402 * Instructions:
3403 * EVAL arg1
3404 * EVAL arg2
3405 * BCALL / DCALL / UCALL
3406 */
3407 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003408compile_call(
3409 char_u **arg,
3410 size_t varlen,
3411 cctx_T *cctx,
3412 ppconst_T *ppconst,
3413 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414{
3415 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003416 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 int argcount = argcount_init;
3418 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003419 char_u fname_buf[FLEN_FIXED + 1];
3420 char_u *tofree = NULL;
3421 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003422 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003423 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003424 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003425 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003427 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003428 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003429 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003430 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003431 {
3432 char_u *s = skipwhite(*arg + varlen + 1);
3433 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003434 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003435
3436 argvars[0].v_type = VAR_UNKNOWN;
3437 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003438 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003439 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003440 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003441 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003442 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003443 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003444 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003445 {
3446 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3447
3448 *arg = s + 1;
3449 argvars[1].v_type = VAR_UNKNOWN;
3450 tv->v_type = VAR_NUMBER;
3451 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003452 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003453 f_has(argvars, tv);
3454 else
3455 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003456 clear_tv(&argvars[0]);
3457 ++ppconst->pp_used;
3458 return OK;
3459 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003460 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003461 if (!is_has)
3462 {
3463 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3464 return FAIL;
3465 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003466 }
3467
3468 if (generate_ppconst(cctx, ppconst) == FAIL)
3469 return FAIL;
3470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 if (varlen >= sizeof(namebuf))
3472 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003473 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 return FAIL;
3475 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003476 vim_strncpy(namebuf, *arg, varlen);
3477 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003479 // We handle the "skip" argument of searchpair() and searchpairpos()
3480 // differently.
3481 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3482 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3483 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3484 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003487 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003488 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489
Bram Moolenaar03290b82020-12-19 16:30:44 +01003490 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003491 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 {
3493 int idx;
3494
3495 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003496 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003498 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003499 if (STRCMP(name, "flatten") == 0)
3500 {
3501 emsg(_(e_cannot_use_flatten_in_vim9_script));
3502 goto theend;
3503 }
3504
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003505 if (STRCMP(name, "add") == 0 && argcount == 2)
3506 {
3507 garray_T *stack = &cctx->ctx_type_stack;
3508 type_T *type = ((type_T **)stack->ga_data)[
3509 stack->ga_len - 2];
3510
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003511 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003512 if (type->tt_type == VAR_LIST)
3513 {
3514 // inline "add(list, item)" so that the type can be checked
3515 res = generate_LISTAPPEND(cctx);
3516 idx = -1;
3517 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003518 else if (type->tt_type == VAR_BLOB)
3519 {
3520 // inline "add(blob, nr)" so that the type can be checked
3521 res = generate_BLOBAPPEND(cctx);
3522 idx = -1;
3523 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003524 }
3525
3526 if (idx >= 0)
3527 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3528 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003529 else
3530 semsg(_(e_unknownfunc), namebuf);
3531 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 }
3533
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003534 // An argument or local variable can be a function reference, this
3535 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003536 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003537 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003538 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003539 // If we can find the function by name generate the right call.
3540 // Skip global functions here, a local funcref takes precedence.
3541 ufunc = find_func(name, FALSE, cctx);
3542 if (ufunc != NULL && !func_is_global(ufunc))
3543 {
3544 res = generate_CALL(cctx, ufunc, argcount);
3545 goto theend;
3546 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
3549 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003550 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003551 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003553 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003554 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003555 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003556 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003557 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003558
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003559 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003560 goto theend;
3561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562
Bram Moolenaar0f769812020-09-12 18:32:34 +02003563 // If we can find a global function by name generate the right call.
3564 if (ufunc != NULL)
3565 {
3566 res = generate_CALL(cctx, ufunc, argcount);
3567 goto theend;
3568 }
3569
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003570 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003571 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003572 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003573 res = generate_UCALL(cctx, name, argcount);
3574 else
3575 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003576
3577theend:
3578 vim_free(tofree);
3579 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580}
3581
3582// like NAMESPACE_CHAR but with 'a' and 'l'.
3583#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3584
3585/*
3586 * Find the end of a variable or function name. Unlike find_name_end() this
3587 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003588 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 * Return a pointer to just after the name. Equal to "arg" if there is no
3590 * valid name.
3591 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003592 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003593to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594{
3595 char_u *p;
3596
3597 // Quick check for valid starting character.
3598 if (!eval_isnamec1(*arg))
3599 return arg;
3600
3601 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3602 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3603 // and can be used in slice "[n:]".
3604 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003605 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3607 break;
3608 return p;
3609}
3610
3611/*
3612 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003613 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003614 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 */
3616 char_u *
3617to_name_const_end(char_u *arg)
3618{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003619 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 typval_T rettv;
3621
Bram Moolenaar678b2072021-07-26 21:10:11 +02003622 if (STRNCMP(p, "<SNR>", 5) == 0)
3623 p = skipdigits(p + 5);
3624 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 if (p == arg && *arg == '[')
3626 {
3627
3628 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003629 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 p = arg;
3631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 return p;
3633}
3634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635/*
3636 * parse a list: [expr, expr]
3637 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003638 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 */
3640 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003641compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642{
3643 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003644 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003646 int is_const;
3647 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003649 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003651 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003652 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003653 semsg(_(e_list_end), *arg);
3654 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003655 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003656 if (*p == ',')
3657 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003658 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003659 return FAIL;
3660 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003661 if (*p == ']')
3662 {
3663 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003664 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003665 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003666 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003667 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003668 if (!is_const)
3669 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 ++count;
3671 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003672 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003674 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3675 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003676 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003677 return FAIL;
3678 }
3679 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003680 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 p = skipwhite(p);
3682 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003683 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003685 ppconst->pp_is_const = is_all_const;
3686 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687}
3688
3689/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003690 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003691 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003692 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 */
3694 static int
3695compile_lambda(char_u **arg, cctx_T *cctx)
3696{
Bram Moolenaare462f522020-12-27 14:43:30 +01003697 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 typval_T rettv;
3699 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003700 evalarg_T evalarg;
3701
3702 CLEAR_FIELD(evalarg);
3703 evalarg.eval_flags = EVAL_EVALUATE;
3704 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705
3706 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003707 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3708 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003709 {
3710 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003711 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003712 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003713
Bram Moolenaar65c44152020-12-24 15:14:01 +01003714 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003716 ++ufunc->uf_refcount;
3717 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718
Bram Moolenaara9931532021-06-12 15:58:16 +02003719 // Compile it here to get the return type. The return type is optional,
3720 // when it's missing use t_unknown. This is recognized in
3721 // compile_return().
3722 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3723 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003724 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725
Bram Moolenaar648594e2021-07-11 17:55:01 +02003726#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003727 // When the outer function is compiled for profiling, the lambda may be
3728 // called without profiling. Compile it here in the right context.
3729 if (cctx->ctx_compile_type == CT_PROFILE)
3730 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003731#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003732
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003733 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3734 // points into it. Point to the original line to avoid a dangling pointer.
3735 if (evalarg.eval_tofree_cmdline != NULL)
3736 {
3737 size_t off = *arg - evalarg.eval_tofree_cmdline;
3738
3739 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3740 + off;
3741 }
3742
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003743 clear_evalarg(&evalarg, NULL);
3744
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003745 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003746 {
3747 // The return type will now be known.
3748 set_function_type(ufunc);
3749
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003750 // The function reference count will be 1. When the ISN_FUNCREF
3751 // instruction is deleted the reference count is decremented and the
3752 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003753 return generate_FUNCREF(cctx, ufunc);
3754 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003755
3756 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 return FAIL;
3758}
3759
3760/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003761 * Get a lambda and compile it. Uses Vim9 syntax.
3762 */
3763 int
3764get_lambda_tv_and_compile(
3765 char_u **arg,
3766 typval_T *rettv,
3767 int types_optional,
3768 evalarg_T *evalarg)
3769{
3770 int r;
3771 ufunc_T *ufunc;
3772 int save_sc_version = current_sctx.sc_version;
3773
3774 // Get the funcref in "rettv".
3775 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3776 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3777 current_sctx.sc_version = save_sc_version;
3778 if (r != OK)
3779 return r;
3780
3781 // "rettv" will now be a partial referencing the function.
3782 ufunc = rettv->vval.v_partial->pt_func;
3783
3784 // Compile it here to get the return type. The return type is optional,
3785 // when it's missing use t_unknown. This is recognized in
3786 // compile_return().
3787 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3788 ufunc->uf_ret_type = &t_unknown;
3789 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3790
3791 if (ufunc->uf_def_status == UF_COMPILED)
3792 {
3793 // The return type will now be known.
3794 set_function_type(ufunc);
3795 return OK;
3796 }
3797 clear_tv(rettv);
3798 return FAIL;
3799}
3800
3801/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003802 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003804 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 */
3806 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003807compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808{
3809 garray_T *instr = &cctx->ctx_instr;
3810 int count = 0;
3811 dict_T *d = dict_alloc();
3812 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003813 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003814 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003815 int is_const;
3816 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817
3818 if (d == NULL)
3819 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003820 if (generate_ppconst(cctx, ppconst) == FAIL)
3821 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003822 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003824 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825
Bram Moolenaar23c55272020-06-21 16:58:13 +02003826 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003827 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003828 *arg = NULL;
3829 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003830 }
3831
3832 if (**arg == '}')
3833 break;
3834
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003835 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003837 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003839 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003840 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003844 if (isn->isn_type == ISN_PUSHNR)
3845 {
3846 char buf[NUMBUFLEN];
3847
3848 // Convert to string at compile time.
3849 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3850 isn->isn_type = ISN_PUSHS;
3851 isn->isn_arg.string = vim_strsave((char_u *)buf);
3852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 if (isn->isn_type == ISN_PUSHS)
3854 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003855 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003856 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003857 *arg = skipwhite(*arg);
3858 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003859 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003860 emsg(_(e_missing_matching_bracket_after_dict_key));
3861 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003862 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003863 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003865 else
3866 {
3867 // {"name": value},
3868 // {'name': value},
3869 // {name: value} use "name" as a literal key
3870 key = get_literal_key(arg);
3871 if (key == NULL)
3872 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003873 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003874 return FAIL;
3875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876
3877 // Check for duplicate keys, if using string keys.
3878 if (key != NULL)
3879 {
3880 item = dict_find(d, key, -1);
3881 if (item != NULL)
3882 {
3883 semsg(_(e_duplicate_key), key);
3884 goto failret;
3885 }
3886 item = dictitem_alloc(key);
3887 if (item != NULL)
3888 {
3889 item->di_tv.v_type = VAR_UNKNOWN;
3890 item->di_tv.v_lock = 0;
3891 if (dict_add(d, item) == FAIL)
3892 dictitem_free(item);
3893 }
3894 }
3895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 if (**arg != ':')
3897 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003898 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003899 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003900 else
3901 semsg(_(e_missing_dict_colon), *arg);
3902 return FAIL;
3903 }
3904 whitep = *arg + 1;
3905 if (!IS_WHITE_OR_NUL(*whitep))
3906 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003907 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
3909 }
3910
Bram Moolenaar23c55272020-06-21 16:58:13 +02003911 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003912 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003913 *arg = NULL;
3914 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003915 }
3916
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003917 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003919 if (!is_const)
3920 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 ++count;
3922
Bram Moolenaar2c330432020-04-13 14:41:35 +02003923 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003924 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003925 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003926 *arg = NULL;
3927 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 if (**arg == '}')
3930 break;
3931 if (**arg != ',')
3932 {
3933 semsg(_(e_missing_dict_comma), *arg);
3934 goto failret;
3935 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003936 if (IS_WHITE_OR_NUL(*whitep))
3937 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003938 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003939 return FAIL;
3940 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003941 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003942 if (!IS_WHITE_OR_NUL(*whitep))
3943 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003944 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003945 return FAIL;
3946 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003947 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 *arg = *arg + 1;
3951
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003952 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003953 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003954 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003955 *arg += STRLEN(*arg);
3956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003958 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 return generate_NEWDICT(cctx, count);
3960
3961failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003962 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003963 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003964 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003965 *arg = (char_u *)"";
3966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 dict_unref(d);
3968 return FAIL;
3969}
3970
3971/*
3972 * Compile "&option".
3973 */
3974 static int
3975compile_get_option(char_u **arg, cctx_T *cctx)
3976{
3977 typval_T rettv;
3978 char_u *start = *arg;
3979 int ret;
3980
3981 // parse the option and get the current value to get the type.
3982 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003983 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 if (ret == OK)
3985 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003986 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003987 char_u *name = vim_strnsave(start, *arg - start);
3988 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3989 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990
3991 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3992 vim_free(name);
3993 }
3994 clear_tv(&rettv);
3995
3996 return ret;
3997}
3998
3999/*
4000 * Compile "$VAR".
4001 */
4002 static int
4003compile_get_env(char_u **arg, cctx_T *cctx)
4004{
4005 char_u *start = *arg;
4006 int len;
4007 int ret;
4008 char_u *name;
4009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 ++*arg;
4011 len = get_env_len(arg);
4012 if (len == 0)
4013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004014 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 return FAIL;
4016 }
4017
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004018 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 name = vim_strnsave(start, len + 1);
4020 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4021 vim_free(name);
4022 return ret;
4023}
4024
4025/*
4026 * Compile "@r".
4027 */
4028 static int
4029compile_get_register(char_u **arg, cctx_T *cctx)
4030{
4031 int ret;
4032
4033 ++*arg;
4034 if (**arg == NUL)
4035 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004036 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 return FAIL;
4038 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004039 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
4041 emsg_invreg(**arg);
4042 return FAIL;
4043 }
4044 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4045 ++*arg;
4046 return ret;
4047}
4048
4049/*
4050 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004051 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 */
4053 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004054apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004056 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
4058 // this works from end to start
4059 while (p > start)
4060 {
4061 --p;
4062 if (*p == '-' || *p == '+')
4063 {
4064 // only '-' has an effect, for '+' we only check the type
4065#ifdef FEAT_FLOAT
4066 if (rettv->v_type == VAR_FLOAT)
4067 {
4068 if (*p == '-')
4069 rettv->vval.v_float = -rettv->vval.v_float;
4070 }
4071 else
4072#endif
4073 {
4074 varnumber_T val;
4075 int error = FALSE;
4076
4077 // tv_get_number_chk() accepts a string, but we don't want that
4078 // here
4079 if (check_not_string(rettv) == FAIL)
4080 return FAIL;
4081 val = tv_get_number_chk(rettv, &error);
4082 clear_tv(rettv);
4083 if (error)
4084 return FAIL;
4085 if (*p == '-')
4086 val = -val;
4087 rettv->v_type = VAR_NUMBER;
4088 rettv->vval.v_number = val;
4089 }
4090 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004091 else if (numeric_only)
4092 {
4093 ++p;
4094 break;
4095 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004096 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
4098 int v = tv2bool(rettv);
4099
4100 // '!' is permissive in the type.
4101 clear_tv(rettv);
4102 rettv->v_type = VAR_BOOL;
4103 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4104 }
4105 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004106 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 return OK;
4108}
4109
4110/*
4111 * Recognize v: variables that are constants and set "rettv".
4112 */
4113 static void
4114get_vim_constant(char_u **arg, typval_T *rettv)
4115{
4116 if (STRNCMP(*arg, "v:true", 6) == 0)
4117 {
4118 rettv->v_type = VAR_BOOL;
4119 rettv->vval.v_number = VVAL_TRUE;
4120 *arg += 6;
4121 }
4122 else if (STRNCMP(*arg, "v:false", 7) == 0)
4123 {
4124 rettv->v_type = VAR_BOOL;
4125 rettv->vval.v_number = VVAL_FALSE;
4126 *arg += 7;
4127 }
4128 else if (STRNCMP(*arg, "v:null", 6) == 0)
4129 {
4130 rettv->v_type = VAR_SPECIAL;
4131 rettv->vval.v_number = VVAL_NULL;
4132 *arg += 6;
4133 }
4134 else if (STRNCMP(*arg, "v:none", 6) == 0)
4135 {
4136 rettv->v_type = VAR_SPECIAL;
4137 rettv->vval.v_number = VVAL_NONE;
4138 *arg += 6;
4139 }
4140}
4141
Bram Moolenaar657137c2021-01-09 15:45:23 +01004142 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004143get_compare_type(char_u *p, int *len, int *type_is)
4144{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004145 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004146 int i;
4147
4148 switch (p[0])
4149 {
4150 case '=': if (p[1] == '=')
4151 type = EXPR_EQUAL;
4152 else if (p[1] == '~')
4153 type = EXPR_MATCH;
4154 break;
4155 case '!': if (p[1] == '=')
4156 type = EXPR_NEQUAL;
4157 else if (p[1] == '~')
4158 type = EXPR_NOMATCH;
4159 break;
4160 case '>': if (p[1] != '=')
4161 {
4162 type = EXPR_GREATER;
4163 *len = 1;
4164 }
4165 else
4166 type = EXPR_GEQUAL;
4167 break;
4168 case '<': if (p[1] != '=')
4169 {
4170 type = EXPR_SMALLER;
4171 *len = 1;
4172 }
4173 else
4174 type = EXPR_SEQUAL;
4175 break;
4176 case 'i': if (p[1] == 's')
4177 {
4178 // "is" and "isnot"; but not a prefix of a name
4179 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4180 *len = 5;
4181 i = p[*len];
4182 if (!isalnum(i) && i != '_')
4183 {
4184 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4185 *type_is = TRUE;
4186 }
4187 }
4188 break;
4189 }
4190 return type;
4191}
4192
4193/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004194 * Skip over an expression, ignoring most errors.
4195 */
4196 static void
4197skip_expr_cctx(char_u **arg, cctx_T *cctx)
4198{
4199 evalarg_T evalarg;
4200
4201 CLEAR_FIELD(evalarg);
4202 evalarg.eval_cctx = cctx;
4203 skip_expr(arg, &evalarg);
4204}
4205
4206/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004208 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 */
4210 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004211compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004213 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
4215 // this works from end to start
4216 while (p > start)
4217 {
4218 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004219 while (VIM_ISWHITE(*p))
4220 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 if (*p == '-' || *p == '+')
4222 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004223 int negate = *p == '-';
4224 isn_T *isn;
4225 garray_T *stack = &cctx->ctx_type_stack;
4226 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004228 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4229 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4230 return FAIL;
4231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4233 {
4234 --p;
4235 if (*p == '-')
4236 negate = !negate;
4237 }
4238 // only '-' has an effect, for '+' we only check the type
4239 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004240 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004242 if (isn == NULL)
4243 return FAIL;
4244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004246 else if (numeric_only)
4247 {
4248 ++p;
4249 break;
4250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 else
4252 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004253 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004255 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004257 if (p[-1] == '!')
4258 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004261 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 return FAIL;
4263 }
4264 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004265 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return OK;
4267}
4268
4269/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004270 * Compile "(expression)": recursive!
4271 * Return FAIL/OK.
4272 */
4273 static int
4274compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4275{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004276 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004277 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004278
Bram Moolenaar24156692021-01-14 20:35:49 +01004279 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4280 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004281 if (ppconst->pp_used <= PPSIZE - 10)
4282 {
4283 ret = compile_expr1(arg, cctx, ppconst);
4284 }
4285 else
4286 {
4287 // Not enough space in ppconst, flush constants.
4288 if (generate_ppconst(cctx, ppconst) == FAIL)
4289 return FAIL;
4290 ret = compile_expr0(arg, cctx);
4291 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004292 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4293 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004294 if (**arg == ')')
4295 ++*arg;
4296 else if (ret == OK)
4297 {
4298 emsg(_(e_missing_close));
4299 ret = FAIL;
4300 }
4301 return ret;
4302}
4303
4304/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004306 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 */
4308 static int
4309compile_subscript(
4310 char_u **arg,
4311 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004312 char_u *start_leader,
4313 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004314 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004316 char_u *name_start = *end_leader;
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 for (;;)
4319 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004320 char_u *p = skipwhite(*arg);
4321
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004322 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004323 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004324 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004325
4326 // If a following line starts with "->{" or "->X" advance to that
4327 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004328 // Also if a following line starts with ".x".
4329 if (next != NULL &&
4330 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004331 && (next[2] == '{'
4332 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004333 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004334 {
4335 next = next_line_from_context(cctx, TRUE);
4336 if (next == NULL)
4337 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004338 *arg = next;
4339 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004340 }
4341 }
4342
Bram Moolenaarcd268012021-07-22 19:11:08 +02004343 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4344 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004345 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004347 garray_T *stack = &cctx->ctx_type_stack;
4348 type_T *type;
4349 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004351 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004352 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004353 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004356 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4357
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004358 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004359 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004361 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 return FAIL;
4363 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004364 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004366 char_u *pstart = p;
4367
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004368 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004369 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004370 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 // something->method()
4373 // Apply the '!', '-' and '+' first:
4374 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004375 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004378 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004379 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004380 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004381 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004382 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004383 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004384 garray_T *stack = &cctx->ctx_type_stack;
4385 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004386 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004387 int expr_isn_start = cctx->ctx_instr.ga_len;
4388 int expr_isn_end;
4389 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004390
4391 // Funcref call: list->(Refs[2])(arg)
4392 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004393 //
4394 // Fist compile the function expression.
4395 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004396 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004397
4398 // Remember the next instruction index, where the instructions
4399 // for arguments are being written.
4400 expr_isn_end = cctx->ctx_instr.ga_len;
4401
4402 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004403 if (**arg != '(')
4404 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004405 if (*skipwhite(*arg) == '(')
4406 emsg(_(e_nowhitespace));
4407 else
4408 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004409 return FAIL;
4410 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004411 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004412 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004413 return FAIL;
4414
Bram Moolenaar2927c072021-04-05 19:41:21 +02004415 // Move the instructions for the arguments to before the
4416 // instructions of the expression and move the type of the
4417 // expression after the argument types. This is what ISN_PCALL
4418 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004419 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004420 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4421 if (arg_isn_count > 0)
4422 {
4423 int expr_isn_count = expr_isn_end - expr_isn_start;
4424 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4425
4426 if (isn == NULL)
4427 return FAIL;
4428 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4429 + expr_isn_start,
4430 sizeof(isn_T) * expr_isn_count);
4431 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4432 + expr_isn_start,
4433 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4434 sizeof(isn_T) * arg_isn_count);
4435 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4436 + expr_isn_start + arg_isn_count,
4437 isn, sizeof(isn_T) * expr_isn_count);
4438 vim_free(isn);
4439
4440 type = ((type_T **)stack->ga_data)[type_idx_start];
4441 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4442 ((type_T **)stack->ga_data) + type_idx_start + 1,
4443 sizeof(type_T *)
4444 * (stack->ga_len - type_idx_start - 1));
4445 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4446 }
4447
Bram Moolenaar7e368202020-12-25 21:56:57 +01004448 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004449 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 return FAIL;
4451 }
4452 else
4453 {
4454 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004455 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004456 if (!eval_isnamec1(*p))
4457 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004458 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004459 return FAIL;
4460 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004461 if (ASCII_ISALPHA(*p) && p[1] == ':')
4462 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004463 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 ;
4465 if (*p != '(')
4466 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004467 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 return FAIL;
4469 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 return FAIL;
4472 }
4473 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004474 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004476 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004477
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004478 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004479 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004480 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004481 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004482 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004483 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004484 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004485
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004486 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004487 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004488 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004489 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004490 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004491 // missing first index is equal to zero
4492 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004493 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004494 else
4495 {
4496 if (compile_expr0(arg, cctx) == FAIL)
4497 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004498 if (**arg == ':')
4499 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004500 semsg(_(e_white_space_required_before_and_after_str_at_str),
4501 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004502 return FAIL;
4503 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004504 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004505 return FAIL;
4506 *arg = skipwhite(*arg);
4507 }
4508 if (**arg == ':')
4509 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004510 is_slice = TRUE;
4511 ++*arg;
4512 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4513 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004514 semsg(_(e_white_space_required_before_and_after_str_at_str),
4515 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004516 return FAIL;
4517 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004518 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004519 return FAIL;
4520 if (**arg == ']')
4521 // missing second index is equal to end of string
4522 generate_PUSHNR(cctx, -1);
4523 else
4524 {
4525 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004526 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004527 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004528 return FAIL;
4529 *arg = skipwhite(*arg);
4530 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
4533 if (**arg != ']')
4534 {
4535 emsg(_(e_missbrac));
4536 return FAIL;
4537 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004538 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
Bram Moolenaare42939a2021-04-05 17:11:17 +02004540 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004541 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004543 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004545 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004546 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004547 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004548 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004549
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004550 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004551 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004552 {
4553 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004554 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004555 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004556 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004557 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 while (eval_isnamec(*p))
4559 MB_PTR_ADV(p);
4560 if (p == *arg)
4561 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004562 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 return FAIL;
4564 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004565 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 return FAIL;
4567 *arg = p;
4568 }
4569 else
4570 break;
4571 }
4572
4573 // TODO - see handle_subscript():
4574 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4575 // Don't do this when "Func" is already a partial that was bound
4576 // explicitly (pt_auto is FALSE).
4577
4578 return OK;
4579}
4580
4581/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004582 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4583 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004585 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004586 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004587 *
4588 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 */
4590
4591/*
4592 * number number constant
4593 * 0zFFFFFFFF Blob constant
4594 * "string" string constant
4595 * 'string' literal string constant
4596 * &option-name option value
4597 * @r register contents
4598 * identifier variable value
4599 * function() function call
4600 * $VAR environment variable
4601 * (expression) nested expression
4602 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004603 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 *
4605 * Also handle:
4606 * ! in front logical NOT
4607 * - in front unary minus
4608 * + in front unary plus (ignored)
4609 * trailing (arg) funcref/partial call
4610 * trailing [] subscript in String or List
4611 * trailing .name entry in Dictionary
4612 * trailing ->name() method call
4613 */
4614 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004615compile_expr7(
4616 char_u **arg,
4617 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004618 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 char_u *start_leader, *end_leader;
4621 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004622 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004623 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004625 ppconst->pp_is_const = FALSE;
4626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 /*
4628 * Skip '!', '-' and '+' characters. They are handled later.
4629 */
4630 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004631 if (eval_leader(arg, TRUE) == FAIL)
4632 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 end_leader = *arg;
4634
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004635 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 switch (**arg)
4637 {
4638 /*
4639 * Number constant.
4640 */
4641 case '0': // also for blob starting with 0z
4642 case '1':
4643 case '2':
4644 case '3':
4645 case '4':
4646 case '5':
4647 case '6':
4648 case '7':
4649 case '8':
4650 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004651 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004653 // Apply "-" and "+" just before the number now, right to
4654 // left. Matters especially when "->" follows. Stops at
4655 // '!'.
4656 if (apply_leader(rettv, TRUE,
4657 start_leader, &end_leader) == FAIL)
4658 {
4659 clear_tv(rettv);
4660 return FAIL;
4661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 break;
4663
4664 /*
4665 * String constant: "string".
4666 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004667 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 return FAIL;
4669 break;
4670
4671 /*
4672 * Literal string constant: 'str''ing'.
4673 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004674 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 return FAIL;
4676 break;
4677
4678 /*
4679 * Constant Vim variable.
4680 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004681 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 ret = NOTDONE;
4683 break;
4684
4685 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 * "true" constant
4687 */
4688 case 't': if (STRNCMP(*arg, "true", 4) == 0
4689 && !eval_isnamec((*arg)[4]))
4690 {
4691 *arg += 4;
4692 rettv->v_type = VAR_BOOL;
4693 rettv->vval.v_number = VVAL_TRUE;
4694 }
4695 else
4696 ret = NOTDONE;
4697 break;
4698
4699 /*
4700 * "false" constant
4701 */
4702 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4703 && !eval_isnamec((*arg)[5]))
4704 {
4705 *arg += 5;
4706 rettv->v_type = VAR_BOOL;
4707 rettv->vval.v_number = VVAL_FALSE;
4708 }
4709 else
4710 ret = NOTDONE;
4711 break;
4712
4713 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004714 * "null" constant
4715 */
4716 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004717 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004718 {
4719 *arg += 4;
4720 rettv->v_type = VAR_SPECIAL;
4721 rettv->vval.v_number = VVAL_NULL;
4722 }
4723 else
4724 ret = NOTDONE;
4725 break;
4726
4727 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 * List: [expr, expr]
4729 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004730 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4731 return FAIL;
4732 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 break;
4734
4735 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 * Dictionary: {'key': val, 'key': val}
4737 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004738 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4739 return FAIL;
4740 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 break;
4742
4743 /*
4744 * Option value: &name
4745 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004746 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4747 return FAIL;
4748 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 break;
4750
4751 /*
4752 * Environment variable: $VAR.
4753 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004754 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4755 return FAIL;
4756 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 break;
4758
4759 /*
4760 * Register contents: @r.
4761 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004762 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4763 return FAIL;
4764 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 break;
4766 /*
4767 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004768 * lambda: (arg, arg) => expr
4769 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004771 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4772 ret = compile_lambda(arg, cctx);
4773 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004774 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 break;
4776
4777 default: ret = NOTDONE;
4778 break;
4779 }
4780 if (ret == FAIL)
4781 return FAIL;
4782
Bram Moolenaar1c747212020-05-09 18:28:34 +02004783 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004785 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 clear_tv(rettv);
4787 else
4788 // A constant expression can possibly be handled compile time,
4789 // return the value instead of generating code.
4790 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 }
4792 else if (ret == NOTDONE)
4793 {
4794 char_u *p;
4795 int r;
4796
4797 if (!eval_isnamec1(**arg))
4798 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004799 if (!vim9_bad_comment(*arg))
4800 {
4801 if (ends_excmd(*skipwhite(*arg)))
4802 semsg(_(e_empty_expression_str), *arg);
4803 else
4804 semsg(_(e_name_expected_str), *arg);
4805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 return FAIL;
4807 }
4808
4809 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004810 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004811 if (p - *arg == (size_t)1 && **arg == '_')
4812 {
4813 emsg(_(e_cannot_use_underscore_here));
4814 return FAIL;
4815 }
4816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818 {
4819 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004822 {
4823 if (generate_ppconst(cctx, ppconst) == FAIL)
4824 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004825 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 if (r == FAIL)
4828 return FAIL;
4829 }
4830
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004831 // Handle following "[]", ".member", etc.
4832 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004833 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 ppconst) == FAIL)
4835 return FAIL;
4836 if (ppconst->pp_used > 0)
4837 {
4838 // apply the '!', '-' and '+' before the constant
4839 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004840 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 return FAIL;
4842 return OK;
4843 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004844 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004846 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847}
4848
4849/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004850 * Give the "white on both sides" error, taking the operator from "p[len]".
4851 */
4852 void
4853error_white_both(char_u *op, int len)
4854{
4855 char_u buf[10];
4856
4857 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004858 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004859}
4860
4861/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004862 * <type>expr7: runtime type check / conversion
4863 */
4864 static int
4865compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4866{
4867 type_T *want_type = NULL;
4868
4869 // Recognize <type>
4870 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4871 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004872 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004873 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4874 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004875 return FAIL;
4876
4877 if (**arg != '>')
4878 {
4879 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004880 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004881 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004882 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004883 return FAIL;
4884 }
4885 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004886 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004887 return FAIL;
4888 }
4889
4890 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4891 return FAIL;
4892
4893 if (want_type != NULL)
4894 {
4895 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004896 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004897 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004898
Bram Moolenaard1103582020-08-14 22:44:25 +02004899 generate_ppconst(cctx, ppconst);
4900 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004901 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004902 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004903 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004904 return FAIL;
4905 }
4906 }
4907
4908 return OK;
4909}
4910
4911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 */
4916 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918{
4919 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004920 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004921 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004923 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004924 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 return FAIL;
4926
4927 /*
4928 * Repeat computing, until no "*", "/" or "%" is following.
4929 */
4930 for (;;)
4931 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004932 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 if (*op != '*' && *op != '/' && *op != '%')
4934 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004935 if (next != NULL)
4936 {
4937 *arg = next_line_from_context(cctx, TRUE);
4938 op = skipwhite(*arg);
4939 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004940
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004941 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004943 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004944 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004946 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004947 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004949 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004950 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004952
4953 if (ppconst->pp_used == ppconst_used + 2
4954 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4955 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004956 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004957 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4958 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4959 varnumber_T res = 0;
4960 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004962 // both are numbers: compute the result
4963 switch (*op)
4964 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004965 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004966 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004967 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004968 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004969 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004970 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004971 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004972 break;
4973 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004974 if (failed)
4975 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004976 tv1->vval.v_number = res;
4977 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004978 }
4979 else
4980 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004981 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004982 generate_two_op(cctx, op);
4983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 }
4985
4986 return OK;
4987}
4988
4989/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004990 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 * - number subtraction
4992 * .. string concatenation
4993 */
4994 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996{
4997 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004998 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005000 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001
5002 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005003 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 return FAIL;
5005
5006 /*
5007 * Repeat computing, until no "+", "-" or ".." is following.
5008 */
5009 for (;;)
5010 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005011 op = may_peek_next_line(cctx, *arg, &next);
5012 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005014 if (op[0] == op[1] && *op != '.' && next)
5015 // Finding "++" or "--" on the next line is a separate command.
5016 // But ".." is concatenation.
5017 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005019 if (next != NULL)
5020 {
5021 *arg = next_line_from_context(cctx, TRUE);
5022 op = skipwhite(*arg);
5023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005025 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005027 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005028 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 }
5030
Bram Moolenaare0de1712020-12-02 17:36:54 +01005031 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005032 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005034 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005035 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 return FAIL;
5037
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005039 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005040 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5041 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5042 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5043 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005045 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5046 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005047
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005048 // concat/subtract/add constant numbers
5049 if (*op == '+')
5050 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5051 else if (*op == '-')
5052 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5053 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005054 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005055 // concatenate constant strings
5056 char_u *s1 = tv1->vval.v_string;
5057 char_u *s2 = tv2->vval.v_string;
5058 size_t len1 = STRLEN(s1);
5059
5060 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5061 if (tv1->vval.v_string == NULL)
5062 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005064 return FAIL;
5065 }
5066 mch_memmove(tv1->vval.v_string, s1, len1);
5067 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005068 vim_free(s1);
5069 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005070 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 }
5073 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005074 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005075 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005076 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005077 if (*op == '.')
5078 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005079 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5080 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005081 return FAIL;
5082 generate_instr_drop(cctx, ISN_CONCAT, 1);
5083 }
5084 else
5085 generate_two_op(cctx, op);
5086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 }
5088
5089 return OK;
5090}
5091
5092/*
5093 * expr5a == expr5b
5094 * expr5a =~ expr5b
5095 * expr5a != expr5b
5096 * expr5a !~ expr5b
5097 * expr5a > expr5b
5098 * expr5a >= expr5b
5099 * expr5a < expr5b
5100 * expr5a <= expr5b
5101 * expr5a is expr5b
5102 * expr5a isnot expr5b
5103 *
5104 * Produces instructions:
5105 * EVAL expr5a Push result of "expr5a"
5106 * EVAL expr5b Push result of "expr5b"
5107 * COMPARE one of the compare instructions
5108 */
5109 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005110compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005112 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005114 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005117 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118
5119 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 return FAIL;
5122
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005123 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005124 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125
5126 /*
5127 * If there is a comparative operator, use it.
5128 */
5129 if (type != EXPR_UNKNOWN)
5130 {
5131 int ic = FALSE; // Default: do not ignore case
5132
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005133 if (next != NULL)
5134 {
5135 *arg = next_line_from_context(cctx, TRUE);
5136 p = skipwhite(*arg);
5137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 if (type_is && (p[len] == '?' || p[len] == '#'))
5139 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005140 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 return FAIL;
5142 }
5143 // extra question mark appended: ignore case
5144 if (p[len] == '?')
5145 {
5146 ic = TRUE;
5147 ++len;
5148 }
5149 // extra '#' appended: match case (ignored)
5150 else if (p[len] == '#')
5151 ++len;
5152 // nothing appended: match case
5153
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005154 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005156 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005157 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 }
5159
5160 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005161 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005162 return FAIL;
5163
Bram Moolenaara5565e42020-05-09 15:44:01 +02005164 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 return FAIL;
5166
Bram Moolenaara5565e42020-05-09 15:44:01 +02005167 if (ppconst->pp_used == ppconst_used + 2)
5168 {
5169 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5170 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5171 int ret;
5172
5173 // Both sides are a constant, compute the result now.
5174 // First check for a valid combination of types, this is more
5175 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005176 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 ret = FAIL;
5178 else
5179 {
5180 ret = typval_compare(tv1, tv2, type, ic);
5181 tv1->v_type = VAR_BOOL;
5182 tv1->vval.v_number = tv1->vval.v_number
5183 ? VVAL_TRUE : VVAL_FALSE;
5184 clear_tv(tv2);
5185 --ppconst->pp_used;
5186 }
5187 return ret;
5188 }
5189
5190 generate_ppconst(cctx, ppconst);
5191 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 }
5193
5194 return OK;
5195}
5196
Bram Moolenaar7f141552020-05-09 17:35:53 +02005197static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199/*
5200 * Compile || or &&.
5201 */
5202 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005203compile_and_or(
5204 char_u **arg,
5205 cctx_T *cctx,
5206 char *op,
5207 ppconst_T *ppconst,
5208 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005210 char_u *next;
5211 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 int opchar = *op;
5213
5214 if (p[0] == opchar && p[1] == opchar)
5215 {
5216 garray_T *instr = &cctx->ctx_instr;
5217 garray_T end_ga;
5218
5219 /*
5220 * Repeat until there is no following "||" or "&&"
5221 */
5222 ga_init2(&end_ga, sizeof(int), 10);
5223 while (p[0] == opchar && p[1] == opchar)
5224 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005225 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005226 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005227 int start_ctx_lnum = cctx->ctx_lnum;
5228 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005229 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005230
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005231 if (next != NULL)
5232 {
5233 *arg = next_line_from_context(cctx, TRUE);
5234 p = skipwhite(*arg);
5235 }
5236
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005237 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5238 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005239 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005240 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005241 return FAIL;
5242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005244 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005245 SOURCING_LNUM = start_lnum;
5246 save_lnum = cctx->ctx_lnum;
5247 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005248
5249 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005250 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005251 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005252 // TODO: use ppconst if the value is a constant
5253 generate_ppconst(cctx, ppconst);
5254
5255 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005256 status = bool_on_stack(cctx);
5257 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005258 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005259 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005260 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005261 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 {
5263 ga_clear(&end_ga);
5264 return FAIL;
5265 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5268 ++end_ga.ga_len;
5269 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005270 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271
5272 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005273 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005274 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005275 {
5276 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005277 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005278 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005279
Bram Moolenaara5565e42020-05-09 15:44:01 +02005280 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5281 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282 {
5283 ga_clear(&end_ga);
5284 return FAIL;
5285 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005286
5287 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005289
5290 if (check_ppconst_bool(ppconst) == FAIL)
5291 {
5292 ga_clear(&end_ga);
5293 return FAIL;
5294 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005295 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005297 // Every part must evaluate to a bool.
5298 if (bool_on_stack(cctx) == FAIL)
5299 {
5300 ga_clear(&end_ga);
5301 return FAIL;
5302 }
5303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 // Fill in the end label in all jumps.
5305 while (end_ga.ga_len > 0)
5306 {
5307 isn_T *isn;
5308
5309 --end_ga.ga_len;
5310 isn = ((isn_T *)instr->ga_data)
5311 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5312 isn->isn_arg.jump.jump_where = instr->ga_len;
5313 }
5314 ga_clear(&end_ga);
5315 }
5316
5317 return OK;
5318}
5319
5320/*
5321 * expr4a && expr4a && expr4a logical AND
5322 *
5323 * Produces instructions:
5324 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005325 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005326 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005328 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 * EVAL expr4c Push result of "expr4c"
5330 * end:
5331 */
5332 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005333compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005335 int ppconst_used = ppconst->pp_used;
5336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005338 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 return FAIL;
5340
5341 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343}
5344
5345/*
5346 * expr3a || expr3b || expr3c logical OR
5347 *
5348 * Produces instructions:
5349 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005350 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005351 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005353 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 * EVAL expr3c Push result of "expr3c"
5355 * end:
5356 */
5357 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005358compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005360 int ppconst_used = ppconst->pp_used;
5361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005363 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 return FAIL;
5365
5366 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005367 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368}
5369
5370/*
5371 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005373 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 * JUMP_IF_FALSE alt jump if false
5375 * EVAL expr1a
5376 * JUMP_ALWAYS end
5377 * alt: EVAL expr1b
5378 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005379 *
5380 * Toplevel expression: expr2 ?? expr1
5381 * Produces instructions:
5382 * EVAL expr2 Push result of "expr2"
5383 * JUMP_AND_KEEP_IF_TRUE end jump if true
5384 * EVAL expr1
5385 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 */
5387 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005388compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389{
5390 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005391 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005392 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393
Bram Moolenaar3988f642020-08-27 22:43:03 +02005394 // Ignore all kinds of errors when not producing code.
5395 if (cctx->ctx_skip == SKIP_YES)
5396 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005397 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005398 return OK;
5399 }
5400
Bram Moolenaar61a89812020-05-07 16:58:17 +02005401 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 return FAIL;
5404
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005405 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 if (*p == '?')
5407 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005408 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 garray_T *instr = &cctx->ctx_instr;
5410 garray_T *stack = &cctx->ctx_type_stack;
5411 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005412 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005414 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005415 int has_const_expr = FALSE;
5416 int const_value = FALSE;
5417 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005419 if (next != NULL)
5420 {
5421 *arg = next_line_from_context(cctx, TRUE);
5422 p = skipwhite(*arg);
5423 }
5424
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005425 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005426 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005427 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005428 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005429 return FAIL;
5430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaara5565e42020-05-09 15:44:01 +02005432 if (ppconst->pp_used == ppconst_used + 1)
5433 {
5434 // the condition is a constant, we know whether the ? or the :
5435 // expression is to be evaluated.
5436 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005437 if (op_falsy)
5438 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5439 else
5440 {
5441 int error = FALSE;
5442
5443 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5444 &error);
5445 if (error)
5446 return FAIL;
5447 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005448 cctx->ctx_skip = save_skip == SKIP_YES ||
5449 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5450
5451 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5452 // "left ?? right" and "left" is truthy: produce "left"
5453 generate_ppconst(cctx, ppconst);
5454 else
5455 {
5456 clear_tv(&ppconst->pp_tv[ppconst_used]);
5457 --ppconst->pp_used;
5458 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005459 }
5460 else
5461 {
5462 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005463 if (op_falsy)
5464 end_idx = instr->ga_len;
5465 generate_JUMP(cctx, op_falsy
5466 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5467 if (op_falsy)
5468 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470
5471 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005472 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005473 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005474 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005475 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476
Bram Moolenaara5565e42020-05-09 15:44:01 +02005477 if (!has_const_expr)
5478 {
5479 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005481 if (!op_falsy)
5482 {
5483 // remember the type and drop it
5484 --stack->ga_len;
5485 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005487 end_idx = instr->ga_len;
5488 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005489
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005490 // jump here from JUMP_IF_FALSE
5491 isn = ((isn_T *)instr->ga_data) + alt_idx;
5492 isn->isn_arg.jump.jump_where = instr->ga_len;
5493 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005494 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005496 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005498 // Check for the ":".
5499 p = may_peek_next_line(cctx, *arg, &next);
5500 if (*p != ':')
5501 {
5502 emsg(_(e_missing_colon));
5503 return FAIL;
5504 }
5505 if (next != NULL)
5506 {
5507 *arg = next_line_from_context(cctx, TRUE);
5508 p = skipwhite(*arg);
5509 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005510
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005511 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5512 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005513 semsg(_(e_white_space_required_before_and_after_str_at_str),
5514 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005515 return FAIL;
5516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005518 // evaluate the third expression
5519 if (has_const_expr)
5520 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005521 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005522 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005523 return FAIL;
5524 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5525 return FAIL;
5526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527
Bram Moolenaara5565e42020-05-09 15:44:01 +02005528 if (!has_const_expr)
5529 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005530 type_T **typep;
5531
Bram Moolenaara5565e42020-05-09 15:44:01 +02005532 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
Bram Moolenaara5565e42020-05-09 15:44:01 +02005534 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005535 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5536 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005537
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005538 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005539 isn = ((isn_T *)instr->ga_data) + end_idx;
5540 isn->isn_arg.jump.jump_where = instr->ga_len;
5541 }
5542
5543 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 }
5545 return OK;
5546}
5547
5548/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005549 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005550 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5551 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005552 */
5553 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005554compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005555{
5556 ppconst_T ppconst;
5557
5558 CLEAR_FIELD(ppconst);
5559 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5560 {
5561 clear_ppconst(&ppconst);
5562 return FAIL;
5563 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005564 if (is_const != NULL)
5565 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005566 if (generate_ppconst(cctx, &ppconst) == FAIL)
5567 return FAIL;
5568 return OK;
5569}
5570
5571/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005572 * Toplevel expression.
5573 */
5574 static int
5575compile_expr0(char_u **arg, cctx_T *cctx)
5576{
5577 return compile_expr0_ext(arg, cctx, NULL);
5578}
5579
5580/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005581 * Compile "return [expr]".
5582 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583 */
5584 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005585compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005586{
5587 char_u *p = arg;
5588 garray_T *stack = &cctx->ctx_type_stack;
5589 type_T *stack_type;
5590
5591 if (*p != NUL && *p != '|' && *p != '\n')
5592 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005593 if (legacy)
5594 {
5595 int save_flags = cmdmod.cmod_flags;
5596
5597 generate_LEGACY_EVAL(cctx, p);
5598 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5599 0, cctx, FALSE, FALSE) == FAIL)
5600 return NULL;
5601 cmdmod.cmod_flags |= CMOD_LEGACY;
5602 (void)skip_expr(&p, NULL);
5603 cmdmod.cmod_flags = save_flags;
5604 }
5605 else
5606 {
5607 // compile return argument into instructions
5608 if (compile_expr0(&p, cctx) == FAIL)
5609 return NULL;
5610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005612 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005613 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005614 // "check_return_type" with uf_ret_type set to &t_unknown is used
5615 // for an inline function without a specified return type. Set the
5616 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005617 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005618 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005619 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5620 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005621 || (!check_return_type
5622 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005623 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005624 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005625 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005626 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005627 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005628 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5629 && stack_type->tt_type != VAR_VOID
5630 && stack_type->tt_type != VAR_UNKNOWN)
5631 {
5632 emsg(_(e_returning_value_in_function_without_return_type));
5633 return NULL;
5634 }
5635 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005636 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005637 return NULL;
5638 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 }
5641 else
5642 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005643 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005644 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005645 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5646 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005648 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649 return NULL;
5650 }
5651
5652 // No argument, return zero.
5653 generate_PUSHNR(cctx, 0);
5654 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005655
5656 // Undo any command modifiers.
5657 generate_undo_cmdmods(cctx);
5658
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005659 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660 return NULL;
5661
5662 // "return val | endif" is possible
5663 return skipwhite(p);
5664}
5665
5666/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005667 * Get a line from the compilation context, compatible with exarg_T getline().
5668 * Return a pointer to the line in allocated memory.
5669 * Return NULL for end-of-file or some error.
5670 */
5671 static char_u *
5672exarg_getline(
5673 int c UNUSED,
5674 void *cookie,
5675 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005676 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005677{
5678 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005679 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005680
Bram Moolenaar66250c92020-08-20 15:02:42 +02005681 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005683 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005684 return NULL;
5685 ++cctx->ctx_lnum;
5686 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5687 // Comment lines result in NULL pointers, skip them.
5688 if (p != NULL)
5689 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005690 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005691}
5692
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005693 void
5694fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5695{
5696 eap->getline = exarg_getline;
5697 eap->cookie = cctx;
5698}
5699
Bram Moolenaar04b12692020-05-04 23:24:44 +02005700/*
5701 * Compile a nested :def command.
5702 */
5703 static char_u *
5704compile_nested_function(exarg_T *eap, cctx_T *cctx)
5705{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005706 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005707 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005708 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005709 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005710 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005711 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005712 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005713
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005714 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005715 {
5716 emsg(_(e_cannot_use_bang_with_nested_def));
5717 return NULL;
5718 }
5719
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005720 if (*name_start == '/')
5721 {
5722 name_end = skip_regexp(name_start + 1, '/', TRUE);
5723 if (*name_end == '/')
5724 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005725 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005726 }
5727 if (name_end == name_start || *skipwhite(name_end) != '(')
5728 {
5729 if (!ends_excmd2(name_start, name_end))
5730 {
5731 semsg(_(e_invalid_command_str), eap->cmd);
5732 return NULL;
5733 }
5734
5735 // "def" or "def Name": list functions
5736 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5737 return NULL;
5738 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5739 }
5740
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005741 // Only g:Func() can use a namespace.
5742 if (name_start[1] == ':' && !is_global)
5743 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005744 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005745 return NULL;
5746 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005747 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005748 return NULL;
5749
Bram Moolenaar04b12692020-05-04 23:24:44 +02005750 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005751 fill_exarg_from_cctx(eap, cctx);
5752
Bram Moolenaar04b12692020-05-04 23:24:44 +02005753 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005754 lambda_name = vim_strsave(get_lambda_name());
5755 if (lambda_name == NULL)
5756 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005757 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005758
Bram Moolenaar822ba242020-05-24 23:00:18 +02005759 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005760 {
5761 r = eap->skip ? OK : FAIL;
5762 goto theend;
5763 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005764
5765 // copy over the block scope IDs before compiling
5766 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5767 {
5768 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5769
5770 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5771 if (ufunc->uf_block_ids != NULL)
5772 {
5773 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5774 sizeof(int) * block_depth);
5775 ufunc->uf_block_depth = block_depth;
5776 }
5777 }
5778
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005779 compile_type = COMPILE_TYPE(ufunc);
5780#ifdef FEAT_PROFILE
5781 // If the outer function is profiled, also compile the nested function for
5782 // profiling.
5783 if (cctx->ctx_compile_type == CT_PROFILE)
5784 compile_type = CT_PROFILE;
5785#endif
5786 if (func_needs_compiling(ufunc, compile_type)
5787 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005788 {
5789 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005790 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005791 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005792
Bram Moolenaar648594e2021-07-11 17:55:01 +02005793#ifdef FEAT_PROFILE
5794 // When the outer function is compiled for profiling, the nested function
5795 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005796 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005797 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5798#endif
5799
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005800 if (is_global)
5801 {
5802 char_u *func_name = vim_strnsave(name_start + 2,
5803 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005804
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005805 if (func_name == NULL)
5806 r = FAIL;
5807 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005808 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005809 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005810 lambda_name = NULL;
5811 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005812 }
5813 else
5814 {
5815 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005816 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005817 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005818
Bram Moolenaareef21022020-08-01 22:16:43 +02005819 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005820 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005821 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005822 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005823 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5824 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005825
5826theend:
5827 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005828 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005829}
5830
5831/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832 * Return the length of an assignment operator, or zero if there isn't one.
5833 */
5834 int
5835assignment_len(char_u *p, int *heredoc)
5836{
5837 if (*p == '=')
5838 {
5839 if (p[1] == '<' && p[2] == '<')
5840 {
5841 *heredoc = TRUE;
5842 return 3;
5843 }
5844 return 1;
5845 }
5846 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5847 return 2;
5848 if (STRNCMP(p, "..=", 3) == 0)
5849 return 3;
5850 return 0;
5851}
5852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005854 * Generate the load instruction for "name".
5855 */
5856 static void
5857generate_loadvar(
5858 cctx_T *cctx,
5859 assign_dest_T dest,
5860 char_u *name,
5861 lvar_T *lvar,
5862 type_T *type)
5863{
5864 switch (dest)
5865 {
5866 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005867 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5868 break;
5869 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005870 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5871 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5872 else
5873 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005874 break;
5875 case dest_buffer:
5876 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5877 break;
5878 case dest_window:
5879 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5880 break;
5881 case dest_tab:
5882 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5883 break;
5884 case dest_script:
5885 compile_load_scriptvar(cctx,
5886 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5887 break;
5888 case dest_env:
5889 // Include $ in the name here
5890 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5891 break;
5892 case dest_reg:
5893 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5894 break;
5895 case dest_vimvar:
5896 generate_LOADV(cctx, name + 2, TRUE);
5897 break;
5898 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005899 if (lvar->lv_from_outer > 0)
5900 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5901 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005902 else
5903 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5904 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005905 case dest_expr:
5906 // list or dict value should already be on the stack.
5907 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005908 }
5909}
5910
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005911/*
5912 * Skip over "[expr]" or ".member".
5913 * Does not check for any errors.
5914 */
5915 static char_u *
5916skip_index(char_u *start)
5917{
5918 char_u *p = start;
5919
5920 if (*p == '[')
5921 {
5922 p = skipwhite(p + 1);
5923 (void)skip_expr(&p, NULL);
5924 p = skipwhite(p);
5925 if (*p == ']')
5926 return p + 1;
5927 return p;
5928 }
5929 // if (*p == '.')
5930 return to_name_end(p + 1, TRUE);
5931}
5932
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005933 void
5934vim9_declare_error(char_u *name)
5935{
5936 char *scope = "";
5937
5938 switch (*name)
5939 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005940 case 'g': scope = _("global"); break;
5941 case 'b': scope = _("buffer"); break;
5942 case 'w': scope = _("window"); break;
5943 case 't': scope = _("tab"); break;
5944 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005945 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005946 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005947 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005948 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005949 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005950 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005951 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005952 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005953 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005954}
5955
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005956/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005957 * For one assignment figure out the type of destination. Return it in "dest".
5958 * When not recognized "dest" is not set.
5959 * For an option "opt_flags" is set.
5960 * For a v:var "vimvaridx" is set.
5961 * "type" is set to the destination type if known, unchanted otherwise.
5962 * Return FAIL if an error message was given.
5963 */
5964 static int
5965get_var_dest(
5966 char_u *name,
5967 assign_dest_T *dest,
5968 int cmdidx,
5969 int *opt_flags,
5970 int *vimvaridx,
5971 type_T **type,
5972 cctx_T *cctx)
5973{
5974 char_u *p;
5975
5976 if (*name == '&')
5977 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005978 int cc;
5979 long numval;
5980 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005981
5982 *dest = dest_option;
5983 if (cmdidx == CMD_final || cmdidx == CMD_const)
5984 {
5985 emsg(_(e_const_option));
5986 return FAIL;
5987 }
5988 p = name;
5989 p = find_option_end(&p, opt_flags);
5990 if (p == NULL)
5991 {
5992 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005993 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005994 return FAIL;
5995 }
5996 cc = *p;
5997 *p = NUL;
5998 opt_type = get_option_value(skip_option_env_lead(name),
5999 &numval, NULL, *opt_flags);
6000 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006001 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006002 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006003 case gov_unknown:
6004 semsg(_(e_unknown_option), name);
6005 return FAIL;
6006 case gov_string:
6007 case gov_hidden_string:
6008 *type = &t_string;
6009 break;
6010 case gov_bool:
6011 case gov_hidden_bool:
6012 *type = &t_bool;
6013 break;
6014 case gov_number:
6015 case gov_hidden_number:
6016 *type = &t_number;
6017 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006018 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006019 }
6020 else if (*name == '$')
6021 {
6022 *dest = dest_env;
6023 *type = &t_string;
6024 }
6025 else if (*name == '@')
6026 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006027 if (name[1] != '@'
6028 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006029 {
6030 emsg_invreg(name[1]);
6031 return FAIL;
6032 }
6033 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006034 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006035 }
6036 else if (STRNCMP(name, "g:", 2) == 0)
6037 {
6038 *dest = dest_global;
6039 }
6040 else if (STRNCMP(name, "b:", 2) == 0)
6041 {
6042 *dest = dest_buffer;
6043 }
6044 else if (STRNCMP(name, "w:", 2) == 0)
6045 {
6046 *dest = dest_window;
6047 }
6048 else if (STRNCMP(name, "t:", 2) == 0)
6049 {
6050 *dest = dest_tab;
6051 }
6052 else if (STRNCMP(name, "v:", 2) == 0)
6053 {
6054 typval_T *vtv;
6055 int di_flags;
6056
6057 *vimvaridx = find_vim_var(name + 2, &di_flags);
6058 if (*vimvaridx < 0)
6059 {
6060 semsg(_(e_variable_not_found_str), name);
6061 return FAIL;
6062 }
6063 // We use the current value of "sandbox" here, is that OK?
6064 if (var_check_ro(di_flags, name, FALSE))
6065 return FAIL;
6066 *dest = dest_vimvar;
6067 vtv = get_vim_var_tv(*vimvaridx);
6068 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6069 }
6070 return OK;
6071}
6072
6073/*
6074 * Generate a STORE instruction for "dest", not being "dest_local".
6075 * Return FAIL when out of memory.
6076 */
6077 static int
6078generate_store_var(
6079 cctx_T *cctx,
6080 assign_dest_T dest,
6081 int opt_flags,
6082 int vimvaridx,
6083 int scriptvar_idx,
6084 int scriptvar_sid,
6085 type_T *type,
6086 char_u *name)
6087{
6088 switch (dest)
6089 {
6090 case dest_option:
6091 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6092 opt_flags);
6093 case dest_global:
6094 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006095 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6096 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006097 case dest_buffer:
6098 // include b: with the name, easier to execute that way
6099 return generate_STORE(cctx, ISN_STOREB, 0, name);
6100 case dest_window:
6101 // include w: with the name, easier to execute that way
6102 return generate_STORE(cctx, ISN_STOREW, 0, name);
6103 case dest_tab:
6104 // include t: with the name, easier to execute that way
6105 return generate_STORE(cctx, ISN_STORET, 0, name);
6106 case dest_env:
6107 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6108 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006109 return generate_STORE(cctx, ISN_STOREREG,
6110 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006111 case dest_vimvar:
6112 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6113 case dest_script:
6114 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006115 // "s:" may be included in the name.
6116 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006117 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006118 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6119 scriptvar_sid, scriptvar_idx, type);
6120 case dest_local:
6121 case dest_expr:
6122 // cannot happen
6123 break;
6124 }
6125 return FAIL;
6126}
6127
Bram Moolenaar752fc692021-01-04 21:57:11 +01006128 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006129generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6130{
6131 if (lhs->lhs_dest != dest_local)
6132 return generate_store_var(cctx, lhs->lhs_dest,
6133 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6134 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6135 lhs->lhs_type, lhs->lhs_name);
6136
6137 if (lhs->lhs_lvar != NULL)
6138 {
6139 garray_T *instr = &cctx->ctx_instr;
6140 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6141
6142 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6143 // ISN_STORENR
6144 if (lhs->lhs_lvar->lv_from_outer == 0
6145 && instr->ga_len == instr_count + 1
6146 && isn->isn_type == ISN_PUSHNR)
6147 {
6148 varnumber_T val = isn->isn_arg.number;
6149 garray_T *stack = &cctx->ctx_type_stack;
6150
6151 isn->isn_type = ISN_STORENR;
6152 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6153 isn->isn_arg.storenr.stnr_val = val;
6154 if (stack->ga_len > 0)
6155 --stack->ga_len;
6156 }
6157 else if (lhs->lhs_lvar->lv_from_outer > 0)
6158 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6159 lhs->lhs_lvar->lv_from_outer);
6160 else
6161 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6162 }
6163 return OK;
6164}
6165
6166 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167is_decl_command(int cmdidx)
6168{
6169 return cmdidx == CMD_let || cmdidx == CMD_var
6170 || cmdidx == CMD_final || cmdidx == CMD_const;
6171}
6172
6173/*
6174 * Figure out the LHS type and other properties for an assignment or one item
6175 * of ":unlet" with an index.
6176 * Returns OK or FAIL.
6177 */
6178 static int
6179compile_lhs(
6180 char_u *var_start,
6181 lhs_T *lhs,
6182 int cmdidx,
6183 int heredoc,
6184 int oplen,
6185 cctx_T *cctx)
6186{
6187 char_u *var_end;
6188 int is_decl = is_decl_command(cmdidx);
6189
6190 CLEAR_POINTER(lhs);
6191 lhs->lhs_dest = dest_local;
6192 lhs->lhs_vimvaridx = -1;
6193 lhs->lhs_scriptvar_idx = -1;
6194
6195 // "dest_end" is the end of the destination, including "[expr]" or
6196 // ".name".
6197 // "var_end" is the end of the variable/option/etc. name.
6198 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6199 if (*var_start == '@')
6200 var_end = var_start + 2;
6201 else
6202 {
6203 // skip over the leading "&", "&l:", "&g:" and "$"
6204 var_end = skip_option_env_lead(var_start);
6205 var_end = to_name_end(var_end, TRUE);
6206 }
6207
6208 // "a: type" is declaring variable "a" with a type, not dict "a:".
6209 if (is_decl && lhs->lhs_dest_end == var_start + 2
6210 && lhs->lhs_dest_end[-1] == ':')
6211 --lhs->lhs_dest_end;
6212 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6213 --var_end;
6214
6215 // compute the length of the destination without "[expr]" or ".name"
6216 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006217 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6219 if (lhs->lhs_name == NULL)
6220 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006221
6222 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6223 // Something follows after the variable: "var[idx]" or "var.key".
6224 lhs->lhs_has_index = TRUE;
6225
Bram Moolenaar752fc692021-01-04 21:57:11 +01006226 if (heredoc)
6227 lhs->lhs_type = &t_list_string;
6228 else
6229 lhs->lhs_type = &t_any;
6230
6231 if (cctx->ctx_skip != SKIP_YES)
6232 {
6233 int declare_error = FALSE;
6234
6235 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6236 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6237 &lhs->lhs_type, cctx) == FAIL)
6238 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006239 if (lhs->lhs_dest != dest_local
6240 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006241 {
6242 // Specific kind of variable recognized.
6243 declare_error = is_decl;
6244 }
6245 else
6246 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006248 if (check_reserved_name(lhs->lhs_name) == FAIL)
6249 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250
6251 if (lookup_local(var_start, lhs->lhs_varlen,
6252 &lhs->lhs_local_lvar, cctx) == OK)
6253 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6254 else
6255 {
6256 CLEAR_FIELD(lhs->lhs_arg_lvar);
6257 if (arg_exists(var_start, lhs->lhs_varlen,
6258 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6259 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6260 {
6261 if (is_decl)
6262 {
6263 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6264 return FAIL;
6265 }
6266 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6267 }
6268 }
6269 if (lhs->lhs_lvar != NULL)
6270 {
6271 if (is_decl)
6272 {
6273 semsg(_(e_variable_already_declared), lhs->lhs_name);
6274 return FAIL;
6275 }
6276 }
6277 else
6278 {
6279 int script_namespace = lhs->lhs_varlen > 1
6280 && STRNCMP(var_start, "s:", 2) == 0;
6281 int script_var = (script_namespace
6282 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006283 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006285 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006286 imported_T *import =
6287 find_imported(var_start, lhs->lhs_varlen, cctx);
6288
6289 if (script_namespace || script_var || import != NULL)
6290 {
6291 char_u *rawname = lhs->lhs_name
6292 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6293
6294 if (is_decl)
6295 {
6296 if (script_namespace)
6297 semsg(_(e_cannot_declare_script_variable_in_function),
6298 lhs->lhs_name);
6299 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006300 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 lhs->lhs_name);
6302 return FAIL;
6303 }
6304 else if (cctx->ctx_ufunc->uf_script_ctx_version
6305 == SCRIPT_VERSION_VIM9
6306 && script_namespace
6307 && !script_var && import == NULL)
6308 {
6309 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6310 return FAIL;
6311 }
6312
6313 lhs->lhs_dest = dest_script;
6314
6315 // existing script-local variables should have a type
6316 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6317 if (import != NULL)
6318 lhs->lhs_scriptvar_sid = import->imp_sid;
6319 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6320 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006321 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006322 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006323 lhs->lhs_scriptvar_sid, rawname,
6324 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6325 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006326 if (lhs->lhs_scriptvar_idx >= 0)
6327 {
6328 scriptitem_T *si = SCRIPT_ITEM(
6329 lhs->lhs_scriptvar_sid);
6330 svar_T *sv =
6331 ((svar_T *)si->sn_var_vals.ga_data)
6332 + lhs->lhs_scriptvar_idx;
6333 lhs->lhs_type = sv->sv_type;
6334 }
6335 }
6336 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006337 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 == FAIL)
6339 return FAIL;
6340 }
6341 }
6342
6343 if (declare_error)
6344 {
6345 vim9_declare_error(lhs->lhs_name);
6346 return FAIL;
6347 }
6348 }
6349
6350 // handle "a:name" as a name, not index "name" on "a"
6351 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6352 var_end = lhs->lhs_dest_end;
6353
6354 if (lhs->lhs_dest != dest_option)
6355 {
6356 if (is_decl && *var_end == ':')
6357 {
6358 char_u *p;
6359
6360 // parse optional type: "let var: type = expr"
6361 if (!VIM_ISWHITE(var_end[1]))
6362 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006363 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 return FAIL;
6365 }
6366 p = skipwhite(var_end + 1);
6367 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6368 if (lhs->lhs_type == NULL)
6369 return FAIL;
6370 lhs->lhs_has_type = TRUE;
6371 }
6372 else if (lhs->lhs_lvar != NULL)
6373 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6374 }
6375
Bram Moolenaare42939a2021-04-05 17:11:17 +02006376 if (oplen == 3 && !heredoc
6377 && lhs->lhs_dest != dest_global
6378 && !lhs->lhs_has_index
6379 && lhs->lhs_type->tt_type != VAR_STRING
6380 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 {
6382 emsg(_(e_can_only_concatenate_to_string));
6383 return FAIL;
6384 }
6385
6386 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6387 && cctx->ctx_skip != SKIP_YES)
6388 {
6389 if (oplen > 1 && !heredoc)
6390 {
6391 // +=, /=, etc. require an existing variable
6392 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6393 return FAIL;
6394 }
6395 if (!is_decl)
6396 {
6397 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6398 return FAIL;
6399 }
6400
Bram Moolenaar3f327882021-03-17 20:56:38 +01006401 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 if ((lhs->lhs_type->tt_type == VAR_FUNC
6403 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006404 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006405 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006406
6407 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006408 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6409 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6410 if (lhs->lhs_lvar == NULL)
6411 return FAIL;
6412 lhs->lhs_new_local = TRUE;
6413 }
6414
6415 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006416 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006417 {
6418 // Something follows after the variable: "var[idx]" or "var.key".
6419 // TODO: should we also handle "->func()" here?
6420 if (is_decl)
6421 {
6422 emsg(_(e_cannot_use_index_when_declaring_variable));
6423 return FAIL;
6424 }
6425
6426 if (var_start[lhs->lhs_varlen] == '['
6427 || var_start[lhs->lhs_varlen] == '.')
6428 {
6429 char_u *after = var_start + lhs->lhs_varlen;
6430 char_u *p;
6431
6432 // Only the last index is used below, if there are others
6433 // before it generate code for the expression. Thus for
6434 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6435 for (;;)
6436 {
6437 p = skip_index(after);
6438 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006439 {
6440 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006442 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 after = p;
6444 }
6445 if (after > var_start + lhs->lhs_varlen)
6446 {
6447 lhs->lhs_varlen = after - var_start;
6448 lhs->lhs_dest = dest_expr;
6449 // We don't know the type before evaluating the expression,
6450 // use "any" until then.
6451 lhs->lhs_type = &t_any;
6452 }
6453
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 if (lhs->lhs_type->tt_member == NULL)
6455 lhs->lhs_member_type = &t_any;
6456 else
6457 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6458 }
6459 else
6460 {
6461 semsg("Not supported yet: %s", var_start);
6462 return FAIL;
6463 }
6464 }
6465 return OK;
6466}
6467
6468/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006469 * Figure out the LHS and check a few errors.
6470 */
6471 static int
6472compile_assign_lhs(
6473 char_u *var_start,
6474 lhs_T *lhs,
6475 int cmdidx,
6476 int is_decl,
6477 int heredoc,
6478 int oplen,
6479 cctx_T *cctx)
6480{
6481 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6482 return FAIL;
6483
6484 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6485 {
6486 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6487 return FAIL;
6488 }
6489 if (!is_decl && lhs->lhs_lvar != NULL
6490 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6491 {
6492 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6493 return FAIL;
6494 }
6495 return OK;
6496}
6497
6498/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006499 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6500 */
6501 static int
6502has_list_index(char_u *idx_start, cctx_T *cctx)
6503{
6504 char_u *p = idx_start;
6505 int save_skip;
6506
6507 if (*p != '[')
6508 return FALSE;
6509
6510 p = skipwhite(p + 1);
6511 if (*p == ':')
6512 return TRUE;
6513
6514 save_skip = cctx->ctx_skip;
6515 cctx->ctx_skip = SKIP_YES;
6516 (void)compile_expr0(&p, cctx);
6517 cctx->ctx_skip = save_skip;
6518 return *skipwhite(p) == ':';
6519}
6520
6521/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006522 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6523 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 */
6525 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006526compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 char_u *var_start,
6528 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006529 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 cctx_T *cctx)
6531{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006532 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006533 char_u *p;
6534 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006535 int need_white_before = TRUE;
6536 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537
Bram Moolenaar752fc692021-01-04 21:57:11 +01006538 p = var_start + varlen;
6539 if (*p == '[')
6540 {
6541 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006542 if (*p == ':')
6543 {
6544 // empty first index, push zero
6545 r = generate_PUSHNR(cctx, 0);
6546 need_white_before = FALSE;
6547 }
6548 else
6549 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006550
6551 if (r == OK && *skipwhite(p) == ':')
6552 {
6553 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006554 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006555 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006556 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006557 empty_second = *skipwhite(p + 1) == ']';
6558 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6559 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006560 {
6561 semsg(_(e_white_space_required_before_and_after_str_at_str),
6562 ":", p);
6563 return FAIL;
6564 }
6565 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006566 if (*p == ']')
6567 // empty second index, push "none"
6568 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6569 else
6570 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006571 }
6572
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 if (r == OK && *skipwhite(p) != ']')
6574 {
6575 // this should not happen
6576 emsg(_(e_missbrac));
6577 r = FAIL;
6578 }
6579 }
6580 else // if (*p == '.')
6581 {
6582 char_u *key_end = to_name_end(p + 1, TRUE);
6583 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6584
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006585 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006586 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006587 return r;
6588}
6589
6590/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006591 * For a LHS with an index, load the variable to be indexed.
6592 */
6593 static int
6594compile_load_lhs(
6595 lhs_T *lhs,
6596 char_u *var_start,
6597 type_T *rhs_type,
6598 cctx_T *cctx)
6599{
6600 if (lhs->lhs_dest == dest_expr)
6601 {
6602 size_t varlen = lhs->lhs_varlen;
6603 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006604 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006605 char_u *p = var_start;
6606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006607 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006608
Bram Moolenaare97976b2021-08-01 13:17:17 +02006609 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6610 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006611 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006612 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6613 res = compile_expr0(&p, cctx);
6614 var_start[varlen] = c;
6615 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6616 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006617 {
6618 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006619 if (res != FAIL)
6620 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006621 return FAIL;
6622 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006623
6624 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6625 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6626 // now we can properly check the type
6627 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6628 && rhs_type != &t_void
6629 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6630 FALSE, FALSE) == FAIL)
6631 return FAIL;
6632 }
6633 else
6634 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6635 lhs->lhs_lvar, lhs->lhs_type);
6636 return OK;
6637}
6638
6639/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006640 * Produce code for loading "lhs" and also take care of an index.
6641 * Return OK/FAIL.
6642 */
6643 static int
6644compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6645{
6646 compile_load_lhs(lhs, var_start, NULL, cctx);
6647
6648 if (lhs->lhs_has_index)
6649 {
6650 int range = FALSE;
6651
6652 // Get member from list or dict. First compile the
6653 // index value.
6654 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6655 return FAIL;
6656 if (range)
6657 {
6658 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6659 var_start);
6660 return FAIL;
6661 }
6662
6663 // Get the member.
6664 if (compile_member(FALSE, cctx) == FAIL)
6665 return FAIL;
6666 }
6667 return OK;
6668}
6669
6670/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006671 * Assignment to a list or dict member, or ":unlet" for the item, using the
6672 * information in "lhs".
6673 * Returns OK or FAIL.
6674 */
6675 static int
6676compile_assign_unlet(
6677 char_u *var_start,
6678 lhs_T *lhs,
6679 int is_assign,
6680 type_T *rhs_type,
6681 cctx_T *cctx)
6682{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006683 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006684 garray_T *stack = &cctx->ctx_type_stack;
6685 int range = FALSE;
6686
Bram Moolenaar68452172021-04-12 21:21:02 +02006687 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006688 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006689 if (is_assign && range
6690 && lhs->lhs_type->tt_type != VAR_LIST
6691 && lhs->lhs_type != &t_blob
6692 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006693 {
6694 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6695 return FAIL;
6696 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006697
6698 if (lhs->lhs_type == &t_any)
6699 {
6700 // Index on variable of unknown type: check at runtime.
6701 dest_type = VAR_ANY;
6702 }
6703 else
6704 {
6705 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006706 if (dest_type == VAR_DICT && range)
6707 {
6708 emsg(e_cannot_use_range_with_dictionary);
6709 return FAIL;
6710 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006711 if (dest_type == VAR_DICT
6712 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006713 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006714 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006715 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006716 type_T *type;
6717
6718 if (range)
6719 {
6720 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6721 if (need_type(type, &t_number,
6722 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006723 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006724 }
6725 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006726 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006727 && need_type(type, &t_number,
6728 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006729 return FAIL;
6730 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006731 }
6732
6733 // Load the dict or list. On the stack we then have:
6734 // - value (for assignment, not for :unlet)
6735 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006736 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006737 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006738 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6739 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006740
Bram Moolenaar68452172021-04-12 21:21:02 +02006741 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6742 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006743 {
6744 if (is_assign)
6745 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006746 if (range)
6747 {
6748 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6749 return FAIL;
6750 }
6751 else
6752 {
6753 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006754
Bram Moolenaar68452172021-04-12 21:21:02 +02006755 if (isn == NULL)
6756 return FAIL;
6757 isn->isn_arg.vartype = dest_type;
6758 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006759 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006760 else if (range)
6761 {
6762 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6763 return FAIL;
6764 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006765 else
6766 {
6767 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6768 return FAIL;
6769 }
6770 }
6771 else
6772 {
6773 emsg(_(e_indexable_type_required));
6774 return FAIL;
6775 }
6776
6777 return OK;
6778}
6779
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006780/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006781 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006782 * "let name"
6783 * "var name = expr"
6784 * "final name = expr"
6785 * "const name = expr"
6786 * "name = expr"
6787 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006788 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006789 * Return NULL for an error.
6790 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 */
6792 static char_u *
6793compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6794{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006795 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006797 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 char_u *ret = NULL;
6799 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006800 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006802 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006804 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 int oplen = 0;
6807 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006808 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006809 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006811 int is_decl = is_decl_command(cmdidx);
6812 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006813 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006815 // Skip over the "var" or "[var, var]" to get to any "=".
6816 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6817 if (p == NULL)
6818 return *arg == '[' ? arg : NULL;
6819
6820 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006822 // TODO: should we allow this, and figure out type inference from list
6823 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006824 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 return NULL;
6826 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006827 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 sp = p;
6830 p = skipwhite(p);
6831 op = p;
6832 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833
6834 if (var_count > 0 && oplen == 0)
6835 // can be something like "[1, 2]->func()"
6836 return arg;
6837
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006838 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006840 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006841 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006842 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006843 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6844 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006845 if (VIM_ISWHITE(eap->cmd[2]))
6846 {
6847 semsg(_(e_no_white_space_allowed_after_str_str),
6848 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6849 return NULL;
6850 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006851 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6852 oplen = 2;
6853 incdec = TRUE;
6854 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 if (heredoc)
6857 {
6858 list_T *l;
6859 listitem_T *li;
6860
6861 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006862 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006864 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006865 if (l == NULL)
6866 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867
Bram Moolenaar078269b2020-09-21 20:35:55 +02006868 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006870 // Push each line and the create the list.
6871 FOR_ALL_LIST_ITEMS(l, li)
6872 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006873 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006874 li->li_tv.vval.v_string = NULL;
6875 }
6876 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 list_free(l);
6879 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006882 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006884 char_u *wp;
6885
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006886 // for "[var, var] = expr" evaluate the expression here, loop over the
6887 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006888 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006889
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006890 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006891 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006892 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006893 if (compile_expr0(&p, cctx) == FAIL)
6894 return NULL;
6895 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006897 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006899 type_T *stacktype;
6900
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006901 stacktype = stack->ga_len == 0 ? &t_void
6902 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006903 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006905 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006906 goto theend;
6907 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006908 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006909 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006910 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006911 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006912 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6913 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006914 if (stacktype->tt_member != NULL)
6915 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006916 }
6917 }
6918
6919 /*
6920 * Loop over variables in "[var, var] = expr".
6921 * For "var = expr" and "let var: type" this is done only once.
6922 */
6923 if (var_count > 0)
6924 var_start = skipwhite(arg + 1); // skip over the "["
6925 else
6926 var_start = arg;
6927 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6928 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006929 int instr_count = -1;
6930 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006931
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006932 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6933 {
6934 // Ignore underscore in "[a, _, b] = list".
6935 if (var_count > 0)
6936 {
6937 var_start = skipwhite(var_start + 2);
6938 continue;
6939 }
6940 emsg(_(e_cannot_use_underscore_here));
6941 goto theend;
6942 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 vim_free(lhs.lhs_name);
6944
6945 /*
6946 * Figure out the LHS type and other properties.
6947 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006948 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6949 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006950 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006951 if (heredoc)
6952 {
6953 SOURCING_LNUM = start_lnum;
6954 if (lhs.lhs_has_type
6955 && need_type(&t_list_string, lhs.lhs_type,
6956 -1, 0, cctx, FALSE, FALSE) == FAIL)
6957 goto theend;
6958 }
6959 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006960 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006961 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006963 if (oplen > 0 && var_count == 0)
6964 {
6965 // skip over the "=" and the expression
6966 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006967 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006968 }
6969 }
6970 else if (oplen > 0)
6971 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006972 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006973 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006974
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006975 // for "+=", "*=", "..=" etc. first load the current value
6976 if (*op != '='
6977 && compile_load_lhs_with_index(&lhs, var_start,
6978 cctx) == FAIL)
6979 goto theend;
6980
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006981 // For "var = expr" evaluate the expression.
6982 if (var_count == 0)
6983 {
6984 int r;
6985
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006986 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006987 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006988 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006989 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006990 r = generate_PUSHNR(cctx, 1);
6991 }
6992 else
6993 {
6994 // Temporarily hide the new local variable here, it is
6995 // not available to this expression.
6996 if (lhs.lhs_new_local)
6997 --cctx->ctx_locals.ga_len;
6998 wp = op + oplen;
6999 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7000 {
7001 if (lhs.lhs_new_local)
7002 ++cctx->ctx_locals.ga_len;
7003 goto theend;
7004 }
7005 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007006 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007007 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007008 if (r == FAIL)
7009 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007010 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007011 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007012 else if (semicolon && var_idx == var_count - 1)
7013 {
7014 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007015 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007016 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7017 goto theend;
7018 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007019 else
7020 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007021 // For "[var, var] = expr" get the "var_idx" item from the
7022 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007023 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007024 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007026
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007027 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007028 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007029 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007030 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007031 if ((rhs_type->tt_type == VAR_FUNC
7032 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007033 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007034 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007035 goto theend;
7036
Bram Moolenaar752fc692021-01-04 21:57:11 +01007037 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007038 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007039 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007040 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007041 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007042 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007043 }
7044 else
7045 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007046 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007047 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007048 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007049 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007050 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007051 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007052 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007053 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007054 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007055 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007056 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007057 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007058 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007059 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007060 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007061 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007062
Bram Moolenaar77709b12021-04-03 21:01:01 +02007063 // Without operator check type here, otherwise below.
7064 // Use the line number of the assignment.
7065 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007066 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7067 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007068 // If assigning to a list or dict member, use the
7069 // member type. Not for "list[:] =".
7070 if (lhs.lhs_has_index
7071 && !has_list_index(var_start + lhs.lhs_varlen,
7072 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007073 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007074 if (need_type_where(rhs_type, use_type, -1, where,
7075 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007076 goto theend;
7077 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007078 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007079 else
7080 {
7081 type_T *lhs_type = lhs.lhs_member_type;
7082
7083 // Special case: assigning to @# can use a number or a
7084 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007085 // Also: can assign a number to a float.
7086 if ((lhs_type == &t_number_or_string
7087 || lhs_type == &t_float)
7088 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007089 lhs_type = &t_number;
7090 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007091 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007092 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007095 else if (cmdidx == CMD_final)
7096 {
7097 emsg(_(e_final_requires_a_value));
7098 goto theend;
7099 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007100 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007102 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007103 goto theend;
7104 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007105 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007106 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007107 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007108 goto theend;
7109 }
7110 else
7111 {
7112 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007113 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007114 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007115 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007116 {
7117 case VAR_BOOL:
7118 generate_PUSHBOOL(cctx, VVAL_FALSE);
7119 break;
7120 case VAR_FLOAT:
7121#ifdef FEAT_FLOAT
7122 generate_PUSHF(cctx, 0.0);
7123#endif
7124 break;
7125 case VAR_STRING:
7126 generate_PUSHS(cctx, NULL);
7127 break;
7128 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007129 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007130 break;
7131 case VAR_FUNC:
7132 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7133 break;
7134 case VAR_LIST:
7135 generate_NEWLIST(cctx, 0);
7136 break;
7137 case VAR_DICT:
7138 generate_NEWDICT(cctx, 0);
7139 break;
7140 case VAR_JOB:
7141 generate_PUSHJOB(cctx, NULL);
7142 break;
7143 case VAR_CHANNEL:
7144 generate_PUSHCHANNEL(cctx, NULL);
7145 break;
7146 case VAR_NUMBER:
7147 case VAR_UNKNOWN:
7148 case VAR_ANY:
7149 case VAR_PARTIAL:
7150 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007151 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007152 case VAR_SPECIAL: // cannot happen
7153 generate_PUSHNR(cctx, 0);
7154 break;
7155 }
7156 }
7157 if (var_count == 0)
7158 end = p;
7159 }
7160
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007161 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007162 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007163 break;
7164
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007165 if (oplen > 0 && *op != '=')
7166 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007167 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007168 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007169
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007170 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007171 {
7172 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7173 goto theend;
7174 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007175 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007176 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007177 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007178 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7179 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007180#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007181 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007182 !(expected == &t_float && (stacktype == &t_number
7183 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007184#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007185 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007186 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007187 goto theend;
7188 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007189
7190 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007191 {
7192 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7193 goto theend;
7194 }
7195 else if (*op == '+')
7196 {
7197 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007198 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007199 lhs.lhs_member_type, stacktype,
7200 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007201 goto theend;
7202 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007203 else if (generate_two_op(cctx, op) == FAIL)
7204 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007207 // Use the line number of the assignment for store instruction.
7208 save_lnum = cctx->ctx_lnum;
7209 cctx->ctx_lnum = start_lnum - 1;
7210
Bram Moolenaar752fc692021-01-04 21:57:11 +01007211 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007212 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007213 // Use the info in "lhs" to store the value at the index in the
7214 // list or dict.
7215 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7216 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007217 {
7218 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007219 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007220 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007221 }
7222 else
7223 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007224 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007225 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007226 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007227 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007228 generate_LOCKCONST(cctx);
7229
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007230 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007231 && (lhs.lhs_type->tt_type == VAR_DICT
7232 || lhs.lhs_type->tt_type == VAR_LIST)
7233 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007234 && !(lhs.lhs_type->tt_member == &t_any
7235 && oplen > 0
7236 && rhs_type != NULL
7237 && rhs_type->tt_type == lhs.lhs_type->tt_type
7238 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007239 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007240 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007241 // also in legacy script. Not for "list<any> = val", then the
7242 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007243 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007244
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007245 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007246 {
7247 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007248 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007249 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007250 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007251 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007252
7253 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007254 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007256
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007257 // For "[var, var] = expr" drop the "expr" value.
7258 // Also for "[var, var; _] = expr".
7259 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007260 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007261 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007262 goto theend;
7263 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007264
Bram Moolenaarb2097502020-07-19 17:17:02 +02007265 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266
7267theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007268 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 return ret;
7270}
7271
7272/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007273 * Check for an assignment at "eap->cmd", compile it if found.
7274 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7275 */
7276 static int
7277may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7278{
7279 char_u *pskip;
7280 char_u *p;
7281
7282 // Assuming the command starts with a variable or function name,
7283 // find what follows.
7284 // Skip over "var.member", "var[idx]" and the like.
7285 // Also "&opt = val", "$ENV = val" and "@r = val".
7286 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7287 ? eap->cmd + 1 : eap->cmd;
7288 p = to_name_end(pskip, TRUE);
7289 if (p > eap->cmd && *p != NUL)
7290 {
7291 char_u *var_end;
7292 int oplen;
7293 int heredoc;
7294
7295 if (eap->cmd[0] == '@')
7296 var_end = eap->cmd + 2;
7297 else
7298 var_end = find_name_end(pskip, NULL, NULL,
7299 FNE_CHECK_START | FNE_INCL_BR);
7300 oplen = assignment_len(skipwhite(var_end), &heredoc);
7301 if (oplen > 0)
7302 {
7303 size_t len = p - eap->cmd;
7304
7305 // Recognize an assignment if we recognize the variable
7306 // name:
7307 // "g:var = expr"
7308 // "local = expr" where "local" is a local var.
7309 // "script = expr" where "script" is a script-local var.
7310 // "import = expr" where "import" is an imported var
7311 // "&opt = expr"
7312 // "$ENV = expr"
7313 // "@r = expr"
7314 if (*eap->cmd == '&'
7315 || *eap->cmd == '$'
7316 || *eap->cmd == '@'
7317 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007318 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007319 {
7320 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7321 if (*line == NULL || *line == eap->cmd)
7322 return FAIL;
7323 return OK;
7324 }
7325 }
7326 }
7327
7328 if (*eap->cmd == '[')
7329 {
7330 // [var, var] = expr
7331 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7332 if (*line == NULL)
7333 return FAIL;
7334 if (*line != eap->cmd)
7335 return OK;
7336 }
7337 return NOTDONE;
7338}
7339
7340/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007341 * Check if "name" can be "unlet".
7342 */
7343 int
7344check_vim9_unlet(char_u *name)
7345{
7346 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7347 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007348 // "unlet s:var" is allowed in legacy script.
7349 if (*name == 's' && !script_is_vim9())
7350 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007351 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007352 return FAIL;
7353 }
7354 return OK;
7355}
7356
7357/*
7358 * Callback passed to ex_unletlock().
7359 */
7360 static int
7361compile_unlet(
7362 lval_T *lvp,
7363 char_u *name_end,
7364 exarg_T *eap,
7365 int deep UNUSED,
7366 void *coookie)
7367{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007368 cctx_T *cctx = coookie;
7369 char_u *p = lvp->ll_name;
7370 int cc = *name_end;
7371 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007372
Bram Moolenaar752fc692021-01-04 21:57:11 +01007373 if (cctx->ctx_skip == SKIP_YES)
7374 return OK;
7375
7376 *name_end = NUL;
7377 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007378 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007379 // :unlet $ENV_VAR
7380 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7381 }
7382 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7383 {
7384 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007385
Bram Moolenaar752fc692021-01-04 21:57:11 +01007386 // This is similar to assigning: lookup the list/dict, compile the
7387 // idx/key. Then instead of storing the value unlet the item.
7388 // unlet {list}[idx]
7389 // unlet {dict}[key] dict.key
7390 //
7391 // Figure out the LHS type and other properties.
7392 //
7393 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7394
7395 // : unlet an indexed item
7396 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007397 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007398 iemsg("called compile_lhs() without an index");
7399 ret = FAIL;
7400 }
7401 else
7402 {
7403 // Use the info in "lhs" to unlet the item at the index in the
7404 // list or dict.
7405 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007406 }
7407
Bram Moolenaar752fc692021-01-04 21:57:11 +01007408 vim_free(lhs.lhs_name);
7409 }
7410 else if (check_vim9_unlet(p) == FAIL)
7411 {
7412 ret = FAIL;
7413 }
7414 else
7415 {
7416 // Normal name. Only supports g:, w:, t: and b: namespaces.
7417 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007418 }
7419
Bram Moolenaar752fc692021-01-04 21:57:11 +01007420 *name_end = cc;
7421 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007422}
7423
7424/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007425 * Callback passed to ex_unletlock().
7426 */
7427 static int
7428compile_lock_unlock(
7429 lval_T *lvp,
7430 char_u *name_end,
7431 exarg_T *eap,
7432 int deep UNUSED,
7433 void *coookie)
7434{
7435 cctx_T *cctx = coookie;
7436 int cc = *name_end;
7437 char_u *p = lvp->ll_name;
7438 int ret = OK;
7439 size_t len;
7440 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007441 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007442
7443 if (cctx->ctx_skip == SKIP_YES)
7444 return OK;
7445
7446 // Cannot use :lockvar and :unlockvar on local variables.
7447 if (p[1] != ':')
7448 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007449 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007450
7451 if (lookup_local(p, end - p, NULL, cctx) == OK)
7452 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007453 char_u *s = p;
7454
7455 if (*end != '.' && *end != '[')
7456 {
7457 emsg(_(e_cannot_lock_unlock_local_variable));
7458 return FAIL;
7459 }
7460
7461 // For "d.member" put the local variable on the stack, it will be
7462 // passed to ex_lockvar() indirectly.
7463 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7464 return FAIL;
7465 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007466 }
7467 }
7468
7469 // Checking is done at runtime.
7470 *name_end = NUL;
7471 len = name_end - p + 20;
7472 buf = alloc(len);
7473 if (buf == NULL)
7474 ret = FAIL;
7475 else
7476 {
7477 vim_snprintf((char *)buf, len, "%s %s",
7478 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7479 p);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007480 ret = generate_EXEC(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007481
7482 vim_free(buf);
7483 *name_end = cc;
7484 }
7485 return ret;
7486}
7487
7488/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007489 * compile "unlet var", "lock var" and "unlock var"
7490 * "arg" points to "var".
7491 */
7492 static char_u *
7493compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7494{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007495 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7496 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7497 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007498 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7499}
7500
7501/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7503 */
7504 static int
7505compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7506{
7507 garray_T *instr = &cctx->ctx_instr;
7508 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7509
7510 if (endlabel == NULL)
7511 return FAIL;
7512 endlabel->el_next = *el;
7513 *el = endlabel;
7514 endlabel->el_end_label = instr->ga_len;
7515
7516 generate_JUMP(cctx, when, 0);
7517 return OK;
7518}
7519
7520 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007521compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522{
7523 garray_T *instr = &cctx->ctx_instr;
7524
7525 while (*el != NULL)
7526 {
7527 endlabel_T *cur = (*el);
7528 isn_T *isn;
7529
7530 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007531 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 *el = cur->el_next;
7533 vim_free(cur);
7534 }
7535}
7536
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007537 static void
7538compile_free_jump_to_end(endlabel_T **el)
7539{
7540 while (*el != NULL)
7541 {
7542 endlabel_T *cur = (*el);
7543
7544 *el = cur->el_next;
7545 vim_free(cur);
7546 }
7547}
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549/*
7550 * Create a new scope and set up the generic items.
7551 */
7552 static scope_T *
7553new_scope(cctx_T *cctx, scopetype_T type)
7554{
7555 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7556
7557 if (scope == NULL)
7558 return NULL;
7559 scope->se_outer = cctx->ctx_scope;
7560 cctx->ctx_scope = scope;
7561 scope->se_type = type;
7562 scope->se_local_count = cctx->ctx_locals.ga_len;
7563 return scope;
7564}
7565
7566/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007567 * Free the current scope and go back to the outer scope.
7568 */
7569 static void
7570drop_scope(cctx_T *cctx)
7571{
7572 scope_T *scope = cctx->ctx_scope;
7573
7574 if (scope == NULL)
7575 {
7576 iemsg("calling drop_scope() without a scope");
7577 return;
7578 }
7579 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007580 switch (scope->se_type)
7581 {
7582 case IF_SCOPE:
7583 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7584 case FOR_SCOPE:
7585 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7586 case WHILE_SCOPE:
7587 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7588 case TRY_SCOPE:
7589 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7590 case NO_SCOPE:
7591 case BLOCK_SCOPE:
7592 break;
7593 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007594 vim_free(scope);
7595}
7596
7597/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 * compile "if expr"
7599 *
7600 * "if expr" Produces instructions:
7601 * EVAL expr Push result of "expr"
7602 * JUMP_IF_FALSE end
7603 * ... body ...
7604 * end:
7605 *
7606 * "if expr | else" Produces instructions:
7607 * EVAL expr Push result of "expr"
7608 * JUMP_IF_FALSE else
7609 * ... body ...
7610 * JUMP_ALWAYS end
7611 * else:
7612 * ... body ...
7613 * end:
7614 *
7615 * "if expr1 | elseif expr2 | else" Produces instructions:
7616 * EVAL expr Push result of "expr"
7617 * JUMP_IF_FALSE elseif
7618 * ... body ...
7619 * JUMP_ALWAYS end
7620 * elseif:
7621 * EVAL expr Push result of "expr"
7622 * JUMP_IF_FALSE else
7623 * ... body ...
7624 * JUMP_ALWAYS end
7625 * else:
7626 * ... body ...
7627 * end:
7628 */
7629 static char_u *
7630compile_if(char_u *arg, cctx_T *cctx)
7631{
7632 char_u *p = arg;
7633 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007634 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007636 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007637 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638
Bram Moolenaara5565e42020-05-09 15:44:01 +02007639 CLEAR_FIELD(ppconst);
7640 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007641 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007642 clear_ppconst(&ppconst);
7643 return NULL;
7644 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007645 if (!ends_excmd2(arg, skipwhite(p)))
7646 {
7647 semsg(_(e_trailing_arg), p);
7648 return NULL;
7649 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007650 if (cctx->ctx_skip == SKIP_YES)
7651 clear_ppconst(&ppconst);
7652 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007653 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007654 int error = FALSE;
7655 int v;
7656
Bram Moolenaara5565e42020-05-09 15:44:01 +02007657 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007658 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007659 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007660 if (error)
7661 return NULL;
7662 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007663 }
7664 else
7665 {
7666 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007667 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007668 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007669 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007670 if (bool_on_stack(cctx) == FAIL)
7671 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673
Bram Moolenaara91a7132021-03-25 21:12:15 +01007674 // CMDMOD_REV must come before the jump
7675 generate_undo_cmdmods(cctx);
7676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 scope = new_scope(cctx, IF_SCOPE);
7678 if (scope == NULL)
7679 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007680 scope->se_skip_save = skip_save;
7681 // "is_had_return" will be reset if any block does not end in :return
7682 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007684 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007685 {
7686 // "where" is set when ":elseif", "else" or ":endif" is found
7687 scope->se_u.se_if.is_if_label = instr->ga_len;
7688 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7689 }
7690 else
7691 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692
Bram Moolenaarced68a02021-01-24 17:53:47 +01007693#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007694 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007695 && skip_save != SKIP_YES)
7696 {
7697 // generated a profile start, need to generate a profile end, since it
7698 // won't be done after returning
7699 cctx->ctx_skip = SKIP_NOT;
7700 generate_instr(cctx, ISN_PROF_END);
7701 cctx->ctx_skip = SKIP_YES;
7702 }
7703#endif
7704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 return p;
7706}
7707
7708 static char_u *
7709compile_elseif(char_u *arg, cctx_T *cctx)
7710{
7711 char_u *p = arg;
7712 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007713 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 isn_T *isn;
7715 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007716 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007717 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718
7719 if (scope == NULL || scope->se_type != IF_SCOPE)
7720 {
7721 emsg(_(e_elseif_without_if));
7722 return NULL;
7723 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007724 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007725 if (!cctx->ctx_had_return)
7726 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727
Bram Moolenaarced68a02021-01-24 17:53:47 +01007728 if (cctx->ctx_skip == SKIP_NOT)
7729 {
7730 // previous block was executed, this one and following will not
7731 cctx->ctx_skip = SKIP_YES;
7732 scope->se_u.se_if.is_seen_skip_not = TRUE;
7733 }
7734 if (scope->se_u.se_if.is_seen_skip_not)
7735 {
7736 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007737 // Do not count the "elseif" for profiling and cmdmod
7738 instr->ga_len = current_instr_idx(cctx);
7739
Bram Moolenaarced68a02021-01-24 17:53:47 +01007740 skip_expr_cctx(&p, cctx);
7741 return p;
7742 }
7743
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007744 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007745 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007746 int moved_cmdmod = FALSE;
7747 int saved_debug = FALSE;
7748 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007749
7750 // Move any CMDMOD instruction to after the jump
7751 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7752 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007753 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007754 return NULL;
7755 ((isn_T *)instr->ga_data)[instr->ga_len] =
7756 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7757 --instr->ga_len;
7758 moved_cmdmod = TRUE;
7759 }
7760
Bram Moolenaar093165c2021-08-22 13:35:31 +02007761 // Remove the already generated ISN_DEBUG, it is written below the
7762 // ISN_FOR instruction.
7763 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7764 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7765 .isn_type == ISN_DEBUG)
7766 {
7767 --instr->ga_len;
7768 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7769 saved_debug = TRUE;
7770 }
7771
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007772 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007774 return NULL;
7775 // previous "if" or "elseif" jumps here
7776 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7777 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007778
Bram Moolenaara91a7132021-03-25 21:12:15 +01007779 if (moved_cmdmod)
7780 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007781
7782 if (saved_debug)
7783 {
7784 // move the debug instruction here
7785 if (GA_GROW_FAILS(instr, 1))
7786 return NULL;
7787 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7788 ++instr->ga_len;
7789 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007792 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007793 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007794 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007795 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007796 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007797#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007798 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007799 {
7800 // the previous block was skipped, need to profile this line
7801 generate_instr(cctx, ISN_PROF_START);
7802 instr_count = instr->ga_len;
7803 }
7804#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007805 if (cctx->ctx_compile_type == CT_DEBUG)
7806 {
7807 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007808 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007809 instr_count = instr->ga_len;
7810 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007811 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007812 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007813 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007814 clear_ppconst(&ppconst);
7815 return NULL;
7816 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007817 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007818 if (!ends_excmd2(arg, skipwhite(p)))
7819 {
7820 semsg(_(e_trailing_arg), p);
7821 return NULL;
7822 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007823 if (scope->se_skip_save == SKIP_YES)
7824 clear_ppconst(&ppconst);
7825 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007826 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007827 int error = FALSE;
7828 int v;
7829
Bram Moolenaar7f141552020-05-09 17:35:53 +02007830 // The expression results in a constant.
7831 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007832 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7833 if (error)
7834 return NULL;
7835 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007836 clear_ppconst(&ppconst);
7837 scope->se_u.se_if.is_if_label = -1;
7838 }
7839 else
7840 {
7841 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007842 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007843 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007844 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007845 if (bool_on_stack(cctx) == FAIL)
7846 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847
Bram Moolenaara91a7132021-03-25 21:12:15 +01007848 // CMDMOD_REV must come before the jump
7849 generate_undo_cmdmods(cctx);
7850
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007851 // "where" is set when ":elseif", "else" or ":endif" is found
7852 scope->se_u.se_if.is_if_label = instr->ga_len;
7853 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855
7856 return p;
7857}
7858
7859 static char_u *
7860compile_else(char_u *arg, cctx_T *cctx)
7861{
7862 char_u *p = arg;
7863 garray_T *instr = &cctx->ctx_instr;
7864 isn_T *isn;
7865 scope_T *scope = cctx->ctx_scope;
7866
7867 if (scope == NULL || scope->se_type != IF_SCOPE)
7868 {
7869 emsg(_(e_else_without_if));
7870 return NULL;
7871 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007872 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007873 if (!cctx->ctx_had_return)
7874 scope->se_u.se_if.is_had_return = FALSE;
7875 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876
Bram Moolenaarced68a02021-01-24 17:53:47 +01007877#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007878 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007879 {
7880 if (cctx->ctx_skip == SKIP_NOT
7881 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7882 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007883 // the previous block was executed, do not count "else" for
7884 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007885 --instr->ga_len;
7886 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7887 {
7888 // the previous block was not executed, this one will, do count the
7889 // "else" for profiling
7890 cctx->ctx_skip = SKIP_NOT;
7891 generate_instr(cctx, ISN_PROF_END);
7892 generate_instr(cctx, ISN_PROF_START);
7893 cctx->ctx_skip = SKIP_YES;
7894 }
7895 }
7896#endif
7897
7898 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007899 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007900 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007901 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007902 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007903 if (!cctx->ctx_had_return
7904 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7905 JUMP_ALWAYS, cctx) == FAIL)
7906 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007907 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007908
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007909 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007910 {
7911 if (scope->se_u.se_if.is_if_label >= 0)
7912 {
7913 // previous "if" or "elseif" jumps here
7914 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7915 isn->isn_arg.jump.jump_where = instr->ga_len;
7916 scope->se_u.se_if.is_if_label = -1;
7917 }
7918 }
7919
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007920 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007921 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923
7924 return p;
7925}
7926
7927 static char_u *
7928compile_endif(char_u *arg, cctx_T *cctx)
7929{
7930 scope_T *scope = cctx->ctx_scope;
7931 ifscope_T *ifscope;
7932 garray_T *instr = &cctx->ctx_instr;
7933 isn_T *isn;
7934
Bram Moolenaarfa984412021-03-25 22:15:28 +01007935 if (misplaced_cmdmod(cctx))
7936 return NULL;
7937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 if (scope == NULL || scope->se_type != IF_SCOPE)
7939 {
7940 emsg(_(e_endif_without_if));
7941 return NULL;
7942 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007943 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007944 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007945 if (!cctx->ctx_had_return)
7946 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007948 if (scope->se_u.se_if.is_if_label >= 0)
7949 {
7950 // previous "if" or "elseif" jumps here
7951 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7952 isn->isn_arg.jump.jump_where = instr->ga_len;
7953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007955 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007956
7957#ifdef FEAT_PROFILE
7958 // even when skipping we count the endif as executed, unless the block it's
7959 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007960 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007961 && scope->se_skip_save != SKIP_YES)
7962 {
7963 cctx->ctx_skip = SKIP_NOT;
7964 generate_instr(cctx, ISN_PROF_START);
7965 }
7966#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007967 cctx->ctx_skip = scope->se_skip_save;
7968
7969 // If all the blocks end in :return and there is an :else then the
7970 // had_return flag is set.
7971 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007973 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974 return arg;
7975}
7976
7977/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007978 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007979 *
7980 * Produces instructions:
7981 * PUSHNR -1
7982 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007983 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7985 * - if beyond end, jump to "end"
7986 * - otherwise get item from list and push it
7987 * STORE var Store item in "var"
7988 * ... body ...
7989 * JUMP top Jump back to repeat
7990 * end: DROP Drop the result of "expr"
7991 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007992 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7993 * UNPACK 2 Split item in 2
7994 * STORE var1 Store item in "var1"
7995 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996 */
7997 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007998compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008000 char_u *arg;
8001 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008002 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008003 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008004 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008005 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008006 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008007 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008010 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008012 lvar_T *loop_lvar; // loop iteration variable
8013 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008015 type_T *item_type = &t_any;
8016 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008017 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018
Bram Moolenaar792f7862020-11-23 08:31:18 +01008019 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008020 if (p == NULL)
8021 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008022 if (var_count == 0)
8023 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008024 else
8025 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026
8027 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008028 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008029 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8030 return NULL;
8031 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008033 if (*p == ':' && wp != p)
8034 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8035 else
8036 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 return NULL;
8038 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008039 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008040 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8041 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008043 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8044 // instruction.
8045 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8046 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8047 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008048 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008049 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008050 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8051 .isn_arg.debug.dbg_break_lnum;
8052 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 scope = new_scope(cctx, FOR_SCOPE);
8055 if (scope == NULL)
8056 return NULL;
8057
Bram Moolenaar792f7862020-11-23 08:31:18 +01008058 // Reserve a variable to store the loop iteration counter and initialize it
8059 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008060 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8061 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008062 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008063 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008064 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008065 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008066 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008067 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068
8069 // compile "expr", it remains on the stack until "endfor"
8070 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008071 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008072 {
8073 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008075 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008076 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077
rbtnnbebf0692021-08-21 17:26:50 +02008078 if (cctx->ctx_skip != SKIP_YES)
8079 {
8080 // If we know the type of "var" and it is a not a supported type we can
8081 // give an error now.
8082 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8083 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008084 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008085 {
rbtnnbebf0692021-08-21 17:26:50 +02008086 semsg(_(e_for_loop_on_str_not_supported),
8087 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008088 drop_scope(cctx);
8089 return NULL;
8090 }
rbtnnbebf0692021-08-21 17:26:50 +02008091
8092 if (vartype->tt_type == VAR_STRING)
8093 item_type = &t_string;
8094 else if (vartype->tt_type == VAR_BLOB)
8095 item_type = &t_number;
8096 else if (vartype->tt_type == VAR_LIST
8097 && vartype->tt_member->tt_type != VAR_ANY)
8098 {
8099 if (!var_list)
8100 item_type = vartype->tt_member;
8101 else if (vartype->tt_member->tt_type == VAR_LIST
8102 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
8103 // TODO: should get the type for each lhs
8104 item_type = vartype->tt_member->tt_member;
8105 }
8106
8107 // CMDMOD_REV must come before the FOR instruction.
8108 generate_undo_cmdmods(cctx);
8109
8110 // "for_end" is set when ":endfor" is found
8111 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8112
8113 generate_FOR(cctx, loop_lvar->lv_idx);
8114
8115 arg = arg_start;
8116 if (var_list)
8117 {
8118 generate_UNPACK(cctx, var_count, semicolon);
8119 arg = skipwhite(arg + 1); // skip white after '['
8120
8121 // the list item is replaced by a number of items
8122 if (GA_GROW_FAILS(stack, var_count - 1))
8123 {
8124 drop_scope(cctx);
8125 return NULL;
8126 }
8127 --stack->ga_len;
8128 for (idx = 0; idx < var_count; ++idx)
8129 {
8130 ((type_T **)stack->ga_data)[stack->ga_len] =
8131 (semicolon && idx == 0) ? vartype : item_type;
8132 ++stack->ga_len;
8133 }
8134 }
8135
Bram Moolenaar792f7862020-11-23 08:31:18 +01008136 for (idx = 0; idx < var_count; ++idx)
8137 {
rbtnnbebf0692021-08-21 17:26:50 +02008138 assign_dest_T dest = dest_local;
8139 int opt_flags = 0;
8140 int vimvaridx = -1;
8141 type_T *type = &t_any;
8142 type_T *lhs_type = &t_any;
8143 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008144
rbtnnbebf0692021-08-21 17:26:50 +02008145 p = skip_var_one(arg, FALSE);
8146 varlen = p - arg;
8147 name = vim_strnsave(arg, varlen);
8148 if (name == NULL)
8149 goto failed;
8150 if (*p == ':')
8151 {
8152 p = skipwhite(p + 1);
8153 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8154 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008155
rbtnnbebf0692021-08-21 17:26:50 +02008156 // TODO: script var not supported?
8157 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008158 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008159 goto failed;
8160 if (dest != dest_local)
8161 {
8162 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008163 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008164 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008165 }
rbtnnbebf0692021-08-21 17:26:50 +02008166 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008167 {
rbtnnbebf0692021-08-21 17:26:50 +02008168 // Assigning to "_": drop the value.
8169 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8170 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008171 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008172 else
rbtnnbebf0692021-08-21 17:26:50 +02008173 {
8174 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8175 {
8176 semsg(_(e_variable_already_declared), arg);
8177 goto failed;
8178 }
8179
8180 if (STRNCMP(name, "s:", 2) == 0)
8181 {
8182 semsg(_(e_cannot_declare_script_variable_in_function), name);
8183 goto failed;
8184 }
8185
8186 // Reserve a variable to store "var".
8187 where.wt_index = var_list ? idx + 1 : 0;
8188 where.wt_variable = TRUE;
8189 if (lhs_type == &t_any)
8190 lhs_type = item_type;
8191 else if (item_type != &t_unknown
8192 && (item_type == &t_any
8193 ? need_type(item_type, lhs_type,
8194 -1, 0, cctx, FALSE, FALSE)
8195 : check_type(lhs_type, item_type, TRUE, where))
8196 == FAIL)
8197 goto failed;
8198 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8199 if (var_lvar == NULL)
8200 // out of memory or used as an argument
8201 goto failed;
8202
8203 if (semicolon && idx == var_count - 1)
8204 var_lvar->lv_type = vartype;
8205 else
8206 var_lvar->lv_type = item_type;
8207 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8208 }
8209
8210 if (*p == ',' || *p == ';')
8211 ++p;
8212 arg = skipwhite(p);
8213 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008214 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008215
rbtnnbebf0692021-08-21 17:26:50 +02008216 if (cctx->ctx_compile_type == CT_DEBUG)
8217 {
8218 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008219
rbtnnbebf0692021-08-21 17:26:50 +02008220 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8221 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8222 cctx->ctx_prev_lnum = prev_lnum;
8223 generate_instr_debug(cctx);
8224 cctx->ctx_prev_lnum = save_prev_lnum;
8225 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008226 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008227
Bram Moolenaar792f7862020-11-23 08:31:18 +01008228 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008229
8230failed:
8231 vim_free(name);
8232 drop_scope(cctx);
8233 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234}
8235
8236/*
8237 * compile "endfor"
8238 */
8239 static char_u *
8240compile_endfor(char_u *arg, cctx_T *cctx)
8241{
8242 garray_T *instr = &cctx->ctx_instr;
8243 scope_T *scope = cctx->ctx_scope;
8244 forscope_T *forscope;
8245 isn_T *isn;
8246
Bram Moolenaarfa984412021-03-25 22:15:28 +01008247 if (misplaced_cmdmod(cctx))
8248 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 if (scope == NULL || scope->se_type != FOR_SCOPE)
8251 {
8252 emsg(_(e_for));
8253 return NULL;
8254 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008255 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008257 if (cctx->ctx_skip != SKIP_YES)
8258 {
8259 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008260
rbtnnbebf0692021-08-21 17:26:50 +02008261 // At end of ":for" scope jump back to the FOR instruction.
8262 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263
rbtnnbebf0692021-08-21 17:26:50 +02008264 // Fill in the "end" label in the FOR statement so it can jump here.
8265 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8266 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267
rbtnnbebf0692021-08-21 17:26:50 +02008268 // Fill in the "end" label any BREAK statements
8269 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270
rbtnnbebf0692021-08-21 17:26:50 +02008271 // Below the ":for" scope drop the "expr" list from the stack.
8272 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8273 return NULL;
8274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275
8276 vim_free(scope);
8277
8278 return arg;
8279}
8280
8281/*
8282 * compile "while expr"
8283 *
8284 * Produces instructions:
8285 * top: EVAL expr Push result of "expr"
8286 * JUMP_IF_FALSE end jump if false
8287 * ... body ...
8288 * JUMP top Jump back to repeat
8289 * end:
8290 *
8291 */
8292 static char_u *
8293compile_while(char_u *arg, cctx_T *cctx)
8294{
8295 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008296 scope_T *scope;
8297
8298 scope = new_scope(cctx, WHILE_SCOPE);
8299 if (scope == NULL)
8300 return NULL;
8301
Bram Moolenaara91a7132021-03-25 21:12:15 +01008302 // "endwhile" jumps back here, one before when profiling or using cmdmods
8303 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304
8305 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008306 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008308
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008309 if (!ends_excmd2(arg, skipwhite(p)))
8310 {
8311 semsg(_(e_trailing_arg), p);
8312 return NULL;
8313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314
rbtnnd895b1d2021-08-20 20:54:25 +02008315 if (cctx->ctx_skip != SKIP_YES)
8316 {
8317 if (bool_on_stack(cctx) == FAIL)
8318 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008319
rbtnnd895b1d2021-08-20 20:54:25 +02008320 // CMDMOD_REV must come before the jump
8321 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008322
rbtnnd895b1d2021-08-20 20:54:25 +02008323 // "while_end" is set when ":endwhile" is found
8324 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008326 return FAIL;
8327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328
8329 return p;
8330}
8331
8332/*
8333 * compile "endwhile"
8334 */
8335 static char_u *
8336compile_endwhile(char_u *arg, cctx_T *cctx)
8337{
8338 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008339 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340
Bram Moolenaarfa984412021-03-25 22:15:28 +01008341 if (misplaced_cmdmod(cctx))
8342 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008343 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8344 {
8345 emsg(_(e_while));
8346 return NULL;
8347 }
8348 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008349 if (cctx->ctx_skip != SKIP_YES)
8350 {
8351 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352
Bram Moolenaarf002a412021-01-24 13:34:18 +01008353#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008354 // count the endwhile before jumping
8355 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008356#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008357
rbtnnd895b1d2021-08-20 20:54:25 +02008358 // At end of ":for" scope jump back to the FOR instruction.
8359 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008360
rbtnnd895b1d2021-08-20 20:54:25 +02008361 // Fill in the "end" label in the WHILE statement so it can jump here.
8362 // And in any jumps for ":break"
8363 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008364 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008365 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008366
8367 vim_free(scope);
8368
8369 return arg;
8370}
8371
8372/*
8373 * compile "continue"
8374 */
8375 static char_u *
8376compile_continue(char_u *arg, cctx_T *cctx)
8377{
8378 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008379 int try_scopes = 0;
8380 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381
8382 for (;;)
8383 {
8384 if (scope == NULL)
8385 {
8386 emsg(_(e_continue));
8387 return NULL;
8388 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008389 if (scope->se_type == FOR_SCOPE)
8390 {
8391 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008393 }
8394 if (scope->se_type == WHILE_SCOPE)
8395 {
8396 loop_label = scope->se_u.se_while.ws_top_label;
8397 break;
8398 }
8399 if (scope->se_type == TRY_SCOPE)
8400 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 scope = scope->se_outer;
8402 }
8403
Bram Moolenaarc150c092021-02-13 15:02:46 +01008404 if (try_scopes > 0)
8405 // Inside one or more try/catch blocks we first need to jump to the
8406 // "finally" or "endtry" to cleanup.
8407 generate_TRYCONT(cctx, try_scopes, loop_label);
8408 else
8409 // Jump back to the FOR or WHILE instruction.
8410 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412 return arg;
8413}
8414
8415/*
8416 * compile "break"
8417 */
8418 static char_u *
8419compile_break(char_u *arg, cctx_T *cctx)
8420{
8421 scope_T *scope = cctx->ctx_scope;
8422 endlabel_T **el;
8423
8424 for (;;)
8425 {
8426 if (scope == NULL)
8427 {
8428 emsg(_(e_break));
8429 return NULL;
8430 }
8431 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8432 break;
8433 scope = scope->se_outer;
8434 }
8435
8436 // Jump to the end of the FOR or WHILE loop.
8437 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008438 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008440 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8442 return FAIL;
8443
8444 return arg;
8445}
8446
8447/*
8448 * compile "{" start of block
8449 */
8450 static char_u *
8451compile_block(char_u *arg, cctx_T *cctx)
8452{
8453 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8454 return NULL;
8455 return skipwhite(arg + 1);
8456}
8457
8458/*
8459 * compile end of block: drop one scope
8460 */
8461 static void
8462compile_endblock(cctx_T *cctx)
8463{
8464 scope_T *scope = cctx->ctx_scope;
8465
8466 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008467 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008468 vim_free(scope);
8469}
8470
8471/*
8472 * compile "try"
8473 * Creates a new scope for the try-endtry, pointing to the first catch and
8474 * finally.
8475 * Creates another scope for the "try" block itself.
8476 * TRY instruction sets up exception handling at runtime.
8477 *
8478 * "try"
8479 * TRY -> catch1, -> finally push trystack entry
8480 * ... try block
8481 * "throw {exception}"
8482 * EVAL {exception}
8483 * THROW create exception
8484 * ... try block
8485 * " catch {expr}"
8486 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008487 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 * EVAL {expr}
8489 * MATCH
8490 * JUMP nomatch -> catch2
8491 * CATCH remove exception
8492 * ... catch block
8493 * " catch"
8494 * JUMP -> finally
8495 * catch2: CATCH remove exception
8496 * ... catch block
8497 * " finally"
8498 * finally:
8499 * ... finally block
8500 * " endtry"
8501 * ENDTRY pop trystack entry, may rethrow
8502 */
8503 static char_u *
8504compile_try(char_u *arg, cctx_T *cctx)
8505{
8506 garray_T *instr = &cctx->ctx_instr;
8507 scope_T *try_scope;
8508 scope_T *scope;
8509
Bram Moolenaarfa984412021-03-25 22:15:28 +01008510 if (misplaced_cmdmod(cctx))
8511 return NULL;
8512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008513 // scope that holds the jumps that go to catch/finally/endtry
8514 try_scope = new_scope(cctx, TRY_SCOPE);
8515 if (try_scope == NULL)
8516 return NULL;
8517
Bram Moolenaar69f70502021-01-01 16:10:46 +01008518 if (cctx->ctx_skip != SKIP_YES)
8519 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008520 isn_T *isn;
8521
8522 // "try_catch" is set when the first ":catch" is found or when no catch
8523 // is found and ":finally" is found.
8524 // "try_finally" is set when ":finally" is found
8525 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008526 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008527 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8528 return NULL;
8529 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8530 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008531 return NULL;
8532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533
8534 // scope for the try block itself
8535 scope = new_scope(cctx, BLOCK_SCOPE);
8536 if (scope == NULL)
8537 return NULL;
8538
8539 return arg;
8540}
8541
8542/*
8543 * compile "catch {expr}"
8544 */
8545 static char_u *
8546compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8547{
8548 scope_T *scope = cctx->ctx_scope;
8549 garray_T *instr = &cctx->ctx_instr;
8550 char_u *p;
8551 isn_T *isn;
8552
Bram Moolenaarfa984412021-03-25 22:15:28 +01008553 if (misplaced_cmdmod(cctx))
8554 return NULL;
8555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 // end block scope from :try or :catch
8557 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8558 compile_endblock(cctx);
8559 scope = cctx->ctx_scope;
8560
8561 // Error if not in a :try scope
8562 if (scope == NULL || scope->se_type != TRY_SCOPE)
8563 {
8564 emsg(_(e_catch));
8565 return NULL;
8566 }
8567
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008568 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008570 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008571 return NULL;
8572 }
8573
Bram Moolenaar69f70502021-01-01 16:10:46 +01008574 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008576#ifdef FEAT_PROFILE
8577 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008578 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008579 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008580 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008581 .isn_type == ISN_PROF_START)
8582 --instr->ga_len;
8583#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008584 // Jump from end of previous block to :finally or :endtry
8585 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8586 JUMP_ALWAYS, cctx) == FAIL)
8587 return NULL;
8588
8589 // End :try or :catch scope: set value in ISN_TRY instruction
8590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008591 if (isn->isn_arg.try.try_ref->try_catch == 0)
8592 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008593 if (scope->se_u.se_try.ts_catch_label != 0)
8594 {
8595 // Previous catch without match jumps here
8596 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8597 isn->isn_arg.jump.jump_where = instr->ga_len;
8598 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008599#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008600 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008601 {
8602 // a "throw" that jumps here needs to be counted
8603 generate_instr(cctx, ISN_PROF_END);
8604 // the "catch" is also counted
8605 generate_instr(cctx, ISN_PROF_START);
8606 }
8607#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008608 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008609 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610 }
8611
8612 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008613 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008615 scope->se_u.se_try.ts_caught_all = TRUE;
8616 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008617 }
8618 else
8619 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008620 char_u *end;
8621 char_u *pat;
8622 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008623 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008624 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008626 // Push v:exception, push {expr} and MATCH
8627 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8628
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008629 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008630 if (*end != *p)
8631 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008632 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008633 vim_free(tofree);
8634 return FAIL;
8635 }
8636 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008637 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008638 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008639 len = (int)(end - tofree);
8640 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008641 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008642 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008643 if (pat == NULL)
8644 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008645 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008646 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8649 return NULL;
8650
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008651 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008652 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8653 return NULL;
8654 }
8655
Bram Moolenaar69f70502021-01-01 16:10:46 +01008656 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 return NULL;
8658
8659 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8660 return NULL;
8661 return p;
8662}
8663
8664 static char_u *
8665compile_finally(char_u *arg, cctx_T *cctx)
8666{
8667 scope_T *scope = cctx->ctx_scope;
8668 garray_T *instr = &cctx->ctx_instr;
8669 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008670 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671
Bram Moolenaarfa984412021-03-25 22:15:28 +01008672 if (misplaced_cmdmod(cctx))
8673 return NULL;
8674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008675 // end block scope from :try or :catch
8676 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8677 compile_endblock(cctx);
8678 scope = cctx->ctx_scope;
8679
8680 // Error if not in a :try scope
8681 if (scope == NULL || scope->se_type != TRY_SCOPE)
8682 {
8683 emsg(_(e_finally));
8684 return NULL;
8685 }
8686
rbtnn84934992021-08-07 13:26:53 +02008687 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688 {
rbtnn84934992021-08-07 13:26:53 +02008689 // End :catch or :finally scope: set value in ISN_TRY instruction
8690 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8691 if (isn->isn_arg.try.try_ref->try_finally != 0)
8692 {
8693 emsg(_(e_finally_dup));
8694 return NULL;
8695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696
rbtnn84934992021-08-07 13:26:53 +02008697 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008698#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008699 if (cctx->ctx_compile_type == CT_PROFILE
8700 && ((isn_T *)instr->ga_data)[this_instr - 1]
8701 .isn_type == ISN_PROF_START)
8702 {
8703 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008704 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008705
8706 // jump to the profile end above it
8707 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8708 .isn_type == ISN_PROF_END)
8709 --this_instr;
8710 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008711#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008712
rbtnn84934992021-08-07 13:26:53 +02008713 // Fill in the "end" label in jumps at the end of the blocks.
8714 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8715 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008716
rbtnn84934992021-08-07 13:26:53 +02008717 // If there is no :catch then an exception jumps to :finally.
8718 if (isn->isn_arg.try.try_ref->try_catch == 0)
8719 isn->isn_arg.try.try_ref->try_catch = this_instr;
8720 isn->isn_arg.try.try_ref->try_finally = this_instr;
8721 if (scope->se_u.se_try.ts_catch_label != 0)
8722 {
8723 // Previous catch without match jumps here
8724 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8725 isn->isn_arg.jump.jump_where = this_instr;
8726 scope->se_u.se_try.ts_catch_label = 0;
8727 }
8728 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8729 return NULL;
8730
8731 // TODO: set index in ts_finally_label jumps
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008733
8734 return arg;
8735}
8736
8737 static char_u *
8738compile_endtry(char_u *arg, cctx_T *cctx)
8739{
8740 scope_T *scope = cctx->ctx_scope;
8741 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008742 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743
Bram Moolenaarfa984412021-03-25 22:15:28 +01008744 if (misplaced_cmdmod(cctx))
8745 return NULL;
8746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 // end block scope from :catch or :finally
8748 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8749 compile_endblock(cctx);
8750 scope = cctx->ctx_scope;
8751
8752 // Error if not in a :try scope
8753 if (scope == NULL || scope->se_type != TRY_SCOPE)
8754 {
8755 if (scope == NULL)
8756 emsg(_(e_no_endtry));
8757 else if (scope->se_type == WHILE_SCOPE)
8758 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008759 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008760 emsg(_(e_endfor));
8761 else
8762 emsg(_(e_endif));
8763 return NULL;
8764 }
8765
Bram Moolenaarc150c092021-02-13 15:02:46 +01008766 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008767 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008768 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008769 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8770 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008771 {
8772 emsg(_(e_missing_catch_or_finally));
8773 return NULL;
8774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008775
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008776#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008777 if (cctx->ctx_compile_type == CT_PROFILE
8778 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008779 .isn_type == ISN_PROF_START)
8780 // move the profile start after "endtry" so that it's not counted when
8781 // the exception is rethrown.
8782 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008783#endif
8784
Bram Moolenaar69f70502021-01-01 16:10:46 +01008785 // Fill in the "end" label in jumps at the end of the blocks, if not
8786 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008787 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8788 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789
Bram Moolenaar69f70502021-01-01 16:10:46 +01008790 if (scope->se_u.se_try.ts_catch_label != 0)
8791 {
8792 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008793 isn_T *isn = ((isn_T *)instr->ga_data)
8794 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008795 isn->isn_arg.jump.jump_where = instr->ga_len;
8796 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008797 }
8798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799 compile_endblock(cctx);
8800
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008801 if (cctx->ctx_skip != SKIP_YES)
8802 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008803 // End :catch or :finally scope: set instruction index in ISN_TRY
8804 // instruction
8805 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008806 if (cctx->ctx_skip != SKIP_YES
8807 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8808 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008809#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008810 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008811 generate_instr(cctx, ISN_PROF_START);
8812#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008814 return arg;
8815}
8816
8817/*
8818 * compile "throw {expr}"
8819 */
8820 static char_u *
8821compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8822{
8823 char_u *p = skipwhite(arg);
8824
Bram Moolenaara5565e42020-05-09 15:44:01 +02008825 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008826 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008827 if (cctx->ctx_skip == SKIP_YES)
8828 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008829 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 return NULL;
8831 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8832 return NULL;
8833
8834 return p;
8835}
8836
Bram Moolenaarc3235272021-07-10 19:42:03 +02008837 static char_u *
8838compile_eval(char_u *arg, cctx_T *cctx)
8839{
8840 char_u *p = arg;
8841 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008842 long lnum = SOURCING_LNUM;
8843
8844 // find_ex_command() will consider a variable name an expression, assuming
8845 // that something follows on the next line. Check that something actually
8846 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008847 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008848
Bram Moolenaarc3235272021-07-10 19:42:03 +02008849 if (compile_expr0(&p, cctx) == FAIL)
8850 return NULL;
8851
8852 if (name_only && lnum == SOURCING_LNUM)
8853 {
8854 semsg(_(e_expression_without_effect_str), arg);
8855 return NULL;
8856 }
8857
8858 // drop the result
8859 generate_instr_drop(cctx, ISN_DROP, 1);
8860
8861 return skipwhite(p);
8862}
8863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008864/*
8865 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008866 * compile "echomsg expr"
8867 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008868 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008869 * compile "execute expr"
8870 */
8871 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008872compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008873{
8874 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008875 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008876 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008877 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008878 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008879 garray_T *stack = &cctx->ctx_type_stack;
8880 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008881
8882 for (;;)
8883 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008884 if (ends_excmd2(prev, p))
8885 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008886 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008887 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008888 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008889
8890 if (cctx->ctx_skip != SKIP_YES)
8891 {
8892 // check for non-void type
8893 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8894 if (type->tt_type == VAR_VOID)
8895 {
8896 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8897 return NULL;
8898 }
8899 }
8900
Bram Moolenaarad39c092020-02-26 18:23:43 +01008901 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008902 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008903 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008904 }
8905
Bram Moolenaare4984292020-12-13 14:19:25 +01008906 if (count > 0)
8907 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008908 long save_lnum = cctx->ctx_lnum;
8909
8910 // Use the line number where the command started.
8911 cctx->ctx_lnum = start_ctx_lnum;
8912
Bram Moolenaare4984292020-12-13 14:19:25 +01008913 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8914 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8915 else if (cmdidx == CMD_execute)
8916 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8917 else if (cmdidx == CMD_echomsg)
8918 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02008919 else if (cmdidx == CMD_echoconsole)
8920 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01008921 else
8922 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008923
8924 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 return p;
8927}
8928
8929/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008930 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008931 * instruction to compute it and return OK.
8932 * Otherwise return FAIL, the caller must deal with any range.
8933 */
8934 static int
8935compile_variable_range(exarg_T *eap, cctx_T *cctx)
8936{
8937 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8938 char_u *p = skipdigits(eap->cmd);
8939
8940 if (p == range_end)
8941 return FAIL;
8942 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8943}
8944
8945/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008946 * :put r
8947 * :put ={expr}
8948 */
8949 static char_u *
8950compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8951{
8952 char_u *line = arg;
8953 linenr_T lnum;
8954 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008955 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008956
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008957 eap->regname = *line;
8958
8959 if (eap->regname == '=')
8960 {
8961 char_u *p = line + 1;
8962
8963 if (compile_expr0(&p, cctx) == FAIL)
8964 return NULL;
8965 line = p;
8966 }
8967 else if (eap->regname != NUL)
8968 ++line;
8969
Bram Moolenaar08597872020-12-10 19:43:40 +01008970 if (compile_variable_range(eap, cctx) == OK)
8971 {
8972 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8973 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008974 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008975 {
8976 // Either no range or a number.
8977 // "errormsg" will not be set because the range is ADDR_LINES.
8978 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008979 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008980 return NULL;
8981 if (eap->addr_count == 0)
8982 lnum = -1;
8983 else
8984 lnum = eap->line2;
8985 if (above)
8986 --lnum;
8987 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008988
8989 generate_PUT(cctx, eap->regname, lnum);
8990 return line;
8991}
8992
8993/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008994 * A command that is not compiled, execute with legacy code.
8995 */
8996 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008997compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008998{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008999 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009000 char_u *p;
9001 int has_expr = FALSE;
9002 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009003 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009004
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009005 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009006 goto theend;
9007
Bram Moolenaare729ce22021-06-06 21:38:09 +02009008 // If there was a prececing command modifier, drop it and include it in the
9009 // EXEC command.
9010 if (cctx->ctx_has_cmdmod)
9011 {
9012 garray_T *instr = &cctx->ctx_instr;
9013 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9014
9015 if (isn->isn_type == ISN_CMDMOD)
9016 {
9017 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9018 ->cmod_filter_regmatch.regprog);
9019 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9020 --instr->ga_len;
9021 cctx->ctx_has_cmdmod = FALSE;
9022 }
9023 }
9024
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009025 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009026 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009027 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009028 int usefilter = FALSE;
9029
9030 has_expr = argt & (EX_XFILE | EX_EXPAND);
9031
9032 // If the command can be followed by a bar, find the bar and truncate
9033 // it, so that the following command can be compiled.
9034 // The '|' is overwritten with a NUL, it is put back below.
9035 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9036 && *eap->arg == '!')
9037 // :w !filter or :r !filter or :r! filter
9038 usefilter = TRUE;
9039 if ((argt & EX_TRLBAR) && !usefilter)
9040 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009041 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009042 separate_nextcmd(eap);
9043 if (eap->nextcmd != NULL)
9044 nextcmd = eap->nextcmd;
9045 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009046 else if (eap->cmdidx == CMD_wincmd)
9047 {
9048 p = eap->arg;
9049 if (*p != NUL)
9050 ++p;
9051 if (*p == 'g' || *p == Ctrl_G)
9052 ++p;
9053 p = skipwhite(p);
9054 if (*p == '|')
9055 {
9056 *p = NUL;
9057 nextcmd = p + 1;
9058 }
9059 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009060 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9061 {
9062 // If there is a trailing '{' read lines until the '}'
9063 p = eap->arg + STRLEN(eap->arg) - 1;
9064 while (p > eap->arg && VIM_ISWHITE(*p))
9065 --p;
9066 if (*p == '{')
9067 {
9068 exarg_T ea;
9069 int flags; // unused
9070 int start_lnum = SOURCING_LNUM;
9071
9072 CLEAR_FIELD(ea);
9073 ea.arg = eap->arg;
9074 fill_exarg_from_cctx(&ea, cctx);
9075 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9076 if (tofree != NULL)
9077 {
9078 *p = NUL;
9079 line = concat_str(line, tofree);
9080 if (line == NULL)
9081 goto theend;
9082 vim_free(tofree);
9083 tofree = line;
9084 SOURCING_LNUM = start_lnum;
9085 }
9086 }
9087 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009088 }
9089
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009090 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9091 {
9092 // expand filename in "syntax include [@group] filename"
9093 has_expr = TRUE;
9094 eap->arg = skipwhite(eap->arg + 7);
9095 if (*eap->arg == '@')
9096 eap->arg = skiptowhite(eap->arg);
9097 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009098
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009099 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9100 && STRLEN(eap->arg) > 4)
9101 {
9102 int delim = *eap->arg;
9103
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009104 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009105 if (*p == delim)
9106 {
9107 eap->arg = p + 1;
9108 has_expr = TRUE;
9109 }
9110 }
9111
Bram Moolenaarecac5912021-01-05 19:23:28 +01009112 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
9113 {
9114 // TODO: should only expand when appropriate for the command
9115 eap->arg = skiptowhite(eap->arg);
9116 has_expr = TRUE;
9117 }
9118
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009119 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009120 {
9121 int count = 0;
9122 char_u *start = skipwhite(line);
9123
9124 // :cmd xxx`=expr1`yyy`=expr2`zzz
9125 // PUSHS ":cmd xxx"
9126 // eval expr1
9127 // PUSHS "yyy"
9128 // eval expr2
9129 // PUSHS "zzz"
9130 // EXECCONCAT 5
9131 for (;;)
9132 {
9133 if (p > start)
9134 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009135 char_u *val = vim_strnsave(start, p - start);
9136
9137 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009138 ++count;
9139 }
9140 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009141 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009142 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009143 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009144 ++count;
9145 p = skipwhite(p);
9146 if (*p != '`')
9147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009148 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009149 return NULL;
9150 }
9151 start = p + 1;
9152
9153 p = (char_u *)strstr((char *)start, "`=");
9154 if (p == NULL)
9155 {
9156 if (*skipwhite(start) != NUL)
9157 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009158 char_u *val = vim_strsave(start);
9159
9160 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009161 ++count;
9162 }
9163 break;
9164 }
9165 }
9166 generate_EXECCONCAT(cctx, count);
9167 }
9168 else
Bram Moolenaaraacc9662021-08-13 19:40:51 +02009169 generate_EXEC(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009170
9171theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009172 if (*nextcmd != NUL)
9173 {
9174 // the parser expects a pointer to the bar, put it back
9175 --nextcmd;
9176 *nextcmd = '|';
9177 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009178 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009179
9180 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009181}
9182
Bram Moolenaar20677332021-06-06 17:02:53 +02009183/*
9184 * A script command with heredoc, e.g.
9185 * ruby << EOF
9186 * command
9187 * EOF
9188 * Has been turned into one long line with NL characters by
9189 * get_function_body():
9190 * ruby << EOF<NL> command<NL>EOF
9191 */
9192 static char_u *
9193compile_script(char_u *line, cctx_T *cctx)
9194{
9195 if (cctx->ctx_skip != SKIP_YES)
9196 {
9197 isn_T *isn;
9198
9199 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9200 return NULL;
9201 isn->isn_arg.string = vim_strsave(line);
9202 }
9203 return (char_u *)"";
9204}
9205
Bram Moolenaar8238f082021-04-20 21:10:48 +02009206
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009207/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009208 * :s/pat/repl/
9209 */
9210 static char_u *
9211compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9212{
9213 char_u *cmd = eap->arg;
9214 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9215
9216 if (expr != NULL)
9217 {
9218 int delimiter = *cmd++;
9219
9220 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009221 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009222 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9223 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009224 garray_T save_ga = cctx->ctx_instr;
9225 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009226 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009227 int trailing_error;
9228 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009229 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009230 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009231
9232 cmd += 3;
9233 end = skip_substitute(cmd, delimiter);
9234
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009235 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009236 // labels are correct.
9237 cctx->ctx_instr.ga_len = 0;
9238 cctx->ctx_instr.ga_maxlen = 0;
9239 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009240 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009241 if (end[-1] == NUL)
9242 end[-1] = delimiter;
9243 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009244 trailing_error = *cmd != delimiter && *cmd != NUL;
9245
Bram Moolenaar169502f2021-04-21 12:19:35 +02009246 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009247 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009248 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009249 if (trailing_error)
9250 semsg(_(e_trailing_arg), cmd);
9251 clear_instr_ga(&cctx->ctx_instr);
9252 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009253 return NULL;
9254 }
9255
Bram Moolenaar8238f082021-04-20 21:10:48 +02009256 // Move the generated instructions into the ISN_SUBSTITUTE
9257 // instructions, then restore the list of instructions before
9258 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009259 instr_count = cctx->ctx_instr.ga_len;
9260 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009261 instr[instr_count].isn_type = ISN_FINISH;
9262
9263 cctx->ctx_instr = save_ga;
9264 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9265 {
9266 int idx;
9267
9268 for (idx = 0; idx < instr_count; ++idx)
9269 delete_instr(instr + idx);
9270 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009271 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009272 }
9273 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9274 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009275
9276 // skip over flags
9277 if (*end == '&')
9278 ++end;
9279 while (ASCII_ISALPHA(*end) || *end == '#')
9280 ++end;
9281 return end;
9282 }
9283 }
9284
9285 return compile_exec(arg, eap, cctx);
9286}
9287
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009288 static char_u *
9289compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9290{
9291 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009292 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009293
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009294 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009295 {
9296 if (STRNCMP(arg, "END", 3) == 0)
9297 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009298 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009299 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009300 // First load the current variable value.
9301 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9302 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009303 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009304 }
9305
9306 // Gets the redirected text and put it on the stack, then store it
9307 // in the variable.
9308 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9309
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009310 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009311 generate_instr_drop(cctx, ISN_CONCAT, 1);
9312
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009313 if (lhs->lhs_has_index)
9314 {
9315 // Use the info in "lhs" to store the value at the index in the
9316 // list or dict.
9317 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9318 &t_string, cctx) == FAIL)
9319 return NULL;
9320 }
9321 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009322 return NULL;
9323
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009324 VIM_CLEAR(lhs->lhs_name);
9325 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009326 return arg + 3;
9327 }
9328 emsg(_(e_cannot_nest_redir));
9329 return NULL;
9330 }
9331
9332 if (arg[0] == '=' && arg[1] == '>')
9333 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009334 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009335
9336 // redirect to a variable is compiled
9337 arg += 2;
9338 if (*arg == '>')
9339 {
9340 ++arg;
9341 append = TRUE;
9342 }
9343 arg = skipwhite(arg);
9344
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009345 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009346 FALSE, FALSE, 1, cctx) == FAIL)
9347 return NULL;
9348 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009349 lhs->lhs_append = append;
9350 if (lhs->lhs_has_index)
9351 {
9352 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9353 if (lhs->lhs_whole == NULL)
9354 return NULL;
9355 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009356
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009357 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009358 }
9359
9360 // other redirects are handled like at script level
9361 return compile_exec(line, eap, cctx);
9362}
9363
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009364#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009365 static char_u *
9366compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9367{
9368 isn_T *isn;
9369 char_u *p;
9370
9371 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9372 if (isn == NULL)
9373 return NULL;
9374 isn->isn_arg.number = eap->cmdidx;
9375
9376 p = eap->arg;
9377 if (compile_expr0(&p, cctx) == FAIL)
9378 return NULL;
9379
9380 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9381 if (isn == NULL)
9382 return NULL;
9383 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9384 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9385 return NULL;
9386 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9387 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9388 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9389
9390 return p;
9391}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009392#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009393
Bram Moolenaar4c137212021-04-19 16:48:48 +02009394/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009395 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009396 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009397 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009398 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009399add_def_function(ufunc_T *ufunc)
9400{
9401 dfunc_T *dfunc;
9402
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009403 if (def_functions.ga_len == 0)
9404 {
9405 // The first position is not used, so that a zero uf_dfunc_idx means it
9406 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009407 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009408 return FAIL;
9409 ++def_functions.ga_len;
9410 }
9411
Bram Moolenaar09689a02020-05-09 22:50:08 +02009412 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009413 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009414 return FAIL;
9415 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9416 CLEAR_POINTER(dfunc);
9417 dfunc->df_idx = def_functions.ga_len;
9418 ufunc->uf_dfunc_idx = dfunc->df_idx;
9419 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009420 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009421 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009422 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009423 ++def_functions.ga_len;
9424 return OK;
9425}
9426
9427/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009428 * After ex_function() has collected all the function lines: parse and compile
9429 * the lines into instructions.
9430 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009431 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9432 * the return statement (used for lambda). When uf_ret_type is already set
9433 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009434 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009435 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009436 * This can be used recursively through compile_lambda(), which may reallocate
9437 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009438 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009439 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009440 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009441compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009442 ufunc_T *ufunc,
9443 int check_return_type,
9444 compiletype_T compile_type,
9445 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009447 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009448 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009450 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009451 cctx_T cctx;
9452 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009453 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009454 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 int ret = FAIL;
9456 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009457 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009458 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009459 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009460 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009461#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009462 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009463#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009464 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009465
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009466 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009467 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009468 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009469 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009470 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9471 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009472 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009473
9474 switch (compile_type)
9475 {
9476 case CT_PROFILE:
9477#ifdef FEAT_PROFILE
9478 instr_dest = dfunc->df_instr_prof; break;
9479#endif
9480 case CT_NONE: instr_dest = dfunc->df_instr; break;
9481 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9482 }
9483 if (instr_dest != NULL)
9484 // Was compiled in this mode before: Free old instructions.
9485 delete_def_function_contents(dfunc, FALSE);
9486 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009487 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009488 else
9489 {
9490 if (add_def_function(ufunc) == FAIL)
9491 return FAIL;
9492 new_def_function = TRUE;
9493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009494
Bram Moolenaar985116a2020-07-12 17:31:09 +02009495 ufunc->uf_def_status = UF_COMPILING;
9496
Bram Moolenaara80faa82020-04-12 19:37:17 +02009497 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009498
Bram Moolenaare99d4222021-06-13 14:01:26 +02009499 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009500 cctx.ctx_ufunc = ufunc;
9501 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009502 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009503 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9504 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9505 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9506 cctx.ctx_type_list = &ufunc->uf_type_list;
9507 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9508 instr = &cctx.ctx_instr;
9509
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009510 // Set the context to the function, it may be compiled when called from
9511 // another script. Set the script version to the most modern one.
9512 // The line number will be set in next_line_from_context().
9513 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009514 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9515
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009516 // Don't use the flag from ":legacy" here.
9517 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9518
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009519 // Make sure error messages are OK.
9520 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9521 if (do_estack_push)
9522 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009523 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009524
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009525 if (ufunc->uf_def_args.ga_len > 0)
9526 {
9527 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009528 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009529 int i;
9530 char_u *arg;
9531 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009532 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009533
9534 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009535 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009536 for (i = 0; i < count; ++i)
9537 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009538 garray_T *stack = &cctx.ctx_type_stack;
9539 type_T *val_type;
9540 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009541 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009542 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009543 int jump_instr_idx = instr->ga_len;
9544 isn_T *isn;
9545
9546 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9547 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9548 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009549
9550 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009551 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009552
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009553 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009554 r = compile_expr0(&arg, &cctx);
9555
Bram Moolenaar12bce952021-03-11 20:04:04 +01009556 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009557 goto erret;
9558
9559 // If no type specified use the type of the default value.
9560 // Otherwise check that the default value type matches the
9561 // specified type.
9562 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009563 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009564 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009565 {
9566 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009567 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009568 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009569 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009570 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009571 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009572
9573 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009574 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009575
9576 // set instruction index in JUMP_IF_ARG_SET to here
9577 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9578 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009579 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009580
9581 if (did_set_arg_type)
9582 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009583 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009584 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009585
9586 /*
9587 * Loop over all the lines of the function and generate instructions.
9588 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009589 for (;;)
9590 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009591 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009592 int starts_with_colon = FALSE;
9593 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009594 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009595
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009596 // Bail out on the first error to avoid a flood of errors and report
9597 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009598 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009599 goto erret;
9600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601 if (line != NULL && *line == '|')
9602 // the line continues after a '|'
9603 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009604 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009605 && !(*line == '#' && (line == cctx.ctx_line_start
9606 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009607 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009608 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009609 goto erret;
9610 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009611 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9612 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613 else
9614 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009615 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009616 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009617 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009618 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009619#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009620 if (cctx.ctx_skip != SKIP_YES)
9621 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009622#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009623 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009624 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009625 // Make a copy, splitting off nextcmd and removing trailing spaces
9626 // may change it.
9627 if (line != NULL)
9628 {
9629 line = vim_strsave(line);
9630 vim_free(line_to_free);
9631 line_to_free = line;
9632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009633 }
9634
Bram Moolenaara80faa82020-04-12 19:37:17 +02009635 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 ea.cmdlinep = &line;
9637 ea.cmd = skipwhite(line);
9638
Bram Moolenaarb2049902021-01-24 12:53:53 +01009639 if (*ea.cmd == '#')
9640 {
9641 // "#" starts a comment
9642 line = (char_u *)"";
9643 continue;
9644 }
9645
Bram Moolenaarf002a412021-01-24 13:34:18 +01009646#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009647 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9648 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009649 {
9650 may_generate_prof_end(&cctx, prof_lnum);
9651
9652 prof_lnum = cctx.ctx_lnum;
9653 generate_instr(&cctx, ISN_PROF_START);
9654 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009655#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009656 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9657 && cctx.ctx_skip != SKIP_YES)
9658 {
9659 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009660 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009661 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009662 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009663
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009664 // Some things can be recognized by the first character.
9665 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009666 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009667 case '}':
9668 {
9669 // "}" ends a block scope
9670 scopetype_T stype = cctx.ctx_scope == NULL
9671 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009673 if (stype == BLOCK_SCOPE)
9674 {
9675 compile_endblock(&cctx);
9676 line = ea.cmd;
9677 }
9678 else
9679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009680 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009681 goto erret;
9682 }
9683 if (line != NULL)
9684 line = skipwhite(ea.cmd + 1);
9685 continue;
9686 }
9687
9688 case '{':
9689 // "{" starts a block scope
9690 // "{'a': 1}->func() is something else
9691 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9692 {
9693 line = compile_block(ea.cmd, &cctx);
9694 continue;
9695 }
9696 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009697 }
9698
9699 /*
9700 * COMMAND MODIFIERS
9701 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009702 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009703 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9704 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009705 {
9706 if (errormsg != NULL)
9707 goto erret;
9708 // empty line or comment
9709 line = (char_u *)"";
9710 continue;
9711 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009712 generate_cmdmods(&cctx, &local_cmdmod);
9713 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009715 // Check if there was a colon after the last command modifier or before
9716 // the current position.
9717 for (p = ea.cmd; p >= line; --p)
9718 {
9719 if (*p == ':')
9720 starts_with_colon = TRUE;
9721 if (p < ea.cmd && !VIM_ISWHITE(*p))
9722 break;
9723 }
9724
Bram Moolenaarce024c32021-06-26 13:00:49 +02009725 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009726 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009727 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009728 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009729 if (checkforcmd(&ea.cmd, "call", 3))
9730 {
9731 if (*ea.cmd == '(')
9732 // not for "call()"
9733 ea.cmd = p;
9734 else
9735 ea.cmd = skipwhite(ea.cmd);
9736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009737
Bram Moolenaarce024c32021-06-26 13:00:49 +02009738 if (!starts_with_colon)
9739 {
9740 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009741
Bram Moolenaarce024c32021-06-26 13:00:49 +02009742 // Check for assignment after command modifiers.
9743 assign = may_compile_assignment(&ea, &line, &cctx);
9744 if (assign == OK)
9745 goto nextline;
9746 if (assign == FAIL)
9747 goto erret;
9748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 }
9750
9751 /*
9752 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009753 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009754 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009755 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009756 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009757 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009758 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009759 && (*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009760 && (starts_with_colon || !(*cmd == '\''
9761 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009762 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009763 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009764 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009765 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009766 if (!starts_with_colon)
9767 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009768 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009769 goto erret;
9770 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009771 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009772 if (ends_excmd2(line, ea.cmd))
9773 {
9774 // A range without a command: jump to the line.
9775 // TODO: compile to a more efficient command, possibly
9776 // calling parse_cmd_address().
9777 ea.cmdidx = CMD_SIZE;
9778 line = compile_exec(line, &ea, &cctx);
9779 goto nextline;
9780 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009781 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009782 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009783 p = find_ex_command(&ea, NULL,
9784 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009785 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009787 if (p == NULL)
9788 {
9789 if (cctx.ctx_skip != SKIP_YES)
9790 emsg(_(e_ambiguous_use_of_user_defined_command));
9791 goto erret;
9792 }
9793
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009794 // When using ":legacy cmd" always use compile_exec().
9795 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009796 {
9797 char_u *start = ea.cmd;
9798
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009799 switch (ea.cmdidx)
9800 {
9801 case CMD_if:
9802 case CMD_elseif:
9803 case CMD_else:
9804 case CMD_endif:
9805 case CMD_for:
9806 case CMD_endfor:
9807 case CMD_continue:
9808 case CMD_break:
9809 case CMD_while:
9810 case CMD_endwhile:
9811 case CMD_try:
9812 case CMD_catch:
9813 case CMD_finally:
9814 case CMD_endtry:
9815 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9816 goto erret;
9817 default: break;
9818 }
9819
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009820 // ":legacy return expr" needs to be handled differently.
9821 if (checkforcmd(&start, "return", 4))
9822 ea.cmdidx = CMD_return;
9823 else
9824 ea.cmdidx = CMD_legacy;
9825 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009827 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9828 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009829 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009830 {
9831 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009832 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009833 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009834 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009835 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009836 // CMD_var cannot happen, compile_assignment() above would be
9837 // used. Most likely an assignment to a non-existing variable.
9838 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009839 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009841 }
9842
Bram Moolenaar3988f642020-08-27 22:43:03 +02009843 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009844 && ea.cmdidx != CMD_elseif
9845 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009846 && ea.cmdidx != CMD_endif
9847 && ea.cmdidx != CMD_endfor
9848 && ea.cmdidx != CMD_endwhile
9849 && ea.cmdidx != CMD_catch
9850 && ea.cmdidx != CMD_finally
9851 && ea.cmdidx != CMD_endtry)
9852 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009853 emsg(_(e_unreachable_code_after_return));
9854 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009855 }
9856
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009857 p = skipwhite(p);
9858 if (ea.cmdidx != CMD_SIZE
9859 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9860 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009861 if (ea.cmdidx >= 0)
9862 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009863 if ((ea.argt & EX_BANG) && *p == '!')
9864 {
9865 ea.forceit = TRUE;
9866 p = skipwhite(p + 1);
9867 }
9868 }
9869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009870 switch (ea.cmdidx)
9871 {
9872 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009873 ea.arg = p;
9874 line = compile_nested_function(&ea, &cctx);
9875 break;
9876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009877 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009878 // TODO: should we allow this, e.g. to declare a global
9879 // function?
9880 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009881 goto erret;
9882
9883 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009884 line = compile_return(p, check_return_type,
9885 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009886 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887 break;
9888
9889 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009890 emsg(_(e_cannot_use_let_in_vim9_script));
9891 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009892 case CMD_var:
9893 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009894 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009895 case CMD_increment:
9896 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009898 if (line == p)
9899 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009900 break;
9901
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009902 case CMD_unlet:
9903 case CMD_unlockvar:
9904 case CMD_lockvar:
9905 line = compile_unletlock(p, &ea, &cctx);
9906 break;
9907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009908 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009909 emsg(_(e_import_can_only_be_used_in_script));
9910 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009911 break;
9912
9913 case CMD_if:
9914 line = compile_if(p, &cctx);
9915 break;
9916 case CMD_elseif:
9917 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009918 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009919 break;
9920 case CMD_else:
9921 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009922 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009923 break;
9924 case CMD_endif:
9925 line = compile_endif(p, &cctx);
9926 break;
9927
9928 case CMD_while:
9929 line = compile_while(p, &cctx);
9930 break;
9931 case CMD_endwhile:
9932 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009933 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009934 break;
9935
9936 case CMD_for:
9937 line = compile_for(p, &cctx);
9938 break;
9939 case CMD_endfor:
9940 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009941 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 break;
9943 case CMD_continue:
9944 line = compile_continue(p, &cctx);
9945 break;
9946 case CMD_break:
9947 line = compile_break(p, &cctx);
9948 break;
9949
9950 case CMD_try:
9951 line = compile_try(p, &cctx);
9952 break;
9953 case CMD_catch:
9954 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009955 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009956 break;
9957 case CMD_finally:
9958 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009959 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009960 break;
9961 case CMD_endtry:
9962 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009963 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009964 break;
9965 case CMD_throw:
9966 line = compile_throw(p, &cctx);
9967 break;
9968
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009969 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009970 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009971 break;
9972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009973 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009975 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009976 case CMD_echomsg:
9977 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +02009978 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009979 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009980 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009981
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009982 case CMD_put:
9983 ea.cmd = cmd;
9984 line = compile_put(p, &ea, &cctx);
9985 break;
9986
Bram Moolenaar4c137212021-04-19 16:48:48 +02009987 case CMD_substitute:
9988 if (cctx.ctx_skip == SKIP_YES)
9989 line = (char_u *)"";
9990 else
9991 {
9992 ea.arg = p;
9993 line = compile_substitute(line, &ea, &cctx);
9994 }
9995 break;
9996
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009997 case CMD_redir:
9998 ea.arg = p;
9999 line = compile_redir(line, &ea, &cctx);
10000 break;
10001
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010002 case CMD_cexpr:
10003 case CMD_lexpr:
10004 case CMD_caddexpr:
10005 case CMD_laddexpr:
10006 case CMD_cgetexpr:
10007 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010008#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010009 ea.arg = p;
10010 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010011#else
10012 ex_ni(&ea);
10013 line = NULL;
10014#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010015 break;
10016
Bram Moolenaarae616492020-07-28 20:07:27 +020010017 case CMD_append:
10018 case CMD_change:
10019 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010020 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010021 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010022 case CMD_xit:
10023 not_in_vim9(&ea);
10024 goto erret;
10025
Bram Moolenaar002262f2020-07-08 17:47:57 +020010026 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010027 if (cctx.ctx_skip != SKIP_YES)
10028 {
10029 semsg(_(e_invalid_command_str), ea.cmd);
10030 goto erret;
10031 }
10032 // We don't check for a next command here.
10033 line = (char_u *)"";
10034 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010035
Bram Moolenaar20677332021-06-06 17:02:53 +020010036 case CMD_lua:
10037 case CMD_mzscheme:
10038 case CMD_perl:
10039 case CMD_py3:
10040 case CMD_python3:
10041 case CMD_python:
10042 case CMD_pythonx:
10043 case CMD_ruby:
10044 case CMD_tcl:
10045 ea.arg = p;
10046 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010047 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010048 else
10049 // heredoc lines have been concatenated with NL
10050 // characters in get_function_body()
10051 line = compile_script(line, &cctx);
10052 break;
10053
10054 default:
10055 // Not recognized, execute with do_cmdline_cmd().
10056 ea.arg = p;
10057 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010058 break;
10059 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010060nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010061 if (line == NULL)
10062 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010063 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010064
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010065 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010066 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010068 if (cctx.ctx_type_stack.ga_len < 0)
10069 {
10070 iemsg("Type stack underflow");
10071 goto erret;
10072 }
10073 }
10074
10075 if (cctx.ctx_scope != NULL)
10076 {
10077 if (cctx.ctx_scope->se_type == IF_SCOPE)
10078 emsg(_(e_endif));
10079 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10080 emsg(_(e_endwhile));
10081 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10082 emsg(_(e_endfor));
10083 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010084 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010085 goto erret;
10086 }
10087
Bram Moolenaarefd88552020-06-18 20:50:10 +020010088 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010090 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10091 ufunc->uf_ret_type = &t_void;
10092 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010094 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010095 goto erret;
10096 }
10097
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010098 // Return void if there is no return at the end.
10099 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 }
10101
Bram Moolenaar599410c2021-04-10 14:03:43 +020010102 // When compiled with ":silent!" and there was an error don't consider the
10103 // function compiled.
10104 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010105 {
10106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10107 + ufunc->uf_dfunc_idx;
10108 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010109 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010110#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010111 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010112 {
10113 dfunc->df_instr_prof = instr->ga_data;
10114 dfunc->df_instr_prof_count = instr->ga_len;
10115 }
10116 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010117#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010118 if (cctx.ctx_compile_type == CT_DEBUG)
10119 {
10120 dfunc->df_instr_debug = instr->ga_data;
10121 dfunc->df_instr_debug_count = instr->ga_len;
10122 }
10123 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010124 {
10125 dfunc->df_instr = instr->ga_data;
10126 dfunc->df_instr_count = instr->ga_len;
10127 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010128 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010129 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010130 if (cctx.ctx_outer_used)
10131 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010132 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010134
10135 ret = OK;
10136
10137erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010138 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010139 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10141 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010142
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010143 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010144 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010145 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010146 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010147
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010148 // If using the last entry in the table and it was added above, we
10149 // might as well remove it.
10150 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010151 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010152 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010153 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010154 ufunc->uf_dfunc_idx = 0;
10155 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010156 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010157
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010158 while (cctx.ctx_scope != NULL)
10159 drop_scope(&cctx);
10160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010161 if (errormsg != NULL)
10162 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010163 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010164 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010165 }
10166
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010167 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10168 {
10169 if (ret == OK)
10170 {
10171 emsg(_(e_missing_redir_end));
10172 ret = FAIL;
10173 }
10174 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010175 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010176 }
10177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010178 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010179 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010180 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010181 if (do_estack_push)
10182 estack_pop();
10183
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010184 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010185 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010186 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010187 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010188 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010189}
10190
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010191 void
10192set_function_type(ufunc_T *ufunc)
10193{
10194 int varargs = ufunc->uf_va_name != NULL;
10195 int argcount = ufunc->uf_args.ga_len;
10196
10197 // Create a type for the function, with the return type and any
10198 // argument types.
10199 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10200 // The type is included in "tt_args".
10201 if (argcount > 0 || varargs)
10202 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010203 if (ufunc->uf_type_list.ga_itemsize == 0)
10204 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010205 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10206 argcount, &ufunc->uf_type_list);
10207 // Add argument types to the function type.
10208 if (func_type_add_arg_types(ufunc->uf_func_type,
10209 argcount + varargs,
10210 &ufunc->uf_type_list) == FAIL)
10211 return;
10212 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10213 ufunc->uf_func_type->tt_min_argcount =
10214 argcount - ufunc->uf_def_args.ga_len;
10215 if (ufunc->uf_arg_types == NULL)
10216 {
10217 int i;
10218
10219 // lambda does not have argument types.
10220 for (i = 0; i < argcount; ++i)
10221 ufunc->uf_func_type->tt_args[i] = &t_any;
10222 }
10223 else
10224 mch_memmove(ufunc->uf_func_type->tt_args,
10225 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10226 if (varargs)
10227 {
10228 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010229 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010230 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10231 }
10232 }
10233 else
10234 // No arguments, can use a predefined type.
10235 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10236 argcount, &ufunc->uf_type_list);
10237}
10238
10239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010240/*
10241 * Delete an instruction, free what it contains.
10242 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010243 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010244delete_instr(isn_T *isn)
10245{
10246 switch (isn->isn_type)
10247 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010248 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010249 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010250 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010251 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010252 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010253 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254 case ISN_LOADENV:
10255 case ISN_LOADG:
10256 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010257 case ISN_LOADT:
10258 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010259 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010260 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010261 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010262 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010263 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010264 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010265 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010266 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010267 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010268 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010269 case ISN_STOREW:
10270 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010271 vim_free(isn->isn_arg.string);
10272 break;
10273
Bram Moolenaar4c137212021-04-19 16:48:48 +020010274 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010275 {
10276 int idx;
10277 isn_T *list = isn->isn_arg.subs.subs_instr;
10278
10279 vim_free(isn->isn_arg.subs.subs_cmd);
10280 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10281 delete_instr(list + idx);
10282 vim_free(list);
10283 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010284 break;
10285
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010286 case ISN_INSTR:
10287 {
10288 int idx;
10289 isn_T *list = isn->isn_arg.instr;
10290
10291 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10292 delete_instr(list + idx);
10293 vim_free(list);
10294 }
10295 break;
10296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010297 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010298 case ISN_STORES:
10299 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010300 break;
10301
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010302 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010303 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010304 vim_free(isn->isn_arg.unlet.ul_name);
10305 break;
10306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010307 case ISN_STOREOPT:
10308 vim_free(isn->isn_arg.storeopt.so_name);
10309 break;
10310
10311 case ISN_PUSHBLOB: // push blob isn_arg.blob
10312 blob_unref(isn->isn_arg.blob);
10313 break;
10314
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010315 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010316#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010317 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010318#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010319 break;
10320
10321 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010322#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010323 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010324#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010325 break;
10326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010327 case ISN_UCALL:
10328 vim_free(isn->isn_arg.ufunc.cuf_name);
10329 break;
10330
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010331 case ISN_FUNCREF:
10332 {
10333 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10334 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010335 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010336
Bram Moolenaar077a4232020-12-22 18:33:27 +010010337 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10338 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010339 }
10340 break;
10341
10342 case ISN_DCALL:
10343 {
10344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10345 + isn->isn_arg.dfunc.cdf_idx;
10346
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010347 if (dfunc->df_ufunc != NULL
10348 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010349 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010350 }
10351 break;
10352
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010353 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010354 {
10355 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10356 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10357
10358 if (ufunc != NULL)
10359 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010360 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010361 func_ptr_unref(ufunc);
10362 }
10363
10364 vim_free(lambda);
10365 vim_free(isn->isn_arg.newfunc.nf_global);
10366 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010367 break;
10368
Bram Moolenaar5e654232020-09-16 15:22:00 +020010369 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010370 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010371 free_type(isn->isn_arg.type.ct_type);
10372 break;
10373
Bram Moolenaar02194d22020-10-24 23:08:38 +020010374 case ISN_CMDMOD:
10375 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10376 ->cmod_filter_regmatch.regprog);
10377 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10378 break;
10379
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010380 case ISN_LOADSCRIPT:
10381 case ISN_STORESCRIPT:
10382 vim_free(isn->isn_arg.script.scriptref);
10383 break;
10384
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010385 case ISN_TRY:
10386 vim_free(isn->isn_arg.try.try_ref);
10387 break;
10388
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010389 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010390 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010391 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10392 break;
10393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010394 case ISN_2BOOL:
10395 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010396 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397 case ISN_ADDBLOB:
10398 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010399 case ISN_ANYINDEX:
10400 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010401 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010402 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010403 case ISN_BLOBINDEX:
10404 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010405 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010406 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010407 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010408 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010409 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010410 case ISN_COMPAREANY:
10411 case ISN_COMPAREBLOB:
10412 case ISN_COMPAREBOOL:
10413 case ISN_COMPAREDICT:
10414 case ISN_COMPAREFLOAT:
10415 case ISN_COMPAREFUNC:
10416 case ISN_COMPARELIST:
10417 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010418 case ISN_COMPARESPECIAL:
10419 case ISN_COMPARESTRING:
10420 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010421 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010422 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010423 case ISN_DROP:
10424 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010425 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010426 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010427 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010428 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010429 case ISN_EXECCONCAT:
10430 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010431 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010432 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010433 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010434 case ISN_GETITEM:
10435 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010436 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010437 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010438 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010439 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010440 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010441 case ISN_LOADBDICT:
10442 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010443 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010444 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010445 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010446 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010447 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010448 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010449 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010450 case ISN_NEGATENR:
10451 case ISN_NEWDICT:
10452 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010453 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010454 case ISN_OPFLOAT:
10455 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010456 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010457 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010458 case ISN_PROF_END:
10459 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010460 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010461 case ISN_PUSHF:
10462 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010463 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010464 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010465 case ISN_REDIREND:
10466 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010467 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010468 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010469 case ISN_SHUFFLE:
10470 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010471 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010472 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010473 case ISN_STORENR:
10474 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010475 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010476 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010477 case ISN_STOREV:
10478 case ISN_STRINDEX:
10479 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010480 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010481 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010482 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010483 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010484 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010485 // nothing allocated
10486 break;
10487 }
10488}
10489
10490/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010491 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010492 */
10493 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010494delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010495{
10496 int idx;
10497
10498 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010499 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010500
10501 if (dfunc->df_instr != NULL)
10502 {
10503 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10504 delete_instr(dfunc->df_instr + idx);
10505 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010506 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010507 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010508 if (dfunc->df_instr_debug != NULL)
10509 {
10510 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10511 delete_instr(dfunc->df_instr_debug + idx);
10512 VIM_CLEAR(dfunc->df_instr_debug);
10513 dfunc->df_instr_debug = NULL;
10514 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010515#ifdef FEAT_PROFILE
10516 if (dfunc->df_instr_prof != NULL)
10517 {
10518 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10519 delete_instr(dfunc->df_instr_prof + idx);
10520 VIM_CLEAR(dfunc->df_instr_prof);
10521 dfunc->df_instr_prof = NULL;
10522 }
10523#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010524
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010525 if (mark_deleted)
10526 dfunc->df_deleted = TRUE;
10527 if (dfunc->df_ufunc != NULL)
10528 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010529}
10530
10531/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010532 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010533 * function, unless another user function still uses it.
10534 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010535 */
10536 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010537unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010538{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010539 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010540 {
10541 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10542 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010543
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010544 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010545 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010546 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010547 ufunc->uf_dfunc_idx = 0;
10548 if (dfunc->df_ufunc == ufunc)
10549 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010550 }
10551}
10552
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010553/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010554 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010555 */
10556 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010557link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010558{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010559 if (ufunc->uf_dfunc_idx > 0)
10560 {
10561 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10562 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010563
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010564 ++dfunc->df_refcount;
10565 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010566}
10567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010568#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010569/*
10570 * Free all functions defined with ":def".
10571 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010572 void
10573free_def_functions(void)
10574{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010575 int idx;
10576
10577 for (idx = 0; idx < def_functions.ga_len; ++idx)
10578 {
10579 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10580
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010581 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010582 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010583 }
10584
10585 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010586}
10587#endif
10588
10589
10590#endif // FEAT_EVAL